-
Notifications
You must be signed in to change notification settings - Fork 4.4k
fix(cli): publish capture metadata without overwriting files #3720
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+143
−6
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import * as fs from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import { generateProjectScaffold } from "./scaffolding.js"; | ||
| import type { DesignTokens } from "./types.js"; | ||
|
|
||
| vi.mock("node:fs", async (importOriginal) => { | ||
| const original = await importOriginal<typeof fs>(); | ||
| return { | ||
| ...original, | ||
| existsSync: vi.fn(original.existsSync), | ||
| writeFileSync: vi.fn(original.writeFileSync), | ||
| linkSync: vi.fn(original.linkSync), | ||
| }; | ||
| }); | ||
|
|
||
| const tokens: DesignTokens = { | ||
| title: "Captured site", | ||
| description: "", | ||
| cssVariables: {}, | ||
| fonts: [], | ||
| colors: [], | ||
| headings: [], | ||
| ctas: [], | ||
| svgs: [], | ||
| sections: [], | ||
| }; | ||
|
|
||
| describe("generateProjectScaffold metadata", () => { | ||
| let dir: string; | ||
| let metaPath: string; | ||
| let warnings: string[]; | ||
| const progress = vi.fn(); | ||
|
|
||
| beforeEach(() => { | ||
| dir = fs.mkdtempSync(join(tmpdir(), "hf-scaffold-")); | ||
| metaPath = join(dir, "meta.json"); | ||
| warnings = []; | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| if (fs.existsSync(dir)) { | ||
| expect(fs.readdirSync(dir).filter((name) => name.startsWith(".hf-meta-"))).toEqual([]); | ||
| } | ||
| fs.rmSync(dir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| function generate(url = "https://www.example.com", title = tokens.title) { | ||
| return generateProjectScaffold( | ||
| dir, | ||
| url, | ||
| { ...tokens, title }, | ||
| undefined, | ||
| false, | ||
| false, | ||
| false, | ||
| [], | ||
| progress, | ||
| warnings, | ||
| ); | ||
| } | ||
|
|
||
| it.each(["Captured site", ""])( | ||
| "creates metadata with title %j and agent instructions", | ||
| async (title) => { | ||
| await generate(undefined, title); | ||
| expect(fs.readFileSync(metaPath, "utf-8")).toBe( | ||
| JSON.stringify({ id: "example.com-video", name: title || "example.com" }, null, 2), | ||
| ); | ||
| expect(fs.readFileSync(join(dir, "AGENTS.md"), "utf-8")).toBe( | ||
| fs.readFileSync(join(dir, "CLAUDE.md"), "utf-8"), | ||
| ); | ||
| expect(fs.existsSync(join(dir, "index.html"))).toBe(false); | ||
| expect(progress).toHaveBeenCalledWith("agent", "AGENTS.md + CLAUDE.md generated"); | ||
| expect(warnings).toEqual([]); | ||
| }, | ||
| ); | ||
|
|
||
| it("keeps existing metadata without parsing the unused URL", async () => { | ||
| fs.writeFileSync(metaPath, "user metadata"); | ||
| await generate("not a URL"); | ||
| expect(fs.readFileSync(metaPath, "utf-8")).toBe("user metadata"); | ||
| }); | ||
|
|
||
| it("preserves a file created after the existence check and continues scaffolding", async () => { | ||
| vi.mocked(fs.existsSync).mockImplementationOnce(() => { | ||
| fs.writeFileSync(metaPath, "concurrent metadata"); | ||
| return false; | ||
| }); | ||
| await generate(); | ||
| expect(fs.readFileSync(metaPath, "utf-8")).toBe("concurrent metadata"); | ||
| expect(progress).toHaveBeenCalledWith("agent", "AGENTS.md + CLAUDE.md generated"); | ||
| expect(warnings).toEqual([]); | ||
| }); | ||
|
|
||
| it("does not follow a dangling metadata symlink", async () => { | ||
| const target = join(dir, "missing-target.json"); | ||
| fs.symlinkSync(target, metaPath); | ||
| await generate(); | ||
| expect(fs.lstatSync(metaPath).isSymbolicLink()).toBe(true); | ||
| expect(fs.existsSync(target)).toBe(false); | ||
| expect(warnings).toEqual([]); | ||
| }); | ||
|
|
||
| it("propagates write errors other than EEXIST", async () => { | ||
| fs.rmSync(dir, { recursive: true }); | ||
| await expect(generate()).rejects.toMatchObject({ code: "ENOENT" }); | ||
| expect(progress).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it.each(["writeFileSync", "linkSync"] as const)( | ||
| "cleans staging and propagates failures from %s", | ||
| async (operation) => { | ||
| const error = Object.assign(new Error("injected I/O failure"), { code: "EIO" }); | ||
| vi.mocked(fs[operation]).mockImplementationOnce(() => { | ||
| throw error; | ||
| }); | ||
| await expect(generate()).rejects.toBe(error); | ||
| expect(fs.existsSync(metaPath)).toBe(false); | ||
| expect(progress).not.toHaveBeenCalled(); | ||
| }, | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.