message-signals.test.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import test from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import { classifyMessage, teamsMessageMetadata } from '../src/message-signals.js';
  4. const self = '11111111-1111-4111-8111-111111111111';
  5. const other = '22222222-2222-4222-8222-222222222222';
  6. const tenant = '33333333-3333-4333-8333-333333333333';
  7. const accountId = `web-api:${tenant}:${self}`;
  8. const message = (raw = {}, overrides = {}) => ({ source: 'web-api', accountId, kind: 'message', parentId: 'fictional-chat', text: '@my name', raw, ...overrides });
  9. const chat = (raw = {}, overrides = {}) => ({ source: 'web-api', accountId, kind: 'chat', externalId: 'fictional-chat', raw, ...overrides });
  10. const sender = id => `https://teams.cloud.microsoft/api/chatsvc/apac/v1/users/ME/contacts/${encodeURIComponent(`8:orgid:${id}`)}`;
  11. test('captured sender and structured mention identities are extracted without retaining URLs or unrelated fields', () => {
  12. const raw = teamsMessageMetadata({ from: sender(other), properties: { mentions: JSON.stringify([
  13. { '@type': 'mention', itemid: 0, mentionType: 'person', mri: `8:orgid:${self}`, displayName: 'unused-name' },
  14. ]), authorization: 'secret-marker' } }, accountId);
  15. assert.deepEqual(raw, { senderId: other, mentions: [{ id: self, type: 'user' }], selfUserId: self });
  16. assert.doesNotMatch(JSON.stringify(raw), /https:|secret-marker|unused-name/);
  17. assert.deepEqual(classifyMessage(message(raw)), { senderId: other, isFromSelf: false, mentionsSelf: true, chatType: 'unknown', isDirect: null });
  18. });
  19. test('mentions of other users do not become mentions of the current user', () => {
  20. const classified = classifyMessage(message({ senderId: self, mentions: [{ type: 'user', id: other }] }));
  21. assert.equal(classified.mentionsSelf, false);
  22. assert.equal(classified.isFromSelf, true);
  23. });
  24. test('missing, malformed, empty, and unsupported mentions retain their distinct evidence', () => {
  25. for (const mentions of [undefined, '{invalid', '{}', JSON.stringify(new Array(501).fill({}))]) {
  26. const raw = teamsMessageMetadata({ properties: { mentions } }, accountId);
  27. assert.equal(raw.mentions, null);
  28. assert.equal(classifyMessage(message(raw)).mentionsSelf, null);
  29. }
  30. const empty = teamsMessageMetadata({ properties: { mentions: '[]' } }, accountId);
  31. assert.deepEqual(empty.mentions, []);
  32. assert.equal(classifyMessage(message(empty)).mentionsSelf, false);
  33. const unknown = teamsMessageMetadata({ properties: { mentions: '[{"mentionType":"team","mri":"private-team-id"}]' } }, accountId);
  34. assert.deepEqual(unknown.mentions, [{ id: null, type: 'unknown' }]);
  35. assert.equal(classifyMessage(message(unknown)).mentionsSelf, null);
  36. });
  37. test('a known mention of self remains positive when another mention has an unsupported target', () => {
  38. assert.equal(classifyMessage(message({ mentions: [{ type: 'unknown', id: null }, { type: 'user', id: self }] })).mentionsSelf, true);
  39. });
  40. test('sender URLs require the observed service route and a stable orgid identity', () => {
  41. for (const from of [
  42. sender(other).replace('teams.cloud.microsoft', 'example.invalid'),
  43. `${sender(other)}?access_token=secret-marker`, `${sender(other)}#fragment`,
  44. sender(other).replace('/contacts/', '/users/'), 'fictional-author', `8:orgid:${self}`,
  45. sender(other).replace('8%3Aorgid%3A', '8%3Askype%3A'),
  46. ]) assert.equal(teamsMessageMetadata({ from }, accountId).senderId, null);
  47. });
  48. test('plain text names, titles, and a two-member roster do not prove mention or direct chat', () => {
  49. const record = message({ author: 'my name' }, { title: 'Private chat with my name' });
  50. assert.deepEqual(classifyMessage(record, chat({ threadType: 'chat', members: [{}, {}] })),
  51. { senderId: null, isFromSelf: null, mentionsSelf: null, chatType: 'unknown', isDirect: null });
  52. });
  53. test('explicit isOneOnOne determines direct status and explicit meeting remains non-direct', () => {
  54. assert.deepEqual(classifyMessage(message(), chat({ isOneOnOne: true })),
  55. { senderId: null, isFromSelf: null, mentionsSelf: null, chatType: 'oneOnOne', isDirect: true });
  56. assert.equal(classifyMessage(message(), chat({ isOneOnOne: false, threadType: 'chat' })).isDirect, false);
  57. assert.deepEqual(classifyMessage(message(), chat({ threadType: 'meeting' })),
  58. { senderId: null, isFromSelf: null, mentionsSelf: null, chatType: 'meeting', isDirect: false });
  59. });
  60. test('chat evidence cannot cross account, source, record kind, or parent identity boundaries', () => {
  61. for (const overrides of [{ source: 'graph' }, { accountId: 'another-account' }, { externalId: 'another-chat' }, { kind: 'event' }]) {
  62. assert.equal(classifyMessage(message(), chat({ isOneOnOne: true }, overrides)).isDirect, null);
  63. }
  64. assert.equal(classifyMessage(message({ chat: { id: 'another-chat', chatType: 'oneOnOne' } })).isDirect, null);
  65. });
  66. test('contradictory structured chat data remains unknown', () => {
  67. const value = classifyMessage(message({ chat: { chatType: 'oneOnOne' } }), chat({ threadType: 'meeting' }));
  68. assert.equal(value.chatType, 'unknown'); assert.equal(value.isDirect, null);
  69. });
  70. test('Graph uses explicit current tenant self identity and Graph structured mention fields', () => {
  71. const record = message({ selfUserId: self, from: { user: { id: other } }, mentions: [{ mentioned: { user: { id: self } } }] },
  72. { source: 'graph', accountId: `graph:${other}.${tenant}:${tenant}` });
  73. const graphChat = chat({ chatType: 'oneOnOne' }, { source: 'graph', accountId: record.accountId });
  74. assert.deepEqual(classifyMessage(record, graphChat), { senderId: other, isFromSelf: false, mentionsSelf: true, chatType: 'oneOnOne', isDirect: true });
  75. record.raw.from.user.id = self.toUpperCase();
  76. assert.equal(classifyMessage(record).isFromSelf, true);
  77. });
  78. test('Graph home account ID cannot establish current tenant identity', () => {
  79. const record = message({ from: { user: { id: self } }, mentions: [{ mentioned: { user: { id: self } } }] },
  80. { source: 'graph', accountId: `graph:${self}.${tenant}:${tenant}` });
  81. assert.equal(classifyMessage(record).isFromSelf, null);
  82. assert.equal(classifyMessage(record).mentionsSelf, null);
  83. record.raw.mentions = [];
  84. assert.equal(classifyMessage(record).mentionsSelf, false);
  85. });
  86. test('unsupported collectors and non-message records cannot claim notification signals', () => {
  87. const raw = { selfUserId: self, senderId: self, mentions: [{ id: self, type: 'user' }], isOneOnOne: true };
  88. for (const override of [{ source: 'browser' }, { kind: 'chat' }, { kind: 'event' }]) {
  89. assert.deepEqual(classifyMessage(message(raw, override)), { senderId: null, isFromSelf: null, mentionsSelf: null, chatType: 'unknown', isDirect: null });
  90. }
  91. });