diff --git a/packages/cli/src/capture/contactSheet.test.ts b/packages/cli/src/capture/contactSheet.test.ts index 2a1db0cb6d..fd27ddc078 100644 --- a/packages/cli/src/capture/contactSheet.test.ts +++ b/packages/cli/src/capture/contactSheet.test.ts @@ -1,6 +1,7 @@ import { existsSync, lstatSync, + mkdirSync, mkdtempSync, readFileSync, rmSync, @@ -9,14 +10,15 @@ import { } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; -import sharp from "sharp"; -import { describe, expect, it } from "vitest"; +import sharp, { type Sharp } from "sharp"; +import { describe, expect, it, vi } from "vitest"; import { createContactSheet, createAssetContactSheet, createScrollContactSheet, createSvgContactSheet, } from "./contactSheet.js"; +import { loadSharp } from "../utils/optionalPackages.js"; function tempDir(): string { return mkdtempSync(join(tmpdir(), "hf-contact-sheet-test-")); @@ -310,4 +312,31 @@ describe("contact-sheet capture budget", () => { rmSync(dir, { recursive: true, force: true }); } }, 60_000); + + it("labels each SVG thumbnail with its file name", async () => { + const dir = tempDir(); + const runtimeSharp = await loadSharp("contact sheets"); + const sharpProto: Sharp = Object.getPrototypeOf( + runtimeSharp({ create: { width: 1, height: 1, channels: 3, background: "#000" } }), + ); + const composite = vi.spyOn(sharpProto, "composite"); + try { + const svgs = join(dir, "svgs"); + mkdirSync(svgs); + writeFileSync( + join(svgs, "logo-mark.svg"), + '', + ); + + await createSvgContactSheet(svgs, join(dir, "svg-contact-sheet.png")); + + const labelSvgs = composite.mock.calls + .flatMap(([overlays]) => overlays ?? []) + .map((overlay) => (Buffer.isBuffer(overlay.input) ? overlay.input.toString() : "")); + expect(labelSvgs.some((svg) => svg.includes(">1. logo-mark"))).toBe(true); + } finally { + composite.mockRestore(); + rmSync(dir, { recursive: true, force: true }); + } + }, 60_000); }); diff --git a/packages/cli/src/capture/contactSheet.ts b/packages/cli/src/capture/contactSheet.ts index f2f3cc1733..57e2a01279 100644 --- a/packages/cli/src/capture/contactSheet.ts +++ b/packages/cli/src/capture/contactSheet.ts @@ -304,7 +304,7 @@ export async function createSvgContactSheet( if (svgPaths.length === 0) return []; const sharp = await loadSharp("contact sheets"); - const svgFileNames = svgPaths.map((p) => p.split("/").pop()!); + const svgFileNames = svgPaths.map((p) => basename(p)); // Render ALL SVGs to PNG thumbnails first, then paginate the sheets const thumbSize = 200;