ui-state.test.js 3.3 KB

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