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
This commit is contained in:
Viktor Vaczi 2026-07-07 10:50:24 +02:00
commit 4c3a4cacd4
102 changed files with 2867 additions and 3407 deletions

View file

@ -1,117 +1,91 @@
// wxTimer Tests - Timer functionality for KiCad animations, auto-save, periodic updates
// Uses element registry for semantic element identification
import { test, expect, tryLoadApp, waitForRegistry, clickByLabel, findByLabel } from './utils/fixtures';
// Uses element registry for semantic element identification.
//
// Determinism: no waitForTimeout. Readiness via waitForWxApp; each button click's effect
// is the console event it emits, so we poll for that event (deterministic) instead of
// sleeping. The timer *tick* is a genuine scheduled event — the app logs each tick, so we
// poll for the tick log rather than guessing a duration. Mid-run "ticking"/"running"
// screenshots (which asserted nothing and whose pixels are timing-dependent) are dropped;
// the static loaded/started/stopped/reset states use stableShot.
import { test, expect, waitForWxApp, findByLabel, clickByLabel } from './utils/fixtures';
import { stableShot } from './utils/element-tracker';
test.describe('wxTimer Tests', () => {
test('Timer test app loads successfully', async ({ page, testLogger }) => {
await page.goto('/standalone/timer/timer_test.html');
const loaded = await tryLoadApp(page);
await waitForWxApp(page);
await page.screenshot({ path: 'test-results/timer-01-loaded.png', fullPage: true });
await stableShot(page, 'timer-01-loaded.png', { fullPage: true });
expect(loaded, 'Timer app should load').toBe(true);
expect(testLogger.errors.filter(e => !e.includes('favicon'))).toHaveLength(0);
});
test('Slow timer can be started and stopped', async ({ page, testLogger }) => {
await page.goto('/standalone/timer/timer_test.html');
const loaded = await tryLoadApp(page);
expect(loaded, 'App should load').toBe(true);
await waitForWxApp(page);
await waitForRegistry(page);
// Click Start button for slow timer
// Note: There are two "Start" buttons, we need the first one in "Slow Timer" section
// Click Start for the slow timer (the first "Start" button, in the Slow Timer section).
const startButton = await findByLabel(page, 'Start', { exact: true });
if (startButton) {
await page.mouse.click(startButton.centerX, startButton.centerY);
}
await page.waitForTimeout(500);
expect(startButton, 'Slow-timer Start button should exist').not.toBeNull();
await page.mouse.click(startButton!.centerX, startButton!.centerY);
await page.screenshot({ path: 'test-results/timer-02-started.png', fullPage: true });
// The click's effect is the console event — poll for it (replaces waitForTimeout(500)).
await expect.poll(() => testLogger.consoleLogs.some(l => l.includes('Slow timer started')),
{ message: 'Should log slow timer started' }).toBe(true);
await stableShot(page, 'timer-02-started.png', { fullPage: true });
const hasStartEvent = testLogger.consoleLogs.some(log =>
log.includes('Slow timer started')
);
expect(hasStartEvent, 'Should log slow timer started').toBe(true);
// Wait for the timer to actually tick (the app logs each tick — deterministic,
// replaces waitForTimeout(1500)).
await expect.poll(() => testLogger.consoleLogs.some(l => l.includes('Slow timer tick')),
{ message: 'slow timer should tick', timeout: 8000 }).toBe(true);
// Wait for timer tick
await page.waitForTimeout(1500);
await page.screenshot({ path: 'test-results/timer-03-ticked.png', fullPage: true });
// Click Stop button for slow timer
// Click Stop for the slow timer.
const stopButton = await findByLabel(page, 'Stop', { exact: true });
if (stopButton) {
await page.mouse.click(stopButton.centerX, stopButton.centerY);
}
await page.waitForTimeout(500);
expect(stopButton, 'Slow-timer Stop button should exist').not.toBeNull();
await page.mouse.click(stopButton!.centerX, stopButton!.centerY);
await page.screenshot({ path: 'test-results/timer-04-stopped.png', fullPage: true });
const hasStopEvent = testLogger.consoleLogs.some(log =>
log.includes('Slow timer stopped')
);
expect(hasStopEvent, 'Should log slow timer stopped').toBe(true);
await expect.poll(() => testLogger.consoleLogs.some(l => l.includes('Slow timer stopped')),
{ message: 'Should log slow timer stopped' }).toBe(true);
await stableShot(page, 'timer-04-stopped.png', { fullPage: true });
});
test('Fast timer can be started and updates gauge', async ({ page, testLogger }) => {
await page.goto('/standalone/timer/timer_test.html');
const loaded = await tryLoadApp(page);
expect(loaded, 'App should load').toBe(true);
await waitForWxApp(page);
await waitForRegistry(page);
// Click "Start Fast" button
await clickByLabel(page, 'Start Fast');
await page.waitForTimeout(500);
await expect.poll(() => testLogger.consoleLogs.some(l => l.includes('Fast timer started')),
{ message: 'Should log fast timer started' }).toBe(true);
await page.screenshot({ path: 'test-results/timer-05-fast-started.png', fullPage: true });
// Let the fast timer tick (deterministic). No screenshot while it runs: the fast
// timer continuously animates the gauge, so no two frames are ever identical and
// stableShot can't stabilize — and the original mid-run shots asserted nothing.
await expect.poll(() => testLogger.consoleLogs.some(l => l.includes('Fast timer tick')),
{ message: 'fast timer should tick', timeout: 8000 }).toBe(true);
const hasFastStartEvent = testLogger.consoleLogs.some(log =>
log.includes('Fast timer started')
);
expect(hasFastStartEvent, 'Should log fast timer started').toBe(true);
// Wait for multiple fast ticks
await page.waitForTimeout(1000);
await page.screenshot({ path: 'test-results/timer-06-fast-running.png', fullPage: true });
// Click "Stop Fast" button
await clickByLabel(page, 'Stop Fast');
await page.waitForTimeout(500);
await page.screenshot({ path: 'test-results/timer-07-fast-stopped.png', fullPage: true });
const hasFastStopEvent = testLogger.consoleLogs.some(log =>
log.includes('Fast timer stopped')
);
expect(hasFastStopEvent, 'Should log fast timer stopped').toBe(true);
await expect.poll(() => testLogger.consoleLogs.some(l => l.includes('Fast timer stopped')),
{ message: 'Should log fast timer stopped' }).toBe(true);
// Screenshot the stopped (now-static) state.
await stableShot(page, 'timer-07-fast-stopped.png', { fullPage: true });
});
test('Reset counters button works', async ({ page, testLogger }) => {
await page.goto('/standalone/timer/timer_test.html');
const loaded = await tryLoadApp(page);
expect(loaded, 'App should load').toBe(true);
await waitForWxApp(page);
await waitForRegistry(page);
// Start slow timer briefly
// Start the slow timer and let it run briefly (poll for a tick) so there are counters
// to reset — replaces waitForTimeout(1500).
const startButton = await findByLabel(page, 'Start', { exact: true });
if (startButton) {
await page.mouse.click(startButton.centerX, startButton.centerY);
}
await page.waitForTimeout(1500);
expect(startButton, 'Slow-timer Start button should exist').not.toBeNull();
await page.mouse.click(startButton!.centerX, startButton!.centerY);
await expect.poll(() => testLogger.consoleLogs.some(l => l.includes('Slow timer tick')),
{ message: 'slow timer should tick before reset', timeout: 8000 }).toBe(true);
// Click "Reset All Counters" button
await clickByLabel(page, 'Reset All Counters');
await page.waitForTimeout(500);
await page.screenshot({ path: 'test-results/timer-08-reset.png', fullPage: true });
const hasResetEvent = testLogger.consoleLogs.some(log =>
log.includes('Counters reset')
);
expect(hasResetEvent, 'Should log counters reset').toBe(true);
await expect.poll(() => testLogger.consoleLogs.some(l => l.includes('Counters reset')),
{ message: 'Should log counters reset' }).toBe(true);
await stableShot(page, 'timer-08-reset.png', { fullPage: true });
});
});