Files
alpenwerk-hr/tests/unit/svnr.test.ts
Andrei Laas d13ff073ed
All checks were successful
CI / Lint, Typen, Tests, Build (push) Successful in 12m31s
CI / Migrationen auf leerer Datenbank (push) Successful in 10m43s
CI / Ausrollen (push) Successful in 8s
SV-Nummer: hoechstens zehn Ziffern, abweichendes Geburtsdatum nur Hinweis
Die Nummer wird einmal vergeben und nicht umgeschrieben, wenn das
Geburtsdatum spaeter berichtigt wird. Personen, deren Nummer ein anderes
Datum traegt als der Stammsatz, muessen sich erfassen lassen.

- lib/svnr.ts: svnrHaeltAuf trennt die Eigenschaften der Nummer (Laenge,
  laufende Nummer, Datum, Pruefziffer) von der Uebereinstimmung mit dem
  Stammsatz; svnrErfassbar sperrt "Weiter"/"Speichern" nur noch bei den
  ersten. begrenzeSvnr laesst hoechstens zehn Ziffern zu.
- SvNummerField: die Abweichung steht als Hinweis unter dem Feld, eine
  elfte Ziffer wird verworfen. Bei Einstellung und Wiedereintritt ein
  Haken "Nicht verfuegbar": leert und sperrt das Feld, steht nur im
  Entwurf.
- Angehoerige im Assistenten und im Dialog der Akte: dieselbe Regel,
  dieselbe Begrenzung.
- Migration 20261015100000: fn_validate_employee_svnr prueft nur noch die
  Nummer (is_valid_svnr ohne Geburtsdatum). Ohne sie haette die Datenbank
  abgewiesen, was das Formular zulaesst. Selbstpruefung: kein Vergleich
  mit new.birth_date, Pruefziffer wird weiter geprueft, Trigger vorhanden.

Geprueft: lint, tsc, types:check, 850 Tests (neu: SvNummerField,
svnrHaeltAuf, begrenzeSvnr), Waechter. Die Migration laeuft in der CI auf
leerer Datenbank.
2026-10-15 13:15:51 +02:00

