|
1 | 1 | // fallow-ignore-file code-duplication |
2 | 2 | import { EventEmitter } from "events"; |
3 | | -import { readFileSync } from "fs"; |
| 3 | +import { spawnSync } from "child_process"; |
| 4 | +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "fs"; |
| 5 | +import { tmpdir } from "os"; |
4 | 6 | import { basename, resolve } from "path"; |
5 | 7 | import { afterEach, describe, expect, it, vi } from "vitest"; |
6 | 8 | import { |
@@ -113,6 +115,272 @@ describe("extractPngMetadataFromBuffer", () => { |
113 | 115 | ); |
114 | 116 | expect(extractPngMetadataFromBuffer(fixture)?.colorSpace?.colorTransfer).toBe("smpte2084"); |
115 | 117 | }); |
| 118 | + |
| 119 | + it("keeps metadata fallback independent from full PNG integrity validation", () => { |
| 120 | + const ihdr = pngChunk("IHDR", [0, 0, 0, 1, 0, 0, 0, 1, 16, 2, 0, 0, 0]); |
| 121 | + expect(extractPngMetadataFromBuffer(buildPngWithChunks([ihdr]))).toMatchObject({ |
| 122 | + width: 1, |
| 123 | + height: 1, |
| 124 | + }); |
| 125 | + }); |
| 126 | +}); |
| 127 | + |
| 128 | +describe("probeMediaProfile", () => { |
| 129 | + afterEach(() => { |
| 130 | + vi.resetModules(); |
| 131 | + vi.doUnmock("child_process"); |
| 132 | + }); |
| 133 | + |
| 134 | + it("classifies still, moving, audio-only, and mixed streams from probe data", async () => { |
| 135 | + const { spawn } = createSpawnSpy([ |
| 136 | + { |
| 137 | + kind: "exit", |
| 138 | + code: 0, |
| 139 | + stdout: JSON.stringify({ |
| 140 | + streams: [{ codec_type: "video", codec_name: "png" }], |
| 141 | + format: { format_name: "png_pipe" }, |
| 142 | + }), |
| 143 | + }, |
| 144 | + { |
| 145 | + kind: "exit", |
| 146 | + code: 0, |
| 147 | + stdout: JSON.stringify({ |
| 148 | + streams: [{ codec_type: "video", codec_name: "h264" }], |
| 149 | + format: { format_name: "mov,mp4,m4a,3gp,3g2,mj2" }, |
| 150 | + }), |
| 151 | + }, |
| 152 | + { |
| 153 | + kind: "exit", |
| 154 | + code: 0, |
| 155 | + stdout: JSON.stringify({ |
| 156 | + streams: [{ codec_type: "audio", codec_name: "mp3" }], |
| 157 | + format: { format_name: "mp3" }, |
| 158 | + }), |
| 159 | + }, |
| 160 | + { |
| 161 | + kind: "exit", |
| 162 | + code: 0, |
| 163 | + stdout: JSON.stringify({ |
| 164 | + streams: [{ codec_type: "video" }, { codec_type: "audio" }], |
| 165 | + format: { format_name: "matroska,webm" }, |
| 166 | + }), |
| 167 | + }, |
| 168 | + ]); |
| 169 | + vi.resetModules(); |
| 170 | + vi.doMock("child_process", () => ({ spawn })); |
| 171 | + const { probeMediaProfile } = await import("./ffprobe.js"); |
| 172 | + |
| 173 | + const validPngPath = resolve( |
| 174 | + __dirname, |
| 175 | + "../../../producer/tests/hdr-regression/src/hdr-photo-pq.png", |
| 176 | + ); |
| 177 | + await expect(probeMediaProfile(validPngPath)).resolves.toEqual({ |
| 178 | + hasVideoStream: true, |
| 179 | + hasAudioStream: false, |
| 180 | + visualKind: "still", |
| 181 | + }); |
| 182 | + await expect(probeMediaProfile("/tmp/extensionless-video")).resolves.toEqual({ |
| 183 | + hasVideoStream: true, |
| 184 | + hasAudioStream: false, |
| 185 | + visualKind: "moving", |
| 186 | + }); |
| 187 | + await expect(probeMediaProfile("/tmp/extensionless-audio")).resolves.toEqual({ |
| 188 | + hasVideoStream: false, |
| 189 | + hasAudioStream: true, |
| 190 | + visualKind: "none", |
| 191 | + }); |
| 192 | + await expect(probeMediaProfile("/tmp/mixed-av")).resolves.toEqual({ |
| 193 | + hasVideoStream: true, |
| 194 | + hasAudioStream: true, |
| 195 | + visualKind: "moving", |
| 196 | + }); |
| 197 | + }); |
| 198 | + |
| 199 | + it("does not treat attached cover art in an audio container as an image asset", async () => { |
| 200 | + const { spawn } = createSpawnSpy([ |
| 201 | + { |
| 202 | + kind: "exit", |
| 203 | + code: 0, |
| 204 | + stdout: JSON.stringify({ |
| 205 | + streams: [ |
| 206 | + { codec_type: "video", disposition: { attached_pic: 1 } }, |
| 207 | + { codec_type: "audio" }, |
| 208 | + ], |
| 209 | + format: { format_name: "mp3" }, |
| 210 | + }), |
| 211 | + }, |
| 212 | + ]); |
| 213 | + vi.resetModules(); |
| 214 | + vi.doMock("child_process", () => ({ spawn })); |
| 215 | + const { probeMediaProfile } = await import("./ffprobe.js"); |
| 216 | + await expect(probeMediaProfile("/tmp/audio-with-cover")).resolves.toMatchObject({ |
| 217 | + hasAudioStream: true, |
| 218 | + visualKind: "none", |
| 219 | + }); |
| 220 | + }); |
| 221 | + |
| 222 | + it("deduplicates probes within one cancellation scope without sharing across scopes", async () => { |
| 223 | + const fixtureDir = mkdtempSync(resolve(tmpdir(), "hf-media-probe-cache-")); |
| 224 | + const fixturePath = resolve(fixtureDir, "asset"); |
| 225 | + writeFileSync(fixturePath, "cache identity only"); |
| 226 | + const outcome = { |
| 227 | + kind: "exit" as const, |
| 228 | + code: 0, |
| 229 | + stdout: JSON.stringify({ |
| 230 | + streams: [{ codec_type: "video", codec_name: "h264" }], |
| 231 | + format: { format_name: "mov,mp4,m4a,3gp,3g2,mj2" }, |
| 232 | + }), |
| 233 | + }; |
| 234 | + const { spawn, calls } = createSpawnSpy([outcome, outcome]); |
| 235 | + vi.resetModules(); |
| 236 | + vi.doMock("child_process", () => ({ spawn })); |
| 237 | + const { probeMediaProfile } = await import("./ffprobe.js"); |
| 238 | + const firstSignal = new AbortController().signal; |
| 239 | + const secondSignal = new AbortController().signal; |
| 240 | + try { |
| 241 | + await Promise.all([ |
| 242 | + probeMediaProfile(fixturePath, { signal: firstSignal }), |
| 243 | + probeMediaProfile(fixturePath, { signal: firstSignal }), |
| 244 | + ]); |
| 245 | + expect(calls).toHaveLength(1); |
| 246 | + await probeMediaProfile(fixturePath, { signal: secondSignal }); |
| 247 | + expect(calls).toHaveLength(2); |
| 248 | + } finally { |
| 249 | + rmSync(fixtureDir, { recursive: true, force: true }); |
| 250 | + } |
| 251 | + }); |
| 252 | + |
| 253 | + it("bounds the process-scoped probe cache", async () => { |
| 254 | + const fixtureDir = mkdtempSync(resolve(tmpdir(), "hf-media-probe-lru-")); |
| 255 | + const fixturePaths = Array.from({ length: 129 }, (_, index) => |
| 256 | + resolve(fixtureDir, `asset-${index}`), |
| 257 | + ); |
| 258 | + for (const fixturePath of fixturePaths) writeFileSync(fixturePath, "probe identity"); |
| 259 | + const outcome = { |
| 260 | + kind: "exit" as const, |
| 261 | + code: 0, |
| 262 | + stdout: JSON.stringify({ |
| 263 | + streams: [{ codec_type: "audio", codec_name: "aac" }], |
| 264 | + format: { format_name: "aac" }, |
| 265 | + }), |
| 266 | + }; |
| 267 | + const { spawn, calls } = createSpawnSpy([outcome]); |
| 268 | + vi.resetModules(); |
| 269 | + vi.doMock("child_process", () => ({ spawn })); |
| 270 | + const { probeMediaProfile } = await import("./ffprobe.js"); |
| 271 | + try { |
| 272 | + for (const fixturePath of fixturePaths) await probeMediaProfile(fixturePath); |
| 273 | + await probeMediaProfile(fixturePaths[0]!); |
| 274 | + expect(calls).toHaveLength(130); |
| 275 | + } finally { |
| 276 | + rmSync(fixtureDir, { recursive: true, force: true }); |
| 277 | + } |
| 278 | + }); |
| 279 | + |
| 280 | + it("classifies extensionless AVIF from its ISO-BMFF brand instead of the generic mov demuxer", async () => { |
| 281 | + const fixtureDir = mkdtempSync(resolve(tmpdir(), "hf-avif-profile-")); |
| 282 | + const fixturePath = resolve(fixtureDir, "asset"); |
| 283 | + const ftyp = Buffer.alloc(24); |
| 284 | + ftyp.writeUInt32BE(24, 0); |
| 285 | + ftyp.write("ftyp", 4, 4, "ascii"); |
| 286 | + ftyp.write("avif", 8, 4, "ascii"); |
| 287 | + ftyp.writeUInt32BE(0, 12); |
| 288 | + ftyp.write("mif1", 16, 4, "ascii"); |
| 289 | + ftyp.write("avif", 20, 4, "ascii"); |
| 290 | + writeFileSync(fixturePath, ftyp); |
| 291 | + const { spawn } = createSpawnSpy([ |
| 292 | + { |
| 293 | + kind: "exit", |
| 294 | + code: 0, |
| 295 | + stdout: JSON.stringify({ |
| 296 | + streams: [{ codec_type: "video", codec_name: "av1" }], |
| 297 | + format: { format_name: "mov,mp4,m4a,3gp,3g2,mj2" }, |
| 298 | + }), |
| 299 | + }, |
| 300 | + ]); |
| 301 | + vi.resetModules(); |
| 302 | + vi.doMock("child_process", () => ({ spawn })); |
| 303 | + const { probeMediaProfile } = await import("./ffprobe.js"); |
| 304 | + try { |
| 305 | + await expect(probeMediaProfile(fixturePath)).resolves.toMatchObject({ |
| 306 | + hasVideoStream: true, |
| 307 | + hasAudioStream: false, |
| 308 | + visualKind: "still", |
| 309 | + }); |
| 310 | + } finally { |
| 311 | + rmSync(fixtureDir, { recursive: true, force: true }); |
| 312 | + } |
| 313 | + }); |
| 314 | + |
| 315 | + it("uses any non-attached video stream when cover art precedes moving video", async () => { |
| 316 | + const { spawn } = createSpawnSpy([ |
| 317 | + { |
| 318 | + kind: "exit", |
| 319 | + code: 0, |
| 320 | + stdout: JSON.stringify({ |
| 321 | + streams: [ |
| 322 | + { codec_type: "video", disposition: { attached_pic: 1 } }, |
| 323 | + { codec_type: "video", codec_name: "h264" }, |
| 324 | + { codec_type: "audio" }, |
| 325 | + ], |
| 326 | + format: { format_name: "mov,mp4,m4a,3gp,3g2,mj2" }, |
| 327 | + }), |
| 328 | + }, |
| 329 | + ]); |
| 330 | + vi.resetModules(); |
| 331 | + vi.doMock("child_process", () => ({ spawn })); |
| 332 | + const { probeMediaProfile } = await import("./ffprobe.js"); |
| 333 | + await expect(probeMediaProfile("/tmp/video-with-cover")).resolves.toMatchObject({ |
| 334 | + visualKind: "moving", |
| 335 | + }); |
| 336 | + }); |
| 337 | + |
| 338 | + it.skipIf(spawnSync("ffprobe", ["-version"]).status !== 0)( |
| 339 | + "rejects an IHDR-only truncated PNG even when ffprobe accepts png_pipe", |
| 340 | + async () => { |
| 341 | + vi.resetModules(); |
| 342 | + vi.doUnmock("child_process"); |
| 343 | + const fixtureDir = mkdtempSync(resolve(tmpdir(), "hf-truncated-png-profile-")); |
| 344 | + const fixturePath = resolve(fixtureDir, "asset"); |
| 345 | + const ihdr = pngChunk("IHDR", [0, 0, 0, 1, 0, 0, 0, 1, 16, 2, 0, 0, 0]); |
| 346 | + writeFileSync(fixturePath, buildPngWithChunks([ihdr])); |
| 347 | + const { probeMediaProfile } = await import("./ffprobe.js"); |
| 348 | + try { |
| 349 | + await expect(probeMediaProfile(fixturePath)).rejects.toThrow(); |
| 350 | + } finally { |
| 351 | + rmSync(fixtureDir, { recursive: true, force: true }); |
| 352 | + } |
| 353 | + }, |
| 354 | + ); |
| 355 | + |
| 356 | + it("does not turn an aborted PNG probe into a successful metadata fallback", async () => { |
| 357 | + type KillableFakeProc = FakeProc & { kill: (signal?: NodeJS.Signals) => boolean }; |
| 358 | + const spawn = () => { |
| 359 | + const proc = new EventEmitter() as KillableFakeProc; |
| 360 | + proc.stdout = new EventEmitter(); |
| 361 | + proc.stderr = new EventEmitter(); |
| 362 | + proc.kill = vi.fn(() => { |
| 363 | + process.nextTick(() => proc.emit("close", null, "SIGTERM")); |
| 364 | + return true; |
| 365 | + }); |
| 366 | + process.nextTick(() => proc.emit("spawn")); |
| 367 | + return proc; |
| 368 | + }; |
| 369 | + const fixtureDir = mkdtempSync(resolve(tmpdir(), "hf-aborted-png-profile-")); |
| 370 | + const fixturePath = resolve(fixtureDir, "asset"); |
| 371 | + writeFileSync(fixturePath, buildMinimalPng()); |
| 372 | + vi.resetModules(); |
| 373 | + vi.doMock("child_process", () => ({ spawn })); |
| 374 | + const { probeMediaProfile } = await import("./ffprobe.js"); |
| 375 | + const controller = new AbortController(); |
| 376 | + try { |
| 377 | + const pending = probeMediaProfile(fixturePath, { signal: controller.signal }); |
| 378 | + controller.abort(new Error("render cancelled")); |
| 379 | + await expect(pending).rejects.toThrow("render cancelled"); |
| 380 | + } finally { |
| 381 | + rmSync(fixtureDir, { recursive: true, force: true }); |
| 382 | + } |
| 383 | + }); |
116 | 384 | }); |
117 | 385 |
|
118 | 386 | interface SpawnCall { |
@@ -222,6 +490,23 @@ describe("ffprobe missing-binary fallback", () => { |
222 | 490 | expect(calls[0]?.args.slice(0, 2)).toEqual(["-v", "error"]); |
223 | 491 | }); |
224 | 492 |
|
| 493 | + it("does not accept an incomplete PNG through the missing-binary fallback", async () => { |
| 494 | + const fixtureDir = mkdtempSync(resolve(tmpdir(), "hf-truncated-png-fallback-")); |
| 495 | + const fixturePath = resolve(fixtureDir, "asset"); |
| 496 | + const ihdr = pngChunk("IHDR", [0, 0, 0, 1, 0, 0, 0, 1, 16, 2, 0, 0, 0]); |
| 497 | + writeFileSync(fixturePath, buildPngWithChunks([ihdr])); |
| 498 | + const { spawn } = createSpawnSpy([{ kind: "missing" }]); |
| 499 | + hidePathBinaries(); |
| 500 | + vi.resetModules(); |
| 501 | + vi.doMock("child_process", () => ({ spawn })); |
| 502 | + const { probeMediaProfile } = await import("./ffprobe.js"); |
| 503 | + try { |
| 504 | + await expect(probeMediaProfile(fixturePath)).rejects.toThrow(/ffprobe/i); |
| 505 | + } finally { |
| 506 | + rmSync(fixtureDir, { recursive: true, force: true }); |
| 507 | + } |
| 508 | + }); |
| 509 | + |
225 | 510 | // `profile` matters now: the packet refinement is an allowlist on AAC-LC, |
226 | 511 | // because the 1024-sample formula is wrong for LD/ELD/HE and unverified for |
227 | 512 | // the rest. An unprofiled "aac" stream deliberately keeps its container |
@@ -715,11 +1000,12 @@ describe("extractPngMetadataFromBuffer cICP ordering", () => { |
715 | 1000 | it("does not emit color space until IHDR provides width and height", () => { |
716 | 1001 | const ihdr = pngChunk("IHDR", [0, 0, 0, 1, 0, 0, 0, 1, 16, 2, 0, 0, 0]); |
717 | 1002 | const cicp = pngChunk("cICP", [9, 16, 0, 1]); |
| 1003 | + const idat = pngChunk("IDAT", [0x78, 0x9c, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01]); |
718 | 1004 | const iend = pngChunk("IEND", []); |
719 | 1005 |
|
720 | 1006 | // cICP before IHDR is invalid PNG ordering; make sure we don't return |
721 | 1007 | // zero-sized metadata in that case. |
722 | | - const malformed = buildPngWithChunks([cicp, ihdr, iend]); |
| 1008 | + const malformed = buildPngWithChunks([cicp, ihdr, idat, iend]); |
723 | 1009 | expect(extractPngMetadataFromBuffer(malformed)).toEqual({ |
724 | 1010 | width: 1, |
725 | 1011 | height: 1, |
|
0 commit comments