pcbjam/tests/kicad/eeschema-sim.spec.ts

134 lines
6.2 KiB
TypeScript
Raw Permalink Normal View History

import { test, expect } from './fixtures';
import { PNG } from 'pngjs';
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-27 12:27:12 +02:00
import { stableShot, waitForEditorReady } from '../e2e/utils/element-tracker';
import { loadRectifier, openSimulator, runSimulation } from './utils/sim-harness';
/**
* eeschema simulator end-to-end (docs/features/ngspice-split/): the historic
* kill-point was SIMULATOR_FRAME never opening (no dlopen for libngspice);
* now NGSPICE binds the sharedspice client stub and the engine runs in the
* lazy ngspice_service worker. These specs drive the REAL UI path:
* project open → Inspect → Simulator → Run → plot, asserting the RPC/event
* plumbing (window.__ngspiceEvents / __ngspiceLog from the harness provider,
* tests/kicad/utils/ngspice-service.ts) and the rendered result.
*
* Fixture: the complete kicad demo rectifier project — its 1N4148 lives in a
* sibling diode.mod pulled in via `.include`, so a passing transient also
* proves the client stub's netlist file shipping (a missing model fails the
* run with "unable to find definition of model").
*/
function distinctColors(png: PNG): number {
const colors = new Set<number>();
// 8x8 grid sampling, same spirit as the 3d-viewer render check.
const stepX = Math.max(1, Math.floor(png.width / 8));
const stepY = Math.max(1, Math.floor(png.height / 8));
for (let y = 0; y < png.height; y += stepY) {
for (let x = 0; x < png.width; x += stepX) {
const i = (png.width * y + x) << 2;
colors.add((png.data[i] << 16) | (png.data[i + 1] << 8) | png.data[i + 2]);
}
}
return colors.size;
}
test.describe('eeschema simulator', () => {
test.describe.configure({ mode: 'serial' });
test.setTimeout(300000);
test('Inspect → Simulator opens the frame; service fetches lazily', async ({ page, testLogger }) => {
const ngspiceFetches: string[] = [];
page.on('request', (r) => {
if (r.url().includes('ngspice_service')) ngspiceFetches.push(r.url());
});
await page.goto('/kicad/eeschema.html');
await waitForEditorReady(page);
await loadRectifier(page);
expect(ngspiceFetches,
'ngspice_service must NOT be fetched before the simulator opens')
.toHaveLength(0);
await openSimulator(page);
await stableShot(page, 'eeschema-sim-frame.png');
// NGSPICE::init_dll ran inside the frame ctor → the client stub's init
// RPC booted the worker.
expect(ngspiceFetches.length,
'ngspice_service fetched lazily by the simulator open')
.toBeGreaterThan(0);
const all = [...testLogger.consoleLogs, ...testLogger.errors];
expect(all.filter((l) => l.includes('Aborted(')), 'no aborts').toHaveLength(0);
});
test('transient run: live console stream, vectors reach the plot, plot renders', async ({ page, testLogger }) => {
await page.goto('/kicad/eeschema.html');
await waitForEditorReady(page);
await loadRectifier(page);
const simWin = await openSimulator(page);
await runSimulation(page);
const evts = await page.evaluate(() => (window as any).__ngspiceEvents as Array<{
kind: string; lines?: string[]; finished?: boolean; t: number }>);
// Live streaming: console/status output must precede the finish event.
const finishT = evts.filter((e) => e.kind === 'bg' && e.finished).map((e) => e.t)[0];
const streamed = evts.filter(
(e) => (e.kind === 'char' || e.kind === 'stat') && e.t <= finishT);
expect(streamed.length, 'ngspice output streamed during the run')
.toBeGreaterThan(3);
// The model shipped via .include resolved (a miss fails the run with
// "unable to find definition" and produces no transient).
const charText = evts.flatMap((e) => e.lines ?? []).join('\n');
expect(charText, 'no missing-model errors').not.toMatch(/unable to find definition/i);
findings(E-5,E-8,E-9): module-identity ngspice events + runWaitCompletion admission gate E-8 (re-implemented for JSPI — the codex gate is entangled with the dropped execution owner; under JSPI a fresh non-suspending JS→wasm entry while another activation is suspended is structurally safe on its own stack, so the admission boundary for worker completions is liveness + trap state, not execution ownership): - jspi-scheduler.js grows `terminal` (trapped instance; distinct from `dead`), canTouchNative(), _terminalizeNativeTrap() (WebAssembly.RuntimeError + cross-realm string classification), and runWaitCompletion(site, token, prepare, inertResult): prepare runs immediately and owns ALL native work; stale tokens and dead/terminal instances drop loudly without resolving (resolving would resume the parked frame inside the damaged module); a trap latches terminal; a plain JS bug resolves inertResult so the wait fails instead of stranding. beginWait refuses (token 0) when dead/terminal. - all four delayed completion sites route their native work through the gate: 'OCC export completion' (exporter_step_stub), 'OCC model completion' (oce_plugin_stub — the MEMFS cache write moves inside the gate too), 'ngspice request completion' and 'ngspice vector completion' (sharedspice_client — every HEAP32/HEAPF64/malloc write inside prepare, inertResult 1 = transport error). Every wxWasmBeginWait caller in the stubs bails on token <= 0. - deliberately NOT ported from codex: ownerModule, enqueueNativeCompletion, executionBarrier, the byte-credit native-entry FIFO — completions are one-shot per wait token and stream volume is bounded at the E-6 transport credit window. Cross-refs logged for group M (M-2/M-6/M-8). E-5 (re-implemented; codex shape kept, owner APIs replaced with the E-8 gate): js_ngspice_install_events binds the handler to the EXACT installing module (handler.__pcbjamNgspiceOwnerModule stamp; presence is not identity), re-installation replaces a foreign module's handler, a superseded handler disarms itself, native entry goes through installingModule._malloc/ ._pcbjam_ngspice_event (never lexical Module), each dispatch checks canTouchNative() (loud drop on a dead/terminal module), and a trap on the per-line entry latches the terminal gate. Tests: scheduler-shim.test.ts +7 (gate happy/stale/dead/terminal/cross-realm/ js-bug/beginWait-refusal). e2e specs updated from the codex line: occ-export decode-fault recovery (real onmessageerror transition via failDecode, J-4), ngspice-probe direct-service coverage, eeschema-sim rewritten onto the E-7 applied-generation receipt (codex's executionBarrier await replaced with a pendingWaits('ngspice') drain poll — the JSPI-line equivalent). Also bumps the kicad submodule to the E-7/E-9 commit (dd5751038f7). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 16:03:16 +02:00
// The exact final-refresh receipt and drained-waits check above prove
// this log entry belongs to a vector which reached the plot, not
// merely a worker response still waiting to copy into native memory.
const vecPulls = await page.evaluate(() =>
((window as any).__ngspiceLog as Array<{ kind: string; length?: number }>)
.filter((l) => l.kind === 'get_vec_info' && (l.length ?? 0) > 100).length);
expect(vecPulls, 'plot fetched transient vectors').toBeGreaterThan(0);
// The plot area rendered something beyond a flat background.
const shot = await page.locator(`#${simWin}`).screenshot({
scale: 'css', animations: 'disabled' });
const png = PNG.sync.read(shot);
expect(distinctColors(png), 'plot window shows structure (axes/trace)')
.toBeGreaterThan(6);
await stableShot(page, 'eeschema-sim-plot.png');
const all = [...testLogger.consoleLogs, ...testLogger.errors];
expect(all.filter((l) => l.includes('Aborted(')), 'no aborts').toHaveLength(0);
const corruption = all.filter((l) =>
l.includes('index out of bounds') || l.includes('indirect call to null')
|| l.includes('uncaught exception: unwind'));
jspi cleanup: remove the asyncify-era residue — dead code, conditionals, pipeline scaffolding, stale prose The runtime is JSPI-only; this removes everything that still pretended otherwise. Three exhaustive sweeps (C++/JS+build+CI/tests+docs) drove the inventory; every deletion verified by grep closure + full gates. Broken-right-now fixes: - deploy-staging.yml passed the retired opt_level input — the workflow could not even start. Removed. - env.sh carried dead exports with a live -sASYNCIFY=1 inside (WASM_LDFLAGS/PTHREAD_LDFLAGS, zero consumers). Removed; the WASM_LEGACY_EXCEPTIONS rationale rewritten to the real reason. - docker/build.sh exported PCBJAM_ASYNC_BACKEND (read nowhere). Gone. Dead weight removed: - binaryen submodule (nothing builds or invokes it), wasm-opt-bench workflow + scripts/bench/, get-wasm-opt.sh, diagnostics.js (242 lines of Asyncify-API-only code), the KICAD_PIPELINE background-postprocess scaffolding (existed to parallelize the deleted wasm-opt phase; the postprocess is a seconds-long node script and now runs inline), build-monitor's dead asyncify rows, sched-context orphan build output, dead .gitignore entries, the .jspi-assets spike dir (the two wf-result research JSONs moved to docs/features/async/migration-evidence/). - bindings: fiber_park.h + its 12 embind registrations (broken-if- called under JSPI), the kicadOpenFileStart/OPEN_JOB starter route, main_stack_runner.h + 5 includes, the always-null context-sleep weak hook in nanosleep_yield.c. - shim: the backend field (installed-flag idempotency instead), noteContextWait (dead both sides), the __wxAsyncifyDump alias (+ the WasmTool fallback and string-dump normalize branch). - web: the emscripten-6-ignored mainScriptUrlOrBlob option in boot.ts (gerber-demo keeps it: it loads the deployed CDN release, which predates emscripten 6 — noted inline). Conditionals: all 'backend === jspi' checks reduced to scheduler- presence checks; races_quiescent re-keyed from Asyncify.state (vacuous) to real backlog quiescence (resumeReady/mutatorQueue — NOT _windowLive, which is the probing activation's own window by definition). Renames (identifiers only, no file renames): ASYNC_LINK_FLAGS→ JSPI_LINK_FLAGS and Makefile ASYNC_LDFLAGS→JSPI_LDFLAGS, kicadCollabFiberBusy→kicadCollabBusy (embind + web + tests), collab_common.h fiber*→apply*/coroutine naming, asyncifySignatures→ wasmTrapSignatures (lists byte-identical). Tests: the two remaining vacuous [wx-asyncify]/fiber-resume-refused asserts re-keyed to live JSPI beacons; eeschema-load's failure message no longer sends the developer to a deleted script; wait-beacons' dead families/parser deleted; lane-0 legacy-glue guards removed (lane 0 is unconstructible); the embind test.fail re-gated with the JSPI reason (plain embind invokers cannot suspend — verified still failing); lint-determinism now scans tests/jspi (166 files clean); eeschema-collab local-move gated to chromium (~50% flaky on FF even solo; pcbnew twin covers both engines). Docs: DEBUG.md rewritten as the JSPI debugging guide; build.md describes the single-phase build; docs/features/async/README.md banner-marked historical and repointed at the NEW 23-jspi-runtime.md (current architecture: export census, turnstile, libcontext ownership + refusal contract, embind call shapes, the em-pthread service-wrapper trick, exception policy, known gaps). Gates on the cleaned tree: test:e2e 725 passed / 0 failed (after the quiescence-probe fix; the 3 other reds were verified contention flakes solo-green or the documented FF gate), web 76/0, jspi 18/18 both engines, vitest 295/295 + 17/17, all lints green, live-app census clean. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016X9eh1s5sTx1o9Em9KBuwR
2026-08-14 09:25:32 +02:00
expect(corruption, 'no wasm trap').toHaveLength(0);
});
test('a second run after the first succeeds (engine reset path)', async ({ page, testLogger }) => {
await page.goto('/kicad/eeschema.html');
await waitForEditorReady(page);
await loadRectifier(page);
await openSimulator(page);
findings(E-5,E-8,E-9): module-identity ngspice events + runWaitCompletion admission gate E-8 (re-implemented for JSPI — the codex gate is entangled with the dropped execution owner; under JSPI a fresh non-suspending JS→wasm entry while another activation is suspended is structurally safe on its own stack, so the admission boundary for worker completions is liveness + trap state, not execution ownership): - jspi-scheduler.js grows `terminal` (trapped instance; distinct from `dead`), canTouchNative(), _terminalizeNativeTrap() (WebAssembly.RuntimeError + cross-realm string classification), and runWaitCompletion(site, token, prepare, inertResult): prepare runs immediately and owns ALL native work; stale tokens and dead/terminal instances drop loudly without resolving (resolving would resume the parked frame inside the damaged module); a trap latches terminal; a plain JS bug resolves inertResult so the wait fails instead of stranding. beginWait refuses (token 0) when dead/terminal. - all four delayed completion sites route their native work through the gate: 'OCC export completion' (exporter_step_stub), 'OCC model completion' (oce_plugin_stub — the MEMFS cache write moves inside the gate too), 'ngspice request completion' and 'ngspice vector completion' (sharedspice_client — every HEAP32/HEAPF64/malloc write inside prepare, inertResult 1 = transport error). Every wxWasmBeginWait caller in the stubs bails on token <= 0. - deliberately NOT ported from codex: ownerModule, enqueueNativeCompletion, executionBarrier, the byte-credit native-entry FIFO — completions are one-shot per wait token and stream volume is bounded at the E-6 transport credit window. Cross-refs logged for group M (M-2/M-6/M-8). E-5 (re-implemented; codex shape kept, owner APIs replaced with the E-8 gate): js_ngspice_install_events binds the handler to the EXACT installing module (handler.__pcbjamNgspiceOwnerModule stamp; presence is not identity), re-installation replaces a foreign module's handler, a superseded handler disarms itself, native entry goes through installingModule._malloc/ ._pcbjam_ngspice_event (never lexical Module), each dispatch checks canTouchNative() (loud drop on a dead/terminal module), and a trap on the per-line entry latches the terminal gate. Tests: scheduler-shim.test.ts +7 (gate happy/stale/dead/terminal/cross-realm/ js-bug/beginWait-refusal). e2e specs updated from the codex line: occ-export decode-fault recovery (real onmessageerror transition via failDecode, J-4), ngspice-probe direct-service coverage, eeschema-sim rewritten onto the E-7 applied-generation receipt (codex's executionBarrier await replaced with a pendingWaits('ngspice') drain poll — the JSPI-line equivalent). Also bumps the kicad submodule to the E-7/E-9 commit (dd5751038f7). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 16:03:16 +02:00
const firstGeneration = await runSimulation(page);
const secondGeneration = await runSimulation(page);
expect(secondGeneration, 'the second run has its own exact generation')
.toBeGreaterThan(firstGeneration);
const finishCount = await page.evaluate(() =>
((window as any).__ngspiceEvents as Array<{ kind: string; finished?: boolean }>)
.filter((e) => e.kind === 'bg' && e.finished === true).length);
expect(finishCount, 'two completed runs').toBeGreaterThanOrEqual(2);
const all = [...testLogger.consoleLogs, ...testLogger.errors];
expect(all.filter((l) => l.includes('Aborted(')), 'no aborts').toHaveLength(0);
});
});