step6-timeout-recovery.test.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. const test = require('node:test');
  2. const assert = require('node:assert/strict');
  3. const fs = require('node:fs');
  4. const source = fs.readFileSync('content/signup-page.js', 'utf8');
  5. function extractFunction(name) {
  6. const markers = [`async function ${name}(`, `function ${name}(`];
  7. const start = markers
  8. .map((marker) => source.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 index = start; index < source.length; index += 1) {
  17. const char = source[index];
  18. if (char === '(') {
  19. parenDepth += 1;
  20. } else if (char === ')') {
  21. parenDepth -= 1;
  22. if (parenDepth === 0) {
  23. signatureEnded = true;
  24. }
  25. } else if (char === '{' && signatureEnded) {
  26. braceStart = index;
  27. break;
  28. }
  29. }
  30. if (braceStart < 0) {
  31. throw new Error(`missing body for function ${name}`);
  32. }
  33. let depth = 0;
  34. let end = braceStart;
  35. for (; end < source.length; end += 1) {
  36. const char = source[end];
  37. if (char === '{') depth += 1;
  38. if (char === '}') {
  39. depth -= 1;
  40. if (depth === 0) {
  41. end += 1;
  42. break;
  43. }
  44. }
  45. }
  46. return source.slice(start, end);
  47. }
  48. test('step 7 timeout recoverable result no longer clicks retry before asking background to rerun', async () => {
  49. const api = new Function(`
  50. const logs = [];
  51. let recoverCalls = 0;
  52. const location = {
  53. href: 'https://auth.openai.com/log-in',
  54. };
  55. function inspectLoginAuthState() {
  56. return {
  57. state: 'login_timeout_error_page',
  58. url: location.href,
  59. };
  60. }
  61. async function recoverCurrentAuthRetryPage() {
  62. recoverCalls += 1;
  63. return { recovered: true };
  64. }
  65. function log(message, level = 'info') {
  66. logs.push({ message, level });
  67. }
  68. ${extractFunction('createStep6RecoverableResult')}
  69. ${extractFunction('normalizeStep6Snapshot')}
  70. ${extractFunction('createStep6LoginTimeoutRecoverableResult')}
  71. return {
  72. async run() {
  73. return createStep6LoginTimeoutRecoverableResult(
  74. 'login_timeout_error_page',
  75. { state: 'login_timeout_error_page', url: location.href },
  76. '当前页面处于登录超时报错页。'
  77. );
  78. },
  79. snapshot() {
  80. return { logs, recoverCalls };
  81. },
  82. };
  83. `)();
  84. const result = await api.run();
  85. const snapshot = api.snapshot();
  86. assert.equal(snapshot.recoverCalls, 0);
  87. assert.equal(result.step6Outcome, 'recoverable');
  88. assert.equal(result.reason, 'login_timeout_error_page');
  89. assert.equal(result.state, 'login_timeout_error_page');
  90. assert.equal(result.message, '当前页面处于登录超时报错页。');
  91. });