189 lines
7.8 KiB
JavaScript
189 lines
7.8 KiB
JavaScript
#!/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/motrix-input";
|
|
const minObjects = Number(process.env.MIN_OBJECTS || "1");
|
|
const minMeshes = Number(process.env.MIN_MESHES || "0");
|
|
const verifyTimeoutMs = Number(process.env.VERIFY_TIMEOUT_MS || "60000");
|
|
|
|
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 input = document.querySelector("#input-status")?.textContent || "";
|
|
const actuators = document.querySelector("#actuator-status")?.textContent || "";
|
|
const actuatorOptions = document.querySelectorAll("#actuator-select option").length;
|
|
return objects >= requiredObjects
|
|
&& meshes >= requiredMeshes
|
|
&& step !== "-"
|
|
&& control.includes("ready")
|
|
&& input.includes("ready")
|
|
&& actuators.includes("ready")
|
|
&& actuatorOptions >= 1;
|
|
}, [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 || "",
|
|
step: document.querySelector("#stat-step")?.textContent || "",
|
|
control: document.querySelector("#control-status")?.textContent || "",
|
|
inputStatus: document.querySelector("#input-status")?.textContent || "",
|
|
inputKeys: document.querySelector("#input-keys")?.textContent || "",
|
|
inputCommand: document.querySelector("#input-command")?.textContent || "",
|
|
actuatorStatus: document.querySelector("#actuator-status")?.textContent || "",
|
|
selectedActuator: document.querySelector("#actuator-select")?.value || "",
|
|
actuatorValue: document.querySelector("#actuator-value")?.value || "",
|
|
}));
|
|
}
|
|
|
|
async function waitForCommand(page, expected) {
|
|
await page.waitForFunction(([commandText]) => {
|
|
const command = document.querySelector("#input-command")?.textContent || "";
|
|
const control = document.querySelector("#control-status")?.textContent || "";
|
|
return command === commandText && control.includes("command ok");
|
|
}, [expected], { timeout: 10000 });
|
|
}
|
|
|
|
async function waitForResetState(page) {
|
|
await page.waitForFunction(async () => {
|
|
const controlUrl = window.__motrixPreviewDebug?.getControlUrl?.();
|
|
if (!controlUrl) {
|
|
return false;
|
|
}
|
|
const response = await fetch(controlUrl.replace(/\/control$/, "/state"), { cache: "no-store" });
|
|
const state = await response.json();
|
|
return state.step === 0
|
|
&& state.paused === true
|
|
&& JSON.stringify(state.command) === JSON.stringify([0, 0, 0]);
|
|
}, undefined, { timeout: 12000 });
|
|
await page.waitForFunction(() => {
|
|
const command = document.querySelector("#input-command")?.textContent || "";
|
|
return command === "0.00 / 0.00 / 0.00";
|
|
}, undefined, { timeout: 10000 });
|
|
}
|
|
|
|
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 page.evaluate(() => document.querySelector(".sidebar")?.scrollTo(0, 0));
|
|
await page.screenshot({ path: path.join(outDir, "motrix-input-ready.jpg"), fullPage: true });
|
|
|
|
const initial = await readState(page);
|
|
|
|
await page.keyboard.press("Space");
|
|
await page.waitForFunction((wasPaused) => {
|
|
const control = document.querySelector("#control-status")?.textContent || "";
|
|
const paused = document.querySelector("#stat-paused")?.textContent || "";
|
|
return control.includes("toggle ok") && paused === String(!wasPaused);
|
|
}, initial.paused, { timeout: 10000 });
|
|
const afterToggle = await readState(page);
|
|
|
|
await page.keyboard.press("KeyR");
|
|
await waitForResetState(page);
|
|
const afterReset = await readState(page);
|
|
|
|
await page.keyboard.down("w");
|
|
await waitForCommand(page, "1.00 / 0.00 / 0.00");
|
|
const afterForward = await readState(page);
|
|
|
|
await page.keyboard.up("w");
|
|
await waitForCommand(page, "0.00 / 0.00 / 0.00");
|
|
const afterStop = await readState(page);
|
|
|
|
const actuatorTarget = await page.evaluate(() => {
|
|
const select = document.querySelector("#actuator-select");
|
|
const valueInput = document.querySelector("#actuator-value");
|
|
const min = Number(valueInput?.getAttribute("min") || "-1");
|
|
const max = Number(valueInput?.getAttribute("max") || "1");
|
|
const name = select?.value || "";
|
|
const preferred = max > 0.2 ? max * 0.35 : max;
|
|
const fallback = min < -0.2 ? min * 0.35 : min;
|
|
const value = Math.abs(preferred) > 0.001 ? preferred : fallback;
|
|
return { name, value: Math.max(min, Math.min(max, value)) };
|
|
});
|
|
await page.locator("#actuator-value").fill(String(actuatorTarget.value));
|
|
await page.getByRole("button", { name: "Set Actuator", exact: true }).click();
|
|
await page.waitForFunction(([name]) => {
|
|
const control = document.querySelector("#control-status")?.textContent || "";
|
|
const status = document.querySelector("#actuator-status")?.textContent || "";
|
|
return control.includes("set_actuator ok") && status.includes(`${name} set`);
|
|
}, [actuatorTarget.name], { timeout: 10000 });
|
|
const afterActuator = await readState(page);
|
|
const dom = await readDom(page);
|
|
|
|
await page.screenshot({ path: path.join(outDir, "motrix-input-desktop.jpg"), fullPage: true });
|
|
await page.mouse.move(910, 370);
|
|
await page.mouse.down({ button: "right" });
|
|
await page.mouse.move(970, 410, { steps: 8 });
|
|
await page.mouse.up({ button: "right" });
|
|
await page.waitForTimeout(120);
|
|
await page.screenshot({ path: path.join(outDir, "motrix-camera-focus.jpg"), fullPage: true });
|
|
await browser.close();
|
|
|
|
const actuatorState = afterActuator.actuators?.[actuatorTarget.name];
|
|
const evidence = {
|
|
captured_at: new Date().toISOString(),
|
|
url,
|
|
initial: {
|
|
step: initial.step,
|
|
paused: initial.paused,
|
|
command: initial.command,
|
|
},
|
|
after_toggle: {
|
|
step: afterToggle.step,
|
|
paused: afterToggle.paused,
|
|
},
|
|
after_reset: {
|
|
step: afterReset.step,
|
|
paused: afterReset.paused,
|
|
command: afterReset.command,
|
|
},
|
|
after_forward_key: {
|
|
command: afterForward.command,
|
|
},
|
|
after_stop_key: {
|
|
command: afterStop.command,
|
|
},
|
|
actuator: {
|
|
name: actuatorTarget.name,
|
|
target: actuatorTarget.value,
|
|
state: actuatorState,
|
|
},
|
|
dom,
|
|
verification: {
|
|
space_toggled_pause: afterToggle.paused === !initial.paused,
|
|
r_reset_step_zero: afterReset.step === 0,
|
|
w_sent_forward_command: JSON.stringify(afterForward.command) === JSON.stringify([1, 0, 0]),
|
|
keyup_sent_stop_command: JSON.stringify(afterStop.command) === JSON.stringify([0, 0, 0]),
|
|
actuator_ctrl_applied: Math.abs(Number(actuatorState?.ctrl) - actuatorTarget.value) < 0.02,
|
|
min_objects: minObjects,
|
|
min_meshes: minMeshes,
|
|
timeout_ms: verifyTimeoutMs,
|
|
},
|
|
};
|
|
await writeFile(path.join(outDir, "motrix-input-evidence.json"), JSON.stringify(evidence, null, 2));
|
|
console.log(JSON.stringify(evidence, null, 2));
|