313 lines
13 KiB
TypeScript
313 lines
13 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
buildActivityStreamVM,
|
|
buildCustomFieldAdminVM,
|
|
buildDuplicateVM,
|
|
buildKontaktCsv,
|
|
buildKontaktDetailVM,
|
|
buildKontaktListVM,
|
|
buildTagAdminVM,
|
|
type BuildActivityStreamVMInput,
|
|
type BuildCustomFieldAdminVMInput,
|
|
type BuildDuplicateVMInput,
|
|
type BuildKontaktDetailVMInput,
|
|
type BuildKontaktListVMInput,
|
|
type BuildTagAdminVMInput,
|
|
type KontaktListAppliedFilter,
|
|
} from "../index.js";
|
|
|
|
const filter: KontaktListAppliedFilter = {
|
|
kontaktTyp: null,
|
|
kontaktStatus: null,
|
|
zustaendigerUserId: null,
|
|
search: null,
|
|
tagIds: null,
|
|
tagMatch: "or",
|
|
includeArchived: false,
|
|
};
|
|
|
|
// --- kontakt-list -----------------------------------------------------------
|
|
describe("buildKontaktListVM", () => {
|
|
const baseInput: BuildKontaktListVMInput = {
|
|
rows: [
|
|
{
|
|
id: "k1",
|
|
kontaktTyp: "privatperson",
|
|
kontaktStatus: "kunde",
|
|
vorname: "Anna",
|
|
nachname: "Muster",
|
|
orgName: null,
|
|
orgMerkmal: null,
|
|
primaerEmail: "anna@example.com",
|
|
primaerTelefon: null,
|
|
zustaendigerUserId: "u1",
|
|
createdAt: new Date("2026-06-20T08:00:00.000Z"),
|
|
archivedAt: null,
|
|
},
|
|
{
|
|
id: "k2",
|
|
kontaktTyp: "organisation",
|
|
kontaktStatus: "bestandskunde",
|
|
vorname: null,
|
|
nachname: null,
|
|
orgName: "Acme GmbH",
|
|
orgMerkmal: "gmbh",
|
|
primaerEmail: null,
|
|
primaerTelefon: "+49 30 123",
|
|
zustaendigerUserId: null,
|
|
createdAt: new Date("2026-06-21T08:00:00.000Z"),
|
|
archivedAt: new Date("2026-06-22T08:00:00.000Z"),
|
|
},
|
|
],
|
|
userLabels: new Map([["u1", "Raffael"]]),
|
|
tagsByKontakt: new Map([["k1", [{ id: "t1", name: "VIP", farbe: "#f00" }]]]),
|
|
pagination: { page: 2, pageCount: 3, total: 120 },
|
|
sort: { field: "name", dir: "asc" },
|
|
filter,
|
|
perms: { canWrite: true, canDelete: false, canReadAll: true },
|
|
tenantSlug: "acme",
|
|
};
|
|
|
|
it("returns an empty state when there are no contacts", () => {
|
|
const vm = buildKontaktListVM({ ...baseInput, rows: [] });
|
|
expect(vm.status).toBe("empty");
|
|
});
|
|
|
|
it("maps rows with display names, i18n keys, tag chips and internal hrefs", () => {
|
|
const vm = buildKontaktListVM(baseInput);
|
|
if (vm.status !== "ready") throw new Error("expected ready");
|
|
const [r1, r2] = vm.data.rows;
|
|
expect(r1?.displayName).toBe("Anna Muster");
|
|
expect(r1?.typLabelKey).toBe("crm.kontaktTyp.privatperson");
|
|
expect(r1?.statusLabelKey).toBe("crm.kontaktStatus.kunde");
|
|
expect(r1?.zustaendigerLabel).toBe("Raffael");
|
|
expect(r1?.tagChips[0]?.name).toBe("VIP");
|
|
expect(r1?.orgMerkmalLabelKey).toBeNull();
|
|
expect(r1?.detailHref).toBe("/acme/contacts/k1");
|
|
expect(r1?.isArchived).toBe(false);
|
|
expect(r1?.createdAtIso).toBe("2026-06-20T08:00:00.000Z");
|
|
// organisation row
|
|
expect(r2?.displayName).toBe("Acme GmbH");
|
|
expect(r2?.orgMerkmalLabelKey).toBe("crm.orgMerkmal.gmbh");
|
|
expect(r2?.zustaendigerLabel).toBeNull();
|
|
expect(r2?.isArchived).toBe(true);
|
|
});
|
|
|
|
it("derives pagination hasPrev/hasNext and echoes filter/sort/perms", () => {
|
|
const vm = buildKontaktListVM(baseInput);
|
|
if (vm.status !== "ready") throw new Error("expected ready");
|
|
expect(vm.data.pagination).toMatchObject({ page: 2, pageCount: 3, total: 120, hasPrev: true, hasNext: true });
|
|
expect(vm.data.sort).toEqual({ field: "name", dir: "asc" });
|
|
expect(vm.data.appliedFilter.tagMatch).toBe("or");
|
|
expect(vm.data.canReadAll).toBe(true);
|
|
});
|
|
|
|
it("fills the optional org-only fields only when supplied", () => {
|
|
const vm = buildKontaktListVM({
|
|
...baseInput,
|
|
orgKontaktAnzahl: new Map([["k2", 4]]),
|
|
orgLastActivityIso: new Map([["k2", "2026-06-23T08:00:00.000Z"]]),
|
|
});
|
|
if (vm.status !== "ready") throw new Error("expected ready");
|
|
expect(vm.data.rows[0]?.kontaktAnzahl).toBeUndefined();
|
|
expect(vm.data.rows[1]?.kontaktAnzahl).toBe(4);
|
|
expect(vm.data.rows[1]?.letzteActivityIso).toBe("2026-06-23T08:00:00.000Z");
|
|
});
|
|
});
|
|
|
|
// --- kontakt-detail ---------------------------------------------------------
|
|
describe("buildKontaktDetailVM", () => {
|
|
const base: BuildKontaktDetailVMInput = {
|
|
detail: {
|
|
kontakt: {
|
|
id: "k1",
|
|
kontaktTyp: "privatperson",
|
|
kontaktStatus: "kontakt",
|
|
vorname: "Anna",
|
|
nachname: "Muster",
|
|
orgName: null,
|
|
orgMerkmal: null,
|
|
primaerEmail: "anna@example.com",
|
|
zustaendigerUserId: "u1",
|
|
archivedAt: null,
|
|
createdAt: new Date("2026-06-20T08:00:00.000Z"),
|
|
updatedAt: new Date("2026-06-21T08:00:00.000Z"),
|
|
},
|
|
emails: [{ id: "e1", email: "anna@example.com", primaer: true }],
|
|
telefone: [{ id: "p1", telefon: "+49 30 1", primaer: true }],
|
|
relationen: [
|
|
{ id: "rel1", vonKontaktId: "k1", zuKontaktId: "k9", rolle: "hauptkontakt", kontext: "Buchhaltung" },
|
|
{ id: "rel2", vonKontaktId: "k7", zuKontaktId: "k1", rolle: "mitarbeiter", kontext: null },
|
|
],
|
|
adresse: { strasse: "Hauptstr", hausnummer: "1", plz: "10115", ort: "Berlin", land: "DE" },
|
|
},
|
|
customFields: [{ definition: { id: "d1", key: "umsatz", label: "Umsatz", datentyp: "waehrung", optionen: null }, wert: 1000 }],
|
|
tags: [{ id: "t1", name: "VIP", farbe: "#f00" }],
|
|
userLabels: new Map([["u1", "Raffael"]]),
|
|
perms: { canWrite: true, canDelete: false },
|
|
tenantSlug: "acme",
|
|
};
|
|
|
|
it("maps channels, relations (von/zu), custom fields and i18n keys", () => {
|
|
const vm = buildKontaktDetailVM(base);
|
|
if (vm.status !== "ready") throw new Error("expected ready");
|
|
expect(vm.data.displayName).toBe("Anna Muster");
|
|
expect(vm.data.typLabelKey).toBe("crm.kontaktTyp.privatperson");
|
|
expect(vm.data.emails[0]?.primaer).toBe(true);
|
|
const von = vm.data.relationen.find((r) => r.id === "rel1");
|
|
const zu = vm.data.relationen.find((r) => r.id === "rel2");
|
|
expect(von?.richtung).toBe("von");
|
|
expect(von?.otherKontaktId).toBe("k9");
|
|
expect(von?.rolleLabelKey).toBe("crm.kontaktArt.hauptkontakt");
|
|
expect(zu?.richtung).toBe("zu");
|
|
expect(zu?.otherKontaktId).toBe("k7");
|
|
expect(vm.data.customFields[0]).toMatchObject({ definitionId: "d1", key: "umsatz", datentyp: "waehrung", wert: 1000 });
|
|
expect(vm.data.zustaendigerLabel).toBe("Raffael");
|
|
expect(vm.data.listHref).toBe("/acme/contacts");
|
|
});
|
|
|
|
it("derives quickActions from perms (archive when active, +delete when deletable)", () => {
|
|
const vm = buildKontaktDetailVM(base);
|
|
if (vm.status !== "ready") throw new Error("expected ready");
|
|
expect(vm.data.quickActions).toEqual(["addNote", "addTag", "reassign", "archive"]);
|
|
|
|
const archived = buildKontaktDetailVM({
|
|
...base,
|
|
detail: { ...base.detail, kontakt: { ...base.detail.kontakt, archivedAt: new Date() } },
|
|
perms: { canWrite: true, canDelete: true },
|
|
});
|
|
if (archived.status !== "ready") throw new Error("expected ready");
|
|
expect(archived.data.quickActions).toContain("reactivate");
|
|
expect(archived.data.quickActions).toContain("delete");
|
|
|
|
const readOnly = buildKontaktDetailVM({ ...base, perms: { canWrite: false, canDelete: false } });
|
|
if (readOnly.status !== "ready") throw new Error("expected ready");
|
|
expect(readOnly.data.quickActions).toEqual([]);
|
|
});
|
|
});
|
|
|
|
// --- activity-stream --------------------------------------------------------
|
|
describe("buildActivityStreamVM", () => {
|
|
const input: BuildActivityStreamVMInput = {
|
|
rows: [
|
|
{ id: "a1", createdAt: new Date("2026-06-20T08:00:00.000Z"), actorUserId: "u1", actorKind: "user", activityType: "note_added", summaryText: "Notiz: Rückruf vereinbart" },
|
|
{ id: "a2", createdAt: new Date("2026-06-20T09:00:00.000Z"), actorUserId: null, actorKind: "system", activityType: "contact_created", summaryText: "Kontakt angelegt" },
|
|
],
|
|
userLabels: new Map([["u1", "Raffael"]]),
|
|
hasMore: true,
|
|
};
|
|
|
|
it("returns an empty state when there are no entries", () => {
|
|
expect(buildActivityStreamVM({ ...input, rows: [] }).status).toBe("empty");
|
|
});
|
|
|
|
it("passes summaryText through and derives label/icon/actor", () => {
|
|
const vm = buildActivityStreamVM(input);
|
|
if (vm.status !== "ready") throw new Error("expected ready");
|
|
expect(vm.data.hasMore).toBe(true);
|
|
const e1 = vm.data.entries[0];
|
|
expect(e1?.summaryText).toBe("Notiz: Rückruf vereinbart");
|
|
expect(e1?.activityLabelKey).toBe("crm.activity.note_added");
|
|
expect(e1?.iconKey).toBe("note");
|
|
expect(e1?.actorLabel).toBe("Raffael");
|
|
const e2 = vm.data.entries[1];
|
|
expect(e2?.actorLabel).toBeNull();
|
|
expect(e2?.iconKey).toBe("contact");
|
|
});
|
|
});
|
|
|
|
// --- custom-field-admin -----------------------------------------------------
|
|
describe("buildCustomFieldAdminVM", () => {
|
|
const input: BuildCustomFieldAdminVMInput = {
|
|
rows: [{ id: "d1", entityType: "kontakt", key: "umsatz", label: "Umsatz", datentyp: "waehrung", optionen: null, archiviert: false, isDemoData: false }],
|
|
entityType: "kontakt",
|
|
canWrite: true,
|
|
};
|
|
|
|
it("stays ready (with the CTA context) even when empty", () => {
|
|
const vm = buildCustomFieldAdminVM({ ...input, rows: [] });
|
|
if (vm.status !== "ready") throw new Error("expected ready");
|
|
expect(vm.data.rows).toHaveLength(0);
|
|
expect(vm.data.entityType).toBe("kontakt");
|
|
expect(vm.data.canWrite).toBe(true);
|
|
});
|
|
|
|
it("maps datentyp label keys", () => {
|
|
const vm = buildCustomFieldAdminVM(input);
|
|
if (vm.status !== "ready") throw new Error("expected ready");
|
|
expect(vm.data.rows[0]?.datentypLabelKey).toBe("crm.datentyp.waehrung");
|
|
});
|
|
});
|
|
|
|
// --- tag-admin --------------------------------------------------------------
|
|
describe("buildTagAdminVM", () => {
|
|
const input: BuildTagAdminVMInput = {
|
|
tags: [
|
|
{ id: "t1", name: "VIP", farbe: "#f00" },
|
|
{ id: "t2", name: "Lead", farbe: "#0f0" },
|
|
],
|
|
usageCounts: new Map([["t1", 5]]),
|
|
canWrite: true,
|
|
};
|
|
|
|
it("defaults usageCount to 0 when no count is supplied", () => {
|
|
const vm = buildTagAdminVM(input);
|
|
if (vm.status !== "ready") throw new Error("expected ready");
|
|
expect(vm.data.rows[0]?.usageCount).toBe(5);
|
|
expect(vm.data.rows[1]?.usageCount).toBe(0);
|
|
});
|
|
});
|
|
|
|
// --- duplicate --------------------------------------------------------------
|
|
describe("buildDuplicateVM", () => {
|
|
const input: BuildDuplicateVMInput = {
|
|
pairs: [
|
|
{
|
|
matchType: "hard",
|
|
matchReason: "email",
|
|
a: { id: "k1", kontaktTyp: "privatperson", kontaktStatus: "kontakt", vorname: "Anna", nachname: "Muster", orgName: null, primaerEmail: "a@x.de", primaerTelefon: null, zustaendigerUserId: "u1", createdAt: new Date("2026-06-20T08:00:00.000Z") },
|
|
b: { id: "k2", kontaktTyp: "privatperson", kontaktStatus: "kunde", vorname: "Anna", nachname: "Muster", orgName: null, primaerEmail: "a@x.de", primaerTelefon: null, zustaendigerUserId: null, createdAt: new Date("2026-06-21T08:00:00.000Z") },
|
|
},
|
|
],
|
|
userLabels: new Map([["u1", "Raffael"]]),
|
|
canMerge: true,
|
|
tenantSlug: "acme",
|
|
};
|
|
|
|
it("returns an empty state when there are no duplicate pairs", () => {
|
|
expect(buildDuplicateVM({ ...input, pairs: [] }).status).toBe("empty");
|
|
});
|
|
|
|
it("builds side-by-side cards with reason keys and internal hrefs", () => {
|
|
const vm = buildDuplicateVM(input);
|
|
if (vm.status !== "ready") throw new Error("expected ready");
|
|
const pair = vm.data.pairs[0];
|
|
expect(pair?.matchType).toBe("hard");
|
|
expect(pair?.matchReasonKey).toBe("crm.duplicate.reason.email");
|
|
expect(pair?.a.detailHref).toBe("/acme/contacts/k1");
|
|
expect(pair?.a.zustaendigerLabel).toBe("Raffael");
|
|
expect(pair?.b.zustaendigerLabel).toBeNull();
|
|
expect(vm.data.canMerge).toBe(true);
|
|
});
|
|
});
|
|
|
|
// --- kontakt-csv ------------------------------------------------------------
|
|
describe("buildKontaktCsv", () => {
|
|
it("writes a German header and escapes RFC 4180 special characters", () => {
|
|
const csv = buildKontaktCsv([
|
|
{ displayName: 'Muster, Anna "die Erste"', kontaktTyp: "privatperson", kontaktStatus: "kunde", primaerEmail: "a@x.de", primaerTelefon: null, zustaendigerLabel: "Raffael", createdAtIso: "2026-06-20T08:00:00.000Z" },
|
|
]);
|
|
const lines = csv.split("\r\n");
|
|
expect(lines[0]).toBe("Name,Typ,Status,E-Mail,Telefon,Zuständig,Erstellt am");
|
|
// comma + quote in the name -> field quoted, inner quotes doubled
|
|
expect(lines[1]).toContain('"Muster, Anna ""die Erste"""');
|
|
// null telefon becomes an empty field
|
|
expect(lines[1]).toBe('"Muster, Anna ""die Erste""",privatperson,kunde,a@x.de,,Raffael,2026-06-20T08:00:00.000Z');
|
|
});
|
|
|
|
it("emits a header-only string for an empty row set", () => {
|
|
expect(buildKontaktCsv([])).toBe("Name,Typ,Status,E-Mail,Telefon,Zuständig,Erstellt am");
|
|
});
|
|
});
|