161 lines
5.5 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
begrenzeSvnr,
formatSvnr,
isValidSvnr,
normalizeSvnr,
requiresAustrianSvnr,
svnrCheckDigit,
svnrErfassbar,
svnrHaeltAuf,
validateSvnr,
} from "@/lib/svnr";
// Weights 3,7,9 on the serial and 5,8,4,2,1,6 on TTMMJJ; sum mod 11 is the
// check digit. Worked through by hand for "123? 010180":
// 3·1 + 7·2 + 9·3 = 44 (serial)
// 5·0 + 8·1 + 4·0 + 2·1 + 1·8 + 6·0 = 18 (010180)
// 62 mod 11 = 7 → 1237 010180
const VALID = "1237 010180";
describe("svnrCheckDigit", () => {
it("computes the documented check digit", () => {
expect(svnrCheckDigit("1230010180")).toBe(7);
});
it("computes it independently of the digit already in that slot", () => {
// The check position is skipped, so a wrong digit there cannot influence
// the result — otherwise the check would validate itself.
expect(svnrCheckDigit("1234010180")).toBe(7);
expect(svnrCheckDigit("1239010180")).toBe(7);
});
it("computes 3 for serial 432 on the same date", () => {
// 3·4 + 7·3 + 9·2 = 51; 51 + 18 = 69; 69 mod 11 = 3
expect(svnrCheckDigit("4320010180")).toBe(3);
});
it("returns null for a serial that would need a check digit of 10", () => {
// 9·4 = 36; 36 + 18 = 54; 54 mod 11 = 10 — never issued, since there is
// no single digit for it.
expect(svnrCheckDigit("0040010180")).toBeNull();
});
});
describe("validateSvnr", () => {
it("accepts a well-formed number", () => {
expect(validateSvnr(VALID)).toBeNull();
expect(isValidSvnr(VALID)).toBe(true);
});
it("accepts it regardless of spacing or separators", () => {
for (const variant of ["1237010180", "1237 010180", "1237-010180", "1237.010180", " 1237 010180 "]) {
expect(validateSvnr(variant), variant).toBeNull();
}
});
it("rejects a wrong check digit", () => {
expect(validateSvnr("1234 010180")).toBe("checksum");
});
it("rejects a serial whose check digit would be 10", () => {
expect(validateSvnr("0040 010180")).toBe("checksum");
});
it("rejects anything that is not ten digits", () => {
for (const bad of ["", "123701018", "12370101801", "1237 01018X", "abcdefghij"]) {
expect(validateSvnr(bad), bad).toBe("length");
}
});
it("rejects the unissued 000 serial", () => {
expect(validateSvnr("0007 010180")).toBe("serial");
});
it("rejects an impossible date tail", () => {
// Month 13 and day 32 never occur; the checksum is irrelevant once the
// tail cannot be a date at all.
expect(validateSvnr("1237 011380")).toBe("date");
expect(validateSvnr("1237 320180")).toBe("date");
expect(validateSvnr("1237 310280")).toBe("date");
});
it("allows 29 February, which a two-digit year cannot disambiguate", () => {
// 3·1+7·2+9·3 = 44; 5·2+8·9+4·0+2·2+1·8+6·0 = 10+72+0+4+8 = 94; 138 mod 11 = 6
expect(validateSvnr("1236 290280")).toBeNull();
});
});
describe("cross-check against the stored birth date", () => {
it("passes when the tail matches", () => {
expect(validateSvnr(VALID, "1980-01-01")).toBeNull();
});
it("catches a transposed birth date the checksum cannot", () => {
expect(validateSvnr(VALID, "1980-01-02")).toBe("birthDateMismatch");
expect(validateSvnr(VALID, "1981-01-01")).toBe("birthDateMismatch");
});
it("skips the cross-check when no birth date is known", () => {
expect(validateSvnr(VALID, null)).toBeNull();
expect(validateSvnr(VALID, undefined)).toBeNull();
});
});
// Die Nummer wird einmal vergeben und nicht umgeschrieben, wenn das
// Geburtsdatum später berichtigt wird. Hielte die Abweichung auf, liesse sich
// eine solche Person nicht erfassen.
describe("svnrHaeltAuf und svnrErfassbar", () => {
it("hält bei allem auf, was die Nummer selbst betrifft", () => {
for (const fehler of ["length", "serial", "checksum", "date"] as const) {
expect(svnrHaeltAuf(fehler), fehler).toBe(true);
}
});
it("hält beim abweichenden Geburtsdatum nicht auf", () => {
expect(svnrHaeltAuf("birthDateMismatch")).toBe(false);
// 1237 010180 trägt den 01.01.1980.
expect(validateSvnr("1237 010180", "1980-01-02")).toBe("birthDateMismatch");
expect(svnrErfassbar("1237 010180", "1980-01-02")).toBe(true);
});
it("weist eine falsche Prüfziffer weiter ab", () => {
expect(svnrErfassbar("2010 290368", "1968-03-29")).toBe(false);
expect(svnrErfassbar("2013 290368", "1968-03-29")).toBe(true);
});
});
describe("begrenzeSvnr", () => {
it("lässt zehn Ziffern durch", () => {
expect(begrenzeSvnr("1237010180")).toBe("1237010180");
expect(begrenzeSvnr("1237 010180")).toBe("1237 010180");
});
it("verwirft jede weitere Ziffer", () => {
expect(begrenzeSvnr("12370101809")).toBe("1237010180");
expect(begrenzeSvnr("1237 0101809")).toBe("1237 010180");
});
it("zählt Ziffern, nicht Zeichen", () => {
expect(begrenzeSvnr("1237-01-01-80")).toBe("1237-01-01-80");
});
});
describe("helpers", () => {
it("normalises and formats", () => {
expect(normalizeSvnr("1237 010180")).toBe("1237010180");
expect(formatSvnr("1237010180")).toBe("1237 010180");
});
it("leaves non-canonical input alone when formatting", () => {
expect(formatSvnr("12")).toBe("12");
});
it("applies only to Austrian locations", () => {
expect(requiresAustrianSvnr("Österreich")).toBe(true);
for (const c of ["Deutschland", "Tschechien", "Slowenien", null, undefined]) {
expect(requiresAustrianSvnr(c)).toBe(false);
}
});
});