diff --git a/.gitattributes b/.gitattributes index 68528d4..6fed571 100644 --- a/.gitattributes +++ b/.gitattributes @@ -37,4 +37,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text *.wav filter=lfs diff=lfs merge=lfs -text *.png filter=lfs diff=lfs merge=lfs -text *.webp filter=lfs diff=lfs merge=lfs -text +*.webm filter=lfs diff=lfs merge=lfs -text *.glb filter=lfs diff=lfs merge=lfs -text diff --git a/README.md b/README.md index eb10d32..3cb84a0 100644 --- a/README.md +++ b/README.md @@ -9,144 +9,107 @@ pinned: false license: apache-2.0 --- -# Microduck RL playground +# Browser robot simulation platform -Real trained RL policies for the Microduck robot, running fully in the -browser: MuJoCo compiled to WebAssembly steps the physics, onnxruntime-web -runs the policy network at 50 Hz. No server, no backend. +MuJoCo WASM + onnxruntime-web + three.js, with **Microduck** (legs / rollers) and **Microban** Robot Packages. Physics and ONNX inference run locally in the browser; no simulation backend is required. -Two locomotion variants of the same robot are included: **legs** (walking, -the default) and **rollers** (the wheeled skating variant). Press `M` (or -hold D-pad up ~1 s on a gamepad) to switch; the roller model, meshes and -policies are lazy-loaded on the first switch. +The application opens directly into simulation, defaults to Microduck, and keeps pause, reset, robot selection and a collapsible keyboard reference on that same screen. There is no welcome / RESUME menu or BIOS animation. Anton is bundled locally, so the default interface does not wait for an external font service. -## Policies +## Robots -| Mode | Checkpoint | What it does | -|--------|-----------|--------------| -| Run (legs) | `BEST_alpha_walking.onnx` | Velocity-tracking locomotion (arrows / WASD to steer) | -| Sit | `BEST_alpha_sitstand.onnx` | Sits down on its hull, stands back up | -| Roll | `roulade.onnx` | Rolls over and recovers | -| Kick | `ball_kick_left.onnx` / `ball_kick_right.onnx` | Blind one-shot kick (0.5 s window, zeroed commands), left or right leg | -| Drive (rollers) | `BEST_roller.onnx` | Velocity-tracking skating on 4 passive wheels (higher top speed: 0.6 m/s) | -| Crouch (rollers) | `BEST_roller_crouch.onnx` | One-shot crouch-glide: sinks low over ~3.5 s and stands back up (phase-encoded command) | +| Robot | Default scene / presentation | Controls | +|---|---|---| +| Microduck | Shared 4.5 × 4.5 m bounded arena, ball, original GLB/palette/comic effects | Existing walking, skating, skills and recovery | +| Microban | Shared 4.5 × 4.5 m bounded arena, dark/orange grid and comic UI, MJCF robot visuals | Walking policy + independent head rotation | -Policies and MJCF model from -[pollen-robotics/microduck](https://github.com/pollen-robotics/microduck) -and [pollen-robotics/microduck_rl](https://github.com/pollen-robotics/microduck_rl). +Switch robots using **Robot** in the toolbar. Returning to Microduck restores the session's colour and feet / rollers choice, but initializes a fresh physical state. Failed loads keep the previous robot available and show an inline retry action. -## Controls +Microduck policies / assets originate from [pollen-robotics/microduck](https://github.com/pollen-robotics/microduck) and [pollen-robotics/microduck_rl](https://github.com/pollen-robotics/microduck_rl). Microban assets originate from [Rhoban/microban](https://github.com/Rhoban/microban); exact source hashes, dirty-worktree provenance and license notices are included in its Package. -- Arrows or WASD / ZQSD: forward / back + turn -- M: switch legs <-> rollers -- Q / E (A / E on AZERTY): kick left / right (legs only) -- F: alternate left / right kicks (legs only) -- R: sit / stand (legs) / crouch-glide (rollers) -- G: ground pick (legs only) -- C: toggle the chase camera (on by default; dragging detaches it) -- Space: reset -- Drag to orbit, scroll to zoom -- Colour dots: repaint the duck (it quacks) +## Keyboard and mouse -In roller mode the legs-only actions (kicks, sit) are disabled and their -hints fade out; play the ball by driving into it. +| Action | Binding | +|---|---| +| Forward / backward / turn | Arrow keys or physical WASD positions (layout labels adapt when supported) | +| Pause / continue | Esc or toolbar | +| Reset current robot | Space or toolbar | +| Orbit / zoom | Mouse drag / wheel | +| Microduck left / right kick | Q / E | +| Microduck alternate kick | F | +| Microduck sit / stand; roller crouch | R | +| Microduck ground pick | G | +| Microduck forward roll; roller crouch | T | +| Microduck feet / rollers | M or quickbar | +| Restore rear chase camera (both robots) | C | +| Microban independent head rotation | H | -### URL parameters +The on-screen reference comes from the same Package bindings as the keyboard, and its skill entries are also clickable buttons. Text fields / selectors retain their normal keyboard behavior. Focused buttons retain native Space / Enter behavior without also resetting the robot. Losing focus, pausing, resetting or switching clears held movement keys. -- `?boot=1`: skip the welcome modal and land straight on the BIOS console, - which then shows the real loading progress live (honest loader) before - the normal entrance plays. Boot failures (missing asset, policy fetch - error...) freeze the console on a `SYSTEM HALTED` screen with the error - detail - handy for debugging. +All bundled robots, and future Packages omitting `presentation.world`, use the shared **4.5 × 4.5 m** arena (length and width increased 50% from 3 m). Its four real collision walls are 0.25 m high and 0.05 m thick, with Microduck's dark glowing grid. Microban's interior stays flat; no relief or automatic coordinate clamping is added. Explicit `world: "flat"` remains available for generic-runtime diagnostics. Both robots start with the camera behind the body. Mouse orbit detaches chase mode; **C** restores rear following and the default zoom, including while paused (pressing C again does not detach). Microduck mouse dragging still applies physical forces to the robot / ball. Microban mouse dragging orbits the camera. Gamepad / touch are not part of this release's acceptance scope. -The ball's physics are local to your tab, but its pose is broadcast with -your duck's, so other visitors see a translucent copy of your ball next -to your ghost (and you see theirs). A square arena (3 x 3 m) fences the -play area so neither the ball nor the duck can wander off. +## Microban fidelity and limits -### Gamepad +- Actual ONNX interface: `obs [1, 63]` → `actions [1, 18]`. +- The model has 19 actuated joints; the policy controls 18 body joints, **not head**. Metadata's 19 names are resolved by name, not mistaken for actor output order. +- Projected gravity uses the trunk frame; the gyro retains the original IMU-site frame. Current policy has no extra reference-phase input. +- Physics timestep: 0.005 s; control decimation: 4 (50 Hz). +- **Simplified actuation**, not BAM: joint PD at every physics substep, kp=0.3, kd=0.02, torque limit=0.65 Nm. +- Default commands: forward 0.15 m/s, backward −0.1 m/s, turn ±0.6 rad/s. These are commanded, not guaranteed measured tracking speeds. Drift remains possible. +- Head follows the source's 0.35 Hz / ±π/4 sinusoid, with 0.5 s start / stop blending. It does not restart the walking controller. +- Falling stops policy updates and requests manual reset. There is no automatic stand-up policy for Microban. -Plug in a controller and the same mapping as the real robot runtime applies: - -- Left stick: forward / back + turn -- Right stick: orbit the camera (detaches the chase cam) -- R3 (right stick click): toggle the chase cam back on -- X or D-pad down: sit / stand (legs) / crouch-glide (rollers) -- A: ground pick (legs only) -- Y: toggle head control -- RB / LB: right / left kick (legs only) -- D-pad up: hold ~1 s to switch legs <-> rollers (the real robot uses a 3 s hold) -- Right trigger: mouth (analog) + quack -- Left trigger: hold for wheee -- A: enter / resume the game while the title or pause overlay is up -- Menu/Start (or Select): exit back to the title overlay while in-game - -## Multiplayer ghosts - -Other people visiting the Space at the same time show up as translucent -ducks, live. Peer-to-peer WebRTC via [Trystero](https://github.com/dmotz/trystero) -(serverless signaling over public Nostr relays), so it works from a static -Space with no backend. Each tab broadcasts its duck's pose (trunk + 14 -joints + jaw + colour + locomotion variant) at 10 Hz; up to 3 ghosts are -rendered, extra peers stay connected but invisible. The camcorder-style OSD -in the top-right corner shows "N ONLINE" when peers are around. - -Ghost limitations in v1: a peer in roller mode renders with the roller rig -only if your tab has already loaded it (otherwise it falls back to the leg -rig), and ghost wheels don't spin (passive wheel joints aren't broadcast). -Old clients simply ignore the new variant flag. - -## How it works - -The app is a Vite + React + MUI shell around an imperative game core: -React/MUI renders the UI chrome (title menu, BIOS console, HUD, touch -overlay), react-three-fiber owns the canvas/lights/environment, and the -physics/policy/rig loop lives in framework-agnostic modules under -`app/src/game/`. A zustand store bridges the two (game state out, -UI intents in). - -- `app/src/game/game.js` fetches the MJCF (`robot_allcollisions.xml`, or - `robot_allcollisions_rollers.xml` for the roller variant), strips the - visual geoms, injects a floor, arena walls, a ball, a collision box for - the arcade cabinet row and a STAND keyframe, and compiles it with the - official `@mujoco/mujoco` WASM bindings. -- Both variants share the exact same policy interface: 61D observation - (gyro, projected gravity, 14 joint pos/vel, last action, 13D command) - and 14 position-targets, matching [`microduck_rl/scripts/infer_policy.py`](https://github.com/pollen-robotics/microduck_rl/blob/main/scripts/infer_policy.py). - The roller variant adds 4 passive wheel hinges that appear in `qpos` - (zeroed in the keyframe) but not in the observation. -- Rendering is a three.js rig built from `kinematics.json` / - `kinematics_rollers.json` + decimated STL meshes, driven directly from - MuJoCo `qpos` (including the passive wheel spin). -- Switching variants swaps the compiled model + data + rig + ONNX sessions - wholesale; both stay resident after the first load so toggling back and - forth is instant. +BAM battery / friction / delay modeling, Placo squat, training, hardware control, ROS, camera observations, external Packages and online sharing are **not implemented** in this release. Ghost networking, stickers and celebration / entrance sequences are deferred. Audio retains the existing `SOUND_DISABLED=true` master setting; simulation never waits for an audio-unlock gesture. ## Development -Large binary assets (robot meshes, GLB models, ONNX policies) are stored -with [Git LFS](https://git-lfs.com/). Clone with LFS installed, or pull -the real files after a plain clone - otherwise the app boots against -text pointer files and fails with errors like -`SyntaxError: Unexpected token 'v', "version ht"... is not valid JSON` -(that string is the start of an LFS pointer, not your model): +Binary robot meshes and ONNX files use Git LFS: ```bash -git lfs install # once per machine -git lfs pull # fetch the actual binaries in an existing clone +git lfs install +git lfs pull +cd app +npm ci +npm run dev # http://localhost:5173 +npm run build # app/dist/ +npm run preview # production bundle ``` +Use a Node version supported by Vite (this revision was tested with Node 24). The Dockerfile builds with Node 22 and serves the static bundle through nginx on port 8080; a Docker build itself is not part of the local browser verification. + +### Verification + ```bash cd app -npm install -npm run dev # dev server on http://localhost:5173 -npm run build # production bundle in app/dist/ +npm test # pure logic tests +npm run validate:packages # schemas, World, paths, source hashes +npx playwright install chromium +npm run test:e2e # real browser / WASM + UI + regression + ownership tests +npm run test:regression # accepted Microduck golden trajectories only +npm run format:check +npm audit ``` -The Space builds with the included `Dockerfile` (Vite build, served by -nginx-unprivileged on port 8080). Static assets (meshes, policies, audio, -images) live in `app/public/` and keep their historical URLs. +`npm run baseline:record` writes **candidates** under `app/test-results/baseline-candidates/`; it never overwrites accepted fixtures. Review changes before accepting any new baseline. See [baseline provenance](app/test/fixtures/microduck/README.md). + +For a running production server, set `TEST_BASE_URL` and run the product-facing tests, e.g. `TEST_BASE_URL=http://127.0.0.1:5175 npm run test:e2e -- chase-camera deployment simulation-shell robot-switching microban-walking microban-observation microduck-regression runtime-smoke`. Development-only fixture pages are deliberately not shipped in `dist/`. + +For prefixed hosting, build and preview with `VITE_BASE=/play/`; `DEPLOY_PATH=/play/` selects that entry path for `deployment.spec.js`. Private HF authentication behavior beyond the retained same-origin asset signing has not been independently deployment-tested. + +`?test=1` is an internal deterministic-step hook, not a normal launch mode. `?boot=1` is no longer required. Legacy `?move`, `?policy` and `?session` requests remain limited to the Microduck adapter; they do not load arbitrary Robot Packages or replace Microban's controller. + +## Architecture and extension + +- `app/public/robots/*/robot.json`: model, joint groups, observations, actions, skills, bindings and presentation. +- `app/public/worlds/`: arena / flat World manifests. +- `app/src/platform/`: schemas / loading, SimCore, observation and action pipelines, term registries, ONNX, PD backend, overlay skills, input, MJCF renderer and transactional runtime manager. +- `app/src/robots/registry.js`: trusted bundled adapters; no remote JS execution. +- `app/src/robots/microduck/runtime.js`: compatibility adapter retaining original skill / recovery quirks and GLB presentation while using shared runtimes, scheduler, input, observation / action mapping and actuation. +- `app/src/game/worlds/arena/`: arena physics composition and rendering. `game/game.js` and `game/arena.js` are compatibility entry points, not generic engines. + +A robot expressible with existing capabilities can be added by a Package and index entry, without a robot-id branch in the platform. Current generic scope is floating-base robots with scalar joint groups, unit-gear actuators, supported proprioceptive terms, ONNX primary control, position targets and optional sinusoidal overlays. Unsupported capabilities are rejected, not silently simulated incorrectly. New capabilities require explicit registry / schema work. + +See [Package authoring](docs/robots/package-authoring.md), [Microban details](docs/robots/microban.md), [approved design](docs/superpowers/specs/2026-10-27-multi-robot-platform-design.md), [implementation plan](docs/superpowers/plans/2026-10-27-multi-robot-platform-implementation.md) and [initial verification](docs/superpowers/verification/2026-10-27-multi-robot-platform.md) and [style/camera verification](docs/superpowers/verification/2026-10-28-style-and-chase-camera.md), plus [arena-style verification](docs/superpowers/verification/2026-10-28-arena-presentation.md) and [current bounded-default verification](docs/superpowers/verification/2026-10-28-default-bounded-arena.md). ## License -The app code is released under the [Apache License 2.0](LICENSE). +The original application code is [Apache-2.0](LICENSE). Bundled third-party models, meshes, policies and samples retain their respective licenses; do **not** assume that every asset is Apache-2.0. Microban's copied XML retains its Apache-2.0 notice, and its upstream repository's mixed-license text, provenance and additional Apache notice are included in [`app/public/robots/microban/`](app/public/robots/microban/). Review those file-specific terms before redistribution. The bundled Anton font is SIL OFL-1.1; its notice is included at [`app/public/licenses/Anton-OFL.txt`](app/public/licenses/Anton-OFL.txt). diff --git a/app/.gitignore b/app/.gitignore index b947077..8c3135e 100644 --- a/app/.gitignore +++ b/app/.gitignore @@ -1,2 +1,5 @@ node_modules/ dist/ +test-results/ +playwright-report/ +.playwright-cli/ diff --git a/app/e2e/arena-presentation.spec.js b/app/e2e/arena-presentation.spec.js new file mode 100644 index 0000000..871cac3 --- /dev/null +++ b/app/e2e/arena-presentation.spec.js @@ -0,0 +1,105 @@ +import { test, expect } from "@playwright/test"; +import { bootDuck } from "./support/simulation.js"; +test("arena appearance matches Microduck while Microban physics stays flat and World visuals dispose on switch", async ({ + page, +}, info) => { + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + page.on("console", (m) => { + if (m.type() === "error") errors.push(m.text()); + }); + await bootDuck(page); + expect( + await page.evaluate(() => window.__gameApi.switchRobot("microban")), + ).toBe(true); + await page.evaluate(() => window.__gameApi.resume()); + await expect + .poll(() => page.evaluate(() => window.rl.data.time)) + .toBeGreaterThan(1); + await page.evaluate(() => window.__gameApi.pause()); + await expect + .poll(() => + page.evaluate( + () => !!window.rl.scene.getObjectByName("arena-floor-presentation"), + ), + ) + .toBe(true); + const state = await page.evaluate(() => { + const r = window.rl, + grid = r.scene.getObjectByName("arena-grid"), + u = grid.material.uniforms, + id = r.model.geom("platform_floor").id; + let floor; + r.scene.traverse((o) => { + if (o.userData.geomId === id) floor = o; + }); + window.__retiredArena = r.scene.getObjectByName("arena-floor-presentation"); + window.__arenaReleased = []; + grid.geometry.addEventListener("dispose", () => + window.__arenaReleased.push("geometry"), + ); + grid.material.addEventListener("dispose", () => + window.__arenaReleased.push("material"), + ); + return { + cell: u.uCell.value, + section: u.uSection.value, + color: u.uSectionColor.value.getHex(), + reveal: u.uReveal.value, + topo: u.uTopoScale.value, + hiddenFloor: !floor.visible, + floorType: r.model.geom_type[id], + friction: Array.from(r.model.geom_friction.slice(id * 3, id * 3 + 3)), + gravity: Array.from(r.model.opt.gravity), + heightfields: r.model.nhfield, + finite: Array.from(r.data.qpos).every(Number.isFinite), + fallen: r.fallen, + }; + }); + expect(state).toEqual({ + cell: 0.1, + section: 0.6, + color: 0xffb366, + reveal: 1, + topo: 0, + hiddenFloor: true, + floorType: 0, + friction: [1, 0.005, 0.0001], + gravity: [0, 0, -9.81], + heightfields: 0, + finite: true, + fallen: false, + }); + await page.screenshot({ path: info.outputPath("microban-arena.png") }); + await page.evaluate(() => { + const r = window.rl; + r.data.qpos[0] = 4; + r.data.qpos[1] = -3; + r.mujoco.mj_forward(r.model, r.data); + }); + await expect + .poll(() => + page.evaluate(() => { + const g = window.rl.scene.getObjectByName("arena-grid"); + return [ + g.position.x, + g.position.z, + g.material.uniforms.uFocus.value.x, + g.material.uniforms.uFocus.value.z, + ]; + }), + ) + .toEqual([4, 3, 4, 3]); + expect( + await page.evaluate(() => window.__gameApi.switchRobot("microduck-legs")), + ).toBe(true); + expect(await page.evaluate(() => window.__arenaReleased.sort())).toEqual([ + "geometry", + "material", + ]); + expect(await page.evaluate(() => window.__retiredArena.parent === null)).toBe( + true, + ); + expect(await page.evaluate(() => window.rl.chaseCam)).toBe(true); + expect(errors).toEqual([]); +}); diff --git a/app/e2e/bounded-world.spec.js b/app/e2e/bounded-world.spec.js new file mode 100644 index 0000000..dcd309d --- /dev/null +++ b/app/e2e/bounded-world.spec.js @@ -0,0 +1,149 @@ +import { test, expect } from "@playwright/test"; +import { bootDuck } from "./support/simulation.js"; +import { arenaWalls } from "../src/platform/worlds/arena.js"; +const boundary = { half_extent: 2.25, wall_height: 0.25, wall_thickness: 0.05 }; +test("Microduck and Microban share the 4.5m collision arena; visual inner faces match and C remains attached", async ({ + page, +}, info) => { + await bootDuck(page); + for (const id of ["microduck-legs", "microban"]) { + if (id === "microban") + expect( + await page.evaluate(() => window.__gameApi.switchRobot("microban")), + ).toBe(true); + const state = await page.evaluate(() => { + const r = window.rl; + return { + id: r.world.id, + boundary: r.world.boundary, + following: r.chaseCam, + walls: ["wall_px", "wall_nx", "wall_py", "wall_ny"].map((name) => { + const g = r.model.geom(name).id; + return { + name, + pos: Array.from(r.model.geom_pos.slice(g * 3, g * 3 + 3)), + size: Array.from(r.model.geom_size.slice(g * 3, g * 3 + 3)), + type: r.model.geom_type[g], + body: r.model.geom_bodyid[g], + }; + }), + visuals: [0, 1, 2, 3].map((i) => + r.scene.getObjectByName(`arena-boundary-${i}`).position.toArray(), + ), + }; + }); + expect(state.id).toBe("arena"); + expect(state.boundary).toEqual(boundary); + expect(state.following).toBe(true); + expect(state.walls).toEqual( + arenaWalls(boundary).map((w) => ({ ...w, type: 6, body: 0 })), + ); + expect(state.visuals).toEqual([ + [2.25, 0.125, 0], + [-2.25, 0.125, 0], + [0, 0.125, 2.25], + [0, 0.125, -2.25], + ]); + } + // QA overview only; normal entry remains the rear chase framing above. + await page.locator("canvas").click({ position: { x: 1000, y: 700 } }); + await page.evaluate(() => { + const r = window.rl; + r.controls.maxDistance = 8; + r.camera.position.set(-3, 3, 3); + r.controls.target.set(0, 0, 0); + r.controls.update(); + }); + await page.screenshot({ path: info.outputPath("bounded-arena.png") }); +}); +test("a future Package with no world field automatically loads the bounded default", async ({ + page, +}) => { + await page.route("**/robots/index.json*", async (route) => { + const response = await route.fetch(), + entries = await response.json(); + entries.push({ + id: "future-bot", + family: "future-bot", + name: "Future Robot", + manifest: "microban/robot.json?future=1", + }); + await route.fulfill({ response, json: entries }); + }); + await page.route("**/robots/microban/robot.json?future=1", async (route) => { + const response = await route.fetch(), + config = await response.json(); + config.id = config.family = "future-bot"; + delete config.presentation.world; + await route.fulfill({ response, json: config }); + }); + await bootDuck(page); + expect( + await page.evaluate(() => window.__gameApi.switchRobot("future-bot")), + ).toBe(true); + expect( + await page.evaluate(() => ({ + id: window.__store.getState().robotId, + world: window.rl.world.id, + bounds: window.rl.world.boundary, + })), + ).toEqual({ id: "future-bot", world: "arena", bounds: boundary }); +}); +test("real MuJoCo wall contacts block commanded motion on all four sides without coordinate clamping", async ({ + page, +}, info) => { + await bootDuck(page); + expect( + await page.evaluate(() => window.__gameApi.switchRobot("microban")), + ).toBe(true); + const results = await page.evaluate(async () => { + const r = window.rl, + report = []; + for (const [name, axis, sign, yaw] of [ + ["wall_px", 0, 1, 0], + ["wall_nx", 0, -1, Math.PI], + ["wall_py", 1, 1, Math.PI / 2], + ["wall_ny", 1, -1, -Math.PI / 2], + ]) { + await r.resetSim(); + const id = r.model.geom(name).id; + r.data.qpos[axis] = sign * 2.05; + r.data.qpos[3] = Math.cos(yaw / 2); + r.data.qpos[6] = Math.sin(yaw / 2); + r.mujoco.mj_forward(r.model, r.data); + r.command.set([0.15, 0, 0]); + let contacts = 0, + max = sign * r.data.qpos[axis]; + for (let tick = 0; tick < 30; tick++) { + await r.step(5); + max = Math.max(max, sign * r.data.qpos[axis]); + const list = r.data.contact; + for (let i = 0; i < r.data.ncon; i++) { + const c = list.get(i); + if (c.geom1 === id || c.geom2 === id) contacts++; + c.delete(); + } + list.delete(); + } + report.push({ + name, + contacts, + max, + time: r.data.time, + fallen: r.fallen, + finite: Array.from(r.data.qpos).every(Number.isFinite), + }); + } + return report; + }); + for (const r of results) { + expect(r.contacts, JSON.stringify(r)).toBeGreaterThan(0); + expect(r.max, JSON.stringify(r)).toBeLessThan(2.25); + expect(r.time).toBeCloseTo(3, 6); + expect(r.finite).toBe(true); + } + await info.attach("wall-contacts.json", { + body: JSON.stringify(results, null, 2), + contentType: "application/json", + }); +}); diff --git a/app/e2e/chase-camera.spec.js b/app/e2e/chase-camera.spec.js new file mode 100644 index 0000000..0da208a --- /dev/null +++ b/app/e2e/chase-camera.spec.js @@ -0,0 +1,125 @@ +import { test, expect } from "@playwright/test"; +import { bootDuck } from "./support/simulation.js"; +async function pose(page) { + return page.evaluate(() => { + const r = window.rl, + q = r.data.qpos, + offset = r.camera.position.clone().sub(r.controls.target), + h = Math.hypot(offset.x, offset.z), + yaw = Math.atan2( + 2 * (q[3] * q[6] + q[4] * q[5]), + 1 - 2 * (q[5] ** 2 + q[6] ** 2), + ); + return { + rear: (-Math.cos(yaw) * offset.x + Math.sin(yaw) * offset.z) / h, + pitch: Math.atan2(offset.y, h), + distance: offset.length(), + following: r.chaseCam, + time: r.data.time, + }; + }); +} +async function rear(page) { + await expect + .poll(async () => { + const p = await pose(page); + return ( + p.following && + p.rear > 0.999 && + Math.abs(p.pitch - 0.42) < 0.005 && + Math.abs(p.distance - 0.8) < 0.005 + ); + }) + .toBe(true); +} +async function orbit(page) { + const canvas = page.locator("canvas"), + rect = await canvas.boundingBox(); + await page.mouse.move(rect.x + rect.width * 0.7, rect.y + rect.height * 0.55); + await page.mouse.down(); + await page.mouse.move( + rect.x + rect.width * 0.85, + rect.y + rect.height * 0.7, + { steps: 8 }, + ); + await page.mouse.up(); + expect((await pose(page)).following).toBe(false); +} +test("both robots default to the same rear camera and comic UI; Microban keeps flat World and valid controls", async ({ + page, +}, info) => { + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + await bootDuck(page); + await rear(page); + await page.evaluate(() => window.__gameApi.switchRobot("microban")); + await rear(page); + expect( + await page.evaluate(() => window.__store.getState().presentationTheme), + ).toBe("comic"); + await expect( + page.getByRole("navigation", { name: "Simulation controls" }), + ).toHaveCSS("border-top-width", "2px"); + await expect(page.getByRole("group", { name: "Duck colours" })).toHaveCount( + 0, + ); + await expect( + page.getByRole("button", { name: "Rollers", exact: true }), + ).toHaveCount(0); + await expect(page.getByText("Drag robot / ball: apply force")).toHaveCount(0); + await expect(page.getByRole("status")).toContainText("Simplified actuation"); + expect( + await page.evaluate(() => window.rl.model.geom("platform_floor").id), + ).toBeGreaterThanOrEqual(0); + await page.screenshot({ path: info.outputPath("microban-chase.png") }); + await page.evaluate(() => window.__gameApi.switchRobot("microduck-legs")); + await rear(page); + await expect(page.getByRole("group", { name: "Duck colours" })).toBeVisible(); + expect(errors).toEqual([]); +}); +for (const id of ["microban", "microduck-legs"]) + test(`${id}: orbit detaches; C restores behind current heading, repeatedly and while paused`, async ({ + page, + }) => { + await bootDuck(page); + if (id === "microban") + await page.evaluate(() => window.__gameApi.switchRobot("microban")); + await page.evaluate(() => window.__gameApi.pause()); + await orbit(page); + await page.evaluate(() => { + const r = window.rl; + r.data.qpos[0] = 1.25; + r.data.qpos[1] = -0.5; + r.data.qpos[3] = Math.cos(Math.PI / 4); + r.data.qpos[4] = r.data.qpos[5] = 0; + r.data.qpos[6] = Math.sin(Math.PI / 4); + r.mujoco.mj_forward(r.model, r.data); + }); + const time = (await pose(page)).time; + await page.keyboard.press("c"); + await rear(page); + expect((await pose(page)).time).toBe(time); + await page.keyboard.press("c"); + await rear(page); + await orbit(page); + await page + .getByRole("button", { name: "Restore chase camera", exact: false }) + .click(); + await rear(page); + expect(await page.evaluate(() => window.__store.getState().paused)).toBe( + true, + ); + if (id === "microban") { + await page.evaluate(() => { + const r = window.rl; + r.data.qpos[3] = 0; + r.data.qpos[6] = 1; + r.mujoco.mj_forward(r.model, r.data); + }); + // Microduck-style yaw hysteresis suppresses gait wobble; continuous + // following may settle a few degrees off yaw. C above is exact. + await expect + .poll(async () => (await pose(page)).rear) + .toBeGreaterThan(0.99); + } + }); diff --git a/app/e2e/deployment.spec.js b/app/e2e/deployment.spec.js new file mode 100644 index 0000000..64b5e5e --- /dev/null +++ b/app/e2e/deployment.spec.js @@ -0,0 +1,42 @@ +import { test, expect } from "@playwright/test"; +test.use({ video: { mode: "on", size: { width: 1280, height: 800 } } }); +test("deployed assets respect the base path and both Robot Packages boot", async ({ + page, +}, info) => { + const failures = [], + errors = [], + requests = []; + page.on("pageerror", (e) => errors.push(e.message)); + page.on("response", (r) => { + if ( + r.status() >= 400 && + new URL(r.url()).origin === new URL(page.url()).origin + ) + failures.push(`${r.status()} ${r.url()}`); + }); + page.on("request", (r) => requests.push(r.url())); + const prefix = process.env.DEPLOY_PATH || "/"; + await page.goto(prefix); + await expect( + page.getByRole("button", { name: "Pause", exact: true }), + ).toBeEnabled(); + await page.waitForTimeout(500); // capture after the ball's existing appear FX + await page.screenshot({ path: info.outputPath("microduck.png") }); + await page.getByLabel("Robot", { exact: true }).selectOption("microban"); + await expect + .poll(() => page.evaluate(() => window.__store.getState().robotId)) + .toBe("microban"); + await expect(page.getByRole("status")).toContainText("Simplified actuation"); + await page.waitForTimeout(500); + await page.screenshot({ path: info.outputPath("microban.png") }); + expect(errors).toEqual([]); + expect(failures).toEqual([]); + const origin = new URL(page.url()).origin; + expect(requests.filter((url) => new URL(url).origin !== origin)).toEqual([]); + expect( + requests.filter( + (url) => + url.startsWith(origin) && !new URL(url).pathname.startsWith(prefix), + ), + ).toEqual([]); +}); diff --git a/app/e2e/fixtures/microban-probe.html b/app/e2e/fixtures/microban-probe.html new file mode 100644 index 0000000..99a57bd --- /dev/null +++ b/app/e2e/fixtures/microban-probe.html @@ -0,0 +1,10 @@ + + +
+Loading actual MuJoCo and ONNX WASM…+ + + diff --git a/app/e2e/fixtures/microban-probe.js b/app/e2e/fixtures/microban-probe.js new file mode 100644 index 0000000..84a067d --- /dev/null +++ b/app/e2e/fixtures/microban-probe.js @@ -0,0 +1,65 @@ +// Render-free integration probe uses the SAME runtime as the product; no +// second observation builder/PD implementation can drift from production. +import * as THREE from "three"; +import { RobotRuntime } from "../../src/platform/runtime/RobotRuntime.js"; +import { + loadPackage, + loadWorld, +} from "../../src/platform/loader/PackageLoader.js"; +window.probeReady = (async () => { + const pkg = await loadPackage( + `${import.meta.env.BASE_URL}robots/microban/robot.json`, + ), + world = await loadWorld("flat"); + const runtime = await RobotRuntime.load({ + pkg, + world, + scene: new THREE.Group(), + camera: new THREE.PerspectiveCamera(), + renderer: null, + }); + const { sim, ctx } = runtime; + const state = () => { + const a = sim.rootQpos, + q = sim.data.qpos, + b = sim.root * 4, + quat = sim.data.xquat, + w = quat[b], + x = quat[b + 1], + y = quat[b + 2], + z = quat[b + 3]; + return { + time: sim.data.time, + pos: Array.from(q.slice(a, a + 3)), + gravity: [ + 2 * (w * y - x * z), + -2 * (y * z + w * x), + -(1 - 2 * (x * x + y * y)), + ], + yaw: Math.atan2(2 * (w * z + x * y), 1 - 2 * (y * y + z * z)), + }; + }; + window.probe = { + model: sim.model, + data: sim.data, + command: runtime.input.command, + state, + buildObs: () => { + ctx.command.set(runtime.input.command); + return runtime.obs.write().obs; + }, + reset: () => runtime.reset(), + step: async (count) => { + await runtime.scheduler.step(count); + if (runtime.fallen) throw new Error(`Fallen at ${sim.data.time}`); + return state(); + }, + dispose: () => runtime.dispose(), + }; + document.querySelector("#status").textContent = + "Ready: generic runtime / 19 joints / 18 actor / 63 obs"; +})().catch((error) => { + window.probeError = error.stack; + document.querySelector("#status").textContent = error.stack; + throw error; +}); diff --git a/app/e2e/fixtures/render-fixture.html b/app/e2e/fixtures/render-fixture.html new file mode 100644 index 0000000..ae1b66f --- /dev/null +++ b/app/e2e/fixtures/render-fixture.html @@ -0,0 +1,75 @@ + + + + + + diff --git a/app/e2e/microban-observation.spec.js b/app/e2e/microban-observation.spec.js new file mode 100644 index 0000000..f452cd6 --- /dev/null +++ b/app/e2e/microban-observation.spec.js @@ -0,0 +1,30 @@ +import { test, expect } from "@playwright/test"; +test("mounted IMU gyro stays in site frame while projected gravity uses trunk frame", async ({ + page, +}) => { + await page.goto("/"); + await expect( + page.getByRole("button", { name: "Pause", exact: true }), + ).toBeEnabled(); + await page.getByLabel("Robot", { exact: true }).selectOption("microban"); + await expect + .poll(() => page.evaluate(() => window.__store.getState().robotId)) + .toBe("microban"); + await expect( + page.getByRole("button", { name: "Pause", exact: true }), + ).toBeEnabled(); + const obs = await page.evaluate(async () => { + const r = window.rl; + await r.pause(); + r.data.qpos.set([Math.cos(Math.PI / 8), 0, Math.sin(Math.PI / 8), 0], 3); + r.data.qvel.set([1, 2, 3], 3); + r.mujoco.mj_forward(r.model, r.data); + return Array.from(r.buildObs()); + }); + expect(obs.length).toBe(63); + for (const [i, value] of [2, -3, -1].entries()) + expect(obs[i]).toBeCloseTo(value, 5); + expect(obs[3]).toBeCloseTo(Math.SQRT1_2, 5); + expect(obs[4]).toBeCloseTo(0, 5); + expect(obs[5]).toBeCloseTo(-Math.SQRT1_2, 5); +}); diff --git a/app/e2e/microban-probe.spec.js b/app/e2e/microban-probe.spec.js new file mode 100644 index 0000000..68aaf5f --- /dev/null +++ b/app/e2e/microban-probe.spec.js @@ -0,0 +1,37 @@ +import { test, expect } from "@playwright/test"; +test("Microban actual WASM simplified PD stands, walks and turns without reset", async ({ + page, +}, testInfo) => { + await page.goto("/e2e/fixtures/microban-probe.html"); + await page.waitForFunction(() => window.probe || window.probeError); + expect(await page.evaluate(() => window.probeError)).toBeUndefined(); + const result = await page.evaluate(async () => { + const p = window.probe, + segments = []; + for (const [name, seconds, vx, wz] of [ + ["stand", 10, 0, 0], + ["forward", 20, 0.15, 0], + ["rest", 3, 0, 0], + ["backward", 10, -0.1, 0], + ["rest", 3, 0, 0], + ["left", 10, 0, 0.6], + ["rest", 3, 0, 0], + ["right", 10, 0, -0.6], + ]) { + const before = p.state(); + p.command.set([vx, 0, wz]); + await p.step(seconds * 50); + segments.push({ name, before, after: p.state() }); + } + return segments; + }); + await testInfo.attach("microban-probe.json", { + body: JSON.stringify(result, null, 2), + contentType: "application/json", + }); + const forward = result.find((s) => s.name === "forward"); + expect(forward.after.pos[0] - forward.before.pos[0]).toBeGreaterThan(0.5); + const backward = result.find((s) => s.name === "backward"); + expect(backward.after.pos[0] - backward.before.pos[0]).toBeLessThan(-0.2); + console.log(JSON.stringify(result)); +}); diff --git a/app/e2e/microban-walking.spec.js b/app/e2e/microban-walking.spec.js new file mode 100644 index 0000000..a72a85e --- /dev/null +++ b/app/e2e/microban-walking.spec.js @@ -0,0 +1,181 @@ +import { test, expect } from "@playwright/test"; +const boot = async (page, world) => { + if (world) + await page.route("**/robots/microban/robot.json*", async (route) => { + const response = await route.fetch(), + config = await response.json(); + config.presentation.world = world; + await route.fulfill({ response, json: config }); + }); + await page.goto("/"); + await expect( + page.getByRole("button", { name: "Pause", exact: true }), + ).toBeEnabled(); + await page.getByLabel("Robot", { exact: true }).selectOption("microban"); + await expect + .poll(() => page.evaluate(() => window.__store.getState().robotId)) + .toBe("microban"); + await expect( + page.getByRole("button", { name: "Pause", exact: true }), + ).toBeEnabled(); +}; +for (const mode of [ + { id: "bounded", forward: 8, back: 8, seconds: 70 }, + { + id: "unbounded-diagnostic", + forward: 20, + back: 10, + seconds: 84, + world: "flat", + }, +]) + test(`three ${mode.seconds}s physics trials (${mode.id}): stand, forward, back, turn, and parallel head`, async ({ + page, + }, info) => { + await boot(page, mode.world); + await page.evaluate(() => window.rl.pause()); + const report = await page.evaluate(async (mode) => { + const results = [], + r = window.rl; + const pose = () => { + const [w, x, y, z] = r.data.qpos.slice(3, 7); + return { + time: r.data.time, + pos: Array.from(r.data.qpos.slice(0, 3)), + yaw: Math.atan2(2 * (w * z + x * y), 1 - 2 * (y * y + z * z)), + head: r.data.qpos[r.model.jnt("head").qposadr], + }; + }; + for (let trial = 0; trial < 3; trial++) { + await r.resetSim(); + const segments = []; + for (const [name, seconds, vx, wz] of [ + ["stand", 10, 0, 0], + ["forward", mode.forward, 0.15, 0], + ["rest", 3, 0, 0], + ["backward", mode.back, -0.1, 0], + ["rest", 3, 0, 0], + ["left", 10, 0, 0.6], + ["rest", 3, 0, 0], + ["right", 10, 0, -0.6], + ["rest", 3, 0, 0], + ["head_walk", 10, 0.15, 0], + ["head_stop", 2, 0.15, 0], + ]) { + const before = pose(); + r.command.set([vx, 0, wz]); + if (name.startsWith("head_")) { + const previous = Array.from(r.lastAction); + r.toggle("head"); + if (previous.some((v, i) => v !== r.lastAction[i])) + throw new Error("head toggle mutated policy history"); + } + let yaw = 0, + previous = before.yaw, + minHead = Infinity, + maxHead = -Infinity, + minHeight = Infinity; + for (let i = 0; i < seconds * 10; i++) { + await r.step(5); + const p = pose(); + let delta = p.yaw - previous; + while (delta > Math.PI) delta -= 2 * Math.PI; + while (delta < -Math.PI) delta += 2 * Math.PI; + yaw += delta; + previous = p.yaw; + minHead = Math.min(minHead, p.head); + maxHead = Math.max(maxHead, p.head); + minHeight = Math.min(minHeight, p.pos[2]); + if (r.fallen) + throw new Error(`Fallen trial ${trial} ${name} at ${p.time}`); + if ( + mode.id === "bounded" && + Math.max(Math.abs(p.pos[0]), Math.abs(p.pos[1])) > + r.world.boundary.half_extent - 0.05 + ) + throw new Error( + `Bounded gait trial reached a wall: ${name} ${p.pos}`, + ); + } + const after = pose(), + dx = after.pos[0] - before.pos[0], + dy = after.pos[1] - before.pos[1]; + segments.push({ + name, + before, + after, + localDisplacement: + dx * Math.cos(before.yaw) + dy * Math.sin(before.yaw), + yaw, + minHead, + maxHead, + minHeight, + }); + } + results.push({ trial, segments, actuation: r.actuationStats }); + } + return results; + }, mode); + for (const trial of report) { + const find = (name) => trial.segments.find((s) => s.name === name); + expect(find("forward").localDisplacement).toBeGreaterThan(0.5); + expect(find("backward").localDisplacement).toBeLessThan(-0.2); + expect(find("left").yaw).toBeGreaterThan(1); + expect(find("right").yaw).toBeLessThan(-1); + expect( + find("head_walk").maxHead - find("head_walk").minHead, + ).toBeGreaterThan(1); + expect(Math.abs(find("head_stop").after.head)).toBeLessThan(0.05); + expect(trial.actuation.longestSaturatedSeconds).toBeLessThan(0.5); + } + await info.attach(`microban-${mode.id}-three-trials.json`, { + body: JSON.stringify(report, null, 2), + contentType: "application/json", + }); + console.log( + `Microban: 3 × ${mode.seconds}s ${mode.id}, no falls/resets, correct motion and independent head.`, + ); + }); +test("keyboard H toggles head, fallen Space resets, and real-time control runs near 50Hz", async ({ + page, +}, info) => { + await boot(page); + await page + .locator("canvas") + .first() + .click({ position: { x: 700, y: 600 } }); + await page.keyboard.press("h"); + expect( + await page.evaluate(() => window.rl.skills.overlays.get("head").enabled), + ).toBe(true); + const first = await page.evaluate(() => ({ + sim: window.rl.data.time, + wall: performance.now(), + })); + await page.waitForTimeout(5000); + const second = await page.evaluate(() => ({ + sim: window.rl.data.time, + wall: performance.now(), + fallen: window.rl.fallen, + })); + const ratio = (second.sim - first.sim) / ((second.wall - first.wall) / 1000); + expect(second.fallen).toBe(false); + expect(ratio).toBeGreaterThan(0.85); + expect(ratio).toBeLessThan(1.15); + await info.attach("real-time.json", { + body: JSON.stringify({ first, second, ratio }), + contentType: "application/json", + }); + await page.evaluate(() => { + const r = window.rl; + r.data.qpos[3] = Math.SQRT1_2; + r.data.qpos[4] = Math.SQRT1_2; + r.data.qpos[5] = 0; + r.data.qpos[6] = 0; + r.mujoco.mj_forward(r.model, r.data); + }); + await expect.poll(() => page.evaluate(() => window.rl.fallen)).toBe(true); + await expect(page.getByRole("status")).toContainText("Robot fallen"); + await page.keyboard.press("Space"); + await expect.poll(() => page.evaluate(() => window.rl.fallen)).toBe(false); +}); diff --git a/app/e2e/microduck-mouse.spec.js b/app/e2e/microduck-mouse.spec.js new file mode 100644 index 0000000..e177dca --- /dev/null +++ b/app/e2e/microduck-mouse.spec.js @@ -0,0 +1,67 @@ +import { test, expect } from "@playwright/test"; +import { bootDuck } from "./support/simulation.js"; +test("orbit camera stays synchronized with waypoint picking and physical drag", async ({ + page, +}) => { + await bootDuck(page); + await page.evaluate(() => { + const r = window.rl; + r.chaseCam = false; + r.camera.position.set(-0.5, 0.6, 1.2); + r.controls.target.set(-0.6, 0.12, 0); + r.controls.update(); + r.camera.updateMatrixWorld(); + }); + await page.evaluate( + () => + new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r))), + ); + const floor = await page.evaluate(() => { + const r = window.rl, + p = r.controls.target.clone().set(-0.95, 0, 0.25).project(r.camera), + rect = document.querySelector("canvas").getBoundingClientRect(); + return { + x: rect.left + ((p.x + 1) * rect.width) / 2, + y: rect.top + ((1 - p.y) * rect.height) / 2, + }; + }); + await page.mouse.click(floor.x, floor.y); + const target = await page.evaluate( + () => window.rl.controller.sources.find((s) => s.id === "waypoint").target, + ); + expect(target[0]).toBeCloseTo(-0.95, 3); + expect(target[1]).toBeCloseTo(-0.25, 3); + await page.evaluate(() => + window.rl.controller.sources.find((s) => s.id === "waypoint").cancel(), + ); + const robot = await page.evaluate(() => { + const r = window.rl, + q = r.data.qpos, + p = r.controls.target + .clone() + .set(q[0], q[2] + 0.02, -q[1]) + .project(r.camera), + rect = document.querySelector("canvas").getBoundingClientRect(); + return { + x: rect.left + ((p.x + 1) * rect.width) / 2, + y: rect.top + ((1 - p.y) * rect.height) / 2, + }; + }); + await page.mouse.move(robot.x, robot.y); + await page.mouse.down(); + expect(await page.evaluate(() => window.rl.controls.enabled)).toBe(false); + await page.mouse.move(robot.x, robot.y - 40, { steps: 3 }); + await page.evaluate(() => window.rl.step(2)); + expect( + await page.evaluate(() => + Array.from(window.rl.data.xfrc_applied).some((v) => Math.abs(v) > 1e-6), + ), + ).toBe(true); + await page.mouse.up(); + expect(await page.evaluate(() => window.rl.controls.enabled)).toBe(true); + expect( + await page.evaluate(() => + Array.from(window.rl.data.xfrc_applied).every((v) => v === 0), + ), + ).toBe(true); +}); diff --git a/app/e2e/microduck-regression.spec.js b/app/e2e/microduck-regression.spec.js new file mode 100644 index 0000000..ca2bcd6 --- /dev/null +++ b/app/e2e/microduck-regression.spec.js @@ -0,0 +1,86 @@ +import { test, expect } from "@playwright/test"; +import fs from "node:fs/promises"; +import { fileURLToPath } from "node:url"; +import { bootDuck, snapshot } from "./support/simulation.js"; + +const fixtures = fileURLToPath( + new URL("../test/fixtures/microduck/", import.meta.url), +); +for (const scenario of [ + "stand", + "walk", + "kick", + "sitstand", + "groundpick", + "roll", + "recovery", + "rollers", + "crouch", +]) { + test(`Microduck deterministic ${scenario}`, async ({ page }) => { + // Keep the ORIGINAL 3m World for the accepted original-code oracle. + // The current 4.5m production default is independently tested in bounded-world. + await page.route("**/worlds/arena/world.json*", async (route) => { + const response = await route.fetch(), + world = await response.json(); + world.boundary.half_extent = 1.5; + await route.fulfill({ response, json: world }); + }); + const errors = await bootDuck(page); + await page.evaluate(async (scenario) => { + const r = window.rl; + if (scenario === "rollers" || scenario === "crouch") { + await r.setLoco("rollers", { force: true }); + await r.prepareTest(); + } + r.resetSim(); + await r.prepareTest(); + await r.step(50); + if (scenario === "walk" || scenario === "rollers") + r.velCmd[0] = scenario === "walk" ? 0.1 : 0.2; + if (scenario === "kick") r.triggerKick("left"); + if (scenario === "sitstand") r.setMode("sit"); + if (scenario === "groundpick") r.triggerGroundPick(); + if (scenario === "roll") r.triggerRoll(); + if (scenario === "crouch") r.triggerCrouch(); + if (scenario === "recovery") { + r.data.qpos[3] = Math.SQRT1_2; + r.data.qpos[4] = Math.SQRT1_2; + r.mujoco.mj_forward(r.model, r.data); + } + }, scenario); + // Preserve the accepted input sequence: latch the sit flag before the + // recorded physics interval. Timers now use a controllable sim clock. + if (scenario === "sitstand") + await page.evaluate(() => window.rl.advanceTimers(0.85)); + const trajectory = []; + for (let i = 0; i < 6; i++) { + await page.evaluate(() => window.rl.step(25)); + trajectory.push(await snapshot(page)); + } + const path = `${fixtures}${scenario}.json`; + if (process.env.RECORD_BASELINE === "1") { + const candidates = fileURLToPath( + new URL("../test-results/baseline-candidates/", import.meta.url), + ); + await fs.mkdir(candidates, { recursive: true }); + // Never overwrite accepted goldens: candidates require explicit review. + await fs.writeFile( + `${candidates}${scenario}.json`, + JSON.stringify( + { + scenario, + backend: "mujoco-3.11.0/ort-wasm-single-thread", + trajectory, + }, + null, + 2, + ) + "\n", + ); + } else { + const expected = JSON.parse(await fs.readFile(path, "utf8")); + expect(trajectory).toEqual(expected.trajectory); + } + expect(errors).toEqual([]); + }); +} diff --git a/app/e2e/mjcf-renderer.spec.js b/app/e2e/mjcf-renderer.spec.js new file mode 100644 index 0000000..19cd89e --- /dev/null +++ b/app/e2e/mjcf-renderer.spec.js @@ -0,0 +1,17 @@ +import { test, expect } from "@playwright/test"; +test("compiled mesh/primitive transforms and reversed actuator order are resolved correctly", async ({ + page, +}) => { + await page.goto("/e2e/fixtures/render-fixture.html"); + await page.waitForFunction(() => window.renderResult || window.renderError); + expect(await page.evaluate(() => window.renderError)).toBeUndefined(); + const result = await page.evaluate(() => window.renderResult); + expect(result.maxError).toBeLessThan(1e-12); + expect(result.count).toBe(8); + expect(result.a).toBe(1); + expect(result.b).toBe(0); + expect(result.missing).toContain("absent"); + expect(result.color[0]).toBeCloseTo(0.1, 6); + expect(result.color[1]).toBeCloseTo(0.2, 6); + expect(result.color[2]).toBeCloseTo(0.3, 6); +}); diff --git a/app/e2e/robot-switching.spec.js b/app/e2e/robot-switching.spec.js new file mode 100644 index 0000000..2682560 --- /dev/null +++ b/app/e2e/robot-switching.spec.js @@ -0,0 +1,64 @@ +import { test, expect } from "@playwright/test"; +async function ready(page) { + await page.goto("/"); + await expect( + page.getByRole("button", { name: "Pause", exact: true }), + ).toBeEnabled(); +} +test("Microduck → Microban → Microduck restores theme, colours and locomotion", async ({ + page, +}) => { + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + await ready(page); + await page.getByRole("button", { name: "Lavender colours" }).click(); + await page.getByRole("button", { name: "Rollers", exact: true }).click(); + await page.waitForFunction(() => window.rl?.loco === "rollers"); + await page.getByLabel("Robot", { exact: true }).selectOption("microban"); + await page.waitForFunction( + () => + window.__store.getState().robotId === "microban" || + window.__store.getState().runtimeError, + ); + expect( + await page.evaluate(() => window.__store.getState().runtimeError), + ).toBeNull(); + await expect( + page.getByText("Toggle head rotation", { exact: false }), + ).toBeVisible(); + await expect(page.getByRole("group", { name: "Duck colours" })).toHaveCount( + 0, + ); + expect(await page.evaluate(() => window.rl.buildObs().length)).toBe(63); + await page.getByLabel("Robot", { exact: true }).selectOption("microduck"); + await page.waitForFunction( + () => + window.__store.getState().presentationTheme === "comic" && + window.rl?.loco === "rollers", + ); + await expect( + page.getByRole("button", { name: "Lavender colours" }), + ).toHaveAttribute("aria-pressed", "true"); + expect(await page.evaluate(() => window.rl.data.time)).toBeLessThan(3); + expect(errors).toEqual([]); +}); +test("failed switch rolls back and retry does not reload the page", async ({ + page, +}) => { + await ready(page); + let fail = true; + await page.route("**/robots/microban/policies/walk.onnx", (route) => + fail ? route.fulfill({ status: 404, body: "missing" }) : route.continue(), + ); + await page.getByLabel("Robot", { exact: true }).selectOption("microban"); + await expect(page.getByRole("alert")).toContainText("404"); + expect(await page.evaluate(() => window.__store.getState().robotId)).toBe( + "microduck-legs", + ); + expect(await page.evaluate(() => window.rl.running)).toBe(true); + fail = false; + await page.getByRole("button", { name: "Retry", exact: true }).click(); + await expect + .poll(() => page.evaluate(() => window.__store.getState().robotId)) + .toBe("microban"); +}); diff --git a/app/e2e/runtime-leaks.spec.js b/app/e2e/runtime-leaks.spec.js new file mode 100644 index 0000000..036712f --- /dev/null +++ b/app/e2e/runtime-leaks.spec.js @@ -0,0 +1,84 @@ +import { test, expect } from "@playwright/test"; +// All twenty transitions run in one page and one shared WASM instance. +test("20 robot transitions release old WASM objects and plateau scene/GPU/listeners", async ({ + page, +}, info) => { + test.setTimeout(180_000); + await page.addInitScript(() => { + const add = EventTarget.prototype.addEventListener, + remove = EventTarget.prototype.removeEventListener, + targets = new Map(); + const key = (type, options) => + `${type}:${typeof options === "boolean" ? options : !!options?.capture}`; + EventTarget.prototype.addEventListener = function (type, cb, options) { + if (this === window || this instanceof HTMLCanvasElement) { + let entries = targets.get(this); + if (!entries) { + entries = new Map(); + targets.set(this, entries); + } + const k = key(type, options); + if (!entries.has(k)) entries.set(k, new Set()); + entries.get(k).add(cb); + } + return add.call(this, type, cb, options); + }; + EventTarget.prototype.removeEventListener = function (type, cb, options) { + targets.get(this)?.get(key(type, options))?.delete(cb); + return remove.call(this, type, cb, options); + }; + window.listenerCount = () => + [...targets.values()].reduce( + (n, entries) => + n + [...entries.values()].reduce((a, set) => a + set.size, 0), + 0, + ); + }); + await page.goto("/"); + await expect( + page.getByRole("button", { name: "Pause", exact: true }), + ).toBeEnabled(); + await page.evaluate(() => window.__gameApi.pause()); + const measurements = []; + for (let i = 0; i < 20; i++) { + const id = i % 2 === 0 ? "microban" : "microduck-legs"; + console.log(`Ownership transition ${i + 1}/20: ${id}`); + const sample = await page.evaluate(async (id) => { + const model = window.rl.model, + data = window.rl.data; + const success = await window.__gameApi.switchRobot(id); + await new Promise((r) => + requestAnimationFrame(() => requestAnimationFrame(r)), + ); + return { + id, + success, + modelDeleted: model.isDeleted(), + dataDeleted: data.isDeleted(), + ...window.__gameApi.diagnostics(), + listeners: window.listenerCount(), + }; + }, id); + expect(sample.success).toBe(true); + expect(sample.modelDeleted).toBe(true); + expect(sample.dataDeleted).toBe(true); + measurements.push(sample); + } + const duck = measurements.filter((s) => s.id === "microduck-legs"), + ban = measurements.filter((s) => s.id === "microban"); + for (const samples of [duck, ban]) { + expect(samples.at(-1).listeners).toBe(samples[0].listeners); + expect(samples.at(-1).sceneChildren).toBe(samples[0].sceneChildren); + expect(samples.at(-1).memory.geometries).toBeLessThanOrEqual( + samples[1].memory.geometries + 1, + ); + expect(samples.at(-1).memory.textures).toBeLessThanOrEqual( + samples[1].memory.textures + 1, + ); + } + await info.attach("runtime-ownership.json", { + body: JSON.stringify(measurements, null, 2), + contentType: "application/json", + }); + console.log(JSON.stringify(measurements)); +}); diff --git a/app/e2e/runtime-races.spec.js b/app/e2e/runtime-races.spec.js new file mode 100644 index 0000000..0d1f31f --- /dev/null +++ b/app/e2e/runtime-races.spec.js @@ -0,0 +1,61 @@ +import { test, expect } from "@playwright/test"; +test("switch is single-flight, drains old ticks, clears held keys and preserves pause intent", async ({ + page, +}) => { + await page.goto("/"); + await expect( + page.getByRole("button", { name: "Pause", exact: true }), + ).toBeEnabled(); + await page.evaluate(() => window.__gameApi.pause()); + let release, + seen = false; + const gate = new Promise((r) => (release = r)); + await page.route("**/robots/microban/model.xml", async (route) => { + seen = true; + await gate; + await route.continue(); + }); + await page.evaluate(() => { + window.switchPending = window.__gameApi.switchRobot("microban"); + }); + await expect.poll(() => seen).toBe(true); + const time = await page.evaluate(() => window.rl.data.time); + await page.waitForTimeout(150); + expect(await page.evaluate(() => window.rl.data.time)).toBe(time); + expect( + await page.evaluate(() => window.__gameApi.switchRobot("microduck-legs")), + ).toBe(false); + await page.keyboard.down("w"); + release(); + expect(await page.evaluate(() => window.switchPending)).toBe(true); + expect(await page.evaluate(() => window.__store.getState().paused)).toBe( + true, + ); + expect(await page.evaluate(() => window.rl.running)).toBe(false); + expect(await page.evaluate(() => Array.from(window.rl.command))).toEqual([ + 0, 0, 0, + ]); + await page.keyboard.up("w"); + await page.evaluate(() => window.rl.step(10)); + await page.keyboard.press("Space"); + await expect.poll(() => page.evaluate(() => window.rl.data.time)).toBe(0); + expect(await page.evaluate(() => window.rl.running)).toBe(false); +}); +test("GLB load failure can retry without poisoned shared cache", async ({ + page, +}) => { + let failed = false; + await page.route("**/microduck.glb?*", (route) => { + if (!failed) { + failed = true; + return route.fulfill({ status: 503, body: "missing" }); + } + return route.continue(); + }); + await page.goto("/"); + await expect(page.getByRole("alert")).toContainText("Simulation error"); + await page.getByRole("button", { name: "Retry", exact: true }).click(); + await expect( + page.getByRole("button", { name: "Pause", exact: true }), + ).toBeEnabled(); +}); diff --git a/app/e2e/runtime-smoke.spec.js b/app/e2e/runtime-smoke.spec.js new file mode 100644 index 0000000..58392aa --- /dev/null +++ b/app/e2e/runtime-smoke.spec.js @@ -0,0 +1,23 @@ +import { test, expect } from "@playwright/test"; +import { bootDuck } from "./support/simulation.js"; +test("real WASM boots and manual steps exclusively own physics time", async ({ + page, +}) => { + const errors = await bootDuck(page); + const result = await page.evaluate(async () => { + const r = window.rl; + const t0 = r.data.time; + await r.step(10); + const t1 = r.data.time; + await new Promise((resolve) => setTimeout(resolve, 150)); + return { + dt: t1 - t0, + idleDt: r.data.time - t1, + finite: [...r.data.qpos].every(Number.isFinite), + }; + }); + expect(result.dt).toBeCloseTo(0.2, 10); + expect(result.idleDt).toBe(0); + expect(result.finite).toBe(true); + expect(errors).toEqual([]); +}); diff --git a/app/e2e/simulation-shell.spec.js b/app/e2e/simulation-shell.spec.js new file mode 100644 index 0000000..46d8f97 --- /dev/null +++ b/app/e2e/simulation-shell.spec.js @@ -0,0 +1,92 @@ +import { test, expect } from "@playwright/test"; +test("automatically runs with inline pause, reset and keyboard help; no menu assets", async ({ + page, +}) => { + const errors = [], + requests = []; + page.on("pageerror", (e) => errors.push(e.message)); + page.on("request", (r) => requests.push(r.url())); + await page.goto("/"); + await expect( + page.getByRole("button", { name: "Pause", exact: true }), + ).toBeEnabled(); + expect(await page.evaluate(() => window.rl.data.time)).toBeGreaterThanOrEqual( + 0, + ); + await expect(page.getByRole("dialog")).toHaveCount(0); + await expect(page.getByText("Waddle in", { exact: true })).toHaveCount(0); + await expect( + page.getByRole("button", { name: "Keyboard controls", exact: false }), + ).toHaveAttribute("aria-expanded", "true"); + await page.getByRole("button", { name: "Pause", exact: true }).click(); + await page.evaluate(() => window.rl.pause()); + const time = await page.evaluate(() => window.rl.data.time); + await page.waitForTimeout(400); + expect(await page.evaluate(() => window.rl.data.time)).toBe(time); + await expect(page.getByRole("status")).toHaveText(/Paused/); + await page.keyboard.press("Escape"); + await expect + .poll(() => page.evaluate(() => window.rl.data.time)) + .toBeGreaterThan(time); + await page.getByRole("button", { name: "Pause", exact: true }).click(); + await page.evaluate(() => window.rl.pause()); + await page.getByRole("button", { name: "Reset", exact: true }).click(); + await expect.poll(() => page.evaluate(() => window.rl.data.time)).toBe(0); + await expect( + page.getByRole("button", { name: "Continue", exact: true }), + ).toBeEnabled(); + await page + .getByRole("button", { name: "Keyboard controls", exact: false }) + .click(); + await expect( + page.getByRole("button", { name: "Keyboard controls", exact: false }), + ).toHaveAttribute("aria-expanded", "false"); + expect( + requests.filter( + (url) => url.includes("nostr") || url.includes("duck-head-mark"), + ), + ).toEqual([]); + expect(errors).toEqual([]); +}); +test("paused sit timer freezes; keyboard focus does not send robot commands", async ({ + page, +}) => { + await page.goto("/"); + await page.waitForFunction(() => window.rl); + await page.evaluate(() => window.rl.setMode("sit")); + await page.getByRole("button", { name: "Pause", exact: true }).click(); + await page.evaluate(() => window.rl.pause()); + const before = await page.evaluate(() => window.rl.sitFlag); + await page.waitForTimeout(1100); + expect(await page.evaluate(() => window.rl.sitFlag)).toBe(before); + await page.keyboard.press("ArrowUp"); + expect(await page.evaluate(() => Array.from(window.rl.velCmd))).toEqual([ + 0, 0, 0, + ]); + // Native Space activates the focused Continue button, not robot reset. + await page.keyboard.press("Space"); + await expect.poll(() => page.evaluate(() => window.rl.running)).toBe(true); + expect(await page.evaluate(() => window.rl.mode)).toBe("sitstand"); + await page.keyboard.down("w"); + expect(await page.evaluate(() => window.rl.velCmd[0])).toBeGreaterThan(0); + await page.keyboard.up("w"); +}); +test("load failure is inline and retry boots a clean instance", async ({ + page, +}) => { + let failed = false; + await page.route("**/policies/BEST_alpha_walking.onnx", (route) => { + if (!failed) { + failed = true; + return route.fulfill({ status: 404, body: "not found" }); + } + return route.continue(); + }); + await page.goto("/"); + await expect(page.getByRole("alert")).toContainText("Simulation error"); + await page.getByRole("button", { name: "Retry", exact: true }).click(); + await expect( + page.getByRole("button", { name: "Pause", exact: true }), + ).toBeEnabled(); + await expect(page.getByRole("alert")).toHaveCount(0); +}); diff --git a/app/e2e/support/simulation.js b/app/e2e/support/simulation.js new file mode 100644 index 0000000..070dc4b --- /dev/null +++ b/app/e2e/support/simulation.js @@ -0,0 +1,39 @@ +import { expect } from "@playwright/test"; +export async function bootDuck(page) { + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + await page.addInitScript(() => { + let seed = 123456789; + Math.random = () => + (seed = (1664525 * seed + 1013904223) >>> 0) / 4294967296; + }); + await page.goto("/?boot=1&test=1"); + await page.waitForFunction( + () => window.rl?.prepareTest || window.__store?.getState().bootFailed, + ); + const failed = await page.evaluate( + () => window.__store.getState().bootFailed, + ); + expect( + failed, + JSON.stringify(await page.evaluate(() => window.__store.getState())), + ).toBe(false); + await page.evaluate(() => window.rl.prepareTest()); + expect(errors).toEqual([]); + return errors; +} +export async function snapshot(page) { + return page.evaluate(() => { + const r = window.rl; + return { + time: r.data.time, + mode: r.mode, + recovery: r.recovery, + qpos: Array.from(r.data.qpos), + qvel: Array.from(r.data.qvel), + ctrl: Array.from(r.data.ctrl), + lastAction: Array.from(r.lastAction), + obs: Array.from(r.buildObs()), + }; + }); +} diff --git a/app/index.html b/app/index.html index 6c88f95..092ecbb 100644 --- a/app/index.html +++ b/app/index.html @@ -4,14 +4,7 @@