searchApiInput.test.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* @vitest-environment node */
  2. import { describe, it, expect } from "vitest";
  3. import { buildSearchApiInput } from "./searchApiInput.js";
  4. import { SEARCH_SCOPE } from "@/lib/frontend/search/urlState";
  5. describe("lib/frontend/search/searchApiInput", () => {
  6. it("returns {input:null} when q is missing", () => {
  7. const { input, error } = buildSearchApiInput({
  8. urlState: {
  9. q: null,
  10. scope: SEARCH_SCOPE.SINGLE,
  11. branch: "NL01",
  12. branches: [],
  13. from: null,
  14. to: null,
  15. },
  16. routeBranch: "NL01",
  17. user: { role: "admin", branchId: null },
  18. });
  19. expect(input).toBe(null);
  20. expect(error).toBe(null);
  21. });
  22. it("branch users always use routeBranch (single) and ignore scope", () => {
  23. const { input, error } = buildSearchApiInput({
  24. urlState: {
  25. q: "x",
  26. scope: SEARCH_SCOPE.ALL,
  27. branch: "NL99",
  28. branches: ["NL06"],
  29. from: null,
  30. to: null,
  31. },
  32. routeBranch: "NL01",
  33. user: { role: "branch", branchId: "NL01" },
  34. });
  35. expect(error).toBe(null);
  36. expect(input).toEqual({ q: "x", limit: 100, branch: "NL01" });
  37. });
  38. it("admin/dev: ALL uses scope=all", () => {
  39. const { input } = buildSearchApiInput({
  40. urlState: {
  41. q: "x",
  42. scope: SEARCH_SCOPE.ALL,
  43. branch: null,
  44. branches: [],
  45. from: null,
  46. to: null,
  47. },
  48. routeBranch: "NL01",
  49. user: { role: "admin", branchId: null },
  50. });
  51. expect(input).toEqual({ q: "x", limit: 100, scope: "all" });
  52. });
  53. it("admin/dev: MULTI uses scope=multi + branches", () => {
  54. const { input, error } = buildSearchApiInput({
  55. urlState: {
  56. q: "x",
  57. scope: SEARCH_SCOPE.MULTI,
  58. branch: null,
  59. branches: ["NL06", "NL20"],
  60. from: null,
  61. to: null,
  62. },
  63. routeBranch: "NL01",
  64. user: { role: "dev", branchId: null },
  65. cursor: "abc",
  66. limit: 100,
  67. });
  68. expect(error).toBe(null);
  69. expect(input).toEqual({
  70. q: "x",
  71. limit: 100,
  72. cursor: "abc",
  73. scope: "multi",
  74. branches: ["NL06", "NL20"],
  75. });
  76. });
  77. it("admin/dev: MULTI without branches returns a validation ApiClientError", () => {
  78. const { input, error } = buildSearchApiInput({
  79. urlState: {
  80. q: "x",
  81. scope: SEARCH_SCOPE.MULTI,
  82. branch: null,
  83. branches: [],
  84. from: null,
  85. to: null,
  86. },
  87. routeBranch: "NL01",
  88. user: { role: "admin", branchId: null },
  89. });
  90. expect(input).toBe(null);
  91. expect(error).toMatchObject({
  92. name: "ApiClientError",
  93. code: "VALIDATION_SEARCH_BRANCHES",
  94. status: 400,
  95. });
  96. });
  97. it("admin/dev: SINGLE uses routeBranch as branch param", () => {
  98. const { input } = buildSearchApiInput({
  99. urlState: {
  100. q: "x",
  101. scope: SEARCH_SCOPE.SINGLE,
  102. branch: "NL99",
  103. branches: [],
  104. from: null,
  105. to: null,
  106. },
  107. routeBranch: "NL01",
  108. user: { role: "admin", branchId: null },
  109. });
  110. expect(input).toEqual({ q: "x", limit: 100, branch: "NL01" });
  111. });
  112. });