44 lines
1.5 KiB
Go
44 lines
1.5 KiB
Go
package service
|
|
|
|
import "testing"
|
|
|
|
func TestNormalizeErrorMsg(t *testing.T) {
|
|
cases := []struct{ in, want string }{
|
|
{"rate limit exceeded for req-abc123", "rate limit exceeded for {reqid}"},
|
|
{"timeout at 2026-09-01T09:00:00Z req_88f1a2", "timeout at {time} {reqid}"},
|
|
{"uuid 0f8fad5b-d9cb-469f-a165-70867728950e remains", "uuid {uuid} remains"},
|
|
{"err code 12345678 quota exceeded", "err code {num} quota exceeded"},
|
|
{"clean message unchanged", "clean message unchanged"},
|
|
}
|
|
for _, c := range cases {
|
|
if got := normalizeErrorMsg(c.in); got != c.want {
|
|
t.Fatalf("normalizeErrorMsg(%q) = %q, want %q", c.in, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBuildMemoryKey(t *testing.T) {
|
|
k1 := buildMemoryKey("https://a.com", "429", "rate limit for req-a1b2c3")
|
|
k2 := buildMemoryKey("https://a.com", "429", "rate limit for req-d4e5f6") // 同因不同请求ID
|
|
if k1 != k2 {
|
|
t.Fatalf("同因不同请求ID应同键: %s != %s", k1, k2)
|
|
}
|
|
k3 := buildMemoryKey("https://b.com", "429", "rate limit for req-a1b2c3") // 不同上游
|
|
if k1 == k3 {
|
|
t.Fatalf("不同上游应不同键")
|
|
}
|
|
k4 := buildMemoryKey("https://a.com", "500", "rate limit for req-a1b2c3") // 不同错误码
|
|
if k1 == k4 {
|
|
t.Fatalf("不同错误码应不同键")
|
|
}
|
|
if len(k1) != 64 {
|
|
t.Fatalf("memory_key 应为 SHA-256 十六进制64位, got %d", len(k1))
|
|
}
|
|
}
|
|
|
|
func TestMsgFingerprint(t *testing.T) {
|
|
if msgFingerprint("a req-a1b2c3") != msgFingerprint("a req-d4e5f6") {
|
|
t.Fatalf("同因指纹应一致")
|
|
}
|
|
}
|