RunicViewDriver<TModel> class

Namespace Runic.Application.Testing in Runic.Application.Testing 0.23.0

public sealed class RunicViewDriver<TModel> where TModel : class

Sends the Views protocol 4 requests of one TModel route by ViewModel member, as the generated TypeScript client does, on the transport's test connection.

Remarks

Each request has an asynchronous method, such as RunicViewDriver.SnapshotAsync and RunicViewDriver.SetAsync, that never blocks a thread. The synchronous methods, such as RunicViewDriver.Snapshot and RunicViewDriver.Set, block the calling thread until the reply, while the window's own asynchronous work continues on thread-pool threads. In a suite that runs many tests in parallel on thread-pool threads, each blocked test holds a thread that the replies it waits for need, which can starve the pool and slow the suite down; use the asynchronous methods there.

Example

var editor = await (await host.Root.ViewAsync<DocumentViewModel>(vm => vm.Main)).ViewAsync<EditorViewModel>(vm => vm.CurrentPane);
(await editor.SetAsync(vm => vm.Title, "Groceries")).ThrowIfFailed();
(await editor.ExecuteAsync(vm => vm.SaveCommand)).ThrowIfFailed();
(await editor.ExecuteAsync(vm => vm.RenameCommand, new RenameInput("Groceries", Pinned: true))).ThrowIfFailed();

Properties

ClientKey

public string ClientKey { get; }

The client of this driver's connection: the one passed to RunicViewDriver.ForConnection, else InMemoryBridgeTransport.DefaultClientKey.

Connection

public RunicConnection? Connection { get; }

The window's own connection for this driver's connection, as IRunicConnections reports it, while it is open; null before its first request and after it closed.

Remarks

The window gives each connection a key of its own, which no later connection reuses: on this transport, whose keys can repeat, it is RunicViewDriver.ConnectionKey with a #n suffix. Compare a command's IRunicConnections.Caller with it.

ConnectionKey

public string ConnectionKey { get; }

The connection this driver's requests use: InMemoryBridgeTransport.DefaultConnectionKey, or the one passed to RunicViewDriver.ForConnection.

Reference

public PageReference? Reference { get; }

The content reference this driver presents, or null for the root route.

Route

public string Route { get; }

The route: the root name, or content{id} for presented content.

Methods

CanExecute

public bool CanExecute(Expression<Func<TModel, object?>> command, string argumentJson)

Asks whether a command with an input can execute (can{Command}(input)).

Remarks

A command with an asynchronous key resolver answers only after the resolver finishes. A resolver that waits on the host's fake clock blocks this call; use RunicViewDriver.CanExecuteAsync.

CanExecuteAsync

public Task<bool> CanExecuteAsync(Expression<Func<TModel, object?>> command, string argumentJson, CancellationToken cancellationToken = default)

Asks whether a command with an input can execute (can{Command}(input)), waiting for an asynchronous key resolver.

Parameters

command

The command property, such as vm => vm.DeleteCommand.

argumentJson

The command input or key as wire JSON.

cancellationToken

Cancels the resolution, as a closed client connection does.

Exceptions

OperationCanceledException

cancellationToken was cancelled.

CanExecuteAsync

public Task<bool> CanExecuteAsync<TArgument>(Expression<Func<TModel, object?>> command, TArgument argument, CancellationToken cancellationToken = default)

Asks whether a command with an input can execute (can{Command}(input)), with the input encoded as RunicViewDriver.ExecuteAsync encodes it, waiting for an asynchronous key resolver.

Parameters

command

The command property, such as vm => vm.DeleteCommand.

argument

The command input or key.

cancellationToken

Cancels the resolution, as a closed client connection does.

Exceptions

OperationCanceledException

cancellationToken was cancelled.

CloseRegionAsync

public Task<RunicCallReply<TModel>> CloseRegionAsync(Expression<Func<TModel, object?>> collection, string key, CancellationToken cancellationToken = default)

Closes the region with key of a region collection and removes it, as the client's close{Name}(key) does (region-collections/1). Every entry of the region is asked to retire; RunicCallReply.RegionOutcome says how it ended.

Parameters

collection

The region collection property, such as vm => vm.Documents.

key

A region's key, from RunicViewState.Regions.

cancellationToken

Cancels the request, as a closed client connection does.

ExecuteAsync

public Task<RunicCallReply<TModel>> ExecuteAsync(Expression<Func<TModel, object?>> command, string? argumentJson = null, CancellationToken cancellationToken = default)

