zidane/ durable/ testing
The conformance kit for durable storages, and the check that a scenario runs again the same way.
import { … } from 'zidane/durable/testing'src/durable/testing/index.ts
Use this entry point in the tests of a custom DurableStorage or of code that runs on the durable kernel. registerStorageConformance registers the storage contract's cases (DURABILITY.md section 4) with your test runner. The kit checks the receipts of every commit call, and compares random commit calls with the memory storage. reopen opens a second instance on the same data, as a restart does. Without it, the kit skips the persistence cases. assertReplayDeterministic runs a scenario and records its host calls. Then it runs the scenario again on fresh storage from the recorded host calls. With prefixes: true, it also runs the scenario once per crash point. It throws at the first commit or host call that differs. memoryFileSystem is an in-memory file system for jsonlStorage. Its fail option makes chosen calls throw. See durable tasks and hosts.
import { describe, it } from 'bun:test'
import { memoryStorage, openDurable } from 'zidane/durable'
import { assertReplayDeterministic, registerStorageConformance } from 'zidane/durable/testing'
registerStorageConformance('memory', { open: async () => memoryStorage() }, { describe, it })
it('a session makes the same commits when it runs again', async () => {
await assertReplayDeterministic(async ({ host, storage }) => {
const durable = await openDurable({ storage, host })
const session = await durable.session('s1')
await session.commit(tx => tx.appendEntry(session.rootConversationId, { text: 'hi' }))
const count = (await session.history()).length
await durable.dispose()
return count
}, async () => memoryStorage(), { prefixes: true })
})Functions#
assertReplayDeterministicFunction#
Run scenario recorded, then replayed on fresh storage from open, then
once per journal prefix (a crash at every entry, continued live). Throws on
any divergence. Throws when the full replay's outcome differs from the
recorded one.
function assertReplayDeterministic(
scenario: ReplayScenario,
open: () => Promise<DurableStorage>,
options?: { prefixes?: boolean },
): Promise<JournalEntry[]>Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| scenario | ReplayScenario | The run to check. It returns a JSON summary of its outcome. | |
| open | () => Promise<DurableStorage> | Opens a fresh, empty storage. Called once per run. The check does not close the storages. The caller closes what | |
| options? | { prefixes?: boolean } | {} |
|
Returns
Promise<JournalEntry[]>memoryFileSystemFunction#
A file system in memory.
function memoryFileSystem(options?: MemoryFileSystemOptions): MemoryFileSystemParameters
| Name | Type | Default | Description |
|---|---|---|---|
| options? | MemoryFileSystemOptions | {} | The map to share, the process identity, and the failure hook. Default: a new disk. |
Returns
recordingHostFunction#
A recording host: a deterministic test clock whose calls and commits go to journal.
function recordingHost(options?: { start?: number }): ReplayCheckParameters
| Name | Type | Default | Description |
|---|---|---|---|
| options? | { start?: number } | {} |
|
Returns
registerStorageConformanceFunction#
Register the suite under name.
function registerStorageConformance(
name: string,
target: ConformanceTarget,
runner: ConformanceRunner,
): voidParameters
| Name | Type | Description |
|---|---|---|
| name | string | Name of the suite, such as the backend's name. |
| target | ConformanceTarget | Opens the backend, and optionally reopens it on the same data. |
| runner | ConformanceRunner | Your test runner's |
replayingHostFunction#
A replaying host over journal. A divergence throws. Past the end it runs live.
function replayingHost(
journal: readonly JournalEntry[],
options?: { start?: number },
): ReplayCheck & { readonly diverged: Promise<never>; readonly position: number }Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| journal | readonly JournalEntry[] | The recorded journal to answer from. | |
| options? | { start?: number } | {} |
|
Returns
ReplayCheck & { readonly diverged: Promise<never>; readonly position: number }Classes#
HostCallInsideEffectErrorClass#
Thrown when an effect's body calls the host. A replay skips the body, so the call shifts every later journal entry.
class HostCallInsideEffectError extends ErrorConstructor
new HostCallInsideEffectError(effect: string, call: string)Parameters
| Name | Type | Description |
|---|---|---|
| effect | string | Name of the effect whose body made the call. |
| call | string | The host method it called: |
Returns
Properties
| Name | Type | Description |
|---|---|---|
| callreadonly | string | The host method it called: |
| effectreadonly | string | Name of the effect whose body made the call. |
ReplayDivergenceErrorClass#
A replayed call that is not the one the journal holds at its place.
class ReplayDivergenceError extends ErrorConstructor
new ReplayDivergenceError(
index: number,
expected: JournalEntry | undefined,
actual: unknown,
)Parameters
| Name | Type | Description |
|---|---|---|
| index | number | Position in the journal where the replay diverged. |
| expected | JournalEntry | undefined | The journal entry at that position, or undefined past the end. |
| actual | unknown | The call or commit the replay made instead. |
Returns
Properties
| Name | Type | Description |
|---|---|---|
| actualreadonly | unknown | The call or commit the replay made instead. |
| expectedreadonly | JournalEntry | undefined | The journal entry at that position, or undefined past the end. |
| indexreadonly | number | Position in the journal where the replay diverged. |
Interfaces#
ConformanceRunnerInterface#
The test functions the suite needs.
interface ConformanceRunnerProperties
| Name | Type | Description |
|---|---|---|
| describe | (name: string, body: () => void) => void | Your runner's |
| it | (name: string, body: () => Promise<void>) => void | Your runner's |
ConformanceTargetInterface#
A backend under test.
interface ConformanceTargetProperties
| Name | Type | Description |
|---|---|---|
| open | () => Promise<DurableStorage> | Open the backend on fresh, empty storage. |
| reopen? | (storage: DurableStorage) => Promise<DurableStorage> | Open a second instance on the same stored data, after |
MemoryFileSystemInterface#
A FileSystem in memory.
interface MemoryFileSystem extends FileSystemProperties
| Name | Type | Description |
|---|---|---|
| filesreadonly | Map<string, Uint8Array<ArrayBufferLike>> | The files, by path. Change or copy it freely between calls. |
listfrom FileSystem | (path: string) => Promise<string[]> | Names of the entries in the directory |
mkdirfrom FileSystem | (path: string) => Promise<void> | Create the directory |
openAppendfrom FileSystem | (path: string) => Promise<AppendFile> | Open a file for appending. Create it when it is missing. |
ownerreadonlyfrom FileSystem | LeaseOwner | This process, as the lock file of a storage directory names it. |
ownerDeadfrom FileSystem | (owner: LeaseOwner) => boolean | Whether the process that wrote a lock file is known to be gone. Another
writer then takes its lock over. |
readfrom FileSystem | (path: string, range?: ReadRange) => Promise<Uint8Array<ArrayBufferLike> | undefined> | The bytes of a file, or of |
removefrom FileSystem | (path: string) => Promise<void> | Remove a file. A missing file is not an error. |
renamefrom FileSystem | (from: string, to: string) => Promise<void> | Rename a file. The rename replaces |
sizefrom FileSystem | (path: string) => Promise<number | undefined> | Size of a file in bytes. |
syncDirfrom FileSystem | (path: string) => Promise<void> | Flush the directory's entries (a create, rename or remove) to the disk, where the platform can. |
truncatefrom FileSystem | (path: string, size: number) => Promise<void> | Cut a file down to |
writeFilefrom FileSystem | (path: string, data: Uint8Array, options: WriteFileOptions) => Promise<boolean> | Replace the file at |
MemoryFileSystemOptionsInterface#
Options of memoryFileSystem.
interface MemoryFileSystemOptionsProperties
| Name | Type | Description |
|---|---|---|
| fail? | (op: MemoryFsOp, path: string) => unknown | Called before each call. Return an error to make the call throw it and
change nothing. |
| files? | Map<string, Uint8Array<ArrayBufferLike>> | The files, by path. Pass another file system's map to share its disk. Default: a new, empty map. |
| owner? | LeaseOwner | This process as lock files name it. Default: host |
| ownerDead? | (owner: LeaseOwner) => boolean | Whether a lock's owner is gone. Default: never (a lock is always held). |
ReplayCheckInterface#
A host and a storage wrapper that share one journal.
interface ReplayCheckProperties
| Name | Type | Description |
|---|---|---|
| hostreadonly | DurableHost | The host to run the scenario on. |
| journalreadonly | JournalEntry[] | Every host call and commit this check made live, in order. A replaying check adds only what it ran past the journal's end. |
| wrap | (storage: DurableStorage) => DurableStorage | Wrap a storage. A recording check writes its commits to the journal. A replaying check compares them with the journal. |
Types#
JournalEntryType#
MemoryFsOpType#
A call of a MemoryFileSystem that MemoryFileSystemOptions.fail can fail.
type MemoryFsOp =
| 'mkdir'
| 'list'
| 'read'
| 'size'
| 'writeFile'
| 'openAppend'
| 'append'
| 'sync'
| 'close'
| 'truncate'
| 'rename'
| 'remove'
| 'syncDir'ReplayScenarioType#
A scenario the check runs twice. It gets a host and a storage, and returns a JSON summary of its outcome.
type ReplayScenario = (env: { host: DurableHost; storage: DurableStorage }) => Promise<Json>