#!/usr/bin/env node import { chromium } from "playwright"; import { mkdir, writeFile } from "node:fs/promises"; import path from "node:path"; const url = process.argv[2] || "http://127.0.0.1:5173/?manifest=http%3A%2F%2F127.0.0.1%3A18086%2Fmanifest"; const outDir = process.argv[3] || "test-results/go2-policy-motion"; const verifyTimeoutMs = Number(process.env.VERIFY_TIMEOUT_MS || "60000"); const minDistance = Number(process.env.MIN_GO2_DISTANCE || "0.5"); async function waitForReady(page) { await page.waitForFunction(() => { const objects = Number(document.querySelector("#stat-objects")?.textContent || "0"); const meshes = Number(document.querySelector("#stat-meshes")?.textContent || "0"); const control = document.querySelector("#control-status")?.textContent || ""; const state = document.querySelector("#state-status")?.textContent || ""; return objects >= 30 && meshes >= 20 && control.includes("ready") && state.includes("connected"); }, undefined, { timeout: verifyTimeoutMs }); } async function readState(page) { return page.evaluate(async () => { const controlUrl = window.__motrixPreviewDebug?.getControlUrl?.(); if (!controlUrl) { throw new Error("missing control URL"); } const response = await fetch(controlUrl.replace(/\/control$/, "/state"), { cache: "no-store" }); return response.json(); }); } async function sendControl(page, payload) { return page.evaluate(async (body) => { const controlUrl = window.__motrixPreviewDebug?.getControlUrl?.(); if (!controlUrl) { throw new Error("missing control URL"); } const response = await fetch(controlUrl, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body), }); return response.json(); }, payload); } function xyDistance(a, b) { return Math.hypot(Number(b[0]) - Number(a[0]), Number(b[1]) - Number(a[1])); } await mkdir(outDir, { recursive: true }); const browser = await chromium.launch({ headless: true }); const page = await browser.newPage({ viewport: { width: 1360, height: 820 }, deviceScaleFactor: 1 }); await page.addInitScript(() => window.localStorage.setItem("motrixsim-web-locale", "en-US")); await page.goto(url, { waitUntil: "domcontentloaded" }); await waitForReady(page); await sendControl(page, { action: "reset" }); await sendControl(page, { action: "resume" }); await page.waitForTimeout(700); const before = await readState(page); await page.mouse.click(920, 360); await page.keyboard.down("w"); await page.waitForTimeout(1700); const during = await readState(page); await page.keyboard.up("w"); await page.waitForTimeout(400); const after = await readState(page); await page.screenshot({ path: path.join(outDir, "go2-policy-motion-after-w.jpg"), fullPage: true }); await browser.close(); const distance = xyDistance(before.bodies.base.pos, during.bodies.base.pos); const evidence = { captured_at: new Date().toISOString(), url, policy: during.policy, before: { step: before.step, paused: before.paused, command: before.command, base_pos: before.bodies.base.pos, }, during_w: { step: during.step, paused: during.paused, command: during.command, base_pos: during.bodies.base.pos, }, after_keyup: { step: after.step, paused: after.paused, command: after.command, base_pos: after.bodies.base.pos, }, verification: { policy_loaded: during.policy === "go2_policy.onnx", w_command_active: JSON.stringify(during.command) === JSON.stringify([1, 0, 0]), keyup_stopped_command: JSON.stringify(after.command) === JSON.stringify([0, 0, 0]), base_moved_xy: distance > minDistance, base_distance_xy: distance, min_distance: minDistance, }, }; await writeFile(path.join(outDir, "go2-policy-motion-evidence.json"), JSON.stringify(evidence, null, 2)); if (!evidence.verification.policy_loaded || !evidence.verification.w_command_active || !evidence.verification.keyup_stopped_command || !evidence.verification.base_moved_xy) { console.error(JSON.stringify(evidence, null, 2)); process.exit(1); } console.log(JSON.stringify(evidence, null, 2));