Files

116 lines
3.4 KiB
Go
Raw Permalink 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.
// 织忆 MemoryWeave — P3 Peer 编排测试(t_f8fc97e6)
// CollectFacts 分类判据 + BuildPeer 四层画像写回,全确定性可复算。
package peer
import (
"testing"
"github.com/xiaoxue/memoryweave/internal/models"
)
// CollectFacts:造 5 条含实体名的 L1 记忆 → matched=5,facts/prefs/skills 分类正确。
func TestCollectFacts_5Memories(t *testing.T) {
mems := []models.MemoryRecord{
{Content: "牧尘选择 Docker 部署"},
{Content: "牧尘偏好 SysV 风格"},
{Content: "牧尘擅长 Python"},
{Content: "牧尘厌恶 systemd"},
{Content: "牧尘使用 jq 处理配置"},
}
facts, prefs, skills, matched := CollectFacts(mems, "牧尘")
if matched != 5 {
t.Fatalf("matched=%d 期望 5(全部含实体名)", matched)
}
if len(prefs) != 2 { // 偏好 SysV / 厌恶 systemd
t.Errorf("prefs=%v 期望 2", prefs)
}
if len(skills) != 1 { // 擅长 Python
t.Errorf("skills=%v 期望 1", skills)
}
if len(facts) != 2 { // 选择 Docker / 使用 jq
t.Errorf("facts=%v 期望 2", facts)
}
}
// CollectFacts:不含实体名的记忆不命中(matched=0)。
func TestCollectFacts_NoMatch(t *testing.T) {
mems := []models.MemoryRecord{{Content: "今天天气很好"}, {Content: "构建一个服务"}}
if _, _, _, matched := CollectFacts(mems, "牧尘"); matched != 0 {
t.Errorf("matched=%d 期望 0", matched)
}
}
// AC1 核心:5 条 L1 记忆 → BuildPeer → peers 表四层非空。
func TestAC1_BuildPeerCreatesRow(t *testing.T) {
s := newTestStore(t)
reasoner := &FakeReasoner{}
mems := []models.MemoryRecord{
{Content: "牧尘选择 Docker 部署"},
{Content: "牧尘偏好 SysV 风格"},
{Content: "牧尘擅长 Python"},
{Content: "牧尘厌恶 systemd"},
{Content: "牧尘使用 jq 处理配置"},
}
facts, prefs, skills, matched := CollectFacts(mems, "牧尘")
if matched != 5 {
t.Fatalf("matched=%d 期望 5", matched)
}
p, err := BuildPeer(s, reasoner, "牧尘", facts, prefs, skills, []string{"容器化"})
if err != nil {
t.Fatalf("BuildPeer: %v", err)
}
if p == nil {
t.Fatal("BuildPeer 返回 nil")
}
// 四层非空
if len(p.InferredTraits) == 0 {
t.Error("inferred_traits 空")
}
if len(p.InferredGoals) == 0 {
t.Error("inferred_goals 空")
}
if len(p.Patterns) == 0 {
t.Error("patterns 空")
}
if p.HypothesizedContext == "" {
t.Error("hypothesized_context 空")
}
// 读回校验
got, err := s.Get("牧尘")
if err != nil {
t.Fatalf("读回: %v", err)
}
if got.PeerID != "牧尘" || len(got.Facts) != 2 {
t.Errorf("读回 peer 不一致: %+v", got)
}
if reasoner.Call != 1 {
t.Errorf("推理引擎调用次数=%d 期望 1", reasoner.Call)
}
}
// BuildPeer:peer_id 为空 → 报错不落库。
func TestBuildPeer_EmptyID(t *testing.T) {
s := newTestStore(t)
if _, err := BuildPeer(s, &FakeReasoner{}, " ", nil, nil, nil, nil); err == nil {
t.Error("空 peer_id 应报错")
}
}
// BuildPeer:推理引擎失败 → 报错不落库(无脏画像)。
func TestBuildPeer_ReasonerError(t *testing.T) {
s := newTestStore(t)
failing := &FakeReasoner{Err: errFake}
if _, err := BuildPeer(s, failing, "muc", []string{"f"}, nil, nil, nil); err == nil {
t.Error("推理失败应报错")
}
if n, _ := s.Count(); n != 0 {
t.Errorf("Count=%d 期望 0(失败不落库)", n)
}
}
var errFake = &fakeErr{}
type fakeErr struct{}
func (*fakeErr) Error() string { return "fake reasoner error" }