|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import type { VideoColorSpace } from "@hyperframes/engine"; |
| 3 | +import { resolveProjectRelativeSrc } from "@hyperframes/engine"; |
| 4 | +import { inspectHdrAutoPromotion } from "./hdrPromotion.js"; |
| 5 | + |
| 6 | +const PROJECT = { dir: "/project", name: "hdr-check", indexPath: "/project/index.html" }; |
| 7 | +const HDR: VideoColorSpace = { |
| 8 | + colorTransfer: "smpte2084", |
| 9 | + colorPrimaries: "bt2020", |
| 10 | + colorSpace: "bt2020nc", |
| 11 | +}; |
| 12 | +const SDR: VideoColorSpace = { |
| 13 | + colorTransfer: "bt709", |
| 14 | + colorPrimaries: "bt709", |
| 15 | + colorSpace: "bt709", |
| 16 | +}; |
| 17 | + |
| 18 | +function metadata(colorSpace: VideoColorSpace, videoCodec = "hevc") { |
| 19 | + return { |
| 20 | + durationSeconds: 1, |
| 21 | + videoStreamDurationSeconds: 1, |
| 22 | + width: 1920, |
| 23 | + height: 1080, |
| 24 | + fps: 30, |
| 25 | + videoCodec, |
| 26 | + hasAudio: false, |
| 27 | + isVFR: false, |
| 28 | + hasAlpha: false, |
| 29 | + colorSpace, |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +function bundledMedia(videos: string[], images: string[]) { |
| 34 | + return { |
| 35 | + bundleHtml: vi.fn(async () => "<html>bundled sub-composition</html>"), |
| 36 | + collectMedia: vi.fn(() => ({ |
| 37 | + videos: videos.map((src) => ({ src })), |
| 38 | + images: images.map((src) => ({ src })), |
| 39 | + })), |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +describe("inspectHdrAutoPromotion", () => { |
| 44 | + it("reports the local HDR asset that promotes automatic output", async () => { |
| 45 | + const assetPath = resolveProjectRelativeSrc("assets/source-hdr.mp4", PROJECT.dir, PROJECT.dir); |
| 46 | + const extractMetadata = vi.fn(async () => metadata(HDR)); |
| 47 | + |
| 48 | + await expect( |
| 49 | + inspectHdrAutoPromotion( |
| 50 | + PROJECT, |
| 51 | + extractMetadata, |
| 52 | + bundledMedia(["assets/source-hdr.mp4?token=secret#preview"], []), |
| 53 | + ), |
| 54 | + ).resolves.toEqual({ |
| 55 | + triggeringAsset: "assets/source-hdr.mp4", |
| 56 | + output: { colorSpace: "BT.2020", codec: "HEVC Main10" }, |
| 57 | + }); |
| 58 | + expect(extractMetadata).toHaveBeenCalledOnce(); |
| 59 | + expect(extractMetadata).toHaveBeenCalledWith(assetPath); |
| 60 | + }); |
| 61 | + |
| 62 | + it("reports an active remote HDR video from the bundled render media", async () => { |
| 63 | + const remote = "https://media.example/remote-hdr.mp4?token=secret"; |
| 64 | + const extractMetadata = vi.fn(async () => metadata(HDR)); |
| 65 | + |
| 66 | + await expect( |
| 67 | + inspectHdrAutoPromotion(PROJECT, extractMetadata, bundledMedia([remote], [])), |
| 68 | + ).resolves.toEqual({ |
| 69 | + triggeringAsset: "https://media.example/remote-hdr.mp4", |
| 70 | + output: { colorSpace: "BT.2020", codec: "HEVC Main10" }, |
| 71 | + }); |
| 72 | + expect(extractMetadata).toHaveBeenCalledWith(remote); |
| 73 | + }); |
| 74 | + |
| 75 | + it("uses the first HDR video before an HDR image and still inspects images", async () => { |
| 76 | + const firstVideo = "https://media.example/first-hdr-video.mp4?token=secret"; |
| 77 | + const image = "assets/fallback-hdr-image.png"; |
| 78 | + const extractMetadata = vi.fn(async (src: string) => |
| 79 | + metadata(src.replaceAll("\\", "/").endsWith("/assets/sdr.mp4") ? SDR : HDR), |
| 80 | + ); |
| 81 | + |
| 82 | + await expect( |
| 83 | + inspectHdrAutoPromotion( |
| 84 | + PROJECT, |
| 85 | + extractMetadata, |
| 86 | + bundledMedia(["assets/sdr.mp4", firstVideo], [image]), |
| 87 | + ), |
| 88 | + ).resolves.toMatchObject({ triggeringAsset: "https://media.example/first-hdr-video.mp4" }); |
| 89 | + expect(extractMetadata).toHaveBeenCalledWith( |
| 90 | + resolveProjectRelativeSrc("assets/sdr.mp4", PROJECT.dir, PROJECT.dir), |
| 91 | + ); |
| 92 | + expect(extractMetadata).toHaveBeenCalledWith(firstVideo); |
| 93 | + expect(extractMetadata).toHaveBeenCalledWith( |
| 94 | + resolveProjectRelativeSrc("assets/fallback-hdr-image.png", PROJECT.dir, PROJECT.dir), |
| 95 | + ); |
| 96 | + }); |
| 97 | + |
| 98 | + it("reports an HDR image when no HDR video is present", async () => { |
| 99 | + const extractMetadata = vi.fn(async () => |
| 100 | + metadata( |
| 101 | + { colorTransfer: "arib-std-b67", colorPrimaries: "bt2020", colorSpace: "bt2020nc" }, |
| 102 | + "png", |
| 103 | + ), |
| 104 | + ); |
| 105 | + |
| 106 | + await expect( |
| 107 | + inspectHdrAutoPromotion(PROJECT, extractMetadata, bundledMedia([], ["assets/hdr.png"])), |
| 108 | + ).resolves.toMatchObject({ triggeringAsset: "assets/hdr.png" }); |
| 109 | + }); |
| 110 | +}); |
0 commit comments