100 lines
3.6 KiB
JavaScript
100 lines
3.6 KiB
JavaScript
import { createBrowserSmokeHarness } from "/test-browser-wasm-smoke-helpers.js";
|
|
import { runBrowserAppOpfsSections } from "/test-browser-wasm-smoke-app-sections.js";
|
|
import { runLinuxCncBrowserSections } from "/test-browser-wasm-smoke-linuxcnc-sections.js";
|
|
import { runBrowserOpfsPolicySections } from "/test-browser-wasm-smoke-opfs-policy-sections.js";
|
|
import { runBrowserOpfsWorkspaceSections } from "/test-browser-wasm-smoke-opfs-workspace-sections.js";
|
|
|
|
export async function runBrowserWasmSmoke({ createWasmSimulator, result }) {
|
|
const harness = createBrowserSmokeHarness(result);
|
|
const createSmokeSimulator = (opfs = { mountPoint: "/cnc", workspacePath: "cnc-simulator-smoke" }) =>
|
|
createWasmSimulator({
|
|
locateFile: (file) => `/public/${file}`,
|
|
print: () => {},
|
|
printErr: () => {},
|
|
...(opfs === undefined ? {} : { opfs }),
|
|
});
|
|
const createDefaultBrowserSimulator = () =>
|
|
createWasmSimulator({
|
|
locateFile: (file) => `/public/${file}`,
|
|
print: () => {},
|
|
printErr: () => {},
|
|
});
|
|
const withSmokeSimulator = async (action, opfs) => {
|
|
const nestedSimulator = await createSmokeSimulator(opfs);
|
|
try {
|
|
return await action(nestedSimulator);
|
|
} finally {
|
|
nestedSimulator.dispose();
|
|
}
|
|
};
|
|
const createHiddenAppFrame = async () => {
|
|
const appFrame = document.createElement("iframe");
|
|
const appHtml = await (await fetch("/")).text();
|
|
appFrame.srcdoc = appHtml.replace(
|
|
"</head>",
|
|
'<script src="/public/cnc_sim.js"><\/script></head>',
|
|
);
|
|
appFrame.style.display = "none";
|
|
document.body.appendChild(appFrame);
|
|
await new Promise((resolve) => {
|
|
appFrame.addEventListener("load", resolve, { once: true });
|
|
});
|
|
return appFrame;
|
|
};
|
|
const waitForAppControls = (appFrame, message) =>
|
|
harness.waitFor(
|
|
() => appFrame.contentDocument?.querySelector("#parseBtn"),
|
|
message,
|
|
);
|
|
const parseAppFrameProgram = (appFrame, program) => {
|
|
const appInput = appFrame.contentDocument.querySelector("#programInput");
|
|
appInput.value = program;
|
|
appInput.dispatchEvent(new Event("input", { bubbles: true }));
|
|
appFrame.contentDocument.querySelector("#parseBtn").click();
|
|
};
|
|
const waitForAppAlarm = (appFrame, text, message) =>
|
|
harness.waitFor(
|
|
() => appFrame.contentDocument?.querySelector("#alarmList")?.textContent.includes(text),
|
|
message,
|
|
);
|
|
const getAppFrameSimulator = async (appFrame, message) => {
|
|
const appSimulator = await appFrame.contentWindow.__cncSimulatorForTest;
|
|
if (!appSimulator?.fs?.opfs) {
|
|
throw new Error(message);
|
|
}
|
|
return appSimulator;
|
|
};
|
|
const disposeAppFrame = async (appFrame) => {
|
|
const appSimulator = await appFrame.contentWindow?.__cncSimulatorForTest;
|
|
appSimulator?.dispose?.();
|
|
appFrame.remove();
|
|
};
|
|
const context = {
|
|
...harness,
|
|
createDefaultBrowserSimulator,
|
|
createHiddenAppFrame,
|
|
createSmokeSimulator,
|
|
disposeAppFrame,
|
|
getAppFrameSimulator,
|
|
parseAppFrameProgram,
|
|
waitForAppAlarm,
|
|
waitForAppControls,
|
|
withSmokeSimulator,
|
|
};
|
|
|
|
try {
|
|
const simulator = await createSmokeSimulator();
|
|
try {
|
|
await runLinuxCncBrowserSections(context, simulator);
|
|
await runBrowserOpfsWorkspaceSections(context, simulator);
|
|
await runBrowserOpfsPolicySections(context);
|
|
await runBrowserAppOpfsSections(context);
|
|
result.textContent = harness.completedMessage();
|
|
} finally {
|
|
simulator.dispose();
|
|
}
|
|
} catch (error) {
|
|
result.textContent = `browser wasm smoke failed: ${error && error.stack ? error.stack : error}`;
|
|
}
|
|
}
|