Selaa lähdekoodia

feat: 更新验证页面逻辑,确保密码重试页面不被视为验证页面,并添加相关测试

QLHazyCoder 4 kuukautta sitten
vanhempi
sitoutus
466011d4e5
2 muutettua tiedostoa jossa 153 lisäystä ja 4 poistoa
  1. 11 4
      content/signup-page.js
  2. 142 0
      tests/signup-verification-state-guard.test.js

+ 11 - 4
content/signup-page.js

@@ -752,10 +752,17 @@ function isOAuthConsentPage() {
 }
 }
 
 
 function isVerificationPageStillVisible() {
 function isVerificationPageStillVisible() {
+  if (getCurrentAuthRetryPageState('signup_password') || getCurrentAuthRetryPageState('login')) {
+    return false;
+  }
   if (getVerificationCodeTarget()) return true;
   if (getVerificationCodeTarget()) return true;
   if (findResendVerificationCodeTrigger({ allowDisabled: true })) return true;
   if (findResendVerificationCodeTrigger({ allowDisabled: true })) return true;
   if (document.querySelector('form[action*="email-verification" i]')) return true;
   if (document.querySelector('form[action*="email-verification" i]')) return true;
 
 
+  if (!isEmailVerificationPage()) {
+    return false;
+  }
+
   return VERIFICATION_PAGE_PATTERN.test(getPageTextSnapshot());
   return VERIFICATION_PAGE_PATTERN.test(getPageTextSnapshot());
 }
 }
 
 
@@ -1314,10 +1321,6 @@ function inspectSignupVerificationState() {
     return { state: 'step5' };
     return { state: 'step5' };
   }
   }
 
 
-  if (isVerificationPageStillVisible()) {
-    return { state: 'verification' };
-  }
-
   if (isSignupPasswordErrorPage()) {
   if (isSignupPasswordErrorPage()) {
     const timeoutPage = getSignupPasswordTimeoutErrorPageState();
     const timeoutPage = getSignupPasswordTimeoutErrorPageState();
     return {
     return {
@@ -1326,6 +1329,10 @@ function inspectSignupVerificationState() {
     };
     };
   }
   }
 
 
+  if (isVerificationPageStillVisible()) {
+    return { state: 'verification' };
+  }
+
   if (isSignupEmailAlreadyExistsPage()) {
   if (isSignupEmailAlreadyExistsPage()) {
     return { state: 'email_exists' };
     return { state: 'email_exists' };
   }
   }

+ 142 - 0
tests/signup-verification-state-guard.test.js

@@ -0,0 +1,142 @@
+const test = require('node:test');
+const assert = require('node:assert/strict');
+const fs = require('node:fs');
+
+const source = fs.readFileSync('content/signup-page.js', 'utf8');
+
+function extractFunction(name) {
+  const markers = [`async function ${name}(`, `function ${name}(`];
+  const start = markers
+    .map((marker) => source.indexOf(marker))
+    .find((index) => index >= 0);
+  if (start < 0) {
+    throw new Error(`missing function ${name}`);
+  }
+
+  let parenDepth = 0;
+  let signatureEnded = false;
+  let braceStart = -1;
+  for (let i = start; i < source.length; i += 1) {
+    const ch = source[i];
+    if (ch === '(') {
+      parenDepth += 1;
+    } else if (ch === ')') {
+      parenDepth -= 1;
+      if (parenDepth === 0) {
+        signatureEnded = true;
+      }
+    } else if (ch === '{' && signatureEnded) {
+      braceStart = i;
+      break;
+    }
+  }
+
+  if (braceStart < 0) {
+    throw new Error(`missing body for function ${name}`);
+  }
+
+  let depth = 0;
+  let end = braceStart;
+  for (; end < source.length; end += 1) {
+    const ch = source[end];
+    if (ch === '{') depth += 1;
+    if (ch === '}') {
+      depth -= 1;
+      if (depth === 0) {
+        end += 1;
+        break;
+      }
+    }
+  }
+
+  return source.slice(start, end);
+}
+
+test('verification visibility text fallback should not treat password retry page as verification page', () => {
+  const api = new Function(`
+const VERIFICATION_PAGE_PATTERN = /check\\s+your\\s+inbox|we\\s+emailed|resend/i;
+const document = {
+  querySelector() {
+    return null;
+  },
+};
+
+function getCurrentAuthRetryPageState(flow) {
+  if (flow === 'signup_password') {
+    return { retryEnabled: true };
+  }
+  return null;
+}
+
+function getVerificationCodeTarget() {
+  return null;
+}
+
+function findResendVerificationCodeTrigger() {
+  return null;
+}
+
+function isEmailVerificationPage() {
+  return false;
+}
+
+function getPageTextSnapshot() {
+  return 'Check your inbox and resend email if needed';
+}
+
+${extractFunction('isVerificationPageStillVisible')}
+
+return {
+  run() {
+    return isVerificationPageStillVisible();
+  },
+};
+`)();
+
+  assert.equal(api.run(), false);
+});
+
+test('signup verification state should prioritize retry error page over verification visibility', () => {
+  const api = new Function(`
+function isStep5Ready() {
+  return false;
+}
+
+function isVerificationPageStillVisible() {
+  return true;
+}
+
+function isSignupPasswordErrorPage() {
+  return true;
+}
+
+function getSignupPasswordTimeoutErrorPageState() {
+  return { retryButton: { textContent: 'Try again' } };
+}
+
+function isSignupEmailAlreadyExistsPage() {
+  return false;
+}
+
+function getSignupPasswordInput() {
+  return null;
+}
+
+function getSignupPasswordSubmitButton() {
+  return null;
+}
+
+${extractFunction('inspectSignupVerificationState')}
+
+return {
+  run() {
+    return inspectSignupVerificationState();
+  },
+};
+`)();
+
+  assert.deepStrictEqual(api.run(), {
+    state: 'error',
+    retryButton: { textContent: 'Try again' },
+  });
+});