Files
tyapi-server/internal/infrastructure/external/yuyuecha/yuyuecha_service_test.go
2026-07-24 23:27:17 +08:00

96 lines
2.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package yuyuecha
import (
"context"
"strings"
"testing"
)
func TestBuildOpenAPIPath(t *testing.T) {
tests := []struct {
in string
want string
}{
{"a01/person-company-relations", "/openapi/v1/a01/person-company-relations"},
{"/a01/person-company-relations", "/openapi/v1/a01/person-company-relations"},
{"openapi/v1/a01/person-company-relations", "/openapi/v1/a01/person-company-relations"},
}
for _, tt := range tests {
if got := buildOpenAPIPath(tt.in); got != tt.want {
t.Fatalf("buildOpenAPIPath(%q)=%q, want %q", tt.in, got, tt.want)
}
}
}
func TestHashIDCard(t *testing.T) {
got := HashIDCard("450322197603171539")
if len(got) != 32 {
t.Fatalf("md5 digest length=%d, want 32", len(got))
}
if got != strings.ToLower(got) {
t.Fatalf("digest should be lowercase: %s", got)
}
if pass := HashIDCard(got); pass != got {
t.Fatalf("already-hashed pass-through: got %q want %q", pass, got)
}
if pass := HashIDCard(strings.ToUpper(got)); pass != got {
t.Fatalf("uppercase digest normalize: got %q want %q", pass, got)
}
}
func sandboxService() *YuyuechaService {
return NewYuyuechaService(ServiceConfig{
BaseURL: "https://sandbox-api.yuyuecha.com",
ClientID: "sandbox-haiyu-20260724",
ClientSecret: "HuidklNz6RnSfCjn6MIcUW_Rj8trLljdN4v6FUs3NsY",
Timeout: defaultRequestTimeout,
}, nil)
}
func TestSandboxPersonCompanyRelations(t *testing.T) {
svc := sandboxService()
result, err := svc.CallAPI(context.Background(), "a01/person-company-relations", map[string]interface{}{
"name": "骆炳荣",
"idCard": HashIDCard("450322197603171539"),
})
if err != nil {
t.Fatalf("CallAPI failed: %v", err)
}
if result == nil || len(result.Data) == 0 {
t.Fatal("expected non-empty data")
}
// 沙箱不计费
if result.Billable {
t.Fatalf("sandbox should not be billable, got chargeFen=%d", result.ChargeFen)
}
t.Logf("requestId=%s sandbox=%v billable=%v data=%s", result.RequestID, result.Sandbox, result.Billable, string(result.Data))
}
// TestSandboxGetAccount 查询账户汇总GET请求体为空
func TestSandboxGetAccount(t *testing.T) {
svc := sandboxService()
info, err := svc.GetAccount(context.Background())
if err != nil {
t.Fatalf("GetAccount failed: %v", err)
}
if info == nil {
t.Fatal("expected account info")
}
if info.ClientID != "sandbox-haiyu-20260724" {
t.Fatalf("clientId=%q", info.ClientID)
}
if info.Mode != "sandbox" {
t.Fatalf("mode=%q", info.Mode)
}
if !info.Enabled {
t.Fatal("expected enabled=true")
}
if info.TotalLimit <= 0 {
t.Fatalf("totalLimit=%d", info.TotalLimit)
}
t.Logf("account: clientId=%s mode=%s totalLimit=%d trialRemaining=%d trialEndsAt=%s totalCalls=%d",
info.ClientID, info.Mode, info.TotalLimit, info.TrialRemaining, info.TrialEndsAt, info.TotalCalls)
}