step5-chatgpt-redirect-race.test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. const assert = require('assert');
  2. const fs = require('fs');
  3. const source = fs.readFileSync('background.js', 'utf8');
  4. function extractFunction(name) {
  5. const markers = [`async function ${name}(`, `function ${name}(`];
  6. const start = markers
  7. .map((marker) => source.indexOf(marker))
  8. .find((index) => index >= 0);
  9. if (start < 0) {
  10. throw new Error(`missing function ${name}`);
  11. }
  12. let parenDepth = 0;
  13. let signatureEnded = false;
  14. let braceStart = -1;
  15. for (let i = start; i < source.length; i += 1) {
  16. const ch = source[i];
  17. if (ch === '(') {
  18. parenDepth += 1;
  19. } else if (ch === ')') {
  20. parenDepth -= 1;
  21. if (parenDepth === 0) {
  22. signatureEnded = true;
  23. }
  24. } else if (ch === '{' && signatureEnded) {
  25. braceStart = i;
  26. break;
  27. }
  28. }
  29. if (braceStart < 0) {
  30. throw new Error(`missing body for function ${name}`);
  31. }
  32. let depth = 0;
  33. let end = braceStart;
  34. for (; end < source.length; end += 1) {
  35. const ch = source[end];
  36. if (ch === '{') depth += 1;
  37. if (ch === '}') {
  38. depth -= 1;
  39. if (depth === 0) {
  40. end += 1;
  41. break;
  42. }
  43. }
  44. }
  45. return source.slice(start, end);
  46. }
  47. const bundle = [
  48. extractFunction('waitForStep5ChatgptRedirect'),
  49. ].join('\n');
  50. const api = new Function(`
  51. let waitArgs = null;
  52. let waitResult = null;
  53. let currentTab = null;
  54. const chrome = {
  55. tabs: {
  56. async get() {
  57. return currentTab;
  58. },
  59. },
  60. };
  61. async function waitForTabUrlMatch(tabId, matcher, options = {}) {
  62. waitArgs = { tabId, matcher, options };
  63. return waitResult;
  64. }
  65. ${bundle}
  66. return {
  67. async run(tabId, timeoutMs) {
  68. return waitForStep5ChatgptRedirect(tabId, timeoutMs);
  69. },
  70. setWaitResult(value) {
  71. waitResult = value;
  72. },
  73. setCurrentTab(value) {
  74. currentTab = value;
  75. },
  76. snapshot() {
  77. return { waitArgs };
  78. },
  79. };
  80. `)();
  81. (async () => {
  82. const redirected = { id: 86, url: 'https://chatgpt.com/' };
  83. api.setWaitResult(redirected);
  84. api.setCurrentTab({ id: 86, url: 'https://auth.openai.com/' });
  85. let result = await api.run(86, 22000);
  86. let snapshot = api.snapshot();
  87. assert.deepStrictEqual(result, redirected, '等待命中 chatgpt.com 时应直接返回匹配到的标签页');
  88. assert.strictEqual(snapshot.waitArgs.tabId, 86, '应使用 signup-page 当前标签页等待跳转');
  89. assert.strictEqual(snapshot.waitArgs.options.timeoutMs, 22000, '应透传等待超时时间');
  90. assert.strictEqual(snapshot.waitArgs.options.retryDelayMs, 300, '应使用较短轮询间隔覆盖 URL 更新 race');
  91. assert.strictEqual(snapshot.waitArgs.matcher('https://chatgpt.com/?model=gpt-5'), true, 'matcher 应接受 chatgpt.com');
  92. assert.strictEqual(snapshot.waitArgs.matcher('https://auth.openai.com/u/signup'), false, 'matcher 不应把旧认证页误判为成功');
  93. api.setWaitResult(null);
  94. api.setCurrentTab({ id: 86, url: 'https://chatgpt.com/?temporary-chat=true' });
  95. result = await api.run(86, 15000);
  96. assert.deepStrictEqual(result, { id: 86, url: 'https://chatgpt.com/?temporary-chat=true' }, '等待超时后仍应回读当前标签页 URL 兜底');
  97. api.setCurrentTab({ id: 86, url: 'https://auth.openai.com/u/signup' });
  98. result = await api.run(86, 15000);
  99. assert.strictEqual(result, null, '仍停留旧域名时不应误判为跳转成功');
  100. result = await api.run(null, 15000);
  101. assert.strictEqual(result, null, '无有效标签页时应直接返回 null');
  102. console.log('step5 chatgpt redirect race tests passed');
  103. })().catch((error) => {
  104. console.error(error);
  105. process.exit(1);
  106. });