Files
Betriebsf-hrungssoftware/test/vcard.test.ts
2026-11-04 21:26:50 +00:00

79 lines
3.4 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. 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 { felderAus, fingerabdruck, karteMit, kartenAus, nummer, revVon, uidVon } from "../src/domain/vcard.js";
// so exportiert Apple Kontakte (gekürzt)
const APPLE = `BEGIN:VCARD\r
VERSION:3.0\r
PRODID:-//Apple Inc.//iPhone OS 18.0//EN\r
N:Braun;Petra;;;\r
FN:Petra Braun\r
ORG:Braun & Müller GbR;\r
EMAIL;type=INTERNET;type=HOME;type=pref:petra@example.de\r
TEL;type=CELL;type=VOICE;type=pref:0171 1234567\r
TEL;type=HOME;type=VOICE:04541 8899\r
item1.ADR;type=HOME;type=pref:;;Am Markt 3\\nHinterhaus;Ratzeburg;;23909;Deutschland\r
item1.X-ABADR:de\r
BDAY:1970-05-04\r
NOTE:sehr lange Notiz, die über die Zeile hin\r
aus geht\r
UID:ABC-123\r
REV:2026-11-04T18:12:03Z\r
END:VCARD\r
BEGIN:VCARD\r
VERSION:3.0\r
FN:Holzhandel Nord\r
ORG:Holzhandel Nord;\r
X-ABShowAs:COMPANY\r
TEL;type=WORK;type=VOICE:+49 451 123\r
END:VCARD\r
`;
describe("vCard", () => {
it("liest Apple-Karten", () => {
const [a, b] = kartenAus(APPLE);
expect(felderAus(a!)).toEqual({ name: "Petra Braun", firma: "Braun & Müller GbR", telefon: "04541 8899", mobil: "0171 1234567", email: "petra@example.de",
strasse: "Am Markt 3\nHinterhaus", plz: "23909", ort: "Ratzeburg" });
expect(uidVon(a!)).toBe("ABC-123");
expect(revVon(a!)).toBe("2026-11-04T18:12:03Z");
expect(felderAus(b!)).toMatchObject({ name: "Holzhandel Nord", firma: "Holzhandel Nord", telefon: "+49 451 123", mobil: null });
});
it("schreibt nur die gepflegten Zeilen, der Rest bleibt", () => {
const [a] = kartenAus(APPLE);
const f = { ...felderAus(a!), mobil: "0171 7654321", email: null };
const neu = karteMit(f, "ABC-123", a!, new Date("2026-11-05T10:00:00Z"));
expect(felderAus(neu)).toEqual(f);
expect(neu).toContain("BDAY:1970-05-04");
expect(neu).toContain("item1.X-ABADR:de");
expect(neu).toContain("Deutschland");
expect(neu).toContain("N:Braun;Petra;;;");
expect(neu).not.toContain("EMAIL");
expect(revVon(neu)).toBe("2026-11-05T10:00:00Z");
});
it("legt neue Karten an, mit Sonderzeichen", () => {
const f = { name: "Jan Peters", firma: "Peters; Söhne", telefon: "0451 1", mobil: null, email: "jan@x.de", strasse: "Weg 1", plz: "23909", ort: "Ratzeburg" };
const k = karteMit(f, "u1", null);
expect(k).toMatch(/^BEGIN:VCARD\r\nVERSION:3.0\r\n/);
expect(k).toContain("N:Peters;Jan;;;");
expect(felderAus(k)).toEqual(f);
expect(uidVon(k)).toBe("u1");
});
it("vergleicht Nummern und Inhalte", () => {
expect(nummer("+49 171 / 123")).toBe(nummer("0171-123"));
expect(fingerabdruck({ name: "A B" })).toBe(fingerabdruck({ name: "A B", firma: null }));
});
});
describe("Netzfehler mit Ursache", () => {
it("zeigt code und message aus err.cause", async () => {
const { abrufMitUrsache, fehlerText } = await import("../src/server/nextcloud.js");
const tls = Object.assign(new Error("unable to verify the first certificate"), { code: "UNABLE_TO_VERIFY_LEAF_SIGNATURE" });
const fehler = new TypeError("fetch failed", { cause: tls });
expect(fehlerText(fehler)).toBe("fetch failed – UNABLE_TO_VERIFY_LEAF_SIGNATURE: unable to verify the first certificate");
const abruf = (async () => { throw fehler; }) as unknown as typeof fetch;
await expect(abrufMitUrsache(abruf, "https://cloud.test/remote.php/dav", {})).rejects.toThrow("cloud.test nicht erreichbar: fetch failed – UNABLE_TO_VERIFY_LEAF_SIGNATURE");
});
});