step8-retry-page-recovery.test.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. const test = require('node:test');
  2. const assert = require('node:assert/strict');
  3. const fs = require('node:fs');
  4. const source = fs.readFileSync('background.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 8 click effect returns restart_current_step when retry page is recovered', async () => {
  49. const api = new Function(`
  50. let recoverCalls = 0;
  51. const chrome = {
  52. tabs: {
  53. async get() {
  54. return {
  55. id: 88,
  56. url: 'https://auth.openai.com/authorize',
  57. };
  58. },
  59. },
  60. };
  61. function throwIfStopped() {}
  62. async function sleepWithStop() {}
  63. async function ensureStep8SignupPageReady() {}
  64. async function recoverAuthRetryPageOnTab() {
  65. recoverCalls += 1;
  66. return { recovered: true };
  67. }
  68. async function getStep8PageState() {
  69. return {
  70. url: 'https://auth.openai.com/authorize',
  71. retryPage: true,
  72. addPhonePage: false,
  73. consentPage: false,
  74. verificationPage: false,
  75. };
  76. }
  77. ${extractFunction('waitForStep8ClickEffect')}
  78. return {
  79. async run() {
  80. return waitForStep8ClickEffect(88, 'https://auth.openai.com/authorize', 1000);
  81. },
  82. snapshot() {
  83. return { recoverCalls };
  84. },
  85. };
  86. `)();
  87. const result = await api.run();
  88. const snapshot = api.snapshot();
  89. assert.deepStrictEqual(result, {
  90. progressed: false,
  91. reason: 'retry_page_recovered',
  92. restartCurrentStep: true,
  93. url: 'https://auth.openai.com/authorize',
  94. });
  95. assert.equal(snapshot.recoverCalls, 1);
  96. });