Files
hyapi-server/internal/infrastructure/external/jiyi/empty_result_test.go

129 lines
3.4 KiB
Go
Raw Normal View History

2026-07-22 17:15:33 +08:00
package jiyi
2026-07-25 13:56:09 +08:00
import (
"encoding/json"
"testing"
)
2026-07-22 17:15:33 +08:00
func TestIsEmptyBusinessResult(t *testing.T) {
cases := []struct {
name string
data interface{}
empty bool
}{
{"nil_map", map[string]interface{}{}, true},
{"missing_result", map[string]interface{}{"foo": 1}, true},
{"nil_result", map[string]interface{}{"result": nil}, true},
{"empty_result_map", map[string]interface{}{"result": map[string]interface{}{}}, true},
{"black_tags_zero_not_empty", map[string]interface{}{
"result": map[string]interface{}{
"black_list": "0",
"black_tag04": "0",
},
}, false},
{"score_present", map[string]interface{}{
"result": map[string]interface{}{
"score": "617",
"reason": "",
"contents": map[string]interface{}{"TC_A001": "0"},
},
}, false},
{"all_blank_strings", map[string]interface{}{
"result": map[string]interface{}{
"score": "",
"reason": "",
},
}, true},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := isEmptyBusinessResult(tc.data)
if got != tc.empty {
t.Fatalf("got %v want %v", got, tc.empty)
}
})
}
}
2026-07-25 13:56:09 +08:00
func TestUnwrapResultPayload(t *testing.T) {
wuj := map[string]interface{}{
"result": map[string]interface{}{
"black_list": "1",
"black_tag04": "1",
},
}
got := unwrapResultPayload(wuj).(map[string]interface{})
if got["black_list"] != "1" || got["black_tag04"] != "1" {
t.Fatalf("wuj unwrap mismatch: %+v", got)
}
if _, ok := got["result"]; ok {
t.Fatal("should not keep result wrapper")
}
cave := map[string]interface{}{
"result": map[string]interface{}{
"score": "617",
"reason": "",
"contents": map[string]interface{}{
"TC_A001": "0",
},
},
}
got2 := unwrapResultPayload(cave).(map[string]interface{})
if got2["score"] != "617" {
t.Fatalf("cave unwrap mismatch: %+v", got2)
}
pano := map[string]interface{}{
"result": map[string]interface{}{
"apply_report_detail": map[string]interface{}{"A22160001": "543"},
"behavior_report_detail": nil,
"current_report_detail": nil,
},
}
got3 := unwrapResultPayload(pano).(map[string]interface{})
if _, ok := got3["apply_report_detail"]; !ok {
t.Fatalf("pano unwrap mismatch: %+v", got3)
}
plain := map[string]interface{}{"x": "1"}
if unwrapResultPayload(plain).(map[string]interface{})["x"] != "1" {
t.Fatal("plain payload should keep")
}
}
func TestStripAndUnwrapPipeline(t *testing.T) {
raw := []byte(`{"busiCode":10,"busiMsg":"查询成功","result":{"black_list":"1","black_tag07":"1"}}`)
var data interface{}
if err := json.Unmarshal(raw, &data); err != nil {
t.Fatal(err)
}
cleaned, err := stripBusiMetaFromData(data)
if err != nil {
t.Fatal(err)
}
out := unwrapResultPayload(cleaned).(map[string]interface{})
if _, ok := out["busiCode"]; ok {
t.Fatal("busiCode should be stripped")
}
if _, ok := out["result"]; ok {
t.Fatal("result wrapper should be unwrapped")
}
if out["black_list"] != "1" || out["black_tag07"] != "1" {
t.Fatalf("unexpected: %+v", out)
}
}
2026-07-22 17:15:33 +08:00
func TestIsQueryBillingAPIKey(t *testing.T) {
if !isQueryBillingAPIKey("jy000022") || !isQueryBillingAPIKey("jy000042") || !isQueryBillingAPIKey("jy000052") {
t.Fatal("查空计费产品应在白名单中")
}
if isQueryBillingAPIKey("jy000004") {
t.Fatal("信用司南查空不计费,不应在白名单中")
}
2026-07-22 17:40:57 +08:00
if isQueryBillingAPIKey("jy000002") || isQueryBillingAPIKey("jy000003") {
2026-07-25 13:56:09 +08:00
t.Fatal("全景雷达BH查空不计费不应在白名单中")
2026-07-22 17:40:57 +08:00
}
2026-07-22 17:15:33 +08:00
}