Files
co-lite/solvway-admin-portal/app/lib/routeModules.test.ts
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

273 lines
14 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { ROUTE_MODULES, ROUTE_ANY_MODULES, requiredModuleFor, requiredModulesFor } from "./routeModules";
import { INVENTORY_MODULE_NAMES } from "./inventoryModules";
describe("requiredModuleFor", () => {
it("returns the required module name for a gated route", () => {
expect(requiredModuleFor("/members")).toBe(ROUTE_MODULES["/members"]);
expect(requiredModuleFor("/reports")).toBe(ROUTE_MODULES["/reports"]);
expect(requiredModuleFor("/network")).toBe(ROUTE_MODULES["/network"]);
});
// Ruling M1 (add-message-catalog): unlike the demo routes above,
// "/messages" is both enrolled AND enforced (RequireModule wraps its page).
it("returns the required module name for /messages", () => {
expect(requiredModuleFor("/messages")).toBe("BO_MESSAGES");
});
// Task 0 (add-admin-portal-core-functions): useAdminAuth().hasModule()
// matches a flattened aclMenu node by `name` or `url`, never by the `desc`
// display label — so ROUTE_MODULES must point at the seeded `name` values
// (BO_*), or a granted user still sees NotAuthorized.
it("gates by the seeded module name, not the desc label", () => {
// Seeded BO row: { name: "BO_MESSAGES", url: "/messages", desc: "Messages" }.
const flatModules = [{ name: "BO_MESSAGES", url: "/messages" }];
const required = requiredModuleFor("/messages"); // must be "BO_MESSAGES"
expect(required).toBe("BO_MESSAGES");
expect(flatModules.some((m) => m.name === required)).toBe(true); // fails today ("Messages")
});
// M2 (add-admin-portal-bulletins): "/bulletins" is enrolled AND enforced, and the
// required module is the seeded module `name` column value ("Bulletins", row id
// 41000000) — not a desc label — because hasModule matches name/url.
it("gates /bulletins behind the seeded Bulletins module name", () => {
expect(requiredModuleFor("/bulletins")).toBe("Bulletins");
});
// M3 (add-admin-locations-warehouses): the gate maps to the seeded module
// NAME ('Locations & Warehouses'), never a desc label.
it("returns the seeded module name for /locations", () => {
expect(requiredModuleFor("/locations")).toBe("Locations & Warehouses");
});
// M4 (add-admin-i18n-currency, Task 9): the module was seeded/renamed to
// 'BO_Currency' because the plain name 'Currency' collides with a legacy
// Area-A module — the gate must match the actual seeded `name` value or
// hasModule() never matches and everyone (including super-admin) sees
// NotAuthorised (live-verification fix, fix-currency-gate-and-type-labels).
it("returns the required module name for /currency", () => {
expect(requiredModuleFor("/currency")).toBe("BO_Currency");
});
// M5 (add-admin-email-templates, Task 9): the seeded Area-'BO' module row is
// named 'BO_EMAIL_TEMPLATES' (id 41200000) — the gate must use that `name`
// column value, never the `desc` label, or hasModule() never matches.
it("returns the required module name for /email-templates", () => {
expect(requiredModuleFor("/email-templates")).toBe("BO_EMAIL_TEMPLATES");
});
// M6 (add-admin-internal-notifications): the seeded module row is
// 'BO_NOTIFICATIONS' (id 41300000, url /notifications) — the gate must use
// the seeded module `name` column, never the 'Notifications' desc label.
it("gates /notifications behind the seeded BO_NOTIFICATIONS module name", () => {
expect(requiredModuleFor("/notifications")).toBe("BO_NOTIFICATIONS");
});
// M7 (add-membership-wallet-foundation, increment I1): the seeded module row
// is 'BO_WALLET' (id 41400000, url /wallet) — the gate must use the seeded
// module `name` column, never the 'Wallet' desc label.
it("gates /wallet behind the seeded BO_WALLET module name", () => {
expect(requiredModuleFor("/wallet")).toBe("BO_WALLET");
});
// M8 (add-admin-login-security): the seeded module row is 'BO_LOGIN_SECURITY'
// (id 41500000, url /login-security) — the gate must use the seeded module
// `name` column, never the 'Login Security' desc label.
it("gates /login-security behind the seeded BO_LOGIN_SECURITY module name", () => {
expect(requiredModuleFor("/login-security")).toBe("BO_LOGIN_SECURITY");
expect(ROUTE_MODULES["/login-security"]).toBe("BO_LOGIN_SECURITY");
});
// M9 (add-membership-wallet-topup, increment I2): the top-up approval queue
// lives under the wallet module, so /wallet/topups is gated by BO_WALLET.
it("gates /wallet/topups behind the seeded BO_WALLET module name", () => {
expect(requiredModuleFor("/wallet/topups")).toBe("BO_WALLET");
expect(ROUTE_MODULES["/wallet/topups"]).toBe("BO_WALLET");
});
// M10 (add-admin-locale-management): the seeded module row is 'BO_LOCALES'
// (id 41600000, url /locales) — the gate must use the seeded module `name`
// column, never the 'Locales (UI languages)' desc label. The /locales page is
// wrapped in RequireModule, so a user without the grant gets the
// not-authorised state; super-admin bypasses module-role gating in code.
it("gates /locales behind the seeded BO_LOCALES module name", () => {
expect(requiredModuleFor("/locales")).toBe("BO_LOCALES");
expect(ROUTE_MODULES["/locales"]).toBe("BO_LOCALES");
});
// M11 (add-admin-module-node-crud): the seeded module row is 'BO_MODULES'
// (id 41700000, url /modules) — the gate must use the seeded module `name`
// column, never the 'Module tree management' desc label. The /modules page is
// wrapped in RequireModule; the page manages the tree it lives in.
it("gates /modules behind the seeded BO_MODULES module name", () => {
expect(requiredModuleFor("/modules")).toBe("BO_MODULES");
expect(ROUTE_MODULES["/modules"]).toBe("BO_MODULES");
});
// M12 (add-admin-reference-data): the seeded module row is 'BO_REFERENCE_DATA'
// (id 41800000, url /reference) — the gate must use that seeded `name` value,
// never the 'Reference Data' desc label. The /reference page is wrapped in
// RequireModule; super-admin bypasses module-role gating in code.
it("gates /reference behind the seeded BO_REFERENCE_DATA module name", () => {
expect(requiredModuleFor("/reference")).toBe("BO_REFERENCE_DATA");
expect(ROUTE_MODULES["/reference"]).toBe("BO_REFERENCE_DATA");
});
// Task D (add-admin-catalog-setup): the seeded module row is 'BO_CATALOG_SETUP'
// (id 41900000, url /catalog-setup) — the gate must use that seeded `name`
// value, never a desc label. The /catalog-setup page is wrapped in
// RequireModule; super-admin bypasses module-role gating in code.
it("gates /catalog-setup behind the seeded BO_CATALOG_SETUP module name", () => {
expect(requiredModuleFor("/catalog-setup")).toBe("BO_CATALOG_SETUP");
expect(ROUTE_MODULES["/catalog-setup"]).toBe("BO_CATALOG_SETUP");
});
// Task E (add-admin-product-setup): the seeded module row is 'BO_PRODUCTS' (id
// 42000000, url /products) under the BO_CATALOG nav group — the gate must use
// that seeded `name` value, never a desc label. The /products page is wrapped
// in RequireModule; super-admin bypasses module-role gating in code.
it("gates /products behind the seeded BO_PRODUCTS module name", () => {
expect(requiredModuleFor("/products")).toBe("BO_PRODUCTS");
expect(ROUTE_MODULES["/products"]).toBe("BO_PRODUCTS");
});
// Task 9 (add-membership-withdrawal, increment I5): the withdrawal approval
// queue lives under the wallet module, so /wallet/withdrawals is gated by
// the same seeded BO_WALLET module name as /wallet and /wallet/topups.
it("gates /wallet/withdrawals behind the seeded BO_WALLET module name", () => {
expect(requiredModuleFor("/wallet/withdrawals")).toBe("BO_WALLET");
expect(ROUTE_MODULES["/wallet/withdrawals"]).toBe("BO_WALLET");
});
// Task 4 (BABYBOO-13, withdrawal-config-and-cycle): the withdrawal settings page is another
// Wallet-tab sub-function, gated by the same seeded BO_WALLET module as its siblings. Asserts
// the map key directly so removing the entry fails this test even though "/wallet" would still
// resolve requiredModuleFor via a prefix fallback.
it("gates /wallet/withdrawal-settings behind the seeded BO_WALLET module name", () => {
expect(requiredModuleFor("/wallet/withdrawal-settings")).toBe("BO_WALLET");
expect(ROUTE_MODULES["/wallet/withdrawal-settings"]).toBe("BO_WALLET");
});
// Task 9 (add-warehouse-setup, BABYBOO-18 increment I1): the seeded module
// row is 'BO_WAREHOUSE_SETUP' (id 42100000, url /warehouses) under the
// System nav group (40460000), mirroring the /locations (M3) convention —
// the gate must use that seeded `name` value, never a desc label.
it("gates /warehouses behind the seeded BO_WAREHOUSE_SETUP module name", () => {
expect(requiredModuleFor("/warehouses")).toBe("BO_WAREHOUSE_SETUP");
expect(ROUTE_MODULES["/warehouses"]).toBe("BO_WAREHOUSE_SETUP");
});
// Part C (add-admin-company-account-crud), simplified by simplify-company-account-nav-gating:
// company-account management is a surface within Network reached from the NetworkTabs sub-nav,
// gated by the SAME BO_NETWORK permission as /network — no separate module.
it("gates /network/company-accounts behind the Network BO_NETWORK module name", () => {
expect(requiredModuleFor("/network/company-accounts")).toBe("BO_NETWORK");
expect(ROUTE_MODULES["/network/company-accounts"]).toBe("BO_NETWORK");
});
// Inventory page gate (merge-inventory-page): the five old routes are removed; /inventory gates
// on any of the five inventory modules; /inventory/delivery-orders is unchanged.
describe("inventory page gate (merge-inventory-page)", () => {
it("requires any one of the five inventory modules for /inventory", () => {
expect([...(requiredModulesFor("/inventory") ?? [])].sort()).toEqual(
["BO_MOVEMENT_HISTORY", "BO_STOCK_ADJUSTMENT", "BO_STOCK_COUNT", "BO_STOCK_COUNT_APPROVE", "BO_STOCK_IN",
"BO_STOCK_ON_HAND", "BO_STOCK_TRANSFER"],
);
expect(ROUTE_ANY_MODULES["/inventory"]).toEqual(INVENTORY_MODULE_NAMES);
});
// add-stock-count-event-closeout: a count detail page needs the count or the approve module.
it("gates /inventory/stock-counts behind either stock count module", () => {
expect(requiredModulesFor("/inventory/stock-counts")).toEqual(["BO_STOCK_COUNT", "BO_STOCK_COUNT_APPROVE"]);
});
it("no longer gates the five old routes (they are redirects now)", () => {
for (const p of ["/inventory/stock-in", "/inventory/stock-on-hand", "/inventory/transfers",
"/inventory/movement-history", "/inventory/adjustments"]) {
expect(requiredModuleFor(p)).toBeUndefined();
expect(requiredModulesFor(p)).toBeUndefined();
}
});
it("wraps a single-module route as a one-element list", () => {
expect(requiredModulesFor("/members")).toEqual(["BO_MEMBERS"]);
expect(requiredModulesFor("/")).toBeUndefined();
});
it("gates the member messaging pages behind BO_MEMBER_MESSAGES", () => { // add-member-messaging
expect(requiredModuleFor("/member-messages")).toBe("BO_MEMBER_MESSAGES");
expect(requiredModuleFor("/member-messages/broadcast")).toBe("BO_MEMBER_MESSAGES");
expect(requiredModulesFor("/member-messages")).toEqual(["BO_MEMBER_MESSAGES"]);
});
it("gates the Rank & referral page behind BO_RANK_REFERRAL", () => { // BABYBOO-43
expect(requiredModuleFor("/rank-referral")).toBe("BO_RANK_REFERRAL");
expect(requiredModulesFor("/rank-referral")).toEqual(["BO_RANK_REFERRAL"]);
});
it("keeps Delivery Orders on its own module", () => {
expect(requiredModuleFor("/inventory/delivery-orders")).toBe("BO_DELIVERY_ORDERS");
});
});
// Task 12 (add-homepage-banners): the seeded module row's `name` column is
// 'Banners' (mirroring /bulletins' "Bulletins" convention) — the gate must
// use that seeded `name` value, never a desc label. The /banners page is
// wrapped in RequireModule; super-admin bypasses module-role gating in code.
it("gates /banners behind the seeded Banners module name", () => {
expect(requiredModuleFor("/banners")).toBe("Banners");
expect(ROUTE_MODULES["/banners"]).toBe("Banners");
});
// (add-branch-counter-sale): the seeded module row is 'BO_COUNTER' (url
// /pos/counter, icon 'Store') for the branch counter (recorded-tender, no-event) POS page.
// The gate must use that seeded `name` value, never a desc label. The /pos/counter page is
// wrapped in RequireModule; super-admin bypasses module-role gating in code.
it("gates /pos/counter behind the seeded BO_COUNTER module name", () => {
expect(requiredModuleFor("/pos/counter")).toBe("BO_COUNTER");
expect(ROUTE_MODULES["/pos/counter"]).toBe("BO_COUNTER");
});
it("returns undefined for an ungated route", () => {
expect(requiredModuleFor("/")).toBeUndefined();
expect(requiredModuleFor("/login")).toBeUndefined();
expect(requiredModuleFor("/some/unknown/route")).toBeUndefined();
});
});
describe("add-storefront-menus", () => {
it("gates /menus on BO_MENUS", () => {
expect(requiredModuleFor("/menus")).toBe("BO_MENUS");
});
});
describe("add-cms-forms", () => {
it("gates /forms on BO_FORMS (new, edit and submissions pages share the gate)", () => {
expect(requiredModuleFor("/forms")).toBe("BO_FORMS");
});
});
describe("add-cms-pages", () => {
it("gates /pages on BO_PAGES (the new and edit pages gate on the same pathname)", () => {
expect(requiredModuleFor("/pages")).toBe("BO_PAGES");
});
it("gates /events/summary behind BO_EVENT_SUMMARY, apart from /events", () => { // add-event-summary
expect(requiredModuleFor("/events/summary")).toBe("BO_EVENT_SUMMARY");
expect(requiredModuleFor("/events")).toBe("BO_EVENTS");
});
});
describe("add-shipping-rates", () => {
it("gates /shipping on BO_SHIPPING", () => {
expect(requiredModuleFor("/shipping")).toBe("BO_SHIPPING");
});
});
describe("babyboo-56-warranty-claims", () => {
it("gates /orders/warranty-claims on BO_WARRANTY_CLAIMS, apart from /orders/refunds", () => {
expect(requiredModuleFor("/orders/warranty-claims")).toBe("BO_WARRANTY_CLAIMS");
expect(requiredModuleFor("/orders/refunds")).toBe("BO_REFUND_REQUESTS");
});
});