Files
ai-customer-service/internal/service/intent/service_test.go
Your Name 82a9306819 test(P1): 补齐 domain/intent、domain/message、domain/ticketstats、platform/logging、service/intent、config 测试
**新增测试文件**:
- internal/domain/intent/intent_test.go: 6 个测试(Recognize 各意图分支、containsAny)
- internal/domain/message/message_test.go: 4 个测试(UnifiedMessage 各字段)
- internal/domain/ticketstats/stats_test.go: 5 个测试(Stats 各字段、零值、nil map)
- internal/platform/logging/logger_test.go: 6 个测试(NewLogger 各日志级别)
- internal/service/intent/service_test.go: 6 个新增测试(通用意图、大小写、空格、containsAny)

**增强测试文件**:
- internal/config/config_test.go: +11 个测试(getEnvBool 全部分支、getEnvInt 无效值、getEnvInt64)
- internal/app/app_test.go: +3 个测试(Shutdown 关闭器顺序、nil 分支)

**覆盖率提升**:
- internal/service/intent: 80.8% → **100.0%** 
- internal/platform/logging: 0% → **100.0%** 
- internal/config: 70.6% → **82.4%** (+11.8%)
- 整体覆盖率: 76.6% → **77.1%** (+0.5%)

Ref: test/PHASE2_TEST_PLAN.md P1-1, P1-2
2026-05-01 11:43:05 +08:00

95 lines
2.7 KiB
Go

package intent
import (
"context"
"testing"
)
func TestRecognize(t *testing.T) {
tests := []struct {
name string
input string
intent string
handoff bool
}{
{name: "quota", input: "我的配额还剩多少", intent: "quota", handoff: false},
{name: "token", input: "今天 token 消耗是多少", intent: "token", handoff: false},
{name: "refund", input: "我要申请退款", intent: "refund", handoff: true},
{name: "security", input: "我的数据可能泄露了", intent: "security", handoff: true},
{name: "general_default", input: "你好", intent: "general", handoff: false},
{name: "error_keyword", input: "系统报错啦", intent: "error", handoff: false},
{name: "handoff_human", input: "转人工客服", intent: "handoff", handoff: true},
{name: "security_attack", input: "遭到攻击了", intent: "security", handoff: true},
}
svc := NewService()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := svc.Recognize(context.Background(), "s1", tt.input, nil)
if err != nil {
t.Fatalf("Recognize() error = %v", err)
}
if got.Intent != tt.intent {
t.Fatalf("intent = %s, want %s", got.Intent, tt.intent)
}
if got.NeedsHuman != tt.handoff {
t.Fatalf("NeedsHuman = %v, want %v", got.NeedsHuman, tt.handoff)
}
})
}
}
func TestRecognize_GeneralIntent(t *testing.T) {
svc := NewService()
got, err := svc.Recognize(context.Background(), "s1", "今天天气不错", nil)
if err != nil {
t.Fatalf("Recognize() error = %v", err)
}
if got.Intent != "general" {
t.Errorf("intent = %s, want general", got.Intent)
}
if got.Confidence != 0.65 {
t.Errorf("Confidence = %f, want 0.65", got.Confidence)
}
}
func TestRecognize_CaseInsensitive(t *testing.T) {
svc := NewService()
got, err := svc.Recognize(context.Background(), "s1", "REFUND", nil)
if err != nil {
t.Fatalf("Recognize() error = %v", err)
}
if got.Intent != "refund" {
t.Errorf("intent = %s, want refund (case insensitive)", got.Intent)
}
}
func TestRecognize_WhitespaceTrimmed(t *testing.T) {
svc := NewService()
got, err := svc.Recognize(context.Background(), "s1", " 退款 ", nil)
if err != nil {
t.Fatalf("Recognize() error = %v", err)
}
if got.Intent != "refund" {
t.Errorf("intent = %s, want refund (whitespace trimmed)", got.Intent)
}
}
func TestContainsAny_Found(t *testing.T) {
if !containsAny("hello world", "world") {
t.Error("containsAny(hello world, world) = false, want true")
}
}
func TestContainsAny_NotFound(t *testing.T) {
if containsAny("hello world", "foo") {
t.Error("containsAny(hello world, foo) = true, want false")
}
}
func TestContainsAny_EmptyTerms(t *testing.T) {
if containsAny("hello", "world") {
t.Error("containsAny(hello, world) = true, want false")
}
}