import { test, expect } from "@playwright/test"; import { bootDuck } from "./support/simulation.js"; test("bright scene has 5–7 free-body toys, no football, and four original-style line colour buttons", async ({ page, }) => { const errors = await bootDuck(page); await page.evaluate(async () => { const r = window.rl; r.setPropsParked(false); await r.step(60); r.render(); }); const initial = await page.evaluate(() => { const r = window.rl; return { toys: r.interactiveProps.map((p) => ({ type: p.type, dof: p.dofAdr, mass: r.model.body_mass[p.bodyId], })), meshes: r.scene.getObjectByName("interactive-props").children.length, ballId: r.mujoco.mj_name2id( r.model, r.mujoco.mjtObj.mjOBJ_BODY.value, "ball", ), hemisphere: r.scene.parent.children.some( (o) => o.isHemisphereLight && o.intensity >= 0.7, ), environment: r.scene.parent.environmentIntensity, robotMaterials: [...r.scene.children] .filter((o) => o.name !== "interactive-props") .flatMap((o) => { const result = []; o.traverse((m) => { if (m.isMesh && m.material?.color) result.push([m.uuid, m.material.color.toArray()]); }); return result; }), }; }); expect(initial.toys.length).toBeGreaterThanOrEqual(5); expect(initial.toys.length).toBeLessThanOrEqual(7); expect(initial.meshes).toBe(initial.toys.length); expect(new Set(initial.toys.map((p) => p.type)).size).toBe(4); expect(initial.toys.every((p) => p.mass > 0 && p.dof >= 0)).toBe(true); expect(initial.ballId).toBe(-1); expect(initial.hemisphere).toBe(true); expect(initial.environment).toBeGreaterThanOrEqual(0.7); const group = page.getByRole("group", { name: "Scene line colours" }); await expect(group.getByRole("button")).toHaveCount(4); for (const [label, hex] of [ ["Cream", "f7e6cb"], ["Graphite", "6c6a68"], ["Lavender", "bfa9cf"], ["Sky", "a9dbe8"], ]) { const button = group.getByRole("button", { name: `${label} scene lines` }); await button.click(); await expect(button).toHaveAttribute("aria-pressed", "true"); const values = await page.evaluate(() => { const r = window.rl, lines = [], robotMaterials = []; r.scene.traverse((o) => { if (o.material?.uniforms?.uSectionColor) lines.push(o.material.uniforms.uSectionColor.value.getHexString()); }); for (const o of r.scene.children.filter( (o) => o.name !== "interactive-props", )) o.traverse((m) => { if (m.isMesh && m.material?.color) robotMaterials.push([m.uuid, m.material.color.toArray()]); }); const style = getComputedStyle( document.querySelector('[aria-label="Scene line colours"] button'), ); return { lines, robotMaterials, radius: style.borderRadius, width: style.width, height: style.height, }; }); expect(values.lines).toEqual(Array(5).fill(hex)); expect(values.robotMaterials).toEqual(initial.robotMaterials); expect(values.radius).toBe("0px"); expect(values.width).toBe(values.height); } await page.evaluate(() => { const r = window.rl; r.chaseCam = false; r.camera.position.set(-0.2, 1.9, 2.7); r.controls.target.set(0.1, 0.05, 0); r.controls.update(); }); await page.screenshot({ path: "test-results/scene-interaction-overview.png", }); expect(errors).toEqual([]); }); test("geometry can be dragged with physical force; pause/reset clear grab and restore layout", async ({ page, }) => { const errors = await bootDuck(page); await page.evaluate(() => { const r = window.rl; r.setPropsParked(false); r.chaseCam = false; const spec = r.interactiveProps[0], q = r.data.qpos; q.set([-0.25, 0, spec.pos[2]], spec.qAdr); r.mujoco.mj_forward(r.model, r.data); r.camera.position.set(-0.6, 0.65, 1.2); r.controls.target.set(q[0], q[2], -q[1]); r.controls.update(); r.camera.updateMatrixWorld(); r.render(); }); await page.waitForTimeout(250); const point = await page.evaluate(() => { const r = window.rl, spec = r.interactiveProps[0], q = r.data.qpos; const p = r.controls.target .clone() .set(q[spec.qAdr], q[spec.qAdr + 2], -q[spec.qAdr + 1]) .project(r.camera); const rect = document.querySelector("canvas").getBoundingClientRect(); return { x: ((p.x + 1) * rect.width) / 2, y: ((1 - p.y) * rect.height) / 2, }; }); await page.mouse.move(point.x, point.y); await page.mouse.down(); expect(await page.evaluate(() => window.rl.controls.enabled)).toBe(false); await page.mouse.move(point.x + 90, point.y - 40, { steps: 6 }); const result = await page.evaluate(async () => { const r = window.rl, spec = r.interactiveProps[0]; await r.step(15); return { x: r.data.qpos[spec.qAdr], force: Array.from( r.data.xfrc_applied.slice(spec.bodyId * 6, spec.bodyId * 6 + 3), ), }; }); expect(Math.abs(result.x + 0.25)).toBeGreaterThan(0.01); expect(result.force.some((v) => Math.abs(v) > 0.001)).toBe(true); await page.evaluate(() => window.__gameApi.pause()); expect(await page.evaluate(() => window.rl.controls.enabled)).toBe(true); await page.mouse.up(); expect( await page.evaluate(() => Array.from(window.rl.data.xfrc_applied).every((v) => v === 0), ), ).toBe(true); await page.evaluate(() => window.__gameApi.reset()); expect( await page.evaluate(() => { const r = window.rl; return ( r.interactiveProps.every((p) => p.pos.every((v, i) => r.data.qpos[p.qAdr + i] === v), ) && r.controls.enabled ); }), ).toBe(true); expect(errors).toEqual([]); }); test("toys transfer collision momentum, collide with the robot and floor, and survive a mode switch", async ({ page, }) => { const errors = await bootDuck(page); const collision = await page.evaluate(async () => { const r = window.rl; r.setPropsParked(false); const specs = r.interactiveProps; // Keep unrelated toys clear of a two-body collision lane. specs.forEach((p, i) => { r.data.qpos.set([-1 + i * 0.3, -1.1, p.pos[2]], p.qAdr); }); const a = specs[0], b = specs[1]; r.data.qpos.set([0.1, 0.7, a.pos[2]], a.qAdr); r.data.qpos.set([0.35, 0.7, b.pos[2]], b.qAdr); r.data.qvel[a.dofAdr] = 1.8; r.mujoco.mj_forward(r.model, r.data); await r.step(15); const moved = r.data.qpos[b.qAdr] - 0.35; // Launch a toy at the duck. A non-colliding body would cross x=-0.6. r.resetSim(); const p = r.interactiveProps.find((s) => s.type === "sphere"); r.data.qpos.set([-0.35, 0, 0.09], p.qAdr); r.data.qvel[p.dofAdr] = -1.5; r.mujoco.mj_forward(r.model, r.data); let contacts = 0, minX = 10; for (let i = 0; i < 15; i++) { await r.step(1); contacts = Math.max(contacts, r.data.ncon); minX = Math.min(minX, r.data.qpos[p.qAdr]); } await r.step(100); const half = r.world.boundary.half_extent; r.data.qpos.set([half - 0.2, 0.9, p.pos[2]], p.qAdr); r.data.qvel.fill(0, p.dofAdr, p.dofAdr + 6); r.data.qvel[p.dofAdr] = 1.5; r.mujoco.mj_forward(r.model, r.data); let wallMax = -Infinity; for (let i = 0; i < 15; i++) { await r.step(1); wallMax = Math.max(wallMax, r.data.qpos[p.qAdr]); } return { moved, contacts, minX, wallMax, half, finite: Array.from(r.data.qpos).every(Number.isFinite), aboveFloor: r.interactiveProps.every( (s) => r.data.qpos[s.qAdr + 2] > 0.025, ), }; }); expect(collision.moved).toBeGreaterThan(0.025); expect(collision.contacts).toBeGreaterThan(0); expect(collision.minX).toBeGreaterThan(-0.58); expect(collision.wallMax).toBeLessThan(collision.half - 0.03); expect(collision.aboveFloor).toBe(true); expect(collision.finite).toBe(true); await page.evaluate(() => window.rl.setLoco("rollers", { force: true })); const switched = await page.evaluate(async () => { const r = window.rl; await r.step(100); r.render(); return { count: r.interactiveProps.length, meshes: r.scene.getObjectByName("interactive-props").children.length, finite: Array.from(r.data.qpos).every(Number.isFinite), }; }); expect(switched.count).toBe(switched.meshes); expect(switched.finite).toBe(true); expect(errors).toEqual([]); });