import { expect } from '@playwright/test'; import { createHash } from 'crypto'; import { existsSync, mkdirSync, writeFileSync, readFileSync } from 'fs'; import { join, dirname } from 'path'; import { fileURLToPath } from 'url'; import { test, huds, HUD_COUNT, renderHud } from './fixture'; const SNAPSHOT_DIR = join(dirname(fileURLToPath(import.meta.url)), 'snapshots'); const BATCH_SIZE = Math.ceil(HUD_COUNT / 8); function xmlHash(xml: string): string { return createHash('sha256').update(xml).digest('hex').slice(0, 16); } for (let batch = 0; batch < 8; batch++) { const start = batch * BATCH_SIZE; const end = Math.min(start + BATCH_SIZE, HUD_COUNT); test(`HUDs #${start}-${end - 1}`, async ({ page, renderCache }) => { mkdirSync(SNAPSHOT_DIR, { recursive: true }); const compileFailures: { index: number; error: string }[] = []; const mismatches: { index: number; hash: string }[] = []; let created = 0; for (let i = start; i < end; i++) { const { error } = await renderHud(page, renderCache, i); if (error) { compileFailures.push({ index: i, error }); continue; } const hash = xmlHash(huds[i]); const snapshotPath = join(SNAPSHOT_DIR, `hud-${hash}.png`); const dataUrl = await page.evaluate(() => (window as any).harness.getCanvasDataURL() ); const actual = Buffer.from(dataUrl.split(',')[1], 'base64'); if (!existsSync(snapshotPath)) { writeFileSync(snapshotPath, actual); created++; } else { const expected = readFileSync(snapshotPath); if (!actual.equals(expected)) { writeFileSync(join(SNAPSHOT_DIR, `hud-${hash}.actual.png`), actual); mismatches.push({ index: i, hash }); } } } if (created > 0) console.log(`Created ${created} new snapshots`); if (compileFailures.length > 0) { const summary = compileFailures.slice(0, 10).map(f => ` HUD #${f.index}: ${f.error}`).join('\n'); const extra = compileFailures.length > 10 ? `\n ...and ${compileFailures.length - 10} more` : ''; expect.soft(compileFailures, `${compileFailures.length} HUDs failed:\n${summary}${extra}`).toEqual([]); } if (mismatches.length > 0) { const summary = mismatches.map(m => ` HUD #${m.index}: hud-${m.hash}.png`).join('\n'); expect.soft(mismatches, `${mismatches.length} snapshots differ:\n${summary}`).toEqual([]); } }); }