| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- import test from 'node:test';
- import assert from 'node:assert/strict';
- import { messageHtml, plainText } from '../src/html-text.js';
- import { normalizeMessages } from '../src/teams-network-records.js';
- import { visibleText } from '../extension/parser.js';
- import { JSDOM } from 'jsdom';
- 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">';
- 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">';
- test('custom emoji use colon-delimited shortcodes while Unicode and ordinary text stay unchanged', () => {
- assert.equal(plainText(`正文${custom}之后<img alt="🔥">`), '正文:quby-dance:之后🔥');
- assert.equal(plainText(custom), ':quby-dance:');
- assert.equal(plainText('quby-dance :normal: https://example.invalid'), 'quby-dance :normal: https://example.invalid');
- assert.equal(plainText('<img itemtype="https://schema.skype.com/CustomEmoji" alt=":hi:">'), ':hi:');
- assert.equal(plainText('<img itemtype="http://schema.skype.com/CustomEmoji" itemid="https://bad.invalid">'), ':custom_emoji:');
- });
- test('image descriptors deduplicate AMS objects and omit emoji, signed URLs and unrecognized routes', () => {
- 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">`);
- assert.deepEqual(result.images, [{ objectId: '0-ea-photo', label: '设计图片', width: 800, height: 600 }]);
- assert.doesNotMatch(JSON.stringify(result), /secret|https:|evil|private|imgt2/);
- assert.equal(messageHtml('<script>' + photo + '</script>').images.length, 0);
- assert.equal(messageHtml('<img src="https://as-prod.asyncgw.teams.microsoft.com/v1/objects/..%2fprivate/views/imgo">').images.length, 0);
- });
- test('normalizer keeps image-only and custom-emoji-only messages with minimal metadata', () => {
- const chatId = '19:fictional@thread.v2';
- const base = { type: 'Message', messagetype: 'RichText/Html', conversationid: chatId,
- conversationLink: `https://teams.cloud.microsoft/api/chatsvc/emea/v1/users/ME/conversations/${encodeURIComponent(chatId)}` };
- const result = normalizeMessages({ messages: [{ ...base, id: '1', content: custom }, { ...base, id: '2', content: photo }] }, { accountId: 'test', chatId });
- assert.equal(result.records.length, 2);
- assert.equal(result.records[0].text, ':quby-dance:');
- assert.equal(result.records[0].raw.images, undefined);
- assert.equal(result.records[1].raw.images[0].objectId, '0-ea-photo');
- assert.doesNotMatch(JSON.stringify(result.records), /secret=|as-api|<img/);
- });
- test('browser DOM collector uses the same custom emoji spelling', () => {
- const dom = new JSDOM(`<main><div id="body">A${custom}<img alt="👍">B</div></main>`, { pretendToBeVisual: true });
- try { assert.equal(visibleText(dom.window.document.getElementById('body'), 1000, { includeImages: true }), 'A:quby-dance:👍B'); }
- finally { dom.window.close(); }
- });
|