step5-direct-complete.test.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 i = start; i < source.length; i += 1) {
  17. const ch = source[i];
  18. if (ch === '(') {
  19. parenDepth += 1;
  20. } else if (ch === ')') {
  21. parenDepth -= 1;
  22. if (parenDepth === 0) {
  23. signatureEnded = true;
  24. }
  25. } else if (ch === '{' && signatureEnded) {
  26. braceStart = i;
  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 ch = source[end];
  37. if (ch === '{') depth += 1;
  38. if (ch === '}') {
  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 5 clicks submit and completes immediately on birthday page', async () => {
  49. const step5Source = extractFunction('step5_fillNameBirthday');
  50. assert.ok(
  51. !step5Source.includes('waitForStep5SubmitOutcome('),
  52. 'Step 5 提交后不应再等待页面结果'
  53. );
  54. const api = new Function(`
  55. const logs = [];
  56. const completions = [];
  57. const clicks = [];
  58. const selectedBirthday = {};
  59. const nameInput = { value: '', hidden: false };
  60. const hiddenBirthday = {
  61. value: '',
  62. hidden: false,
  63. dispatchEvent() {},
  64. };
  65. const completeButton = {
  66. tagName: 'BUTTON',
  67. textContent: '完成帐户创建',
  68. hidden: false,
  69. };
  70. const birthdaySelects = {
  71. '年': { label: '年', button: { hidden: false }, nativeSelect: {} },
  72. '月': { label: '月', button: { hidden: false }, nativeSelect: {} },
  73. '天': { label: '天', button: { hidden: false }, nativeSelect: {} },
  74. };
  75. const document = {
  76. querySelector(selector) {
  77. switch (selector) {
  78. case '[role="spinbutton"][data-type="year"]':
  79. case '[role="spinbutton"][data-type="month"]':
  80. case '[role="spinbutton"][data-type="day"]':
  81. case 'input[name="age"]':
  82. return null;
  83. case 'input[name="birthday"]':
  84. return hiddenBirthday;
  85. case 'button[type="submit"]':
  86. return completeButton;
  87. default:
  88. return null;
  89. }
  90. },
  91. querySelectorAll(selector) {
  92. if (selector === 'input[name="allCheckboxes"][type="checkbox"]') {
  93. return [];
  94. }
  95. return [];
  96. },
  97. execCommand() {},
  98. };
  99. const location = {
  100. href: 'https://auth.openai.com/u/signup/profile',
  101. };
  102. function Event(type, init = {}) {
  103. this.type = type;
  104. this.bubbles = Boolean(init.bubbles);
  105. }
  106. function log(message, level = 'info') {
  107. logs.push({ message, level });
  108. }
  109. async function waitForElement() {
  110. return nameInput;
  111. }
  112. async function humanPause() {}
  113. async function sleep() {}
  114. function fillInput(input, value) {
  115. input.value = value;
  116. }
  117. function findBirthdayReactAriaSelect(label) {
  118. return birthdaySelects[label] || null;
  119. }
  120. function isVisibleElement(el) {
  121. return Boolean(el) && !el.hidden;
  122. }
  123. async function setReactAriaBirthdaySelect(select, value) {
  124. selectedBirthday[select.label] = String(value).padStart(select.label === '年' ? 4 : 2, '0');
  125. if (selectedBirthday['年'] && selectedBirthday['月'] && selectedBirthday['天']) {
  126. hiddenBirthday.value = \`\${selectedBirthday['年']}-\${selectedBirthday['月']}-\${selectedBirthday['天']}\`;
  127. }
  128. }
  129. async function waitForElementByText() {
  130. throw new Error('waitForElementByText should not run in this test');
  131. }
  132. function simulateClick(el) {
  133. clicks.push(el.textContent || el.tagName || 'element');
  134. }
  135. function reportComplete(step, payload) {
  136. completions.push({ step, payload });
  137. }
  138. function normalizeInlineText(text) {
  139. return text;
  140. }
  141. ${extractFunction('getStep5DirectCompletionPayload')}
  142. ${extractFunction('step5_fillNameBirthday')}
  143. return {
  144. async run(payload) {
  145. return step5_fillNameBirthday(payload);
  146. },
  147. snapshot() {
  148. return {
  149. logs,
  150. completions,
  151. clicks,
  152. nameValue: nameInput.value,
  153. birthdayValue: hiddenBirthday.value,
  154. };
  155. },
  156. };
  157. `)();
  158. const result = await api.run({
  159. firstName: 'Test',
  160. lastName: 'User',
  161. year: 2003,
  162. month: 6,
  163. day: 19,
  164. });
  165. const snapshot = api.snapshot();
  166. assert.deepStrictEqual(
  167. result,
  168. {
  169. skippedPostSubmitCheck: true,
  170. directProceedToStep6: true,
  171. },
  172. '生日模式点击提交后应直接返回完成载荷'
  173. );
  174. assert.deepStrictEqual(snapshot.completions, [
  175. {
  176. step: 5,
  177. payload: {
  178. skippedPostSubmitCheck: true,
  179. directProceedToStep6: true,
  180. },
  181. },
  182. ]);
  183. assert.deepStrictEqual(snapshot.clicks, ['完成帐户创建']);
  184. assert.equal(snapshot.nameValue, 'Test User');
  185. assert.equal(snapshot.birthdayValue, '2003-06-19');
  186. assert.ok(
  187. snapshot.logs.some(({ message }) => /不再等待页面结果/.test(message)),
  188. '日志应明确说明 Step 5 已直接完成'
  189. );
  190. });