| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- /* @vitest-environment node */
- import { describe, it, expect } from "vitest";
- import {
- SEARCH_SCOPE,
- DEFAULT_SEARCH_LIMIT,
- } from "@/lib/frontend/search/urlState";
- import { normalizeSearchUrlStateForUser } from "./normalizeState.js";
- describe("lib/frontend/search/normalizeState", () => {
- it("forces SINGLE for branch users on the route branch", () => {
- const normalized = normalizeSearchUrlStateForUser(
- {
- q: "x",
- scope: SEARCH_SCOPE.ALL,
- branch: "NL99",
- branches: ["NL01", "NL02"],
- limit: 200,
- from: null,
- to: null,
- },
- { routeBranch: "NL01", user: { role: "branch", branchId: "NL01" } }
- );
- expect(normalized).toEqual({
- q: "x",
- scope: SEARCH_SCOPE.SINGLE,
- branch: "NL01",
- branches: [],
- limit: 200,
- from: null,
- to: null,
- });
- });
- it("admin/dev: SINGLE uses route branch even if URL carries a different branch", () => {
- const normalized = normalizeSearchUrlStateForUser(
- {
- q: "x",
- scope: SEARCH_SCOPE.SINGLE,
- branch: "NL77",
- branches: [],
- limit: DEFAULT_SEARCH_LIMIT,
- from: null,
- to: null,
- },
- { routeBranch: "NL01", user: { role: "admin", branchId: null } }
- );
- expect(normalized.branch).toBe("NL01");
- expect(normalized.scope).toBe(SEARCH_SCOPE.SINGLE);
- expect(normalized.branches).toEqual([]);
- });
- it("admin/dev: MULTI keeps branches and clears branch", () => {
- const normalized = normalizeSearchUrlStateForUser(
- {
- q: "x",
- scope: SEARCH_SCOPE.MULTI,
- branch: "NL01",
- branches: ["NL06", "NL20"],
- limit: 50,
- from: null,
- to: null,
- },
- { routeBranch: "NL99", user: { role: "dev", branchId: null } }
- );
- expect(normalized.scope).toBe(SEARCH_SCOPE.MULTI);
- expect(normalized.branch).toBe(null);
- expect(normalized.branches).toEqual(["NL06", "NL20"]);
- expect(normalized.limit).toBe(50);
- });
- it("admin/dev: ALL clears branch and branches", () => {
- const normalized = normalizeSearchUrlStateForUser(
- {
- q: "x",
- scope: SEARCH_SCOPE.ALL,
- branch: "NL01",
- branches: ["NL06"],
- limit: 200,
- from: null,
- to: null,
- },
- { routeBranch: "NL99", user: { role: "admin", branchId: null } }
- );
- expect(normalized).toEqual({
- q: "x",
- scope: SEARCH_SCOPE.ALL,
- branch: null,
- branches: [],
- limit: 200,
- from: null,
- to: null,
- });
- });
- it("unknown roles: fail-safe to SINGLE on route branch", () => {
- const normalized = normalizeSearchUrlStateForUser(
- {
- q: "x",
- scope: SEARCH_SCOPE.ALL,
- branch: "NL01",
- branches: ["NL06"],
- limit: 999, // invalid -> normalized to default
- from: null,
- to: null,
- },
- { routeBranch: "NL02", user: { role: "user", branchId: "NL02" } }
- );
- expect(normalized.scope).toBe(SEARCH_SCOPE.SINGLE);
- expect(normalized.branch).toBe("NL02");
- expect(normalized.limit).toBe(DEFAULT_SEARCH_LIMIT);
- });
- });
|