Executes a command, as the client's {command}(input) does, and waits for an asynchronous command to finish.

Parameters

command

The command property, such as vm => vm.SaveCommand.

argumentJson

The command input as wire JSON, for a command with an input. Use RunicViewDriver.ExecuteAsync to pass the input as a .NET value.

cancellationToken

Cancels an awaited command, as a closed client connection does.

Exceptions

ArgumentException

argumentJson is not one JSON value.

Remarks

A command that waits on the host's fake clock finishes only after the test advances it: start the call, advance RunicWindowTestHost.Time, then await the call.

ExecuteAsync

public Task<RunicCallReply<TModel>> ExecuteAsync<TArgument>(Expression<Func<TModel, object?>> command, TArgument argument, CancellationToken cancellationToken = default)

Executes a command with an input, as the client's {command}(input) does, and waits for an asynchronous command to finish. The input is encoded as the generated client encodes it.

Parameters

command

The command property, such as vm => vm.RenameCommand.

argument

The command input.

cancellationToken

Cancels an awaited command, as a closed client connection does.

Remarks

The input follows the Bridge's JSON rules: scalars as RunicViewDriver.SetAsync encodes them, a record or class as an object of its public properties by wire name (camelCase, or its [RunicAlias] or [JsonPropertyName], without [RunicIgnore] and, unless the assembly opts out, [JsonIgnore] members), a case of a [RunicUnion] with its $case, a list or array as an array, a dictionary with string keys as an object, and a type with a [RunicCodec] by its codec.

A String input selects the overload that takes wire JSON unless you name the type: ExecuteAsync<string>(vm => vm.SearchCommand, "groceries").

ForConnection

public RunicViewDriver<TModel> ForConnection(string connectionKey, string? clientKey = null)

Returns a driver of this route whose requests, and the drivers and operations it returns, use another page connection, as a second browser tab of a multi-client window does.

Parameters

connectionKey

The connection's key, as the transport would authenticate it.

clientKey

The client's key; by default InMemoryBridgeTransport.DefaultClientKey.

Returns

The driver of the same route on that connection.

Remarks

The connection sends its hello with its first request, and then is one of the window's IRunicConnections.Current; a command it executes runs with it as IRunicConnections.Caller. Close it with InMemoryBridgeTransport.CloseConnection.

var bob = host.Root.ForConnection("bob-tab", clientKey: "bob");
(await bob.ExecuteAsync(vm => vm.JoinCommand)).ThrowIfFailed();

IndexOfAsync

public Task<int> IndexOfAsync<TRow, TKey>(Expression<Func<TModel, RunicQueryCollection<TRow, TKey>>> collection, string key, CancellationToken cancellationToken = default) where TKey : notnull

Finds the position of a row in a query collection's current query, as a page's scrollToKey does (query-index).

Parameters

collection

The query collection property, such as vm => vm.Rows.

key

The row's wire key: the string, a lowercase GUID or an Int32 in decimal.

cancellationToken

Stops waiting for the reply.

Returns

The row's position, or -1 when no row has the key.

Mount

public string Mount(string? presentation = null, string? clientKey = null, string? connectionKey = null)

Opens a presentation of this route, as a page that shows the View sends open.

Parameters

presentation

The presentation id; the host issues test-presentation{n} when omitted.

clientKey

The client identity, for tests of several browser clients.

connectionKey

The connection identity, for tests of a client disconnect.

Returns

The presentation id; pass it to RunicViewDriver.Unmount.

Exceptions

InvalidOperationException

The open was not answered ok; the message names the reply.

MountAsync

public Task<string> MountAsync(string? presentation = null, string? clientKey = null, string? connectionKey = null, CancellationToken cancellationToken = default)

Opens a presentation of this route, as a page that shows the View sends open, without blocking a thread.

Parameters

presentation

The presentation id; the host issues test-presentation{n} when omitted.

clientKey

The client identity, for tests of several browser clients.

connectionKey

The connection identity, for tests of a client disconnect.

cancellationToken

Stops waiting for the reply.

Returns

The presentation id; pass it to RunicViewDriver.UnmountAsync.

Exceptions

InvalidOperationException

The open was not answered ok; the message names the reply.

MountPresentation

public RunicTestPresentation<TModel> MountPresentation(string? presentation = null, string? clientKey = null, string? connectionKey = null)

