|
| 1 | +import { describe, expect, test } from 'vitest'; |
| 2 | +import { readdirSync, readFileSync } from 'node:fs'; |
| 3 | +import { join } from 'node:path'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
| 5 | +import { AppError } from '../../src/kernel/errors.ts'; |
| 6 | +import { parseReplayScriptDetailed, readReplayScriptMetadata } from '../../src/replay/script.ts'; |
| 7 | +import { |
| 8 | + REPLAY_COMPAT_CORPUS, |
| 9 | + type ReplayCompatCoverage, |
| 10 | + type ReplayCompatEntry, |
| 11 | +} from './manifest.ts'; |
| 12 | + |
| 13 | +const CORPUS_DIR = fileURLToPath(new URL('.', import.meta.url)); |
| 14 | + |
| 15 | +/** |
| 16 | + * Every historical surface the corpus exists to hold still. A coverage tag with |
| 17 | + * no entry means the corpus stopped covering a compat surface #1417 named. |
| 18 | + */ |
| 19 | +const REQUIRED_COVERAGE: ReplayCompatCoverage[] = [ |
| 20 | + 'context-header', |
| 21 | + 'env-vars', |
| 22 | + 'quoting', |
| 23 | + 'retired-gesture', |
| 24 | + 'target-annotation', |
| 25 | + 'wait-landmark', |
| 26 | +]; |
| 27 | + |
| 28 | +function readCorpusScript(entry: ReplayCompatEntry): string { |
| 29 | + return readFileSync(join(CORPUS_DIR, entry.file), 'utf8'); |
| 30 | +} |
| 31 | + |
| 32 | +/** The whole parse surface a `.ad` file meets before replay executes anything. */ |
| 33 | +function parseCorpusScript(script: string): void { |
| 34 | + readReplayScriptMetadata(script); |
| 35 | + parseReplayScriptDetailed(script); |
| 36 | +} |
| 37 | + |
| 38 | +function listCorpusScriptFiles(): string[] { |
| 39 | + const areas = readdirSync(join(CORPUS_DIR, 'scripts'), { withFileTypes: true }); |
| 40 | + return areas.flatMap((area) => |
| 41 | + readdirSync(join(CORPUS_DIR, 'scripts', area.name)) |
| 42 | + .filter((name) => name.endsWith('.ad')) |
| 43 | + .map((name) => `scripts/${area.name}/${name}`), |
| 44 | + ); |
| 45 | +} |
| 46 | + |
| 47 | +describe('replay-compat corpus', () => { |
| 48 | + test.each(REPLAY_COMPAT_CORPUS.map((entry) => [entry.id, entry] as const))('%s', (_id, entry) => { |
| 49 | + const script = readCorpusScript(entry); |
| 50 | + if (entry.verdict.kind === 'parses') { |
| 51 | + expect(() => { |
| 52 | + parseCorpusScript(script); |
| 53 | + }).not.toThrow(); |
| 54 | + return; |
| 55 | + } |
| 56 | + let thrown: unknown; |
| 57 | + try { |
| 58 | + parseCorpusScript(script); |
| 59 | + } catch (error) { |
| 60 | + thrown = error; |
| 61 | + } |
| 62 | + expect(thrown, `expected ${entry.id} to fail parsing`).toBeInstanceOf(AppError); |
| 63 | + const error = thrown as AppError; |
| 64 | + expect(error.code).toBe(entry.verdict.code); |
| 65 | + expect(error.message).toContain(entry.verdict.hint); |
| 66 | + }); |
| 67 | + |
| 68 | + test('every corpus script is claimed by exactly one manifest entry', () => { |
| 69 | + const claimed = REPLAY_COMPAT_CORPUS.map((entry) => entry.file); |
| 70 | + expect([...claimed].sort()).toEqual(listCorpusScriptFiles().sort()); |
| 71 | + expect(new Set(claimed).size).toBe(claimed.length); |
| 72 | + }); |
| 73 | + |
| 74 | + test('entry ids are unique and name the producing released version', () => { |
| 75 | + const ids = REPLAY_COMPAT_CORPUS.map((entry) => entry.id); |
| 76 | + expect(new Set(ids).size).toBe(ids.length); |
| 77 | + for (const entry of REPLAY_COMPAT_CORPUS) { |
| 78 | + expect(entry.recordedBy, entry.id).toMatch(/^v\d+\.\d+\.\d+$/); |
| 79 | + expect(entry.source, entry.id).toContain(entry.recordedBy); |
| 80 | + expect(entry.file, entry.id).toContain(`.${entry.recordedBy}.ad`); |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + test('the corpus covers every compat surface it is required to freeze', () => { |
| 85 | + const covered = new Set(REPLAY_COMPAT_CORPUS.flatMap((entry) => entry.covers)); |
| 86 | + expect([...covered].sort()).toEqual([...REQUIRED_COVERAGE].sort()); |
| 87 | + }); |
| 88 | +}); |
0 commit comments