import { test } from 'node:test'; import assert from 'node:assert/strict'; import { evaluateSpam } from '../src/domain/spamGate.js'; import type { SupportMessageEvent } from '../src/domain/messageNormalizer.js'; function evt(overrides: Partial = {}): SupportMessageEvent { return { source: 'chatwoot', channel: 'email', conversationId: 1311, messageId: '1', inboxId: 1, content: 'Czy ten produkt pasuje do mojego auta?', subject: 'Pytanie', senderEmail: 'klient@example.com', senderName: 'Klient', senderId: '1', labels: [], customAttributes: {}, additionalAttributes: {}, attachmentCount: 0, needsConversationFetch: false, ...overrides, }; } test('a normal customer question passes', () => { assert.equal(evaluateSpam(evt()).spam, false); }); test('mailer-daemon is blocked', () => { const v = evaluateSpam(evt({ senderEmail: 'mailer-daemon@example.com' })); assert.equal(v.spam, true); assert.match(v.reason, /automated_sender/); }); test('no-reply senders are blocked', () => { assert.equal(evaluateSpam(evt({ senderEmail: 'no-reply@shop.example' })).spam, true); assert.equal(evaluateSpam(evt({ senderEmail: 'noreply@shop.example' })).spam, true); }); test('bounce subjects are blocked', () => { const v = evaluateSpam(evt({ subject: 'Undelivered Mail Returned to Sender' })); assert.equal(v.spam, true); assert.equal(v.reason, 'bounce_subject'); }); test('out-of-office autoreplies are blocked', () => { assert.equal(evaluateSpam(evt({ subject: 'Automatic reply: your message' })).spam, true); assert.equal(evaluateSpam(evt({ subject: 'Automatyczna odpowiedź' })).spam, true); }); test('DMARC aggregate reports are blocked', () => { assert.equal(evaluateSpam(evt({ subject: 'Report domain: easyklima.com' })).spam, true); }); test('empty bodies and attachment-only mails are blocked', () => { assert.equal(evaluateSpam(evt({ content: '' })).reason, 'empty_body'); assert.equal(evaluateSpam(evt({ content: '', attachmentCount: 2 })).reason, 'attachment_only'); }); test('auto-submitted headers are blocked', () => { const v = evaluateSpam( evt({ additionalAttributes: { headers: { 'Auto-Submitted': 'auto-replied' } } }), ); assert.equal(v.spam, true); assert.equal(v.reason, 'header:auto-submitted'); }); test('auto-submitted: no is NOT treated as automated', () => { const v = evaluateSpam(evt({ additionalAttributes: { headers: { 'Auto-Submitted': 'no' } } })); assert.equal(v.spam, false); }); test('list-unsubscribe marks a newsletter', () => { const v = evaluateSpam( evt({ additionalAttributes: { headers: { 'List-Unsubscribe': '' } } }), ); assert.equal(v.spam, true); }); test('a body with no words is blocked', () => { assert.equal(evaluateSpam(evt({ content: '??? !!! ...' })).reason, 'no_textual_content'); }); test('a legitimate mail from an address merely containing "notification" passes', () => { // Only exact local-part matches are automated; substrings must not trigger. assert.equal(evaluateSpam(evt({ senderEmail: 'notifications.jan@example.com' })).spam, false); });