import { describe, expect, it } from "vitest"; import { resolveOrgSnapshot } from "@/lib/orgchart-data"; const DIV = "div-1"; const TEAM_A = "team-a"; const TEAM_B = "team-b"; type EmployeeInput = Parameters[0]["employees"][number]; type AssignmentInput = Parameters[0]["assignments"][number]; function emp(id: string, overrides: Partial = {}): EmployeeInput { return { id, personnel_number: 1000, first_name: "Test", last_name: id, job_title: "Mitarbeiter:in", manager_id: null, team_id: TEAM_A, division_id: DIV, is_lead: false, org_level: 3, entry_date: "2020-01-01", exit_date: null, karenz_start_date: null, karenz_return_date: null, absence_type: null, ...overrides, }; } function assignment(employeeId: string, overrides: Partial = {}): AssignmentInput { return { employee_id: employeeId, manager_id: null, team_id: TEAM_A, division_id: DIV, job_title: "Mitarbeiter:in", is_lead: false, org_level: 3, valid_from: "2020-01-01", ...overrides, }; } const TEAMS = [ { id: TEAM_A, department_id: "dept-1" }, { id: TEAM_B, department_id: "dept-2" }, ]; const DEPARTMENTS = [ { id: "dept-1", division_id: DIV }, { id: "dept-2", division_id: "div-2" }, ]; function snapshot(args: Partial[0]> & { asOf: string }) { return resolveOrgSnapshot({ employees: [], assignments: [], teams: TEAMS, departments: DEPARTMENTS, pending: [], ...args }); } describe("membership as of a date", () => { it("excludes someone who had not started yet and includes them once they have", () => { const employees = [emp("a", { entry_date: "2026-06-01" })]; expect(snapshot({ asOf: "2026-05-31", employees }).employees).toHaveLength(0); expect(snapshot({ asOf: "2026-06-01", employees }).employees).toHaveLength(1); }); it("excludes someone from their exit date onwards", () => { const employees = [emp("a", { exit_date: "2026-06-30" })]; expect(snapshot({ asOf: "2026-06-29", employees }).employees).toHaveLength(1); expect(snapshot({ asOf: "2026-06-30", employees }).employees).toHaveLength(0); }); it("keeps someone on Karenz in the chart", () => { const employees = [emp("a", { karenz_start_date: "2026-01-01", karenz_return_date: "2026-12-01" })]; expect(snapshot({ asOf: "2026-06-01", employees }).employees).toHaveLength(1); }); }); describe("placement as of a date", () => { it("uses the assignment interval covering the date, not today's row on employees", () => { const employees = [emp("a", { team_id: TEAM_B, job_title: "Heutiger Titel" })]; const assignments = [assignment("a", { team_id: TEAM_A, job_title: "Damaliger Titel" })]; const [result] = snapshot({ asOf: "2024-03-01", employees, assignments }).employees; expect(result.team_id).toBe(TEAM_A); expect(result.job_title).toBe("Damaliger Titel"); }); it("falls back to the employee row when no assignment covers the date", () => { const employees = [emp("a", { team_id: TEAM_B })]; const [result] = snapshot({ asOf: "2024-03-01", employees, assignments: [] }).employees; expect(result.team_id).toBe(TEAM_B); }); }); describe("orphan re-rooting", () => { // Without this the whole reporting line below an absent manager silently // disappears from the chart instead of moving up a level. it("drops a manager reference to somebody not employed on that date", () => { const employees = [ emp("boss", { exit_date: "2026-01-01", org_level: 2, is_lead: true }), emp("report", { manager_id: "boss" }), ]; const assignments = [assignment("report", { manager_id: "boss" })]; const result = snapshot({ asOf: "2026-06-01", employees, assignments }).employees; expect(result).toHaveLength(1); expect(result[0].id).toBe("report"); expect(result[0].manager_id).toBeNull(); }); }); describe("future projection from pending changes", () => { const leadB = emp("lead-b", { id: "lead-b", team_id: TEAM_B, division_id: "div-2", is_lead: true, org_level: 2 }); it("moves an employee into the target team and under that team's lead", () => { const employees = [emp("a", { manager_id: "lead-a" }), leadB]; const assignments = [assignment("a", { manager_id: "lead-a" }), assignment("lead-b", { team_id: TEAM_B, division_id: "div-2", is_lead: true, org_level: 2 })]; const pending = [{ employee_id: "a", effective_date: "2026-08-01", payload: { new_team_id: TEAM_B } }]; const result = snapshot({ asOf: "2026-09-01", employees, assignments, pending }); const moved = result.employees.find((e) => e.id === "a")!; expect(moved.team_id).toBe(TEAM_B); expect(moved.division_id).toBe("div-2"); expect(moved.manager_id).toBe("lead-b"); expect(result.projectedCount).toBe(1); }); it("lets a later change win over an earlier one", () => { const employees = [emp("a")]; const assignments = [assignment("a")]; const pending = [ { employee_id: "a", effective_date: "2026-08-01", payload: { new_team_id: TEAM_B, new_title: "Zwischenstand" } }, { employee_id: "a", effective_date: "2026-09-01", payload: { new_team_id: TEAM_A, new_title: "Endstand" } }, ]; const [result] = snapshot({ asOf: "2026-10-01", employees, assignments, pending }).employees; expect(result.team_id).toBe(TEAM_A); expect(result.job_title).toBe("Endstand"); }); it("leaves an untouched employee's manager exactly as recorded", () => { // Deliberately deviating from the resolve rule: real data drifts, and a // snapshot must not silently "repair" reporting lines it was not asked // to change. const employees = [emp("a", { manager_id: "someone-else" }), emp("someone-else", { id: "someone-else" }), leadB]; const assignments = [assignment("a", { manager_id: "someone-else" })]; const [result] = snapshot({ asOf: "2026-09-01", employees, assignments }).employees; expect(result.manager_id).toBe("someone-else"); }); it("ignores pending changes for a date the caller did not ask about", () => { // loadOrgAsOf only fetches pending rows for a future date, so an empty // list here must simply mean "no projection", not "drop the employee". const employees = [emp("a")]; const assignments = [assignment("a")]; const result = snapshot({ asOf: "2026-09-01", employees, assignments, pending: [] }); expect(result.projectedCount).toBe(0); expect(result.employees).toHaveLength(1); }); }); describe("historyStartsAt", () => { it("reports the earliest recorded assignment so the UI can flag older dates", () => { const employees = [emp("a"), emp("b", { id: "b" })]; const assignments = [assignment("a", { valid_from: "2023-05-01" }), assignment("b", { valid_from: "2021-02-01" })]; expect(snapshot({ asOf: "2026-01-01", employees, assignments }).historyStartsAt).toBe("2021-02-01"); }); it("is null when nothing is recorded yet", () => { expect(snapshot({ asOf: "2026-01-01", employees: [emp("a")] }).historyStartsAt).toBeNull(); }); });