homeBranchTarget.test.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* @vitest-environment node */
  2. import { describe, it, expect } from "vitest";
  3. import {
  4. resolveOverviewBranchTarget,
  5. OVERVIEW_BRANCH_SOURCE,
  6. } from "./homeBranchTarget.js";
  7. describe("lib/frontend/overview/homeBranchTarget", () => {
  8. it("uses own branch for role=branch", () => {
  9. const out = resolveOverviewBranchTarget({
  10. role: "branch",
  11. userBranchId: "NL20",
  12. routeBranch: "NL01",
  13. storedBranch: "NL02",
  14. });
  15. expect(out).toEqual({
  16. branch: "NL20",
  17. source: OVERVIEW_BRANCH_SOURCE.USER,
  18. shouldFetchBranches: false,
  19. });
  20. });
  21. it("prefers route branch for admin-like users", () => {
  22. const out = resolveOverviewBranchTarget({
  23. role: "admin",
  24. routeBranch: "NL11",
  25. storedBranch: "NL22",
  26. });
  27. expect(out).toEqual({
  28. branch: "NL11",
  29. source: OVERVIEW_BRANCH_SOURCE.ROUTE,
  30. shouldFetchBranches: false,
  31. });
  32. });
  33. it("uses stored branch when no route branch exists", () => {
  34. const out = resolveOverviewBranchTarget({
  35. role: "superadmin",
  36. routeBranch: null,
  37. storedBranch: "NL22",
  38. });
  39. expect(out).toEqual({
  40. branch: "NL22",
  41. source: OVERVIEW_BRANCH_SOURCE.STORED,
  42. shouldFetchBranches: false,
  43. });
  44. });
  45. it("falls back to NL01 and signals optional API refinement", () => {
  46. const out = resolveOverviewBranchTarget({
  47. role: "dev",
  48. routeBranch: null,
  49. storedBranch: null,
  50. });
  51. expect(out).toEqual({
  52. branch: "NL01",
  53. source: OVERVIEW_BRANCH_SOURCE.FALLBACK,
  54. shouldFetchBranches: true,
  55. });
  56. });
  57. it("falls back to first API branch when NL01 is not available", () => {
  58. const out = resolveOverviewBranchTarget({
  59. role: "admin",
  60. routeBranch: null,
  61. storedBranch: null,
  62. availableBranches: ["NL20", "NL06"],
  63. });
  64. expect(out).toEqual({
  65. branch: "NL20",
  66. source: OVERVIEW_BRANCH_SOURCE.API_FIRST,
  67. shouldFetchBranches: false,
  68. });
  69. });
  70. it("behaves deterministically for empty and invalid branch lists", () => {
  71. const emptyListOut = resolveOverviewBranchTarget({
  72. role: "admin",
  73. routeBranch: null,
  74. storedBranch: null,
  75. availableBranches: [],
  76. });
  77. expect(emptyListOut).toEqual({
  78. branch: null,
  79. source: OVERVIEW_BRANCH_SOURCE.NONE,
  80. shouldFetchBranches: false,
  81. });
  82. const invalidListOut = resolveOverviewBranchTarget({
  83. role: "admin",
  84. routeBranch: null,
  85. storedBranch: null,
  86. availableBranches: ["bad", ""],
  87. });
  88. expect(invalidListOut).toEqual({
  89. branch: null,
  90. source: OVERVIEW_BRANCH_SOURCE.NONE,
  91. shouldFetchBranches: false,
  92. });
  93. });
  94. });