pcbjam/web/standalone/src/wasm/libs/models-bridge.test.ts
Istvan Matejcsok c421d724b0 findings(E-10..E-22): fix the defects a code review found in the E-1..E-9 work
A review of the group-E fixes found 13 further defects; ten were introduced by
those fixes, two pre-existed and were merely relocated, one is deferred.

Services / transport
  E-10  retireWorker synthesized no bg/exit frame, so sharedspice's s_bgRunning
        mirror stayed latched true after a mid-run worker death: Run stayed
        disabled and the promised fresh-worker restart was unreachable for the
        whole session. Retirement now dispatches a synthetic controlled-exit
        straight to the installed handler (never through dispatchEvt — a
        fabricated frame must not touch the credit ledger). Driving the repro
        exposed two further defects, both fixed here: a replacement worker
        trapped on pre-init engine reads, and the rerun's cm_input_path/circ hit
        that uninitialized engine before KiCad's validate() re-init (the native
        flow assumes a crashed engine survives in-process — true for the dll,
        false for a dead worker). Reads now answer their empty shapes pre-init,
        writes lazy-init, and init is idempotent per worker engine.
  E-19  dispatchEvt acked only AFTER handler(evt) returned, and the sharedspice
        client deliberately rethrows non-trap errors — so each throw leaked one
        unit of the 64-frame credit window until the stream died with a
        misattributed "transport exceeded". The ack moves to a finally in both
        service copies; the throw still propagates (the trap machinery needs it).
  E-20  the oversize-line path promises to transfer the accepted prefix, but
        with the window full that flush only DEFERS, and stopEventStream wiped
        the deferred queue — losing the diagnostics that explain the failure.
        The terminal notice now carries them as pendingEvents; both hosts
        deliver them in order, unacked (the fatal frame is outside the credit
        protocol).
  E-21  the 30s prefetch deadline discarded every model already collected and
        reported nothing. A caller-owned progress sink ships the partials and
        the omission reaches the export report. (Awaiting the aborted collection
        was rejected: an in-flight source fetch is not abortable — E-4's
        original disease.) Plus a serving-candidate memo, so a .wrl ref served
        by its .step fallback stops re-probing the miss on every export.

Scheduler
  E-14  _terminalizeNativeTrap classified by message substring, so any plain JS
        error QUOTING 'Aborted(' or 'out of bounds' permanently bricked a
        healthy instance. Now structural only: instanceof RuntimeError plus a
        duck-typed name check (verified in this build's glue that abort() throws
        a genuine RuntimeError both pre- and post-runtime-init). Module.onAbort
        now latches the gate — the authoritative notification, previously
        ignored.
  E-15  the shim half: _pumpResume gates on terminal (catching wakes already
        queued at latch time) and resolveWait refuses on terminal WITHOUT
        consuming the entry, so a frame stays visibly parked rather than
        resuming inside a trapped module.
  E-16  the E-5 handler read the realm-global scheduler at dispatch instead of
        its installing module's; also frees the per-line buffer on the non-trap
        rethrow path.
  E-11  get_vec trusted the worker's res.length over the transferred arrays.
        Observed death shape: a 4 GiB std::vector threw an unhandled
        std::length_error that exited the editor's main loop. Now clamped, with
        the buffers freed on every failure path.

Guardrails (replacing two deferred refactors: e2e→production-code injection and
collapsing the four copies of the worker-lifecycle machinery)
  E-18  the source contract asserted comment-string counts — rewording failed
        CI while moving a guard outside its #ifdef passed. It now parses the
        #ifdef regions and asserts on code.
        service-stub-parity.ts pins what the four lifecycle copies must share:
        credit-window equality parsed from source, the finally-ack, boot
        deadlines, terminal-notice consumption. The transport numbers are now
        single-sourced from the worker.
        CI actually runs the gates: the web/standalone vitest suites (which had
        NEVER run in CI), the reducer, the source contract and the parity tool —
        with a NON_PLAYWRIGHT_GATES check so deleting a step re-fails the lint.
  E-22  the e2e occ stub's 60s boot watchdog, deleted in a66e109, is restored in
        the ngspice-stub shape with a wedgeNextBoot() repro hook.

