Files
cnc_wams/wasm-port/tests/browser/ini_panel_smoke.html
wangdequan 8c7c2f1079 按规划继续工作
结论:补齐通用会话快照 envelope 的格式、版本、会话 ID 与对象形状拒绝验证,覆盖 Node OPFS 与 Chromium OPFS 路径,并通过 host/WASM/browser 聚合验证。
2026-06-08 09:16:24 +08:00

266 lines
9.5 KiB
HTML

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>LinuxCNC INI Browser Smoke</title>
</head>
<body>
<pre id="status">running</pre>
<script type="module">
import { createLinuxCncIniSdk } from "../../runtime/sdk/src/index.js";
import { loadTextFile, saveTextFile } from "../../runtime/opfs/file-service.js";
import { machineIniPath, sessionSnapshotPath } from "../../runtime/opfs/path-model.js";
import {
loadSessionSnapshot,
saveSessionSnapshot,
} from "../../runtime/opfs/snapshot-store.js";
import {
loadGcodeProgram,
loadMachineTextFiles,
saveGcodeProgram,
saveMachineTextFiles,
} from "../../runtime/opfs/machine-file-store.js";
const status = document.getElementById("status");
function assertEqual(actual, expected, label) {
if (actual !== expected) {
throw new Error(`${label}: expected ${expected}, got ${actual}`);
}
}
async function assertRejects(label, operation, expectedPattern) {
try {
await operation();
} catch (error) {
if (!expectedPattern.test(error.message)) {
throw new Error(`${label}: unexpected error ${error.message}`);
}
return;
}
throw new Error(`${label}: expected rejection`);
}
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function waitFor(predicate, label) {
const deadline = performance.now() + 8000;
while (performance.now() < deadline) {
const value = predicate();
if (value) {
return value;
}
await delay(50);
}
throw new Error(`Timed out waiting for ${label}`);
}
async function loadUiFrame() {
const frame = document.createElement("iframe");
frame.src = "../../runtime/ui/ini-panel/index.html";
document.body.appendChild(frame);
await new Promise((resolve, reject) => {
frame.addEventListener("load", resolve, { once: true });
frame.addEventListener("error", reject, { once: true });
});
return frame.contentDocument;
}
try {
const ini = await createLinuxCncIniSdk();
const wasmPath = "/work/browser-smoke.ini";
const iniText = `[EMC]
MACHINE = browser-smoke
[TRAJ]
LINEAR_UNITS = mm
COORDINATES = X Y Z
[KINS]
KINEMATICS = trivkins
JOINTS = 3
`;
ini.writeTextFile(wasmPath, iniText);
assertEqual(ini.getString(wasmPath, "EMC", "MACHINE"), "browser-smoke", "machine");
assertEqual(ini.getString(wasmPath, "TRAJ", "LINEAR_UNITS"), "mm", "units");
assertEqual(ini.getString(wasmPath, "KINS", "KINEMATICS"), "trivkins", "kinematics");
assertEqual(ini.getString(wasmPath, "TRAJ", "MISSING"), null, "missing tag");
const fields = ini.getFields(wasmPath, {
machine: { section: "EMC", tag: "MACHINE" },
joints: { section: "KINS", tag: "JOINTS" },
});
assertEqual(fields.machine, "browser-smoke", "field machine");
assertEqual(fields.joints, "3", "field joints");
const opfsPath = machineIniPath("browser-smoke");
await saveTextFile(opfsPath, iniText);
assertEqual(await loadTextFile(opfsPath), iniText, "opfs roundtrip");
const snapshotPayload = { files: { ini: opfsPath } };
const savedSnapshot = await saveSessionSnapshot(
"browser-session",
snapshotPayload,
{
createdAt: "2026-06-08T00:00:00.000Z",
metadata: { source: "browser-smoke" },
},
);
const loadedSnapshot = await loadSessionSnapshot("browser-session");
assertEqual(loadedSnapshot.format, "linuxcnc-wasm-session-snapshot", "snapshot format");
assertEqual(loadedSnapshot.version, 1, "snapshot version");
assertEqual(loadedSnapshot.sessionId, "browser-session", "snapshot session");
assertEqual(loadedSnapshot.createdAt, savedSnapshot.createdAt, "snapshot timestamp");
assertEqual(loadedSnapshot.payload.files.ini, opfsPath, "snapshot payload");
assertEqual(loadedSnapshot.metadata.source, "browser-smoke", "snapshot metadata");
await saveTextFile(
sessionSnapshotPath("browser-bad-format"),
JSON.stringify({
...loadedSnapshot,
format: "other-format",
sessionId: "browser-bad-format",
}),
);
await assertRejects(
"browser snapshot bad format",
() => loadSessionSnapshot("browser-bad-format"),
/Unsupported session snapshot format/,
);
await saveTextFile(
sessionSnapshotPath("browser-bad-version"),
JSON.stringify({
...loadedSnapshot,
version: 99,
sessionId: "browser-bad-version",
}),
);
await assertRejects(
"browser snapshot bad version",
() => loadSessionSnapshot("browser-bad-version"),
/Unsupported session snapshot version/,
);
await saveTextFile(
sessionSnapshotPath("browser-bad-session-id"),
JSON.stringify({
...loadedSnapshot,
sessionId: "other-browser-session",
}),
);
await assertRejects(
"browser snapshot id mismatch",
() => loadSessionSnapshot("browser-bad-session-id"),
/Session snapshot id mismatch/,
);
await saveMachineTextFiles("browser-smoke", {
ini: iniText,
toolTable: "T0 P0 ; no tool\n",
parameters: "5161 0.0\n",
});
const machineFiles = await loadMachineTextFiles("browser-smoke");
assertEqual(machineFiles.ini, iniText, "machine ini text");
assertEqual(machineFiles.toolTable, "T0 P0 ; no tool\n", "tool table text");
assertEqual(machineFiles.parameters, "5161 0.0\n", "parameter text");
await saveGcodeProgram("browser-smoke.ngc", "G0 X0 Y0\nM2\n");
assertEqual(await loadGcodeProgram("browser-smoke.ngc"), "G0 X0 Y0\nM2\n", "gcode text");
const uiDocument = await loadUiFrame();
await waitFor(
() => uiDocument.getElementById("wasm-badge")?.textContent.includes("ready"),
"INI panel INI WASM readiness",
);
await waitFor(
() => uiDocument.getElementById("interp-badge")?.textContent.includes("ready"),
"INI panel interpreter WASM readiness",
);
await waitFor(
() => uiDocument.getElementById("opfs-badge")?.textContent.includes("ready"),
"INI panel OPFS readiness",
);
uiDocument.getElementById("ini-editor").value = iniText;
uiDocument.getElementById("save-machine-files").click();
await waitFor(
() => uiDocument.getElementById("log")?.textContent.includes("Saved machine text files"),
"machine file save",
);
uiDocument.getElementById("load-session").click();
await waitFor(
() => uiDocument.getElementById("log")?.textContent.includes("Loaded machine session"),
"machine session load",
);
assertEqual(
uiDocument.getElementById("field-session-ini").textContent,
"/work/session-machine.ini",
"UI session INI path",
);
assertEqual(
uiDocument.getElementById("field-session-parameters").textContent,
"/work/session-linuxcnc.var",
"UI session parameter path",
);
assertEqual(
uiDocument.getElementById("field-session-tool-table").textContent,
"/work/session-tool.tbl",
"UI session tool table path",
);
const uiLog = uiDocument.getElementById("log").textContent;
if (!uiLog.includes("restore_parameters=0") || !uiLog.includes("tooldata_load=0")) {
throw new Error(`UI session log missing LinuxCNC load result: ${uiLog}`);
}
uiDocument.getElementById("run-gcode").click();
await waitFor(
() => uiDocument.getElementById("log")?.textContent.includes("Ran G-code"),
"G-code run",
);
assertEqual(
uiDocument.getElementById("field-session-gcode").textContent,
"/work/ui-session.ngc",
"UI session G-code path",
);
assertEqual(
uiDocument.getElementById("field-run-status").textContent,
"ok",
"UI G-code run status",
);
const runLog = uiDocument.getElementById("log").textContent;
if (
!runLog.includes("canon_event=STRAIGHT_TRAVERSE") ||
!runLog.includes("canon_event=STRAIGHT_FEED")
) {
throw new Error(`UI G-code run missing LinuxCNC canonical events: ${runLog}`);
}
const eventText = uiDocument.getElementById("canon-events").textContent;
if (
!eventText.includes("canon_event=STRAIGHT_TRAVERSE") ||
!eventText.includes("canon_event=STRAIGHT_FEED")
) {
throw new Error(`UI canonical event panel missing LinuxCNC events: ${eventText}`);
}
const eventFilter = uiDocument.getElementById("event-filter");
eventFilter.value = "STRAIGHT_FEED";
eventFilter.dispatchEvent(new Event("input", { bubbles: true }));
const filteredEventText = uiDocument.getElementById("canon-events").textContent;
if (
filteredEventText.includes("canon_event=STRAIGHT_TRAVERSE") ||
!filteredEventText.includes("canon_event=STRAIGHT_FEED")
) {
throw new Error(`UI canonical event filter did not preserve raw LinuxCNC lines: ${filteredEventText}`);
}
assertEqual(
uiDocument.getElementById("event-count").textContent,
"1 / 4",
"UI canonical event count",
);
status.textContent = "browser_ini_opfs_smoke=ok";
} catch (error) {
status.textContent = `browser_ini_opfs_smoke=fail ${error.stack || error.message}`;
}
</script>
</body>
</html>