test(wiki): cover index view helpers and curation mapping
Add tests for buildExcerpt (markdown stripping, truncation), filterWikiPages (category/tag/excerpt), sortWikiPages (recent/oldest/title), groupWikiPagesByCategory, collectTagCounts (dedupe/count/sort), selectFeatured (ordering, FEATURED_CAP), CATEGORY_META coverage, and mapWikiPageToEntry (System fallback, curation passthrough).
This commit is contained in:
parent
35bae586b4
commit
eff7aea914
1 changed files with 208 additions and 0 deletions
|
|
@ -19,6 +19,19 @@ import { slugify } from "@/lib/wiki/slugify";
|
|||
import { expandTemplates } from "@/lib/wiki/templates";
|
||||
import { preprocessWikilinks } from "@/lib/wiki/wikilinks";
|
||||
import { prepareMarkdown } from "@/lib/wiki/prepare";
|
||||
import {
|
||||
buildExcerpt,
|
||||
collectTagCounts,
|
||||
FEATURED_CAP,
|
||||
filterWikiPages,
|
||||
groupWikiPagesByCategory,
|
||||
mapWikiPageToEntry,
|
||||
selectFeatured,
|
||||
sortWikiPages,
|
||||
type WikiIndexEntry,
|
||||
} from "@/lib/wiki/indexView";
|
||||
import { CATEGORY_META, WIKI_CATEGORIES } from "@/lib/wiki/categories";
|
||||
import type { WikiPage as WikiPageDocument } from "@/payload-types";
|
||||
|
||||
let payload: Payload;
|
||||
|
||||
|
|
@ -506,4 +519,199 @@ describe("Wiki", () => {
|
|||
expect(await wikiDeleteAccess(superuserUser)).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
const makeEntry = (
|
||||
overrides: Partial<WikiIndexEntry> & Pick<WikiIndexEntry, "id" | "title">,
|
||||
): WikiIndexEntry => ({
|
||||
slug: overrides.title.toLowerCase().replace(/\s+/g, "-"),
|
||||
category: "Lore",
|
||||
tags: [],
|
||||
updatedAt: "2026-01-01T00:00:00.000Z",
|
||||
lastEditorName: "Tester",
|
||||
excerpt: "",
|
||||
featured: false,
|
||||
featuredOrder: null,
|
||||
...overrides,
|
||||
});
|
||||
|
||||
describe("wiki index view helpers", () => {
|
||||
describe("buildExcerpt", () => {
|
||||
it("strips heading and emphasis markers", () => {
|
||||
expect(buildExcerpt("# **Operation** Overlord *plan*")).toBe("Operation Overlord plan");
|
||||
});
|
||||
|
||||
it("keeps link labels and image alt text", () => {
|
||||
expect(buildExcerpt("See  the [Ops](https://example.com) page")).toBe(
|
||||
"See ridge the Ops page",
|
||||
);
|
||||
});
|
||||
|
||||
it("reduces wikilinks to their label and templates to their name", () => {
|
||||
expect(buildExcerpt("Meet [[Captain Hart|Hart]] via {{CallSign}}")).toBe(
|
||||
"Meet Hart via CallSign",
|
||||
);
|
||||
});
|
||||
|
||||
it("drops fenced code blocks and collapses whitespace", () => {
|
||||
expect(buildExcerpt("Intro\n```\nconst x = 1;\n```\nOutro")).toBe("Intro Outro");
|
||||
});
|
||||
|
||||
it("truncates on a word boundary with an ellipsis", () => {
|
||||
const excerpt = buildExcerpt("alpha ".repeat(60), 40);
|
||||
expect(excerpt.endsWith("...")).toBe(true);
|
||||
expect(excerpt.length).toBeLessThanOrEqual(43);
|
||||
expect(excerpt.trimEnd().replace(/\.\.\.$/, "")).toBe("alpha alpha alpha alpha alpha alpha");
|
||||
});
|
||||
|
||||
it("returns short bodies unchanged apart from whitespace collapsing", () => {
|
||||
expect(buildExcerpt("One line\nof prose")).toBe("One line of prose");
|
||||
});
|
||||
});
|
||||
|
||||
describe("filterWikiPages", () => {
|
||||
const pages = [
|
||||
makeEntry({ id: 1, title: "Op Overlord", category: "Campaign", tags: ["Operation"] }),
|
||||
makeEntry({ id: 2, title: "Hart", category: "Characters", excerpt: "squad leader" }),
|
||||
makeEntry({ id: 3, title: "Logistics Guide", category: "Documentation", tags: ["howto"] }),
|
||||
];
|
||||
|
||||
it("filters by category", () => {
|
||||
const result = filterWikiPages(pages, { query: "", tag: "", category: "Campaign" });
|
||||
expect(result.map((page) => page.id)).toEqual([1]);
|
||||
});
|
||||
|
||||
it("matches tags and excerpt text case-insensitively", () => {
|
||||
expect(
|
||||
filterWikiPages(pages, { query: "OPERATION", tag: "", category: "all" }).map((p) => p.id),
|
||||
).toEqual([1]);
|
||||
expect(
|
||||
filterWikiPages(pages, { query: "squad", tag: "", category: "all" }).map((p) => p.id),
|
||||
).toEqual([2]);
|
||||
});
|
||||
|
||||
it("filters by tag substring", () => {
|
||||
expect(
|
||||
filterWikiPages(pages, { query: "", tag: "HOW", category: "all" }).map((p) => p.id),
|
||||
).toEqual([3]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("sortWikiPages", () => {
|
||||
const pages = [
|
||||
makeEntry({ id: 1, title: "Bravo", updatedAt: "2026-01-02T00:00:00.000Z" }),
|
||||
makeEntry({ id: 2, title: "alpha", updatedAt: "2026-01-03T00:00:00.000Z" }),
|
||||
makeEntry({ id: 3, title: "Charlie", updatedAt: "2026-01-01T00:00:00.000Z" }),
|
||||
];
|
||||
|
||||
it("sorts newest-edited first by default", () => {
|
||||
expect(sortWikiPages(pages, "recent").map((p) => p.id)).toEqual([2, 1, 3]);
|
||||
});
|
||||
|
||||
it("sorts oldest-edited first", () => {
|
||||
expect(sortWikiPages(pages, "oldest").map((p) => p.id)).toEqual([3, 1, 2]);
|
||||
});
|
||||
|
||||
it("sorts by title alphabetically regardless of case", () => {
|
||||
expect(sortWikiPages(pages, "title").map((p) => p.title)).toEqual([
|
||||
"alpha",
|
||||
"Bravo",
|
||||
"Charlie",
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("groupWikiPagesByCategory", () => {
|
||||
it("groups in WIKI_CATEGORIES order and omits empty groups", () => {
|
||||
const groups = groupWikiPagesByCategory([
|
||||
makeEntry({ id: 1, title: "Guide", category: "Guides" }),
|
||||
makeEntry({ id: 2, title: "Op", category: "Campaign" }),
|
||||
makeEntry({ id: 3, title: "More", category: "Guides" }),
|
||||
]);
|
||||
expect(groups.map((group) => group.category)).toEqual(["Campaign", "Guides"]);
|
||||
expect(groups[1].pages.map((page) => page.id)).toEqual([1, 3]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("collectTagCounts", () => {
|
||||
it("deduplicates case-insensitively, counts, and sorts alphabetically", () => {
|
||||
const counts = collectTagCounts([
|
||||
makeEntry({ id: 1, title: "A", tags: ["zebra", "Alpha"] }),
|
||||
makeEntry({ id: 2, title: "B", tags: ["ZEBRA", " beta"] }),
|
||||
]);
|
||||
expect(counts).toEqual([
|
||||
{ tag: "Alpha", count: 1 },
|
||||
{ tag: "beta", count: 1 },
|
||||
{ tag: "zebra", count: 2 },
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("selectFeatured", () => {
|
||||
it("orders explicit picks first, then unnumbered by newest, skipping plain pages", () => {
|
||||
const pages = [
|
||||
makeEntry({
|
||||
id: 1,
|
||||
title: "Unnumbered new",
|
||||
featured: true,
|
||||
updatedAt: "2026-01-03T00:00:00.000Z",
|
||||
}),
|
||||
makeEntry({ id: 2, title: "Second", featured: true, featuredOrder: 2 }),
|
||||
makeEntry({ id: 3, title: "First", featured: true, featuredOrder: 1 }),
|
||||
makeEntry({
|
||||
id: 4,
|
||||
title: "Unnumbered old",
|
||||
featured: true,
|
||||
updatedAt: "2026-01-01T00:00:00.000Z",
|
||||
}),
|
||||
makeEntry({ id: 5, title: "Not featured" }),
|
||||
];
|
||||
expect(selectFeatured(pages).map((page) => page.id)).toEqual([3, 2, 1, 4]);
|
||||
});
|
||||
|
||||
it("caps the strip at the configured limit", () => {
|
||||
const many = Array.from({ length: FEATURED_CAP + 2 }, (_, index) =>
|
||||
makeEntry({ id: index + 1, title: `Page ${index}`, featured: true }),
|
||||
);
|
||||
expect(selectFeatured(many)).toHaveLength(FEATURED_CAP);
|
||||
});
|
||||
});
|
||||
|
||||
describe("CATEGORY_META", () => {
|
||||
it("covers every category with a real blurb", () => {
|
||||
for (const category of WIKI_CATEGORIES) {
|
||||
expect(CATEGORY_META[category].length).toBeGreaterThan(10);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("mapWikiPageToEntry", () => {
|
||||
const baseDoc = {
|
||||
id: 1,
|
||||
title: "Doc Page",
|
||||
slug: "doc-page",
|
||||
category: "Documentation",
|
||||
body: "Some **body** text",
|
||||
updatedAt: "2026-01-01T00:00:00.000Z",
|
||||
createdAt: "2026-01-01T00:00:00.000Z",
|
||||
lastEditor: null,
|
||||
};
|
||||
|
||||
it("falls back to System for editor-less Documentation pages and defaults curation", () => {
|
||||
const entry = mapWikiPageToEntry(baseDoc as unknown as WikiPageDocument);
|
||||
expect(entry.lastEditorName).toBe("System");
|
||||
expect(entry.excerpt).toBe("Some body text");
|
||||
expect(entry.featured).toBe(false);
|
||||
expect(entry.featuredOrder).toBeNull();
|
||||
});
|
||||
|
||||
it("reads curation flags through", () => {
|
||||
const entry = mapWikiPageToEntry({
|
||||
...baseDoc,
|
||||
curation: { featured: true, featuredOrder: 2 },
|
||||
} as unknown as WikiPageDocument);
|
||||
expect(entry.featured).toBe(true);
|
||||
expect(entry.featuredOrder).toBe(2);
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
Reference in a new issue