feat: 分析响应JSON解析

This commit is contained in:
2026-09-01 10:27:11 +08:00
parent a356803809
commit a454826433
2 changed files with 71 additions and 0 deletions
+43
View File
@@ -0,0 +1,43 @@
package service
import "testing"
func TestParseAnalysisResponse(t *testing.T) {
cases := []struct {
name string
in string
wantRetry bool
wantErr bool
}{
{"纯JSON", `{"retryable": true, "reason": "限流"}`, true, false},
{"带json代码块", "```json\n{\"retryable\": false, \"reason\": \"参数错误\"}\n```", false, false},
{"带前后文字", `分析结果: {"retryable": true, "reason": "瞬时故障"} 完毕`, true, false},
{"非法响应", `抱歉,我无法分析`, false, true},
{"空串", ``, false, true},
}
for _, c := range cases {
retry, _, err := parseAnalysisResponse(c.in)
if c.wantErr {
if err == nil {
t.Fatalf("[%s] 期望错误, got retry=%v", c.name, retry)
}
continue
}
if err != nil {
t.Fatalf("[%s] 不应报错: %v", c.name, err)
}
if retry != c.wantRetry {
t.Fatalf("[%s] retryable = %v, want %v", c.name, retry, c.wantRetry)
}
}
}
func TestParseAnalysisResponseReason(t *testing.T) {
_, reason, err := parseAnalysisResponse(`{"retryable": true, "reason": "服务过载"}`)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
if reason != "服务过载" {
t.Fatalf("reason = %q, want 服务过载", reason)
}
}