|
| 1 | +// fallow-ignore-file complexity |
| 2 | +import { readdirSync, readFileSync } from "node:fs"; |
| 3 | +import { relative, resolve } from "node:path"; |
| 4 | +import { fileURLToPath } from "node:url"; |
| 5 | + |
| 6 | +export const PRODUCER_ROOT = fileURLToPath(new URL("../", import.meta.url)); |
| 7 | + |
| 8 | +// These tests need host capabilities such as Chrome, ffmpeg, worker threads, |
| 9 | +// or local sockets. Keep the list explicit so a filename-only rename does not |
| 10 | +// make Git/fallow re-audit thousands of unchanged test lines as new code. |
| 11 | +const INTEGRATION_TEST_FILES = new Set([ |
| 12 | + "src/services/deterministicFonts-systemCapture.test.ts", |
| 13 | + "src/services/distributed/assemble.test.ts", |
| 14 | + "src/services/distributed/chunkBoundary.test.ts", |
| 15 | + "src/services/distributed/crossWorkerIdempotency.test.ts", |
| 16 | + "src/services/distributed/plan.test.ts", |
| 17 | + "src/services/distributed/planSizeCap.test.ts", |
| 18 | + "src/services/distributed/renderChunk.test.ts", |
| 19 | + "src/services/fileServer.test.ts", |
| 20 | + "src/services/healthWorker.test.ts", |
| 21 | + "src/utils/audioRegression.test.ts", |
| 22 | + "src/utils/streamDurationParity.test.ts", |
| 23 | +]); |
| 24 | + |
| 25 | +function collectTestFiles(directory, files = []) { |
| 26 | + for (const entry of readdirSync(directory, { withFileTypes: true })) { |
| 27 | + const entryPath = resolve(directory, entry.name); |
| 28 | + if (entry.isDirectory()) collectTestFiles(entryPath, files); |
| 29 | + else if (entry.isFile() && entry.name.endsWith(".test.ts")) files.push(entryPath); |
| 30 | + } |
| 31 | + return files; |
| 32 | +} |
| 33 | + |
| 34 | +export function classifyTestSource(filePath, source, integrationFiles = INTEGRATION_TEST_FILES) { |
| 35 | + const importsBun = /\bfrom\s+["']bun:test["']/.test(source); |
| 36 | + const importsVitest = /\bfrom\s+["']vitest["']/.test(source); |
| 37 | + if (importsBun === importsVitest) { |
| 38 | + const detail = importsBun ? "imports both bun:test and vitest" : "imports neither runner"; |
| 39 | + throw new Error(`${filePath}: ${detail}`); |
| 40 | + } |
| 41 | + return { |
| 42 | + file: filePath, |
| 43 | + runner: importsBun ? "bun" : "vitest", |
| 44 | + lane: integrationFiles.has(filePath) ? "integration" : "unit", |
| 45 | + }; |
| 46 | +} |
| 47 | + |
| 48 | +export function discoverProducerTests(producerRoot = PRODUCER_ROOT) { |
| 49 | + const srcDir = resolve(producerRoot, "src"); |
| 50 | + const files = collectTestFiles(srcDir).map((absolutePath) => ({ |
| 51 | + absolutePath, |
| 52 | + filePath: relative(producerRoot, absolutePath).replaceAll("\\", "/"), |
| 53 | + })); |
| 54 | + const discoveredFiles = new Set(files.map((test) => test.filePath)); |
| 55 | + const staleEntries = [...INTEGRATION_TEST_FILES].filter((file) => !discoveredFiles.has(file)); |
| 56 | + if (staleEntries.length > 0) { |
| 57 | + throw new Error(`Integration test manifest contains missing files: ${staleEntries.join(", ")}`); |
| 58 | + } |
| 59 | + return files |
| 60 | + .map(({ absolutePath, filePath }) => { |
| 61 | + return classifyTestSource(filePath, readFileSync(absolutePath, "utf8")); |
| 62 | + }) |
| 63 | + .sort((left, right) => left.file.localeCompare(right.file)); |
| 64 | +} |
| 65 | + |
| 66 | +export function summarizeTests(tests) { |
| 67 | + const summary = { |
| 68 | + total: tests.length, |
| 69 | + unit: { bun: 0, vitest: 0 }, |
| 70 | + integration: { bun: 0, vitest: 0 }, |
| 71 | + }; |
| 72 | + for (const test of tests) summary[test.lane][test.runner] += 1; |
| 73 | + return summary; |
| 74 | +} |
0 commit comments