| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- import { test } from 'node:test';
- import assert from 'node:assert/strict';
- import { readFileSync } from 'node:fs';
- import { JSDOM } from 'jsdom';
- const html = readFileSync(new URL('../public/index.html', import.meta.url), 'utf8');
- const app = readFileSync(new URL('../public/app.js', import.meta.url), 'utf8');
- const iso = delta => new Date(Date.now() + delta).toISOString();
- test('控制台正确区分配对、页面心跳、新错误、账号切换与暂停', async () => {
- const heartbeat = () => ({ at: iso(-10000), accountId: 'browser:A', signedIn: true, received: 0, changed: 0, coverage: 'visible-loaded-dom-only' });
- const diagnostic = () => ({ at: iso(0), accountId: 'A', enabled: true, teamsTabCount: 1, readyTabCount: 1, error: '' });
- const cases = [
- { data: { pairing: { at: iso(0) } }, title: '已配对 · 等待页面' },
- { data: { browser: heartbeat(), extension: { ...diagnostic(), error: '脚本启动失败' } }, title: '页面连接异常', detail: '脚本启动失败' },
- { data: { browser: { ...heartbeat(), at: iso(0) }, extension: { ...diagnostic(), at: iso(-20000), error: '旧错误' } }, title: '页面已连接' },
- { data: { browser: heartbeat(), extension: { ...diagnostic(), readyTabCount: 0 } }, title: '等待页面连接' },
- { data: { browser: heartbeat(), extension: { ...diagnostic(), accountId: 'B' } }, title: '等待页面心跳', coverage: '当前配置账号:B', timeLabel: '最近扩展连接' },
- { data: { browser: heartbeat(), extension: { ...diagnostic(), enabled: false } }, title: '已配对 · 采集暂停' },
- { data: {}, title: '等待配对' },
- ];
- const keys = ['document', 'location', 'history', 'fetch', 'setInterval'];
- const previous = new Map(keys.map(key => [key, Object.getOwnPropertyDescriptor(globalThis, key)]));
- let run = 0;
- try {
- for (const scenario of cases) {
- const dom = new JSDOM(html, { url: 'http://127.0.0.1:4318/' });
- const data = { localToken: 'test', pairingToken: 'test', dataDir: 'test', browser: null, extension: null, pairing: null,
- stats: { groups: [], accounts: [], versions: 0 }, graph: { configured: false }, ...scenario.data };
- const values = { document: dom.window.document, location: dom.window.location, history: dom.window.history, setInterval: () => 0,
- fetch: async url => ({ ok: true, json: async () => url === '/api/status' ? data : url === '/api/logs' ? [] : { items: [], total: 0 } }) };
- for (const [key, value] of Object.entries(values)) Object.defineProperty(globalThis, key, { value, configurable: true, writable: true });
- try {
- await import(`data:text/javascript;base64,${Buffer.from(app + `\n// scenario ${run++}`).toString('base64')}`);
- const text = id => dom.window.document.getElementById(id).textContent;
- assert.equal(text('connection'), scenario.title);
- if (scenario.detail) assert.equal(text('browserDetail'), scenario.detail);
- if (scenario.coverage) assert.ok(text('coverage').startsWith(scenario.coverage));
- if (scenario.timeLabel) assert.ok(text('heartbeat').startsWith(scenario.timeLabel));
- } finally { dom.window.close(); }
- }
- } finally {
- for (const [key, descriptor] of previous) if (descriptor) Object.defineProperty(globalThis, key, descriptor); else delete globalThis[key];
- }
- });
|