Files
HomeAgent/internal/agent/core/agent_functions_test.go
root 8ae1d19869 refactor: output channel interface (payload/meta/type) + memory fixes
- Redesign output_send__ tools: content JSON string -> structured
  payload/meta/type params for LLM reliability
- executeOutputSendTool: route by type with capability check
- executeOutputSendHelp: show meta format + type enum
- Updated system prompt rules for new interface
- docToTriples: use jieba exact mode adjacent co-occurrence
- Unify vector space: Doc.Vector field, ContextToDoc vectorizer,
  ReindexWithVectorizer on startup
2026-07-19 10:29:21 +08:00

115 lines
2.8 KiB
Go

package core
import (
"testing"
"gitcode.com/JianFeeeee/HomeAgent/internal/memory/document"
)
func TestEntitySimilarity(t *testing.T) {
tests := []struct {
a, b string
want float64
}{
{"", "", 0}, // empty → 0
{"a", "b", 0}, // single char → 0
{"张三", "张三", 1.0}, // identical → 1.0
{"张三", "李四", 0}, // no common bigrams
{"iPhone", "iPhone 15", 0.625}, // partial overlap
}
for _, tt := range tests {
got := entitySimilarity(tt.a, tt.b)
if got != tt.want {
t.Errorf("entitySimilarity(%q, %q) = %.3f, want %.3f", tt.a, tt.b, got, tt.want)
}
}
}
func TestDocToTriples(t *testing.T) {
doc := &document.Doc{
Summary: "用户喜欢编程",
Content: "用户提到喜欢Go和Python",
Source: "context",
}
triples := docToTriples(doc)
foundSummary := false
foundRel := false
foundSource := false
for _, tr := range triples {
switch {
case tr.Subject == "文档" && tr.Relation == "主题":
foundSummary = true
case tr.Relation == "关联":
foundRel = true
case tr.Subject == "文档" && tr.Relation == "来源":
foundSource = true
}
}
if !foundSummary {
t.Error("missing '主题' triple")
}
if !foundSource {
t.Error("missing '来源' triple")
}
if needJieba() && !foundRel {
t.Error("missing '关联' triple with jieba available")
}
}
func TestDocToTriplesNil(t *testing.T) {
triples := docToTriples(nil)
if len(triples) != 0 {
t.Errorf("expected empty for nil doc, got %d", len(triples))
}
}
func TestDocToTriplesNoSource(t *testing.T) {
doc := &document.Doc{
Summary: "无来源文档",
Content: "content",
}
triples := docToTriples(doc)
for _, tr := range triples {
if tr.Relation == "来源" {
t.Error("should not have source triple when Source is empty")
}
}
}
func TestDocToTriplesTypes(t *testing.T) {
doc := &document.Doc{
Summary: "测试三元组类型",
Content: "用于验证 SubjectType 和 ObjectType",
Source: "test",
}
triples := docToTriples(doc)
// 主题 and 来源 triples have Subject=文档
for _, tr := range triples {
if tr.Subject == "文档" {
if tr.SubjectType != "Concept" {
t.Errorf("文档 subject_type should be Concept, got %q", tr.SubjectType)
}
if tr.Confidence != 1.0 {
t.Errorf("文档 triple confidence should be 1.0, got %f", tr.Confidence)
}
} else {
// 关联 triples use extracted terms as subject/object
if tr.Relation != "关联" {
t.Errorf("non-文档 triple should have 关联 relation, got %q", tr.Relation)
}
if tr.Confidence != 0.8 {
t.Errorf("关联 triple confidence should be 0.8, got %f", tr.Confidence)
}
}
// all should have SubjectType/ObjectType set
if tr.SubjectType == "" || tr.ObjectType == "" {
t.Errorf("triple %+v missing SubjectType or ObjectType", tr)
}
}
}