step6-login-state.test.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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('inspectLoginAuthState'),
  49. extractFunction('normalizeStep6Snapshot'),
  50. ].join('\n');
  51. function createApi(overrides = {}) {
  52. return new Function(`
  53. const location = {
  54. href: ${JSON.stringify(overrides.href || 'https://auth.openai.com/log-in')},
  55. pathname: ${JSON.stringify(overrides.pathname || '/log-in')},
  56. };
  57. function getLoginTimeoutErrorPageState() {
  58. return ${JSON.stringify(overrides.retryState || null)};
  59. }
  60. function getVerificationCodeTarget() {
  61. return ${JSON.stringify(overrides.verificationTarget || null)};
  62. }
  63. function getLoginPasswordInput() {
  64. return ${JSON.stringify(overrides.passwordInput || null)};
  65. }
  66. function getLoginEmailInput() {
  67. return ${JSON.stringify(overrides.emailInput || null)};
  68. }
  69. function findOneTimeCodeLoginTrigger() {
  70. return ${JSON.stringify(overrides.switchTrigger || null)};
  71. }
  72. function getLoginSubmitButton() {
  73. return ${JSON.stringify(overrides.submitButton || null)};
  74. }
  75. function isVerificationPageStillVisible() {
  76. return ${JSON.stringify(Boolean(overrides.verificationVisible))};
  77. }
  78. function isAddPhonePageReady() {
  79. return ${JSON.stringify(Boolean(overrides.addPhonePage))};
  80. }
  81. function isStep8Ready() {
  82. return ${JSON.stringify(Boolean(overrides.consentReady))};
  83. }
  84. function isOAuthConsentPage() {
  85. return ${JSON.stringify(Boolean(overrides.oauthConsentPage))};
  86. }
  87. ${bundle}
  88. return {
  89. inspectLoginAuthState,
  90. normalizeStep6Snapshot,
  91. };
  92. `)();
  93. }
  94. {
  95. const api = createApi({
  96. emailInput: { id: 'email' },
  97. submitButton: { id: 'submit' },
  98. oauthConsentPage: true,
  99. consentReady: true,
  100. });
  101. const snapshot = api.inspectLoginAuthState();
  102. assert.strictEqual(
  103. snapshot.state,
  104. 'email_page',
  105. '第六步在 /log-in 页应优先识别为邮箱页'
  106. );
  107. }
  108. {
  109. const api = createApi({
  110. oauthConsentPage: true,
  111. consentReady: true,
  112. });
  113. const snapshot = api.normalizeStep6Snapshot({
  114. state: 'oauth_consent_page',
  115. url: 'https://auth.openai.com/authorize',
  116. });
  117. assert.strictEqual(snapshot.state, 'unknown', '第六步应忽略 oauth_consent_page 状态');
  118. }
  119. assert.ok(
  120. !extractFunction('inspectLoginAuthState').includes("state: 'oauth_consent_page'"),
  121. 'inspectLoginAuthState 不应再产出 oauth_consent_page 状态'
  122. );
  123. console.log('step6 login state tests passed');