import test from 'node:test'; import assert from 'node:assert/strict'; import { createStore } from '../src/store.js'; import { createChatMetadataCollector } from '../src/network-chat-metadata.js'; import { classifyMessage } from '../src/message-signals.js'; const tenant = '11111111-1111-4111-8111-111111111111'; const self = '22222222-2222-4222-8222-222222222222'; const other = '33333333-3333-4333-8333-333333333333'; const accountId = `web-api:${tenant}:${self}`; const account2 = `web-api:${tenant}:${other}`; const chatId = '19:fictional-group@thread.v2'; const base = (chat = chatId, host = 'teams.cloud.microsoft') => `https://${host}/api/chatsvc/apac/v1/users/ME/conversations/${encodeURIComponent(chat)}`; const scope = (chat = chatId, account = accountId) => ({ source: 'web-api', accountId: account, kind: 'chat', externalId: chat }); const observation = (chat = chatId, account = accountId) => ({ accountId: account, chatId: chat, requestUrl: `${base(chat)}/messages?pageSize=200`, headers: { Authorization: 'Bearer private-token-marker', 'X-Ms-Client-Type': 'web' } }); const messages = (chat = chatId) => ({ messages: [{ conversationid: chat, conversationLink: base(chat), content: 'Private message marker' }] }); const details = (chat = chatId, topic = 'Fictional group', revision = '1788850000000') => ({ type: 'Conversation', id: chat, version: 17, properties: { secret: 'private-property-marker' }, threadProperties: { topic, threadType: 'chat', version: revision, secret: 'private-thread-marker' }, lastMessage: { content: 'private-preview-marker' }, messages: 'private-messages-marker' }); const publicResult = result => assert.doesNotMatch(JSON.stringify(result), /private-|Fictional|19:|web-api:|https:|Bearer/); test('observed meeting topics resolve existing messages by account without rewriting history or classifying them as direct chats', async () => { const store = createStore(':memory:'); const meetingId = '19:meeting_ZmljdGlvbmFs@thread.v2'; try { for (const account of [accountId, account2]) store.upsertRecord({ ...scope(meetingId, account), kind: 'message', externalId: 'meeting-message', parentId: meetingId, title: '聊天消息', text: 'Original meeting message', raw: { author: 'Fictional speaker' } }); const messageScope = { ...scope(meetingId), kind: 'message', externalId: 'meeting-message' }; const before = store.getRecordByIdentity(messageScope), versions = store.getRecord(before.id).versions; const body = details(meetingId, 'Fictional meeting & review'); body.threadProperties.threadType = 'meeting'; const collector = createChatMetadataCollector({ store, fetch: async () => Response.json(body) }); assert.equal(collector.observe(observation(meetingId), messages(meetingId)), true); const result = await collector.runOnce(); publicResult(result); assert.equal(result.successful, 1); assert.equal(result.changed, 1); assert.deepEqual(result.errors, []); const chat = store.getRecordByIdentity(scope(meetingId)); assert.equal(chat.title, 'Fictional meeting & review'); assert.equal(chat.raw.threadType, 'meeting'); assert.equal(chat.raw.titleSource, 'conversation-details'); assert.doesNotMatch(JSON.stringify(chat), /private-/); assert.equal(store.getRecord(before.id).displayTitle, chat.title); assert.equal(store.listRecords({ kind: 'message', accountId, q: chat.title }).total, 1); assert.equal(store.listRecords({ kind: 'message', accountId: account2, q: chat.title }).total, 0); assert.deepEqual(store.getRecordByIdentity(messageScope), before); assert.deepEqual(store.getRecord(before.id).versions, versions); assert.equal(classifyMessage(before, chat).chatType, 'meeting'); assert.equal(classifyMessage(before, chat).isDirect, false); } finally { store.close(); } }); test('meeting details keep identity, topic and revision validation and preserve known names when a topic is omitted', async () => { const store = createStore(':memory:'); let at = 2000000000000; const meetingId = '19:meeting_c2Vjb25k@thread.v2'; try { let body; const collector = createChatMetadataCollector({ store, now: () => at, fetch: async () => Response.json(body) }); collector.observe(observation(meetingId), messages(meetingId)); const fresh = () => { const value = details(meetingId, 'Current meeting'); value.threadProperties.threadType = 'meeting'; return value; }; body = fresh(); assert.equal((await collector.runOnce()).changed, 1); for (const change of [v => { delete v.threadProperties.topic; }, v => { v.threadProperties.topic = null; }, v => { v.threadProperties.topic = ''; }, v => { v.threadProperties.topic = 'Old name'; v.threadProperties.version = '1788840000000'; }]) { at += 30 * 60_000; body = fresh(); change(body); const result = await collector.runOnce(); assert.equal(result.successful, 1); assert.equal(result.changed, 0); assert.equal(store.getRecordByIdentity(scope(meetingId)).title, 'Current meeting'); } for (const change of [v => { v.id = '19:meeting_d3Jvbmc@thread.v2'; }, v => { v.threadProperties.version = 'invalid'; }, v => { v.threadProperties.topic = {}; }, v => { v.threadProperties.threadType = 'unknown'; }]) { at += 30 * 60_000; body = fresh(); change(body); const result = await collector.runOnce(); assert.equal(result.changed, 0); assert.equal(result.errors[0].code, 'invalid-metadata'); assert.equal(store.getRecordByIdentity(scope(meetingId)).title, 'Current meeting'); } } finally { store.close(); } }); test('observed conversation detail names the account-scoped chat without copying previews or changing messages', async () => { const store = createStore(':memory:'); let calls = 0; try { store.upsertRecord({ ...scope(), kind: 'message', externalId: 'message-id', parentId: chatId, title: '聊天消息', text: 'unchanged', raw: {} }); const message = store.getRecordByIdentity({ ...scope(), kind: 'message', externalId: 'message-id' }); const collector = createChatMetadataCollector({ store, fetch: async (url, options) => { calls++; assert.equal(url, base()); assert.equal(options.method, 'GET'); assert.equal(options.redirect, 'manual'); assert.equal(options.credentials, 'omit'); assert.equal(options.headers.Authorization, 'Bearer private-token-marker'); assert.ok(options.signal instanceof AbortSignal); return Response.json(details()); } }); assert.equal(collector.observe(observation(), messages()), true); const result = await collector.runOnce(); publicResult(result); assert.equal(calls, 1); assert.equal(result.successful, 1); assert.equal(result.changed, 1); const chat = store.getRecordByIdentity(scope()); assert.equal(chat.title, 'Fictional group'); assert.equal(chat.updatedAt, '2026-09-08T06:46:40.000Z'); assert.deepEqual(chat.raw, { chatId, titleSource: 'conversation-details', threadType: 'chat', version: '1788850000000' }); assert.doesNotMatch(JSON.stringify(chat), /private-/); const after = store.getRecordByIdentity({ ...scope(), kind: 'message', externalId: 'message-id' }); assert.equal(after.hash, message.hash); assert.equal(after.title, '聊天消息'); assert.equal(store.listRecords({ kind: 'message' }).items[0].displayTitle, 'Fictional group'); assert.equal((await collector.runOnce()).requests, 0); } finally { store.close(); } }); test('observations reject foreign origin, conversation mismatches, activity streams, and non-observed URLs before HTTP', async () => { const store = createStore(':memory:'); let calls = 0; try { const collector = createChatMetadataCollector({ store, fetch: async () => { calls++; throw new Error('must not fetch'); } }); const invalidLinks = [base().replace('teams.cloud.microsoft', 'teams.microsoft.com'), base().replace('teams.cloud.microsoft', 'evil.example'), `${base()}?token=private-token`, `${base()}#private-token`, base().replace('https:', 'http:'), base().replace('https://', 'https://user:private-token@'), base().replace('/api/', ':8443/api/'), base().replace('/api/', ':443/api/'), base().replace('/apac/', '/emea/'), `${base()}/messages`, base('19:other-chat@thread.v2'), base().replace('/ME/', '/someone/'), base().replace('/api/', '/evil/'), base().replace('https://', 'https:\\')]; for (const link of invalidLinks) assert.equal(collector.observe(observation(), { messages: [{ conversationid: chatId, conversationLink: link }] }), false); assert.equal(collector.observe(observation(), { messages: [...messages().messages, { conversationid: '19:other@thread.v2', conversationLink: base() }] }), false); assert.equal(collector.observe(observation(), { messages: [] }), false); assert.equal(collector.observe(observation('48:notifications'), messages('48:notifications')), false); assert.equal(collector.observe({ ...observation(), accountId: 'other-account' }, messages()), false); assert.equal(collector.observe({ ...observation(), headers: {} }, messages()), false); assert.equal(collector.observe({ ...observation(), headers: { Authorization: 'Bearer private-token\r\ninjected' } }, messages()), false); assert.equal(collector.observe({ ...observation(), requestUrl: `${base()}/messages#private-fragment` }, messages()), false); assert.equal((await collector.runOnce()).requests, 0); assert.equal(calls, 0); assert.equal(store.stats().total, 0); } finally { store.close(); } }); test('both observed Teams hosts and unq private-chat identities are supported only within the original origin', async () => { const store = createStore(':memory:'); try { const chat = `19:${self}_${other}@unq.gbl.spaces`; const collector = createChatMetadataCollector({ store, fetch: async () => Response.json(details(chat)) }); assert.equal(collector.observe({ ...observation(chat), requestUrl: `${base(chat, 'teams.microsoft.com')}/messages` }, { messages: [{ conversationid: chat, conversationLink: base(chat, 'teams.microsoft.com') }] }), true); assert.equal((await collector.runOnce()).changed, 1); } finally { store.close(); } }); test('same chat IDs remain isolated across account records and persistent cache keys', async () => { const store = createStore(':memory:'); let calls = 0; try { const collector = createChatMetadataCollector({ store, fetch: async () => Response.json(details(chatId, ++calls === 1 ? 'First group' : 'Second group')) }); collector.observe(observation(), messages()); collector.observe(observation(chatId, account2), messages()); const result = await collector.runOnce(); assert.equal(result.requests, 2); assert.equal(result.changed, 2); assert.equal(store.getRecordByIdentity(scope()).title, 'First group'); assert.equal(store.getRecordByIdentity(scope(chatId, account2)).title, 'Second group'); assert.equal(store.readRows("SELECT key FROM state WHERE key LIKE 'web-api:chat-metadata:%'").length, 2); } finally { store.close(); } }); test('zero budget performs no HTTP and a larger requested budget still sends at most three detail reads', async () => { const store = createStore(':memory:'); let calls = 0; try { const collector = createChatMetadataCollector({ store, fetch: async raw => { calls++; return Response.json(details(decodeURIComponent(new URL(raw).pathname.split('/').at(-1)))); } }); for (let index = 0; index < 5; index++) { const chat = `19:group-${index}@thread.v2`; collector.observe(observation(chat), messages(chat)); } assert.equal((await collector.runOnce({ maxRequests: 0 })).requests, 0); assert.equal((await collector.runOnce({ maxRequests: 100 })).requests, 3); assert.equal(calls, 3); assert.deepEqual(collector.coverage(), { discovered: 5, cached: 3, pending: 2, paused: 0, rateLimited: 0 }); assert.equal((await collector.runOnce()).requests, 2); assert.equal(calls, 5); } finally { store.close(); } }); test('success cache survives collector recreation and unchanged valid detail does not add a record version', async () => { const store = createStore(':memory:'); let at = 2000000000000, calls = 0; try { const make = () => createChatMetadataCollector({ store, now: () => at, fetch: async () => { calls++; return Response.json(details()); } }); const first = make(); first.observe(observation(), messages()); await first.runOnce(); const versions = store.stats().versions; const next = make(); next.observe(observation(), messages()); assert.equal((await next.runOnce()).requests, 0); at += 30 * 60_000 - 1; assert.equal((await next.runOnce()).requests, 0); at++; assert.equal((await next.runOnce()).successful, 1); assert.equal(calls, 2); assert.equal(store.stats().versions, versions); } finally { store.close(); } }); test('empty and older topics preserve known titles; newer details retain previously normalized member evidence', async () => { const store = createStore(':memory:'); let at = 2000000000000, body = details(chatId, ''); try { store.upsertRecord({ ...scope(), title: 'Current title', text: '', updatedAt: '2026-09-08T06:46:40.000Z', raw: { version: 1788850000000, titleSource: 'explicit', isOneOnOne: false, members: [{ userId: other, displayName: 'Fictional person' }] } }); const collector = createChatMetadataCollector({ store, now: () => at, fetch: async () => Response.json(body) }); collector.observe(observation(), messages()); assert.equal((await collector.runOnce()).changed, 0); at += 30 * 60_000; body = details(chatId, 'Old title', '1788840000000'); assert.equal((await collector.runOnce()).changed, 0); assert.equal(store.getRecordByIdentity(scope()).title, 'Current title'); at += 30 * 60_000; body = details(chatId, 'New & clean', '1788860000000'); assert.equal((await collector.runOnce()).changed, 1); const chat = store.getRecordByIdentity(scope()); assert.equal(chat.title, 'New & clean'); assert.equal(chat.raw.isOneOnOne, false); assert.equal(chat.raw.members[0].userId, other); assert.equal(chat.raw.titleSource, 'conversation-details'); } finally { store.close(); } }); test('missing or null topics cache a valid unnamed response without inventing or erasing chat titles', async () => { const store = createStore(':memory:'); let at = 2000000000000, calls = 0; try { const knownChat = '19:known-group@thread.v2'; store.upsertRecord({ ...scope(knownChat), title: 'Preserved title', text: '', raw: { titleSource: 'explicit' } }); const original = store.getRecordByIdentity(scope(knownChat)); const collector = createChatMetadataCollector({ store, now: () => at, fetch: async raw => { calls++; const id = decodeURIComponent(new URL(raw).pathname.split('/').at(-1)), body = details(id); if (id === chatId) delete body.threadProperties.topic; else body.threadProperties.topic = null; return Response.json(body); } }); collector.observe(observation(), messages()); collector.observe(observation(knownChat), messages(knownChat)); const result = await collector.runOnce(); assert.equal(result.successful, 2); assert.equal(result.changed, 0); assert.deepEqual(result.errors, []); assert.equal(store.getRecordByIdentity(scope()), null); assert.equal(store.getRecordByIdentity(scope(knownChat)).hash, original.hash); assert.equal(store.getRecordByIdentity(scope(knownChat)).title, 'Preserved title'); assert.equal((await collector.runOnce()).requests, 0); at += 30 * 60_000 - 1; assert.equal((await collector.runOnce()).requests, 0); assert.equal(calls, 2); at++; assert.equal((await collector.runOnce()).successful, 2); assert.equal(calls, 4); assert.equal(store.stats().versions, 1); } finally { store.close(); } }); test('unrecognized or mismatched detail schemas never save names and retry only after the failure TTL', async () => { const variants = [value => { value.id = '19:other@thread.v2'; }, value => { value.type = 'Message'; }, value => { value.version = '17'; }, value => { value.threadProperties = null; }, value => { value.threadProperties.version = 'not-a-version'; }, value => { value.threadProperties.topic = {}; }, value => { value.threadProperties.topic = 42; }, value => { value.threadProperties.topic = []; }, value => { delete value.threadProperties.topic; value.id = '19:other@thread.v2'; }, value => { value.threadProperties.topic = null; value.threadProperties.version = 'not-a-version'; }, value => { value.threadProperties.threadType = 'unknown'; }]; for (const modify of variants) { const store = createStore(':memory:'); let at = 2000000000000, calls = 0; try { const body = details(); modify(body); const collector = createChatMetadataCollector({ store, now: () => at, fetch: async () => { calls++; return Response.json(body); } }); collector.observe(observation(), messages()); const result = await collector.runOnce(); publicResult(result); assert.equal(result.errors[0].status, 200); assert.equal(result.errors[0].code, 'invalid-metadata'); assert.match(result.errors[0].reason, /校验/); assert.equal(store.stats().total, 0); assert.equal((await collector.runOnce()).requests, 0); at += 5 * 60_000; assert.equal((await collector.runOnce()).requests, 1); assert.equal(calls, 2); } finally { store.close(); } } }); test('401 and 403 pause a detail plan even when its messages are observed again', async () => { for (const status of [401, 403]) { const store = createStore(':memory:'); let at = 2000000000000; try { const collector = createChatMetadataCollector({ store, now: () => at, fetch: async () => new Response('private-error-marker', { status }) }); collector.observe(observation(), messages()); const result = await collector.runOnce(); assert.equal(result.paused, 1); publicResult(result); at += 60 * 60_000; collector.observe(observation(), messages()); assert.equal((await collector.runOnce()).requests, 0); const renewed = createChatMetadataCollector({ store, now: () => at, fetch: async () => Response.json(details()) }); renewed.observe(observation(), messages()); assert.equal((await renewed.runOnce()).changed, 1); } finally { store.close(); } } }); test('429 Retry-After persists an origin cooldown that defers other chats across recreation', async () => { const store = createStore(':memory:'); let at = 2000000000000, calls = 0; try { const make = response => createChatMetadataCollector({ store, now: () => at, fetch: async raw => { calls++; return response(raw); } }); const first = make(() => new Response('', { status: 429, headers: { 'retry-after': '1200' } })); for (const chat of [chatId, '19:second@thread.v2']) first.observe(observation(chat), messages(chat)); const result = await first.runOnce(); assert.equal(result.requests, 1); assert.equal(result.rateLimited, 2); publicResult(result); const next = make(raw => Response.json(details(decodeURIComponent(new URL(raw).pathname.split('/').at(-1))))); next.observe(observation('19:second@thread.v2'), messages('19:second@thread.v2')); at += 20 * 60_000 - 1; assert.equal((await next.runOnce()).requests, 0); at++; assert.equal((await next.runOnce()).successful, 1); assert.equal(calls, 2); } finally { store.close(); } }); test('redirects, unexpected MIME, oversize streamed bodies and provider exceptions return only safe errors', async () => { const responses = [() => new Response('private-redirect', { status: 302, headers: { location: 'https://evil.example/private-target' } }), () => new Response('private-html', { headers: { 'content-type': 'text/html' } }), () => new Response('{}', { headers: { 'content-type': 'application/json', 'content-length': '2097153' } }), () => new Response('x'.repeat(2097153), { headers: { 'content-type': 'application/json' } }), () => { throw new Error('Bearer private-token-marker https://evil.example/private-secret'); }]; for (const response of responses) { const store = createStore(':memory:'); let calls = 0; try { const collector = createChatMetadataCollector({ store, fetch: async () => { calls++; return response(); } }); collector.observe(observation(), messages()); const result = await collector.runOnce(); publicResult(result); assert.equal(result.successful, 0); assert.equal(result.errors.length, 1); assert.equal(store.stats().total, 0); assert.equal(calls, 1); } finally { store.close(); } } }); test('cancelling a pending request prevents record and cache writes even if an injected fetch ignores abort', async () => { const store = createStore(':memory:'); const controller = new AbortController(); let release; try { const collector = createChatMetadataCollector({ store, fetch: () => new Promise(resolve => { release = resolve; }) }); collector.observe(observation(), messages()); const running = collector.runOnce({ signal: controller.signal }); controller.abort(); const result = await running; assert.equal(result.cancelled, true); let cancelled = false; release(new Response(new ReadableStream({ cancel() { cancelled = true; } }), { headers: { 'content-type': 'application/json' } })); await new Promise(resolve => setImmediate(resolve)); assert.equal(cancelled, true); assert.equal(store.stats().total, 0); assert.equal(store.readRows("SELECT key FROM state WHERE key LIKE 'web-api:chat-metadata:%'").length, 0); } finally { store.close(); } }); test('cancellation while streaming does not save parsed metadata', async () => { const store = createStore(':memory:'); const controller = new AbortController(); let streaming, cancelled = false; try { const ready = new Promise(resolve => { streaming = resolve; }); const collector = createChatMetadataCollector({ store, fetch: async () => new Response(new ReadableStream({ pull() { streaming(); }, cancel() { cancelled = true; } }), { headers: { 'content-type': 'application/json' } }) }); collector.observe(observation(), messages()); const run = collector.runOnce({ signal: controller.signal }); await ready; controller.abort(); assert.equal((await run).cancelled, true); assert.equal(store.stats().total, 0); assert.equal(cancelled, true); // The caller is released even if a transport cannot finish its response. assert.equal(collector.coverage().pending, 1); } finally { store.close(); } }); test('concurrent passes share a single request and bounded plain-text topics preserve Unicode', async () => { const store = createStore(':memory:'); let release, calls = 0; try { const collector = createChatMetadataCollector({ store, fetch: () => { calls++; return new Promise(resolve => { release = resolve; }); } }); collector.observe(observation(), messages()); const first = collector.runOnce(), second = collector.runOnce(); release(Response.json(details(chatId, '😀'.repeat(501)))); await Promise.all([first, second]); assert.equal(calls, 1); assert.equal([...store.getRecordByIdentity(scope()).title].length, 500); assert.equal(store.stats().versions, 1); } finally { store.close(); } }); test('the ten-second timeout releases an unresponsive fetch and later cancels its response body', async context => { context.mock.timers.enable({ apis: ['setTimeout'] }); const store = createStore(':memory:'); let release, cancelled = false; try { const collector = createChatMetadataCollector({ store, fetch: () => new Promise(resolve => { release = resolve; }) }); collector.observe(observation(), messages()); const run = collector.runOnce(); context.mock.timers.tick(9999); assert.equal(store.stats().total, 0); context.mock.timers.tick(1); const result = await run; assert.equal(result.errors[0].code, 'timeout'); publicResult(result); release(new Response(new ReadableStream({ cancel() { cancelled = true; } }), { headers: { 'content-type': 'application/json' } })); await new Promise(resolve => setImmediate(resolve)); assert.equal(cancelled, true); assert.equal(store.stats().total, 0); } finally { store.close(); context.mock.timers.reset(); } });