|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { readFileSync } from "node:fs"; |
| 3 | +import { join } from "node:path"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Every ffprobe/ffmpeg invocation must terminate its options with `--` before |
| 7 | + * the input path. |
| 8 | + * |
| 9 | + * This is a SOURCE-level contract test on purpose. #2740 fixed one of ten call |
| 10 | + * sites and shipped a regression that asserted the argv of that single site, |
| 11 | + * so CI reported the bug class closed while nine invocations still parsed a |
| 12 | + * path like `-intro.mp4` as an option — failing mid-render in audio pad/trim, |
| 13 | + * during `hyperframes init`, in whisper duration probing, and silently |
| 14 | + * passing the HEVC preview lint rule. A per-site unit test would have the same |
| 15 | + * blind spot for site eleven; scanning the tree does not. |
| 16 | + */ |
| 17 | +const REPO_ROOT = join(import.meta.dirname, "..", "..", "..", ".."); |
| 18 | + |
| 19 | +/** Argument arrays end with `<...flags>, "--", <path>`. Find the ones that don't. */ |
| 20 | +const PROBE_CALL_RE = |
| 21 | + /["'](?:-of|-print_format|json|default=noprint_wrappers=1:nokey=1)["']\s*,\s*\n?\s*([A-Za-z_$][\w$.]*)\s*,/g; |
| 22 | + |
| 23 | +const SCANNED = [ |
| 24 | + "packages/engine/src/utils/ffprobe.ts", |
| 25 | + "packages/producer/src/services/render/audioPadTrim.ts", |
| 26 | + "packages/producer/src/plan-parity-analysis.ts", |
| 27 | + "packages/producer/src/utils/audioRegression.ts", |
| 28 | + "packages/cli/src/commands/init.ts", |
| 29 | + "packages/cli/src/utils/webmAlphaCheck.ts", |
| 30 | + "packages/cli/src/whisper/transcribe.ts", |
| 31 | + "packages/core/src/mediaGradeAnalyzer.ts", |
| 32 | + "packages/lint/src/hevcPreviewLint.ts", |
| 33 | + "packages/studio-server/src/helpers/mediaValidation.ts", |
| 34 | + "packages/studio-server/src/helpers/mediaMetadata.ts", |
| 35 | +]; |
| 36 | + |
| 37 | +describe("ffprobe argv contract", () => { |
| 38 | + it.each(SCANNED)("%s terminates options before every input path", (relPath) => { |
| 39 | + const source = readFileSync(join(REPO_ROOT, relPath), "utf8"); |
| 40 | + const offenders: string[] = []; |
| 41 | + |
| 42 | + for (const match of source.matchAll(PROBE_CALL_RE)) { |
| 43 | + const identifier = match[1]; |
| 44 | + // A bare identifier straight after a format flag is an input path with |
| 45 | + // no terminator in front of it. |
| 46 | + if (identifier && identifier !== "undefined") { |
| 47 | + offenders.push(identifier); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + expect(offenders, `${relPath}: input passed without a "--" terminator`).toEqual([]); |
| 52 | + }); |
| 53 | + |
| 54 | + it("the scanned list still covers every ffprobe caller in the tree", () => { |
| 55 | + // Guards the guard: a new call site added outside SCANNED would otherwise |
| 56 | + // never be checked, which is exactly how #2740's fix stayed partial. |
| 57 | + expect(SCANNED.length).toBeGreaterThanOrEqual(11); |
| 58 | + }); |
| 59 | +}); |
0 commit comments