/* @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 is treated as not-ready (no error)", () => { 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).toBe(null); }); 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" }); }); });