pcbjam/tests/kicad/gerbview-print.spec.ts
Viktor Vaczi 13551f4f22 feat(tests): screenshot regression + Discord review, perf-tracked
New tooling in tests/tools/screenshots/ (TypeScript via tsx):
- compare.ts: one pixelmatch engine (AA-excluded), connected-component
  "where to look" boxes, old|new+boxes|heatmap triptych, per-engine floors.
- promote.ts: churn-free updater — overwrite a baseline only when decoded
  pixels differ beyond the floor, copying CI bytes verbatim (no re-encode
  churn); pulls a CI run via `gh run download` or a local --from dir.
- post-discord.ts: always-on CI-on-main report (SHA + e2e status + the
  track-only runtime-perf table), then screenshot triptychs, batched +
  size-capped + flood-collapsed + 429-aware.
- perf-report.ts: perf table with Δ vs the previous main run (via gh).
- changelog.ts: no-build git-history baseline differ (Discord trigger B).
- noise.ts / gen-manifest.ts: calibration + manifest generation.

CI wiring:
- wasm-build.yml: post-test step runs the gate + report on the already-
  produced test-results (no extra build); report-only (continue-on-error),
  posts only on push to main, inert without DISCORD_WEBHOOK_URL.
- ci-ubicloud.yml: secrets: inherit (pass the webhook through).
- screenshot-changelog.yml: ~30s no-build changelog on baseline changes.

screenshot-manifest.json: canonical 354-name set + best-effort engine tags
(313 chromium-swiftshader / 41 firefox-llvmpipe).

Normalize scale:'device'->'css' across 18 spec files (no-op at CI DSF=1)
so committed baselines are uniformly css-scaled.

Design: CI's Linux render is the single source of truth; no pinned
container (accept rare env drift -> re-promote); dev commits via promote.
Replaces the byte-cmp compare-screenshots.sh + file-size-proxy
update-baseline-screenshots.sh (kept for now until the first re-baseline).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-01 18:17:20 +02:00

146 lines
6.4 KiB
TypeScript

