Files
alpenwerk-hr/tests/unit/report-criteria.test.ts
Maximilian Stubhan 0b8f874fa5 Let the export select on everything the data model holds
The export offered four criteria — unit, location, status, employment
type — while the employee record carries around twenty selectable
attributes. Anything else had to be filtered by hand in Excel afterwards,
which is how a payroll hand-off stops matching the application it came
from.

All of them are now filters: contract type, blue/white collar,
collective agreement, paygrade, internal/external, gender, company car
and its drivetrain, works council, lateral leadership, C-level, type of
long-term absence, weekday worked, dependents on file, and open ranges
for entry, exit, birth date and weekly hours. The unit filter covers
every level rather than only divisions, so a single department can be
selected without going the long way round.

They live in one table in lib/report-criteria.ts, which the filter panel
builds itself from, the parser validates against, and the query turns
into conditions. A new criterion is one entry there and nothing else —
and it cannot end up working in the report while being silently ignored
by the export.

The two export links and the saved-report config now carry the query
string through as it stands instead of listing the parameters they know
about. That enumeration was the actual defect: adding a filter meant
remembering three separate places, and forgetting one produced an export
that quietly disagreed with the figure on screen.

Validation is not housekeeping here. These values reach SQL comparisons
and the download filename, i.e. a Content-Disposition header; what is not
in the list does not get through.

The company car dropdown leaves the employee list. It is one of twenty
equals under Berichte now, where the selection can also be exported —
which was the point of asking in the first place.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-12 12:40:47 +02:00

100 lines
4.0 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
anzahlKriterien,
AUSWAHL_KRITERIEN,
JANEIN_KRITERIEN,
kriterienParameter,
LEERE_CRITERIA,
parseCriteria,
passtImSpeicher,
ZEITRAUM_KRITERIEN,
} from "@/lib/report-criteria";
function aus(params: Record<string, string>) {
return parseCriteria((k) => params[k]);
}
describe("parseCriteria", () => {
it("nimmt bekannte Werte an", () => {
const c = aus({ employment: "Teilzeit", paygrade: "C", gender: "w" });
expect(c.auswahl).toEqual({ employment: "Teilzeit", paygrade: "C", gender: "w" });
});
it("verwirft Werte, die nicht in der Liste stehen", () => {
// Die Werte landen in SQL-Vergleichen und im Dateinamen des Downloads,
// also in einem Content-Disposition-Header — was nicht in der Liste
// steht, darf nicht durchkommen.
const c = aus({ employment: "Vollzeit'; drop table employees; --", paygrade: "Z", gender: "x" });
expect(c.auswahl).toEqual({});
});
it("nimmt ja/nein an und sonst nichts", () => {
const c = aus({ dienstwagen: "ja", betriebsrat: "vielleicht", clevel: "nein" });
expect(c.jaNein).toEqual({ dienstwagen: "ja", clevel: "nein" });
});
it("prüft Zeiträume auf echte Kalenderdaten", () => {
const c = aus({ entryFrom: "2024-01-01", entryTo: "2024-02-31", exitTo: "gestern" });
// Der 31. Februar ist kein Datum, auch wenn er wie eines aussieht.
expect(c.zeitraum.entry).toEqual({ von: "2024-01-01", bis: undefined });
expect(c.zeitraum.exit).toBeUndefined();
});
it("nimmt Stunden mit Komma und weist Unsinn ab", () => {
expect(aus({ hoursFrom: "20,5" }).stundenVon).toBe(20.5);
expect(aus({ hoursFrom: "-5" }).stundenVon).toBeUndefined();
expect(aus({ hoursTo: "500" }).stundenBis).toBeUndefined();
expect(aus({ hoursTo: "keine" }).stundenBis).toBeUndefined();
});
it("liefert ohne Parameter nichts Gesetztes", () => {
expect(anzahlKriterien(aus({}))).toBe(0);
expect(anzahlKriterien(LEERE_CRITERIA)).toBe(0);
});
it("zählt jedes gesetzte Kriterium einzeln", () => {
const c = aus({ employment: "Teilzeit", dienstwagen: "ja", entryFrom: "2024-01-01", entryTo: "2024-12-31", hoursTo: "30" });
expect(anzahlKriterien(c)).toBe(5);
});
});
describe("kriterienParameter", () => {
it("nennt jeden Parameter, den parseCriteria liest", () => {
// „Zurücksetzen" räumt anhand dieser Liste auf. Fehlt darin ein Name,
// bleibt ein unsichtbares Kriterium in der Adresszeile stehen und
// schränkt den Export weiter ein, ohne dass es jemand sieht.
const namen = kriterienParameter();
const alle = {
...Object.fromEntries(AUSWAHL_KRITERIEN.map((k) => [k.key, k.optionen[0].wert])),
...Object.fromEntries(JANEIN_KRITERIEN.map((k) => [k.key, "ja"])),
...Object.fromEntries(ZEITRAUM_KRITERIEN.flatMap((k) => [[`${k.key}From`, "2024-01-01"], [`${k.key}To`, "2024-12-31"]])),
hoursFrom: "10",
hoursTo: "38.5",
};
expect(anzahlKriterien(aus(alle))).toBeGreaterThan(0);
const uebrig = aus(Object.fromEntries(Object.entries(alle).filter(([k]) => !namen.includes(k))));
expect(anzahlKriterien(uebrig)).toBe(0);
});
});
describe("passtImSpeicher", () => {
const zeile = { work_days: ["Mo", "Di", "Mi"], dependentsCount: 2 };
it("lässt ohne Kriterien alles durch", () => {
expect(passtImSpeicher(zeile, LEERE_CRITERIA)).toBe(true);
});
it("prüft den Wochentag gegen die Arbeitstage", () => {
expect(passtImSpeicher(zeile, aus({ weekday: "Di" }))).toBe(true);
expect(passtImSpeicher(zeile, aus({ weekday: "Fr" }))).toBe(false);
});
it("unterscheidet mit und ohne Angehörige", () => {
expect(passtImSpeicher(zeile, aus({ dependents: "ja" }))).toBe(true);
expect(passtImSpeicher(zeile, aus({ dependents: "nein" }))).toBe(false);
expect(passtImSpeicher({ ...zeile, dependentsCount: 0 }, aus({ dependents: "nein" }))).toBe(true);
expect(passtImSpeicher({ ...zeile, dependentsCount: 0 }, aus({ dependents: "ja" }))).toBe(false);
});
});