step3-direct-complete.test.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 3 reports completion before deferred submit click', async () => {
  49. const api = new Function(`
  50. const logs = [];
  51. const completions = [];
  52. const clicks = [];
  53. const scheduled = [];
  54. const snapshot = {
  55. state: 'password_page',
  56. passwordInput: { value: '', hidden: false },
  57. submitButton: { textContent: 'Continue', hidden: false },
  58. displayedEmail: 'user@example.com',
  59. };
  60. const window = {
  61. setTimeout(fn) {
  62. scheduled.push(fn);
  63. return scheduled.length;
  64. },
  65. };
  66. const location = {
  67. href: 'https://auth.openai.com/create-account/password',
  68. };
  69. function inspectSignupEntryState() {
  70. return snapshot;
  71. }
  72. async function ensureSignupPasswordPageReady() {
  73. return { ready: true };
  74. }
  75. function getSignupPasswordSubmitButton() {
  76. return snapshot.submitButton;
  77. }
  78. async function waitForElementByText() {
  79. return null;
  80. }
  81. function fillInput(input, value) {
  82. input.value = value;
  83. }
  84. async function humanPause() {}
  85. async function sleep() {}
  86. function log(message, level = 'info') {
  87. logs.push({ message, level });
  88. }
  89. function reportComplete(step, payload) {
  90. completions.push({ step, payload });
  91. }
  92. function simulateClick(target) {
  93. clicks.push(target.textContent || 'button');
  94. }
  95. ${extractFunction('step3_fillEmailPassword')}
  96. return {
  97. async run(payload) {
  98. return step3_fillEmailPassword(payload);
  99. },
  100. async flushDeferredSubmit() {
  101. if (!scheduled.length) {
  102. throw new Error('missing deferred submit');
  103. }
  104. await scheduled[0]();
  105. },
  106. snapshot() {
  107. return {
  108. logs,
  109. completions,
  110. clicks,
  111. passwordValue: snapshot.passwordInput.value,
  112. scheduledCount: scheduled.length,
  113. };
  114. },
  115. };
  116. `)();
  117. const result = await api.run({
  118. email: 'user@example.com',
  119. password: 'Secret123!',
  120. });
  121. const beforeSubmit = api.snapshot();
  122. assert.equal(beforeSubmit.passwordValue, 'Secret123!');
  123. assert.equal(beforeSubmit.scheduledCount, 1);
  124. assert.deepStrictEqual(beforeSubmit.clicks, []);
  125. assert.equal(beforeSubmit.completions.length, 1);
  126. assert.equal(beforeSubmit.completions[0].step, 3);
  127. assert.deepStrictEqual(result, beforeSubmit.completions[0].payload);
  128. assert.equal(result.email, 'user@example.com');
  129. assert.equal(result.deferredSubmit, true);
  130. assert.equal(typeof result.signupVerificationRequestedAt, 'number');
  131. await api.flushDeferredSubmit();
  132. const afterSubmit = api.snapshot();
  133. assert.deepStrictEqual(afterSubmit.clicks, ['Continue']);
  134. });