|
| 1 | +/** |
| 2 | + * Unit tests for the distributed format banlist. |
| 3 | + * |
| 4 | + * Two formats `plan()` refuses up front: |
| 5 | + * - webm — VP9 + matroska concat-copy is fragile across libvpx-vp9 builds. |
| 6 | + * - mp4 + HDR (`hdrMode === "force-hdr"`) — chunked HDR pre-extract + |
| 7 | + * HDR signaling re-apply on the assembled file is not implemented. |
| 8 | + * |
| 9 | + * The banlist must trip BEFORE any other work runs (file server, browser, |
| 10 | + * ffprobe) — otherwise a banned config can leak a partial planDir on disk. |
| 11 | + * Each case asserts `existsSync(planDir)` is `false` after the throw to |
| 12 | + * pin the early-exit contract. |
| 13 | + */ |
| 14 | + |
| 15 | +import { afterAll, beforeAll, describe, expect, it } from "bun:test"; |
| 16 | +import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 17 | +import { tmpdir } from "node:os"; |
| 18 | +import { join } from "node:path"; |
| 19 | +import { |
| 20 | + FORMAT_NOT_SUPPORTED_IN_DISTRIBUTED, |
| 21 | + FormatNotSupportedInDistributedError, |
| 22 | + plan, |
| 23 | + rejectUnsupportedDistributedFormat, |
| 24 | + type DistributedRenderConfig, |
| 25 | +} from "./plan.js"; |
| 26 | + |
| 27 | +const FIXTURE_HTML = `<!doctype html> |
| 28 | +<html><body> |
| 29 | + <div data-composition-id="root" data-width="320" data-height="240" data-duration="1">hi</div> |
| 30 | +</body></html>`; |
| 31 | + |
| 32 | +let runRoot: string; |
| 33 | +let projectDir: string; |
| 34 | + |
| 35 | +beforeAll(() => { |
| 36 | + runRoot = mkdtempSync(join(tmpdir(), "hf-plan-format-ban-")); |
| 37 | + projectDir = join(runRoot, "project"); |
| 38 | + mkdirSync(projectDir, { recursive: true }); |
| 39 | + writeFileSync(join(projectDir, "index.html"), FIXTURE_HTML, "utf-8"); |
| 40 | +}); |
| 41 | + |
| 42 | +afterAll(() => { |
| 43 | + rmSync(runRoot, { recursive: true, force: true }); |
| 44 | +}); |
| 45 | + |
| 46 | +describe("rejectUnsupportedDistributedFormat (pure)", () => { |
| 47 | + it("accepts the v1-supported formats (mp4 / mov / png-sequence)", () => { |
| 48 | + expect(() => rejectUnsupportedDistributedFormat({ format: "mp4" })).not.toThrow(); |
| 49 | + expect(() => rejectUnsupportedDistributedFormat({ format: "mov" })).not.toThrow(); |
| 50 | + expect(() => rejectUnsupportedDistributedFormat({ format: "png-sequence" })).not.toThrow(); |
| 51 | + expect(() => |
| 52 | + rejectUnsupportedDistributedFormat({ format: "mp4", hdrMode: "auto" }), |
| 53 | + ).not.toThrow(); |
| 54 | + expect(() => |
| 55 | + rejectUnsupportedDistributedFormat({ format: "mp4", hdrMode: "force-sdr" }), |
| 56 | + ).not.toThrow(); |
| 57 | + }); |
| 58 | + |
| 59 | + it("rejects webm with FORMAT_NOT_SUPPORTED_IN_DISTRIBUTED", () => { |
| 60 | + let caught: unknown; |
| 61 | + try { |
| 62 | + // Cast forces the runtime check even though the type narrows webm out. |
| 63 | + rejectUnsupportedDistributedFormat({ |
| 64 | + format: "webm" as DistributedRenderConfig["format"], |
| 65 | + }); |
| 66 | + } catch (err) { |
| 67 | + caught = err; |
| 68 | + } |
| 69 | + expect(caught).toBeInstanceOf(FormatNotSupportedInDistributedError); |
| 70 | + expect((caught as FormatNotSupportedInDistributedError).code).toBe( |
| 71 | + FORMAT_NOT_SUPPORTED_IN_DISTRIBUTED, |
| 72 | + ); |
| 73 | + expect((caught as FormatNotSupportedInDistributedError).format).toBe("webm"); |
| 74 | + expect((caught as Error).message).toMatch(/webm/); |
| 75 | + expect((caught as Error).message).toMatch(/in-process|executeRenderJob/); |
| 76 | + }); |
| 77 | + |
| 78 | + it('rejects HDR mp4 (`hdrMode === "force-hdr"`)', () => { |
| 79 | + let caught: unknown; |
| 80 | + try { |
| 81 | + rejectUnsupportedDistributedFormat({ |
| 82 | + format: "mp4", |
| 83 | + hdrMode: "force-hdr" as DistributedRenderConfig["hdrMode"], |
| 84 | + }); |
| 85 | + } catch (err) { |
| 86 | + caught = err; |
| 87 | + } |
| 88 | + expect(caught).toBeInstanceOf(FormatNotSupportedInDistributedError); |
| 89 | + expect((caught as FormatNotSupportedInDistributedError).code).toBe( |
| 90 | + FORMAT_NOT_SUPPORTED_IN_DISTRIBUTED, |
| 91 | + ); |
| 92 | + expect((caught as FormatNotSupportedInDistributedError).format).toBe("mp4-hdr"); |
| 93 | + expect((caught as Error).message).toMatch(/HDR/); |
| 94 | + }); |
| 95 | +}); |
| 96 | + |
| 97 | +describe("plan() banlist (end-to-end)", () => { |
| 98 | + it("throws on webm and does not create the planDir", async () => { |
| 99 | + const planDir = join(runRoot, "plandir-webm-bans"); |
| 100 | + // Don't pre-create planDir — plan() shouldn't create it on the throw path. |
| 101 | + let caught: unknown; |
| 102 | + try { |
| 103 | + await plan( |
| 104 | + projectDir, |
| 105 | + { |
| 106 | + format: "webm" as DistributedRenderConfig["format"], |
| 107 | + fps: 30, |
| 108 | + width: 320, |
| 109 | + height: 240, |
| 110 | + }, |
| 111 | + planDir, |
| 112 | + ); |
| 113 | + } catch (err) { |
| 114 | + caught = err; |
| 115 | + } |
| 116 | + expect(caught).toBeInstanceOf(FormatNotSupportedInDistributedError); |
| 117 | + expect((caught as FormatNotSupportedInDistributedError).format).toBe("webm"); |
| 118 | + expect(existsSync(planDir)).toBe(false); |
| 119 | + }); |
| 120 | + |
| 121 | + it("throws on HDR mp4 and does not create the planDir", async () => { |
| 122 | + const planDir = join(runRoot, "plandir-hdr-bans"); |
| 123 | + let caught: unknown; |
| 124 | + try { |
| 125 | + await plan( |
| 126 | + projectDir, |
| 127 | + { |
| 128 | + format: "mp4", |
| 129 | + fps: 30, |
| 130 | + width: 320, |
| 131 | + height: 240, |
| 132 | + hdrMode: "force-hdr" as DistributedRenderConfig["hdrMode"], |
| 133 | + }, |
| 134 | + planDir, |
| 135 | + ); |
| 136 | + } catch (err) { |
| 137 | + caught = err; |
| 138 | + } |
| 139 | + expect(caught).toBeInstanceOf(FormatNotSupportedInDistributedError); |
| 140 | + expect((caught as FormatNotSupportedInDistributedError).format).toBe("mp4-hdr"); |
| 141 | + expect(existsSync(planDir)).toBe(false); |
| 142 | + }); |
| 143 | +}); |
0 commit comments