runtime/mock.ts

Mock RuntimeDeps for testing.

Provides a fully controllable runtime implementation for unit tests. Consumer projects can extend MockRuntime with project-specific helpers (e.g. setup_mock_zap_config) that stay local.

view source

Declarations
#

10 declarations

create_mock_program
#

runtime/mock.ts view source

(respond?: (args: readonly string[]) => ProgramOutput | Promise<ProgramOutput>): MockProgram import {create_mock_program} from '@fuzdev/fuz_app/runtime/mock.js';

Create a fake Program for testing a helper that runs one.

Each call is recorded on calls, then answered by respond; with no respond, every call exits 0 with empty output. respond may throw, as a program that can't start does.

respond?

the output for a call's arguments

type (args: readonly string[]) => ProgramOutput | Promise<ProgramOutput>
optional default () => mock_program_exit(0)

returns

MockProgram

the fake program

examples

const git = create_mock_program((args) => args[0] === 'status' ? mock_program_exit(0, '', 'clean') : mock_program_exit(1, 'unknown') ); assert.strictEqual((await git.out(['status'])).stdout, 'clean'); assert.deepStrictEqual(git.calls, [['status']]);

create_mock_program_factory
#

runtime/mock.ts view source

(program?: MockProgram | null): MockProgramFactory import {create_mock_program_factory} from '@fuzdev/fuz_app/runtime/mock.js';

Create a fake ProgramFactory for testing a helper that makes its program with extra environment: every call records its env and returns program.

program?

the program to hand out, null for one that isn't installed

type MockProgram | null
optional default create_mock_program()

returns

MockProgramFactory

the fake factory

examples

const createdb = create_mock_program_factory(); await create_database({createdb}, 'postgres://app:pw@localhost/app'); assert.deepStrictEqual(createdb.envs, [{PGPASSWORD: 'pw'}]); assert.strictEqual(createdb.program?.calls.length, 1);

create_mock_runtime
#

runtime/mock.ts view source

(args?: string[]): MockRuntime import {create_mock_runtime} from '@fuzdev/fuz_app/runtime/mock.js';

Create a mock RuntimeDeps for testing.

The mock exit records the code on exit_calls and throws MockExitError (so the never-returning contract holds in tests). fetch throws TypeError when no mock_fetch_responses pattern matches the request URL.

args?

type string[]
optional default []

returns

MockRuntime

MockRuntime with controllable state

examples

const runtime = create_mock_runtime(['apply', 'zap.ts']); runtime.mock_env.set('HOME', '/home/test'); runtime.mock_fs.set('/home/test/.app/config.json', '{}'); await some_function(runtime); assert.deepStrictEqual(runtime.exit_calls, [0]);

mock_program_exit
#

runtime/mock.ts view source

(code: number, stderr?: string, stdout?: string): ProgramOutput import {mock_program_exit} from '@fuzdev/fuz_app/runtime/mock.js';

A program output that exited with code.

code

the exit code

type number

stderr?

what the program wrote to stderr

type string
optional default ''

stdout?

what the program wrote to stdout

type string
optional default ''

returns

ProgramOutput

MockExitError
#

runtime/mock.ts view source

import {MockExitError} from '@fuzdev/fuz_app/runtime/mock.js';

Error thrown when mock runtime.exit() is called.

Tests can catch this to verify exit behavior.

inheritance

extends: Error

code

type number

readonly

constructor

type new (code: number): MockExitError

code

type number

MockProgram
#

runtime/mock.ts view source

MockProgram import type {MockProgram} from '@fuzdev/fuz_app/runtime/mock.js';

A Program fake with its calls recorded for assertions.

inheritance

extends: Program

calls

The arguments of each call, in order.

type (readonly string[])[]

readonly

MockProgramFactory
#

runtime/mock.ts view source

MockProgramFactory import type {MockProgramFactory} from '@fuzdev/fuz_app/runtime/mock.js';

A ProgramFactory fake that hands out one program and records each environment asked for.

inheritance

envs

The env of each call, in order.

type Readonly<Record<string, string>>[]

readonly

program

The program every call returns — null for one that isn't installed.

type MockProgram | null

readonly

MockRuntime
#

runtime/mock.ts view source

MockRuntime import type {MockRuntime} from '@fuzdev/fuz_app/runtime/mock.js';

Mock RuntimeDeps with observable state for assertions.

inheritance

extends: RuntimeDeps

mock_env

Mock environment variables.

type Map<string, string>

mock_fs

Mock file system (path -> content).

type Map<string, string>

mock_fs_bytes

Mock binary file system (path -> bytes).

type Map<string, Uint8Array<ArrayBufferLike>>

mock_fs_modes

Recorded POSIX modes (path -> mode), written by write_text_file's mode option. A file with no recorded mode reads as 0o644 (the default-umask hazard) — so read_secure_file refuses it unless the writer asked for 0o600 explicitly, mirroring the real runtimes.

type Map<string, number>

mock_dirs

Mock directories that exist.

type Set<string>

exit_calls

Exit calls recorded (exit codes).

type number[]

mock_processes

Processes probe_process finds (pid -> probe). A pid with no entry is 'not_found'.

type Map<number, "running" | "not_permitted">

stdout_writes

Stdout writes recorded.

type string[]

stdin_buffer

Stdin buffer for input simulation.

type Uint8Array<ArrayBufferLike> | null

fetch_calls

Fetch calls recorded.

type { input: string | URL | Request; init?: RequestInit | undefined; }[]

mock_fetch_responses

Mock fetch responses (URL substring -> Response).

type Map<string, Response>

reset_mock_runtime
#

runtime/mock.ts view source

(runtime: MockRuntime): void import {reset_mock_runtime} from '@fuzdev/fuz_app/runtime/mock.js';

Reset a mock runtime to initial state.

runtime

returns

void

mutates

  • runtime — clears all mock state (env, fs, modes, dirs, processes, exit/stdout/fetch call records, fetch responses, stdin buffer)

set_mock_stdin
#

runtime/mock.ts view source

(runtime: MockRuntime, input: string): void import {set_mock_stdin} from '@fuzdev/fuz_app/runtime/mock.js';

Set stdin buffer for simulating user input.

runtime

input

string to provide as stdin input

type string

returns

void

mutates

  • runtime.stdin_buffer

Depends on
#