import test from 'node:test'; import assert from 'node:assert/strict'; import { classifyMessage, teamsMessageMetadata } from '../src/message-signals.js'; const self = '11111111-1111-4111-8111-111111111111'; const other = '22222222-2222-4222-8222-222222222222'; const tenant = '33333333-3333-4333-8333-333333333333'; const accountId = `web-api:${tenant}:${self}`; const message = (raw = {}, overrides = {}) => ({ source: 'web-api', accountId, kind: 'message', parentId: 'fictional-chat', text: '@my name', raw, ...overrides }); const chat = (raw = {}, overrides = {}) => ({ source: 'web-api', accountId, kind: 'chat', externalId: 'fictional-chat', raw, ...overrides }); const sender = id => `https://teams.cloud.microsoft/api/chatsvc/apac/v1/users/ME/contacts/${encodeURIComponent(`8:orgid:${id}`)}`; test('captured sender and structured mention identities are extracted without retaining URLs or unrelated fields', () => { const raw = teamsMessageMetadata({ from: sender(other), properties: { mentions: JSON.stringify([ { '@type': 'mention', itemid: 0, mentionType: 'person', mri: `8:orgid:${self}`, displayName: 'unused-name' }, ]), authorization: 'secret-marker' } }, accountId); assert.deepEqual(raw, { senderId: other, mentions: [{ id: self, type: 'user' }], selfUserId: self }); assert.doesNotMatch(JSON.stringify(raw), /https:|secret-marker|unused-name/); assert.deepEqual(classifyMessage(message(raw)), { senderId: other, isFromSelf: false, mentionsSelf: true, chatType: 'unknown', isDirect: null }); }); test('mentions of other users do not become mentions of the current user', () => { const classified = classifyMessage(message({ senderId: self, mentions: [{ type: 'user', id: other }] })); assert.equal(classified.mentionsSelf, false); assert.equal(classified.isFromSelf, true); }); test('missing, malformed, empty, and unsupported mentions retain their distinct evidence', () => { for (const mentions of [undefined, '{invalid', '{}', JSON.stringify(new Array(501).fill({}))]) { const raw = teamsMessageMetadata({ properties: { mentions } }, accountId); assert.equal(raw.mentions, null); assert.equal(classifyMessage(message(raw)).mentionsSelf, null); } const empty = teamsMessageMetadata({ properties: { mentions: '[]' } }, accountId); assert.deepEqual(empty.mentions, []); assert.equal(classifyMessage(message(empty)).mentionsSelf, false); const unknown = teamsMessageMetadata({ properties: { mentions: '[{"mentionType":"team","mri":"private-team-id"}]' } }, accountId); assert.deepEqual(unknown.mentions, [{ id: null, type: 'unknown' }]); assert.equal(classifyMessage(message(unknown)).mentionsSelf, null); }); test('a known mention of self remains positive when another mention has an unsupported target', () => { assert.equal(classifyMessage(message({ mentions: [{ type: 'unknown', id: null }, { type: 'user', id: self }] })).mentionsSelf, true); }); test('sender URLs require the observed service route and a stable orgid identity', () => { for (const from of [ sender(other).replace('teams.cloud.microsoft', 'example.invalid'), `${sender(other)}?access_token=secret-marker`, `${sender(other)}#fragment`, sender(other).replace('/contacts/', '/users/'), 'fictional-author', `8:orgid:${self}`, sender(other).replace('8%3Aorgid%3A', '8%3Askype%3A'), ]) assert.equal(teamsMessageMetadata({ from }, accountId).senderId, null); }); test('plain text names, titles, and a two-member roster do not prove mention or direct chat', () => { const record = message({ author: 'my name' }, { title: 'Private chat with my name' }); assert.deepEqual(classifyMessage(record, chat({ threadType: 'chat', members: [{}, {}] })), { senderId: null, isFromSelf: null, mentionsSelf: null, chatType: 'unknown', isDirect: null }); }); test('explicit isOneOnOne determines direct status and explicit meeting remains non-direct', () => { assert.deepEqual(classifyMessage(message(), chat({ isOneOnOne: true })), { senderId: null, isFromSelf: null, mentionsSelf: null, chatType: 'oneOnOne', isDirect: true }); assert.equal(classifyMessage(message(), chat({ isOneOnOne: false, threadType: 'chat' })).isDirect, false); assert.deepEqual(classifyMessage(message(), chat({ threadType: 'meeting' })), { senderId: null, isFromSelf: null, mentionsSelf: null, chatType: 'meeting', isDirect: false }); }); test('chat evidence cannot cross account, source, record kind, or parent identity boundaries', () => { for (const overrides of [{ source: 'graph' }, { accountId: 'another-account' }, { externalId: 'another-chat' }, { kind: 'event' }]) { assert.equal(classifyMessage(message(), chat({ isOneOnOne: true }, overrides)).isDirect, null); } assert.equal(classifyMessage(message({ chat: { id: 'another-chat', chatType: 'oneOnOne' } })).isDirect, null); }); test('contradictory structured chat data remains unknown', () => { const value = classifyMessage(message({ chat: { chatType: 'oneOnOne' } }), chat({ threadType: 'meeting' })); assert.equal(value.chatType, 'unknown'); assert.equal(value.isDirect, null); }); test('Graph uses explicit current tenant self identity and Graph structured mention fields', () => { const record = message({ selfUserId: self, from: { user: { id: other } }, mentions: [{ mentioned: { user: { id: self } } }] }, { source: 'graph', accountId: `graph:${other}.${tenant}:${tenant}` }); const graphChat = chat({ chatType: 'oneOnOne' }, { source: 'graph', accountId: record.accountId }); assert.deepEqual(classifyMessage(record, graphChat), { senderId: other, isFromSelf: false, mentionsSelf: true, chatType: 'oneOnOne', isDirect: true }); record.raw.from.user.id = self.toUpperCase(); assert.equal(classifyMessage(record).isFromSelf, true); }); test('Graph home account ID cannot establish current tenant identity', () => { const record = message({ from: { user: { id: self } }, mentions: [{ mentioned: { user: { id: self } } }] }, { source: 'graph', accountId: `graph:${self}.${tenant}:${tenant}` }); assert.equal(classifyMessage(record).isFromSelf, null); assert.equal(classifyMessage(record).mentionsSelf, null); record.raw.mentions = []; assert.equal(classifyMessage(record).mentionsSelf, false); }); test('unsupported collectors and non-message records cannot claim notification signals', () => { const raw = { selfUserId: self, senderId: self, mentions: [{ id: self, type: 'user' }], isOneOnOne: true }; for (const override of [{ source: 'browser' }, { kind: 'chat' }, { kind: 'event' }]) { assert.deepEqual(classifyMessage(message(raw, override)), { senderId: null, isFromSelf: null, mentionsSelf: null, chatType: 'unknown', isDirect: null }); } });