Kontakte mit dem Handy abgleichen über Nextcloud-Adressbuch, .vcf einlesen (0.44.0)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AmyPfuNc4sCRfaF3oURXZz
This commit is contained in:
116
test/kontaktabgleich.test.ts
Normal file
116
test/kontaktabgleich.test.ts
Normal file
@@ -0,0 +1,116 @@
|
||||
// Kontakte mit dem Handy abgleichen: Programm ↔ Adressbuch in der Nextcloud (CardDAV-Nachbildung im Speicher)
|
||||
import { describe, it, expect, beforeAll, afterAll } from "vitest";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { erstellePool, type Pool } from "../src/server/db.js";
|
||||
import { migriere } from "../src/server/migrate.js";
|
||||
import { baueApp } from "../src/server/app.js";
|
||||
import { Nextcloud } from "../src/server/nextcloud.js";
|
||||
import { felderAus, karteMit } from "../src/domain/vcard.js";
|
||||
|
||||
const url = process.env.TEST_DATABASE_URL;
|
||||
const BUCH = "/remote.php/dav/addressbooks/users/henning/holzbau/";
|
||||
|
||||
function falschesAdressbuch() {
|
||||
const karten = new Map<string, { etag: string; karte: string }>();
|
||||
let n = 0;
|
||||
const etag = () => `"e${++n}"`;
|
||||
const x = (s: string) => s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
||||
const abruf = (async (u: string | URL, init?: RequestInit) => {
|
||||
const p = decodeURIComponent(new URL(String(u)).pathname);
|
||||
const kopf = (init?.headers ?? {}) as Record<string, string>;
|
||||
switch (init?.method) {
|
||||
case "PROPFIND":
|
||||
return new Response(`<?xml version="1.0"?><d:multistatus xmlns:d="DAV:" xmlns:card="urn:ietf:params:xml:ns:carddav">
|
||||
<d:response><d:href>/remote.php/dav/addressbooks/users/henning/</d:href><d:propstat><d:prop><d:resourcetype><d:collection/></d:resourcetype></d:prop></d:propstat></d:response>
|
||||
<d:response><d:href>${BUCH}</d:href><d:propstat><d:prop><d:displayname>Holzbau</d:displayname><d:resourcetype><d:collection/><card:addressbook/></d:resourcetype></d:prop></d:propstat></d:response>
|
||||
</d:multistatus>`, { status: 207 });
|
||||
case "REPORT":
|
||||
return new Response(`<?xml version="1.0"?><d:multistatus xmlns:d="DAV:" xmlns:card="urn:ietf:params:xml:ns:carddav">${[...karten].map(([h, k]) =>
|
||||
`<d:response><d:href>${h}</d:href><d:propstat><d:prop><d:getetag>${x(k.etag)}</d:getetag><card:address-data>${x(k.karte)}</card:address-data></d:prop></d:propstat></d:response>`).join("")}</d:multistatus>`, { status: 207 });
|
||||
case "PUT": {
|
||||
const da = karten.get(p);
|
||||
if (kopf["if-none-match"] === "*" && da) return new Response(null, { status: 412 });
|
||||
if (kopf["if-match"] && kopf["if-match"] !== da?.etag) return new Response(null, { status: 412 });
|
||||
const e = etag();
|
||||
karten.set(p, { etag: e, karte: String(init!.body) });
|
||||
return new Response(null, { status: da ? 204 : 201, headers: { etag: e } });
|
||||
}
|
||||
case "DELETE":
|
||||
karten.delete(p);
|
||||
return new Response(null, { status: 204 });
|
||||
}
|
||||
return new Response(null, { status: 400 });
|
||||
}) as typeof fetch;
|
||||
/** Änderung am Handy */
|
||||
const handy = (href: string, karte: string) => karten.set(href, { etag: etag(), karte });
|
||||
return { abruf, karten, handy };
|
||||
}
|
||||
|
||||
describe.skipIf(!url)("Kontakte mit dem Handy abgleichen", () => {
|
||||
let pool: Pool;
|
||||
let app: FastifyInstance;
|
||||
const f = falschesAdressbuch();
|
||||
const nc = new Nextcloud({ url: "https://cloud.test/remote.php/dav/files/henning/01 Projekte", benutzer: "henning", passwort: "geheim" }, f.abruf);
|
||||
const jetzt = async () => { const r = await app.inject({ method: "POST", url: "/api/kontakte-abgleich/jetzt" }); expect(r.statusCode, r.body).toBe(200); return r.json().meldung as string; };
|
||||
const kontakt = async (name: string) => (await app.inject("/api/kontakte")).json().find((k: { name: string }) => k.name === name);
|
||||
|
||||
beforeAll(async () => {
|
||||
pool = erstellePool(url!);
|
||||
await pool.query("drop schema public cascade; create schema public;");
|
||||
await migriere(pool);
|
||||
app = await baueApp({ pool, webOrdner: "/gibt-es-nicht", nextcloud: nc, anmeldung: "testversion" });
|
||||
});
|
||||
afterAll(async () => {
|
||||
await app?.close();
|
||||
await pool?.end();
|
||||
});
|
||||
|
||||
it("Adressbuch wählen, erster Abgleich führt zusammen und legt an", async () => {
|
||||
expect((await app.inject("/api/kontakte-abgleich/adressbuecher")).json()).toEqual([{ href: BUCH, name: "Holzbau" }]);
|
||||
await app.inject({ method: "POST", url: "/api/kontakte", payload: { name: "Petra Braun", rolle: "bauherr", email: "petra@example.de" } });
|
||||
await app.inject({ method: "POST", url: "/api/kontakte", payload: { name: "Holzhandel Nord", firma: "Holzhandel Nord", rolle: "lieferant" } });
|
||||
f.handy(`${BUCH}a1.vcf`, "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:Petra Braun\r\nN:Braun;Petra;;;\r\nTEL;type=CELL:0171 1\r\nEMAIL:petra@example.de\r\nUID:a1\r\nBDAY:1970-05-04\r\nEND:VCARD\r\n");
|
||||
f.handy(`${BUCH}a2.vcf`, "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:Jan Peters\r\nTEL;type=WORK:0451 2\r\nUID:a2\r\nEND:VCARD\r\n");
|
||||
expect((await app.inject({ method: "PUT", url: "/api/kontakte-abgleich", payload: { adressbuch: BUCH, an: true } })).statusCode).toBe(200);
|
||||
expect(await jetzt()).toBe("1 neu vom Handy, 1 neu ins Handy, 1 zusammengeführt");
|
||||
// Petra: Mobilnummer vom Handy ergänzt, Rolle bleibt
|
||||
expect(await kontakt("Petra Braun")).toMatchObject({ mobil: "0171 1", rolle: "bauherr" });
|
||||
expect(await kontakt("Jan Peters")).toMatchObject({ telefon: "0451 2", rolle: "sonstige" });
|
||||
expect(f.karten.size).toBe(3);
|
||||
expect(await jetzt()).toBe("alles gleich");
|
||||
const s = (await app.inject("/api/kontakte-abgleich")).json();
|
||||
expect(s).toMatchObject({ an: true, adressbuch: BUCH, moeglich: true, verknuepft: 3, geloescht: 0 });
|
||||
});
|
||||
|
||||
it("Änderungen gehen in beide Richtungen, Rest der Karte bleibt", async () => {
|
||||
const petra = await kontakt("Petra Braun");
|
||||
await app.inject({ method: "PATCH", url: `/api/kontakte/${petra.id}`, payload: { ort: "Ratzeburg" } });
|
||||
expect(await jetzt()).toBe("1 ins Handy geschrieben");
|
||||
const karte = f.karten.get(`${BUCH}a1.vcf`)!.karte;
|
||||
expect(felderAus(karte).ort).toBe("Ratzeburg");
|
||||
expect(karte).toContain("BDAY:1970-05-04");
|
||||
// am Handy geändert
|
||||
f.handy(`${BUCH}a2.vcf`, karteMit({ ...felderAus(f.karten.get(`${BUCH}a2.vcf`)!.karte), email: "jan@peters.de" }, "a2", f.karten.get(`${BUCH}a2.vcf`)!.karte));
|
||||
expect(await jetzt()).toBe("1 vom Handy übernommen");
|
||||
expect((await kontakt("Jan Peters")).email).toBe("jan@peters.de");
|
||||
});
|
||||
|
||||
it("im Handy gelöscht bleibt im Programm; im Programm gelöscht verschwindet die Karte", async () => {
|
||||
f.karten.delete(`${BUCH}a2.vcf`);
|
||||
expect(await jetzt()).toBe("1 im Handy gelöscht");
|
||||
expect(await kontakt("Jan Peters")).toBeTruthy();
|
||||
expect((await app.inject("/api/kontakte-abgleich/kontakte")).json().filter((k: { geloescht: boolean }) => k.geloescht)).toHaveLength(1);
|
||||
expect(await jetzt()).toBe("alles gleich");
|
||||
const holz = await kontakt("Holzhandel Nord");
|
||||
expect((await app.inject({ method: "DELETE", url: `/api/kontakte/${holz.id}` })).statusCode).toBeLessThan(300);
|
||||
expect(await jetzt()).toBe("1 aus dem Adressbuch entfernt");
|
||||
expect([...f.karten.values()].some((k) => k.karte.includes("Holzhandel"))).toBe(false);
|
||||
});
|
||||
|
||||
it(".vcf-Datei einlesen ergänzt und legt an", async () => {
|
||||
const r = await app.inject({ method: "POST", url: "/api/kontakte/vcf", payload: { text: "BEGIN:VCARD\nVERSION:3.0\nFN:Petra Braun\nTEL;type=HOME:04541 8899\nEND:VCARD\nBEGIN:VCARD\nVERSION:3.0\nFN:Stadt Ratzeburg\nORG:Stadt Ratzeburg;\nEND:VCARD\n" } });
|
||||
expect(r.json()).toEqual({ neu: 1, ergaenzt: 1, gleich: 0 });
|
||||
expect((await kontakt("Petra Braun")).telefon).toBe("04541 8899");
|
||||
expect((await app.inject({ method: "POST", url: "/api/kontakte/vcf", payload: { text: "kein vcard" } })).statusCode).toBe(400);
|
||||
});
|
||||
});
|
||||
67
test/vcard.test.ts
Normal file
67
test/vcard.test.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
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 }));
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user