# Test and debug

> Test your extension against a mock Songstress, hold the compiler to the sandbox, and read its log on your own server.

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

## Hold the compiler to the sandbox [#hold-the-compiler-to-the-sandbox]

Your code runs with the JavaScript language and the `songstress` API alone. Tell the compiler so, and `fetch`,
`process` and `document` stop compiling:

```json title="tsconfig.json"
{ "compilerOptions": { "lib": ["ES2023"], "types": ["songstress/sandbox"], "moduleResolution": "bundler" } }
```

A project from `create-songstress-extension` has this already.

## Test against the mock host [#test-against-the-mock-host]

`songstress/testing` puts a mock Songstress in place. It keeps what your entry registers and lets a test call it as
Songstress does, answers the store, the secrets and the settings from memory, and hands the network to a function of
yours. Given the manifest, it refuses what a real server refuses, for the same reasons: a call whose permission the
manifest doesn't ask for, a request to a host it doesn't name, a hook it doesn't list.

```ts title="src/server.test.ts"
import { createMockHost } from 'songstress/testing'
import { expect, it, vi } from 'vitest'
import manifest from '../songstress.json'

it('counts a play', async () => {
  const host = createMockHost({ manifest })
  vi.resetModules()
  await import('./server') // 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' },
  )

  expect(host.calls.at(-1)).toEqual({ member: 'storage.set', args: ['count', 1, { userId: 'ana' }] })
  host.remove()
})
```

`createMockHost` takes:

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"). |

It answers the mock host:

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

The mock runs no sandbox. Limits, deadlines and what a member granted are for a real server to show, with
`songstress dev`.

## Debug on your own server [#debug-on-your-own-server]

`songstress dev` runs your extension on your server, for you alone, and prints its log as the server writes it.
`console.log` writes to that log. Each save builds and pushes again; when a build fails, or the server refuses the
package, the command says why and the last good version keeps running.

- A permission you forgot shows as `permission-denied` in the log, and `songstress check` names it before you push:
  `PERMISSION_REQUIRED`.
- A hook that never fires is usually missing from `hooks.server` or `hooks.client`. The check says so:
  `ENTRY_UNUSED` or `PERMISSION_REQUIRED`.
- A call that runs over 30 seconds, or past 64 MiB, is stopped with `limit-exceeded`.

## Check before you push [#check-before-you-push]

```sh title="Terminal"
npx songstress check
npx songstress check --profile listing
```

The first checks what a server checks when it installs. The second adds what a listing asks. CI from
`create-songstress-extension` runs the types, the tests, the check and the pack on every push.
