内容
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('
', { 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('