Files
alpenwerk-hr/tests/unit/cornerstone.test.ts
Maximilian Stubhan 410acfe01f
Some checks failed
CI / Lint, Typen, Tests, Build (push) Successful in 11m34s
CI / Migrationen auf leerer Datenbank (push) Has been cancelled
Cornerstone Report: system values, not display names
Reworked against the load spec and the 27.09. test file. The values I
had guessed were the German display names, which is the first entry on
the list of errors from earlier loads: Cornerstone answers "ungültiger
Wert" and rejects the whole row, followed by "Alle abhängigen Felder
müssen gültig sein" as a follow-on.

  Status            Aktiv/Inaktiv    -> Active/Inactive
  Employment Status Arbeitend/...    -> Working/On Leave/Terminated
  User Type         Mitarbeiter      -> Employee
  Time Zone         CET              -> empty

The time zone is the second entry on that list: only a portal time zone
id is valid, an abbreviation gives "Zeitzonencode nicht eindeutig".
Empty means the portal or the OU decides.

Division ID is the GUID from the test file, not a name. Termination
fields and Leave Reason are filled only when the employment status
carries them -- a reason without a termination is an invalid state for
the load, not extra information.

Four fields now stay empty on purpose, because filling them would mean
inventing an identifier that belongs to the target system: Location ID
(locations has id/name/country and no Cornerstone id), Position ID (our
S-0001 is not a Cornerstone position), Months of Service (Cornerstone
derives it) and Rehired Employee (the value for "yes" is unconfirmed,
and an unconfirmed value costs the whole row). Retention Rules and
Organisationsstufe stay empty because the load ignores them.

Header and row now match the test file byte for byte, except User ID
(no TEST- prefix outside a test load), Required Training Approvals and
Location ID.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-27 20:10:56 +02:00

