content-automation.test.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { test } from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import { JSDOM } from 'jsdom';
  4. import { installContentRuntime } from '../extension/content-runtime.js';
  5. import { collectVisible } from '../extension/parser.js';
  6. function fixture() {
  7. const dom = new JSDOM('<html><body></body></html>', { url: 'https://teams.cloud.microsoft/' });
  8. let current = { accountId: 'work', enabled: true, selectors: {}, automation: { enabled: true, assigned: true } };
  9. let fail = false, snapshots = 0, context = { id: 'chat-stable-id', title: 'chat' };
  10. const messages = [], listeners = [], scope = {};
  11. let callbacks, controller;
  12. dom.window.setTimeout = () => 1; dom.window.clearTimeout = () => {};
  13. dom.window.setInterval = () => 2; dom.window.clearInterval = () => {};
  14. const page = { url: dom.window.location.href, title: 'Teams', signedIn: true, view: 'message', coverage: 'test' };
  15. const records = Array.from({ length: 200 }, (_, i) => ({ kind: 'message', externalId: `m${i}`, title: 'chat', text: `message ${i}`, raw: {} }));
  16. const runtime = installContentRuntime({ window: dom.window, document: dom.window.document, scope,
  17. chrome: { runtime: { id: 'test', onMessage: { addListener: cb => listeners.push(cb), removeListener() {} }, sendMessage: async message => {
  18. messages.push(message);
  19. if (message.type === 'GET_CONFIG') return { ok: true, config: structuredClone(current) };
  20. if (message.type === 'CAPTURE' && fail) return { ok: false, error: '服务离线' };
  21. return { ok: true };
  22. } } },
  23. collect: (document, options) => { snapshots++; assert.equal(options.contextOverride?.id, 'chat-stable-id'); return { records, page, counts: { message: 200 }, errors: [] }; },
  24. Crawler: dependencies => {
  25. callbacks = dependencies;
  26. controller = { configure() {}, stop() {}, dispose() {}, resume() {}, async tick() {}, isNavigating: () => false,
  27. status: () => ({ running: false, phase: 'paused' }), captureContext: () => context, preview: () => ({ count: 0, labels: [], errors: [] }) };
  28. return controller;
  29. },
  30. });
  31. return { runtime, messages, callbacks: () => callbacks, snapshots: () => snapshots, setFail: value => { fail = value; },
  32. setContext: value => { context = value; },
  33. async configure(patch) { current = { ...current, ...patch }; return new Promise(resolve => listeners[0]({ type: 'CONFIG_CHANGED' }, { id: 'test' }, resolve)); },
  34. close() { runtime.dispose(); dom.window.close(); } };
  35. }
  36. test('自动标签的被动扫描保持静默;验证后的采集回调排空多批消息并使用稳定会话上下文', async () => {
  37. const f = fixture();
  38. try {
  39. await f.runtime.ready;
  40. assert.equal((await f.runtime.scan()).deferred, true); assert.equal(f.snapshots(), 0);
  41. const first = await f.callbacks().capture(); assert.equal(first.ok, true); assert.equal(first.pending, 50);
  42. const second = await f.callbacks().capture(); assert.equal(second.ok, true); assert.equal(second.pending, 0);
  43. assert.deepEqual(f.messages.filter(m => m.type === 'CAPTURE').map(m => m.records.length), [150, 50]);
  44. } finally { f.close(); }
  45. });
  46. test('失败与停止自动巡检不丢弃待发队列,撤销分配后旧自动采集回调不能发送', async () => {
  47. const f = fixture();
  48. try {
  49. await f.runtime.ready; f.setFail(true);
  50. const failed = await f.callbacks().capture(); assert.equal(failed.ok, false); assert.equal(failed.pending, 200);
  51. await f.configure({ automation: { enabled: false, assigned: false } });
  52. assert.equal((await f.runtime.scan()).deferred, true);
  53. const count = f.messages.filter(m => m.type === 'CAPTURE').length;
  54. assert.equal((await f.callbacks().capture()).ok, false); assert.equal(f.messages.filter(m => m.type === 'CAPTURE').length, count);
  55. // Re-enabling the same account resumes its original pending queue.
  56. f.setFail(false); await f.configure({ automation: { enabled: true, assigned: true } });
  57. assert.equal((await f.callbacks().capture()).pending, 50); assert.equal((await f.callbacks().capture()).pending, 0);
  58. } finally { f.close(); }
  59. });
  60. test('未验证初始聊天时只排空已收集队列,不解析可能正在切换的消息区', async () => {
  61. const f = fixture();
  62. try {
  63. await f.runtime.ready; f.setContext(null);
  64. assert.equal((await f.callbacks().capture()).records, 0); assert.equal(f.snapshots(), 0);
  65. f.setContext({ id: 'chat-stable-id', title: 'chat' }); f.setFail(true);
  66. assert.equal((await f.callbacks().capture()).pending, 200);
  67. f.setContext(null); f.setFail(false);
  68. assert.equal((await f.callbacks().capture()).pending, 50);
  69. assert.equal((await f.callbacks().capture()).pending, 0);
  70. assert.equal(f.snapshots(), 1);
  71. assert.deepEqual(f.messages.filter(m => m.type === 'CAPTURE').map(m => m.records.length), [0, 150, 150, 50]);
  72. } finally { f.close(); }
  73. });
  74. test('同名会话的稳定巡检上下文保持消息身份分离;页面自身会话 ID 优先', () => {
  75. const dom = new JSDOM('<div data-tid="chat-header-title">同名聊天</div><article data-message-id="m1"><div data-tid="message-body">内容</div></article>', { url: 'https://teams.cloud.microsoft/' });
  76. try {
  77. const a = collectVisible(dom.window.document, { contextOverride: { id: 'chat-A' } }).records[0];
  78. const b = collectVisible(dom.window.document, { contextOverride: { id: 'chat-B' } }).records[0];
  79. assert.notEqual(a.externalId, b.externalId); assert.equal(a.parentId, 'chat-A'); assert.equal(a.raw.automationContext, true);
  80. dom.window.document.querySelector('article').setAttribute('data-chat-id', 'pane-chat');
  81. const authoritative = collectVisible(dom.window.document, { contextOverride: { id: 'chat-A' } }).records[0];
  82. assert.equal(authoritative.parentId, 'pane-chat'); assert.equal(authoritative.raw.automationContext, undefined);
  83. } finally { dom.window.close(); }
  84. });
  85. test('自动采集只读取验证过的消息区,旁边另一个聊天或事件不能借用其身份', () => {
  86. const dom = new JSDOM('<div data-tid="chat-header-title">侧栏聊天</div><section id="primary" role="log"><article data-message-id="a">主聊天正文</article></section><section role="log"><article data-message-id="b">侧栏聊天正文</article></section><div data-event-id="event">旁边日程</div>', { url: 'https://teams.cloud.microsoft/' });
  87. try {
  88. const root = dom.window.document.querySelector('#primary');
  89. const options = { captureRoot: root, contextOverride: { id: 'chat-A', title: '主聊天' }, includeActivity: true };
  90. const result = collectVisible(dom.window.document, options);
  91. assert.equal(result.records.length, 1); assert.equal(result.records[0].text, '主聊天正文');
  92. assert.equal(result.records[0].parentId, 'chat-A'); assert.equal(result.records[0].title, '主聊天');
  93. root.remove();
  94. const stale = collectVisible(dom.window.document, options);
  95. assert.equal(stale.records.length, 0); assert.match(stale.errors[0], /已失效/);
  96. } finally { dom.window.close(); }
  97. });