"""邮箱验证码渠道回退:默认通道失败后自动换另一条。 运营以前要在「登录设置 → 登录接口」里手工切 wgapi / iframe:某个渠道收不到 斗鱼的验证码邮件时,整批账号都会卡在同一个渠道上。现在的规则是:**先按人工 设置的默认通道登录**,只有它在取码这一步失败后才自动切到另一条再试一次。 """ 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_iframe_default_switches_to_wgapi_after_code_timeouts(monkeypatch): """默认通道是 iframe 时:先用 iframe,两轮收不到码后切 wgapi。""" 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) if len(calls) <= 2: raise EmailCodeTimeoutError("等待验证码超时") return "cookie" monkeypatch.setattr(client, "_run_login_steps", steps) _stub(monkeypatch, client) result = client.login() assert result.success is True assert client.api.name == "wgapi" assert calls == ["iframe", "iframe", "wgapi"] def test_wgapi_default_switches_to_iframe(monkeypatch): """默认通道是 wgapi 时:反向切到 iframe。""" 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) if len(calls) <= 2: raise EmailCodeTimeoutError("等待验证码超时") return "cookie" monkeypatch.setattr(client, "_run_login_steps", steps) _stub(monkeypatch, client) result = client.login() assert result.success is True assert client.api.name == "iframe" assert calls == ["wgapi", "wgapi", "iframe"] def test_fallback_only_switches_once(monkeypatch): """两条通道都收不到码时收口,不能在 wgapi/iframe 之间来回打转。""" monkeypatch.delenv("DOUYU_LOGIN_EMAIL_FALLBACK_API", raising=False) client = _login("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 == "wgapi" # iframe 两轮 + wgapi 两轮,之后直接判定失败 assert calls == ["iframe", "iframe", "wgapi", "wgapi"] 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 EmailCodeTimeoutError("等待验证码超时") monkeypatch.setattr(client, "_run_login_steps", steps) _stub(monkeypatch, client) result = client.login() assert result.success is False assert client.api.name == "iframe" assert calls == ["iframe", "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("发送验证邮件失败: 繁忙"))