Files
alpenwerk-hr/tests/unit/orgchart-data.test.ts
Maximilian Stubhan 79f0e19bf8 Org assignment history, mobile support, and a correctness pass
Data model
- employee_assignments records org placement over time (valid_from/valid_to),
  written by a trigger on `employees` rather than inside each RPC: ~70
  `update employees` statements spread over fifteen migrations mean per-call
  bookkeeping would miss paths today and again with every future RPC. A
  partial unique index enforces the one-open-interval invariant the trigger
  relies on when closing the current row.
- The Organigramm gains a Stichtag (default today). Membership comes from
  entry/exit/karenz, past placement from the new history, future placement
  projected from pending_org_changes. Placements predating the migration are
  backfilled with today's values and flagged as such in the UI, since
  employee_history only ever stored free text and cannot be reconstructed.

Correctness
- Reports and exports silently truncated at PostgREST's 1000-row cap
  (db.max_rows); employee_history is already past it at ~800 staff. Every
  whole-table read now pages explicitly.
- XLSX date cells were a day early: ExcelJS converts a Date to an Excel
  serial straight off getTime(), so a Date built at local midnight lands on
  the previous day's serial in any positive-offset zone.
- Date handling is pinned to Europe/Vienna throughout, and date-only strings
  are formatted without a Date round-trip. The dashboard's YTD window was
  built by round-tripping a local Date through toISOString(), which shifted
  it a day early and dropped 31 December entirely.
- Export routes parsed measure/group/split/eventType with unchecked `as`
  casts, so an unknown value reached column headers as `undefined` and the
  Content-Disposition filename. Parsed against the label maps now, with the
  filename slugged as a backstop.
- toXlsx keyed columns by header text, silently dropping the second of any
  two columns sharing a name — split columns take their header from data.
- The org chart tree walks had no cycle guard; nothing in the schema forbids
  a manager_id cycle, and one would hang the tab rather than misreport.
- The login page reflected ?error= verbatim, letting anyone put arbitrary
  text on the real sign-in screen; messages are looked up by code now.
- React Flow needs elementsSelectable on, or it sets pointer-events:none on
  the whole node and the expand control stops responding.

UI
- Mobile: the shell was unusable below lg — a fixed 236px margin pushed
  content off-screen with no mobile navigation at all. The sidebar is now a
  drawer, dvh replaces vh, safe-area insets are honoured, inputs are 16px so
  iOS stops zooming on focus, and form grids stack.
- Org chart nodes redesigned: per-kind accent stripes and icons, vacant
  roles called out, expand control moved to the bottom edge carrying the
  child count.
- Pagination is windowed; it previously rendered one link per page (54 for
  the employee list, unbounded for the audit log).
- Positions page reduced to open positions with a single "Besetzen" action.
- The employee Organisation tab links into the org chart focused on that
  person, reusing the chart's existing search-match highlighting.

Also included, uncommitted until now
- Dependants, HR notes, academic titles, split address fields, position
  validity and role/employment fields, with their migrations and UI.
- Docker/compose deployment setup, data-model and security-review docs.
2026-07-24 23:38:10 +02:00

169 lines
7.0 KiB
TypeScript

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<typeof resolveOrgSnapshot>[0]["employees"][number];
type AssignmentInput = Parameters<typeof resolveOrgSnapshot>[0]["assignments"][number];
function emp(id: string, overrides: Partial<EmployeeInput> = {}): 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,
...overrides,
};
}
function assignment(employeeId: string, overrides: Partial<AssignmentInput> = {}): 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<Parameters<typeof resolveOrgSnapshot>[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();
});
});