#!/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/"; const outDir = process.argv[3] || "test-results/joint-control"; const minObjects = Number(process.env.MIN_OBJECTS || "50"); const minMeshes = Number(process.env.MIN_MESHES || "40"); const verifyTimeoutMs = Number(process.env.VERIFY_TIMEOUT_MS || "60000"); const jointName = process.env.TEST_JOINT || "joint2"; const jointValue = Number(process.env.TEST_JOINT_VALUE || "0.6"); async function waitForReady(page) { await page.waitForFunction(([requiredObjects, requiredMeshes]) => { const objects = Number(document.querySelector("#stat-objects")?.textContent || "0"); const meshes = Number(document.querySelector("#stat-meshes")?.textContent || "0"); const step = document.querySelector("#stat-step")?.textContent || "-"; const control = document.querySelector("#control-status")?.textContent || ""; const jointStatus = document.querySelector("#joint-status")?.textContent || ""; const options = document.querySelectorAll("#joint-select option").length; return objects >= requiredObjects && meshes >= requiredMeshes && step !== "-" && control.includes("ready") && options >= 1 && jointStatus.includes("ready"); }, [minObjects, minMeshes], { timeout: verifyTimeoutMs }); } async function readState(page) { return page.evaluate(async () => { const controlUrl = window.__motrixPreviewDebug?.getControlUrl?.(); if (!controlUrl) { throw new Error("missing control URL"); } const stateUrl = controlUrl.replace(/\/control$/, "/state"); const response = await fetch(stateUrl, { cache: "no-store" }); return response.json(); }); } async function readDom(page) { return page.evaluate(() => ({ objects: document.querySelector("#stat-objects")?.textContent || "", meshes: document.querySelector("#stat-meshes")?.textContent || "", paused: document.querySelector("#stat-paused")?.textContent || "", control: document.querySelector("#control-status")?.textContent || "", jointStatus: document.querySelector("#joint-status")?.textContent || "", selectedJoint: document.querySelector("#joint-select")?.value || "", jointValue: document.querySelector("#joint-value")?.value || "", })); } await mkdir(outDir, { recursive: true }); const browser = await chromium.launch({ headless: true }); const page = await browser.newPage({ viewport: { width: 1280, height: 720 }, deviceScaleFactor: 1 }); await page.addInitScript(() => window.localStorage.setItem("motrixsim-web-locale", "en-US")); await page.goto(url, { waitUntil: "domcontentloaded" }); await waitForReady(page); await page.getByRole("button", { name: "Home", exact: true }).click(); await page.waitForFunction(() => (document.querySelector("#control-status")?.textContent || "").includes("home ok"), undefined, { timeout: 10000 }); const before = await readState(page); await page.locator("#joint-select").selectOption(jointName); await page.locator("#joint-value").fill(String(jointValue)); await page.getByRole("button", { name: "Set Joint", exact: true }).click(); await page.waitForFunction(([name, value]) => { const control = document.querySelector("#control-status")?.textContent || ""; const status = document.querySelector("#joint-status")?.textContent || ""; const inputValue = Number(document.querySelector("#joint-value")?.value || "NaN"); return control.includes("set_joint ok") && status.includes(`${name} set`) && Math.abs(inputValue - Number(value)) < 0.02; }, [jointName, jointValue], { timeout: 10000 }); const afterJoint = await readState(page); await page.getByRole("button", { name: "Open Grip", exact: true }).click(); await page.waitForFunction(() => (document.querySelector("#control-status")?.textContent || "").includes("gripper ok") && (document.querySelector("#joint-status")?.textContent || "").includes("gripper open"), undefined, { timeout: 10000 }); const afterGrip = await readState(page); const dom = await readDom(page); await page.screenshot({ path: path.join(outDir, "joint-control-desktop.jpg"), fullPage: true }); await browser.close(); const handBefore = before.bodies?.hand; const handAfter = afterJoint.bodies?.hand; const jointAfter = afterJoint.joints?.[jointName]?.pos; const fingerAfter = afterGrip.joints?.finger_joint1?.pos; const evidence = { captured_at: new Date().toISOString(), url, joint_name: jointName, joint_target: jointValue, dom, before: { paused: before.paused, joint: before.joints?.[jointName], hand: handBefore, }, after_joint: { paused: afterJoint.paused, joint: afterJoint.joints?.[jointName], hand: handAfter, }, after_gripper: { finger_joint1: afterGrip.joints?.finger_joint1, finger_joint2: afterGrip.joints?.finger_joint2, }, verification: { joint_value_applied: Math.abs(Number(jointAfter) - jointValue) < 0.02, hand_pose_changed: JSON.stringify(handBefore) !== JSON.stringify(handAfter), gripper_opened: Number(fingerAfter) > 0.035, min_objects: minObjects, min_meshes: minMeshes, timeout_ms: verifyTimeoutMs, }, }; await writeFile(path.join(outDir, "joint-control-evidence.json"), JSON.stringify(evidence, null, 2)); console.log(JSON.stringify(evidence, null, 2));