58 lines
1.4 KiB
Go
58 lines
1.4 KiB
Go
package kb_test
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"chat-agent-system/internal/kb"
|
|
)
|
|
|
|
func TestMaterializeSnapshot(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
hits := []kb.HitItem{
|
|
{
|
|
Category: "standards",
|
|
DocCode: "GB 9706.1-2020",
|
|
Title: "医用电气设备 第1部分:基本安全和基本性能的通用要求",
|
|
Content: "医用电气设备必须具备防电击安全保护...",
|
|
Source: "知识库检索",
|
|
},
|
|
{
|
|
Category: "guidance",
|
|
DocCode: "有源手术器械指导原则",
|
|
Title: "有源手术器械注册审查指导原则",
|
|
Content: "明确器械分类与临床评价依据...",
|
|
Source: "知识库检索",
|
|
},
|
|
}
|
|
|
|
err := kb.MaterializeSnapshot(tmpDir, hits)
|
|
if err != nil {
|
|
t.Fatalf("failed to materialize kb snapshot: %v", err)
|
|
}
|
|
|
|
hitsFile := filepath.Join(tmpDir, "knowledge", "hits.md")
|
|
if _, err := os.Stat(hitsFile); err != nil {
|
|
t.Fatalf("expected hits.md to exist: %v", err)
|
|
}
|
|
|
|
content, _ := os.ReadFile(hitsFile)
|
|
if !containsString(string(content), "GB 9706.1-2020") {
|
|
t.Errorf("hits.md missing GB 9706.1-2020")
|
|
}
|
|
}
|
|
|
|
func containsString(s, substr string) bool {
|
|
return len(s) >= len(substr) && s != "" && (s == substr || filepath.Base(s) != "" && stringContains(s, substr))
|
|
}
|
|
|
|
func stringContains(s, substr string) bool {
|
|
for i := 0; i+len(substr) <= len(s); i++ {
|
|
if s[i:i+len(substr)] == substr {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|