import { describe, expect, it } from "vitest"; import { WORKBENCH_CHAPTER_NO, buildChapterEntries, parseChapterParam, } from "./chapter"; describe("parseChapterParam", () => { it("parses a valid positive chapter number", () => { expect(parseChapterParam("2")).toBe(2); expect(parseChapterParam("17")).toBe(17); }); it("takes the first value when given an array", () => { expect(parseChapterParam(["3", "9"])).toBe(3); }); it("falls back to chapter 1 for missing/invalid/<1 values", () => { expect(parseChapterParam(undefined)).toBe(WORKBENCH_CHAPTER_NO); expect(parseChapterParam("")).toBe(WORKBENCH_CHAPTER_NO); expect(parseChapterParam("abc")).toBe(WORKBENCH_CHAPTER_NO); expect(parseChapterParam("0")).toBe(WORKBENCH_CHAPTER_NO); expect(parseChapterParam("-4")).toBe(WORKBENCH_CHAPTER_NO); }); }); describe("buildChapterEntries", () => { it("dedupes by no and sorts ascending", () => { const out = buildChapterEntries( [ { no: 3, title: "c3" }, { no: 1, title: "c1" }, { no: 3, title: "dup" }, ], 1, ); expect(out.map((e) => e.no)).toEqual([1, 3]); expect(out[1]?.title).toBe("c3"); // first wins on duplicate }); it("always includes the current chapter even if beyond the outline", () => { const out = buildChapterEntries([{ no: 1 }, { no: 2 }], 5); expect(out.map((e) => e.no)).toEqual([1, 2, 5]); }); it("returns just the current chapter when the outline is empty", () => { expect(buildChapterEntries([], 1)).toEqual([{ no: 1 }]); }); it("drops invalid chapter numbers from the outline", () => { const out = buildChapterEntries([{ no: 0 }, { no: -2 }, { no: 2 }], 1); expect(out.map((e) => e.no)).toEqual([1, 2]); }); });