Files
Agent-System/internal/kb/integration_test.go
T

93 lines
2.5 KiB
Go

package kb_test
import (
"context"
"os"
"strings"
"testing"
"time"
"chat-agent-system/internal/config"
"chat-agent-system/internal/kb"
)
func TestKB_RealIntegration(t *testing.T) {
if testing.Short() {
t.Skip("skipping KB real integration in short mode")
}
if os.Getenv("RUN_KB_INTEGRATION") != "1" {
t.Skip("set RUN_KB_INTEGRATION=1 to run KB real integration")
}
cfg, err := config.Load("")
if err != nil {
t.Fatalf("failed to load config: %v", err)
}
// 强制使用本机开发环境共享知识库容器端口
if cfg.KbDbPort == 0 {
cfg.KbDbPort = 5432
}
client := kb.NewClient(cfg)
defer client.Close()
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
// 1. 验证多领域资产规模统计
t.Run("Stats", func(t *testing.T) {
stats, err := client.Stats(ctx)
if err != nil {
t.Fatalf("client.Stats failed: %v", err)
}
regStats, ok := stats[kb.DomainRegulatory]
if !ok {
t.Fatalf("expected regulatory domain in stats")
}
if regStats.DocumentCount != 2907 {
t.Errorf("expected 2907 documents, got %d", regStats.DocumentCount)
}
if regStats.ChunkCount < 312900 {
t.Errorf("expected >= 312900 chunks, got %d", regStats.ChunkCount)
}
t.Logf("Stats verified: %d docs, %d chunks", regStats.DocumentCount, regStats.ChunkCount)
})
// 2. 验证关键法规标准精确与模糊检索 (GB 9706.1 医用电气设备安全通用要求)
t.Run("Search_GB9706", func(t *testing.T) {
hits, err := client.Search(ctx, "GB 9706.1 医用电气设备 基本安全", kb.SearchOptions{
Limit: 3,
CandidateLimit: 10,
})
if err != nil {
t.Fatalf("client.Search failed: %v", err)
}
if len(hits) == 0 {
t.Fatalf("expected hits for GB 9706.1, got 0")
}
found := false
for _, h := range hits {
t.Logf("Hit: [%s] %s (Cite: %s, Score: %f)", h.DocCode, h.Title, h.CiteKey, h.Score)
if strings.Contains(h.DocCode, "9706") || strings.Contains(h.Title, "9706") || strings.Contains(h.Title, "电气安全") {
found = true
}
}
if !found {
t.Errorf("expected at least one hit containing '9706' or '电气安全'")
}
})
// 3. 验证引用规范精准定位 (LookupCite)
t.Run("LookupCite", func(t *testing.T) {
line, ok := client.LookupCite(ctx, "GB 9706.1-2020 §8.4")
if !ok {
t.Fatalf("LookupCite failed to find GB 9706.1-2020 §8.4")
}
t.Logf("LookupCite result: %s", line)
if !strings.Contains(line, "9706.1") {
t.Errorf("expected line to contain 9706.1, got: %s", line)
}
})
}