# Testing

> `@typegpu/kit` 0.1.0. If it is installed, read `node_modules/@typegpu/kit/docs/` instead.

Check what a module records in plain Node, then compile its WGSL on a real device.

> `import { recordingRoot, offscreenSurface, steppedSchedule } from '@typegpu/kit/testing'`: `recordingRoot(options?) → RecordingRoot`, `offscreenSurface(root, { width, height, format, … }) → OffscreenSurface`, `steppedSchedule() → SteppedSchedule`. Runs scene frames in tests without a GPU. **kind** tool. **owns** a texture: an offscreen surface's, replaced when it resizes. **lifecycle** `destroy()`, `[Symbol.dispose]`. **types** `RecordingLog`, `AliveOptions`, `RecordingCounts`. `src/testing.ts`.
>
> `RecordingOptions`, taken by `recordingRoot`:
> - `features?: readonly string[]` Sets the device features reported by the recording root, such as `'timestamp-query'`. Default none.
> - `limits?: Partial<Record<keyof GPUSupportedLimits, number>>` Overrides device limits, such as a small `maxStorageBufferBindingSize` to reach a size guard. Default the WebGPU default limits.
>
> `OffscreenSurfaceOptions`, taken by `offscreenSurface`:
> - `width: number` Sets the texture width in pixels.
> - `height: number` Sets the texture height in pixels.
> - `format: GPUTextureFormat` Sets the texture format, as a canvas preferred format would.
> - `pixelRatio?: number` Sets the reported `pixelRatio`, as for a canvas on a phone. Default 1.
>
> `RecordingRoot`, from `recordingRoot`:
> - `root: TgpuRoot` Provides a TypeGPU root on a device that records instead of drawing.
> - `log: RecordingLog` Records the calls and objects the device sees.
> - `bytes(buffer)` Returns the buffer bytes after queued writes and submitted copies.
>
> `OffscreenSurface`, from `offscreenSurface`:
> - `texture: GPUTexture` Provides the texture that frames draw into, a WebGPU texture and not a TypeGPU one, so a `source` option does not take it.
> - `paused: boolean` Reports whether the surface is paused, true between `pause` and `resume`.
> - `setSize(size)` Replaces the texture with one of the given size.
> - `pause()` Pauses the surface, setting `paused`.
> - `resume()` Resumes the surface, clearing `paused`.
> - `format: GPUTextureFormat` Reports the format of its textures, which pipelines that draw into it target.
> - `width: number` Reports the drawing-buffer size in pixels.
> - `height: number` Reports the drawing-buffer height in pixels.
> - `pixelRatio: number` Reports drawing-buffer pixels per CSS pixel or point after applying the cap.
> - `view(frame)` Returns a view of the current texture, created once per texture.
> - `destroy()` Releases the drawing destination.
>
> `SteppedSchedule`, from `steppedSchedule`:
> - `step(...milliseconds)` Runs a requested frame for each given time, in milliseconds.
> - `waiting: boolean` Reports whether a requested frame is waiting to run or be cancelled.
> - `request(callback)` Calls `callback` with the time in milliseconds at the next frame and returns the request's id.
> - `cancel(request)` Cancels the request with this id.

A GPU module can record a dispatch in the wrong pass, submit unexpectedly or generate invalid WGSL. `recordingRoot()` lets a test inspect recorded commands in Node. It creates a TypeGPU root on a stand-in device that stores WGSL and logs commands without executing them.

```ts
import { recordingRoot } from '@typegpu/kit/testing';

const { root, log, bytes } = recordingRoot();
```

## The log

