import { describe, expect, it, spyOn } from "bun:test"; import { log } from "@clack/prompts"; import { collectPublishedPosts, Injected, injectLinkTags, matchPostsToHtmlFiles, } from "../src/commands/inject"; const atUri = "at://did:plc:abc123/app.bsky.feed.post/xyz"; const publicationUri = "at://did:plc:def456/app.bsky.feed.generator/main"; describe("injectLinkTags", () => { describe("neither tag needs injection", () => { it("returns AlreadyPresent when both tags already exist", () => { const content = `
`; const result = injectLinkTags( false, "test.html", content, atUri, publicationUri, ); expect(result).toBe(Injected.AlreadyPresent); }); }); describe("one tag needs injection", () => { it("injects only documentLinkTag when publicationLinkTag is already present", () => { const content = ` `; const result = injectLinkTags( false, "test.html", content, atUri, publicationUri, ); expect(typeof result).toBe("string"); expect(result as string).toContain( ``, ); expect( result as string, ).not.toContain(` { const content = ` `; const result = injectLinkTags( false, "test.html", content, atUri, publicationUri, ); expect(typeof result).toBe("string"); expect(result as string).toContain( ``, ); }); }); describe("both tags need injection", () => { it("injects both tags when neither is present", () => { const content = "\n"; const result = injectLinkTags( false, "test.html", content, atUri, publicationUri, ); expect(typeof result).toBe("string"); expect(result as string).toContain( ``, ); expect(result as string).toContain( ``, ); }); it("injects tags before ", () => { const content = "\n"; const result = injectLinkTags( false, "test.html", content, atUri, publicationUri, ) as string; const headCloseIndex = result.indexOf(""); expect(result.indexOf('rel="site.standard.document"')).toBeLessThan( headCloseIndex, ); expect(result.indexOf('rel="site.standard.publication"')).toBeLessThan( headCloseIndex, ); }); it("returns Skipped when no is found", () => { const warnSpy = spyOn(log, "warn").mockImplementation(() => {}); const content = "No head tag here"; const result = injectLinkTags( false, "test.html", content, atUri, publicationUri, ); expect(result).toBe(Injected.Skipped); expect(warnSpy).toHaveBeenCalledWith( " No found in test.html, skipping", ); warnSpy.mockRestore(); }); it("returns Faked and does not modify content during dry run", () => { const messageSpy = spyOn(log, "message").mockImplementation(() => {}); const content = "\n"; const result = injectLinkTags( true, "test.html", content, atUri, publicationUri, ); expect(result).toBe(Injected.Faked); expect(messageSpy).toHaveBeenCalledWith(" Would inject into: test.html"); expect(messageSpy).toHaveBeenCalledWith( ` `, ); expect(messageSpy).toHaveBeenCalledWith( ` `, ); messageSpy.mockRestore(); }); }); }); describe("matchPostsToHtmlFiles", () => { const otherAtUri = "at://did:plc:abc123/site.standard.document/other"; it("prefers the generated post path over a section index with the same slug", () => { // https://tangled.org/stevedylan.dev/sequoia/issues/62 // Zola: content/blog/2024-12-17-blog/index.md with slug = "blog" // builds to public/blog/blog/index.html, alongside the section index // public/blog/index.html const { matches } = matchPostsToHtmlFiles( ["index.html", "blog/index.html", "blog/blog/index.html"], [ { filePath: "content/blog/2024-12-17-blog/index.md", slug: "blog", atUri, }, ], { contentDir: "./content", pathPrefix: "/blog" }, ); expect(matches.get("blog/blog/index.html")).toBe(atUri); expect(matches.has("blog/index.html")).toBe(false); expect(matches.has("index.html")).toBe(false); }); it("matches the generated path when pathPrefix does not describe the output", () => { const { matches } = matchPostsToHtmlFiles( ["blog/index.html", "blog/blog/index.html"], [ { filePath: "content/blog/2024-12-17-blog/index.md", slug: "blog", atUri, }, ], { contentDir: "./content", pathPrefix: "/posts" }, ); expect(matches.get("blog/blog/index.html")).toBe(atUri); expect(matches.has("blog/index.html")).toBe(false); }); it("matches the configured pathPrefix path", () => { const { matches } = matchPostsToHtmlFiles( ["posts/my-post/index.html", "my-post/index.html"], [{ filePath: "content/my-post.md", slug: "my-post", atUri }], { contentDir: "./content", pathPrefix: "/posts" }, ); expect(matches.get("posts/my-post/index.html")).toBe(atUri); expect(matches.has("my-post/index.html")).toBe(false); }); it("matches flat HTML files", () => { const { matches } = matchPostsToHtmlFiles( ["posts/my-post.html"], [{ filePath: "content/my-post.md", slug: "my-post", atUri }], { contentDir: "./content", pathPrefix: "/posts" }, ); expect(matches.get("posts/my-post.html")).toBe(atUri); }); it("matches nested slugs", () => { const { matches } = matchPostsToHtmlFiles( ["other/my-other-post/index.html"], [ { filePath: "content/other/my-other-post.md", slug: "other/my-other-post", atUri, }, ], { contentDir: "./content", pathPrefix: "" }, ); expect(matches.get("other/my-other-post/index.html")).toBe(atUri); }); it("falls back to the slug when the output layout is unrelated to the source", () => { const { matches } = matchPostsToHtmlFiles( ["archive/my-post/index.html"], [{ filePath: "content/2024/my-post.md", slug: "archive/my-post", atUri }], { contentDir: "./content" }, ); expect(matches.get("archive/my-post/index.html")).toBe(atUri); }); it("falls back to the last slug segment", () => { const { matches } = matchPostsToHtmlFiles( ["writing/my-post/index.html"], [{ filePath: "content/blog/my-post.md", slug: "blog/my-post", atUri }], { contentDir: "./content", pathPrefix: "" }, ); expect(matches.get("writing/my-post/index.html")).toBe(atUri); }); it("skips files two posts match equally well", () => { const { matches, warnings } = matchPostsToHtmlFiles( ["writing/my-post/index.html"], [ { filePath: "content/blog/my-post.md", slug: "blog/my-post", atUri }, { filePath: "content/notes/my-post.md", slug: "notes/my-post", atUri: otherAtUri, }, ], { contentDir: "./content", pathPrefix: "" }, ); expect(matches.size).toBe(0); expect(warnings).toHaveLength(1); expect(warnings[0]).toContain("writing/my-post/index.html"); }); it("still matches the better of two posts claiming one file", () => { const { matches, warnings } = matchPostsToHtmlFiles( ["blog/my-post/index.html", "notes/my-post/index.html"], [ { filePath: "content/blog/my-post.md", slug: "blog/my-post", atUri }, { filePath: "content/notes/my-post.md", slug: "notes/my-post", atUri: otherAtUri, }, ], { contentDir: "./content", pathPrefix: "" }, ); expect(matches.get("blog/my-post/index.html")).toBe(atUri); expect(matches.get("notes/my-post/index.html")).toBe(otherAtUri); expect(warnings).toHaveLength(0); }); it("falls back to the last slug segment for a flat output layout", () => { const { matches } = matchPostsToHtmlFiles( ["my-post.html"], [{ filePath: "content/blog/my-post.md", slug: "blog/my-post", atUri }], { contentDir: "./content", pathPrefix: "" }, ); expect(matches.get("my-post.html")).toBe(atUri); }); it("matches a templated path the slug alone does not describe", () => { const { matches } = matchPostsToHtmlFiles( ["2024/12/my-post/index.html"], [{ filePath: "content/2024/my-post.md", slug: "my-post", atUri }], { contentDir: "./content", pathPrefix: "/posts", pathTemplate: "{year}/{month}/{slug}", }, ); expect(matches.get("2024/12/my-post/index.html")).toBe(atUri); }); it("matches a root index.html for a top-level page", () => { const { matches } = matchPostsToHtmlFiles( ["index.html", "about/index.html"], [{ filePath: "content/about.md", slug: "about", atUri }], { contentDir: "./content", pathPrefix: "" }, ); expect(matches.get("about/index.html")).toBe(atUri); expect(matches.has("index.html")).toBe(false); }); }); describe("collectPublishedPosts", () => { it("skips unpublished posts and derives a slug from old state entries", () => { const posts = collectPublishedPosts({ posts: { "./content/blog/My-Post.md": { contentHash: "a", atUri }, "./content/blog/other.md": { contentHash: "b", atUri: "at://did:plc:abc123/site.standard.document/other", slug: "blog/other", }, "./content/blog/draft.md": { contentHash: "c" }, }, }); expect(posts).toEqual([ { filePath: "./content/blog/My-Post.md", slug: "my-post", atUri }, { filePath: "./content/blog/other.md", slug: "blog/other", atUri: "at://did:plc:abc123/site.standard.document/other", }, ]); }); });