import test from 'node:test'; import assert from 'node:assert/strict'; import http from 'node:http'; import { once } from 'node:events'; import { createHmac } from 'node:crypto'; import { mkdtempSync, rmSync, rmdirSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { createStore } from '../src/store.js'; import { normalizeMessages } from '../src/teams-network-records.js'; import { refreshChatNames } from '../src/chat-names.js'; import { createWebhookService, sendWebhook, isAllowedWebhookAddress, validateWebhookUrl } from '../src/webhook-service.js'; function fixture(send=async()=>({status:204})) { const store=createStore(':memory:'); let time=Date.parse('2026-09-08T08:00:00Z'); const service=createWebhookService({store,send,now:()=>time,classify:record=>record.raw}); const add=(id,raw={},options={})=>store.upsertRecord({source:'web-api',accountId:'test-account',kind:'message',externalId:id,parentId:'test-chat',title:'Fictional',text:'Fictional body',occurredAt:new Date(time+100).toISOString(), raw:{senderId:'other-user',isFromSelf:false,mentionsSelf:false,isDirect:false,...raw},...options.record},{ingestion:options.ingestion || 'live'}); const subscription=(overrides={})=>service.configure({url:'https://receiver.example/secret-path?key=private',enabled:true,mode:'all',...overrides}); return {store,service,add,subscription,advance:ms=>{time+=ms;},now:()=>time,close:()=>store.close()}; } test('local participant-name enrichment does not grant private-chat notification evidence or replay imported metadata', async () => { const store=createStore(':memory:'), time=Date.parse('2026-09-08T08:00:00Z'); const self='22222222-2222-2222-2222-222222222222',peer='33333333-3333-3333-3333-333333333333'; const accountId=`web-api:11111111-1111-1111-1111-111111111111:${self}`,chatId=`19:${self}_${peer}@unq.gbl.spaces`; let sends=0; const service=createWebhookService({store,now:()=>time,send:async()=>{sends++;return{status:204};}}); try { service.configure({url:'https://receiver.example/hook',enabled:true,mode:'direct'}); store.upsertRecord({source:'web-api',accountId,kind:'message',externalId:'peer',parentId:chatId,title:'聊天消息',text:'🔥',occurredAt:new Date(time+100).toISOString(), raw:{senderId:peer,selfUserId:self,author:'Fictional colleague',mentions:[]}},{ingestion:'live'}); assert.equal(refreshChatNames(store,{accountId,chatId}),1); const chat=store.getRecordByIdentity({source:'web-api',accountId,kind:'chat',externalId:chatId}); assert.equal(chat.title,'Fictional colleague'); assert.equal(chat.raw.isOneOnOne,undefined); assert.equal(chat.raw.chatType,undefined); await service.dispatchOnce(); assert.equal(sends,0); assert.equal(service.listDeliveries().items.length,0); } finally {store.close();} }); test('notification modes distinguish mention of self, direct chat, self messages and unknown metadata',async()=>{ for(const [mode,expected] of [['all',3],['mention',1],['direct',1],['mention_or_direct',2]]) { let calls=0;const f=fixture(async()=>{calls++;return{status:204};}); try { f.subscription({mode}); f.add('group'); f.add('mention',{mentionsSelf:true}); f.add('direct',{isDirect:true}); f.add('mine',{isFromSelf:true,mentionsSelf:true,isDirect:true}); f.add('unknown',{isFromSelf:null,mentionsSelf:true}); await f.service.dispatchOnce();assert.equal(calls,expected,mode); }finally{f.close();} } }); test('captured API metadata flows through the real classifier, database and signed notification queue',async()=>{ const store=createStore(':memory:'), sent=[]; const time=Date.parse('2026-09-08T08:00:00Z'); const self='11111111-1111-4111-8111-111111111111', other='22222222-2222-4222-8222-222222222222'; const accountId=`web-api:33333333-3333-4333-8333-333333333333:${self}`; const service=createWebhookService({store,now:()=>time,send:async request=>{sent.push(JSON.parse(request.body));return{status:204};}}); try { service.configure({url:'https://receiver.example/fictional',enabled:true,mode:'mention_or_direct'}); store.upsertRecord({source:'web-api',accountId,kind:'chat',externalId:'fictional-direct',raw:{isOneOnOne:true}}); for(const [id,chatId,sender,mentions] of [ ['direct','fictional-direct',other,[]],['mention','fictional-group',other,[self]], ['other-mention','fictional-group',other,[other]],['self','fictional-direct',self,[self]], ]) { const {records}=normalizeMessages({messages:[{id,type:'Message',messagetype:'Text',content:'Fictional message', conversationid:chatId,conversationLink:`https://teams.cloud.microsoft/api/chatsvc/apac/v1/users/ME/conversations/${encodeURIComponent(chatId)}`, originalarrivaltime:new Date(time+100).toISOString(), from:`https://teams.cloud.microsoft/api/chatsvc/apac/v1/users/ME/contacts/${encodeURIComponent(`8:orgid:${sender}`)}`, properties:{mentions:JSON.stringify(mentions.map(id=>({mentionType:'person',mri:`8:orgid:${id}`})))}, }]},{accountId,chatId}); for(const record of records)store.upsertRecord(record,{ingestion:'live'}); } await service.dispatchOnce(); assert.equal(sent.length,2); assert.equal(sent.filter(p=>p.message.isDirect===true).length,1); assert.equal(sent.filter(p=>p.message.mentionsSelf===true).length,1); } finally {store.close();} }); test('new subscriptions, history/import/old discovery, edits and duplicate reads never flood reminders',async()=>{ let calls=0;const f=fixture(async()=>{calls++;return{status:204};}); try { f.add('before'); f.subscription(); f.add('fresh'); f.add('history',{}, {ingestion:'history'});f.add('import',{}, {ingestion:'import'}); f.add('old',{}, {record:{occurredAt:'2025-01-01T00:00:00Z'}}); f.add('unknown-time',{}, {record:{occurredAt:null}}); f.add('fresh'); f.add('fresh',{}, {record:{text:'Fictional edit'}}); await f.service.dispatchOnce();await f.service.dispatchOnce();assert.equal(calls,1); assert.equal(f.service.listDeliveries().items.length,1); }finally{f.close();} }); test('record, version and notification change sequence commit atomically',()=>{ const f=fixture(); try { assert.throws(()=>f.store.transaction(()=>{f.add('rolled-back');throw new Error('rollback');})); assert.equal(f.store.stats().total,0);assert.equal(f.store.readOne('SELECT COUNT(*) n FROM change_log').n,0); assert.equal(f.store.readOne('SELECT COUNT(*) n FROM message_live_observations').n,0); f.add('saved');f.add('saved');assert.equal(f.store.readOne('SELECT COUNT(*) n FROM change_log').n,1); }finally{f.close();} }); test('a fresh message first captured by history is notified once after identical live confirmation',async()=>{ const sent=[];const f=fixture(async request=>{sent.push(JSON.parse(request.body));return{status:204};}); try { f.subscription({includeContent:true}); f.add('history-first',{}, {ingestion:'history'}); await f.service.dispatchOnce();assert.equal(sent.length,0); const unchanged=f.add('history-first');assert.equal(unchanged.changed,false); assert.equal(f.store.readOne('SELECT COUNT(*) n FROM change_log').n,1); await f.service.dispatchOnce();assert.equal(sent.length,1); assert.equal(sent[0].message.text,'Fictional body'); f.add('history-first');f.add('history-first',{}, {record:{text:'Fictional later edit'}}); await f.service.dispatchOnce();assert.equal(sent.length,1); assert.equal(f.store.readOne('SELECT COUNT(*) n FROM message_live_observations').n,1); assert.equal(f.store.readOne('SELECT ingestion FROM change_log ORDER BY seq LIMIT 1').ingestion,'history'); }finally{f.close();} }); test('the first live snapshot is retained when a history record changes before live confirmation',async()=>{ let sent;const f=fixture(async request=>{sent=JSON.parse(request.body);return{status:204};}); try { f.subscription({includeContent:true});f.add('history-first',{}, {ingestion:'history',record:{text:'Fictional history snapshot'}}); f.add('history-first',{}, {record:{text:'Fictional first live snapshot'}}); f.add('history-first',{}, {record:{text:'Fictional later edit'}}); await f.service.dispatchOnce();assert.equal(sent.message.text,'Fictional first live snapshot'); const created=f.store.readOne("SELECT seq FROM change_log WHERE action='created'"); assert.equal(sent.sequence,created.seq); assert.equal(f.service.listDeliveries().items[0].seq,created.seq); }finally{f.close();} }); test('imported, unknown, old history and pre-change-log records never become new-message reminders',async()=>{ let sent=0;const f=fixture(async()=>{sent++;return{status:204};}); try { f.subscription(); for(const ingestion of ['import','unknown']) { f.add(ingestion,{}, {ingestion});f.add(ingestion);f.add(ingestion,{}, {record:{text:'Fictional edited import'}}); } f.add('legacy',{}, {ingestion:'history'}); // The previous release stored versions but had no creation change events. f.store.execute("DELETE FROM change_log WHERE recordId=(SELECT id FROM records WHERE externalId='legacy')"); f.add('legacy');f.add('legacy',{}, {record:{text:'Fictional legacy edit'}}); const oldRecord={occurredAt:'2025-01-01T00:00:00Z'}; f.add('old-history',{}, {ingestion:'history',record:oldRecord});f.add('old-history',{}, {record:oldRecord}); await f.service.dispatchOnce();assert.equal(sent,0); assert.equal(f.store.readOne('SELECT COUNT(*) n FROM message_live_observations').n,1); }finally{f.close();} }); test('history-to-live promotion and its observation roll back together',()=>{ const f=fixture(); try { f.add('history-first',{}, {ingestion:'history'}); assert.throws(()=>f.store.transaction(()=>{f.add('history-first');throw new Error('rollback');})); assert.equal(f.store.readOne('SELECT COUNT(*) n FROM message_live_observations').n,0); assert.equal(f.store.readOne('SELECT COUNT(*) n FROM change_log').n,1); f.add('history-first');assert.equal(f.store.readOne('SELECT COUNT(*) n FROM message_live_observations').n,1); }finally{f.close();} }); test('pending deliveries survive restart and abandoned leases retry once with the original event ID',async()=>{ const directory=mkdtempSync(join(tmpdir(),'teams-webhook-restart-')), filename=join(directory,'fictional.sqlite'); let store=createStore(filename), time=Date.parse('2026-09-08T08:00:00Z'), sent=[]; const options=()=>({store,now:()=>time,classify:record=>record.raw,send:async request=>{sent.push(JSON.parse(request.body));return{status:204};}}); try { let service=createWebhookService(options()); service.configure({url:'https://receiver.example/fictional',enabled:true,mode:'all'}); const record={source:'web-api',accountId:'fictional-account',kind:'message',externalId:'fictional-message',title:'Fictional',text:'Fictional', occurredAt:new Date(time+100).toISOString(),raw:{isFromSelf:false}}; store.upsertRecord(record,{ingestion:'history'});store.upsertRecord(record,{ingestion:'live'}); await service.dispatchOnce({signal:AbortSignal.abort()}); const delivery=service.listDeliveries().items[0];assert.equal(delivery.status,'pending'); store.execute("UPDATE webhook_deliveries SET status='inflight',attempts=1,leaseToken='fictional-abandoned-lease',leaseUntil=? WHERE id=?", [new Date(time+60_000).toISOString(),delivery.id]); store.close();store=createStore(filename);service=createWebhookService(options()); await service.dispatchOnce();assert.equal(sent.length,0); time+=60_000;await service.dispatchOnce();assert.equal(sent.length,1);assert.equal(sent[0].id,delivery.id); assert.equal(service.listDeliveries().items[0].attempts,2); store.upsertRecord(record,{ingestion:'live'});await service.dispatchOnce();assert.equal(sent.length,1); }finally{ store.close();for(const suffix of ['','-wal','-shm'])rmSync(filename+suffix,{force:true});rmdirSync(directory); } }); test('migration converts the previous change-log cursor to a live-observation baseline without replay or gaps',async()=>{ const directory=mkdtempSync(join(tmpdir(),'teams-webhook-migration-')), filename=join(directory,'fictional.sqlite'); let store=createStore(filename), sent=[]; const time=Date.parse('2026-09-08T08:00:00Z'); const options=()=>({store,now:()=>time,classify:record=>record.raw,send:async request=>{sent.push(JSON.parse(request.body));return{status:204};}}); const record={source:'web-api',accountId:'fictional-account',kind:'message',externalId:'fictional-before-upgrade',title:'Fictional',text:'Fictional', occurredAt:new Date(time+100).toISOString(),raw:{isFromSelf:false}}; try { store.upsertRecord(record,{ingestion:'live'}); for(let i=0;i<5;i++)store.upsertRecord({...record,kind:'activity',externalId:`fictional-activity-${i}`}); let service=createWebhookService(options()); const id=service.configure({url:'https://receiver.example/fictional',enabled:true,mode:'all'}).subscription.id; const pendingId=service.configure({url:'https://receiver.example/fictional-pending',enabled:true,mode:'all'}).subscription.id; // Recreate the older schema's cursor domain with several non-message changes. store.execute('UPDATE webhook_subscriptions SET cursor=(SELECT MAX(seq) FROM change_log) WHERE id=?',[id]); store.execute('UPDATE webhook_subscriptions SET cursor=0 WHERE id=?',[pendingId]); store.execute('DELETE FROM message_live_observations'); store.execute("DELETE FROM state WHERE key='migration:message-live-observations:v1'"); store.close();store=createStore(filename);service=createWebhookService(options()); assert.equal(service.list().items.find(item=>item.id===id).cursor,store.readOne('SELECT MAX(seq) n FROM message_live_observations').n); assert.equal(service.list().items.find(item=>item.id===pendingId).cursor,0); await service.dispatchOnce();assert.equal(sent.length,1); assert.equal(service.listDeliveries({subscriptionId:id}).items.length,0); assert.equal(service.listDeliveries({subscriptionId:pendingId}).items.length,1); store.upsertRecord({...record,externalId:'fictional-after-upgrade'},{ingestion:'live'}); await service.dispatchOnce();assert.equal(sent.length,3); assert.equal(sent.slice(1).every(item=>item.message.id===store.readOne("SELECT id FROM records WHERE externalId='fictional-after-upgrade'").id),true); }finally{ store.close();for(const suffix of ['','-wal','-shm'])rmSync(filename+suffix,{force:true});rmdirSync(directory); } }); test('payload is signed, defaults to metadata, and public status does not reveal secrets or endpoint path',async()=>{ let sent;const f=fixture(async request=>{sent=request;return{status:200};}); try { const {signingSecret}=f.subscription();f.add('signed');await f.service.dispatchOnce(); const payload=JSON.parse(sent.body);assert.equal(payload.message.text,undefined);assert.equal(payload.message.title,undefined); const expected=createHmac('sha256',signingSecret).update(sent.headers['X-Teams-Timestamp']+'.'+sent.body).digest('hex'); assert.equal(sent.headers['X-Teams-Signature'],'sha256='+expected);assert.equal(sent.headers['X-Teams-Event-Id'],payload.id); assert.doesNotMatch(JSON.stringify([f.service.list(),f.service.listDeliveries(),f.service.status()]),new RegExp(signingSecret+'|secret-path|key=private|Fictional body')); }finally{f.close();} }); test('explicit content and account/chat scope are honored',async()=>{ const sent=[];const f=fixture(async request=>{sent.push(JSON.parse(request.body));return{status:200};}); try { f.subscription({accountIds:['test-account'],chatIds:['test-chat'],includeContent:true}); f.add('one');f.add('foreign',{}, {record:{accountId:'other-account'}});f.add('other-chat',{}, {record:{parentId:'elsewhere'}}); await f.service.dispatchOnce();assert.equal(sent.length,1);assert.equal(sent[0].message.text,'Fictional body'); }finally{f.close();} }); test('Retry-After persists a stable event and retries after delay; permanent failures can be explicitly retried',async()=>{ const events=[];let code=429;const f=fixture(async request=>{events.push(request.headers['X-Teams-Event-Id']);return{status:code,retryAfter:'120'};}); try { f.subscription();f.add('retry');await f.service.dispatchOnce();await f.service.dispatchOnce();assert.equal(events.length,1); f.advance(119_000);await f.service.dispatchOnce();assert.equal(events.length,1); f.advance(1000);code=400;await f.service.dispatchOnce();assert.equal(events.length,2);assert.equal(events[0],events[1]); const delivery=f.service.listDeliveries().items[0];assert.equal(delivery.status,'dead'); code=200;f.service.retry({deliveryId:delivery.id});await f.service.dispatchOnce();assert.equal(events.length,3);assert.equal(events[0],events[2]); }finally{f.close();} }); test('excessive Retry-After becomes a manual failure instead of retrying earlier than requested',async()=>{ const f=fixture(async()=>({status:429,retryAfter:'99999999999999999999'})); try {f.subscription();f.add('long-delay');await f.service.dispatchOnce();assert.equal(f.service.listDeliveries().items[0].status,'dead');} finally {f.close();} }); test('configuration changes cancel old queued destinations, disabled subscriptions do not replay old messages',async()=>{ let calls=0;const f=fixture(async()=>{calls++;return{status:503};}); try { const id=f.subscription().subscription.id;f.add('queued');await f.service.dispatchOnce(); f.service.configure({id,enabled:false});f.advance(60_000);await f.service.dispatchOnce();assert.equal(calls,1); f.add('while-disabled');f.service.configure({id,url:'https://new.example/another-secret',enabled:true}); await f.service.dispatchOnce();assert.equal(calls,1);assert.equal(f.service.listDeliveries().items[0].status,'cancelled'); }finally{f.close();} }); test('concurrent dispatchers claim one lease and a late response cannot revive a removed subscription',async()=>{ let release,calls=0;const f=fixture(async()=>{calls++;return new Promise(resolve=>{release=resolve;});}); try { const id=f.subscription().subscription.id;f.add('one');const first=f.service.dispatchOnce(); const other=createWebhookService({store:f.store,now:f.now,send:async()=>{calls++;return{status:200};},classify:r=>r.raw}); await other.dispatchOnce();assert.equal(calls,1);f.service.remove({id});release({status:200});await first; assert.equal(f.service.listDeliveries().items.length,0); }finally{f.close();} }); test('URL and DNS checks reject private, metadata, credentials and redirects without exposing response content',async()=>{ for(const address of ['127.0.0.1','10.0.0.1','169.254.169.254','172.16.0.1','192.168.1.1','::1','::ffff:127.0.0.1','fc00::1','2001:db8::1']) assert.equal(isAllowedWebhookAddress(address),false,address); assert.equal(isAllowedWebhookAddress('8.8.8.8'),true);assert.equal(isAllowedWebhookAddress('2606:4700:4700::1111'),true); assert.equal(isAllowedWebhookAddress('127.0.0.1',true),true);assert.equal(isAllowedWebhookAddress('10.0.0.1',true),false); for(const url of ['http://public.example/hook','https://user:secret@public.example/','https://169.254.169.254/','file:///test','https://public.example/#secret']) assert.throws(()=>validateWebhookUrl(url)); await assert.rejects(sendWebhook({url:'https://rebind.example/hook',body:'{}',headers:{}},{resolveHost:async()=>[{address:'127.0.0.1',family:4}]})); }); test('real loopback receiver obtains one signed JSON POST; redirects are returned without a second request',async()=>{ const received=[];const server=http.createServer(async(req,res)=>{let body='';for await(const chunk of req)body+=chunk;received.push({method:req.method,body});res.writeHead(302,{Location:'http://127.0.0.1:1/should-not-follow'});res.end('private receiver body');}); server.listen(0,'127.0.0.1');await once(server,'listening'); try { const response=await sendWebhook({url:`http://127.0.0.1:${server.address().port}/test`,allowLocal:true,body:'{"fictional":true}',headers:{'X-Test':'safe'}}); assert.equal(response.status,302);assert.deepEqual(received,[{method:'POST',body:'{"fictional":true}'}]);assert.doesNotMatch(JSON.stringify(response),/private receiver body/); }finally{await new Promise(resolve=>server.close(resolve));} });