- **`shaders`** The WGSL of every shader module, in creation order.
- **`events`** `begin-compute`, `dispatch`, `end-compute`, `begin-render`, `draw`, `copy-buffer`, `submit` and more.
- **`state`** `set-pipeline` and `set-bind-group` for each `setPipeline` and `setBindGroup` a pass receives, in order. A draw with the same pipeline as the draw before it adds none.
- **`writes`** Every `queue.writeBuffer`, in order, with its buffer, offset and size in bytes.
- **Counts** `buffers`, `textures`, `views`, `bindGroups`, `samplers`, `querySets` and `pipelines`: how many of each the device created, and `counts()` all seven at once. `submissions`: the queue submissions.
- **`blocking`** The pipelines created without `…Async`, each of which holds the thread until it compiles. A frame that adds one compiled what [`pipelines.compile`](pipelines.md) should have while loading.
- **`alive()`** The buffers, textures and query sets created and not yet destroyed, as `[Buffer "label"]`, in creation order. Pass `{ uniforms: false }` to omit uniform buffers.
- **`bytes(buffer)`** A TypeGPU buffer's or `GPUBuffer`'s contents after the queue writes and the copies and clears submitted so far. It returns the bytes themselves, so a test can write into them what a kernel would have, such as a counter.
- **First use** TypeGPU makes GPU objects the first time something uses them, so the counts grow when a module first records, not when you create it.
- **Features** `recordingRoot({ features: ['timestamp-query'] })` reports device features, for code that branches on them.
- **Errors** Resolution errors, including missing fills, throw as usual. The recording device also throws immediately for errors a GPU reports later. These include missing usage flags, writes or copies out of bounds of either side or not aligned to 4 bytes, a buffer from another device, a second pending `mapAsync`, submitting with an open pass and submitting a destroyed buffer.

## A test

```ts
it('records steps and a draw into the caller’s passes', async () => {
  const { root, log } = recordingRoot();
  const field = createFlowField(root, {
    count: 1000,
    step: 1 / 60,
    format: 'bgra8unorm',
    size: { width: 64, height: 32 },
  });
  await field.initAsync();
  expect(log.pipelines).toBe(3);

  const target = root.createTexture({ size: [64, 32], format: 'bgra8unorm' }).$usage('render');
  const encoder = root.createCommandEncoder();
  const compute = encoder.beginComputePass();
  field.step(compute);
  field.step(compute);
  compute.end();
  const pass = encoder.beginRenderPass({ colorAttachments: { view: target } });
  field.draw(pass);
  pass.end();
  encoder.submit();

  expect(log.events).toEqual([
    'begin-compute',
    'dispatch',
    'dispatch',
    'end-compute',
    'begin-render',
    'draw',
    'end-render',
    'submit',
  ]);
  expect(log.pipelines).toBe(3); // nothing compiled mid-frame
});
```

Also check `log.shaders` for unwanted WGSL, such as `read_write` storage in a vertex shader. Inspect upload sizes in `log.writes` or contents through `bytes`. Use `log.submissions` to check that only your frame submits:

```ts
it('uploads a radius at flush, without a submit', () => {
  const { root, log, bytes } = recordingRoot();
  const enemies = table.create(root, Enemy, { capacity: 64 });
  const id = enemies.add({ radius: 0.5 });
  enemies.fields.radius.set(id, 2);
  enemies.flush();

  const radii = new Float32Array(bytes(enemies.buffers.host!).buffer);
  expect(radii[enemies.slotOf(id)]).toBe(2);
  expect(log.submissions).toBe(0); // flush writes through the queue and never submits
});
```

## A frame without a canvas

