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: "Scene line colours" }), ).toBeVisible(); await expect( page.getByRole("button", { name: "Rollers", exact: true }), ).toHaveCount(0); await expect( page.getByText("Drag robot / geometry: apply force"), ).toBeVisible(); 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: "Scene line 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); } });