package errors import ( "net/http" "testing" ) // TestCodeFormat 验收项:全部预定义错误码符合 ISC:xxx 格式。 func TestCodeFormat(t *testing.T) { for _, e := range All() { if !IsValidCode(e.Code) { t.Errorf("错误码格式非法: %q", e.Code) } if e.Msg == "" || e.MsgEn == "" || e.HTTPStatus == 0 { t.Errorf("错误 %s 缺少描述或状态码", e.Code) } } } // TestBlacklist 验收锚点:黑名单错误码定义。 func TestBlacklist(t *testing.T) { if Blacklist.Code != "ISC:006" || Blacklist.Msg != "黑名单" { t.Fatalf("Blacklist = %+v, 期望 ISC:006 黑名单", Blacklist) } if Blacklist.HTTPStatus != http.StatusForbidden { t.Errorf("Blacklist.HTTPStatus = %d, 期望 403", Blacklist.HTTPStatus) } } // TestByCode 覆盖查询与未知码回退。 func TestByCode(t *testing.T) { if got := ByCode("ISC:006"); got != Blacklist { t.Errorf("ByCode(ISC:006) = %v", got) } if got := ByCode("ISC:999"); got != InternalError { t.Errorf("未知码应回退 InternalError, 实际 %v", got) } } // TestWithMsg 派生错误保留错误码。 func TestWithMsg(t *testing.T) { e := Blacklist.WithMsg("号码 138**** 已加入黑名单") if e.Code != Blacklist.Code || e.Msg == Blacklist.Msg { t.Errorf("WithMsg 派生异常: %+v", e) } if e.Error() == "" { t.Error("Error() 不应为空") } } // TestIsValidCode 覆盖非法格式。 func TestIsValidCode(t *testing.T) { for _, bad := range []string{"UT:0006", "ISC:00", "ISC:0006", "isc:006", "", "ISC:0a6"} { if IsValidCode(bad) { t.Errorf("%q 应为非法格式", bad) } } for _, ok := range []string{"ISC:000", "ISC:500"} { if !IsValidCode(ok) { t.Errorf("%q 应为合法格式", ok) } } } // TestAuthoritativeCatalog 权威码表锚点:产品侧清单关键码逐条核对。 func TestAuthoritativeCatalog(t *testing.T) { want := map[string]string{ "ISC:001": "网关错误", "ISC:004": "签名非法", "ISC:007": "欠费", "ISC:009": "短信网关内部错误", "ISC:013": "没有路由,携号转网", "ISC:018": "用户未开通5G消息", "ISC:025": "找不到归属运营商,可能是新号段", "ISC:029": "无通道路由", "ISC:031": "内容不合规拦截", "ISC:035": "超出账号授信额限制", "ISC:040": "夜间频控", "ISC:043": "5G消息拦截", "ISC:500": "其他错误,需要人工核查", } for code, msg := range want { if got := ByCode(code); got.Code != code || got.Msg != msg { t.Errorf("ByCode(%s) = {%s %s}, 期望 {%s %s}", code, got.Code, got.Msg, code, msg) } } // 全量 51 条 = 成功码 ISC:000 + 错误码 50 条(权威清单;Day42 +ISC:036;Day43 +037/038)。 if n := len(All()); n != 51 { t.Errorf("预定义码数量 = %d, 期望 51(含成功码)", n) } }