| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- 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> = {}): SupportMessageEvent {
- return {
- source: 'chatwoot',
- channel: 'email',
- conversationId: 1311,
- messageId: '1',
- inboxId: 1,
- content: 'Czy ten produkt pasuje do mojego auta?',
- subject: 'Pytanie',
- messageCreatedAt: new Date().toISOString(),
- senderEmail: 'klient@example.com',
- senderName: 'Klient',
- senderId: '1',
- labels: [],
- customAttributes: {},
- additionalAttributes: {},
- attachmentCount: 0,
- attachments: [],
- formMail: null,
- 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': '<mailto:x@y.z>' } } }),
- );
- 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);
- });
|