281 lines
10 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
baueCornerstoneZeile,
benutzername,
beschaeftigungsStatus,
CORNERSTONE_CSV,
CORNERSTONE_SPALTEN,
cornerstoneSpalten,
csDatum,
DIVISION_GUID,
geschlecht,
kontoStatus,
type CornerstoneKontext,
type CornerstoneQuelle,
} from "@/lib/cornerstone";
import { toCsv } from "@/lib/export";
// Die Datei geht in ein fremdes System, das sie über eine im Portal
// eingerichtete Zuordnung einliest. Zwei Sorten Fehler sind hier teuer:
//
// * Ein deutscher Anzeigename statt des Systemwerts („Aktiv" statt
// „Active") weist Cornerstone mit „ungültiger Wert" ab — und zwar die
// ganze Zeile, gefolgt von „Alle abhängigen Felder müssen gültig sein".
// * Ein Datum in der falschen Reihenfolge fällt gar nicht auf: 09.03. und
// 03.09. sind beide gültig.
//
// Deshalb steht hier beides fest, Wert für Wert.
// Die Kopfzeile aus der Vorlage, Zeichen für Zeichen.
const VORGABE =
"User ID,Local System ID,Prefix,First Name,Middle Name,Last Name,Suffix,Username,Approver,Manager,Absent,Allow Reconciliation,Email,Required Training Approvals,Gender,Exempt,Ethnicity,Months of Service,Compensation Currency,Address Line 1,Address Line 2,City,State,Zip,Mailstop,Country,Original Hire Date,Last Hire Date,Phone,Mobile,Home Phone,Fax,Status,User activation date,User de-activation date,Personal Email,Language,Time Zone,User Type,Employment Status,Leave Reason,Termination Type,Termination Reason,Termination Date,Eligible For Rehire,Rehired Employee,Division ID,Position ID,Grade ID,Cost Center ID,Location ID,Retention Rules,Organisationsstufe,Customfield ID AD,Customfield ID Doxis,Customfield ID Interflex,Customfield ID LGVplus";
const kontext: CornerstoneKontext = {
managerNummer: new Map([["chef", 1000]]),
kostenstelle: new Map([["pos-1", { id: "k", code: "41130", name: "Waffelproduktion" }]]),
};
function person(teil: Partial<CornerstoneQuelle> = {}): CornerstoneQuelle {
return {
id: "e1",
personnel_number: 9002,
first_name: "Max",
last_name: "Alpentest",
title_prefix: [],
title_suffix: [],
gender: "m",
company_email: "m.test@alpenwerk.test.com",
address: "Teststrasse 2",
postal_code: "1010",
city: "Wien",
address_country: "AT",
entry_date: "2026-10-01",
exit_date: null,
exit_reason: null,
austrittsart: null,
absence_type: null,
status: "Aktiv",
paygrade: "HG15",
location_id: "wien",
position_id: "pos-1",
manager_id: null,
...teil,
};
}
const zeile = (teil: Partial<CornerstoneQuelle> = {}) => baueCornerstoneZeile(person(teil), kontext);
describe("Spalten", () => {
it("stehen genau so und in genau der Reihenfolge wie in der Vorlage", () => {
expect(CORNERSTONE_SPALTEN.join(",")).toBe(VORGABE);
});
it("sind 57", () => {
expect(CORNERSTONE_SPALTEN).toHaveLength(57);
});
it("decken sich mit den Feldern, die eine Zeile liefert", () => {
// Ein Feld, das die Zeile setzt, das aber in keiner Spalte steht, ginge
// stillschweigend verloren.
expect(Object.keys(zeile()).sort()).toEqual([...CORNERSTONE_SPALTEN].sort());
});
it("schreibt die Datei mit Komma und ohne BOM", () => {
// Stünden die Vorgaben von toCsv drin (Semikolon fürs deutsche Excel,
// BOM gegen Buchstabensalat), käme in Cornerstone eine einspaltige Datei
// an, deren Spalte auch noch ein unsichtbares Zeichen im Namen trägt.
expect(CORNERSTONE_CSV).toEqual({ trenner: ",", bom: false });
});
});
describe("Systemwerte statt Anzeigenamen", () => {
it("setzt Status auf Active beziehungsweise Inactive", () => {
expect(kontoStatus("Aktiv")).toBe("Active");
expect(kontoStatus("Karenz")).toBe("Active");
expect(kontoStatus("Ausgetreten")).toBe("Inactive");
});
it("setzt Employment Status auf Working, On Leave oder Terminated", () => {
expect(beschaeftigungsStatus("Aktiv")).toBe("Working");
expect(beschaeftigungsStatus("Karenz")).toBe("On Leave");
expect(beschaeftigungsStatus("Ausgetreten")).toBe("Terminated");
// „Geplant" kennt Cornerstone nicht — die Person bekommt ein Konto.
expect(beschaeftigungsStatus("Geplant")).toBe("Working");
});
it("schreibt nirgends einen deutschen Anzeigenamen in die Statusfelder", () => {
// Der häufigste Fehler aus früheren Loads.
const z = zeile();
expect([z.Status, z["Employment Status"], z["User Type"]]).toEqual(["Active", "Working", "Employee"]);
});
it("lässt die Zeitzone leer, statt ein Kürzel zu schreiben", () => {
// Gültig ist allein eine Zeitzonen-ID aus dem Portal; „CET" quittiert
// der Load mit „Zeitzonencode nicht eindeutig".
expect(zeile()["Time Zone"]).toBe("");
});
it("übersetzt das Geschlecht in Cornerstones Werte", () => {
expect(geschlecht("w")).toBe("female");
expect(geschlecht("m")).toBe("male");
// Alles andere ist nicht „leer", sondern ein gültiger Wert — sonst weist
// Cornerstone die ganze Zeile ab.
expect(geschlecht("x")).toBe("not specified");
});
it("schreibt das Datum mit dem Tag zuerst", () => {
expect(csDatum("2026-10-01")).toBe("01.10.2026");
expect(csDatum(null)).toBe("");
});
it("baut den Anmeldenamen ohne Umlaute und Sonderzeichen", () => {
expect(benutzername("Jörg", "Müller-Groß")).toBe("joerg.muellergross");
expect(benutzername("Anna Maria", "Berger")).toBe("annamaria.berger");
});
});
describe("Eine Zeile gegen das Muster vom 27.09.2026", () => {
it("trägt dieselben Werte wie der Testload", () => {
expect(zeile()).toMatchObject({
"Local System ID": "9002",
"First Name": "Max",
"Last Name": "Alpentest",
Username: "max.alpentest",
Email: "m.test@alpenwerk.test.com",
Gender: "male",
"Compensation Currency": "EUR",
"Address Line 1": "Teststrasse 2",
City: "Wien",
Zip: "1010",
Country: "AT",
"Original Hire Date": "01.10.2026",
"Last Hire Date": "01.10.2026",
Status: "Active",
Language: "de-DE",
"Time Zone": "",
"User Type": "Employee",
"Employment Status": "Working",
"Division ID": DIVISION_GUID,
"Grade ID": "HG15",
"Cost Center ID": "41130",
"Customfield ID AD": "max.alpentest",
"Customfield ID Doxis": "DOX-9002",
"Customfield ID Interflex": "9002",
"Customfield ID LGVplus": "LGV-9002",
});
});
it("lässt die Felder leer, die das Muster leer lässt", () => {
const z = zeile();
for (const feld of [
"Prefix",
"Middle Name",
"Suffix",
"Approver",
"Manager",
"Absent",
"Allow Reconciliation",
"Required Training Approvals",
"Exempt",
"Ethnicity",
"Months of Service",
"Address Line 2",
"State",
"Mailstop",
"Phone",
"Mobile",
"Home Phone",
"Fax",
"User activation date",
"User de-activation date",
"Personal Email",
"Leave Reason",
"Termination Type",
"Termination Reason",
"Termination Date",
"Eligible For Rehire",
"Rehired Employee",
"Position ID",
"Location ID",
"Retention Rules",
"Organisationsstufe",
]) {
expect(z[feld], feld).toBe("");
}
});
});
describe("Abhängige Felder", () => {
it("füllt die Austrittsfelder nur bei Terminated", () => {
// Ein Austrittsgrund ohne Austritt ist für den Load ein ungültiger
// Zustand, nicht eine Zusatzangabe.
const offen = zeile({ exit_date: "2026-12-31", exit_reason: "Kündigung", austrittsart: "freiwillig" });
expect(offen["Termination Date"]).toBe("");
expect(offen["Termination Type"]).toBe("");
expect(offen["Termination Reason"]).toBe("");
const weg = zeile({ status: "Ausgetreten", exit_date: "2026-12-31", exit_reason: "Kündigung", austrittsart: "freiwillig" });
expect(weg["Termination Date"]).toBe("31.12.2026");
expect(weg["Termination Type"]).toBe("freiwillig");
expect(weg["Termination Reason"]).toBe("Kündigung");
expect(weg.Status).toBe("Inactive");
expect(weg["Employment Status"]).toBe("Terminated");
});
it("nennt den Abwesenheitsgrund nur bei On Leave", () => {
expect(zeile({ absence_type: "Elternkarenz" })["Leave Reason"]).toBe("");
const karenz = zeile({ status: "Karenz", absence_type: "Elternkarenz" });
expect(karenz["Leave Reason"]).toBe("Elternkarenz");
expect(karenz["Employment Status"]).toBe("On Leave");
});
it("nennt keinen Abwesenheitsgrund an einer ausgetretenen Person", () => {
const weg = zeile({ status: "Ausgetreten", absence_type: "Elternkarenz", exit_date: "2026-12-31" });
expect(weg["Leave Reason"]).toBe("");
});
});
describe("Kennungen statt Namen", () => {
it("verweist auf die vorgesetzte Person über deren Personalnummer", () => {
// Cornerstone verknüpft über die User ID, nicht über den Namen.
expect(zeile({ manager_id: "chef" }).Manager).toBe("1000");
expect(zeile({ manager_id: null }).Manager).toBe("");
});
it("nimmt als Kostenstelle den Code der Planstelle", () => {
expect(zeile()["Cost Center ID"]).toBe("41130");
expect(zeile({ position_id: null })["Cost Center ID"]).toBe("");
});
it("schreibt die Division als GUID", () => {
expect(zeile()["Division ID"]).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/);
});
});
describe("als CSV", () => {
it("schreibt die Kopfzeile Zeichen für Zeichen wie die Vorlage", () => {
const csv = toCsv([zeile()], cornerstoneSpalten(), CORNERSTONE_CSV);
expect(csv.startsWith("User ID,")).toBe(true);
expect(csv.split(/\r?\n/)[0]).toBe(VORGABE);
});
it("trennt die Zeilen mit CRLF", () => {
expect(toCsv([zeile()], cornerstoneSpalten(), CORNERSTONE_CSV)).toContain("\r\n");
});
it("maskiert ein Komma im Wert, statt die Zeile zu zerreissen", () => {
const csv = toCsv([zeile({ address: "Teststrasse 2, Stiege 3" })], cornerstoneSpalten(), CORNERSTONE_CSV);
expect(csv).toContain('"Teststrasse 2, Stiege 3"');
// Felder zählen, nicht Kommas: das maskierte Komma steht ja in der Zeile.
const felder = (t: string) => t.split(/,(?=(?:[^"]*"[^"]*")*[^"]*$)/).length;
const [kopf, daten] = csv.split(/\r?\n/);
expect(felder(daten)).toBe(felder(kopf));
expect(felder(kopf)).toBe(57);
});
it("lässt die private Adresse nirgends durch", () => {
const csv = toCsv([zeile({ company_email: null })], cornerstoneSpalten(), CORNERSTONE_CSV);
expect(csv).not.toContain("@");
});
});