Code that draws to the screen takes a [`Surface`](web.md#drawing-to-it). `offscreenSurface(root, { width, height, format })` provides one backed by a texture on a recording or real root. Tests can run the same scene frames without a canvas.

```ts
import { offscreenSurface, recordingRoot } from '@typegpu/kit/testing';

const { root } = recordingRoot();
const surface = offscreenSurface(root, { width: 64, height: 36, format: 'bgra8unorm' });
const scene = await createScene(root, surface);

frames.create(root, (frame) => scene.frame(frame)).run(0);
```

- `setSize({ width, height })` replaces the texture, as a canvas does on a new layout; [targets](targets.md) that follow the surface rebuild at their next `fit()`. It takes the same [fixed size](web.md#a-fixed-size) as a canvas surface's `setSize`.
- `surface.texture` is the WebGPU texture the frames draw into. It can be copied from, so a test on a real GPU can read its pixels. A `source` option takes a [target](targets.md)'s texture, not this one.
- `pixelRatio` is 1 unless you give one, such as `pixelRatio: 3` for a scene that draws hairlines on a phone.
- It pauses and resumes like a canvas surface, so a test can [stage](hosting.md#a-host-of-your-own) a scene on it or host one.

### Frames by hand

`steppedSchedule()` lets a test run animation frames requested by a host or `animate`. `step(0, 16)` runs one at 0 ms and the next at 16 ms. `waiting` reports whether a frame is requested.

```ts
const display = steppedSchedule();
const steps: number[] = [];
const loop = frames.create(root, ({ dt }) => steps.push(dt));
const animation = frames.animate(loop, { schedule: display });

display.step(0, 16, 32); // steps is [0, 0.016, 0.016]
animation.destroy();
display.waiting; // false: a destroyed animation asks for no frame
```

### Models with images

Node and dawn.node have no `createImageBitmap`, so a [mesh pool](mesh-pool.md) can't decode a glTF model's images and `add` throws. Give the scene a `decode` option that it hands to `meshPool.create`, and pass a stub from the test, such as a white pixel for every image; the page leaves it out:

```ts
import { offscreenSurface, recordingRoot } from '@typegpu/kit/testing';

const { root } = recordingRoot();
const surface = offscreenSurface(root, { width: 64, height: 36, format: 'bgra8unorm' });
const white = { width: 1, height: 1, pixels: new Uint8Array([255, 255, 255, 255]) };
const scene = await createScene(root, surface, { decode: async () => white });
```

The scene calls `meshPool.create(root, { capacity: 32, decode: options.decode })`. A model whose images are already `{ width, height, pixels }` needs no `decode`, so a test can also swap each image for the white pixel before `add`.

## A steady frame creates nothing

Nothing should allocate during a frame. Run a couple of frames so TypeGPU's first-use objects exist, then take the counts, run more and compare:

```ts title="scene.test.ts"
it('creates nothing in a steady frame', async () => {
  const { root, log } = recordingRoot();
  const surface = offscreenSurface(root, { width: 64, height: 36, format: 'bgra8unorm' });
  const scene = await createScene(root, surface);
  const loop = frames.create(root, (frame) => scene.frame(frame));

  loop.run(0);
  loop.run(1 / 60);
  const before = log.counts();

  loop.run(2 / 60);
  loop.run(3 / 60);
  expect(log.counts()).toEqual(before);
});
```

- A frame after a resize creates the new [targets](targets.md) and the bind groups that read them, so take the counts once the size has settled.
- Counts include only GPU objects. They omit JS allocations such as the handle returned by `.with(...)`. [Bind groups at setup](../concepts/modules.md#recording-methods), then set the pipeline on the pass during a frame.

## Destroy frees what it made

A module's `destroy()` frees its textures, non-uniform buffers and query sets. Take `log.alive({ uniforms: false })` before the module is made, run some frames, destroy it and compare:

```ts title="glow.test.ts"
it('frees what it made when destroyed', () => {
  const { root, log } = recordingRoot();
  const surface = offscreenSurface(root, { width: 64, height: 36, format: 'bgra8unorm' });
  const before = log.alive({ uniforms: false });
  const glow = createGlow(root, surface);
  const loop = frames.create(root, (frame) => glow.record(frame));

  for (let i = 0; i < 10; i++) loop.run(i / 60);

  glow.destroy();
  expect(log.alive({ uniforms: false })).toEqual(before);
});
```

- Run frames first: TypeGPU makes most objects on first use, and a resize makes new [targets](targets.md).
- The surface is borrowed, so it stays alive, and is in `before`.

## On a real GPU

A recording root resolves WGSL without compiling it. Add `*.test.gpu.ts` tests on dawn.node to catch compiler errors and check shader results. Create their roots with `gpuRoot()` from `@typegpu/kit/testing/dawn`:

```ts title="compile.test.gpu.ts"
it('runs the flow field on a real device', async () => {
  const root = await gpuRoot();
  const surface = offscreenSurface(root, { width: 64, height: 36, format: 'bgra8unorm' });
  const scene = await createScene(root, surface); // Dawn compiles the WGSL here

  frames.create(root, (frame) => scene.frame(frame)).run(0); // and validates the frame here
}); // fails here on any uncaptured WebGPU error, and destroys the device
```

- **Features** `gpuRoot({ features })` throws when the adapter lacks one of `features`, so a test never runs a fallback it didn't mean to. `optionalFeatures` are requested where the adapter has them, `limits` are the device's required limits, and `dawn` holds flags for the Dawn instance, such as `'enable-dawn-features=allow_unsafe_apis'`.
- **Errors** Call it inside a test: when the test ends, any uncaptured WebGPU error fails it, and the device is destroyed. A test that expects errors takes them first with `takeErrors()`, which waits for the queue and returns the errors so far:

```ts
expect((await root.takeErrors()).join('\n')).toMatch(/bindGroupLayoutCount \(5\)/);
```

- **Without a GPU** On a machine without a GPU, such as a CI runner, `gpuRoot({ dawn: ['backend=null'] })` uses Dawn's null backend. It compiles WGSL and validates every call at the default limits, and executes nothing, so a test that reads results needs a real adapter.
- **GPU tests** GPU tests run one file at a time. They catch WGSL compiler errors, binding and format limits, and incorrect shader results. To count pipelines, events and submissions on a GPU, use gpu-debug's `instrument`.
- **Warnings** Both levels load `@typegpu/kit/testing/warnings`, which fails any test during which TypeGPU warned, such as about `>>` on a `u32`. To let some through, a setup file of your own calls `failOnWarnings({ allow: [/deprecated/] })` from `@typegpu/kit/testing/failOnWarnings` instead and disposes of what it returns in `afterAll`, which puts `console.warn` back. Tests running at once with `it.concurrent` share one list of warnings, so a warning from one can fail another.

### In your own project

For `@typegpu/kit/testing/dawn`, install its optional peers, `webgpu` for dawn.node and `vitest`, as dev dependencies. `@typegpu/kit/testing/warnings` and `@typegpu/kit/testing/failOnWarnings` need only `vitest`. pnpm 11 stops an install at a build script it hasn't been told about, and `webgpu` has one, which only clears macOS's quarantine flag from a downloaded dawn.node. Allow it, or deny it:

```yaml title="pnpm-workspace.yaml"
allowBuilds:
  webgpu: false
```

Run both levels from one config:

```ts title="vitest.config.ts"
import typegpu from 'unplugin-typegpu/vite';
import { defineConfig } from 'vitest/config';

export default defineConfig({
  plugins: [typegpu()],
  test: {
    setupFiles: ['@typegpu/kit/testing/warnings'],
    projects: [
      { extends: true, test: { name: 'unit', include: ['tests/**/*.test.ts'] } },
      {
        extends: true,
        test: {
          name: 'gpu',
          include: ['tests/**/*.test.gpu.ts'],
          pool: 'forks',
          fileParallelism: false, // one GPU file at a time
        },
      },
    ],
  },
});
```

## Limits

- A recording root keeps no texture contents, so a test that reads pixels runs on `gpuRoot()`.
- A recording root reports WebGPU's default limits. Pass `recordingRoot({ limits })` to reach a size guard. A device method it lacks throws, naming the method; run that test on `gpuRoot()`.
- Maps on a recording root complete in a microtask. To test code that depends on readback taking several frames, use a stand-in that delays mapping.
