diff --git a/platforms/chatgpt/plugin.py b/platforms/chatgpt/plugin.py index 3faa81c..cfe5528 100644 --- a/platforms/chatgpt/plugin.py +++ b/platforms/chatgpt/plugin.py @@ -68,11 +68,17 @@ class ChatGPTPlatform(BasePlatform): def __init__(self): self._acct = None self._email = _fixed_email + self._before_ids = set() def create_email(self, config=None): if self._email and self._acct and _fixed_email: return {"email": self._email, "service_id": self._acct.account_id, "token": ""} self._acct = _mailbox.get_email() + get_current_ids = getattr(_mailbox, "get_current_ids", None) + if callable(get_current_ids): + self._before_ids = set(get_current_ids(self._acct) or []) + else: + self._before_ids = set() generated_email = getattr(self._acct, "email", "") if not self._email: self._email = _resolve_email(generated_email) @@ -95,6 +101,7 @@ class ChatGPTPlatform(BasePlatform): self._acct, keyword="", timeout=timeout, + before_ids=self._before_ids, otp_sent_at=otp_sent_at, exclude_codes=exclude_codes, ) @@ -116,9 +123,14 @@ class ChatGPTPlatform(BasePlatform): class TempMailEmailService: service_type = type("ST", (), {"value": "tempmail_lol"})() + def __init__(self): + self._acct = None + self._before_ids = set() + def create_email(self, config=None): acct = _tmail.get_email() self._acct = acct + self._before_ids = set(_tmail.get_current_ids(acct) or []) resolved_email = str(getattr(acct, "email", "") or "").strip() if not resolved_email: raise RuntimeError("tempmail_lol 返回空邮箱地址") @@ -137,6 +149,7 @@ class ChatGPTPlatform(BasePlatform): self._acct, keyword="", timeout=timeout, + before_ids=self._before_ids, otp_sent_at=otp_sent_at, exclude_codes=exclude_codes, ) diff --git a/tests/test_chatgpt_plugin.py b/tests/test_chatgpt_plugin.py index 9d878b2..2ffff58 100644 --- a/tests/test_chatgpt_plugin.py +++ b/tests/test_chatgpt_plugin.py @@ -14,12 +14,49 @@ class _BlankMailbox: return "123456" +class _TrackingMailbox: + def __init__(self): + self.account = MailboxAccount(email="demo@example.com", account_id="tracked-mailbox") + self.wait_call = None + self.current_ids_calls = [] + + def get_email(self): + return self.account + + def get_current_ids(self, account): + self.current_ids_calls.append(account) + return {"mid-1"} + + def wait_for_code(self, *args, **kwargs): + self.wait_call = (args, kwargs) + return "123456" + + class _FakeAdapter: def run(self, context): context.email_service.create_email() raise AssertionError("create_email 应该先报错") +class _VerificationAdapter: + def __init__(self): + self.run_called = False + + def run(self, context): + self.run_called = True + context.email_service.create_email() + code = context.email_service.get_verification_code( + timeout=30, + otp_sent_at=123.0, + exclude_codes={"654321"}, + ) + self.last_code = code + return mock.Mock(success=True) + + def build_account(self, result, fallback_password): + return {"success": True, "password": fallback_password} + + class ChatGPTPluginTests(unittest.TestCase): def test_custom_provider_rejects_blank_email(self): platform = ChatGPTPlatform( @@ -36,6 +73,30 @@ class ChatGPTPluginTests(unittest.TestCase): self.assertIn("custom_provider 返回空邮箱地址", str(ctx.exception)) + def test_custom_provider_uses_mailbox_baseline_for_verification_code(self): + mailbox = _TrackingMailbox() + platform = ChatGPTPlatform( + config=RegisterConfig(extra={"chatgpt_registration_mode": "refresh_token"}), + mailbox=mailbox, + ) + adapter = _VerificationAdapter() + + with mock.patch( + "platforms.chatgpt.plugin.build_chatgpt_registration_mode_adapter", + return_value=adapter, + ): + result = platform.register() + + self.assertTrue(adapter.run_called) + self.assertEqual(adapter.last_code, "123456") + self.assertEqual(result["success"], True) + self.assertEqual(mailbox.current_ids_calls, [mailbox.account]) + self.assertIsNotNone(mailbox.wait_call) + _, kwargs = mailbox.wait_call + self.assertEqual(kwargs.get("before_ids"), {"mid-1"}) + self.assertEqual(kwargs.get("otp_sent_at"), 123.0) + self.assertEqual(kwargs.get("exclude_codes"), {"654321"}) + if __name__ == "__main__": unittest.main()