| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- import { test } from 'node:test';
- import assert from 'node:assert/strict';
- import { JSDOM } from 'jsdom';
- import { installContentRuntime } from '../extension/content-runtime.js';
- import { collectVisible } from '../extension/parser.js';
- function fixture() {
- const dom = new JSDOM('<html><body></body></html>', { url: 'https://teams.cloud.microsoft/' });
- let current = { accountId: 'work', enabled: true, selectors: {}, automation: { enabled: true, assigned: true } };
- let fail = false, snapshots = 0, context = { id: 'chat-stable-id', title: 'chat' };
- const messages = [], listeners = [], scope = {};
- let callbacks, controller;
- dom.window.setTimeout = () => 1; dom.window.clearTimeout = () => {};
- dom.window.setInterval = () => 2; dom.window.clearInterval = () => {};
- const page = { url: dom.window.location.href, title: 'Teams', signedIn: true, view: 'message', coverage: 'test' };
- const records = Array.from({ length: 200 }, (_, i) => ({ kind: 'message', externalId: `m${i}`, title: 'chat', text: `message ${i}`, raw: {} }));
- const runtime = installContentRuntime({ window: dom.window, document: dom.window.document, scope,
- chrome: { runtime: { id: 'test', onMessage: { addListener: cb => listeners.push(cb), removeListener() {} }, sendMessage: async message => {
- messages.push(message);
- if (message.type === 'GET_CONFIG') return { ok: true, config: structuredClone(current) };
- if (message.type === 'CAPTURE' && fail) return { ok: false, error: '服务离线' };
- return { ok: true };
- } } },
- collect: (document, options) => { snapshots++; assert.equal(options.contextOverride?.id, 'chat-stable-id'); return { records, page, counts: { message: 200 }, errors: [] }; },
- Crawler: dependencies => {
- callbacks = dependencies;
- controller = { configure() {}, stop() {}, dispose() {}, resume() {}, async tick() {}, isNavigating: () => false,
- status: () => ({ running: false, phase: 'paused' }), captureContext: () => context, preview: () => ({ count: 0, labels: [], errors: [] }) };
- return controller;
- },
- });
- return { runtime, messages, callbacks: () => callbacks, snapshots: () => snapshots, setFail: value => { fail = value; },
- setContext: value => { context = value; },
- async configure(patch) { current = { ...current, ...patch }; return new Promise(resolve => listeners[0]({ type: 'CONFIG_CHANGED' }, { id: 'test' }, resolve)); },
- close() { runtime.dispose(); dom.window.close(); } };
- }
- test('自动标签的被动扫描保持静默;验证后的采集回调排空多批消息并使用稳定会话上下文', async () => {
- const f = fixture();
- try {
- await f.runtime.ready;
- assert.equal((await f.runtime.scan()).deferred, true); assert.equal(f.snapshots(), 0);
- const first = await f.callbacks().capture(); assert.equal(first.ok, true); assert.equal(first.pending, 50);
- const second = await f.callbacks().capture(); assert.equal(second.ok, true); assert.equal(second.pending, 0);
- assert.deepEqual(f.messages.filter(m => m.type === 'CAPTURE').map(m => m.records.length), [150, 50]);
- } finally { f.close(); }
- });
- test('失败与停止自动巡检不丢弃待发队列,撤销分配后旧自动采集回调不能发送', async () => {
- const f = fixture();
- try {
- await f.runtime.ready; f.setFail(true);
- const failed = await f.callbacks().capture(); assert.equal(failed.ok, false); assert.equal(failed.pending, 200);
- await f.configure({ automation: { enabled: false, assigned: false } });
- assert.equal((await f.runtime.scan()).deferred, true);
- const count = f.messages.filter(m => m.type === 'CAPTURE').length;
- assert.equal((await f.callbacks().capture()).ok, false); assert.equal(f.messages.filter(m => m.type === 'CAPTURE').length, count);
- // Re-enabling the same account resumes its original pending queue.
- f.setFail(false); await f.configure({ automation: { enabled: true, assigned: true } });
- assert.equal((await f.callbacks().capture()).pending, 50); assert.equal((await f.callbacks().capture()).pending, 0);
- } finally { f.close(); }
- });
- test('未验证初始聊天时只排空已收集队列,不解析可能正在切换的消息区', async () => {
- const f = fixture();
- try {
- await f.runtime.ready; f.setContext(null);
- assert.equal((await f.callbacks().capture()).records, 0); assert.equal(f.snapshots(), 0);
- f.setContext({ id: 'chat-stable-id', title: 'chat' }); f.setFail(true);
- assert.equal((await f.callbacks().capture()).pending, 200);
- f.setContext(null); f.setFail(false);
- assert.equal((await f.callbacks().capture()).pending, 50);
- assert.equal((await f.callbacks().capture()).pending, 0);
- assert.equal(f.snapshots(), 1);
- assert.deepEqual(f.messages.filter(m => m.type === 'CAPTURE').map(m => m.records.length), [0, 150, 150, 50]);
- } finally { f.close(); }
- });
- test('同名会话的稳定巡检上下文保持消息身份分离;页面自身会话 ID 优先', () => {
- 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/' });
- try {
- const a = collectVisible(dom.window.document, { contextOverride: { id: 'chat-A' } }).records[0];
- const b = collectVisible(dom.window.document, { contextOverride: { id: 'chat-B' } }).records[0];
- assert.notEqual(a.externalId, b.externalId); assert.equal(a.parentId, 'chat-A'); assert.equal(a.raw.automationContext, true);
- dom.window.document.querySelector('article').setAttribute('data-chat-id', 'pane-chat');
- const authoritative = collectVisible(dom.window.document, { contextOverride: { id: 'chat-A' } }).records[0];
- assert.equal(authoritative.parentId, 'pane-chat'); assert.equal(authoritative.raw.automationContext, undefined);
- } finally { dom.window.close(); }
- });
- test('自动采集只读取验证过的消息区,旁边另一个聊天或事件不能借用其身份', () => {
- 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/' });
- try {
- const root = dom.window.document.querySelector('#primary');
- const options = { captureRoot: root, contextOverride: { id: 'chat-A', title: '主聊天' }, includeActivity: true };
- const result = collectVisible(dom.window.document, options);
- assert.equal(result.records.length, 1); assert.equal(result.records[0].text, '主聊天正文');
- assert.equal(result.records[0].parentId, 'chat-A'); assert.equal(result.records[0].title, '主聊天');
- root.remove();
- const stale = collectVisible(dom.window.document, options);
- assert.equal(stale.records.length, 0); assert.match(stale.errors[0], /已失效/);
- } finally { dom.window.close(); }
- });
|