sidepanel-account-run-history.test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. const test = require('node:test');
  2. const assert = require('node:assert/strict');
  3. const fs = require('node:fs');
  4. const sidepanelSource = fs.readFileSync('sidepanel/sidepanel.js', 'utf8');
  5. function extractFunction(name) {
  6. const markers = [`async function ${name}(`, `function ${name}(`];
  7. const start = markers
  8. .map((marker) => sidepanelSource.indexOf(marker))
  9. .find((index) => index >= 0);
  10. if (start < 0) {
  11. throw new Error(`missing function ${name}`);
  12. }
  13. let parenDepth = 0;
  14. let signatureEnded = false;
  15. let braceStart = -1;
  16. for (let i = start; i < sidepanelSource.length; i += 1) {
  17. const ch = sidepanelSource[i];
  18. if (ch === '(') {
  19. parenDepth += 1;
  20. } else if (ch === ')') {
  21. parenDepth -= 1;
  22. if (parenDepth === 0) {
  23. signatureEnded = true;
  24. }
  25. } else if (ch === '{' && signatureEnded) {
  26. braceStart = i;
  27. break;
  28. }
  29. }
  30. let depth = 0;
  31. let end = braceStart;
  32. for (; end < sidepanelSource.length; end += 1) {
  33. const ch = sidepanelSource[end];
  34. if (ch === '{') depth += 1;
  35. if (ch === '}') {
  36. depth -= 1;
  37. if (depth === 0) {
  38. end += 1;
  39. break;
  40. }
  41. }
  42. }
  43. return sidepanelSource.slice(start, end);
  44. }
  45. test('sidepanel html contains account run history strip under log header', () => {
  46. const html = fs.readFileSync('sidepanel/sidepanel.html', 'utf8');
  47. assert.match(html, /id="account-run-history-strip"/);
  48. assert.match(html, /id="account-run-history-meta"/);
  49. assert.match(html, /id="account-run-history-stats"/);
  50. assert.match(html, /id="account-run-history-list"/);
  51. });
  52. test('sidepanel account run history helpers classify statuses and summarize counts', () => {
  53. const bundle = [
  54. extractFunction('parseAccountRunStatus'),
  55. extractFunction('summarizeAccountRunHistory'),
  56. extractFunction('buildAccountRunHistoryDetailText'),
  57. ].join('\n');
  58. const api = new Function(`${bundle}; return { parseAccountRunStatus, summarizeAccountRunHistory, buildAccountRunHistoryDetailText };`)();
  59. assert.deepStrictEqual(api.parseAccountRunStatus('step7_failed'), {
  60. kind: 'failed',
  61. label: '步7失败',
  62. });
  63. assert.deepStrictEqual(api.parseAccountRunStatus('step4_stopped'), {
  64. kind: 'stopped',
  65. label: '步4停止',
  66. });
  67. assert.deepStrictEqual(api.parseAccountRunStatus('success'), {
  68. kind: 'success',
  69. label: '成功',
  70. });
  71. assert.deepStrictEqual(api.summarizeAccountRunHistory([
  72. { status: 'success' },
  73. { status: 'step7_failed' },
  74. { status: 'stopped' },
  75. { status: 'step2_failed' },
  76. ]), {
  77. total: 4,
  78. success: 1,
  79. failed: 2,
  80. stopped: 1,
  81. other: 0,
  82. });
  83. assert.equal(
  84. api.buildAccountRunHistoryDetailText({ status: 'success', reason: '' }),
  85. '流程已完成并写入本地记录'
  86. );
  87. assert.equal(
  88. api.buildAccountRunHistoryDetailText({ status: 'step7_failed', reason: '' }),
  89. '流程执行失败,已保留当前账号快照'
  90. );
  91. assert.equal(
  92. api.buildAccountRunHistoryDetailText({ status: 'stopped', reason: '手动停止' }),
  93. '手动停止'
  94. );
  95. });