import assert from "node:assert/strict"; import { readFile } from "node:fs/promises"; import { WorkerClient, CONTEXT_ID } from "./worker-client.mjs"; class FakeWorker { constructor() { this.sent = []; this.onmessage = null; this.onerror = null; } postMessage(message) { this.sent.push(message); queueMicrotask(() => this.onmessage?.({ data: { schema_version: 1, kind: message.kind === "HELLO" ? "CAPABILITIES" : message.kind === "SESSION_ATTACH" ? "SESSION_READY" : message.kind === "SNAPSHOT_REQUEST" ? "STATE_SNAPSHOT" : "COMMAND_RESULT", context_id: message.context_id, session_id: message.session_id, seq: message.seq, operation_id: message.operation_id, simulation_tick: message.kind === "SIM_STEP" ? message.payload.cycles : 0, payload: { status: "verified", tick: message.payload.cycles ?? 0 }, } })); } terminate() { this.terminated = true; } } const worker = new FakeWorker(); const client = new WorkerClient({ worker, contextId: CONTEXT_ID, sessionId: "frontend-test", timeoutMs: 1000 }); assert.equal((await client.hello()).kind, "CAPABILITIES"); assert.equal((await client.attach()).kind, "SESSION_READY"); const first = await client.step(3, "same-operation"); const sentCount = worker.sent.length; const replay = await client.step(99, "same-operation"); assert.deepEqual(replay, first); assert.equal(worker.sent.length, sentCount); assert.equal(worker.sent.every((message) => message.context_id === CONTEXT_ID), true); client.close(); assert.equal(worker.terminated, true); const [html, app, css] = await Promise.all([ readFile(new URL("./index.html", import.meta.url), "utf8"), readFile(new URL("./app.mjs", import.meta.url), "utf8"), readFile(new URL("./styles.css", import.meta.url), "utf8"), ]); assert.match(html, /type="module"/); assert.match(app, /WorkerClient/); assert.match(app, /loadWorkspace/); assert.match(app, /loadSimulationCase/); assert.match(app, /jogStart/); assert.match(app, /override/); assert.doesNotMatch(app, /_sim_|wasm-core|FileSystemSyncAccessHandle/); assert.match(css, /@media/); console.log("frontend_m1=passed messages=" + worker.sent.length);