127 lines
5.1 KiB
TypeScript
127 lines
5.1 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import { counterpartAddress, decideEmailMatch, extractSubjectProjektNummer, type EmailMatchInput } from "./email-match.js";
|
|
|
|
// VA-P23 — the auto-match rules (M09 Block E) incl. every spec edge case:
|
|
// primary-contact tie-break, own-address exclusion, newsletter exclusion,
|
|
// subject token precedence, 1-open-Projekt heuristic.
|
|
|
|
function input(overrides: Partial<EmailMatchInput> = {}): EmailMatchInput {
|
|
return {
|
|
direction: "inbound",
|
|
fromAddress: "max@kunde.de",
|
|
toAddresses: [{ address: "info@handwerk.de" }],
|
|
isNewsletter: false,
|
|
ownAddresses: ["info@handwerk.de"],
|
|
subjectProjektId: null,
|
|
kontaktCandidates: [],
|
|
leadCandidateIds: [],
|
|
openProjekteByKontaktId: {},
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
describe("extractSubjectProjektNummer", () => {
|
|
it("extracts the token in its variants", () => {
|
|
expect(extractSubjectProjektNummer("Re: [Projekt #123] Angebot")).toBe("123");
|
|
expect(extractSubjectProjektNummer("[projekt # P-42 ] Nachtrag")).toBe("P-42");
|
|
expect(extractSubjectProjektNummer("Kein Token")).toBeNull();
|
|
expect(extractSubjectProjektNummer(null)).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("counterpartAddress", () => {
|
|
it("uses the sender for inbound mail", () => {
|
|
expect(counterpartAddress(input())).toBe("max@kunde.de");
|
|
});
|
|
|
|
it("excludes the tenant's own mailbox addresses (self-mail)", () => {
|
|
expect(counterpartAddress(input({ fromAddress: "Info@Handwerk.de" }))).toBeNull();
|
|
});
|
|
|
|
it("uses the first non-own recipient for outbound mail", () => {
|
|
const result = counterpartAddress(
|
|
input({ direction: "outbound", fromAddress: "info@handwerk.de", toAddresses: [{ address: "info@handwerk.de" }, { address: "max@kunde.de" }] }),
|
|
);
|
|
expect(result).toBe("max@kunde.de");
|
|
});
|
|
});
|
|
|
|
describe("decideEmailMatch", () => {
|
|
it("hard-matches a Kontakt by exact email (high)", () => {
|
|
const result = decideEmailMatch(input({ kontaktCandidates: [{ kontaktId: "k1", primaer: true }] }));
|
|
expect(result).toMatchObject({ matchedKontaktId: "k1", matchedLeadId: null, matchedProjektId: null, confidence: "high" });
|
|
});
|
|
|
|
it("prefers the PRIMARY-email contact when several contacts share the address", () => {
|
|
const result = decideEmailMatch(
|
|
input({
|
|
kontaktCandidates: [
|
|
{ kontaktId: "k-secondary", primaer: false },
|
|
{ kontaktId: "k-primary", primaer: true },
|
|
],
|
|
}),
|
|
);
|
|
expect(result.matchedKontaktId).toBe("k-primary");
|
|
});
|
|
|
|
it("matches a Lead only when no Kontakt matched", () => {
|
|
const leadOnly = decideEmailMatch(input({ leadCandidateIds: ["l1"] }));
|
|
expect(leadOnly).toMatchObject({ matchedKontaktId: null, matchedLeadId: "l1", confidence: "high" });
|
|
|
|
const both = decideEmailMatch(input({ kontaktCandidates: [{ kontaktId: "k1", primaer: true }], leadCandidateIds: ["l1"] }));
|
|
expect(both.matchedLeadId).toBeNull();
|
|
expect(both.matchedKontaktId).toBe("k1");
|
|
});
|
|
|
|
it("a valid [Projekt #] subject token matches high — even without a Kontakt", () => {
|
|
const result = decideEmailMatch(input({ subjectProjektId: "p42" }));
|
|
expect(result).toMatchObject({ matchedProjektId: "p42", confidence: "high" });
|
|
});
|
|
|
|
it("applies the 1-open-Projekt heuristic as medium", () => {
|
|
const result = decideEmailMatch(
|
|
input({ kontaktCandidates: [{ kontaktId: "k1", primaer: true }], openProjekteByKontaktId: { k1: ["p1"] } }),
|
|
);
|
|
expect(result).toMatchObject({ matchedKontaktId: "k1", matchedProjektId: "p1", confidence: "medium" });
|
|
});
|
|
|
|
it("does NOT auto-assign a Projekt when the Kontakt has several open ones", () => {
|
|
const result = decideEmailMatch(
|
|
input({ kontaktCandidates: [{ kontaktId: "k1", primaer: true }], openProjekteByKontaktId: { k1: ["p1", "p2"] } }),
|
|
);
|
|
expect(result.matchedProjektId).toBeNull();
|
|
expect(result.matchedKontaktId).toBe("k1");
|
|
expect(result.confidence).toBe("high");
|
|
});
|
|
|
|
it("the subject token beats the heuristic", () => {
|
|
const result = decideEmailMatch(
|
|
input({ subjectProjektId: "p-subject", kontaktCandidates: [{ kontaktId: "k1", primaer: true }], openProjekteByKontaktId: { k1: ["p-heuristic"] } }),
|
|
);
|
|
expect(result.matchedProjektId).toBe("p-subject");
|
|
expect(result.confidence).toBe("high");
|
|
});
|
|
|
|
it("never matches newsletters (List-Unsubscribe)", () => {
|
|
const result = decideEmailMatch(
|
|
input({ isNewsletter: true, kontaktCandidates: [{ kontaktId: "k1", primaer: true }], subjectProjektId: "p1" }),
|
|
);
|
|
expect(result).toMatchObject({ matchedKontaktId: null, matchedLeadId: null, matchedProjektId: null, confidence: "none" });
|
|
});
|
|
|
|
it("returns none (manually assignable) when nothing matches", () => {
|
|
const result = decideEmailMatch(input());
|
|
expect(result.confidence).toBe("none");
|
|
expect(result.counterpartAddress).toBe("max@kunde.de");
|
|
});
|
|
|
|
it("does not contact-match a self-addressed mail but keeps a subject-token Projekt", () => {
|
|
const result = decideEmailMatch(
|
|
input({ fromAddress: "info@handwerk.de", subjectProjektId: "p9", kontaktCandidates: [] }),
|
|
);
|
|
expect(result.matchedKontaktId).toBeNull();
|
|
expect(result.matchedProjektId).toBe("p9");
|
|
});
|
|
});
|