|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import { afterEach, describe, expect, it } from "vitest"; |
| 4 | +import { |
| 5 | + generateStandardSiteContent, |
| 6 | + getStandardSiteSlug, |
| 7 | +} from "./standard-site-content"; |
| 8 | + |
| 9 | +const tmpDirs: Array<string> = []; |
| 10 | + |
| 11 | +afterEach(async () => { |
| 12 | + await Promise.all( |
| 13 | + tmpDirs |
| 14 | + .splice(0) |
| 15 | + .map((dir) => fs.rm(dir, { recursive: true, force: true })), |
| 16 | + ); |
| 17 | +}); |
| 18 | + |
| 19 | +async function createTmpDir() { |
| 20 | + const dir = await fs.mkdtemp(path.join(process.cwd(), ".tmp-standard-site-")); |
| 21 | + tmpDirs.push(dir); |
| 22 | + return dir; |
| 23 | +} |
| 24 | + |
| 25 | +describe("getStandardSiteSlug", () => { |
| 26 | + it("prefers explicit frontmatter slugs", () => { |
| 27 | + expect( |
| 28 | + getStandardSiteSlug("content/posts/folder/index.mdx", "custom"), |
| 29 | + ).toBe("custom"); |
| 30 | + }); |
| 31 | + |
| 32 | + it("falls back to the parent folder for index.mdx posts", () => { |
| 33 | + expect(getStandardSiteSlug("content/posts/my-post/index.mdx")).toBe( |
| 34 | + "my-post", |
| 35 | + ); |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +describe("generateStandardSiteContent", () => { |
| 40 | + it("writes generated MDX with metadata-only standard.site frontmatter", async () => { |
| 41 | + const rootDir = await createTmpDir(); |
| 42 | + const contentDir = path.join(rootDir, "content/posts"); |
| 43 | + const outputDir = path.join(rootDir, ".sequoia/generated/posts"); |
| 44 | + |
| 45 | + await fs.mkdir(path.join(contentDir, "my-post"), { recursive: true }); |
| 46 | + await fs.writeFile( |
| 47 | + path.join(contentDir, "my-post/index.mdx"), |
| 48 | + `--- |
| 49 | +title: My Post |
| 50 | +description: A useful post |
| 51 | +date: 2026-05-29 |
| 52 | +slug: my-custom-post |
| 53 | +tags: |
| 54 | + - React |
| 55 | + - TypeScript |
| 56 | +--- |
| 57 | +
|
| 58 | +import Aside from '@components/Aside' |
| 59 | +
|
| 60 | +<Aside>Implementation detail</Aside> |
| 61 | +
|
| 62 | +Article source |
| 63 | +`, |
| 64 | + ); |
| 65 | + |
| 66 | + const result = await generateStandardSiteContent({ |
| 67 | + contentDir, |
| 68 | + outputDir, |
| 69 | + }); |
| 70 | + |
| 71 | + expect(result).toEqual([ |
| 72 | + { |
| 73 | + canonicalPath: "/blog/my-custom-post", |
| 74 | + coverImage: "my-custom-post.png", |
| 75 | + slug: "my-custom-post", |
| 76 | + title: "My Post", |
| 77 | + }, |
| 78 | + ]); |
| 79 | + |
| 80 | + const generated = await fs.readFile( |
| 81 | + path.join(outputDir, "my-custom-post/index.mdx"), |
| 82 | + "utf-8", |
| 83 | + ); |
| 84 | + |
| 85 | + expect(generated).toContain("title: My Post"); |
| 86 | + expect(generated).toContain("description: A useful post"); |
| 87 | + expect(generated).toContain("date: 2026-05-29"); |
| 88 | + expect(generated).toContain("ogImage: my-custom-post.png"); |
| 89 | + expect(generated).toContain("slug: my-custom-post"); |
| 90 | + expect(generated).toContain("- React"); |
| 91 | + expect(generated).toContain( |
| 92 | + "Source metadata is generated for standard.site publishing.", |
| 93 | + ); |
| 94 | + }); |
| 95 | +}); |
0 commit comments