Repository navigation
Expand file tree
/
Copy pathdesktop-e2e.ts
More file actions
61 lines (58 loc) · 2.56 KB
/
Copy pathdesktop-e2e.ts
File metadata and controls
61 lines (58 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { spawnSync } from 'node:child_process'
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { partitionFiles } from '@scripts/duration-shards'
import { createLogger } from '@sim/logger'
import { escapeRegExp } from '@sim/utils/string'
interface DiscoveryReport {
config: { rootDir: string }
suites: { file: string }[]
errors: unknown[]
}
const logger = createLogger('DesktopE2E')
const match = /^(\d+)\/(\d+)$/.exec(process.argv[2] ?? '')
if (!match) throw new Error('Usage: bun run ../../scripts/desktop-e2e.ts <shard>/<total>')
const index = Number(match[1])
const count = Number(match[2])
if (!Number.isSafeInteger(index) || !Number.isSafeInteger(count) || index < 1 || index > count) {
throw new Error('Invalid desktop shard index or count')
}
mkdirSync('test-results', { recursive: true })
const discoveryPath = resolve('test-results/desktop-discovery.json')
const discovery = spawnSync('bunx', ['playwright', 'test', '--list', '--reporter=json'], {
stdio: 'inherit',
env: { ...process.env, PLAYWRIGHT_JSON_OUTPUT_FILE: discoveryPath },
})
if (discovery.error) throw discovery.error
if (discovery.status !== 0) process.exit(discovery.status ?? 1)
const discoveryJson = readFileSync(discoveryPath, 'utf8')
const report = JSON.parse(discoveryJson) as DiscoveryReport
if (report.errors.length > 0) throw new Error('Playwright discovery reported errors')
// A test factory can declare tests in a helper; the outer suite owns the discoverable test file.
const files = new Set(report.suites.map((suite) => suite.file))
if (count > files.size) throw new Error('More desktop shards than discovered test files')
const durations = JSON.parse(
readFileSync(new URL('../apps/desktop/e2e-durations.json', import.meta.url), 'utf8')
) as Record<string, number>
const shards = partitionFiles([...files], durations, count)
const selected = shards[index - 1]
if (selected.length === 0) throw new Error('Desktop shard selected no test files')
logger.info(`Running desktop shard ${index}/${count}: ${selected.join(', ')}`)
const run = spawnSync(
'bunx',
[
'playwright',
'test',
...selected.map((file) => `^${escapeRegExp(resolve(report.config.rootDir, file))}$`),
'--reporter=line,json',
],
{ stdio: 'inherit', env: process.env }
)
mkdirSync('test-results', { recursive: true })
writeFileSync(discoveryPath, discoveryJson)
writeFileSync(
'test-results/desktop-shard-plan.json',
`${JSON.stringify({ index, count, discoveredFiles: files.size, shards }, null, 2)}\n`
)
if (run.error) throw run.error
process.exit(run.status ?? 1)