Skip to content
File

Blob: tests/sites/json-prewalk.test.ts

typescript74 lines
1import { describe, expect, it } from "vitest";
2 
3import { collectMentionPageIds, preWalkSitesJson, type PreWalkMention } from "@/sites/json-prewalk";
4 
5function mention(pageId: string): { type: "pageMention"; attrs: { pageId: string | null } } {
6 return { type: "pageMention", attrs: { pageId } };
7}
8 
9function image(src: string): { type: "image"; attrs: { src: string } } {
10 return { type: "image", attrs: { src } };
11}
12 
13function mentionsMap(entries: Array<[string, boolean]>): Map<string, PreWalkMention> {
14 return new Map(entries.map(([id, reachable]) => [id, { reachable }]));
15}
16 
17describe("preWalkSitesJson", () => {
18 it("strips pageId on unreachable mentions but keeps reachable ones intact", () => {
19 const root = {
20 type: "doc",
21 content: [{ type: "paragraph", content: [mention("reachable-1"), mention("private-1")] }],
22 };
23 preWalkSitesJson(root, {
24 pageId: "host-page",
25 mentions: mentionsMap([
26 ["reachable-1", true],
27 ["private-1", false],
28 ]),
29 });
30 expect(root.content[0].content?.[0].attrs).toEqual({ pageId: "reachable-1" });
31 expect(root.content[0].content?.[1].attrs).toEqual({ pageId: null });
32 });
33 
34 it("treats absent mention entries as restricted", () => {
35 const root = {
36 type: "doc",
37 content: [{ type: "paragraph", content: [mention("unknown-page")] }],
38 };
39 preWalkSitesJson(root, { pageId: "host", mentions: new Map() });
40 expect(root.content[0].content?.[0].attrs).toEqual({ pageId: null });
41 });
42 
43 it("rewrites /uploads/<id> image src to /_assets/<pageId>/<id>", () => {
44 const root = {
45 type: "doc",
46 content: [image("/uploads/01ABC")],
47 };
48 preWalkSitesJson(root, { pageId: "host-page", mentions: new Map() });
49 expect(root.content[0].attrs?.src).toBe("/_assets/host-page/01ABC");
50 });
51 
52 it("leaves absolute URLs, empty src, and already-rewritten paths alone", () => {
53 const root = {
54 type: "doc",
55 content: [image("https://cdn.example.com/foo.png"), image("/_assets/other/already"), image("")],
56 };
57 preWalkSitesJson(root, { pageId: "host", mentions: new Map() });
58 expect(root.content[0].attrs?.src).toBe("https://cdn.example.com/foo.png");
59 expect(root.content[1].attrs?.src).toBe("/_assets/other/already");
60 expect(root.content[2].attrs?.src).toBe("");
61 });
62 
63 it("collectMentionPageIds dedupes across the document", () => {
64 const root = {
65 type: "doc",
66 content: [
67 { type: "paragraph", content: [mention("a"), mention("b"), mention("a")] },
68 { type: "paragraph", content: [mention("c")] },
69 ],
70 };
71 expect(collectMentionPageIds(root).sort()).toEqual(["a", "b", "c"]);
72 });
73});