MC/website/tests/news-delivery-worker.test.mjs
2026-11-27 23:43:05 +00:00

6 lines
1.9 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';
import {createNewsDelivery} from '../ops/news-delivery-worker.mjs';
const logger={log(){},warn(){}};
test('singleton delivery starts both existing outboxes and closes the database on shutdown',async()=>{const calls=[];const db={async close(){calls.push('closed');}};const discord={async initializeDiscordNews(){calls.push('discord-init');},async drainDiscordNews(){calls.push('discord-drain');return ['sent'];}};const forum={async initializeForumNews(){calls.push('forum-init');},async drainForumNews(){calls.push('forum-drain');return ['sent'];}};const worker=await createNewsDelivery(db,{discord,forum,config:{fixture:true},logger,intervalMs:60000});await worker.stop();assert.deepEqual(calls,['discord-init','forum-init','discord-drain','forum-drain','closed']);await worker.tick();assert.equal(calls.length,5);});
test('concurrent ticks share one delivery pass and shutdown waits for the active pass',async()=>{let calls=0,release,closed=false;const discord={async initializeDiscordNews(){},async drainDiscordNews(){calls++;if(calls===2)await new Promise(resolve=>{release=resolve;});return [];}};const worker=await createNewsDelivery({async close(){closed=true;}},{discord,forum:{},config:{fixture:true},forumEnabled:false,logger,intervalMs:60000});const a=worker.tick(),b=worker.tick();assert.equal(a,b);const stopping=worker.stop();assert.equal(closed,false);release();await stopping;assert.equal(calls,2);assert.equal(closed,true);});
test('one failing integration does not suppress the other delivery queue',async()=>{let forumCalls=0;const worker=await createNewsDelivery({async close(){}},{config:{fixture:true},discord:{async initializeDiscordNews(){},async drainDiscordNews(){throw Error('Unavailable');}},forum:{async initializeForumNews(){},async drainForumNews(){forumCalls++;return [];}},logger,intervalMs:60000});await worker.stop();assert.equal(forumCalls,1);});