pcbjam/tests/web/tool-switch.spec.ts
Viktor Vaczi 4c3a4cacd4 test(determinism): deterministic waits + stableShot screenshots; drop blind sleeps/ifs/retries
Make the Playwright e2e + kicad suites deterministic so screenshot flake stops
tracing to timing races.

- Blind page.waitForTimeout -> condition waits (expect.poll, web-first
  assertions, waitUntil) + readiness helpers (waitForWxApp, waitForCanvasApp).
  Remaining sleeps are documented interaction dwells (annotated).
- Defensive "if element exists" branches -> loud asserts; label-fallback chains
  -> normalized clickMenuItemByText. First-run wizard for/if loops removed by
  seeding calculator/gerbview/pcbnew HTMLs.
- Screenshots: new stableShot(page, name) settles the render in-page (canvas
  hash over rAF) then writes a raw PNG to test-results/ for the existing offline
  gate (tools/screenshots vs baseline-screenshots). Replaces toHaveScreenshot,
  which did inline compare + its own baselines and had decoupled the specs from
  the real gate. scale:'css' pinned.
- retries: 0 in both configs.
- Guard: tests/tools/lint-determinism.ts (npm run lint:determinism) bans blind
  sleeps / toHaveScreenshot / inline retries / swallowed catches in specs;
  documented exceptions carry a marker. Rules in tests/TESTING.md.

Assertions, coverage, and renders unchanged (semantic-equivalence reviewed;
captures pixel-identical modulo inherent timer/timestamp/3d-raytrace variance).
Both suites green at retries:0 (e2e 340, kicad 92); ~35-61% faster.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BVX1pHMvRPYHdp6ZfEawrk
2026-07-07 10:50:24 +02:00

79 lines
2.6 KiB
TypeScript

import { test, expect, type Page } from '@playwright/test';
import { clickMenuBarItem, clickMenuItemByText, stableShot } from '../e2e/utils/element-tracker';
/**
* Tool-switch e2e: eeschema Tools → "Switch to PCB Editor" (and the reverse)
* must navigate the browser to the other tool's URL.
*
* Native KiCad spawns a process for this via ExecuteFile (common/gestfich.cpp);
* the WASM build delegates to window.kicadWebOpenTool (WasmTool.tsx), which
* maps the MEMFS file path to the project-relative file and calls
* location.assign(/p/demo/<tool>/<file>). Each direction is a full page
* navigation followed by a fresh wasm boot — hence the generous timeouts.
*/
async function waitForToolReady(page: Page, titleRe: RegExp): Promise<void> {
await expect(page.locator('#canvas')).toBeVisible({ timeout: 120000 });
await expect
.poll(() => page.title(), {
message: `editor never reached title ${titleRe}`,
timeout: 120000,
intervals: [1000],
})
.toMatch(titleRe);
// The menu helpers drive the rendered-element registry.
await page.waitForFunction(
() =>
!!(window as unknown as { wxElementRegistry?: { findAllRendered?: unknown } })
.wxElementRegistry,
null,
{ timeout: 30000 }
);
}
async function switchTool(
page: Page,
menuLabel: string,
expectedUrl: RegExp,
expectedTitle: RegExp
): Promise<void> {
expect(await clickMenuBarItem(page, 'Tools'), 'Tools menubar item clickable').toBe(true);
await clickMenuItemByText(page, menuLabel);
await page.waitForURL(expectedUrl, { timeout: 30000 });
await waitForToolReady(page, expectedTitle);
}
test.describe('web app — tool switching', () => {
test('eeschema → Switch to PCB Editor navigates to pcbnew', async ({ page }) => {
test.setTimeout(420000); // two full wasm boots
await page.goto('/p/demo/eeschema/demo.kicad_sch');
await waitForToolReady(page, /demo — Schematic Editor/i);
await switchTool(
page,
'Switch to PCB Editor',
/\/p\/demo\/pcbnew\/demo\.kicad_pcb/,
/demo — PCB Editor/i
);
await stableShot(page, 'web-switch-sch-to-pcb.png');
});
test('pcbnew → Switch to Schematic Editor navigates to eeschema', async ({ page }) => {
test.setTimeout(420000);
await page.goto('/p/demo/pcbnew/demo.kicad_pcb');
await waitForToolReady(page, /demo — PCB Editor/i);
await switchTool(
page,
'Switch to Schematic Editor',
/\/p\/demo\/eeschema\/demo\.kicad_sch/,
/demo — Schematic Editor/i
);
await stableShot(page, 'web-switch-pcb-to-sch.png');
});
});