|
| 1 | +import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { describe, expect, it, vi } from "vitest"; |
| 5 | +import { |
| 6 | + buildCompareSuccessPayload, |
| 7 | + capCompareVariants, |
| 8 | + parseCompareArgs, |
| 9 | + prepareCompareVariantProjects, |
| 10 | +} from "./compare.js"; |
| 11 | + |
| 12 | +function tempDir(): string { |
| 13 | + return mkdtempSync(join(tmpdir(), "hf-compare-test-")); |
| 14 | +} |
| 15 | + |
| 16 | +describe("parseCompareArgs", () => { |
| 17 | + it("requires at least two composition paths", () => { |
| 18 | + expect(() => parseCompareArgs({ _: ["variant-a"] }, "/tmp")).toThrow( |
| 19 | + "need 2+ paths to compare", |
| 20 | + ); |
| 21 | + }); |
| 22 | + |
| 23 | + it("rejects --labels when the count does not match the path count", () => { |
| 24 | + expect(() => |
| 25 | + parseCompareArgs({ _: ["variant-a", "variant-b"], labels: "one,two,three" }, "/tmp"), |
| 26 | + ).toThrow("--labels count (3) must match path count (2)"); |
| 27 | + }); |
| 28 | + |
| 29 | + it("derives default labels from directory basenames and html filenames", () => { |
| 30 | + const parsed = parseCompareArgs( |
| 31 | + { _: ["./looks/warm", "./looks/cool.html", "/tmp/hero.alt.html"] }, |
| 32 | + "/work/project", |
| 33 | + ); |
| 34 | + |
| 35 | + expect(parsed.variants).toEqual([ |
| 36 | + { label: "warm", inputPath: "/work/project/looks/warm", displayPath: "./looks/warm" }, |
| 37 | + { |
| 38 | + label: "cool", |
| 39 | + inputPath: "/work/project/looks/cool.html", |
| 40 | + displayPath: "./looks/cool.html", |
| 41 | + }, |
| 42 | + { label: "hero.alt", inputPath: "/tmp/hero.alt.html", displayPath: "/tmp/hero.alt.html" }, |
| 43 | + ]); |
| 44 | + }); |
| 45 | +}); |
| 46 | + |
| 47 | +describe("capCompareVariants", () => { |
| 48 | + it("truncates over-cap variants and exposes truncation metadata for JSON output", () => { |
| 49 | + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); |
| 50 | + const logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); |
| 51 | + try { |
| 52 | + const variants = Array.from({ length: 20 }, (_, index) => ({ |
| 53 | + label: `variant ${index + 1}`, |
| 54 | + inputPath: `/tmp/variant-${index + 1}`, |
| 55 | + displayPath: `variant-${index + 1}`, |
| 56 | + })); |
| 57 | + |
| 58 | + const capped = capCompareVariants(variants); |
| 59 | + |
| 60 | + expect(capped.variants).toHaveLength(16); |
| 61 | + expect(capped.variants.at(0)?.label).toBe("variant 1"); |
| 62 | + expect(capped.variants.at(15)?.label).toBe("variant 16"); |
| 63 | + expect(capped.truncated).toBe(true); |
| 64 | + expect(capped.total).toBe(20); |
| 65 | + expect(buildCompareSuccessPayload("/tmp/compare.png", capped.variants, capped)).toMatchObject( |
| 66 | + { |
| 67 | + ok: true, |
| 68 | + sheet: "/tmp/compare.png", |
| 69 | + rendered: 16, |
| 70 | + truncated: true, |
| 71 | + total: 20, |
| 72 | + }, |
| 73 | + ); |
| 74 | + expect(errorSpy).toHaveBeenCalledWith( |
| 75 | + expect.stringContaining("Warning: 20 compare variants exceed the 16-variant cap"), |
| 76 | + ); |
| 77 | + expect(logSpy).not.toHaveBeenCalled(); |
| 78 | + } finally { |
| 79 | + vi.restoreAllMocks(); |
| 80 | + } |
| 81 | + }); |
| 82 | +}); |
| 83 | + |
| 84 | +describe("prepareCompareVariantProjects", () => { |
| 85 | + it("uses project directories directly and stages standalone html files as index.html", () => { |
| 86 | + const dir = tempDir(); |
| 87 | + try { |
| 88 | + const projectDir = join(dir, "variant-a"); |
| 89 | + const htmlDir = join(dir, "variant-b"); |
| 90 | + const projectIndex = join(projectDir, "index.html"); |
| 91 | + const htmlFile = join(htmlDir, "candidate.html"); |
| 92 | + mkdirSync(projectDir, { recursive: true }); |
| 93 | + mkdirSync(htmlDir, { recursive: true }); |
| 94 | + writeFileSync(projectIndex, "<!doctype html><title>A</title>", { flag: "wx" }); |
| 95 | + writeFileSync(htmlFile, "<!doctype html><title>B</title>", { flag: "wx" }); |
| 96 | + writeFileSync(join(htmlDir, "asset.txt"), "asset"); |
| 97 | + |
| 98 | + const prepared = prepareCompareVariantProjects([ |
| 99 | + { label: "A", inputPath: projectDir, displayPath: "variant-a" }, |
| 100 | + { label: "B", inputPath: htmlFile, displayPath: "variant-b/candidate.html" }, |
| 101 | + ]); |
| 102 | + |
| 103 | + try { |
| 104 | + expect(prepared).toHaveLength(2); |
| 105 | + expect(prepared[0]).toMatchObject({ |
| 106 | + label: "A", |
| 107 | + inputPath: projectDir, |
| 108 | + displayPath: "variant-a", |
| 109 | + projectDir, |
| 110 | + }); |
| 111 | + expect(prepared[0]?.stagedDir).toBeUndefined(); |
| 112 | + expect(prepared[1]?.projectDir).not.toBe(htmlDir); |
| 113 | + expect(prepared[1]?.stagedDir).toBe(prepared[1]?.projectDir); |
| 114 | + expect(readFileSync(join(prepared[1]!.projectDir, "index.html"), "utf-8")).toContain( |
| 115 | + "<title>B</title>", |
| 116 | + ); |
| 117 | + expect(readFileSync(join(prepared[1]!.projectDir, "asset.txt"), "utf-8")).toBe("asset"); |
| 118 | + expect(existsSync(join(prepared[1]!.projectDir, "candidate.html"))).toBe(false); |
| 119 | + } finally { |
| 120 | + for (const variant of prepared) { |
| 121 | + if (variant.stagedDir) rmSync(variant.stagedDir, { recursive: true, force: true }); |
| 122 | + } |
| 123 | + } |
| 124 | + } finally { |
| 125 | + rmSync(dir, { recursive: true, force: true }); |
| 126 | + } |
| 127 | + }); |
| 128 | + |
| 129 | + it("errors clearly for inputs that are not composition projects or html files", () => { |
| 130 | + const dir = tempDir(); |
| 131 | + try { |
| 132 | + const badFile = join(dir, "notes.txt"); |
| 133 | + writeFileSync(badFile, "not a composition"); |
| 134 | + |
| 135 | + expect(() => |
| 136 | + prepareCompareVariantProjects([ |
| 137 | + { label: "notes", inputPath: badFile, displayPath: "notes.txt" }, |
| 138 | + { label: "other", inputPath: join(dir, "missing"), displayPath: "missing" }, |
| 139 | + ]), |
| 140 | + ).toThrow(/not a composition input.*notes\.txt/); |
| 141 | + } finally { |
| 142 | + rmSync(dir, { recursive: true, force: true }); |
| 143 | + } |
| 144 | + }); |
| 145 | +}); |
0 commit comments