| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- /* @vitest-environment node */
- import { describe, it, expect } from "vitest";
- import { buildSearchApiInput } from "./searchApiInput.js";
- import { SEARCH_SCOPE } from "@/lib/frontend/search/urlState";
- describe("lib/frontend/search/searchApiInput", () => {
- it("returns {input:null} when q is missing", () => {
- const { input, error } = buildSearchApiInput({
- urlState: {
- q: null,
- scope: SEARCH_SCOPE.SINGLE,
- branch: "NL01",
- branches: [],
- from: null,
- to: null,
- },
- routeBranch: "NL01",
- user: { role: "admin", branchId: null },
- });
- expect(input).toBe(null);
- expect(error).toBe(null);
- });
- it("branch users always use routeBranch (single) and ignore scope", () => {
- const { input, error } = buildSearchApiInput({
- urlState: {
- q: "x",
- scope: SEARCH_SCOPE.ALL,
- branch: "NL99",
- branches: ["NL06"],
- from: null,
- to: null,
- },
- routeBranch: "NL01",
- user: { role: "branch", branchId: "NL01" },
- });
- expect(error).toBe(null);
- expect(input).toEqual({ q: "x", limit: 100, branch: "NL01" });
- });
- it("admin/dev: ALL uses scope=all", () => {
- const { input } = buildSearchApiInput({
- urlState: {
- q: "x",
- scope: SEARCH_SCOPE.ALL,
- branch: null,
- branches: [],
- from: null,
- to: null,
- },
- routeBranch: "NL01",
- user: { role: "admin", branchId: null },
- });
- expect(input).toEqual({ q: "x", limit: 100, scope: "all" });
- });
- it("admin/dev: MULTI uses scope=multi + branches", () => {
- const { input, error } = buildSearchApiInput({
- urlState: {
- q: "x",
- scope: SEARCH_SCOPE.MULTI,
- branch: null,
- branches: ["NL06", "NL20"],
- from: null,
- to: null,
- },
- routeBranch: "NL01",
- user: { role: "dev", branchId: null },
- cursor: "abc",
- limit: 100,
- });
- expect(error).toBe(null);
- expect(input).toEqual({
- q: "x",
- limit: 100,
- cursor: "abc",
- scope: "multi",
- branches: ["NL06", "NL20"],
- });
- });
- it("admin/dev: MULTI without branches returns a validation ApiClientError", () => {
- const { input, error } = buildSearchApiInput({
- urlState: {
- q: "x",
- scope: SEARCH_SCOPE.MULTI,
- branch: null,
- branches: [],
- from: null,
- to: null,
- },
- routeBranch: "NL01",
- user: { role: "admin", branchId: null },
- });
- expect(input).toBe(null);
- expect(error).toMatchObject({
- name: "ApiClientError",
- code: "VALIDATION_SEARCH_BRANCHES",
- status: 400,
- });
- });
- it("admin/dev: SINGLE uses routeBranch as branch param", () => {
- const { input } = buildSearchApiInput({
- urlState: {
- q: "x",
- scope: SEARCH_SCOPE.SINGLE,
- branch: "NL99",
- branches: [],
- from: null,
- to: null,
- },
- routeBranch: "NL01",
- user: { role: "admin", branchId: null },
- });
- expect(input).toEqual({ q: "x", limit: 100, branch: "NL01" });
- });
- });
|