Every behavioral fix has red-then-green evidence (the reds were captured first).
E-17 (a stale RUNNING cross-stamping the next run's generation under E-6's
transport deferral) is DEFERRED with its analysis recorded — a real fix needs
run identity on the bg frames.

Test hygiene: the dwell lint now requires the mandated ": <why>" and all 47 bare
markers carry their reason; three export-report dwells became modal-lease polls;
exact-ledger assertions became relative deltas; the dead data-wx-dom-id branch,
an unused fault hook and unused receipt plumbing are gone; abort scans, wx
dialog drivers, the sim harness and the vitest FakeWorker are each one copy now.

Bumps kicad and wxwidgets to their findings-group-e tips.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-31 18:19:16 +02:00

374 lines
15 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import {
collectBoardModelFiles,
ensureModelInMemfs,
installModel3dHandler,
deferBoardModelPrescan,
runDeferredModelPrescan,
normalizeModelRef,
scanModelRefs,
type BoardModelFile,
} from "./models-bridge";
import type { Model3dSource } from "./models-source";
describe("normalizeModelRef", () => {
it("strips any vintage of the model-dir var", () => {
for (const v of ["KICAD6", "KICAD7", "KICAD8", "KICAD9", "KICAD10"]) {
expect(
normalizeModelRef(`\${${v}_3DMODEL_DIR}/Resistor_SMD.3dshapes/R_0201.wrl`),
).toBe("Resistor_SMD.3dshapes/R_0201.wrl");
}
});
it("accepts the paren syntax and the legacy KISYS3DMOD alias", () => {
expect(normalizeModelRef("$(KICAD8_3DMODEL_DIR)/L.3dshapes/m.step")).toBe(
"L.3dshapes/m.step",
);
expect(normalizeModelRef("${KISYS3DMOD}/L.3dshapes/m.wrl")).toBe(
"L.3dshapes/m.wrl",
);
});
it("passes bare relative refs through", () => {
expect(normalizeModelRef("L.3dshapes/m.wrl")).toBe("L.3dshapes/m.wrl");
});
it("rejects refs it cannot serve", () => {
expect(normalizeModelRef("${KIPRJMOD}/libs/3d/m.wrl")).toBeNull(); // project-local
expect(normalizeModelRef("/abs/path/m.wrl")).toBeNull();
expect(normalizeModelRef("kicad_embed://m.wrl")).toBeNull();
expect(normalizeModelRef("")).toBeNull();
expect(normalizeModelRef("${UNCLOSED/m.wrl")).toBeNull();
});
});
describe("ensureModelInMemfs format fallback", () => {
function installFakes(available: (ref: string) => boolean) {
const files = new Map<string, Uint8Array>();
const fs = {
mkdirTree: () => {},
writeFile: (p: string, b: Uint8Array) => void files.set(p, b),
analyzePath: (p: string) => ({ exists: files.has(p) }),
};
(globalThis as unknown as { window: unknown }).window ??= globalThis;
(globalThis as unknown as { FS: unknown }).FS = fs;
const source: Model3dSource = {
getModelBody: async (ref) =>
available(ref) ? new TextEncoder().encode(`body:${ref}`) : null,
hasModel: async (ref) => available(ref),
};
installModel3dHandler(source, () => {});
return files;
}
it("serves a .wrl ask from the same-stem .step, under the .step path", async () => {
// kicad-packages3D is STEP-only from 10.x — old boards still ask for .wrl.
const files = installFakes((r) => r.endsWith(".step"));
const dest = await ensureModelInMemfs("FallbackLibA.3dshapes/M1.wrl");
// Returned (and written) under the SUBSTITUTED extension: the path picks
// the parsing plugin, so the .step body must dispatch to oce, not vrml.
expect(dest).toBe("/pcbjam/3dmodels/FallbackLibA.3dshapes/M1.step");
expect(files.has("/pcbjam/3dmodels/FallbackLibA.3dshapes/M1.step")).toBe(true);
expect(files.has("/pcbjam/3dmodels/FallbackLibA.3dshapes/M1.wrl")).toBe(false);
});
it("returns the substituted .step path on the memoized second ensure", async () => {
// Regression: the first ensure (e.g. the prescan) writes the .step body and
// memoizes it; a second ensure for the SAME .wrl ref (the C++ viewer's lazy
// fallback) must hand back the .step path that exists on disk — NOT the
// ref's own .wrl path, which was never written. Returning the .wrl path
// pointed KiCad at a missing file → "Failed to retrieve file times '…​.wrl'".
const files = installFakes((r) => r.endsWith(".step"));
const first = await ensureModelInMemfs("FallbackLibD.3dshapes/M4.wrl");
const second = await ensureModelInMemfs("FallbackLibD.3dshapes/M4.wrl");
expect(first).toBe("/pcbjam/3dmodels/FallbackLibD.3dshapes/M4.step");
expect(second).toBe(first);
expect(files.has("/pcbjam/3dmodels/FallbackLibD.3dshapes/M4.wrl")).toBe(false);
});
it("prefers the exact ref when it exists", async () => {
const files = installFakes(() => true);
const dest = await ensureModelInMemfs("FallbackLibB.3dshapes/M2.wrl");
expect(dest).toBe("/pcbjam/3dmodels/FallbackLibB.3dshapes/M2.wrl");
expect(files.has("/pcbjam/3dmodels/FallbackLibB.3dshapes/M2.wrl")).toBe(true);
});
it("resolves null when no format of the model exists", async () => {
installFakes(() => false);
expect(await ensureModelInMemfs("FallbackLibC.3dshapes/M3.wrl")).toBeNull();
});
});
describe("collectBoardModelFiles", () => {
function installFakes(available: (ref: string) => boolean) {
const files = new Map<string, Uint8Array>();
const fs = {
mkdirTree: () => {},
writeFile: (p: string, b: Uint8Array) => void files.set(p, b),
analyzePath: (p: string) => ({ exists: files.has(p) }),
readFile: (p: string) => files.get(p),
};
(globalThis as unknown as { window: unknown }).window ??= globalThis;
(globalThis as unknown as { FS: unknown }).FS = fs;
const source: Model3dSource = {
getModelBody: async (ref) =>
available(ref) ? new TextEncoder().encode(`body:${ref}`) : null,
hasModel: async (ref) => available(ref),
};
installModel3dHandler(source, () => {});
}
it("collects staged bodies under their REAL extension, deduped, misses skipped", async () => {
installFakes((r) => r.startsWith("ExportLibA") && r.endsWith(".step"));
const board = `
(model "\${KICAD10_3DMODEL_DIR}/ExportLibA.3dshapes/M1.wrl")
(model "\${KICAD10_3DMODEL_DIR}/ExportLibA.3dshapes/M1.step")
(model "\${KICAD10_3DMODEL_DIR}/ExportLibB.3dshapes/GONE.wrl")
(model "\${KIPRJMOD}/libs/3d_shapes/prj.wrl")
`;
// M1.wrl is served by the .step sibling; the M1.step ref materializes to
// the SAME file → one entry. The unservable ref is skipped; the
// project-local ref never enters the scan.
const models = await collectBoardModelFiles(board);
expect(models).toHaveLength(1);
expect(models[0]!.path).toBe("ExportLibA.3dshapes/M1.step");
expect(new TextDecoder().decode(models[0]!.bytes)).toBe(
"body:ExportLibA.3dshapes/M1.step",
);
});
it("returns empty for a board without lib model refs", async () => {
installFakes(() => true);
expect(await collectBoardModelFiles("(kicad_pcb (version 1))")).toEqual([]);
});
it("makes an in-flight source result inert and starts no later ref after abort", async () => {
// repro for E-4: the prefetch abort must stop selection immediately and
// may not retain a body that resolves after the abort.
(globalThis as unknown as { window: unknown }).window ??= globalThis;
let resolveFirst!: (body: Uint8Array | null) => void;
const source: Model3dSource = {
getModelBody: vi.fn(
() => new Promise<Uint8Array | null>((resolve) => {
resolveFirst = resolve;
}),
),
hasModel: async () => true,
};
installModel3dHandler(source, () => {});
const controller = new AbortController();
const retired = new Error("exact OCC prefetch retired");
const collection = collectBoardModelFiles(
'(model "AbortA.3dshapes/A.step")\n' +
'(model "AbortB.3dshapes/B.step")',
1,
controller.signal,
);
await vi.waitFor(() => expect(source.getModelBody).toHaveBeenCalledTimes(1));
controller.abort(retired);
resolveFirst(new Uint8Array([1, 2, 3]));
await expect(collection).rejects.toBe(retired);
expect(source.getModelBody).toHaveBeenCalledTimes(1);
});
it("feeds the caller's progress sink as models are accepted", async () => {
// E-21: the prefetch caller reads the sink synchronously at its timeout —
// the accepted models must be there the moment they are accepted, and the
// scan total as soon as it is known.
(globalThis as unknown as { window: unknown }).window ??= globalThis;
let resolveSecond!: (body: Uint8Array | null) => void;
const source: Model3dSource = {
getModelBody: vi.fn((ref: string) => {
if (ref.startsWith("SinkA")) {
return Promise.resolve(new TextEncoder().encode(`body:${ref}`));
}
return new Promise<Uint8Array | null>((resolve) => {
resolveSecond = resolve;
});
}),
hasModel: async () => true,
};
installModel3dHandler(source, () => {});
const progress = { totalRefs: 0, models: [] as BoardModelFile[] };
const collection = collectBoardModelFiles(
'(model "SinkA.3dshapes/A.step")\n(model "SinkB.3dshapes/B.step")',
1,
undefined,
progress,
);
await vi.waitFor(() => expect(progress.models).toHaveLength(1));
expect(progress.totalRefs, "scan total known up front").toBe(2);
expect(progress.models[0]!.path).toBe("SinkA.3dshapes/A.step");
resolveSecond(new TextEncoder().encode("body:SinkB.3dshapes/B.step"));
const models = await collection;
expect(models, "the sink IS the result array").toBe(progress.models);
expect(models).toHaveLength(2);
});
it("gains no sink entries after abort (in-flight result stays inert)", async () => {
// E-4 barrier, restated through the sink: an aborted collection may not
// retain a body that resolves after the abort — not in its result, and
// not in the caller's progress sink either.
(globalThis as unknown as { window: unknown }).window ??= globalThis;
let resolveFirst!: (body: Uint8Array | null) => void;
const source: Model3dSource = {
getModelBody: vi.fn(
() => new Promise<Uint8Array | null>((resolve) => {
resolveFirst = resolve;
}),
),
hasModel: async () => true,
};
installModel3dHandler(source, () => {});
const controller = new AbortController();
const retired = new Error("exact OCC prefetch retired");
const progress = { totalRefs: 0, models: [] as BoardModelFile[] };
const collection = collectBoardModelFiles(
'(model "SinkAbortA.3dshapes/A.step")\n'
+ '(model "SinkAbortB.3dshapes/B.step")',
1,
controller.signal,
progress,
);
await vi.waitFor(() => expect(source.getModelBody).toHaveBeenCalledTimes(1));
controller.abort(retired);
resolveFirst(new Uint8Array([1, 2, 3]));
await expect(collection).rejects.toBe(retired);
expect(progress.totalRefs).toBe(2);
expect(progress.models, "no post-abort retention through the sink").toEqual([]);
});
it("remembers the serving fallback candidate across collects (no re-probes)", async () => {
// E-21 memo: a .wrl ref served by its .step fallback re-probed the missing
// .wrl on every export (IDB + network round-trips). The serving candidate
// is remembered per ref — positive results only, no body caching.
(globalThis as unknown as { window: unknown }).window ??= globalThis;
const getModelBody = vi.fn(async (ref: string) =>
ref.endsWith(".step") ? new TextEncoder().encode(`body:${ref}`) : null);
const source: Model3dSource = { getModelBody, hasModel: async () => true };
installModel3dHandler(source, () => {});
const board = '(model "${KICAD10_3DMODEL_DIR}/MemoLib.3dshapes/M1.wrl")';
await collectBoardModelFiles(board);
expect(getModelBody.mock.calls.map((call) => call[0]),
"first collect probes the .wrl miss then the .step hit").toEqual([
"MemoLib.3dshapes/M1.wrl",
"MemoLib.3dshapes/M1.step",
]);
getModelBody.mockClear();
const models = await collectBoardModelFiles(board);
expect(getModelBody.mock.calls.map((call) => call[0]),
"second collect goes straight to the remembered candidate").toEqual([
"MemoLib.3dshapes/M1.step",
]);
expect(models[0]!.path).toBe("MemoLib.3dshapes/M1.step");
});
it("never touches the editor MEMFS (pure source/IDB/network path)", async () => {
// repro for E-4: routing bodies through the editor heap added a stale
// native-completion tail after a prefetch timeout — the collect path must
// stay off FS entirely (the OCC worker stages bodies in its own MEMFS).
const fs = {
mkdirTree: vi.fn(),
writeFile: vi.fn(),
analyzePath: vi.fn(() => ({ exists: false })),
readFile: vi.fn(),
};
(globalThis as unknown as { window: unknown }).window ??= globalThis;
(globalThis as unknown as { FS: unknown }).FS = fs;
const source: Model3dSource = {
getModelBody: async (ref) => new TextEncoder().encode(`body:${ref}`),
hasModel: async () => true,
};
installModel3dHandler(source, () => {});
const models = await collectBoardModelFiles(
'(model "PureLib.3dshapes/M1.step")',
);
expect(models).toHaveLength(1);
expect(fs.mkdirTree).not.toHaveBeenCalled();
expect(fs.writeFile).not.toHaveBeenCalled();
expect(fs.readFile).not.toHaveBeenCalled();
expect(fs.analyzePath).not.toHaveBeenCalled();
});
});
describe("scanModelRefs", () => {
it("finds, normalizes and dedupes board model refs", () => {
const board = `
(footprint "Resistor_THT:R_Axial"
(model "\${KICAD10_3DMODEL_DIR}/Resistor_THT.3dshapes/R_Axial.step"
(offset (xyz 0 0 0))))
(footprint "Resistor_THT:R_Axial"
(model "\${KICAD10_3DMODEL_DIR}/Resistor_THT.3dshapes/R_Axial.step"))
(footprint "X:Y"
(model "\${KIPRJMOD}/libs/3d_shapes/custom.wrl"))
(footprint "L:M" (model "\${KICAD8_3DMODEL_DIR}/LED_THT.3dshapes/LED_D5.0mm.wrl"))
`;
expect(scanModelRefs(board).sort()).toEqual([
"LED_THT.3dshapes/LED_D5.0mm.wrl",
"Resistor_THT.3dshapes/R_Axial.step",
]);
});
it("handles escaped quotes inside the path", () => {
expect(
scanModelRefs('(model "${KICAD9_3DMODEL_DIR}/A.3dshapes/we\\"ird.wrl")'),
).toEqual(['A.3dshapes/we"ird.wrl']);
});
it("returns empty for a board with no models", () => {
expect(scanModelRefs("(kicad_pcb (version 20240101))")).toEqual([]);
});
});
describe("deferred board prescan (read-only sessions)", () => {
function installCounting() {
const files = new Map<string, Uint8Array>();
const fs = {
mkdirTree: () => {},
writeFile: (p: string, b: Uint8Array) => void files.set(p, b),
analyzePath: (p: string) => ({ exists: files.has(p) }),
};
(globalThis as unknown as { window: unknown }).window ??= globalThis;
// The prescan emits its progress event on window (node: bare globalThis).
(globalThis as unknown as { dispatchEvent?: unknown }).dispatchEvent ??= () => true;
(globalThis as unknown as { FS: unknown }).FS = fs;
let fetches = 0;
const source: Model3dSource = {
getModelBody: async (ref) => {
fetches++;
return new TextEncoder().encode(`body:${ref}`);
},
hasModel: async () => true,
};
installModel3dHandler(source, () => {});
return { files, fetches: () => fetches };
}
it("fetches nothing at defer time and everything on the first run", async () => {
const t = installCounting();
deferBoardModelPrescan(
'(model "${KICAD10_3DMODEL_DIR}/DeferLib.3dshapes/A.step") (model "${KICAD10_3DMODEL_DIR}/DeferLib.3dshapes/B.step")',
);
expect(t.fetches()).toBe(0);
await runDeferredModelPrescan();
expect(t.fetches()).toBe(2);
expect(t.files.has("/pcbjam/3dmodels/DeferLib.3dshapes/A.step")).toBe(true);
// Consumed: a second open does not re-scan.
await runDeferredModelPrescan();
expect(t.fetches()).toBe(2);
});
it("is a no-op when nothing was deferred", async () => {
const t = installCounting();
await runDeferredModelPrescan();
expect(t.fetches()).toBe(0);
});
});