Files

292 lines
9.9 KiB
JavaScript

import { test, expect } from "@playwright/test";
async function boot(page) {
await page.goto("/");
await expect(
page.getByRole("button", { name: "Pause", exact: true }),
).toBeEnabled();
await page.getByLabel("Robot", { exact: true }).selectOption("rp1");
await page.waitForFunction(
() =>
window.__store.getState().robotId === "rp1" ||
window.__store.getState().runtimeError,
);
expect(
await page.evaluate(() => window.__store.getState().runtimeError),
).toBeNull();
await page.evaluate(() => window.rl.pause());
}
test("third robot: real WASM/ONNX, keyboard command contract, trained skill bindings, fall/reset, switching", async ({
page,
}, info) => {
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await boot(page);
expect(
await page.getByLabel("Robot", { exact: true }).locator("option").count(),
).toBe(5);
expect(await page.evaluate(() => window.rl.buildObs().length)).toBe(243);
expect(await page.evaluate(() => window.rl.lastAction.length)).toBe(24);
await page
.locator("canvas")
.first()
.click({ position: { x: 700, y: 600 } });
await page.evaluate(() => window.rl.resume());
await expect(page.getByRole("button", { name: /^J\s*Dance$/ })).toBeEnabled();
await expect(page.getByRole("button", { name: /^H\s*Wave$/ })).toBeEnabled();
expect(
await page
.getByRole("complementary", { name: "Keyboard controls" })
.innerText(),
).not.toMatch(/\p{Script=Han}/u);
for (const [key, expected] of [
["KeyW", [0.4, 0, 0]],
["KeyS", [-0.3, 0, 0]],
["KeyA", [0, 0, 0.6]],
["KeyD", [0, 0, -0.6]],
]) {
await page.evaluate(async () => {
await window.rl.resetSim();
window.rl.resume();
});
await page.keyboard.down(key);
const command = await page.evaluate(() => Array.from(window.rl.command));
command.forEach((v, i) => expect(v).toBeCloseTo(expected[i], 5));
await page.keyboard.up(key);
expect(await page.evaluate(() => Array.from(window.rl.command))).toEqual([
0, 0, 0,
]);
await page.evaluate(() => window.rl.pause());
}
const report = await page.evaluate(async () => {
const r = window.rl,
trials = [];
for (const [name, command] of [
["stand", [0, 0, 0]],
["forward", [0.4, 0, 0]],
["backward", [-0.3, 0, 0]],
["left", [0, 0, 0.6]],
["right", [0, 0, -0.6]],
]) {
await r.resetSim();
r.command.set(command);
const before = Array.from(r.data.qpos.slice(0, 3));
let firstFall = null;
for (let i = 0; i < 150; i++) {
await r.step(1);
if (
![
...r.data.qpos,
...r.data.qvel,
...r.data.ctrl,
...r.lastAction,
].every(Number.isFinite)
)
throw new Error("non-finite RP1 state");
if (r.fallen) {
firstFall = r.data.time;
break;
}
}
const [w, x, y, z] = r.data.qpos.slice(3, 7);
trials.push({
name,
before,
after: Array.from(r.data.qpos.slice(0, 3)),
time: r.data.time,
yaw: Math.atan2(2 * (w * z + x * y), 1 - 2 * (y * y + z * z)),
firstFall,
actuation: r.actuationStats,
});
}
return trials;
});
for (const trial of report) expect(trial.firstFall).toBeNull();
expect(report.find((t) => t.name === "forward").after[0]).toBeGreaterThan(
0.4,
);
expect(report.find((t) => t.name === "backward").after[0]).toBeLessThan(-0.3);
expect(report.find((t) => t.name === "left").yaw).toBeGreaterThan(0.5);
expect(report.find((t) => t.name === "right").yaw).toBeLessThan(-0.5);
console.log("RP1 completed-checkpoint trials", JSON.stringify(report));
await info.attach("rp1-preview-trials.json", {
body: JSON.stringify(report, null, 2),
contentType: "application/json",
});
await page.evaluate(async () => {
await window.rl.resetSim();
window.rl.data.qpos[2] = 1.2;
window.rl.data.qpos.set([Math.SQRT1_2, Math.SQRT1_2, 0, 0], 3);
window.rl.mujoco.mj_forward(window.rl.model, window.rl.data);
await window.rl.step(1);
});
expect(await page.evaluate(() => window.rl.fallen)).toBe(true);
await page.keyboard.press("Space");
await expect.poll(() => page.evaluate(() => window.rl.fallen)).toBe(false);
expect(await page.evaluate(() => window.rl.policyState.state)).toBe("walk");
await page.screenshot({ path: info.outputPath("rp1.png") });
for (const robot of ["microban", "microduck", "rp1"]) {
await page.getByLabel("Robot", { exact: true }).selectOption(robot);
await expect
.poll(() => page.evaluate(() => window.__store.getState().robotId))
.toBe(robot === "microduck" ? "microduck-legs" : robot);
}
expect(errors).toEqual([]);
});
test("three 25-second bounded RP1 sequences preserve direction and balance; realtime controller stays near 50Hz", async ({
page,
}, info) => {
await boot(page);
const report = await page.evaluate(async () => {
const r = window.rl,
trials = [];
const pose = () => {
const [w, x, y, z] = r.data.qpos.slice(3, 7);
return {
pos: Array.from(r.data.qpos.slice(0, 3)),
yaw: Math.atan2(2 * (w * z + x * y), 1 - 2 * (y * y + z * z)),
};
};
for (let trial = 0; trial < 3; trial++) {
await r.resetSim();
const segments = [];
for (const [name, seconds, vx, wz] of [
["stand", 5, 0, 0],
["forward", 3, 0.4, 0],
["rest", 2, 0, 0],
["backward", 3, -0.3, 0],
["rest", 2, 0, 0],
["left", 3, 0, 0.6],
["rest", 2, 0, 0],
["right", 3, 0, -0.6],
["rest", 2, 0, 0],
]) {
const before = pose();
r.command.set([vx, 0, wz]);
let yaw = 0,
previousYaw = before.yaw,
minHeight = Infinity;
for (let i = 0; i < seconds * 10; i++) {
await r.step(5);
const p = pose();
if (r.fallen)
throw new Error(`trial ${trial} ${name}: fallen at ${r.data.time}`);
if (Math.max(Math.abs(p.pos[0]), Math.abs(p.pos[1])) > 1.9)
throw new Error("unexpected wall approach");
yaw += Math.atan2(
Math.sin(p.yaw - previousYaw),
Math.cos(p.yaw - previousYaw),
);
previousYaw = p.yaw;
minHeight = Math.min(minHeight, p.pos[2]);
}
const after = pose();
segments.push({
name,
minHeight,
yaw,
displacement:
(after.pos[0] - before.pos[0]) * Math.cos(before.yaw) +
(after.pos[1] - before.pos[1]) * Math.sin(before.yaw),
});
}
trials.push({
trial,
time: r.data.time,
segments,
actuation: r.actuationStats,
});
}
return trials;
});
for (const trial of report) {
const find = (name) => trial.segments.find((s) => s.name === name);
expect(trial.time).toBeCloseTo(25, 5);
expect(find("forward").displacement).toBeGreaterThan(0.4);
expect(find("backward").displacement).toBeLessThan(-0.3);
expect(find("left").yaw).toBeGreaterThan(0.5);
expect(find("right").yaw).toBeLessThan(-0.5);
expect(trial.actuation.longestSaturatedSeconds).toBeLessThan(0.5);
}
await info.attach("rp1-three-sequences.json", {
body: JSON.stringify(report, null, 2),
contentType: "application/json",
});
await page.evaluate(async () => {
await window.rl.resetSim();
window.rl.resume();
});
const first = await page.evaluate(() => ({
sim: window.rl.data.time,
wall: performance.now(),
}));
await page.waitForTimeout(4000);
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.8);
expect(ratio).toBeLessThan(1.15);
await info.attach("rp1-realtime.json", {
body: JSON.stringify({ first, second, ratio }),
contentType: "application/json",
});
});
test("episodic ONNX lifecycle is exclusive and resets on return (walk reused as interface fixture, NOT a dance policy)", async ({
page,
}) => {
await page.route("**/robots/rp1/robot.json*", async (route) => {
const response = await route.fetch(),
config = await response.json();
const skill = config.skills.find((s) => s.id === "dance");
Object.assign(skill, {
available: true,
duration: 0.8,
blend: 0.2,
observations: config.observations,
actions: config.actions,
defaults: config.joints.defaults,
});
delete skill.reference;
skill.controller.policy = "policies/walk.onnx";
config.input.bindings.find((b) => b.action === "skill:dance").disabled =
false;
await route.fulfill({ response, json: config });
});
await boot(page);
const report = await page.evaluate(async () => {
const r = window.rl;
await r.resetSim();
if (!r.policyState.request("dance", r.data.time))
throw new Error("request refused");
await r.step(23); // Real zero-command locomotion must satisfy the stop gate.
const first = { active: r.activePolicy, state: r.policyState.state };
await r.step(65);
const returned = { active: r.activePolicy, state: r.policyState.state };
r.policyState.request("dance", r.data.time);
await r.step(23);
r.command.set([0.4, 0, 0]);
await r.step(1);
const interrupted = { active: r.activePolicy, state: r.policyState.state };
await r.step(12);
return {
first,
returned,
interrupted,
active: r.activePolicy,
state: r.policyState.state,
fallen: r.fallen,
};
});
expect(report.first).toEqual({ active: "dance", state: "entering" });
expect(report.returned).toEqual({ active: "walk", state: "walk" });
expect(report.interrupted).toEqual({ active: "walk", state: "returning" });
expect(report.active).toBe("walk");
expect(report.state).toBe("walk");
expect(report.fallen).toBe(false);
});