|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import * as fs from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { generateProjectScaffold } from "./scaffolding.js"; |
| 6 | +import type { DesignTokens } from "./types.js"; |
| 7 | + |
| 8 | +vi.mock("node:fs", async (importOriginal) => { |
| 9 | + const original = await importOriginal<typeof fs>(); |
| 10 | + return { |
| 11 | + ...original, |
| 12 | + existsSync: vi.fn(original.existsSync), |
| 13 | + writeFileSync: vi.fn(original.writeFileSync), |
| 14 | + linkSync: vi.fn(original.linkSync), |
| 15 | + }; |
| 16 | +}); |
| 17 | + |
| 18 | +const tokens: DesignTokens = { |
| 19 | + title: "Captured site", |
| 20 | + description: "", |
| 21 | + cssVariables: {}, |
| 22 | + fonts: [], |
| 23 | + colors: [], |
| 24 | + headings: [], |
| 25 | + ctas: [], |
| 26 | + svgs: [], |
| 27 | + sections: [], |
| 28 | +}; |
| 29 | + |
| 30 | +describe("generateProjectScaffold metadata", () => { |
| 31 | + let dir: string; |
| 32 | + let metaPath: string; |
| 33 | + let warnings: string[]; |
| 34 | + const progress = vi.fn(); |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + dir = fs.mkdtempSync(join(tmpdir(), "hf-scaffold-")); |
| 38 | + metaPath = join(dir, "meta.json"); |
| 39 | + warnings = []; |
| 40 | + vi.clearAllMocks(); |
| 41 | + }); |
| 42 | + |
| 43 | + afterEach(() => { |
| 44 | + if (fs.existsSync(dir)) { |
| 45 | + expect(fs.readdirSync(dir).filter((name) => name.startsWith(".hf-meta-"))).toEqual([]); |
| 46 | + } |
| 47 | + fs.rmSync(dir, { recursive: true, force: true }); |
| 48 | + }); |
| 49 | + |
| 50 | + function generate(url = "https://www.example.com", title = tokens.title) { |
| 51 | + return generateProjectScaffold( |
| 52 | + dir, |
| 53 | + url, |
| 54 | + { ...tokens, title }, |
| 55 | + undefined, |
| 56 | + false, |
| 57 | + false, |
| 58 | + false, |
| 59 | + [], |
| 60 | + progress, |
| 61 | + warnings, |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + it.each(["Captured site", ""])( |
| 66 | + "creates metadata with title %j and agent instructions", |
| 67 | + async (title) => { |
| 68 | + await generate(undefined, title); |
| 69 | + expect(fs.readFileSync(metaPath, "utf-8")).toBe( |
| 70 | + JSON.stringify({ id: "example.com-video", name: title || "example.com" }, null, 2), |
| 71 | + ); |
| 72 | + expect(fs.readFileSync(join(dir, "AGENTS.md"), "utf-8")).toBe( |
| 73 | + fs.readFileSync(join(dir, "CLAUDE.md"), "utf-8"), |
| 74 | + ); |
| 75 | + expect(fs.existsSync(join(dir, "index.html"))).toBe(false); |
| 76 | + expect(progress).toHaveBeenCalledWith("agent", "AGENTS.md + CLAUDE.md generated"); |
| 77 | + expect(warnings).toEqual([]); |
| 78 | + }, |
| 79 | + ); |
| 80 | + |
| 81 | + it("keeps existing metadata without parsing the unused URL", async () => { |
| 82 | + fs.writeFileSync(metaPath, "user metadata"); |
| 83 | + await generate("not a URL"); |
| 84 | + expect(fs.readFileSync(metaPath, "utf-8")).toBe("user metadata"); |
| 85 | + }); |
| 86 | + |
| 87 | + it("preserves a file created after the existence check and continues scaffolding", async () => { |
| 88 | + vi.mocked(fs.existsSync).mockImplementationOnce(() => { |
| 89 | + fs.writeFileSync(metaPath, "concurrent metadata"); |
| 90 | + return false; |
| 91 | + }); |
| 92 | + await generate(); |
| 93 | + expect(fs.readFileSync(metaPath, "utf-8")).toBe("concurrent metadata"); |
| 94 | + expect(progress).toHaveBeenCalledWith("agent", "AGENTS.md + CLAUDE.md generated"); |
| 95 | + expect(warnings).toEqual([]); |
| 96 | + }); |
| 97 | + |
| 98 | + it("does not follow a dangling metadata symlink", async () => { |
| 99 | + const target = join(dir, "missing-target.json"); |
| 100 | + fs.symlinkSync(target, metaPath); |
| 101 | + await generate(); |
| 102 | + expect(fs.lstatSync(metaPath).isSymbolicLink()).toBe(true); |
| 103 | + expect(fs.existsSync(target)).toBe(false); |
| 104 | + expect(warnings).toEqual([]); |
| 105 | + }); |
| 106 | + |
| 107 | + it("propagates write errors other than EEXIST", async () => { |
| 108 | + fs.rmSync(dir, { recursive: true }); |
| 109 | + await expect(generate()).rejects.toMatchObject({ code: "ENOENT" }); |
| 110 | + expect(progress).not.toHaveBeenCalled(); |
| 111 | + }); |
| 112 | + |
| 113 | + it.each(["writeFileSync", "linkSync"] as const)( |
| 114 | + "cleans staging and propagates failures from %s", |
| 115 | + async (operation) => { |
| 116 | + const error = Object.assign(new Error("injected I/O failure"), { code: "EIO" }); |
| 117 | + vi.mocked(fs[operation]).mockImplementationOnce(() => { |
| 118 | + throw error; |
| 119 | + }); |
| 120 | + await expect(generate()).rejects.toBe(error); |
| 121 | + expect(fs.existsSync(metaPath)).toBe(false); |
| 122 | + expect(progress).not.toHaveBeenCalled(); |
| 123 | + }, |
| 124 | + ); |
| 125 | +}); |
0 commit comments