|
1 | 1 | import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
2 | 2 | import { tmpdir } from "node:os"; |
3 | 3 | import { join } from "node:path"; |
4 | | -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
5 | | -import { warnOnDimensionMismatch } from "./_dimensions.js"; |
| 4 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 5 | +import { resolveOutputResolutionFromSize } from "./_dimensions.js"; |
6 | 6 |
|
7 | 7 | const indexHtml = (width: number, height: number) => |
8 | 8 | `<!doctype html><html><body><div data-composition-id="root" data-width="${width}" data-height="${height}"></div></body></html>`; |
9 | 9 |
|
10 | | -describe("warnOnDimensionMismatch", () => { |
| 10 | +describe("resolveOutputResolutionFromSize", () => { |
11 | 11 | let dir: string; |
12 | | - let warnSpy: ReturnType<typeof vi.fn>; |
13 | | - let originalWarn: typeof console.warn; |
14 | 12 |
|
15 | 13 | beforeEach(() => { |
16 | | - dir = mkdtempSync(join(tmpdir(), "hf-dim-mismatch-")); |
17 | | - originalWarn = console.warn; |
18 | | - warnSpy = vi.fn(); |
19 | | - console.warn = warnSpy as unknown as typeof console.warn; |
| 14 | + dir = mkdtempSync(join(tmpdir(), "hf-dim-size-")); |
20 | 15 | }); |
21 | 16 |
|
22 | 17 | afterEach(() => { |
23 | | - console.warn = originalWarn; |
24 | 18 | rmSync(dir, { recursive: true, force: true }); |
25 | 19 | }); |
26 | 20 |
|
27 | | - function writeIndex(html: string): void { |
28 | | - writeFileSync(join(dir, "index.html"), html); |
29 | | - } |
30 | | - |
31 | | - it("warns when the CLI dimensions don't match the composition", () => { |
32 | | - writeIndex(indexHtml(1920, 1080)); |
33 | | - warnOnDimensionMismatch({ |
| 21 | + const resolve = (compositionSize: [number, number], size: [number, number]) => { |
| 22 | + writeFileSync(join(dir, "index.html"), indexHtml(...compositionSize)); |
| 23 | + return resolveOutputResolutionFromSize({ |
34 | 24 | projectDir: dir, |
35 | | - cliWidth: 3840, |
36 | | - cliHeight: 2160, |
| 25 | + cliWidth: size[0], |
| 26 | + cliHeight: size[1], |
37 | 27 | outputResolution: undefined, |
38 | | - quiet: false, |
39 | 28 | }); |
40 | | - expect(warnSpy).toHaveBeenCalledTimes(1); |
41 | | - const call = (warnSpy.mock.calls[0]?.[0] as string) ?? ""; |
42 | | - expect(call).toContain("3840×2160"); |
43 | | - expect(call).toContain("1920×1080"); |
44 | | - expect(call).toContain("--output-resolution"); |
| 29 | + }; |
| 30 | + |
| 31 | + it("renders a whole-number multiple of the composition at that scale", () => { |
| 32 | + expect(resolve([1920, 1080], [3840, 2160])).toBe("landscape-4k"); |
| 33 | + expect(resolve([1080, 1920], [2160, 3840])).toBe("portrait-4k"); |
45 | 34 | }); |
46 | 35 |
|
47 | | - it("is silent when CLI and composition agree", () => { |
48 | | - writeIndex(indexHtml(1920, 1080)); |
49 | | - warnOnDimensionMismatch({ |
50 | | - projectDir: dir, |
51 | | - cliWidth: 1920, |
52 | | - cliHeight: 1080, |
53 | | - outputResolution: undefined, |
54 | | - quiet: false, |
55 | | - }); |
56 | | - expect(warnSpy).not.toHaveBeenCalled(); |
| 36 | + it("leaves the composition's own size alone", () => { |
| 37 | + expect(resolve([1920, 1080], [1920, 1080])).toBeUndefined(); |
57 | 38 | }); |
58 | 39 |
|
59 | | - it("is silent when --output-resolution is set (the supersampling path)", () => { |
60 | | - writeIndex(indexHtml(1920, 1080)); |
61 | | - warnOnDimensionMismatch({ |
62 | | - projectDir: dir, |
63 | | - cliWidth: 3840, |
64 | | - cliHeight: 2160, |
65 | | - outputResolution: "landscape-4k", |
66 | | - quiet: false, |
67 | | - }); |
68 | | - expect(warnSpy).not.toHaveBeenCalled(); |
| 40 | + it("refuses a size that is not a supported multiple, naming the composition", () => { |
| 41 | + expect(() => resolve([1920, 1080], [2560, 1440])).toThrow(/1920×1080/); |
| 42 | + expect(() => resolve([1920, 1080], [3840, 1080])).toThrow(/1920×1080/); |
69 | 43 | }); |
70 | 44 |
|
71 | | - it("is silent when quiet=true (--json)", () => { |
72 | | - writeIndex(indexHtml(1920, 1080)); |
73 | | - warnOnDimensionMismatch({ |
74 | | - projectDir: dir, |
75 | | - cliWidth: 3840, |
76 | | - cliHeight: 2160, |
77 | | - outputResolution: undefined, |
78 | | - quiet: true, |
79 | | - }); |
80 | | - expect(warnSpy).not.toHaveBeenCalled(); |
| 45 | + it("refuses a size smaller than the composition", () => { |
| 46 | + expect(() => resolve([1920, 1080], [1280, 720])).toThrow( |
| 47 | + /smaller than the composition \(1920×1080\)/, |
| 48 | + ); |
81 | 49 | }); |
82 | 50 |
|
83 | | - it("is silent when index.html is missing (typical with --site-id)", () => { |
84 | | - warnOnDimensionMismatch({ |
85 | | - projectDir: dir, |
86 | | - cliWidth: 3840, |
87 | | - cliHeight: 2160, |
88 | | - outputResolution: undefined, |
89 | | - quiet: false, |
90 | | - }); |
91 | | - expect(warnSpy).not.toHaveBeenCalled(); |
| 51 | + it("keeps an explicit --output-resolution", () => { |
| 52 | + writeFileSync(join(dir, "index.html"), indexHtml(1920, 1080)); |
| 53 | + expect( |
| 54 | + resolveOutputResolutionFromSize({ |
| 55 | + projectDir: dir, |
| 56 | + cliWidth: 1234, |
| 57 | + cliHeight: 567, |
| 58 | + outputResolution: "landscape-4k", |
| 59 | + }), |
| 60 | + ).toBe("landscape-4k"); |
92 | 61 | }); |
93 | 62 |
|
94 | | - it("is silent when the composition has no data-composition-id root", () => { |
95 | | - writeIndex("<body><h1>just a comp</h1></body>"); |
96 | | - warnOnDimensionMismatch({ |
97 | | - projectDir: dir, |
98 | | - cliWidth: 3840, |
99 | | - cliHeight: 2160, |
100 | | - outputResolution: undefined, |
101 | | - quiet: false, |
102 | | - }); |
103 | | - expect(warnSpy).not.toHaveBeenCalled(); |
| 63 | + it("passes through when the composition is not on disk", () => { |
| 64 | + expect( |
| 65 | + resolveOutputResolutionFromSize({ |
| 66 | + projectDir: join(dir, "missing"), |
| 67 | + cliWidth: 3840, |
| 68 | + cliHeight: 2160, |
| 69 | + outputResolution: undefined, |
| 70 | + }), |
| 71 | + ).toBeUndefined(); |
104 | 72 | }); |
105 | 73 | }); |
0 commit comments