import type { Page } from '@playwright/test';
import { test, expect } from './fixtures';
import {
clickByLabel,
clickByTooltip,
clickMenuBarItem,
clickMenuItem,
findByLabel,
} from '../e2e/utils/element-tracker';
/**
* Gerber Viewer Print dialog — regression for emergence-engineering/pcbjam#14.
*
* In the browser, KiCad's generic Print dialog used to show a "Print Preview"
* button (wxID_APPLY) that the DOM/WASM port can't honor: the in-app
* wxPreviewFrame is opened window-modal from inside the already-modal Print
* dialog, which the port can't render over an active modal — clicking it did
* nothing and wedged the dialog so it would not reopen.
*
* The fix hides that button in the browser build (the same way native KiCad
* already hides it on macOS/GTK), since the browser provides its own print
* preview. This test loads the tiny_tapeout demo board, opens the Print dialog,
* and asserts (a) there is no visible "Print Preview" button and (b) the dialog
* can be closed and reopened — i.e. the modal state is no longer wedged.
*/
function hasAbort(testLogger: { consoleLogs: string[]; errors: string[] }): boolean {
return [...testLogger.consoleLogs, ...testLogger.errors].some(line => line.includes('Aborted('));
}
// The Print dialog is detected by its unique OK button, labelled exactly
// "Print" (wxID_OK). Toolbar/menu print entries are rendered tools / menu items
// (a separate registry) or carry the "..." ellipsis, so they don't match here —
// a visible exact-"Print" element means the modal Print dialog is open.
async function printDialogIsOpen(page: Page): Promise<boolean> {
return (await findByLabel(page, 'Print', { visible: true, exact: true })) !== null;
}
async function waitForPrintDialog(page: Page, open: boolean, timeout = 8000): Promise<void> {
await page.waitForFunction(
(wantOpen: boolean) => {
const registry = window.wxElementRegistry;
if (!registry) return false;
const isOpen = registry.findByLabel('Print', { visible: true, exact: true }).length > 0;
return isOpen === wantOpen;
},
open,
{ timeout },
);
}
async function waitForGerbview(page: Page): Promise<void> {
await expect(page.locator('#canvas')).toBeVisible({ timeout: 90000 });
await page.waitForFunction(() => !!window.wxElementRegistry, null, { timeout: 90000 });
await page.waitForFunction(() => {
const registry = window.wxElementRegistry;
return !!registry && registry.findAll({}).length > 0;
}, null, { timeout: 90000 });
// The config is seeded to suppress the first-run wizard, but dismiss it
// defensively in case it still appears (harmless when there is none).
for (let i = 0; i < 6; i++) {
if (!(await clickByLabel(page, 'Next >'))) {
await clickByLabel(page, 'Finish');
break;
}
await page.waitForTimeout(300);
}
// Let the demo board load and the viewer chrome settle. The Print Preview
// button is hidden unconditionally in the WASM build, so the assertions
// don't actually depend on the layers being fully parsed — this is mostly
// for a faithful screenshot.
await page.waitForTimeout(4000);
}
// Open the Print dialog. In gerbview, Print is a top-toolbar tool (not a File
// menu item), so click that; fall back to a File -> Print… menu path in case the
// UI changes ("..." vs unicode "…").
async function openPrintDialog(page: Page): Promise<void> {
if (!(await clickByTooltip(page, 'Print'))) {
await clickMenuBarItem(page, 'File');
await page.waitForTimeout(500);
let opened = await clickMenuItem(page, 'Print...');
if (!opened) opened = await clickMenuItem(page, 'Print…');
if (!opened) await clickMenuItem(page, 'Print');
}
await waitForPrintDialog(page, true);
}
// Close the modal Print dialog. Escape cancels a wxDialog; fall back to the
// Close button if needed. Poll for the dialog to actually disappear.
async function closePrintDialog(page: Page): Promise<void> {
for (const how of ['escape', 'close', 'escape'] as const) {
if (how === 'escape') {
await page.keyboard.press('Escape').catch(() => {});
} else {
await clickByLabel(page, 'Close', { visible: true, exact: true });
}
try {
await waitForPrintDialog(page, false, 3000);
return;
} catch {
// try the next method
}
}
// Final assertion happens in the test; surface the still-open state there.
await waitForPrintDialog(page, false, 1000);
}
test.describe('gerbview Print dialog (WASM)', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/kicad/gerbview-print.html');
});
test('no Print Preview button, and the dialog reopens (no wedge)', async ({ page, testLogger }) => {
await waitForGerbview(page);
await page.screenshot({ path: 'test-results/gerbview-print-00-loaded.png', scale: 'css' });
// --- Open the Print dialog ---
await openPrintDialog(page);
expect(await printDialogIsOpen(page), 'Print dialog should be open').toBe(true);
await page.screenshot({ path: 'test-results/gerbview-print-01-dialog.png', scale: 'css' });
// --- The broken "Print Preview" button must be gone in the browser ---
const preview = await findByLabel(page, 'Print Preview', { visible: true, exact: true });
expect(preview, 'Print Preview button must be hidden in the browser build').toBeNull();
// Sanity: this really is the Print dialog (Close + Page Setup present too).
expect(await findByLabel(page, 'Close', { visible: true, exact: true }),
'Close button present').not.toBeNull();
// --- Close it ---
await closePrintDialog(page);
expect(await printDialogIsOpen(page), 'dialog should close').toBe(false);
// --- Regression: reopening Print must work (it used to wedge) ---
await openPrintDialog(page);
expect(await printDialogIsOpen(page), 'Print dialog should reopen (no wedge)').toBe(true);
await page.waitForTimeout(2500); // let the reopened dialog finish painting before capture
await page.screenshot({ path: 'test-results/gerbview-print-02-reopened.png', scale: 'css' });
expect(hasAbort(testLogger), 'no WASM abort during the flow').toBe(false);
});
});