| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- /* @vitest-environment node */
- import { describe, it, expect } from "vitest";
- import { ApiClientError } from "@/lib/frontend/apiClient";
- import { SEARCH_SCOPE } from "@/lib/frontend/search/urlState";
- import { buildSearchApiInput } from "./searchApiInput.js";
- 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" });
- });
- it("includes from/to when valid", () => {
- const { input, error } = buildSearchApiInput({
- urlState: {
- q: "x",
- scope: SEARCH_SCOPE.SINGLE,
- branch: "NL01",
- branches: [],
- from: "2025-12-01",
- to: "2025-12-31",
- },
- routeBranch: "NL01",
- user: { role: "admin", branchId: null },
- });
- expect(error).toBe(null);
- expect(input).toEqual({
- q: "x",
- limit: 100,
- branch: "NL01",
- from: "2025-12-01",
- to: "2025-12-31",
- });
- });
- it("accepts from===to as a valid single-day search", () => {
- const { input, error } = buildSearchApiInput({
- urlState: {
- q: "x",
- scope: SEARCH_SCOPE.SINGLE,
- branch: "NL01",
- branches: [],
- from: "2025-12-01",
- to: "2025-12-01",
- },
- routeBranch: "NL01",
- user: { role: "admin", branchId: null },
- });
- expect(error).toBe(null);
- expect(input).toEqual({
- q: "x",
- limit: 100,
- branch: "NL01",
- from: "2025-12-01",
- to: "2025-12-01",
- });
- });
- it("returns a local validation error when from > to (no request should be sent)", () => {
- const { input, error } = buildSearchApiInput({
- urlState: {
- q: "x",
- scope: SEARCH_SCOPE.SINGLE,
- branch: "NL01",
- branches: [],
- from: "2025-12-31",
- to: "2025-12-01",
- },
- routeBranch: "NL01",
- user: { role: "admin", branchId: null },
- });
- expect(input).toBe(null);
- expect(error).toBeInstanceOf(ApiClientError);
- expect(error.code).toBe("VALIDATION_SEARCH_RANGE");
- });
- it("returns a local validation error for invalid date format", () => {
- const { input, error } = buildSearchApiInput({
- urlState: {
- q: "x",
- scope: SEARCH_SCOPE.SINGLE,
- branch: "NL01",
- branches: [],
- from: "2025/12/01",
- to: null,
- },
- routeBranch: "NL01",
- user: { role: "admin", branchId: null },
- });
- expect(input).toBe(null);
- expect(error).toBeInstanceOf(ApiClientError);
- expect(error.code).toBe("VALIDATION_SEARCH_DATE");
- });
- });
|