/* @vitest-environment node */ import { describe, it, expect } from "vitest"; import { resolveOverviewBranchTarget, OVERVIEW_BRANCH_SOURCE, } from "./homeBranchTarget.js"; describe("lib/frontend/overview/homeBranchTarget", () => { it("uses own branch for role=branch", () => { const out = resolveOverviewBranchTarget({ role: "branch", userBranchId: "NL20", routeBranch: "NL01", storedBranch: "NL02", }); expect(out).toEqual({ branch: "NL20", source: OVERVIEW_BRANCH_SOURCE.USER, shouldFetchBranches: false, }); }); it("prefers route branch for admin-like users", () => { const out = resolveOverviewBranchTarget({ role: "admin", routeBranch: "NL11", storedBranch: "NL22", }); expect(out).toEqual({ branch: "NL11", source: OVERVIEW_BRANCH_SOURCE.ROUTE, shouldFetchBranches: false, }); }); it("uses stored branch when no route branch exists", () => { const out = resolveOverviewBranchTarget({ role: "superadmin", routeBranch: null, storedBranch: "NL22", }); expect(out).toEqual({ branch: "NL22", source: OVERVIEW_BRANCH_SOURCE.STORED, shouldFetchBranches: false, }); }); it("falls back to NL01 and signals optional API refinement", () => { const out = resolveOverviewBranchTarget({ role: "dev", routeBranch: null, storedBranch: null, }); expect(out).toEqual({ branch: "NL01", source: OVERVIEW_BRANCH_SOURCE.FALLBACK, shouldFetchBranches: true, }); }); it("falls back to first API branch when NL01 is not available", () => { const out = resolveOverviewBranchTarget({ role: "admin", routeBranch: null, storedBranch: null, availableBranches: ["NL20", "NL06"], }); expect(out).toEqual({ branch: "NL20", source: OVERVIEW_BRANCH_SOURCE.API_FIRST, shouldFetchBranches: false, }); }); it("behaves deterministically for empty and invalid branch lists", () => { const emptyListOut = resolveOverviewBranchTarget({ role: "admin", routeBranch: null, storedBranch: null, availableBranches: [], }); expect(emptyListOut).toEqual({ branch: null, source: OVERVIEW_BRANCH_SOURCE.NONE, shouldFetchBranches: false, }); const invalidListOut = resolveOverviewBranchTarget({ role: "admin", routeBranch: null, storedBranch: null, availableBranches: ["bad", ""], }); expect(invalidListOut).toEqual({ branch: null, source: OVERVIEW_BRANCH_SOURCE.NONE, shouldFetchBranches: false, }); }); });