pcbjam/tests/kicad/utils/fs-inject.ts
Viktor Vaczi 31ff88ee9e tests: load-pcb e2e for microwave + pic_programmer demos
Adds an end-to-end test that drives File→Open in pcbnew, injects
the .kicad_pcb and .kicad_pro files into MEMFS at the dialog's
default starting directory, drives the menu + filename text input
+ Enter accept path, and screenshots the loaded board. Parametrized
for both kicad/demos/microwave (RF polygon footprints) and
kicad/demos/pic_programmer (full multi-IC layout).

Without the rtree fix bumped in via the kicad submodule, the load
would abort on every PCB at rtree.h:1771 Classify; the test asserts
no [RTREE-DIAG] line and no Aborted(. The post-load clipboard
RuntimeError in __asyncjs__js_clipboardHasText is a separate,
pre-existing wasm-port limitation that we explicitly do not regress
on here.

- tests/kicad/load-pcb.spec.ts: serial-mode parametrized spec
- tests/kicad/load-pcb-probe.spec.ts: one-shot diagnostic probe
  for inspecting wxFileDialog state on the canvas
- tests/kicad/utils/fs-inject.ts: FS.writeFile bridge from Node fs
- tests/kicad/utils/board-ready.ts: poll-for-no-dialogs readiness
- tests/baseline-screenshots/load-pcb-*.png: 6 baselines covering
  both demos at pcbnew-ready / dialog-open / loaded states
- features/.../rtree-debug-findings.md: full diagnosis trail with
  an upstream-reportable summary the maintainer can lift verbatim
- kicad submodule bumped to 07d8130d44 (shape_poly_set rtree fix)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-29 09:51:18 +02:00

56 lines
1.9 KiB
TypeScript

import * as fs from 'fs';
import * as path from 'path';
import type { Page } from '@playwright/test';
/**
* Read a host-side file and write it into the page's Emscripten MEMFS.
*
* Mirrors the resource-write pattern already in tests/apps/kicad/pcbnew.html
* (FS.mkdirTree + FS.writeFile) and the DnD writer in tests/apps/kicad/wx.js.
* Emits a "[KICAD] Wrote …" console line so the per-test log records the
* injection.
*/
export async function injectFileIntoMemfs(
page: Page,
hostPath: string,
memfsPath: string,
): Promise<void> {
const bytes = fs.readFileSync(hostPath);
const memfsDir = memfsPath.replace(/\/[^/]+$/, '') || '/';
await page.evaluate(
({ memfsDir, memfsPath, dataBase64 }) => {
// Decode base64 → Uint8Array in the page so we don't have to ship
// megabytes through Playwright's JSON channel as a number array.
const binary = atob(dataBase64);
const data = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) {
data[i] = binary.charCodeAt(i);
}
// @ts-expect-error — Emscripten FS lives on window via Module
const FS = (window as any).FS;
FS.mkdirTree(memfsDir);
FS.writeFile(memfsPath, data);
console.log(`[KICAD] Wrote ${memfsPath} (${data.length} bytes)`);
},
{
memfsDir,
memfsPath,
dataBase64: bytes.toString('base64'),
},
);
}
/**
* Convenience: read a file from the kicad/ submodule and inject it.
*/
export async function injectFromSubmodule(
page: Page,
relativePath: string, // e.g. "kicad/demos/microwave/microwave.kicad_pcb"
memfsPath: string,
): Promise<void> {
const projectRoot = path.resolve(__dirname, '..', '..', '..');
const hostPath = path.join(projectRoot, relativePath);
await injectFileIntoMemfs(page, hostPath, memfsPath);
}