step8-state-timeout-retry.test.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. const assert = require('assert');
  2. const fs = require('fs');
  3. const source = fs.readFileSync('background.js', 'utf8');
  4. function extractFunction(name) {
  5. const start = source.indexOf(`function ${name}(`);
  6. if (start < 0) {
  7. throw new Error(`missing function ${name}`);
  8. }
  9. const braceStart = source.indexOf('{', start);
  10. let depth = 0;
  11. let end = braceStart;
  12. for (; end < source.length; end++) {
  13. const ch = source[end];
  14. if (ch === '{') depth += 1;
  15. if (ch === '}') {
  16. depth -= 1;
  17. if (depth === 0) {
  18. end += 1;
  19. break;
  20. }
  21. }
  22. }
  23. return source.slice(start, end);
  24. }
  25. const bundle = [
  26. extractFunction('isRetryableContentScriptTransportError'),
  27. ].join('\n');
  28. const api = new Function(`${bundle}; return { isRetryableContentScriptTransportError };`)();
  29. assert.strictEqual(
  30. api.isRetryableContentScriptTransportError(new Error('Content script on signup-page did not respond in 2s. Try refreshing the tab and retry.')),
  31. true,
  32. 'Step 8 状态探测短超时应被视为可重试错误'
  33. );
  34. assert.strictEqual(
  35. api.isRetryableContentScriptTransportError(new Error('Content script on signup-page did not respond in 30s. Try refreshing the tab and retry.')),
  36. true,
  37. '普通内容脚本超时也应沿用可重试分支'
  38. );
  39. assert.strictEqual(
  40. api.isRetryableContentScriptTransportError(new Error('按钮不存在')),
  41. false,
  42. '真实业务错误不应被误判为可重试传输错误'
  43. );
  44. console.log('step8 state timeout retry tests passed');