|
| 1 | +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { readCompositionDimensions, warnOnDimensionMismatch } from "./_dimensions.js"; |
| 6 | + |
| 7 | +describe("readCompositionDimensions", () => { |
| 8 | + it("parses width-first attribute order", () => { |
| 9 | + const html = |
| 10 | + '<!doctype html><html><body data-width="1920" data-height="1080" data-duration="5"></body></html>'; |
| 11 | + expect(readCompositionDimensions(html)).toEqual({ width: 1920, height: 1080 }); |
| 12 | + }); |
| 13 | + |
| 14 | + it("parses height-first attribute order", () => { |
| 15 | + const html = |
| 16 | + '<!doctype html><html><body data-duration="5" data-height="2160" data-width="3840"></body></html>'; |
| 17 | + expect(readCompositionDimensions(html)).toEqual({ width: 3840, height: 2160 }); |
| 18 | + }); |
| 19 | + |
| 20 | + it("tolerates single quotes", () => { |
| 21 | + const html = "<body data-width='1080' data-height='1920'></body>"; |
| 22 | + expect(readCompositionDimensions(html)).toEqual({ width: 1080, height: 1920 }); |
| 23 | + }); |
| 24 | + |
| 25 | + it("returns null when an attribute is missing", () => { |
| 26 | + expect(readCompositionDimensions('<body data-width="1920"></body>')).toBeNull(); |
| 27 | + }); |
| 28 | + |
| 29 | + it("returns null on empty HTML", () => { |
| 30 | + expect(readCompositionDimensions("")).toBeNull(); |
| 31 | + }); |
| 32 | +}); |
| 33 | + |
| 34 | +describe("warnOnDimensionMismatch", () => { |
| 35 | + let dir: string; |
| 36 | + let warnSpy: ReturnType<typeof vi.fn>; |
| 37 | + let originalWarn: typeof console.warn; |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + dir = mkdtempSync(join(tmpdir(), "hf-dim-mismatch-")); |
| 41 | + originalWarn = console.warn; |
| 42 | + warnSpy = vi.fn(); |
| 43 | + console.warn = warnSpy as unknown as typeof console.warn; |
| 44 | + }); |
| 45 | + |
| 46 | + afterEach(() => { |
| 47 | + console.warn = originalWarn; |
| 48 | + rmSync(dir, { recursive: true, force: true }); |
| 49 | + }); |
| 50 | + |
| 51 | + function writeIndex(html: string): void { |
| 52 | + writeFileSync(join(dir, "index.html"), html); |
| 53 | + } |
| 54 | + |
| 55 | + it("warns when the CLI dimensions don't match the composition", () => { |
| 56 | + writeIndex('<body data-width="1920" data-height="1080"></body>'); |
| 57 | + warnOnDimensionMismatch({ |
| 58 | + projectDir: dir, |
| 59 | + cliWidth: 3840, |
| 60 | + cliHeight: 2160, |
| 61 | + outputResolution: undefined, |
| 62 | + quiet: false, |
| 63 | + }); |
| 64 | + expect(warnSpy).toHaveBeenCalledTimes(1); |
| 65 | + const call = (warnSpy.mock.calls[0]?.[0] as string) ?? ""; |
| 66 | + expect(call).toContain("3840×2160"); |
| 67 | + expect(call).toContain("1920×1080"); |
| 68 | + expect(call).toContain("--output-resolution"); |
| 69 | + }); |
| 70 | + |
| 71 | + it("is silent when CLI and composition agree", () => { |
| 72 | + writeIndex('<body data-width="1920" data-height="1080"></body>'); |
| 73 | + warnOnDimensionMismatch({ |
| 74 | + projectDir: dir, |
| 75 | + cliWidth: 1920, |
| 76 | + cliHeight: 1080, |
| 77 | + outputResolution: undefined, |
| 78 | + quiet: false, |
| 79 | + }); |
| 80 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 81 | + }); |
| 82 | + |
| 83 | + it("is silent when --output-resolution is set (the supported supersampling path)", () => { |
| 84 | + writeIndex('<body data-width="1920" data-height="1080"></body>'); |
| 85 | + warnOnDimensionMismatch({ |
| 86 | + projectDir: dir, |
| 87 | + cliWidth: 3840, |
| 88 | + cliHeight: 2160, |
| 89 | + outputResolution: "landscape-4k", |
| 90 | + quiet: false, |
| 91 | + }); |
| 92 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 93 | + }); |
| 94 | + |
| 95 | + it("is silent when quiet=true (--json)", () => { |
| 96 | + writeIndex('<body data-width="1920" data-height="1080"></body>'); |
| 97 | + warnOnDimensionMismatch({ |
| 98 | + projectDir: dir, |
| 99 | + cliWidth: 3840, |
| 100 | + cliHeight: 2160, |
| 101 | + outputResolution: undefined, |
| 102 | + quiet: true, |
| 103 | + }); |
| 104 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 105 | + }); |
| 106 | + |
| 107 | + it("is silent when index.html is missing (typical with --site-id)", () => { |
| 108 | + warnOnDimensionMismatch({ |
| 109 | + projectDir: dir, |
| 110 | + cliWidth: 3840, |
| 111 | + cliHeight: 2160, |
| 112 | + outputResolution: undefined, |
| 113 | + quiet: false, |
| 114 | + }); |
| 115 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 116 | + }); |
| 117 | + |
| 118 | + it("is silent when the composition has no data-width/data-height", () => { |
| 119 | + writeIndex("<body><h1>just a comp</h1></body>"); |
| 120 | + warnOnDimensionMismatch({ |
| 121 | + projectDir: dir, |
| 122 | + cliWidth: 3840, |
| 123 | + cliHeight: 2160, |
| 124 | + outputResolution: undefined, |
| 125 | + quiet: false, |
| 126 | + }); |
| 127 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 128 | + }); |
| 129 | +}); |
0 commit comments