message-images.test.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import test from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import { messageHtml, plainText } from '../src/html-text.js';
  4. import { normalizeMessages } from '../src/teams-network-records.js';
  5. import { visibleText } from '../extension/parser.js';
  6. import { JSDOM } from 'jsdom';
  7. const custom = '<img itemtype="http://schema.skype.com/CustomEmoji" itemid="quby-dance;0-ea-example" alt="quby-dance" src="https://as-prod.asyncgw.teams.microsoft.com/v1/objects/0-ea-example/views/imgt2_anim">';
  8. const photo = '<img itemtype="http://schema.skype.com/AMSImage" alt="设计图片" src="https://as-api.asm.skype.com/v1/objects/0-ea-photo/views/imgo?secret=discard" width="800" height="600">';
  9. test('custom emoji use colon-delimited shortcodes while Unicode and ordinary text stay unchanged', () => {
  10. assert.equal(plainText(`正文${custom}之后<img alt="🔥">`), '正文:quby-dance:之后🔥');
  11. assert.equal(plainText(custom), ':quby-dance:');
  12. assert.equal(plainText('quby-dance :normal: https://example.invalid'), 'quby-dance :normal: https://example.invalid');
  13. assert.equal(plainText('<img itemtype="https://schema.skype.com/CustomEmoji" alt=":hi:">'), ':hi:');
  14. assert.equal(plainText('<img itemtype="http://schema.skype.com/CustomEmoji" itemid="https://bad.invalid">'), ':custom_emoji:');
  15. });
  16. test('image descriptors deduplicate AMS objects and omit emoji, signed URLs and unrecognized routes', () => {
  17. const result = messageHtml(`${photo}${photo}${custom}<img alt="😀" itemtype="http://schema.skype.com/Emoji"><img src="https://evil.invalid/pixel"><img src="http://127.0.0.1/private"><img src="https://as-prod.asyncgw.teams.microsoft.com/unknown">`);
  18. assert.deepEqual(result.images, [{ objectId: '0-ea-photo', label: '设计图片', width: 800, height: 600 }]);
  19. assert.doesNotMatch(JSON.stringify(result), /secret|https:|evil|private|imgt2/);
  20. assert.equal(messageHtml('<script>' + photo + '</script>').images.length, 0);
  21. assert.equal(messageHtml('<img src="https://as-prod.asyncgw.teams.microsoft.com/v1/objects/..%2fprivate/views/imgo">').images.length, 0);
  22. });
  23. test('normalizer keeps image-only and custom-emoji-only messages with minimal metadata', () => {
  24. const chatId = '19:fictional@thread.v2';
  25. const base = { type: 'Message', messagetype: 'RichText/Html', conversationid: chatId,
  26. conversationLink: `https://teams.cloud.microsoft/api/chatsvc/emea/v1/users/ME/conversations/${encodeURIComponent(chatId)}` };
  27. const result = normalizeMessages({ messages: [{ ...base, id: '1', content: custom }, { ...base, id: '2', content: photo }] }, { accountId: 'test', chatId });
  28. assert.equal(result.records.length, 2);
  29. assert.equal(result.records[0].text, ':quby-dance:');
  30. assert.equal(result.records[0].raw.images, undefined);
  31. assert.equal(result.records[1].raw.images[0].objectId, '0-ea-photo');
  32. assert.doesNotMatch(JSON.stringify(result.records), /secret=|as-api|<img/);
  33. });
  34. test('browser DOM collector uses the same custom emoji spelling', () => {
  35. const dom = new JSDOM(`<main><div id="body">A${custom}<img alt="👍">B</div></main>`, { pretendToBeVisual: true });
  36. try { assert.equal(visibleText(dom.window.document.getElementById('body'), 1000, { includeImages: true }), 'A:quby-dance:👍B'); }
  37. finally { dom.window.close(); }
  38. });