121 lines
5.1 KiB
JavaScript
121 lines
5.1 KiB
JavaScript
import { mkdir, writeFile } from "node:fs/promises";
|
|
import { dirname, resolve } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const rootDir = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
|
const defaultManifest = "http://127.0.0.1:18090/manifest";
|
|
const defaultFrontend = `http://127.0.0.1:5173/?manifest=${encodeURIComponent(defaultManifest)}&v=acceptance-probe`;
|
|
const frontendUrl = new URL(process.argv[2] ?? process.env.MOTRIXSIM_WEB_URL ?? defaultFrontend);
|
|
const manifestUrl = process.env.MOTRIXSIM_MANIFEST_URL ?? frontendUrl.searchParams.get("manifest") ?? defaultManifest;
|
|
const outputPath = resolve(rootDir, process.env.MOTRIXSIM_ACCEPTANCE_OUTPUT ?? "research-tests/product-acceptance/latest-acceptance-probe.json");
|
|
|
|
async function fetchJson(url) {
|
|
const response = await fetch(url, { cache: "no-store" });
|
|
const text = await response.text();
|
|
let json;
|
|
try {
|
|
json = JSON.parse(text);
|
|
} catch {
|
|
json = undefined;
|
|
}
|
|
return { ok: response.ok, status: response.status, url: response.url, json, text };
|
|
}
|
|
|
|
async function fetchHead(url) {
|
|
const response = await fetch(url, { method: "HEAD", cache: "no-store" });
|
|
return { ok: response.ok, status: response.status, url: response.url };
|
|
}
|
|
|
|
function check(id, label, ok, detail, level = "fail") {
|
|
return { id, label, state: ok ? "pass" : level, detail };
|
|
}
|
|
|
|
const startedAt = new Date().toISOString();
|
|
const checks = [];
|
|
let manifest;
|
|
let stateFrame;
|
|
|
|
try {
|
|
const frontend = await fetchHead(frontendUrl);
|
|
checks.push(check("frontend", "Frontend page", frontend.ok, `HTTP ${frontend.status} ${frontend.url}`));
|
|
} catch (error) {
|
|
checks.push(check("frontend", "Frontend page", false, error.message));
|
|
}
|
|
|
|
try {
|
|
const manifestResponse = await fetchJson(manifestUrl);
|
|
manifest = manifestResponse.json;
|
|
checks.push(check("manifest", "Scene manifest", manifestResponse.ok && Array.isArray(manifest?.objects), `HTTP ${manifestResponse.status} ${manifestResponse.url}`));
|
|
} catch (error) {
|
|
checks.push(check("manifest", "Scene manifest", false, error.message));
|
|
}
|
|
|
|
const objects = Array.isArray(manifest?.objects) ? manifest.objects : [];
|
|
const meshes = objects.filter((object) => object?.geometry?.kind === "mesh");
|
|
const fallbackObjects = objects.filter((object) => String(object?.material ?? "").toLowerCase().includes("fallback"));
|
|
const bodyBindings = objects.filter((object) => typeof object?.body === "string" && object.body.length > 0);
|
|
|
|
checks.push(check("objects", "Objects present", objects.length > 0, `${objects.length} objects`));
|
|
checks.push(check("body-bindings", "Body bindings", bodyBindings.length > 0, `${bodyBindings.length} body-bound objects`, "warn"));
|
|
checks.push(check("assets", "Mesh assets", meshes.length > 0, `${meshes.length} mesh objects`, "warn"));
|
|
checks.push(check("fallbacks", "Fallback materials", fallbackObjects.length === 0, `${fallbackObjects.length} fallback-tagged objects`, "warn"));
|
|
|
|
if (manifest?.state?.url) {
|
|
const stateUrl = new URL(manifest.state.url, manifestUrl).href;
|
|
try {
|
|
const state = await fetchJson(stateUrl);
|
|
stateFrame = state.json;
|
|
checks.push(check("state", "State endpoint", state.ok && typeof stateFrame === "object", `HTTP ${state.status} ${state.url}`));
|
|
checks.push(check("state-bodies", "State bodies", Object.keys(stateFrame?.bodies ?? {}).length > 0, `${Object.keys(stateFrame?.bodies ?? {}).length} bodies in frame`, "warn"));
|
|
} catch (error) {
|
|
checks.push(check("state", "State endpoint", false, error.message));
|
|
}
|
|
} else {
|
|
checks.push(check("state", "State endpoint", false, "manifest has no state.url", "warn"));
|
|
}
|
|
|
|
const controlUrl = manifest?.state?.controlUrl
|
|
? new URL(manifest.state.controlUrl, manifestUrl).href
|
|
: manifest?.state?.url
|
|
? new URL(String(manifest.state.url).replace(/\/state$/, "/control"), manifestUrl).href
|
|
: undefined;
|
|
checks.push(check("control", "Control endpoint configured", Boolean(controlUrl), controlUrl ?? "missing", "warn"));
|
|
|
|
const failed = checks.filter((item) => item.state === "fail").length;
|
|
const warned = checks.filter((item) => item.state === "warn").length;
|
|
const passed = checks.filter((item) => item.state === "pass").length;
|
|
const result = failed > 0 ? "fail" : warned > 0 ? "warn" : "pass";
|
|
const report = {
|
|
schema: "motrixsim-web.acceptance-probe.v1",
|
|
startedAt,
|
|
finishedAt: new Date().toISOString(),
|
|
frontendUrl: frontendUrl.href,
|
|
manifestUrl,
|
|
result,
|
|
summary: { passed, warned, failed, total: checks.length },
|
|
scene: manifest
|
|
? {
|
|
name: manifest.name,
|
|
objectCount: objects.length,
|
|
meshObjectCount: meshes.length,
|
|
bodyBoundObjectCount: bodyBindings.length,
|
|
stateMode: manifest.state?.mode ?? "none",
|
|
controlUrl,
|
|
}
|
|
: undefined,
|
|
lastStateFrame: stateFrame
|
|
? {
|
|
step: stateFrame.step,
|
|
time: stateFrame.time,
|
|
paused: stateFrame.paused,
|
|
bodyCount: Object.keys(stateFrame.bodies ?? {}).length,
|
|
}
|
|
: undefined,
|
|
checks,
|
|
};
|
|
|
|
await mkdir(dirname(outputPath), { recursive: true });
|
|
await writeFile(outputPath, `${JSON.stringify(report, null, 2)}\n`);
|
|
console.log(JSON.stringify(report, null, 2));
|
|
process.exitCode = failed > 0 ? 1 : 0;
|