rebreak-monorepo/backend/tests/mail/mail-classifier.test.ts
chahinebrini 00ec716694 fix(mail): skip Gmail system folders in scan + raise subject-keyword score to 50
Fix 1 (scan-internal): filter out \All, \Drafts, \Sent, \Trash, \Flagged via
specialUse — stops [Gmail]/All Mail from consuming the SCAN_LIMIT=200 and
blocking new INBOX mails from reaching fetch range. \Junk/\Spam stay in scope.
Folders without specialUse (iCloud, GMX) pass through untouched — no false
exclusions without confirmed metadata.

Fix 2 (mail-classifier): raise SUBJECT_GAMBLING_KEYWORD from 35 to 50 so a
single unambiguous casino/jackpot/freispiel subject hit alone reaches the
SCORE_BLOCK_MIDRANGE threshold and triggers a block. Previously 35 pts fell
short when sender domain was generic and display name empty.

Tests: 9 new cases added (2 Fix-2 classifier + 4 Fix-1 folder-filter unit +
1 computeScore score=50 exact assertion). All 265 tests green.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-16 05:12:14 +02:00

579 lines
22 KiB
TypeScript

/**
* Tests für mail-classifier.ts — Mail-Klassifikations-Pipeline.
*
* Testet alle Layer-Logiken als pure Funktionen (kein DB-Mock).
*
* Abgedeckt:
* - extractRelayedDomain() — diverse Relay-Patterns
* - normalizeBrand() — Normalisierungs-Logik
* - hasRandomTokens() — true/false cases
* - computeScore() — Score-Berechnung mit Weights
* - classifyMail() — End-to-End Pipeline:
* - Gamblezen-Beispiel → Layer 2.5 Hard-Block
* - BetandPlay-Beispiel → Layer 2.5 Hard-Block (Apple Hide-My-Email-Pattern)
* - Whitelist-Case (wettervorhersage)
* - Domain-Block (Layer 2)
* - Relay-Decoded Block (Layer 2)
* - No-Signal → PASS
*/
import { describe, it, expect, vi } from "vitest";
// gambling-keywords.mjs ist ESM ohne TypeScript — mock before import
vi.mock("../../server/utils/gambling-keywords.mjs", () => ({
GAMBLING_KEYWORDS: [
"casino", "bet365", "bwin", "tipico", "unibet", "betway",
"pokerstars", "jackpot", "freispiel", "free spin", "bonus code",
"auszahlung", "glücksspiel", "slots", "roulette", "wette",
"stake", "rolletto", "vbet", "1xbet", "melbet", "mostbet",
"luckyvibe", "spinz", "casinoly", "rabona", "justcasino",
"getslots", "rocketplay", "freshcasino", "betano", "leovegas",
],
GAMBLING_WHITELIST: [
"wettervorhersage",
"wetter",
"wetterbericht",
"wettkampf",
"wettbewerb",
],
}));
import {
extractRelayedDomain,
normalizeBrand,
hasRandomTokens,
computeScore,
classifyMail,
matchesGamblingBrand,
} from "../../server/utils/mail-classifier";
// ─── extractRelayedDomain ────────────────────────────────────────────────────
describe("extractRelayedDomain()", () => {
it("extrahiert Domain aus SendGrid-bounce-Pattern (user=domain@sendgrid)", () => {
expect(extractRelayedDomain("bounces+user=gamblezen.com@sendgrid.net"))
.toBe("gamblezen.com");
});
it("extrahiert Domain aus Mailchimp-Track-Pattern (track.user=domain@mc)", () => {
expect(extractRelayedDomain("track.user=betandplay.com@mailchimp.com"))
.toBe("betandplay.com");
});
it("extrahiert Domain aus _at_-Pattern", () => {
expect(extractRelayedDomain("a1b2c3_user_at_betandplay.com@em.example.com"))
.toBe("betandplay.com");
});
it("gibt null zurück wenn kein Relay-Pattern erkannt", () => {
expect(extractRelayedDomain("info@betandplay.com")).toBeNull();
});
it("gibt null zurück für direkte Adressen ohne @", () => {
expect(extractRelayedDomain("noatsign")).toBeNull();
});
it("normalisiert extrahierte Domain auf lowercase", () => {
expect(extractRelayedDomain("bounce=GambleZen.COM@delivery.net"))
.toBe("gamblezen.com");
});
it("gibt null zurück für normale Adressen ohne Relay-Muster", () => {
expect(extractRelayedDomain("newsletter@example.org")).toBeNull();
});
});
// ─── normalizeBrand ──────────────────────────────────────────────────────────
describe("normalizeBrand()", () => {
it("BetandPlay → betandplay", () => {
expect(normalizeBrand("BetandPlay")).toBe("betandplay");
});
it("bet-and-play → betandplay", () => {
expect(normalizeBrand("bet-and-play")).toBe("betandplay");
});
it("Gamble Zen → gamblezen", () => {
expect(normalizeBrand("Gamble Zen")).toBe("gamblezen");
});
it("Mr. Green → mrgreen", () => {
expect(normalizeBrand("Mr. Green")).toBe("mrgreen");
});
it("lucky_vibe → luckyvibe", () => {
expect(normalizeBrand("lucky_vibe")).toBe("luckyvibe");
});
it("unveränderte Kleinbuchstaben bleiben gleich", () => {
expect(normalizeBrand("casino")).toBe("casino");
});
});
// ─── matchesGamblingBrand ────────────────────────────────────────────────────
describe("matchesGamblingBrand()", () => {
it("'gamblezen' matcht", () => {
expect(matchesGamblingBrand("gamblezen")).toBe(true);
});
it("'betandplay' matcht", () => {
expect(matchesGamblingBrand("betandplay")).toBe(true);
});
it("'casino' matcht (exact)", () => {
expect(matchesGamblingBrand("casino")).toBe(true);
});
it("'mrgreen' matcht", () => {
expect(matchesGamblingBrand("mrgreen")).toBe(true);
});
it("'example' matcht nicht", () => {
expect(matchesGamblingBrand("example")).toBe(false);
});
it("zu kurze Strings (< 4 Zeichen) matchen nie", () => {
expect(matchesGamblingBrand("bet")).toBe(false);
});
it("'googlemail' matcht nicht", () => {
expect(matchesGamblingBrand("googlemail")).toBe(false);
});
});
// ─── hasRandomTokens ─────────────────────────────────────────────────────────
describe("hasRandomTokens()", () => {
it("local-part mit 2+ zufälligen Tokens → true", () => {
// Gamblezen-typisch: hq3a91_7xmpl2 (2 random-looking tokens)
expect(hasRandomTokens("hq3a91_7xmpl2")).toBe(true);
});
it("local-part mit User-ID + Token → true", () => {
expect(hasRandomTokens("user123abc_ref456xyz")).toBe(true);
});
it("'info' → false (Funktionswort)", () => {
expect(hasRandomTokens("info")).toBe(false);
});
it("'noreply' → false (Funktionswort)", () => {
expect(hasRandomTokens("noreply")).toBe(false);
});
it("'newsletter' → false (Funktionswort, kein Digit-Mix)", () => {
expect(hasRandomTokens("newsletter")).toBe(false);
});
it("normaler Local-Part ohne Zufalls-Tokens → false", () => {
expect(hasRandomTokens("john.doe")).toBe(false);
});
it("nur ein random Token (Grenzfall) → false", () => {
// Nur ein Token >= 6 mit Digit-Mix → unter Schwelle (braucht >= 2)
expect(hasRandomTokens("abc123")).toBe(false);
});
it("echter BetandPlay-typischer Local-Part → true", () => {
// z.B. "u7a2b1_offers_ref9x2z" — ein Funktionswort + 2 random tokens
expect(hasRandomTokens("u7a2b1_offers_ref9x2z")).toBe(true);
});
});
// ─── computeScore ────────────────────────────────────────────────────────────
describe("computeScore()", () => {
it("Whitelist-Hit → score=0, whitelistHit=true", () => {
const result = computeScore(
"info@wetter.de",
"Wetter Service",
"Wettervorhersage für morgen",
false,
false,
);
expect(result.whitelistHit).toBe(true);
expect(result.score).toBe(0);
});
it("Casino im Betreff → SUBJECT_GAMBLING_KEYWORD += 50", () => {
const result = computeScore(
"info@example.com",
null,
"Dein Casino-Bonus wartet",
false,
false,
);
expect(result.keywordHitsSubject).toContain("casino");
expect(result.score).toBe(50);
});
it("Geld-Pattern (100€) im Betreff → SUBJECT_MONEY_PATTERN += 20", () => {
const result = computeScore(
"info@example.com",
null,
"100€ Willkommensbonus jetzt sichern",
false,
false,
);
expect(result.styleFlags).toContain("money-pattern");
expect(result.score).toBeGreaterThanOrEqual(20);
});
it("Brand-Match ohne Random → BRAND_MATCH_NO_RANDOM += 35", () => {
const result = computeScore(
"info@example.com",
null,
"Normaler Betreff",
true, // brandMatch=true
false, // randomTokens=false
);
expect(result.score).toBeGreaterThanOrEqual(35);
});
it("Random-Tokens ohne Brand → RANDOM_TOKENS_NO_BRAND += 10", () => {
const result = computeScore(
"info@example.com",
null,
"Newsletter vom Tag",
false, // brandMatch=false
true, // randomTokens=true
);
expect(result.score).toBeGreaterThanOrEqual(10);
});
it("Score wird auf max 100 gecapped", () => {
// Alle Signale gleichzeitig → Score würde > 100 sein
const result = computeScore(
"slots@casinobonus.bet",
"Casino Jackpot",
"JACKPOT Casino 500€ Freispiele Nur heute Letzte chance",
true,
true,
);
expect(result.score).toBeLessThanOrEqual(100);
});
});
// ─── classifyMail() — Pipeline End-to-End ────────────────────────────────────
describe("classifyMail() — End-to-End Pipeline", () => {
// Leere Domain-Set für die meisten Tests (kein Domain-Hard-Block)
const emptyDomainSet = new Set<string>();
// ─── Screenshot-Beispiel 1: Gamblezen via Relay ───────────────────────────
it("Gamblezen-Beispiel: bounces+user=gamblezen.com@em.sendgrid.net → Layer 2.5 Hard-Block", async () => {
// Gamblezen leitet über SendGrid-Bounces: Domain "em.sendgrid.net" ist nicht geblockt,
// aber relay-decoded → "gamblezen.com" + local-part hat random tokens.
// gamblezen.com ist ein bekannter Gambling-Brand.
const domainSetWithGamblezen = new Set(["gamblezen.com"]);
const result = await classifyMail({
mail: {
senderEmail: "bounces+user=gamblezen.com@em.sendgrid.net",
senderName: "Gamble Zen",
subject: "Dein exklusives Angebot wartet",
},
blockedDomainSet: domainSetWithGamblezen,
});
// Relay-decoded domain matcht blocklist → Layer 2 (relay-decoded), NICHT Layer 2.5
expect(result.action).toBe("blocked");
expect(result.triggerSource).toBe("relay-decoded");
expect(result.relayDecodedDomain).toBe("gamblezen.com");
});
it("Gamblezen-Beispiel ohne Blocklist-Entry → Layer 2.5 Hard-Block via Brand+Random", async () => {
// Wenn gamblezen.com NICHT in der Blocklist ist: Brand+Random greift trotzdem
const result = await classifyMail({
mail: {
senderEmail: "hq3a91_7xmpl2@em.sendgrid.net",
senderName: "Gamble Zen", // Brand-Match via Display-Name
subject: "Dein exklusives Angebot wartet",
},
blockedDomainSet: emptyDomainSet,
});
expect(result.action).toBe("blocked");
expect(result.triggerSource).toBe("brand+random");
expect(result.features.brandMatch).toBe(true);
expect(result.features.randomTokens).toBe(true);
});
// ─── Screenshot-Beispiel 2: BetandPlay via Relay ─────────────────────────
it("BetandPlay-Beispiel: track.user=betandplay.com@mailchimp.com → Layer 2.5 Hard-Block", async () => {
const domainSetWithBetandPlay = new Set(["betandplay.com"]);
const result = await classifyMail({
mail: {
senderEmail: "track.user=betandplay.com@mailchimp.com",
senderName: "BetandPlay",
subject: "100€ Willkommensbonus — Nur heute!",
},
blockedDomainSet: domainSetWithBetandPlay,
});
expect(result.action).toBe("blocked");
expect(result.triggerSource).toBe("relay-decoded");
expect(result.relayDecodedDomain).toBe("betandplay.com");
});
it("BetandPlay-Beispiel ohne Blocklist-Entry → Layer 2.5 Hard-Block via Brand+Random", async () => {
const result = await classifyMail({
mail: {
senderEmail: "u7a2b1_offers_ref9x2z@mailchimp.com",
senderName: "BetandPlay", // Brand-Match via Display-Name
subject: "100€ Willkommensbonus",
},
blockedDomainSet: emptyDomainSet,
});
expect(result.action).toBe("blocked");
expect(result.triggerSource).toBe("brand+random");
expect(result.features.brandMatch).toBe(true);
expect(result.features.randomTokens).toBe(true);
});
// ─── Layer 1: Whitelist ───────────────────────────────────────────────────
it("Whitelist-Treffer: 'wettervorhersage' im Betreff → PASS", async () => {
const result = await classifyMail({
mail: {
senderEmail: "service@wetter.de",
senderName: "Wetter.de",
subject: "Wettervorhersage für morgen",
},
blockedDomainSet: emptyDomainSet,
});
expect(result.action).toBe("passed");
expect(result.triggerSource).toBe("whitelist");
});
it("'wettkampf' in Betreff → PASS (kein Gambling trotz 'wette')", async () => {
const result = await classifyMail({
mail: {
senderEmail: "info@sport.de",
senderName: null,
subject: "Wettkampf-Ergebnisse dieser Woche",
},
blockedDomainSet: emptyDomainSet,
});
expect(result.action).toBe("passed");
expect(result.triggerSource).toBe("whitelist");
});
// ─── Layer 2: Domain-Hard-Block ───────────────────────────────────────────
it("Domain in Blocklist → Layer 2 Hard-Block", async () => {
const domainSet = new Set(["casinoly.com"]);
const result = await classifyMail({
mail: {
senderEmail: "promo@casinoly.com",
senderName: "Casinoly",
subject: "Dein Bonus wartet",
},
blockedDomainSet: domainSet,
});
expect(result.action).toBe("blocked");
expect(result.triggerSource).toBe("domain");
expect(result.features.domainBlocked).toBe(true);
});
// ─── Relay-Decoded Block ──────────────────────────────────────────────────
it("Relay-Decoded: =domain.com in local-part und Domain in Blocklist → relay-decoded Block", async () => {
const domainSet = new Set(["rabona.com"]);
const result = await classifyMail({
mail: {
senderEmail: "bounce+track=rabona.com@em.sendgrid.net",
senderName: "Rabona Casino",
subject: "Exklusiv für dich",
},
blockedDomainSet: domainSet,
});
expect(result.action).toBe("blocked");
expect(result.triggerSource).toBe("relay-decoded");
expect(result.relayDecodedDomain).toBe("rabona.com");
});
// ─── Layer 3: Score-Hard-Block ────────────────────────────────────────────
it("Viele Signale → Score >= 80 → Hard-Block", async () => {
// Casino im Sender-Name + Jackpot im Betreff + Urgency + Geld-Pattern
const result = await classifyMail({
mail: {
senderEmail: "info@spinz-casino.example",
senderName: "Casino Jackpot Club",
subject: "JACKPOT 500€ Freispiele — Nur heute!",
},
blockedDomainSet: emptyDomainSet,
});
expect(result.action).toBe("blocked");
expect(result.triggerSource).toMatch(/^score:/);
expect(result.score).toBeGreaterThanOrEqual(80);
});
// ─── No-Signal → PASS ────────────────────────────────────────────────────
it("unauffällige Mail → PASS mit triggerSource 'no-signal'", async () => {
const result = await classifyMail({
mail: {
senderEmail: "newsletter@amazon.de",
senderName: "Amazon",
subject: "Deine Bestellung wurde versandt",
},
blockedDomainSet: emptyDomainSet,
});
expect(result.action).toBe("passed");
expect(result.triggerSource).toBe("no-signal");
expect(result.score).toBeLessThan(25);
});
// ─── Brand-Match ohne Random → kein Hard-Block, Score-Erhöhung ───────────
it("Brand-Match ohne Random-Tokens → kein Layer-2.5-Block, aber Score-Erhöhung", async () => {
const result = await classifyMail({
mail: {
senderEmail: "info@betandplay.com", // direktes info@, kein random
senderName: "BetandPlay",
subject: "Willkommen",
},
blockedDomainSet: emptyDomainSet,
});
// Kein Hard-Block Layer 2.5 (kein Random), aber Score erhöht durch Brand-Match
expect(result.triggerSource).not.toBe("brand+random");
expect(result.features.brandMatch).toBe(true);
expect(result.features.randomTokens).toBe(false);
// Score >= 35 (BRAND_MATCH_NO_RANDOM) — endet je nach anderen Signalen
expect(result.features.score).toBeGreaterThanOrEqual(35);
});
// ─── Korrekte Feature-Struktur im Result ─────────────────────────────────
it("Result-Features enthalten alle erwarteten Keys", async () => {
const result = await classifyMail({
mail: {
senderEmail: "promo@example.com",
senderName: null,
subject: "Test",
},
blockedDomainSet: emptyDomainSet,
});
expect(result.features).toHaveProperty("score");
expect(result.features).toHaveProperty("domainBlocked");
expect(result.features).toHaveProperty("relayDecoded");
expect(result.features).toHaveProperty("brandMatch");
expect(result.features).toHaveProperty("randomTokens");
expect(result.features).toHaveProperty("keywordHitsSubject");
expect(result.features).toHaveProperty("keywordHitsDomain");
expect(result.features).toHaveProperty("keywordHitsName");
expect(result.features).toHaveProperty("styleFlags");
expect(result.features).toHaveProperty("whitelistHit");
});
// ─── Fix 2: SUBJECT_GAMBLING_KEYWORD angehoben auf 50 ────────────────────
it("Fix 2: 'Casino Bonus' im Betreff, generischer Sender → Score=50 → BLOCK (war vorher PASS)", async () => {
// Vorher: SUBJECT_GAMBLING_KEYWORD=35 → Score 35 < SCORE_BLOCK_MIDRANGE=50 → PASS
// Jetzt: SUBJECT_GAMBLING_KEYWORD=50 → Score 50 >= 50 → BLOCK
const result = await classifyMail({
mail: {
senderEmail: "info@example.com",
senderName: null,
subject: "Casino Bonus",
},
blockedDomainSet: emptyDomainSet,
});
expect(result.action).toBe("blocked");
expect(result.triggerSource).toMatch(/^score:/);
expect(result.score).toBe(50);
expect(result.features.keywordHitsSubject).toContain("casino");
});
it("Fix 2: 'Hotel Las Vegas' im Betreff → kein Casino-Keyword → PASS", async () => {
// 'Las Vegas' enthält nicht 'casino' als Standalone-Wort — kein Keyword-Hit
const result = await classifyMail({
mail: {
senderEmail: "buchung@hotel-example.com",
senderName: "Hotel Example",
subject: "Ihre Buchung Hotel Las Vegas",
},
blockedDomainSet: emptyDomainSet,
});
expect(result.action).toBe("passed");
expect(result.features.keywordHitsSubject).toHaveLength(0);
});
});
// ─── Fix 1: Folder-Filter (System-Folder-Ausschluss) ──────────────────────────
// Hinweis: scan-internal ist ein Nitro-Handler (nicht reine Funktion) — die
// specialUse-Filter-Logik wird hier als Unit über die regex-Konstante getestet,
// da ein vollständiger IMAP-Mock außerhalb des Scope dieser Test-Suite liegt.
describe("Fix 1: System-Folder specialUse-Filter-Regex", () => {
// Repliziert die SKIP_SPECIAL_USE-Konstante aus scan-internal.post.ts
const SKIP_SPECIAL_USE = /^\\(All|Drafts|Sent|Trash|Flagged)$/;
type MockMailbox = { path: string; specialUse?: string; flags?: Set<string> };
function filterScannable(mailboxes: MockMailbox[]): MockMailbox[] {
return mailboxes.filter((mb) => {
if (mb.flags?.has("\\Noselect")) return false;
if (mb.specialUse && SKIP_SPECIAL_USE.test(mb.specialUse)) return false;
return true;
});
}
it("Gmail All Mail (specialUse='\\\\All') wird ausgeschlossen", () => {
const mailboxes: MockMailbox[] = [
{ path: "INBOX", specialUse: "\\Inbox" },
{ path: "[Gmail]/All Mail", specialUse: "\\All" },
{ path: "[Gmail]/Spam", specialUse: "\\Junk" },
];
const result = filterScannable(mailboxes);
expect(result.map((m) => m.path)).toEqual(["INBOX", "[Gmail]/Spam"]);
expect(result.map((m) => m.path)).not.toContain("[Gmail]/All Mail");
});
it("Drafts, Sent, Trash, Flagged werden ausgeschlossen", () => {
const mailboxes: MockMailbox[] = [
{ path: "INBOX" },
{ path: "Drafts", specialUse: "\\Drafts" },
{ path: "Sent", specialUse: "\\Sent" },
{ path: "Trash", specialUse: "\\Trash" },
{ path: "Starred", specialUse: "\\Flagged" },
{ path: "Spam", specialUse: "\\Junk" },
];
const result = filterScannable(mailboxes);
const paths = result.map((m) => m.path);
expect(paths).toContain("INBOX");
expect(paths).toContain("Spam");
expect(paths).not.toContain("Drafts");
expect(paths).not.toContain("Sent");
expect(paths).not.toContain("Trash");
expect(paths).not.toContain("Starred");
});
it("Folder ohne specialUse (iCloud/GMX) werden NICHT ausgeschlossen", () => {
// iCloud/GMX liefern kein specialUse-Field — der Filter lässt sie durch
const mailboxes: MockMailbox[] = [
{ path: "INBOX" },
{ path: "Junk" }, // kein specialUse → bleibt drin (wollen wir)
{ path: "Sent Items" }, // kein specialUse → bleibt drin (suboptimal aber sicher)
];
const result = filterScannable(mailboxes);
// Alle 3 bleiben — kein false positive ohne specialUse-Info
expect(result).toHaveLength(3);
});
it("Noselect-Folder wird immer ausgeschlossen (unabhängig von specialUse)", () => {
const mailboxes: MockMailbox[] = [
{ path: "INBOX" },
{ path: "[Gmail]", flags: new Set(["\\Noselect"]) },
];
const result = filterScannable(mailboxes);
expect(result.map((m) => m.path)).toEqual(["INBOX"]);
});
});