"""邮箱验证码通道切换:只有接口级错误才换通道。 运营以前要在「登录设置 → 登录接口」里手工切 wgapi / iframe。现在的规则: 1. **发信/提交验证码接口本身报错** → 换另一条通道再试一次; 2. **取码等超时** → 不换通道、不重新发信:斗鱼验证码是攒批投递的,迟到的 邮件还在路上,重新发信只会把队列压更长(生产实测每账号发信量 1.5 → 3.3 封,延迟从 10 秒级恶化到 8~13 分钟)。改由补等迟到邮件 + 批次级退避重试。 """ from __future__ import annotations from types import SimpleNamespace from core.douyu import create_login_api_strategy from core.douyu.login import DouyuLogin, EmailCodeTimeoutError def _login(strategy: str | None = None, max_retries: int = 20) -> DouyuLogin: account = SimpleNamespace( username="u", password="p", email="e@example.com", email_password="ep", email_imap_server="imap.example.com", email_imap_port=993, email_imap_ssl=True, ) return DouyuLogin( account=account, proxy="http://127.0.0.1:1", max_login_retries=max_retries, api_strategy=create_login_api_strategy(strategy) if strategy else None, ) def _stub(monkeypatch, client: DouyuLogin) -> None: """跳过真实代理切换与等待,只保留渠道切换逻辑。""" monkeypatch.setattr(client, "_prepare_retry", lambda: True) monkeypatch.setattr(client, "_sleep_interruptible", lambda _s: None) def test_code_timeout_does_not_switch_channel(monkeypatch): """取码超时不换通道:换通道会重新发信,把斗鱼邮件队列压得更长。""" monkeypatch.delenv("DOUYU_LOGIN_EMAIL_FALLBACK_API", raising=False) client = _login("iframe") assert client.api.name == "iframe" calls: list[str] = [] def steps(deadline: float = 0) -> str: calls.append(client.api.name) raise EmailCodeTimeoutError("等待验证码超时") monkeypatch.setattr(client, "_run_login_steps", steps) _stub(monkeypatch, client) result = client.login() assert result.success is False assert result.code == "email_code_timeout" assert client.api.name == "iframe" # 只跑一轮:不再靠「整轮重试」去重发邮件 assert calls == ["iframe"] def test_code_timeout_does_not_resend_email(monkeypatch): """wgapi 默认通道下,取码超时同样只跑一轮(只发一封信)。""" monkeypatch.delenv("DOUYU_LOGIN_EMAIL_FALLBACK_API", raising=False) client = _login() assert client.api.name == "wgapi" calls: list[str] = [] def steps(deadline: float = 0) -> str: calls.append(client.api.name) raise EmailCodeTimeoutError("等待验证码超时") monkeypatch.setattr(client, "_run_login_steps", steps) _stub(monkeypatch, client) result = client.login() assert result.success is False assert result.code == "email_code_timeout" assert client.api.name == "wgapi" assert calls == ["wgapi"] def test_late_mail_is_rescued_without_resending(monkeypatch): """首轮等超时后补等一轮,拿到迟到验证码;全程只发一次信。""" monkeypatch.delenv("DOUYU_LOGIN_EMAIL_FALLBACK_API", raising=False) client = _login("wgapi") waits: list[int] = [] class _LateVerifier: def get_verification_code(self, max_wait: int, **kwargs): waits.append(max_wait) if len(waits) == 1: raise TimeoutError("等待验证码超时") return "123456" code = client._wait_for_email_code( _LateVerifier(), # type: ignore[arg-type] baseline_uid=7, email_sent_at=0.0, ) assert code == "123456" # 首轮按配置窗口等,超时后补等一轮;两次等待都不是「重新发信」 assert len(waits) == 2 assert waits[0] == DouyuLogin._email_code_wait_seconds() assert waits[1] == DouyuLogin._email_code_rescue_seconds() def test_fallback_can_be_disabled_with_env(monkeypatch): """关闭自动切换后,接口级错误也不再换通道。""" monkeypatch.setenv("DOUYU_LOGIN_EMAIL_FALLBACK_API", "off") client = _login("iframe") calls: list[str] = [] def steps(deadline: float = 0) -> str: calls.append(client.api.name) raise ValueError("发送验证邮件失败: 服务繁忙") monkeypatch.setattr(client, "_run_login_steps", steps) _stub(monkeypatch, client) result = client.login() assert result.success is False assert client.api.name == "iframe" # 一直在 iframe 上重试,从没切到 wgapi assert set(calls) == {"iframe"} def test_send_email_failure_also_switches_channel(monkeypatch): """发信接口本身报错也是「这条通道不通」,同样换一条再试。""" monkeypatch.delenv("DOUYU_LOGIN_EMAIL_FALLBACK_API", raising=False) client = _login("wgapi") calls: list[str] = [] def steps(deadline: float = 0) -> str: calls.append(client.api.name) if client.api.name == "wgapi": raise ValueError("发送验证邮件失败: 服务繁忙") return "cookie" monkeypatch.setattr(client, "_run_login_steps", steps) _stub(monkeypatch, client) result = client.login() assert result.success is True assert calls == ["wgapi", "iframe"] def test_channel_error_classifier_ignores_geetest_failures(): """极验失败与收码通道无关:换通道只会白烧一次代理。""" assert not DouyuLogin._is_email_channel_error( ValueError("第二次登录失败: 验证码错误") ) assert not DouyuLogin._is_email_channel_error(ValueError("极验验证失败: slide")) assert DouyuLogin._is_email_channel_error(ValueError("提交验证码失败: 验证码错误")) assert DouyuLogin._is_email_channel_error(ValueError("发送验证邮件失败: 繁忙"))