import test from 'node:test' import assert from 'node:assert/strict' import { loadModule } from './load.mjs' // Chat im WOMS: Sortierung, Hinweis zur Kunden-Mail (K17, Texte wie im Admin-Panel), Zeilen und Neuladen const { sortChats, noticeText, lastLine, messageMeta, needsFullRefresh } = await loadModule('src/lib/chatFormat.js') test('sortChats: ungelesene zuerst, dann neueste', () => { const chats = [ { customerId: 'a', lastAt: '2026-10-01T10:00:00.000Z', unreadFromCustomer: 0 }, { customerId: 'b', lastAt: '2026-10-01T08:00:00.000Z', unreadFromCustomer: 2 }, { customerId: 'c', lastAt: '2026-10-01T12:00:00.000Z', unreadFromCustomer: 0 }, { customerId: 'd', lastAt: '2026-10-01T09:00:00.000Z', unreadFromCustomer: 1 }, ] assert.deepEqual(sortChats(chats).map((c) => c.customerId), ['d', 'b', 'c', 'a']) assert.equal(chats[0].customerId, 'a', 'Eingabe bleibt unveraendert') }) test('noticeText: gesendet, gedrosselt, Grund, leer', () => { assert.equal(noticeText({ sent: true, to: 'k@x.de' }, '14:05'), 'E-Mail-Hinweis an k@x.de gesendet (14:05)') assert.equal(noticeText({ sent: false, reason: 'throttled' }, '14:05'), 'Kein neuer E-Mail-Hinweis: Der Kunde wurde in den letzten 15 Minuten schon benachrichtigt.') assert.equal(noticeText({ sent: false, reason: 'SMTP weg' }, '14:05'), 'Kein E-Mail-Hinweis: SMTP weg') assert.equal(noticeText(null, '14:05'), '') }) test('messageMeta und lastLine', () => { assert.equal(messageMeta({ sender: 'admin', senderName: 'Andrej', createdAt: '2026-09-30T10:05:00.000Z' }, 'Berta Berg'), 'Andrej · 30.09.2026 12:05') assert.equal(messageMeta({ sender: 'admin', senderName: '', createdAt: '2026-09-30T10:05:00.000Z' }, 'Berta Berg'), 'Team · 30.09.2026 12:05') assert.equal(messageMeta({ sender: 'customer', senderName: 'x', createdAt: '2026-09-30T10:05:00.000Z' }, 'Berta Berg'), 'Berta Berg · 30.09.2026 12:05') assert.equal(lastLine({ lastSender: 'admin', lastText: 'Erledigt' }), 'Du: Erledigt') assert.equal(lastLine({ lastSender: 'customer', lastText: 'Danke' }), 'Danke') }) test('needsFullRefresh ab 10 Minuten', () => { assert.equal(needsFullRefresh(0, 599999), false) assert.equal(needsFullRefresh(0, 600000), true) }) // Abschlussreview: die Auswahl "Neue Nachricht an Kunde…" darf nur eindeutig gewaehlte Kunden oeffnen const { customerPickLabel, findCustomerByLabel } = await loadModule('src/lib/chatFormat.js') test('customerPickLabel: Name · E-Mail · Kd.-Nr. wie im Admin-Panel, ohne Name die E-Mail', () => { assert.equal(customerPickLabel({ $id: 'c1', name: 'Amin', email: 'a@x.de', code: '5' }), 'Amin · a@x.de · Kd.-Nr. 5') assert.equal(customerPickLabel({ $id: 'c3', name: '', email: 'leer@x.de' }), 'leer@x.de') }) test('findCustomerByLabel: Teil- oder Namens-Treffer oeffnen nichts, nur die volle Beschriftung', () => { const customers = [ { $id: 'c1', name: 'Amin', email: 'a@x.de', code: '5' }, { $id: 'c2', name: 'Amin Friseur', email: 'f@x.de', code: '6' }, ] assert.equal(findCustomerByLabel(customers, 'Amin'), null) assert.equal(findCustomerByLabel(customers, 'Amin Friseur'), null) assert.equal(findCustomerByLabel(customers, 'Amin Friseur · f@x.de · Kd.-Nr. 6')?.$id, 'c2') assert.equal(findCustomerByLabel(customers, 'Amin · a@x.de · Kd.-Nr. 5')?.$id, 'c1') })