# zidane/contexts/testing

> The conformance kit for execution contexts, with cases for files, commands, aborts, streams, background tasks and host restarts.

Import: `import { … } from 'zidane/contexts/testing'`. Source: [src/contexts/testing/index.ts](https://github.com/Tahul/zidane/blob/main/src/contexts/testing/index.ts).

Use `registerContextConformance` in the tests of an execution context of your own, or to prove a built-in one on your machine. It registers the cases of the context contract with your test runner. The cases cover files, commands with their aborts and timeouts, streams, background tasks, and what survives a host restart. `create` builds the context, and `workspace` gives each case a fresh, empty working folder. `confined` adds the cases that refuse paths outside that folder, and `reattaches` adds the restart cases. The kit does not register a case that the context cannot take part in. See [execution contexts](https://zidane.si/docs/execution-contexts.md).

```ts
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { describe, it, setDefaultTimeout } from 'bun:test'
import { createProcessContext } from 'zidane/contexts'
import { registerContextConformance } from 'zidane/contexts/testing'

// A case waits up to 25 s for a condition: give it a budget above that.
setDefaultTimeout(60_000)

registerContextConformance('process', {
  create: () => createProcessContext(),
  workspace: async () => {
    const cwd = await mkdtemp(join(tmpdir(), 'conformance-'))
    return { spawn: { cwd }, dispose: () => rm(cwd, { recursive: true, force: true }) }
  },
  reattaches: true,
}, { describe, it })
```

## Functions

### `registerContextConformance`

Function.

Register the suite under `name`.

```ts
function registerContextConformance(
  name: string,
  factory: ContextFactory,
  runner: ContextConformanceRunner,
): void
```

**Parameters**

- `name` (`string`): Name of the suite, such as the context's name.
- `factory` (`ContextFactory`): Builds the context and a fresh working folder for each case, and says what it promises beyond the base contract.
- `runner` (`ContextConformanceRunner`): Your test runner's `describe` and `it`, and how long a case waits for a condition.

**Example**

```ts
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { describe, it, setDefaultTimeout } from 'bun:test'
import { createProcessContext } from 'zidane/contexts'
import { registerContextConformance } from 'zidane/contexts/testing'

setDefaultTimeout(60_000)
registerContextConformance('process', {
  create: () => createProcessContext(),
  workspace: async () => {
    const cwd = await mkdtemp(join(tmpdir(), 'conformance-'))
    return { spawn: { cwd }, dispose: () => rm(cwd, { recursive: true, force: true }) }
  },
  reattaches: true,
}, { describe, it })
```

Source: [src/contexts/testing/conformance.ts:61](https://github.com/Tahul/zidane/blob/main/src/contexts/testing/conformance.ts#L61)

### `registerPortConformance`

Function.

Register only the port cases (`ExecutionContext.ports`) under `name`:
for a context whose `ports` is new, or proven apart from the rest.

```ts
function registerPortConformance(
  name: string,
  factory: ContextFactory,
  runner: ContextConformanceRunner,
): void
```

**Parameters**

- `name` (`string`): Name of the suite.
- `factory` (`ContextFactory`): Builds the context and a fresh working folder for each case.
- `runner` (`ContextConformanceRunner`): Your test runner's `describe` and `it`, and how long a case waits for a condition.

Source: [src/contexts/testing/conformance.ts:28](https://github.com/Tahul/zidane/blob/main/src/contexts/testing/conformance.ts#L28)

## Interfaces

### `ContextConformanceRunner`

Interface.

The test functions the suite needs.

```ts
interface ContextConformanceRunner
```

**Properties**

- `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.
- `waitMs?` (`number`): How long a case waits for a condition (a process to die, output to
  arrive) before it gives up, in milliseconds. Default 25 000. Give each
  case a test budget above it.

Source: [src/contexts/testing/harness.ts:45](https://github.com/Tahul/zidane/blob/main/src/contexts/testing/harness.ts#L45)

### `ContextFactory`

Interface.

How to build the context under test, and what it promises beyond the base contract.

```ts
interface ContextFactory
```

**Properties**

- `confined?` (`boolean`): The context refuses file paths that resolve outside the handle's working
  folder (`..`, absolute paths, symlinks pointing out), such as a process
  context with `workspaceRoot`. The cases write a probe file next to the
  working folder through the shell, and remove it.
- `create` (`() => ExecutionContext`): Builds an instance of the context. Called once at registration (the
  instance every case runs on) and once more per restart case: a second
  instance on the same environment, as a restarted host builds it.
- `reattaches?` (`boolean`): A new instance from `create` can operate a handle an earlier
  instance spawned (after its `attach`, when it has one): the process
  context, sandbox providers. Absent: the restart cases are skipped.
- `workspace` (`() => Promise<ContextWorkspace>`): Prepares a fresh working folder for one case.

Source: [src/contexts/testing/harness.ts:20](https://github.com/Tahul/zidane/blob/main/src/contexts/testing/harness.ts#L20)

### `ContextWorkspace`

Interface.

A fresh, empty working folder for one case.

```ts
interface ContextWorkspace
```

**Properties**

- `dispose?` (`() => Promise<void>`): Removes what `ContextFactory.workspace` made, after the case destroyed its handles.
- `spawn` (`SpawnConfig`): What the case spawns its handles with. Its `cwd` must be an empty folder (or one `spawn` creates).

Source: [src/contexts/testing/harness.ts:12](https://github.com/Tahul/zidane/blob/main/src/contexts/testing/harness.ts#L12)
