import { describe, it, expect } from "vitest"; import { applySidebarLayout } from "./sidebarLayout"; import type { SidebarLink } from "./aclSidebar"; // Spec (module-id-category-order): the sidebar follows the aclMenu tree as given (already in id // order); the portal keeps no module order of its own. It only applies presentation merges // (Inventory, Languages, Locations & Warehouses) and never grants access. const link = (name: string, url = `/${name.toLowerCase()}`, children: SidebarLink[] = []): SidebarLink => ({ name, url, children, }); const group = (name: string, children: SidebarLink[]): SidebarLink => ({ name, url: "", children }); /** Names only, groups as "GROUP[child,child]", for readable assertions. */ function shape(links: SidebarLink[]): string[] { return links.map((l) => (l.url === "" ? `${l.name}[${l.children.map((c) => c.name).join(",")}]` : l.name)); } describe("applySidebarLayout", () => { it("keeps the tree's order and grouping as given, with no portal-side reordering", () => { const input = [ link("BO_REPORTS"), link("BO_DASHBOARD", "/"), group("BO_NAV_MEMBERS", [link("BO_KYC"), link("BO_MEMBERS")]), group("BO_NAV_SALES_ORDERS", [link("BO_REFUND_REQUESTS"), link("BO_WEB_ORDERS")]), ]; expect(shape(applySidebarLayout(input))).toEqual([ "BO_REPORTS", "BO_DASHBOARD", "BO_NAV_MEMBERS[BO_KYC,BO_MEMBERS]", "BO_NAV_SALES_ORDERS[BO_REFUND_REQUESTS,BO_WEB_ORDERS]", ]); }); it("renders a url-less module row as its own group, not regrouped under a portal label", () => { const out = applySidebarLayout([ group("BO_SYSTEM", [link("BO_MESSAGES"), link("Bulletins")]), group("BO_ADMINISTRATION", [link("BO_USERS")]), ]); expect(shape(out)).toEqual(["BO_SYSTEM[BO_NAV_LANGUAGES,Bulletins]", "BO_ADMINISTRATION[BO_USERS]"]); }); it("keeps a group's own name and icon", () => { const g: SidebarLink = { name: "BO_NAV_MEMBERS", url: "", iconName: "Users", children: [link("BO_MEMBERS")] }; expect(applySidebarLayout([g])).toEqual([g]); }); it("shows a module the layout never heard of at its tree position, with no Other group", () => { const out = applySidebarLayout([ group("BO_NAV_MEMBERS", [link("BO_MEMBERS"), link("BO_KYC"), link("BO_NEW_THING")]), link("BO_COUNTER"), ]); expect(shape(out)).toEqual(["BO_NAV_MEMBERS[BO_MEMBERS,BO_KYC,BO_NEW_THING]", "BO_COUNTER"]); expect(out.map((l) => l.name)).not.toContain("BO_NAV_OTHER"); }); it("never adds a module the aclMenu did not grant", () => { const out = applySidebarLayout([link("BO_DASHBOARD", "/"), group("BO_NAV_SALES_ORDERS", [link("BO_WEB_ORDERS")])]); expect(shape(out)).toEqual(["BO_DASHBOARD", "BO_NAV_SALES_ORDERS[BO_WEB_ORDERS]"]); }); it("keeps a linkable module's own nested children intact", () => { const reports = link("BO_REPORTS", "/reports", [link("Sales", "/reports/sales")]); expect(applySidebarLayout([reports])).toEqual([reports]); }); it("returns no entries for an empty input", () => { expect(applySidebarLayout([])).toEqual([]); }); it("keeps the Event group before Sales & Orders as the tree orders them", () => { const out = applySidebarLayout([ group("BO_NAV_EVENTS", [link("BO_EVENTS"), link("BO_POS"), link("BO_EVENT_SUMMARY")]), group("BO_NAV_SALES_ORDERS", [link("BO_WEB_ORDERS")]), ]); expect(shape(out)).toEqual([ "BO_NAV_EVENTS[BO_EVENTS,BO_POS,BO_EVENT_SUMMARY]", "BO_NAV_SALES_ORDERS[BO_WEB_ORDERS]", ]); }); it("renders Modules inside Administration, not Settings, as the tree places it", () => { const out = applySidebarLayout([ group("BO_SYSTEM", [link("BO_PAYMENT_METHODS")]), group("BO_ADMINISTRATION", [link("BO_USERS"), link("BO_LOGIN_SECURITY"), link("BO_MODULES")]), ]); expect(shape(out)).toEqual([ "BO_SYSTEM[BO_PAYMENT_METHODS]", "BO_ADMINISTRATION[BO_USERS,BO_LOGIN_SECURITY,BO_MODULES]", ]); }); // Every live module name placed in its tree group: nothing may be dropped, and no Other group. it("reaches every granted module exactly once across a full live-shaped tree", () => { const inventory = ["BO_STOCK_ON_HAND", "BO_MOVEMENT_HISTORY", "BO_STOCK_IN", "BO_STOCK_TRANSFER", "BO_STOCK_ADJUSTMENT", "BO_STOCK_COUNT"]; const tree = [ link("BO_DASHBOARD", "/"), link("BO_REPORTS"), group("BO_NAV_EVENTS", ["BO_EVENTS", "BO_POS", "BO_EVENT_SUMMARY"].map((n) => link(n))), link("BO_COUNTER"), group("BO_NAV_MEMBERS", ["BO_MEMBERS", "BO_KYC", "BO_NETWORK", "BO_RANK_REFERRAL", "BO_WALLET", "BO_MEMBER_MESSAGES"].map((n) => link(n))), group("BO_NAV_SALES_ORDERS", ["BO_WEB_ORDERS", "BO_REFUND_REQUESTS", "BO_WARRANTY_CLAIMS", "BO_DELIVERY_ORDERS", "BO_ONLINE_PAYMENTS"].map((n) => link(n))), group("BO_NAV_INVENTORY", inventory.map((n) => link(n))), link("BO_PROMOTIONS"), link("BO_GIFT_CARDS"), group("BO_CATALOG", ["BO_PRODUCTS", "BO_COLLECTIONS", "BO_PRODUCT_REVIEWS", "BO_CATALOG_SETUP", "Banners"].map((n) => link(n))), group("BO_NAV_ONLINE_STORE", ["BO_PAGES", "BO_FORMS", "BO_MENUS", "BO_SHIPPING"].map((n) => link(n))), group("BO_NAV_COMMUNICATIONS", ["Bulletins", "BO_NOTIFICATIONS", "BO_EMAIL_TEMPLATES"].map((n) => link(n))), group("BO_SYSTEM", ["BO_PAYMENT_METHODS", "BO_Currency", "Locations & Warehouses", "BO_WAREHOUSE_SETUP", "BO_LOCALES", "BO_MESSAGES", "BO_REFERENCE_DATA"].map((n) => link(n))), group("BO_ADMINISTRATION", ["BO_USERS", "BO_ROLES", "BO_LOGIN_SECURITY", "BO_MODULES"].map((n) => link(n))), ]; expect(shape(applySidebarLayout(tree))).toEqual([ "BO_DASHBOARD", "BO_REPORTS", "BO_NAV_EVENTS[BO_EVENTS,BO_POS,BO_EVENT_SUMMARY]", "BO_COUNTER", "BO_NAV_MEMBERS[BO_MEMBERS,BO_KYC,BO_NETWORK,BO_RANK_REFERRAL,BO_WALLET,BO_MEMBER_MESSAGES]", "BO_NAV_SALES_ORDERS[BO_WEB_ORDERS,BO_REFUND_REQUESTS,BO_WARRANTY_CLAIMS,BO_DELIVERY_ORDERS,BO_ONLINE_PAYMENTS]", "BO_NAV_INVENTORY", "BO_PROMOTIONS", "BO_GIFT_CARDS", "BO_CATALOG[BO_PRODUCTS,BO_COLLECTIONS,BO_PRODUCT_REVIEWS,BO_CATALOG_SETUP,Banners]", "BO_NAV_ONLINE_STORE[BO_PAGES,BO_FORMS,BO_MENUS,BO_SHIPPING]", "BO_NAV_COMMUNICATIONS[Bulletins,BO_NOTIFICATIONS,BO_EMAIL_TEMPLATES]", "BO_SYSTEM[BO_PAYMENT_METHODS,BO_Currency,BO_NAV_LOCATIONS_WAREHOUSES,BO_NAV_LANGUAGES,BO_REFERENCE_DATA]", "BO_ADMINISTRATION[BO_USERS,BO_ROLES,BO_LOGIN_SECURITY,BO_MODULES]", ]); }); // Merged entries stay (consolidate-admin-settings-nav): paired pages share one sidebar entry. describe("merged entries", () => { const system = (out: SidebarLink[]) => out.find((l) => l.name === "BO_SYSTEM")?.children ?? []; it("shows one Languages entry linking to the first granted module, active on the other", () => { const out = applySidebarLayout([group("BO_SYSTEM", [link("BO_LOCALES", "/locales"), link("BO_MESSAGES", "/messages")])]); expect(system(out)).toEqual([ { name: "BO_NAV_LANGUAGES", url: "/locales", children: [], activeUrls: ["/messages"] }, ]); }); it("falls back to the second module when the first is not granted", () => { const out = applySidebarLayout([group("BO_SYSTEM", [link("BO_MESSAGES", "/messages")])]); expect(system(out)).toEqual([{ name: "BO_NAV_LANGUAGES", url: "/messages", children: [], activeUrls: [] }]); }); it("keeps the first granted module's icon", () => { const out = applySidebarLayout([ group("BO_SYSTEM", [{ name: "BO_WAREHOUSE_SETUP", url: "/warehouses", iconName: "Warehouse", children: [] }]), ]); expect(system(out)[0]).toMatchObject({ name: "BO_NAV_LOCATIONS_WAREHOUSES", url: "/warehouses", iconName: "Warehouse" }); }); it("merges Locations and Warehouse Setup into one entry", () => { const out = applySidebarLayout([ group("BO_SYSTEM", [link("Locations & Warehouses", "/locations"), link("BO_WAREHOUSE_SETUP", "/warehouses")]), ]); expect(system(out)).toEqual([ { name: "BO_NAV_LOCATIONS_WAREHOUSES", url: "/locations", children: [], activeUrls: ["/warehouses"] }, ]); }); it("places the merged entry at the first present member's position", () => { const out = applySidebarLayout([ group("BO_SYSTEM", [ link("BO_PAYMENT_METHODS"), link("BO_LOCALES", "/locales"), link("BO_Currency"), link("BO_MESSAGES", "/messages"), link("BO_REFERENCE_DATA"), ]), ]); expect(system(out).map((l) => l.name)).toEqual([ "BO_PAYMENT_METHODS", "BO_NAV_LANGUAGES", "BO_Currency", "BO_REFERENCE_DATA", ]); }); it("hides Locations & Warehouses when neither member is granted", () => { const out = applySidebarLayout([group("BO_SYSTEM", [link("BO_LOCALES", "/locales")])]); expect(shape(out)).toEqual(["BO_SYSTEM[BO_NAV_LANGUAGES]"]); expect(JSON.stringify(out)).not.toContain("BO_NAV_LOCATIONS_WAREHOUSES"); }); }); // merge-inventory-page: a group of only inventory modules is one top-level Inventory link. describe("Inventory", () => { it("renders one Inventory link to /inventory in the group's place for a single granted module", () => { const out = applySidebarLayout([ link("BO_REPORTS"), group("BO_NAV_INVENTORY", [link("BO_STOCK_IN", "/inventory/stock-in")]), link("BO_PROMOTIONS"), ]); expect(shape(out)).toEqual(["BO_REPORTS", "BO_NAV_INVENTORY", "BO_PROMOTIONS"]); expect(out[1]).toMatchObject({ name: "BO_NAV_INVENTORY", url: "/inventory", iconName: "Box", exact: true, children: [] }); expect(JSON.stringify(out)).not.toContain("BO_STOCK_IN"); }); it("collapses all granted inventory modules into the one link", () => { const out = applySidebarLayout([ group("BO_NAV_INVENTORY", [ link("BO_STOCK_ON_HAND"), link("BO_MOVEMENT_HISTORY"), link("BO_STOCK_IN"), link("BO_STOCK_TRANSFER"), link("BO_STOCK_ADJUSTMENT"), link("BO_STOCK_COUNT"), ]), ]); expect(out).toHaveLength(1); expect(out[0]).toMatchObject({ name: "BO_NAV_INVENTORY", url: "/inventory", exact: true, children: [] }); }); it("renders no Inventory link when no inventory module is granted", () => { expect(shape(applySidebarLayout([link("BO_MEMBERS")]))).toEqual(["BO_MEMBERS"]); }); }); });