Opens a presentation of this route that acts as a page's View: it handles every ViewRequest of TModel, so RunicTestPresentation.TakeViewRequests returns the requests it receives, and answers the interactions its Handle registers. Blocks the calling thread until it is open.

Parameters

presentation

The presentation id; the host issues test-presentation{n} when omitted.

clientKey

The client identity, for tests of several browser clients.

connectionKey

The connection identity, for tests of a client disconnect.

Returns

The presentation; dispose it to stop answering, or unmount it.

Exceptions

InvalidOperationException

The open or the registration was not answered ok; the message names the reply.

MountPresentationAsync

public Task<RunicTestPresentation<TModel>> MountPresentationAsync(string? presentation = null, string? clientKey = null, string? connectionKey = null, CancellationToken cancellationToken = default)

Opens a presentation that acts as a page's View, as RunicViewDriver.MountPresentation does, without blocking a thread.

Parameters

presentation

The presentation id; the host issues test-presentation{n} when omitted.

clientKey

The client identity, for tests of several browser clients.

connectionKey

The connection identity, for tests of a client disconnect.

cancellationToken

Stops waiting for the replies.

Returns

The presentation; dispose it to stop answering, or unmount it.

Exceptions

InvalidOperationException

The open or the registration was not answered ok; the message names the reply.

NextPublicationAsync

