import { test } from 'node:test'; import assert from 'node:assert/strict'; import { createDiscordNotifier } from '../src/discord.js'; const URL_OK = 'https://discord.com/api/webhooks/123456/geheim_TOKEN-1'; test('Ohne URL ist der Notifier aus und tut nichts', async () => { let called = false; const n = createDiscordNotifier('', { fetchImpl: async () => { called = true; } }); assert.equal(n.enabled, false); await n.send('Test'); assert.equal(called, false); }); test('Ungültige URL wird beim Start abgelehnt, ohne die URL zu zeigen', () => { assert.throws(() => createDiscordNotifier('https://example.com/geheim'), (err) => { assert.match(err.message, /keine gültige Discord-Webhook-URL/); assert.ok(!err.message.includes('geheim')); return true; }); }); test('Nachricht wird als JSON an den Webhook geschickt, überlange Texte gekürzt', async () => { const sent = []; const n = createDiscordNotifier(URL_OK, { fetchImpl: async (url, init) => { sent.push({ url, init }); return { ok: true, status: 204 }; }, }); await n.send('Hallo'); await n.send('x'.repeat(5000)); assert.equal(sent[0].url, URL_OK); assert.equal(sent[0].init.method, 'POST'); assert.equal(JSON.parse(sent[0].init.body).content, 'Hallo'); assert.equal(JSON.parse(sent[1].init.body).content.length, 1900); }); test('Fehler des Webhooks brechen den Ablauf nicht ab und werden protokolliert ohne URL', async () => { const warnings = []; const logger = { warn: (m) => warnings.push(m) }; const failing = createDiscordNotifier(URL_OK, { logger, fetchImpl: async () => { throw new TypeError('fetch failed'); } }); await failing.send('Test'); const bad = createDiscordNotifier(URL_OK, { logger, fetchImpl: async () => ({ ok: false, status: 404 }) }); await bad.send('Test'); assert.equal(warnings.length, 2); assert.match(warnings[1], /HTTP 404/); assert.ok(warnings.every((w) => !w.includes('TOKEN'))); });