|
| 1 | +import { spawnSync } from 'node:child_process' |
| 2 | +import { mkdirSync, readFileSync, writeFileSync } from 'node:fs' |
| 3 | +import { resolve } from 'node:path' |
| 4 | +import { createLogger } from '@sim/logger' |
| 5 | +import { escapeRegExp } from '@sim/utils/string' |
| 6 | +import { partitionFiles } from './duration-shards' |
| 7 | + |
| 8 | +interface Suite { |
| 9 | + specs?: { file: string }[] |
| 10 | + suites?: Suite[] |
| 11 | +} |
| 12 | + |
| 13 | +interface DiscoveryReport { |
| 14 | + config: { rootDir: string } |
| 15 | + suites: Suite[] |
| 16 | + errors: unknown[] |
| 17 | +} |
| 18 | + |
| 19 | +const logger = createLogger('DesktopE2E') |
| 20 | +const match = /^(\d+)\/(\d+)$/.exec(process.argv[2] ?? '') |
| 21 | +if (!match) throw new Error('Usage: bun run ../../scripts/desktop-e2e.ts <shard>/<total>') |
| 22 | +const index = Number(match[1]) |
| 23 | +const count = Number(match[2]) |
| 24 | +if (!Number.isSafeInteger(index) || !Number.isSafeInteger(count) || index < 1 || index > count) { |
| 25 | + throw new Error('Invalid desktop shard index or count') |
| 26 | +} |
| 27 | + |
| 28 | +mkdirSync('test-results', { recursive: true }) |
| 29 | +const discoveryPath = resolve('test-results/desktop-discovery.json') |
| 30 | +const discovery = spawnSync('bunx', ['playwright', 'test', '--list', '--reporter=json'], { |
| 31 | + stdio: 'inherit', |
| 32 | + env: { ...process.env, PLAYWRIGHT_JSON_OUTPUT_FILE: discoveryPath }, |
| 33 | +}) |
| 34 | +if (discovery.error) throw discovery.error |
| 35 | +if (discovery.status !== 0) process.exit(discovery.status ?? 1) |
| 36 | +const discoveryJson = readFileSync(discoveryPath, 'utf8') |
| 37 | +const report = JSON.parse(discoveryJson) as DiscoveryReport |
| 38 | +if (report.errors.length > 0) throw new Error('Playwright discovery reported errors') |
| 39 | +const files = new Set<string>() |
| 40 | +function visit(suite: Suite): void { |
| 41 | + for (const spec of suite.specs ?? []) files.add(spec.file) |
| 42 | + for (const child of suite.suites ?? []) visit(child) |
| 43 | +} |
| 44 | +for (const suite of report.suites) visit(suite) |
| 45 | +if (count > files.size) throw new Error('More desktop shards than discovered test files') |
| 46 | +const durations = JSON.parse( |
| 47 | + readFileSync(new URL('../apps/desktop/e2e-durations.json', import.meta.url), 'utf8') |
| 48 | +) as Record<string, number> |
| 49 | +const shards = partitionFiles([...files], durations, count) |
| 50 | +const selected = shards[index - 1] |
| 51 | +if (selected.length === 0) throw new Error('Desktop shard selected no test files') |
| 52 | +logger.info(`Running desktop shard ${index}/${count}: ${selected.join(', ')}`) |
| 53 | +const run = spawnSync( |
| 54 | + 'bunx', |
| 55 | + [ |
| 56 | + 'playwright', |
| 57 | + 'test', |
| 58 | + ...selected.map((file) => `^${escapeRegExp(resolve(report.config.rootDir, file))}$`), |
| 59 | + '--reporter=line,json', |
| 60 | + ], |
| 61 | + { stdio: 'inherit', env: process.env } |
| 62 | +) |
| 63 | +mkdirSync('test-results', { recursive: true }) |
| 64 | +writeFileSync(discoveryPath, discoveryJson) |
| 65 | +writeFileSync( |
| 66 | + 'test-results/desktop-shard-plan.json', |
| 67 | + `${JSON.stringify({ index, count, discoveredFiles: files.size, shards }, null, 2)}\n` |
| 68 | +) |
| 69 | +if (run.error) throw run.error |
| 70 | +process.exit(run.status ?? 1) |
0 commit comments