Testing
Testing backend code against an in-memory engine, and client code against a local server.
This page shows how to test code that uses the packages: backend code that calls the engine SDK, with an in-memory stand-in for the server's HTTP API, and browser code that opens documents, against a server running on your machine.
Backend code
@voidhash/mimic-server/testing provides createTestEngine: the server's
HTTP API for documents, in memory, behind a fetch function. Give that
fetch to an EngineSDK, and the code under test runs as it would against
mimicd, without a server.
import * as Option from "effect/Option";
import { Primitive } from "@voidhash/mimic-core";
import { EngineSDK } from "@voidhash/mimic-server/engine";
import { createTestEngine } from "@voidhash/mimic-server/testing";
import { expect, it } from "vitest";
const Note = Primitive.Struct({
title: Primitive.String().required(),
done: Primitive.Boolean().default(false),
}).required();
it("ticks a note off", async () => {
const engine = createTestEngine();
const database = engine.createDatabase();
const notes = engine.createCollection(database, { name: "notes", schema: Note.schema });
const sdk = new EngineSDK({
url: engine.url,
credential: engine.createKey(database, "write"),
fetch: engine.fetch,
});
const collection = sdk.database(database).collection(notes, { primitive: Note });
await collection.createFrom("note-1", { title: "Milk" });
await collection.update("note-1", (root) => root.done.set(true));
const read = await collection.get("note-1");
expect(Option.getOrUndefined(read.snapshot)).toEqual({ title: "Milk", done: true });
await sdk.dispose();
});The test engine evaluates requests by the server's rules: exact and rebased transactions, schema validation, transaction ids answered from their recorded outcome, listings, minted document tokens, and presence leases, with the server's error codes and statuses.
| Method | Does |
|---|---|
createDatabase(id?) | Creates a database, or finds it; returns its id |
createCollection(database, { name, schema, id? }) | Creates a collection at schema version 1; returns its id |
createKey(database, permission) | Creates a database key; returns its secret |
state(database, collection, document) | The document's state as the engine holds it, for assertions |
leases(database, collection, document) | The document's live presence leases |
sessionsUrl | The address sessions open at, for createSocket |
createSocket(url) | A socket to the engine's sessions, in memory |
settled() | Resolves once every frame in flight is delivered |
It serves sessions (protocol 2) in memory: edits are applied and sent to
every session of the document, presence and leases are members, and a
client reconnecting resumes where it left off. It does not model scoped
sessions, a capability's expiry closing a session, migrations or schema
versions past 1, imports and repairs, partitions, quotas, rate and size
limits, origins, or the retry horizon; set the database up through the
methods above rather than the administration API. createTestEngine({ clock }) runs it on an Effect Clock you control, to test token and lease
expiry.
Client code
A sync document opens against the test engine with its sessionsUrl and
createSocket:
import { createSyncClientDocument } from "@voidhash/mimic/sync";
const doc = createSyncClientDocument({
primitive: Note,
url: engine.sessionsUrl,
createSocket: engine.createSocket,
database,
collection,
document: "note-1",
credential: () => key,
});
await doc.connect();
await engine.settled();A sync document needs a session to hold a value: a test's "offline"
document is one connected to the test engine. To test against what the test
engine does not model, run mimicd on your machine over a PostgreSQL
database of its own (version 16 or later), which starts in seconds:
createdb mimic_test
export MIMIC_POSTGRES_URL='postgres://localhost:5432/mimic_test'
MIMIC_BOOTSTRAP_PASSWORD=… mimicd serve \
--deployment-name test \
--listen.public 127.0.0.1:7700 \
--bootstrap-admin adminIt is ready once http://127.0.0.1:7702/readyz answers 200. Set up a
database, a collection and a key through the administration API as in the
quickstart, then open documents with
createSyncClientDocument and wait for them to settle:
import type { SyncClientDocument } from "@voidhash/mimic/sync";
const settled = (document: SyncClientDocument<typeof Note>) =>
document.isConnected() && document.isReady() && !document.hasPendingChanges();Two client documents on the same document, edited before either has settled, are the way to test how your code behaves under concurrent edits. Stopping and starting the server tests reconnection and the sending of edits made while offline.
createSyncClientDocument also takes createSocket and clock options, to
supply the WebSocket and the timers yourself, for example to drive the client
from a test of your own.
Code that uses a store
The zustand middleware, the
command layer and the
history work with any client document. Pass
{ autoConnect: false } to the mimic middleware when the test connects the
document itself.