Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AmyPfuNc4sCRfaF3oURXZz
127 lines
6.9 KiB
TypeScript
127 lines
6.9 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
||
import { ergaenzt, felderAus, fingerabdruck, istVerteiler, 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", () => {
|
||
const PETRA = {
|
||
name: "Petra Braun", anrede: null, titel: null, firma: "Braun & Müller GbR", funktion: null, gruppe: false,
|
||
telefone: [{ typ: "mobil", wert: "0171 1234567", haupt: true }, { typ: "privat", wert: "04541 8899", haupt: false }],
|
||
emails: [{ typ: "privat", wert: "petra@example.de", haupt: true }],
|
||
adressen: [{ typ: "privat", strasse: "Am Markt 3", zusatz: "Hinterhaus", plz: "23909", ort: "Ratzeburg", region: "", land: "Deutschland", postfach: "", haupt: true }],
|
||
notiz: "sehr lange Notiz, die über die Zeile hinaus geht",
|
||
};
|
||
|
||
it("liest Apple-Karten mit allen Nummern samt Typ", () => {
|
||
const [a, b] = kartenAus(APPLE);
|
||
expect(felderAus(a!)).toEqual(PETRA);
|
||
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", gruppe: true, telefone: [{ typ: "geschaeftlich", wert: "+49 451 123", haupt: true }] });
|
||
});
|
||
|
||
it("liest Bezeichnungen, Anrede und Titel", () => {
|
||
const k = felderAus(`BEGIN:VCARD\r\nVERSION:3.0\r\nN:Petersen;Jan;;Herr Dipl.-Ing.;\r\nFN:Jan Petersen\r\nORG:Wohnbau Nord eG;Technik\r\nTITLE:Technischer Leiter\r\n`
|
||
+ `item1.TEL:0451 1\r\nitem1.X-ABLabel:_$!<Main>!$_\r\nTEL;TYPE="work,fax":0451 2\r\nitem2.EMAIL;type=INTERNET:rechnung@wn.de\r\nitem2.X-ABLabel:Rechnungen\r\n`
|
||
+ `EMAIL;TYPE=work;PREF=1:jan@wn.de\r\nTEL;VALUE=uri:tel:+49171555\r\nEND:VCARD\r\n`);
|
||
expect(k).toMatchObject({ name: "Jan Petersen", anrede: "Herr", titel: "Dipl.-Ing.", firma: "Wohnbau Nord eG", funktion: "Technischer Leiter", gruppe: false });
|
||
expect(k.telefone).toEqual([{ typ: "zentrale", wert: "0451 1", haupt: true }, { typ: "fax", wert: "0451 2", haupt: false }, { typ: "sonstige", wert: "+49171555", haupt: false }]);
|
||
expect(k.emails).toEqual([{ typ: "rechnung", wert: "rechnung@wn.de", haupt: false }, { typ: "geschaeftlich", wert: "jan@wn.de", haupt: true }]);
|
||
expect(istVerteiler("BEGIN:VCARD\r\nFN:Kunden\r\nX-ADDRESSBOOKSERVER-KIND:group\r\nEND:VCARD")).toBe(true);
|
||
expect(istVerteiler(APPLE)).toBe(false);
|
||
});
|
||
|
||
it("schreibt nur geänderte Zeilen, der Rest bleibt", () => {
|
||
const [a] = kartenAus(APPLE);
|
||
const f = { ...felderAus(a!), telefone: [{ typ: "mobil", wert: "0171 7654321", haupt: true }, { typ: "privat", wert: "04541 8899", haupt: false }], emails: [] };
|
||
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");
|
||
// Adresse unverändert: Apples Zeilen bleiben samt Ländercode
|
||
expect(neu).toContain("item1.X-ABADR:de");
|
||
expect(neu).toContain("item1.ADR;type=HOME;type=pref:;;Am Markt 3\\nHinterhaus;Ratzeburg;;23909;Deutschland");
|
||
expect(neu).toContain("N:Braun;Petra;;;");
|
||
expect(neu).toContain("TEL;type=CELL;type=VOICE;type=pref:0171 7654321");
|
||
expect(neu).not.toContain("EMAIL");
|
||
expect(revVon(neu)).toBe("2026-11-05T10:00:00Z");
|
||
// nur die Anrede geändert: Apples Namensaufteilung bleibt
|
||
expect(karteMit({ ...f, anrede: "Frau" }, "ABC-123", neu)).toContain("N:Braun;Petra;;Frau;");
|
||
});
|
||
|
||
it("legt neue Karten an, mit Sonderzeichen und eigenen Typen", () => {
|
||
const f = {
|
||
name: "Jan Peters", anrede: "Herr", titel: "Dr.", firma: "Peters; Söhne", funktion: "Inhaber", gruppe: false, notiz: "Zeile 1\nZeile 2",
|
||
telefone: [{ typ: "geschaeftlich", wert: "0451 1", haupt: true }, { typ: "sonstige", wert: "0451 9", haupt: false }],
|
||
emails: [{ typ: "rechnung", wert: "jan@x.de", haupt: true }],
|
||
adressen: [{ typ: "rechnung", strasse: "Weg 1", zusatz: "", plz: "23909", ort: "Ratzeburg", region: "Schleswig-Holstein", land: "", postfach: "Postfach 12", haupt: true }],
|
||
};
|
||
const k = karteMit(f, "u1", null);
|
||
expect(k).toMatch(/^BEGIN:VCARD\r\nVERSION:3.0\r\n/);
|
||
expect(k).toContain("N:Peters;Jan;;Herr Dr.;");
|
||
expect(k).toContain("item2.X-ABLabel:Rechnungen");
|
||
expect(felderAus(k)).toEqual(f);
|
||
expect(fingerabdruck(felderAus(k))).toBe(fingerabdruck(f));
|
||
expect(uidVon(k)).toBe("u1");
|
||
});
|
||
|
||
it("Firmenkarte", () => {
|
||
const f = { name: "Wohnbau Nord eG", anrede: null, titel: null, firma: "Wohnbau Nord eG", funktion: undefined, gruppe: true, notiz: null,
|
||
telefone: [{ typ: "zentrale", wert: "04541 80800", haupt: true }], emails: [], adressen: [] };
|
||
const k = karteMit(f, "g1", null);
|
||
expect(k).toContain("X-ABShowAs:COMPANY");
|
||
expect(k).toContain("N:;;;;");
|
||
expect(felderAus(k)).toEqual(f);
|
||
});
|
||
|
||
it("ergänzt Programm um die Karte, ohne Programmwerte zu überschreiben", () => {
|
||
const [a] = kartenAus(APPLE);
|
||
const k = { ...PETRA, firma: null, notiz: "eigene", telefone: [{ typ: "mobil", wert: "+49 171 1234567", haupt: true }], emails: [], adressen: [] };
|
||
const m = ergaenzt(k, felderAus(a!));
|
||
expect(m.notiz).toBe("eigene");
|
||
expect(m.firma).toBe("Braun & Müller GbR");
|
||
expect(m.telefone).toEqual([{ typ: "mobil", wert: "+49 171 1234567", haupt: true }, { typ: "privat", wert: "04541 8899", haupt: false }]);
|
||
expect(m.emails).toHaveLength(1);
|
||
});
|
||
|
||
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");
|
||
});
|
||
});
|