1
0
Fork 0
fahrPool/web/lib/audit/csv.test.ts

47 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { describe, expect, it } from "vitest";
import { auditCsv, csvFeld } from "./csv";
describe("csvFeld", () => {
it("quotet Trennzeichen, Anfuehrungszeichen und Zeilenumbrueche", () => {
expect(csvFeld("a;b")).toBe('"a;b"');
expect(csvFeld('Er sagte "Hallo"')).toBe('"Er sagte ""Hallo"""');
expect(csvFeld("Zeile1\nZeile2")).toBe('"Zeile1\nZeile2"');
expect(csvFeld("Müller")).toBe("Müller");
});
it("entschaerft Formeln", () => {
expect(csvFeld("=SUMME(A1)")).toBe("'=SUMME(A1)");
expect(csvFeld("+49151")).toBe("'+49151");
expect(csvFeld("-5")).toBe("'-5");
expect(csvFeld("@x")).toBe("'@x");
});
it("entschaerft Formeln hinter Tab, CR und fuehrenden Leerzeichen", () => {
expect(csvFeld("\t=1")).toBe("'\t=1");
expect(csvFeld("\r=1")).toBe("\"'\r=1\"");
expect(csvFeld(" =1")).toBe("' =1");
expect(csvFeld("\t")).toBe("'\t");
});
});
describe("auditCsv", () => {
it("beginnt mit BOM und Kopfzeile, eine Zeile pro Eintrag", () => {
const csv = auditCsv([
{
id: 1,
zeitpunkt: "2026-10-12T08:00:00Z",
akteur_id: "a",
akteur_name: "Anna",
akteur_rolle: "admin",
bereich: "auftrag",
aktion: "status_geaendert",
datensatz_id: "x",
auftragsnummer: "26-10-12-0001",
aenderungen: { status: { alt: "zugewiesen", neu: "unterwegs" } },
},
]);
expect(csv.startsWith("")).toBe(true);
const zeilen = csv.slice(1).split("\r\n");
expect(zeilen[0]).toBe("Zeitpunkt;Akteur;Rolle;Bereich;Aktion;Auftragsnummer;Datensatz;Änderungen");
expect(zeilen[1]).toBe("12.10.2026, 10:00;Anna;admin;Auftrag;Status geändert;26-10-12-0001;x;Status: zugewiesen → unterwegs");
});
});