|
| 1 | +import { assertEquals } from "@std/assert"; |
| 2 | +import BlogPostItem from "../loaders/BlogPostItem.ts"; |
| 3 | +import BlogPostPageLoader from "../loaders/BlogPostPage.ts"; |
| 4 | +import { AppContext } from "../mod.ts"; |
| 5 | +import { BlogPost } from "../types.ts"; |
| 6 | + |
| 7 | +const COLLECTION_PATH = "collections/blog/posts"; |
| 8 | + |
| 9 | +/** |
| 10 | + * Both loaders read records through `getRecordsByPath`, which resolves |
| 11 | + * `{ __resolveType: "resolvables" }` off the context. Stubbing `ctx.get` is |
| 12 | + * enough to drive them without a live deco runtime. |
| 13 | + */ |
| 14 | +const ctxWith = (post: Partial<BlogPost>) => |
| 15 | + ({ |
| 16 | + get: () => |
| 17 | + Promise.resolve({ |
| 18 | + [`${COLLECTION_PATH}/${post.slug}`]: { |
| 19 | + name: `${COLLECTION_PATH}/${post.slug}`, |
| 20 | + post, |
| 21 | + }, |
| 22 | + }), |
| 23 | + }) as unknown as AppContext; |
| 24 | + |
| 25 | +const draft: Partial<BlogPost> = { |
| 26 | + title: "Work in progress", |
| 27 | + excerpt: "Not out yet", |
| 28 | + date: "2026-01-01", |
| 29 | + slug: "wip", |
| 30 | + status: "draft", |
| 31 | +}; |
| 32 | + |
| 33 | +const req = new Request("https://example.com/blog/wip"); |
| 34 | + |
| 35 | +Deno.test("BlogPostItem still serves a draft, marked noIndexing", async () => { |
| 36 | + const post = await BlogPostItem({ slug: "wip" }, req, ctxWith(draft)); |
| 37 | + |
| 38 | + assertEquals(post?.slug, "wip"); |
| 39 | + assertEquals(post?.seo?.noIndexing, true); |
| 40 | +}); |
| 41 | + |
| 42 | +Deno.test("BlogPostItem keeps the draft's other seo fields", async () => { |
| 43 | + const post = await BlogPostItem( |
| 44 | + { slug: "wip" }, |
| 45 | + req, |
| 46 | + ctxWith({ |
| 47 | + ...draft, |
| 48 | + seo: { title: "Custom title", canonical: "https://example.com/canon" }, |
| 49 | + }), |
| 50 | + ); |
| 51 | + |
| 52 | + assertEquals(post?.seo?.title, "Custom title"); |
| 53 | + assertEquals(post?.seo?.canonical, "https://example.com/canon"); |
| 54 | + assertEquals(post?.seo?.noIndexing, true); |
| 55 | +}); |
| 56 | + |
| 57 | +Deno.test("BlogPostItem leaves a published post untouched", async () => { |
| 58 | + const post = await BlogPostItem( |
| 59 | + { slug: "live" }, |
| 60 | + req, |
| 61 | + ctxWith({ ...draft, slug: "live", status: "published" }), |
| 62 | + ); |
| 63 | + |
| 64 | + assertEquals(post?.slug, "live"); |
| 65 | + assertEquals(post?.seo?.noIndexing, undefined); |
| 66 | +}); |
| 67 | + |
| 68 | +Deno.test("BlogPostPage still serves a draft, marked noIndexing", async () => { |
| 69 | + const page = await BlogPostPageLoader({ slug: "wip" }, req, ctxWith(draft)); |
| 70 | + |
| 71 | + assertEquals(page?.post.slug, "wip"); |
| 72 | + assertEquals(page?.seo?.noIndexing, true); |
| 73 | +}); |
| 74 | + |
| 75 | +Deno.test("BlogPostPage keeps the draft's other seo fields", async () => { |
| 76 | + const page = await BlogPostPageLoader( |
| 77 | + { slug: "wip" }, |
| 78 | + req, |
| 79 | + ctxWith({ |
| 80 | + ...draft, |
| 81 | + seo: { title: "Custom title", canonical: "https://example.com/canon" }, |
| 82 | + }), |
| 83 | + ); |
| 84 | + |
| 85 | + assertEquals(page?.seo?.title, "Custom title"); |
| 86 | + assertEquals(page?.seo?.canonical, "https://example.com/canon"); |
| 87 | + assertEquals(page?.seo?.noIndexing, true); |
| 88 | +}); |
| 89 | + |
| 90 | +Deno.test("BlogPostPage leaves a published post indexable", async () => { |
| 91 | + const page = await BlogPostPageLoader( |
| 92 | + { slug: "live" }, |
| 93 | + req, |
| 94 | + ctxWith({ ...draft, slug: "live", status: "published" }), |
| 95 | + ); |
| 96 | + |
| 97 | + assertEquals(page?.seo?.noIndexing, false); |
| 98 | +}); |
| 99 | + |
| 100 | +Deno.test("every non-published status is served but unindexable", async () => { |
| 101 | + for ( |
| 102 | + const status of [ |
| 103 | + "draft", |
| 104 | + "archived", |
| 105 | + "generating", |
| 106 | + "awaiting_review", |
| 107 | + ] as const |
| 108 | + ) { |
| 109 | + const ctx = ctxWith({ ...draft, status }); |
| 110 | + |
| 111 | + const item = await BlogPostItem({ slug: "wip" }, req, ctx); |
| 112 | + assertEquals(item?.slug, "wip", status); |
| 113 | + assertEquals(item?.seo?.noIndexing, true, status); |
| 114 | + |
| 115 | + const page = await BlogPostPageLoader({ slug: "wip" }, req, ctx); |
| 116 | + assertEquals(page?.post.slug, "wip", status); |
| 117 | + assertEquals(page?.seo?.noIndexing, true, status); |
| 118 | + } |
| 119 | +}); |
0 commit comments