/** * Sidebar order preference tests. Pure module (src/lib/sidebar/order.ts), * no database access needed; lives under tests/int per repo convention for * vitest specs. */ import { describe, expect, it } from "vitest"; import { DEFAULT_SIDEBAR_ORDER, SIDEBAR_SECTION_IDS, isSidebarSectionId, normalizeSidebarOrder, } from "@/lib/sidebar/order"; describe("normalizeSidebarOrder", () => { it("falls back to the default order for non-array input", () => { expect(normalizeSidebarOrder(undefined)).toEqual([...DEFAULT_SIDEBAR_ORDER]); expect(normalizeSidebarOrder(null)).toEqual([...DEFAULT_SIDEBAR_ORDER]); expect(normalizeSidebarOrder("platform")).toEqual([...DEFAULT_SIDEBAR_ORDER]); expect(normalizeSidebarOrder({ 0: "platform" })).toEqual([...DEFAULT_SIDEBAR_ORDER]); }); it("keeps known ids in the user's order (after the pinned Platform section)", () => { const custom = ["personal", "platform", "commerce", "map"]; const result = normalizeSidebarOrder(custom); expect(result.slice(0, 4)).toEqual(["platform", "personal", "commerce", "map"]); }); it("pins Platform to the front even when stored elsewhere or missing", () => { expect(normalizeSidebarOrder(["personal", "platform", "map"])[0]).toBe("platform"); expect(normalizeSidebarOrder([...DEFAULT_SIDEBAR_ORDER].reverse())[0]).toBe("platform"); expect(normalizeSidebarOrder(["commerce", "map"])[0]).toBe("platform"); }); it("drops unknown and duplicate ids", () => { const result = normalizeSidebarOrder([ "platform", "not-a-section", 42, null, "platform", "map", ]); expect(result.filter((id) => id === "platform")).toEqual(["platform"]); expect(result).toContain("map"); expect(result).not.toContain("not-a-section"); }); it("appends missing sections in default order so the list is always complete", () => { const result = normalizeSidebarOrder(["personal"]); expect(result.slice(0, 2)).toEqual(["platform", "personal"]); expect(result.slice(2)).toEqual( DEFAULT_SIDEBAR_ORDER.filter((id) => id !== "platform" && id !== "personal"), ); }); it("always returns every section id exactly once", () => { const inputs: unknown[] = [ [], ["commerce", "commerce"], [...SIDEBAR_SECTION_IDS].reverse(), ["bogus", ...SIDEBAR_SECTION_IDS, "bogus"], ]; for (const input of inputs) { const result = normalizeSidebarOrder(input); expect(result).toHaveLength(SIDEBAR_SECTION_IDS.length); expect(new Set(result).size).toBe(SIDEBAR_SECTION_IDS.length); for (const id of SIDEBAR_SECTION_IDS) { expect(result).toContain(id); } } }); }); describe("isSidebarSectionId", () => { it("accepts only known section ids", () => { expect(isSidebarSectionId("platform")).toBe(true); expect(isSidebarSectionId("personal")).toBe(true); expect(isSidebarSectionId("Platform")).toBe(false); expect(isSidebarSectionId(undefined)).toBe(false); expect(isSidebarSectionId(7)).toBe(false); }); });