step5-chatgpt-onboarding.test.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. const assert = require('assert');
  2. const fs = require('fs');
  3. const source = fs.readFileSync('content/signup-page.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('getPageTextSnapshot'),
  49. extractFunction('findChatgptSkipButton'),
  50. extractFunction('waitForChatgptSkipButton'),
  51. extractFunction('isChatgptOnboardingPage'),
  52. extractFunction('isChatgptUrl'),
  53. extractFunction('hasVisibleElementMatchingSelector'),
  54. extractFunction('isChatgptAuthenticatedHomePage'),
  55. extractFunction('waitForChatgptPostSignupState'),
  56. extractFunction('skipChatgptOnboarding'),
  57. ].join('\n');
  58. const api = new Function(`
  59. const CHATGPT_ONBOARDING_TEXT_PATTERN = /welcome\\s+to\\s+chatgpt|what\\s+should\\s+chatgpt\\s+call\\s+you|how\\s+do\\s+you\\s+want\\s+chatgpt\\s+to\\s+respond|tell\\s+chatgpt\\s+what\\s+traits|personal(?:ize|ise)\\s+your\\s+experience|介绍一下你自己|ChatGPT 应该如何称呼你|你希望 ChatGPT 如何回应/i;
  60. const CHATGPT_HOME_TEXT_PATTERN = /new\\s+chat|temporary\\s+chat|message\\s+chatgpt|send\\s+a\\s+message|chatgpt\\s+can\\s+make\\s+mistakes|新建聊天|临时聊天|给\\s*ChatGPT\\s*发消息|ChatGPT\\s*可能会犯错/i;
  61. let buttonList = [];
  62. let selectorMap = {};
  63. let pageText = '';
  64. let clickedButtons = [];
  65. let logs = [];
  66. const location = { href: 'https://chatgpt.com/' };
  67. const document = {
  68. body: { innerText: '', textContent: '' },
  69. querySelectorAll(selector) {
  70. if (selector === 'button') {
  71. return buttonList;
  72. }
  73. return selectorMap[selector] || [];
  74. },
  75. };
  76. function isVisibleElement(el) {
  77. return Boolean(el) && !el.hidden;
  78. }
  79. function throwIfStopped() {}
  80. async function sleep() {}
  81. async function humanPause() {}
  82. function log(message, level = 'info') {
  83. logs.push({ message, level });
  84. }
  85. function simulateClick(button) {
  86. clickedButtons.push(button.textContent || button.id || 'button');
  87. button.hidden = true;
  88. }
  89. ${bundle}
  90. return {
  91. setPage({ href = 'https://chatgpt.com/', text = '', buttons = [], selectors = {} }) {
  92. location.href = href;
  93. pageText = text;
  94. buttonList = buttons.map((button, index) => ({
  95. id: button.id || \`button-\${index}\`,
  96. textContent: button.textContent || '',
  97. className: button.className || '',
  98. hidden: Boolean(button.hidden),
  99. }));
  100. selectorMap = {};
  101. for (const [selector, count] of Object.entries(selectors)) {
  102. selectorMap[selector] = Array.from({ length: count }, (_, index) => ({ id: \`\${selector}-\${index}\`, hidden: false }));
  103. }
  104. document.body.innerText = pageText;
  105. document.body.textContent = pageText;
  106. clickedButtons = [];
  107. logs = [];
  108. },
  109. isChatgptOnboardingPage() {
  110. return isChatgptOnboardingPage();
  111. },
  112. isChatgptAuthenticatedHomePage() {
  113. return isChatgptAuthenticatedHomePage();
  114. },
  115. async skipChatgptOnboarding() {
  116. return skipChatgptOnboarding();
  117. },
  118. snapshot() {
  119. return { clickedButtons, logs };
  120. },
  121. };
  122. `)();
  123. (async () => {
  124. api.setPage({
  125. href: 'https://chatgpt.com/',
  126. text: 'New chat ChatGPT can make mistakes',
  127. selectors: {
  128. 'textarea[placeholder*="Message" i]': 1,
  129. },
  130. });
  131. assert.strictEqual(api.isChatgptOnboardingPage(), false, '已登录主页不应仅因 chatgpt.com URL 被误判为 onboarding');
  132. assert.strictEqual(api.isChatgptAuthenticatedHomePage(), true, '主页特征存在时应识别为已登录 ChatGPT 页面');
  133. let result = await api.skipChatgptOnboarding();
  134. let snapshot = api.snapshot();
  135. assert.deepStrictEqual(result, { success: true, alreadyCompleted: true }, '无 Skip 按钮但已进入主页时应按成功处理');
  136. assert.deepStrictEqual(snapshot.clickedButtons, [], '已登录主页场景不应再误点按钮');
  137. api.setPage({
  138. href: 'https://chatgpt.com/',
  139. text: 'Welcome to ChatGPT',
  140. buttons: [
  141. { textContent: 'Skip', className: 'btn-ghost' },
  142. { textContent: 'Skip', className: 'btn-ghost' },
  143. ],
  144. });
  145. assert.strictEqual(api.isChatgptOnboardingPage(), true, '存在 Skip 按钮时应识别为 onboarding');
  146. result = await api.skipChatgptOnboarding();
  147. snapshot = api.snapshot();
  148. assert.deepStrictEqual(result, { success: true }, '真实 onboarding 仍应继续执行跳过逻辑');
  149. assert.deepStrictEqual(snapshot.clickedButtons, ['Skip', 'Skip'], '双 Skip onboarding 应依次点击两个按钮');
  150. console.log('step5 chatgpt onboarding tests passed');
  151. })().catch((error) => {
  152. console.error(error);
  153. process.exit(1);
  154. });