import { describe, expect, it } from "vitest"; import { TOOLCRAFT_MAX_VIDEO_ARTIFACT_BYTES, getToolcraftVideoExportBitrate, resolveToolcraftVideoEncodingPolicy, } from "./video-encoding-policy"; describe("video encoding policy", () => { it("prefers AVC for MP4 and VP9 then VP8 for WebM", () => { const support = { avc: true, vp8: true, vp9: true }; expect( resolveToolcraftVideoEncodingPolicy({ durationSeconds: 1, height: 1080, requestedFormat: "mp4", support, width: 1920, }), ).toMatchObject({ codec: "avc", extension: ".mp4" }); expect( resolveToolcraftVideoEncodingPolicy({ durationSeconds: 1, height: 1080, requestedFormat: "webm", support, width: 1920, }), ).toMatchObject({ codec: "vp9", extension: ".webm" }); }); it("falls back to the other real container without changing quality", () => { expect( resolveToolcraftVideoEncodingPolicy({ durationSeconds: 1, height: 2160, requestedFormat: "mp4", support: { avc: false, vp8: true, vp9: false }, width: 3840, }), ).toEqual({ bitrate: 12_000_000, codec: "vp8", extension: ".webm", mediaType: "video/webm", }); }); it("bounds bitrate and the maximum supported artifact", () => { expect(getToolcraftVideoExportBitrate(320, 180)).toBe(2_000_000); expect(getToolcraftVideoExportBitrate(3840, 2160)).toBe(12_000_000); const projectedBytes = (12_000_000 * 60 * 1.1) / 8; expect(projectedBytes).toBeLessThan(TOOLCRAFT_MAX_VIDEO_ARTIFACT_BYTES); }); it("fails visibly when no timestamped codec is supported", () => { expect(() => resolveToolcraftVideoEncodingPolicy({ durationSeconds: 1, height: 1080, requestedFormat: "mp4", support: { avc: false, vp8: false, vp9: false }, width: 1920, }), ).toThrow("timestamped video encoder"); }); });