# songstress/testing

> The mock Songstress for your tests.

Page: https://songstress.app/docs/develop/reference/testing/

See [Test and debug](/docs/develop/testing/).

What it exports is the SDK's own record of its public surface, which the SDK's tests hold to the code. Each type's
fields, and every description, are read from the SDK's source when the site is built.

### HookArguments

What a hook's listeners are called with.

```ts
export type HookArguments<Name extends HookName> = ArgumentsOf<ListenerOf<ServerHooks, Name> | ListenerOf<ClientHooks, Name>>;
```

Refers to [HookName](/docs/develop/reference/testing/#type-HookName), [ServerHooks](/docs/develop/reference/songstress/#type-ServerHooks), [ClientHooks](/docs/develop/reference/songstress/#type-ClientHooks).

### HookName

A hook's name, on either side.

```ts
export type HookName = keyof ServerHooks | keyof ClientHooks;
```

Refers to [ServerHooks](/docs/develop/reference/songstress/#type-ServerHooks), [ClientHooks](/docs/develop/reference/songstress/#type-ClientHooks).

### MockCall

A call the code under test made, by the member's full name ("storage.get").

| Field | Type | Description |
|---|---|---|
| `member` | `string` | The member's full name, as `storage.get`. |
| `args` | `readonly unknown[]` | What it was called with, in order. |

### MockHost

The mock host, as a test holds it.

| Field | Type | Description |
|---|---|---|
| `calls` | `readonly MockCall[]` | Every call the code made, in order. |
| `requests` | `readonly MockRequest[]` | Every request it made through `net.fetch`, in order, refused ones too. |
| `emit` | `<Name extends HookName>(hook: Name, ...args: HookArguments<Name>) => Promise<void>` | Fires a hook: each of its listeners is called in turn and waited for. It throws when nothing listens, or when the manifest does not list the hook. |
| `run` | `(command: string, input?: unknown, context?: Partial<CommandContext>) => Promise<CommandOutcome \| undefined>` | Runs a command the code registered, as a person picking it would. `context` is laid over `{ userId: 'member', source: 'menu' }`. |
| `provider` | `<Kind extends keyof Providers>(kind: Kind, id: string) => ProviderOf<Kind>` | The provider the code registered under a kind and an id. |
| `registered` | `<Handler = unknown>(member: string, id?: string) => Handler` | What the code handed to any registering member, by the member's full name and, where it takes one, the id: `registered('jobs.register', 'sync')` is that job's handler, `registered('signIn.register')` the sign-in's. |
| `remove` | `() => void` | Takes the mock host away. Later calls throw "no host" again. |

Refers to [MockCall](/docs/develop/reference/testing/#type-MockCall), [MockRequest](/docs/develop/reference/testing/#type-MockRequest), [HookName](/docs/develop/reference/testing/#type-HookName), [HookArguments](/docs/develop/reference/testing/#type-HookArguments), [CommandContext](/docs/develop/reference/songstress/#type-CommandContext), [CommandOutcome](/docs/develop/reference/songstress/#type-CommandOutcome), [Providers](/docs/develop/reference/songstress/#type-Providers), [ProviderOf](/docs/develop/reference/testing/#type-ProviderOf).

### MockHostOptions

What a mock host is given: the manifest, the settings, the network, answers for other calls, and the side it stands in for.

| Field | Type | Description |
|---|---|---|
| `side?` | `Side \| undefined` | The entry under test. Left out, "server". |
| `manifest?` | `MockManifest \| undefined` | The extension's manifest. With it the mock host refuses what a real one would: a call whose permission the manifest does not ask for, a request to a host its `network` permission does not name (anything goes once it asks for `network.local`: a test cannot tell what is on your network), and a hook it does not list. Left out, nothing is refused. |
| `settings?` | `Record<string, ((userId: string \| undefined) => SettingValue \| undefined) \| SettingValue \| undefined> \| undefined` | What the settings hold, by key. A function answers for one member: it is given the `userId` the code asked for, if any. |
| `fetch?` | `((url: string, request: NetRequest) => Awaitable<Partial<NetResponse>>) \| undefined` | The network: what answers `net.fetch`. Whatever it leaves out is filled in (status 200, the address asked, no headers, an empty body). With no `fetch`, every request fails. |
| `answers?` | `Record<string, (...args: never[]) => unknown> \| undefined` | Answers for any other member, by its full name ("library.getSong"), or in place of the mock host's own ("storage.get"). |

Refers to [Side](/docs/develop/reference/songstress/#type-Side), [MockManifest](/docs/develop/reference/testing/#type-MockManifest), [SettingValue](/docs/develop/reference/songstress/#type-SettingValue), [NetRequest](/docs/develop/reference/songstress/#type-NetRequest), [Awaitable](/docs/develop/reference/songstress/#type-Awaitable), [NetResponse](/docs/develop/reference/songstress/#type-NetResponse).

### MockManifest

The parts of a manifest the mock host reads. Hand it your songstress.json
as you import it, or a `Manifest` the validator answered.

| Field | Type | Description |
|---|---|---|
| `id?` | `string \| undefined` | The extension's id. |
| `version?` | `string \| undefined` | The extension's version. |
| `hooks?` | `{ readonly server?: readonly string[]; readonly client?: readonly string[]; } \| undefined` | The hooks each entry listens to: `emit` throws for a hook not listed. |
| `permissions?` | `object \| undefined` | Keyed by the permission's name, as the manifest writes it. |

### MockRequest

A request the code under test made through `net.fetch`.

| Field | Type | Description |
|---|---|---|
| `url` | `string` | The address asked for. |
| `request` | `NetRequest` | The method, the headers and the body it was asked with. |

Refers to [NetRequest](/docs/develop/reference/songstress/#type-NetRequest).

### ProviderOf

The provider a kind registers: `ProviderOf<'lyrics'>` is a LyricsProvider.

```ts
export type ProviderOf<Kind extends keyof Providers> = Providers[Kind] extends ProviderRegistry<infer Provider> ? Provider : never;
```

Refers to [Providers](/docs/develop/reference/songstress/#type-Providers), [ProviderRegistry](/docs/develop/reference/songstress/#type-ProviderRegistry).

### createMockHost

Puts a mock Songstress in place, for the entry a test then imports.

```ts
export declare function createMockHost(options?: MockHostOptions): MockHost;
```

| Parameter | Type | Description |
|---|---|---|
| `options?` | `MockHostOptions` | The manifest, the settings, the network, answers for other calls, and the side. |

Returns `MockHost`: The mock host: fire hooks, run commands, read every call. `remove()` takes it away.

Example:

```ts
import { createMockHost, type MockManifest } from 'songstress/testing'

declare const manifest: MockManifest // your songstress.json, as you import it
declare const entry: string // the path of the entry under test

const host = createMockHost({ manifest })
await import(entry) // the entry registers as it loads
await host.emit(
  'onScrobble',
  { id: 's1', title: 'Blue in Green', artist: 'Miles Davis' },
  { userId: 'ana', playedAt: '2026-01-01T20:00:00Z' },
)
console.log(host.calls)
host.remove()
```

Refers to [MockHostOptions](/docs/develop/reference/testing/#type-MockHostOptions), [MockHost](/docs/develop/reference/testing/#type-MockHost).
