Skip to content

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.

TypeScript
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.

TypeScript
function assertReplayDeterministic(
  scenario: ReplayScenario,
  open: () => Promise<DurableStorage>,
  options?: { prefixes?: boolean },
): Promise<JournalEntry[]>
Parameters
NameTypeDefaultDescription
scenarioReplayScenario

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 open opens.

options?{ prefixes?: boolean }{}

prefixes: true also replays every journal prefix, a crash at each entry.

Promise<JournalEntry[]>

src/durable/testing/replay-check.ts:205

memoryFileSystemFunction#

A file system in memory.

TypeScript
function memoryFileSystem(options?: MemoryFileSystemOptions): MemoryFileSystem
Parameters
NameTypeDefaultDescription
options?MemoryFileSystemOptions{}

The map to share, the process identity, and the failure hook. Default: a new disk.

src/durable/storage/fs-memory.ts:40

recordingHostFunction#

A recording host: a deterministic test clock whose calls and commits go to journal.

TypeScript
function recordingHost(options?: { start?: number }): ReplayCheck
Parameters
NameTypeDefaultDescription
options?{ start?: number }{}

start sets the clock's first value.

src/durable/testing/replay-check.ts:79

registerStorageConformanceFunction#

Register the suite under name.

TypeScript
function registerStorageConformance(
  name: string,
  target: ConformanceTarget,
  runner: ConformanceRunner,
): void
Parameters
NameTypeDescription
namestring

Name of the suite, such as the backend's name.

targetConformanceTarget

Opens the backend, and optionally reopens it on the same data.

runnerConformanceRunner

Your test runner's describe and it.

src/durable/testing/storage-conformance.ts:23

replayingHostFunction#

A replaying host over journal. A divergence throws. Past the end it runs live.

TypeScript
function replayingHost(
  journal: readonly JournalEntry[],
  options?: { start?: number },
): ReplayCheck & { readonly diverged: Promise<never>; readonly position: number }
Parameters
NameTypeDefaultDescription
journalreadonly JournalEntry[]

The recorded journal to answer from.

options?{ start?: number }{}

start sets the clock's first value when the journal holds no clock reading.

ReplayCheck & { readonly diverged: Promise<never>; readonly position: number }

src/durable/testing/replay-check.ts:120

Classes#

HostCallInsideEffectErrorClass#

Thrown when an effect's body calls the host. A replay skips the body, so the call shifts every later journal entry.

TypeScript
class HostCallInsideEffectError extends Error
TypeScript
new HostCallInsideEffectError(effect: string, call: string)
Parameters
NameTypeDescription
effectstring

Name of the effect whose body made the call.

callstring

The host method it called: now, id or effect.

HostCallInsideEffectError properties
NameTypeDescription
callreadonlystring

The host method it called: now, id or effect.

effectreadonlystring

Name of the effect whose body made the call.

src/durable/testing/replay-check.ts:58

ReplayDivergenceErrorClass#

A replayed call that is not the one the journal holds at its place.

TypeScript
class ReplayDivergenceError extends Error
TypeScript
new ReplayDivergenceError(
  index: number,
  expected: JournalEntry | undefined,
  actual: unknown,
)
Parameters
NameTypeDescription
indexnumber

Position in the journal where the replay diverged.

expectedJournalEntry | undefined

The journal entry at that position, or undefined past the end.

actualunknown

The call or commit the replay made instead.

ReplayDivergenceError properties
NameTypeDescription
actualreadonlyunknown

The call or commit the replay made instead.

expectedreadonlyJournalEntry | undefined

The journal entry at that position, or undefined past the end.

indexreadonlynumber

Position in the journal where the replay diverged.

src/durable/testing/replay-check.ts:27

Interfaces#

ConformanceRunnerInterface#

The test functions the suite needs.

TypeScript
interface ConformanceRunner
ConformanceRunner properties
NameTypeDescription
describe(name: string, body: () => void) => void

Your runner's describe: groups cases under name.

it(name: string, body: () => Promise<void>) => void

Your runner's it: registers one async case.

src/durable/testing/storage-conformance-fixtures.ts:18

ConformanceTargetInterface#

A backend under test.

TypeScript
interface ConformanceTarget
ConformanceTarget properties
NameTypeDescription
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 storage closed (a restart). Without it, the suite skips the persistence cases.

src/durable/testing/storage-conformance-fixtures.ts:10

MemoryFileSystemInterface#

A FileSystem in memory.

TypeScript
interface MemoryFileSystem extends FileSystem
MemoryFileSystem properties
NameTypeDescription
filesreadonlyMap<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 path. Empty when the directory is missing.

mkdirfrom FileSystem(path: string) => Promise<void>

Create the directory path and its missing parents.

openAppendfrom FileSystem(path: string) => Promise<AppendFile>

Open a file for appending. Create it when it is missing.

ownerreadonlyfrom FileSystemLeaseOwner

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. false when this file system cannot tell, for example a process of another host.

readfrom FileSystem(path: string, range?: ReadRange) => Promise<Uint8Array<ArrayBufferLike> | undefined>

The bytes of a file, or of range of it. undefined when the file is missing.

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 to atomically when to exists.

sizefrom FileSystem(path: string) => Promise<number | undefined>

Size of a file in bytes. undefined when the file is missing.

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 size bytes.

writeFilefrom FileSystem(path: string, data: Uint8Array, options: WriteFileOptions) => Promise<boolean>

Replace the file at path with data. Resolves false only for an exclusive write over an existing file.

src/durable/storage/fs-memory.ts:31

MemoryFileSystemOptionsInterface#

Options of memoryFileSystem.

TypeScript
interface MemoryFileSystemOptions
MemoryFileSystemOptions properties
NameTypeDescription
fail?(op: MemoryFsOp, path: string) => unknown

Called before each call. Return an error to make the call throw it and change nothing. undefined lets the call run.

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 memory, pid 1 and a token of this file system's own.

ownerDead?(owner: LeaseOwner) => boolean

Whether a lock's owner is gone. Default: never (a lock is always held).

src/durable/storage/fs-memory.ts:16

ReplayCheckInterface#

A host and a storage wrapper that share one journal.

TypeScript
interface ReplayCheck
ReplayCheck properties
NameTypeDescription
hostreadonlyDurableHost

The host to run the scenario on.

journalreadonlyJournalEntry[]

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.

src/durable/testing/replay-check.ts:40

Types#

JournalEntryType#

One journal entry.

TypeScript
type JournalEntry =
  | { kind: 'now'; value: number }
  | { kind: 'id'; value: string }
  | { error?: ReturnType<typeof errorRecord>; kind: 'effect'; name: string; value?: Json }
  | { batches: Batch[]; kind: 'commit' }

src/durable/testing/replay-check.ts:20

MemoryFsOpType#

A call of a MemoryFileSystem that MemoryFileSystemOptions.fail can fail.

TypeScript
type MemoryFsOp =
  | 'mkdir'
  | 'list'
  | 'read'
  | 'size'
  | 'writeFile'
  | 'openAppend'
  | 'append'
  | 'sync'
  | 'close'
  | 'truncate'
  | 'rename'
  | 'remove'
  | 'syncDir'

src/durable/storage/fs-memory.ts:13

ReplayScenarioType#

A scenario the check runs twice. It gets a host and a storage, and returns a JSON summary of its outcome.

TypeScript
type ReplayScenario = (env: { host: DurableHost; storage: DurableStorage }) => Promise<Json>

src/durable/testing/replay-check.ts:194