from __future__ import annotations import pytest from yuxi.channels.adapters.wechat.errors import ( WECOM_ERROR_CODES, MP_ERROR_CODES, is_token_expired, parse_mp_error, parse_wecom_error, ) class TestWeComErrorParsing: def test_parse_wecom_success(self): errcode, detail = parse_wecom_error({"errcode": 0, "errmsg": "ok"}) assert errcode == 0 assert "成功" in detail def test_parse_wecom_invalid_access_token(self): errcode, detail = parse_wecom_error({"errcode": 40014, "errmsg": "invalid access_token"}) assert errcode == 40014 assert "access_token" in detail def test_parse_wecom_rate_limit(self): errcode, detail = parse_wecom_error({"errcode": 45009, "errmsg": "api freq limit"}) assert errcode == 45009 assert "超过限制" in detail def test_parse_wecom_unknown_error(self): errcode, detail = parse_wecom_error({"errcode": 99999, "errmsg": "custom error"}) assert errcode == 99999 assert "custom error" in detail def test_parse_wecom_no_errmsg(self): errcode, detail = parse_wecom_error({"errcode": 40003}) assert errcode == 40003 assert "UserID" in detail def test_parse_wecom_missing_errcode(self): errcode, detail = parse_wecom_error({}) assert errcode == -1 assert "未知错误" in detail class TestMPErrorParsing: def test_parse_mp_success(self): errcode, detail = parse_mp_error({"errcode": 0, "errmsg": "ok"}) assert errcode == 0 assert "成功" in detail def test_parse_mp_expired_token(self): errcode, detail = parse_mp_error({"errcode": 42001, "errmsg": "access_token expired"}) assert errcode == 42001 assert "超时" in detail def test_parse_mp_require_subscribe(self): errcode, detail = parse_mp_error({"errcode": 43004, "errmsg": "require subscribe"}) assert errcode == 43004 assert "需要接收者关注" in detail def test_parse_mp_invalid_openid(self): errcode, detail = parse_mp_error({"errcode": 40003, "errmsg": "invalid openid"}) assert errcode == 40003 assert "OpenID" in detail class TestIsTokenExpired: def test_40014_is_expired(self): assert is_token_expired(40014) is True def test_42001_is_expired(self): assert is_token_expired(42001) is True def test_zero_not_expired(self): assert is_token_expired(0) is False def test_unknown_not_expired(self): assert is_token_expired(99999) is False def test_45009_not_expired(self): assert is_token_expired(45009) is False class TestErrorCodeMaps: def test_wecom_map_has_common_codes(self): assert 40014 in WECOM_ERROR_CODES assert 42001 in WECOM_ERROR_CODES assert 45009 in WECOM_ERROR_CODES def test_mp_map_has_common_codes(self): assert 40014 in MP_ERROR_CODES assert 42001 in MP_ERROR_CODES assert 45009 in MP_ERROR_CODES assert 45015 in MP_ERROR_CODES