package xerr

import "testing"

// TestMapErrMsg_MissingCodesAreFilled 覆盖 HIF-138 中补齐的 5 个错误码：
// 这些码之前在 message 表中缺失，导致 MapErrMsg 回退到 "Internal Server Error"。
func TestMapErrMsg_MissingCodesAreFilled(t *testing.T) {
	cases := []struct {
		name string
		code uint32
		want string
	}{
		{"UserCommissionNotEnough", UserCommissionNotEnough, "佣金余额不足"},
		{"SendSmsError", SendSmsError, "短信发送失败"},
		{"AreaCodeIsEmpty", AreaCodeIsEmpty, "国家区号不能为空"},
		{"DeviceBindLimitExceeded", DeviceBindLimitExceeded, "设备绑定数量已达上限"},
		{"ExistAvailableTraffic", ExistAvailableTraffic, "存在可用流量"},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			got := MapErrMsg(tc.code)
			if got == "Internal Server Error" {
				t.Fatalf("MapErrMsg(%d) fell back to Internal Server Error; expected %q", tc.code, tc.want)
			}
			if got != tc.want {
				t.Errorf("MapErrMsg(%d) = %q, want %q", tc.code, got, tc.want)
			}
			if !IsCodeErr(tc.code) {
				t.Errorf("IsCodeErr(%d) = false, want true", tc.code)
			}
		})
	}
}

// TestMapErrMsg_UnknownCodeFallsBack 验证未定义码仍然安全回退，不 panic。
func TestMapErrMsg_UnknownCodeFallsBack(t *testing.T) {
	const unknown uint32 = 99999
	if got := MapErrMsg(unknown); got != "Internal Server Error" {
		t.Errorf("MapErrMsg(%d) = %q, want %q", unknown, got, "Internal Server Error")
	}
	if IsCodeErr(unknown) {
		t.Errorf("IsCodeErr(%d) = true, want false", unknown)
	}
}
