|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { Hono } from "hono"; |
| 6 | +import type { ProjectDir } from "../utils/project.js"; |
| 7 | + |
| 8 | +// `registerCompositionRoute` reaches these two studio-server helpers via a |
| 9 | +// module, since both files sit at the same depth under packages/cli/src/. |
| 10 | +// |
| 11 | +// The error class lives inside this `vi.hoisted` block (not a plain top-level |
| 12 | +// `class`) because `vi.mock` factories run during static-import resolution — |
| 13 | +// before any of the test file's own top-level statements execute — so a |
| 14 | +// `class` declared below would still be in its temporal dead zone. |
| 15 | +const mocks = vi.hoisted(() => { |
| 16 | + class FakeProxyTranscodeError extends Error { |
| 17 | + readonly exitCode: number | null; |
| 18 | + readonly stderrTail: string; |
| 19 | + constructor(message: string, exitCode: number | null = null, stderrTail = "") { |
| 20 | + super(message); |
| 21 | + this.name = "ProxyTranscodeError"; |
| 22 | + this.exitCode = exitCode; |
| 23 | + this.stderrTail = stderrTail; |
| 24 | + } |
| 25 | + } |
| 26 | + class FakeProxyCapacityError extends FakeProxyTranscodeError { |
| 27 | + constructor(message = "media proxy queue is full") { |
| 28 | + super(message); |
| 29 | + this.name = "ProxyCapacityError"; |
| 30 | + } |
| 31 | + } |
| 32 | + return { |
| 33 | + resolveProxy: vi.fn<(projectDir: string, absoluteSourcePath: string) => Promise<string>>( |
| 34 | + // Benign default so `injectMediaCodecMap`'s fire-and-forget pre-warm |
| 35 | + // (called for every hostile map entry) always gets a promise to |
| 36 | + // `.catch()`, even in tests that don't care about the proxy path. |
| 37 | + async () => "/unused-prewarm-proxy-path", |
| 38 | + ), |
| 39 | + scanProjectMediaCodecMap: vi.fn< |
| 40 | + ( |
| 41 | + ...args: unknown[] |
| 42 | + ) => Promise< |
| 43 | + Record< |
| 44 | + string, |
| 45 | + { codecName: string; browserHostile: boolean; representativeMime: string | null } |
| 46 | + > |
| 47 | + > |
| 48 | + >(async () => ({})), |
| 49 | + ProxyTranscodeError: FakeProxyTranscodeError, |
| 50 | + ProxyCapacityError: FakeProxyCapacityError, |
| 51 | + }; |
| 52 | +}); |
| 53 | +const FakeProxyTranscodeError = mocks.ProxyTranscodeError; |
| 54 | + |
| 55 | +const mediaMocks = vi.hoisted(() => ({ |
| 56 | + probeAssetCodec: vi.fn(async () => ({ |
| 57 | + codecName: "hevc", |
| 58 | + browserHostile: true, |
| 59 | + representativeMime: "video/mp4", |
| 60 | + hasAlpha: false, |
| 61 | + })), |
| 62 | + decideMediaProxyEligibility: vi.fn( |
| 63 | + (facts: { hasAlpha: boolean; browserHostile: boolean } | null) => |
| 64 | + facts?.hasAlpha ? { eligible: false, reason: "alpha_source" } : { eligible: true }, |
| 65 | + ), |
| 66 | +})); |
| 67 | + |
| 68 | +vi.mock("@hyperframes/studio-server/proxy-transcoder", () => ({ |
| 69 | + resolveProxy: mocks.resolveProxy, |
| 70 | + ProxyTranscodeError: mocks.ProxyTranscodeError, |
| 71 | + ProxyCapacityError: mocks.ProxyCapacityError, |
| 72 | +})); |
| 73 | +vi.mock("@hyperframes/studio-server/media-codec-map", () => mediaMocks); |
| 74 | + |
| 75 | +// The shared injection helper ships as a self-contained dist bundle (its copy |
| 76 | +// of scanProjectMediaCodecMap is inlined), so it must be mocked wholesale — |
| 77 | +// mocking the media-codec-map subpath can't reach inside it. The fake mirrors |
| 78 | +// the real contract (scan → inject tag) via this file's scan mock so the |
| 79 | +// existing injection assertions stay meaningful. |
| 80 | +vi.mock("@hyperframes/studio-server/media-proxy-preview", () => ({ |
| 81 | + injectMediaCodecMapIntoHtml: vi.fn( |
| 82 | + async (html: string, projectDir: string, htmlSources: unknown[]) => { |
| 83 | + const map = await mocks.scanProjectMediaCodecMap(projectDir, htmlSources); |
| 84 | + const tag = `<script data-hf-media-codec-map>window.__HF_MEDIA_CODEC_MAP__=${JSON.stringify(map)};</script>`; |
| 85 | + return html.includes("</head>") |
| 86 | + ? html.replace("</head>", `${tag}\n</head>`) |
| 87 | + : `${tag}\n${html}`; |
| 88 | + }, |
| 89 | + ), |
| 90 | +})); |
| 91 | + |
| 92 | +const { registerCompositionRoute } = await import("./play.js"); |
| 93 | + |
| 94 | +let dir: string | undefined; |
| 95 | + |
| 96 | +function tmpProject(): ProjectDir { |
| 97 | + dir = mkdtempSync(join(tmpdir(), "hf-play-test-")); |
| 98 | + return { dir, name: "test-project", indexPath: join(dir, "index.html") }; |
| 99 | +} |
| 100 | + |
| 101 | +afterEach(() => { |
| 102 | + mocks.resolveProxy.mockReset(); |
| 103 | + mocks.resolveProxy.mockResolvedValue("/unused-prewarm-proxy-path"); |
| 104 | + mocks.scanProjectMediaCodecMap.mockReset(); |
| 105 | + mediaMocks.probeAssetCodec.mockClear(); |
| 106 | + mediaMocks.decideMediaProxyEligibility.mockClear(); |
| 107 | + mocks.scanProjectMediaCodecMap.mockResolvedValue({}); |
| 108 | + if (dir) rmSync(dir, { recursive: true, force: true }); |
| 109 | + dir = undefined; |
| 110 | +}); |
| 111 | + |
| 112 | +it("rejects direct proxy requests for alpha sources", async () => { |
| 113 | + const project = tmpProject(); |
| 114 | + writeFileSync(join(project.dir, "clip.mov"), "alpha-prores"); |
| 115 | + mediaMocks.probeAssetCodec.mockResolvedValueOnce({ |
| 116 | + codecName: "prores", |
| 117 | + browserHostile: true, |
| 118 | + representativeMime: "video/quicktime", |
| 119 | + hasAlpha: true, |
| 120 | + }); |
| 121 | + mediaMocks.decideMediaProxyEligibility.mockReturnValueOnce({ |
| 122 | + eligible: false, |
| 123 | + reason: "alpha_source", |
| 124 | + }); |
| 125 | + const app = await buildApp(project, true); |
| 126 | + |
| 127 | + const res = await app.request("/composition/clip.mov?hf-proxy=h264"); |
| 128 | + |
| 129 | + expect(res.status).toBe(422); |
| 130 | + expect(await res.text()).toContain("alpha_source"); |
| 131 | + expect(mocks.resolveProxy).not.toHaveBeenCalled(); |
| 132 | +}); |
| 133 | + |
| 134 | +async function buildApp(project: ProjectDir, autoProxy: boolean): Promise<Hono> { |
| 135 | + const app = new Hono(); |
| 136 | + await registerCompositionRoute(app, project, autoProxy); |
| 137 | + return app; |
| 138 | +} |
| 139 | + |
| 140 | +describe("registerCompositionRoute", () => { |
| 141 | + it("answers a Range request on a plain asset with 206 + the requested byte slice", async () => { |
| 142 | + const project = tmpProject(); |
| 143 | + writeFileSync(join(project.dir, "clip.mp4"), Buffer.from("0123456789", "utf-8")); |
| 144 | + const app = await buildApp(project, true); |
| 145 | + |
| 146 | + const res = await app.request("/composition/clip.mp4", { headers: { Range: "bytes=2-5" } }); |
| 147 | + |
| 148 | + expect(res.status).toBe(206); |
| 149 | + expect(res.headers.get("accept-ranges")).toBe("bytes"); |
| 150 | + expect(res.headers.get("content-range")).toBe("bytes 2-5/10"); |
| 151 | + expect(await res.text()).toBe("2345"); |
| 152 | + }); |
| 153 | + |
| 154 | + it("serves the resolved proxy's bytes for ?hf-proxy=h264 on a hostile asset", async () => { |
| 155 | + const project = tmpProject(); |
| 156 | + writeFileSync(join(project.dir, "clip.mp4"), "original-hevc-bytes"); |
| 157 | + const proxyPath = join(project.dir, "proxy.mp4"); |
| 158 | + writeFileSync(proxyPath, "transcoded-h264-bytes"); |
| 159 | + mocks.resolveProxy.mockResolvedValue(proxyPath); |
| 160 | + const app = await buildApp(project, true); |
| 161 | + |
| 162 | + const res = await app.request("/composition/clip.mp4?hf-proxy=h264"); |
| 163 | + |
| 164 | + expect(res.status).toBe(200); |
| 165 | + expect(await res.text()).toBe("transcoded-h264-bytes"); |
| 166 | + expect(mocks.resolveProxy).toHaveBeenCalledWith(project.dir, join(project.dir, "clip.mp4")); |
| 167 | + }); |
| 168 | + |
| 169 | + it("serves ?hf-proxy=h264 for a .mov hostile asset as Content-Type video/mp4 (the proxy IS mp4)", async () => { |
| 170 | + const project = tmpProject(); |
| 171 | + writeFileSync(join(project.dir, "clip.mov"), "original-prores-bytes"); |
| 172 | + const proxyPath = join(project.dir, "proxy.mp4"); |
| 173 | + writeFileSync(proxyPath, "transcoded-h264-bytes"); |
| 174 | + mocks.resolveProxy.mockResolvedValue(proxyPath); |
| 175 | + const app = await buildApp(project, true); |
| 176 | + |
| 177 | + const res = await app.request("/composition/clip.mov?hf-proxy=h264"); |
| 178 | + |
| 179 | + expect(res.status).toBe(200); |
| 180 | + expect(res.headers.get("content-type")).toBe("video/mp4"); |
| 181 | + expect(await res.text()).toBe("transcoded-h264-bytes"); |
| 182 | + }); |
| 183 | + |
| 184 | + it.each(["mxf", "mts", "m2ts", "ts"])( |
| 185 | + "recognizes .%s camera/container media as proxy-eligible video", |
| 186 | + async (extension) => { |
| 187 | + const project = tmpProject(); |
| 188 | + writeFileSync(join(project.dir, `clip.${extension}`), "hostile-video-bytes"); |
| 189 | + const proxyPath = join(project.dir, "proxy.mp4"); |
| 190 | + writeFileSync(proxyPath, "transcoded-h264-bytes"); |
| 191 | + mocks.resolveProxy.mockResolvedValue(proxyPath); |
| 192 | + const app = await buildApp(project, true); |
| 193 | + |
| 194 | + const res = await app.request(`/composition/clip.${extension}?hf-proxy=h264`); |
| 195 | + |
| 196 | + expect(res.status).toBe(200); |
| 197 | + expect(await res.text()).toBe("transcoded-h264-bytes"); |
| 198 | + }, |
| 199 | + ); |
| 200 | + |
| 201 | + it("answers 502 (not a silent failure) when the proxy transcode fails", async () => { |
| 202 | + const project = tmpProject(); |
| 203 | + writeFileSync(join(project.dir, "clip.mp4"), "original-hevc-bytes"); |
| 204 | + mocks.resolveProxy.mockRejectedValue( |
| 205 | + new FakeProxyTranscodeError("ffmpeg exited with code 1", 1), |
| 206 | + ); |
| 207 | + const app = await buildApp(project, true); |
| 208 | + |
| 209 | + const res = await app.request("/composition/clip.mp4?hf-proxy=h264"); |
| 210 | + |
| 211 | + expect(res.status).toBe(502); |
| 212 | + }); |
| 213 | + |
| 214 | + it("answers a retryable 503 when the proxy queue is full", async () => { |
| 215 | + const project = tmpProject(); |
| 216 | + writeFileSync(join(project.dir, "clip.mp4"), "original-hevc-bytes"); |
| 217 | + mocks.resolveProxy.mockRejectedValue(new mocks.ProxyCapacityError()); |
| 218 | + const app = await buildApp(project, true); |
| 219 | + |
| 220 | + const res = await app.request("/composition/clip.mp4?hf-proxy=h264"); |
| 221 | + |
| 222 | + expect(res.status).toBe(503); |
| 223 | + expect(res.headers.get("retry-after")).toBe("1"); |
| 224 | + }); |
| 225 | + |
| 226 | + it("404s ?hf-proxy=h264 for a non-video asset without attempting a transcode", async () => { |
| 227 | + const project = tmpProject(); |
| 228 | + writeFileSync(join(project.dir, "image.png"), "not-a-video"); |
| 229 | + const app = await buildApp(project, true); |
| 230 | + |
| 231 | + const res = await app.request("/composition/image.png?hf-proxy=h264"); |
| 232 | + |
| 233 | + expect(res.status).toBe(404); |
| 234 | + expect(mocks.resolveProxy).not.toHaveBeenCalled(); |
| 235 | + }); |
| 236 | + |
| 237 | + it("injects __HF_MEDIA_CODEC_MAP__ into served composition HTML", async () => { |
| 238 | + const project = tmpProject(); |
| 239 | + writeFileSync(join(project.dir, "index.html"), "<html><head></head><body></body></html>"); |
| 240 | + mocks.scanProjectMediaCodecMap.mockResolvedValue({ |
| 241 | + "/clip.mp4": { |
| 242 | + codecName: "hevc", |
| 243 | + browserHostile: true, |
| 244 | + representativeMime: 'video/mp4; codecs="hvc1.1.6.L120.B0"', |
| 245 | + }, |
| 246 | + }); |
| 247 | + const app = await buildApp(project, true); |
| 248 | + |
| 249 | + const res = await app.request("/composition/index.html"); |
| 250 | + const html = await res.text(); |
| 251 | + |
| 252 | + expect(html).toContain("__HF_MEDIA_CODEC_MAP__"); |
| 253 | + expect(html).toContain("/clip.mp4"); |
| 254 | + }); |
| 255 | + |
| 256 | + it("opt-out (autoProxy=false) skips codec-map injection and 404s the proxy param", async () => { |
| 257 | + const project = tmpProject(); |
| 258 | + writeFileSync(join(project.dir, "index.html"), "<html><head></head><body></body></html>"); |
| 259 | + writeFileSync(join(project.dir, "clip.mp4"), "original-hevc-bytes"); |
| 260 | + mocks.scanProjectMediaCodecMap.mockResolvedValue({ |
| 261 | + "/clip.mp4": { codecName: "hevc", browserHostile: true, representativeMime: null }, |
| 262 | + }); |
| 263 | + const app = await buildApp(project, false); |
| 264 | + |
| 265 | + const htmlRes = await app.request("/composition/index.html"); |
| 266 | + expect(await htmlRes.text()).not.toContain("__HF_MEDIA_CODEC_MAP__"); |
| 267 | + expect(mocks.scanProjectMediaCodecMap).not.toHaveBeenCalled(); |
| 268 | + |
| 269 | + const proxyRes = await app.request("/composition/clip.mp4?hf-proxy=h264"); |
| 270 | + expect(proxyRes.status).toBe(404); |
| 271 | + expect(mocks.resolveProxy).not.toHaveBeenCalled(); |
| 272 | + }); |
| 273 | +}); |
0 commit comments