# emulated headset

Does a controller press reach this? Is the frame stereo? An emulated headset
answers both without a headset, in a test that runs in CI. Headless Chrome
runs the page with Meta's [iwer](https://github.com/meta-quest/immersive-web-emulation-runtime)
(a WebXR runtime in plain JavaScript) installed before the page loads. The
page's own Enter VR button starts a session against a fake Quest 3, both eyes
render into the canvas, and a test poses the two controllers and pulls their
triggers.

It is the right check for the half of VR that is not the same as flat: where
the pointer's ray starts. Flat, it starts at the camera; in a headset it starts
at a hand, and code that forgets that is wrong only there. What is DRAWN is
the same in both, so a panel bug is cheaper to find flat.

`xrEmulatorScript()` is the script a page needs: iwer's build plus the
helpers below, for Playwright's `addInitScript` (or Chrome's
`Page.addScriptToEvaluateOnNewDocument`). `iwer` is an optional peer: install
it beside `tosijs-3d` in the project that runs the test.

```typescript
import { test, expect } from '@playwright/test'
import { xrEmulatorScript, XR_EMULATOR_CHROME_ARGS } from 'tosijs-3d/xr-emulate'

test.use({ launchOptions: { args: XR_EMULATOR_CHROME_ARGS } })

test('a controller press selects the piece', async ({ page }) => {
  await page.addInitScript({ content: await xrEmulatorScript() })
  await page.goto('https://localhost:8032/editor/')
  await page.waitForFunction(() => window.__deep('tosi-b3d')?.scene != null)
  expect(await page.evaluate(() => __xrTest.enter())).toBe(true)
  // A session that is not ticking makes every later reading a lie.
  const f = await page.evaluate(() => __xrTest.frames())
  await page.waitForTimeout(500)
  expect(await page.evaluate(() => __xrTest.frames())).toBeGreaterThan(f)
  await page.evaluate(async () => {
    const piece = __xrTest.b3d().scene.getMeshByName('piece')
    await __xrTest.hold('right')
    await __xrTest.press('right') // the first press after entry may be lost
    await __xrTest.aim('right', piece.getAbsolutePosition())
    await __xrTest.press('right')
  })
})
```

## The helpers

In the page, after `xrEmulatorScript()`:

| | |
| --- | --- |
| `__deep(selector)` | `querySelector` through shadow roots (a doc page nests `tosi-b3d` in one) |
| `__xr` | iwer's device: `__xr.quaternion.set(…)` turns the head, `__xr.position` moves it |
| `__xrTest.b3d()` | the page's `<tosi-b3d>` |
| `__xrTest.enter()` / `exit()` | start or end the session by the page's own button; resolves `true` when settled |
| `__xrTest.frames()` | the engine's frame count: it must advance in a live session |
| `__xrTest.controller(hand)` | Babylon's input source for `'left'` or `'right'` |
| `__xrTest.ray(hand)` | that controller's pointer ray, in world space |
| `__xrTest.hold(hand, dx, dy, dz)` | park a controller relative to the head (metres; +x right, +y up, −z ahead) |
| `__xrTest.aim(hand, point)` | turn a controller until its ray passes through a world point (a Babylon `Vector3`) |
| `__xrTest.trigger(hand, value)` | set a trigger (1 down, 0 up) |
| `__xrTest.press(hand, ms)` | down, wait, up |
| `__xrTest.wait(ms)` | a promise that resolves after `ms` |

## Traps

Four, none of which says what it is:

1. **iwer declines quietly.** Chrome has a native `navigator.xr`; iwer replaces
   it only with `installRuntime({ forceInstall: true })`, which the script does.
2. **Mono passes for stereo.** With iwer's stereo off the canvas shows one view,
   so a broken stereo frame and a good one look the same. The script turns it
   on (`stereo: false` to compare).
3. **The scene freezes after entry.** `<tosi-b3d>` replaces
   `window.requestAnimationFrame` during a session and flushes it from XR
   frames, and iwer drives XR frames from `window.requestAnimationFrame`, so
   each waits on the other. The script stubs the replacement out as soon as
   `tosi-b3d` is defined, so a page that enters by itself is covered too.
   `frames()` is the check. (`enter()` on a page already in a session
   returns `true` without pressing the button, which would exit.)
4. **The first trigger press after entry may make no pointer-down.** Press once,
   aimed at nothing, before the press you care about.

Flat tests have one of their own: a synthetic pointer event dispatched on the
canvas needs `composed: true`, or it never leaves `<tosi-b3d>`'s shadow root,
and nothing outside it (the radial menu's listener, your own) hears it.

**Its session draws into the canvas's own framebuffer**, and a Quest's does
not, so a question about which framebuffer is cleared still needs a device.
Nor can it say anything about frame rate or multiview.

In a checkout of this repo, `bun bin/xr-emulate.ts <path> --script file.js`
runs the same helpers against the dev server and screenshots both eyes.