public Task<RunicViewPublication> NextPublicationAsync(TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Waits for the next state or frame pushed to this route.

Parameters

timeout

Real time to wait; defaults to the host's publication timeout.

cancellationToken

Stops waiting.

Exceptions

TimeoutException

Nothing was pushed in time.

QueryRowsAsync

public Task<RunicQueryRows> QueryRowsAsync<TRow, TKey>(Expression<Func<TModel, RunicQueryCollection<TRow, TKey>>> collection, int start, int count, string? connectionKey = null, CancellationToken cancellationToken = default) where TKey : notnull

Reads rows of a RunicQueryCollection as a page's queries.{name}.setRange(start, count) does (query-rows), on the test connection or on connectionKey, as another page.

Parameters

collection

The query collection property, such as vm => vm.Rows.

start

The first row.

count

The number of rows, at most RunicQueryCollectionOptions.MaximumRangeCount.

connectionKey

The connection, or null for the test connection.

cancellationToken

Stops waiting for the reply.

Returns

The rows as the page receives them.

Exceptions

InvalidOperationException

The route answered with a failure.

SelectRegionAsync

public Task<RunicCallReply<TModel>> SelectRegionAsync(Expression<Func<TModel, object?>> collection, string key, CancellationToken cancellationToken = default)

Selects the region with key of a region collection, as the client's select{Name}(key) does (region-collections/1), and waits until the selection settled. The departure guards of the region left decide; RunicCallReply.RegionOutcome says how it ended.

Parameters

collection

The region collection property, such as vm => vm.Documents.

key

A region's key, from RunicViewState.Regions.

cancellationToken

Cancels the request, as a closed client connection does.

Set

public RunicCallReply<TModel> Set<TValue>(Expression<Func<TModel, TValue>> property, TValue value)

Sends the generated setter's set request with the next edit number of the member, blocking the calling thread until the reply. The value is encoded as its wire type, as RunicViewDriver.SetAsync encodes it.

Remarks

Prefer RunicViewDriver.SetAsync in suites that run tests in parallel; see the class remarks.

SetAsync

public Task<RunicCallReply<TModel>> SetAsync<TValue>(Expression<Func<TModel, TValue>> property, TValue value, CancellationToken cancellationToken = default)

Sends the generated setter's set request with the next edit number of the member, without blocking a thread. The value is encoded as its wire type: an Int64 or decimal as a string, an enum by name, null for a nullable value, and a DTO, list or union with the Bridge's JSON rules (see RunicViewDriver.ExecuteAsync). Use RunicViewDriver.SetJsonAsync for a value already encoded.

Parameters

property

The state property, such as vm => vm.Title.

value

The value the page sets.

cancellationToken

Stops waiting for the reply.

SetJson

public RunicCallReply<TModel> SetJson<TValue>(Expression<Func<TModel, TValue>> property, string wireJson)

Sends a set request with a value already encoded as wire JSON, blocking the calling thread.

Remarks

Prefer RunicViewDriver.SetJsonAsync in suites that run tests in parallel; see the class remarks.

SetJsonAsync

public Task<RunicCallReply<TModel>> SetJsonAsync<TValue>(Expression<Func<TModel, TValue>> property, string wireJson, CancellationToken cancellationToken = default)

Sends a set request with a value already encoded as wire JSON, without blocking a thread.

Parameters

property

The state property, such as vm => vm.Title.

wireJson

The value as wire JSON.

cancellationToken

Stops waiting for the reply.

SetQueryAsync

public Task<RunicCallReply<TModel>> SetQueryAsync<TRow, TKey>(Expression<Func<TModel, RunicQueryCollection<TRow, TKey>>> collection, RunicQuery query, CancellationToken cancellationToken = default) where TKey : notnull

Changes the query of a query collection as a page's setQuery does (query-set).

Parameters

collection

The query collection property, such as vm => vm.Rows.

query

The query.

cancellationToken

Stops waiting for the reply.

Returns

The reply, with the state after it.

Snapshot

public RunicViewState<TModel> Snapshot()

Reads the current state with a snapshot request, blocking the calling thread until the reply.

Exceptions

InvalidOperationException

The route answered with a failure, such as released.

Remarks

Prefer RunicViewDriver.SnapshotAsync in suites that run tests in parallel; see the class remarks.

SnapshotAsync

public Task<RunicViewState<TModel>> SnapshotAsync(CancellationToken cancellationToken = default)

Reads the current state with a snapshot request, without blocking a thread.

Parameters

cancellationToken

Stops waiting for the reply.

Exceptions

InvalidOperationException

The route answered with a failure, such as released.

Start

public RunicTestOperation Start(Expression<Func<TModel, object?>> command, string? argumentJson = null, string? requestId = null)

Starts an asynchronous command as a recoverable operation with a start request, blocking the calling thread until the admission.

Parameters

command

The command property.

argumentJson

The command input as wire JSON, for a command with an input.

requestId

The operation id; the host issues request{n} when omitted.

Remarks

Prefer RunicViewDriver.StartAsync in suites that run tests in parallel; see the class remarks.

StartAsync

public Task<RunicTestOperation> StartAsync(Expression<Func<TModel, object?>> command, string? argumentJson = null, string? requestId = null, CancellationToken cancellationToken = default)

Starts an asynchronous command as a recoverable operation with a start request, and completes with the admission. Await RunicTestOperation.WaitAsync for the outcome.

Parameters

command

The command property.

argumentJson

The command input or key as wire JSON, for a command with an input.

requestId

The operation id; the host issues request{n} when omitted.

cancellationToken

Cancels the resolution, as a closed client connection does.

StartAsync

public Task<RunicTestOperation> StartAsync<TArgument>(Expression<Func<TModel, object?>> command, TArgument argument, string? requestId = null, CancellationToken cancellationToken = default)

Starts an asynchronous command with an input as a recoverable operation with a start request, with the input encoded as RunicViewDriver.ExecuteAsync encodes it, and completes with the admission.

Parameters

command

The command property.

argument

The command input or key.

requestId

The operation id; the host issues request{n} when omitted.

cancellationToken

Cancels the resolution, as a closed client connection does.

TakePublications

public IReadOnlyList<RunicViewPublication> TakePublications()

Returns and removes the states and frames pushed to this route so far.

Track

public RunicStateTracker<TModel> Track()

Follows this route's state from a snapshot through its pushed states and frames, applying their changes as the client does and failing on a frame the client could not apply.

Remarks

Its first snapshot blocks the calling thread; prefer RunicViewDriver.TrackAsync in suites that run tests in parallel.

TrackAsync

public Task<RunicStateTracker<TModel>> TrackAsync(CancellationToken cancellationToken = default)

Follows this route's state from a snapshot through its pushed states and frames, as RunicViewDriver.Track does, reading the first snapshot without blocking a thread.

Parameters

cancellationToken

Stops waiting for the snapshot.

Unmount

public void Unmount(string presentation, string? clientKey = null, string? connectionKey = null)

Closes a presentation, as a page that stops showing the View sends close.

Parameters

presentation

The id RunicViewDriver.Mount returned.

clientKey

The client identity the presentation was opened with.

connectionKey

The connection identity the presentation was opened with.

Exceptions

InvalidOperationException

The route did not close it, for example because another client opened it (ignored) or the route was released (released). The message names the reply.

UnmountAsync

public Task UnmountAsync(string presentation, string? clientKey = null, string? connectionKey = null, CancellationToken cancellationToken = default)

Closes a presentation, as a page that stops showing the View sends close, without blocking a thread.

Parameters

presentation

The id RunicViewDriver.MountAsync returned.

clientKey

The client identity the presentation was opened with.

connectionKey

The connection identity the presentation was opened with.

cancellationToken

Stops waiting for the reply.

Exceptions

InvalidOperationException

The route did not close it; the message names the reply.

View

public RunicViewDriver<TContent> View<TContent>(Expression<Func<TModel, object?>> property) where TContent : class

Drives the content that a ViewModel content property presents now, reading it with a snapshot that blocks the calling thread.

Type parameters

TContent

The presented ViewModel type, checked against the reference's kind.

Remarks

Prefer RunicViewDriver.ViewAsync in suites that run tests in parallel; see the class remarks.

ViewAsync

public Task<RunicViewDriver<TContent>> ViewAsync<TContent>(Expression<Func<TModel, object?>> property, CancellationToken cancellationToken = default) where TContent : class

Drives the content that a ViewModel content property presents now, without blocking a thread.

Type parameters

TContent

The presented ViewModel type, checked against the reference's kind.

Parameters

property

The content property, such as vm => vm.Main.

cancellationToken

Stops waiting for the snapshot.

Views

public IReadOnlyList<RunicViewDriver<TContent>> Views<TContent>(Expression<Func<TModel, object?>> property) where TContent : class

Drives each item that a ViewModel content collection presents now, blocking the calling thread.

Remarks

Prefer RunicViewDriver.ViewsAsync in suites that run tests in parallel; see the class remarks.

ViewsAsync

public Task<IReadOnlyList<RunicViewDriver<TContent>>> ViewsAsync<TContent>(Expression<Func<TModel, object?>> property, CancellationToken cancellationToken = default) where TContent : class

Drives each item that a ViewModel content collection presents now, without blocking a thread.

Parameters

property

The content collection property, such as vm => vm.Tabs.

cancellationToken

Stops waiting for the snapshot.

Write

public RunicFieldWriteReceipt<TModel> Write<TValue>(Expression<Func<TModel, TValue>> property, TValue value, long? expectedVersion = null)

Writes a checked field, encoded as RunicViewDriver.Set encodes values, as the client's write{Property}(value, { requestId, baseline }) does, blocking the calling thread. The baseline is the current value and, unless given, its current version.

Remarks

Prefer RunicViewDriver.WriteAsync in suites that run tests in parallel; see the class remarks.

WriteAsync

public Task<RunicFieldWriteReceipt<TModel>> WriteAsync<TValue>(Expression<Func<TModel, TValue>> property, TValue value, long? expectedVersion = null, CancellationToken cancellationToken = default)

Writes a checked field, encoded as RunicViewDriver.SetAsync encodes values, as the client's write{Property}(value, { requestId, baseline }) does, without blocking a thread. The baseline is the current value and, unless given, its current version.

Parameters

property

The checked state property.

value

The value to write.

expectedVersion

The baseline version; by default the current one.

cancellationToken

Stops waiting for the replies.

WritePath

public RunicFieldWriteReceipt<TModel> WritePath<TValue>(Expression<Func<TModel, object?>> member, IReadOnlyList<string> path, TValue value, long? expectedVersion = null)

Writes a field path below a member (field-paths/1), as a generated field path descriptor such as InvoiceFields.lines.at(7).quantity does, blocking the calling thread. The path is in wire names: for a RunicCollectionAttribute collection, the row's key and then the cell, such as ["7", "quantity"]; for an object member, its members, such as ["street"]. The baseline is the path's value in the current state (null when the path reaches no value) and, unless given, the state's revision.

Remarks

Prefer RunicViewDriver.WritePathAsync in suites that run tests in parallel; see the class remarks.

WritePathAsync

public Task<RunicFieldWriteReceipt<TModel>> WritePathAsync<TValue>(Expression<Func<TModel, object?>> member, IReadOnlyList<string> path, TValue value, long? expectedVersion = null, CancellationToken cancellationToken = default)

Writes a field path below a member (field-paths/1), as RunicViewDriver.WritePath does, without blocking a thread. A cell that changed after expectedVersion is a RunicFieldWriteReceiptKind.Conflict.

Parameters

member

The object member or keyed collection, such as vm => vm.Lines.

path

The path below it in wire names: a row key and the cell, or the object's members.

value

The value to write.

expectedVersion

The baseline version; by default the state's revision.

cancellationToken

Stops waiting for the replies.