// 织忆 MemoryWeave — 自优化层单元测试 package selfoptimize import ( "os" "path/filepath" "testing" "time" ) // zeroDash 复位全局 Dash(测试用;逐字段置零以避免复制含 mutex 的结构体) func zeroDash() { Dash.mu.Lock() defer Dash.mu.Unlock() Dash.UsefulCount = 0 Dash.NotUsefulCount = 0 Dash.TotalRecalls = 0 Dash.HitCount = 0 Dash.ClosedGaps = 0 Dash.TotalGaps = 0 Dash.CascadeFixedTotal = 0 Dash.TotalFixes = 0 Dash.DeprecatedToday = 0 Dash.DistillLossSum = 0 Dash.DistillLossCount = 0 Dash.AutoResolvedConflicts = 0 Dash.TotalConflicts = 0 } // TestMain 单元测试不得写到生产落盘点(曾实测:整套测试跑完把 /var/lib/memoryweave/dashboard.json 写成了测试数字) func TestMain(m *testing.M) { dir, err := os.MkdirTemp("", "selfopt-test-*") if err != nil { panic(err) } _ = os.Setenv("ZHIYI_DASHBOARD_PATH", filepath.Join(dir, "dashboard.json")) code := m.Run() _ = os.RemoveAll(dir) os.Exit(code) } // ─── 仪表盘 ────────────────────────────────────────────── func TestDashboard_InitialMetricsAllZero(t *testing.T) { d := &Dashboard{} m := d.Metrics() if m["recall_usefulness_rate"] != 0 { t.Errorf("expected 0, got %f", m["recall_usefulness_rate"]) } if m["recall_hit_rate"] != 0 { t.Errorf("expected 0, got %f", m["recall_hit_rate"]) } } func TestDashboard_UsefulnessRate(t *testing.T) { d := &Dashboard{} d.RecordFeedback(true) d.RecordFeedback(true) d.RecordFeedback(true) d.RecordFeedback(false) m := d.Metrics() if m["recall_usefulness_rate"] != 0.75 { t.Errorf("expected 0.75, got %f", m["recall_usefulness_rate"]) } } func TestDashboard_HitRate(t *testing.T) { d := &Dashboard{} d.RecordRecall(true) d.RecordRecall(true) d.RecordRecall(false) m := d.Metrics() if m["recall_hit_rate"] < 0.66 || m["recall_hit_rate"] > 0.67 { t.Errorf("expected ~0.67, got %f", m["recall_hit_rate"]) } } func TestDashboard_GapClosureRate(t *testing.T) { d := &Dashboard{} d.TotalGaps = 10 d.ClosedGaps = 5 m := d.Metrics() if m["gap_closure_rate"] != 0.5 { t.Errorf("expected 0.5, got %f", m["gap_closure_rate"]) } } func TestDashboard_RecordDeprecation(t *testing.T) { d := &Dashboard{} d.RecordDeprecation() d.RecordDeprecation() if d.DeprecatedToday != 2 { t.Errorf("expected 2, got %d", d.DeprecatedToday) } } func TestDashboard_RecordCorrection(t *testing.T) { d := &Dashboard{} d.RecordCorrection("muchen_correction") d.RecordCorrection("config_parse") if d.TotalFixes != 2 { t.Errorf("expected 2 fixes, got %d", d.TotalFixes) } if d.CascadeFixedTotal != 1 { t.Errorf("expected 1 cascade, got %d", d.CascadeFixedTotal) } } func TestDashboard_RecordConflictResolved(t *testing.T) { d := &Dashboard{} d.RecordConflictResolved(true) d.RecordConflictResolved(false) if d.TotalConflicts != 2 { t.Errorf("expected 2 conflicts, got %d", d.TotalConflicts) } if d.AutoResolvedConflicts != 1 { t.Errorf("expected 1 auto, got %d", d.AutoResolvedConflicts) } m := d.Metrics() if m["auto_resolve_rate"] != 0.5 { t.Errorf("expected 0.5, got %f", m["auto_resolve_rate"]) } } // ─── 知识缺口检测 ──────────────────────────────────────── func TestGapDetector_NotTriggeredBeforeThreshold(t *testing.T) { gd := NewGapDetector(nil, nil) gap := gd.RecordMiss("kubernetes") if gap != nil { t.Error("should not trigger gap after 1 miss") } gd.RecordMiss("kubernetes") if gap := gd.RecordMiss("kubernetes"); gap == nil { t.Error("should trigger gap after 3 misses") } } func TestGapDetector_DetectsGapAtThreshold(t *testing.T) { gd := NewGapDetector(nil, nil) gd.RecordMiss("topicX") gd.RecordMiss("topicX") gap := gd.RecordMiss("topicX") if gap == nil { t.Fatal("expected gap after 3 misses") } if gap.Topic != "topicX" { t.Errorf("wrong topic: %s", gap.Topic) } if gap.MissCount != 3 { t.Errorf("expected 3 misses, got %d", gap.MissCount) } } func TestGapDetector_CloseGap(t *testing.T) { gd := NewGapDetector(nil, nil) gd.RecordMiss("topic") gd.RecordMiss("topic") gd.RecordMiss("topic") gd.Close("topic") gaps := gd.List() if len(gaps) == 0 || !gaps[0].Closed { t.Error("gap should be closed") } } func TestGapDetector_ClassifySynonym(t *testing.T) { gd := NewGapDetector(nil, nil) gd.RecordMiss("API") gd.RecordMiss("API") gd.RecordMiss("API") gaps := gd.List() if len(gaps) > 0 && gaps[0].Type != GapSynonym { t.Errorf("expected synonym gap, got %s", gaps[0].Type) } } // ─── 因果追踪 ──────────────────────────────────────────── func TestCausalTracker_RecordVersions(t *testing.T) { ct := NewCausalTracker() ct.RecordVersion("mem1", "Docker is used by Hermes", "muchen_oral", "init") ct.RecordVersion("mem1", "Docker is used by Hermes AND OpenClaw", "config_parse", "update") if !ct.IsVolatile("mem1") { // 2 versions → not volatile (volatile = 3+) } ct.RecordVersion("mem1", "Docker usage updated", "muchen_correction", "correction") if !ct.IsVolatile("mem1") { t.Error("mem1 should be volatile after 3 versions") } } func TestCausalTracker_DependencyChain(t *testing.T) { ct := NewCausalTracker() ct.AddDependency("memory_A", "memory_B") ct.AddDependency("memory_B", "memory_C") affected := ct.GetAffected("memory_C", nil) // memory_B depends on C, memory_A depends on B foundB := false foundA := false for _, id := range affected { if id == "memory_B" { foundB = true } if id == "memory_A" { foundA = true } } if !foundB || !foundA { t.Errorf("expected both A and B affected, got %v", affected) } } func TestCausalTracker_CircularDependency(t *testing.T) { ct := NewCausalTracker() ct.AddDependency("A", "B") ct.AddDependency("B", "A") affected := ct.GetAffected("A", nil) // Should not loop infinitely if len(affected) >= 10 { t.Errorf("circular dependency not handled: %d affected", len(affected)) } } // ─── 来源信任度 ────────────────────────────────────────── func TestSourceTrust(t *testing.T) { tests := []struct { source string expect float64 }{ {"muchen_oral", 1.0}, {"muchen_feishu", 0.95}, {"config_parse", 0.7}, {"agent_infer", 0.5}, {"llm_distill", 0.4}, {"unknown", 0.3}, } for _, tc := range tests { if got := SourceTrust(tc.source); got != tc.expect { t.Errorf("SourceTrust(%s) = %f, want %f", tc.source, got, tc.expect) } } } // ─── 记忆预取 ──────────────────────────────────────────── func TestPrefetchGraph_CoOccurrence(t *testing.T) { pg := NewPrefetchGraph() pg.RecordCoAccess("docker", "nginx") pg.RecordCoAccess("docker", "nginx") pg.RecordCoAccess("docker", "kubernetes") prefetch := pg.GetPrefetch("docker") if len(prefetch) < 1 { t.Errorf("expected at least 1 prefetch item, got %d", len(prefetch)) } } func TestPrefetchGraph_NothingToPrefetch(t *testing.T) { pg := NewPrefetchGraph() prefetch := pg.GetPrefetch("nonexistent") if len(prefetch) != 0 { t.Errorf("expected empty prefetch, got %d", len(prefetch)) } } // ─── 并发安全 ──────────────────────────────────────────── func TestDashboard_Concurrent(t *testing.T) { d := &Dashboard{} done := make(chan bool) for i := 0; i < 50; i++ { go func(v bool) { for j := 0; j < 100; j++ { d.RecordFeedback(v) d.RecordRecall(v) } done <- true }(i%2 == 0) } for i := 0; i < 50; i++ { <-done } m := d.Metrics() if m["recall_usefulness_rate"] < 0 || m["recall_usefulness_rate"] > 1 { t.Errorf("usefulness rate out of bounds: %f", m["recall_usefulness_rate"]) } } // ─── 基准测试 ──────────────────────────────────────────── func BenchmarkDashboardMetrics(b *testing.B) { d := &Dashboard{} d.UsefulCount = 1000 d.NotUsefulCount = 200 d.TotalRecalls = 1200 d.HitCount = 900 b.ResetTimer() for i := 0; i < b.N; i++ { d.Metrics() } } func BenchmarkGapDetection(b *testing.B) { gd := NewGapDetector(nil, nil) b.ResetTimer() for i := 0; i < b.N; i++ { gd.RecordMiss("benchmark_topic") } } func BenchmarkCausalTracking(b *testing.B) { ct := NewCausalTracker() for i := 0; i < 100; i++ { ct.AddDependency("dep"+string(rune('a'+i%26)), "dep"+string(rune('a'+(i+1)%26))) } b.ResetTimer() for i := 0; i < b.N; i++ { ct.GetAffected("depa", nil) } } func BenchmarkPrefetch(b *testing.B) { pg := NewPrefetchGraph() for i := 0; i < 100; i++ { pg.RecordCoAccess("main", "item"+string(rune('a'+i%26))) } b.ResetTimer() for i := 0; i < b.N; i++ { pg.GetPrefetch("main") } } func init() { // Ensure consistent timestamps for snapshot tests time.Local = time.UTC } // ─── 本地 JSON 持久化(2026-09-27 加:Redis 不可用时的兜底)────────────── func TestDashboardLocalPersistence_RoundTrip(t *testing.T) { p := filepath.Join(t.TempDir(), "dash.json") t.Setenv("ZHIYI_DASHBOARD_PATH", p) src := &Dashboard{ TotalRecalls: 7, HitCount: 6, UsefulCount: 5, NotUsefulCount: 1, DistillLossSum: 1.05, DistillLossCount: 5, ClosedGaps: 2, TotalGaps: 4, TotalFixes: 3, CascadeFixedTotal: 1, AutoResolvedConflicts: 2, TotalConflicts: 3, DeprecatedToday: 4, } src.mu.Lock() src.persist() // 测试环境无 Redis → 走本地 JSON 分支 src.mu.Unlock() if _, err := os.Stat(p); err != nil { t.Fatalf("本地落盘文件未生成: %v", err) } zeroDash() // 模拟进程重启 if err := loadDashboardLocal(); err != nil { t.Fatalf("loadDashboardLocal: %v", err) } m := Dash.Metrics() want := map[string]float64{ "recall_hit_rate": 0.86, // 6/7 "recall_usefulness_rate": 0.83, // 5/6 "gap_closure_rate": 0.5, // 2/4 "cascade_fix_rate": 0.33, // 1/3 "auto_resolve_rate": 0.67, // 2/3 "deprecated_per_day": 4, "avg_distill_loss": 0.21, // 1.05/5 } for k, v := range want { if m[k] != v { t.Errorf("%s: want %v got %v", k, v, m[k]) } } } func TestDashboardLocalPersistence_StaleDeprecatedResetsToZero(t *testing.T) { p := filepath.Join(t.TempDir(), "dash.json") t.Setenv("ZHIYI_DASHBOARD_PATH", p) // 手写一份"很久以前"的快照:deprecated_today 应归零,其余计数应保留 blob := `{"deprecated_today":9,"deprecated_date":"2000-01-01","total_recalls":3,"hit_count":2}` if err := os.WriteFile(p, []byte(blob), 0o600); err != nil { t.Fatal(err) } zeroDash() if err := loadDashboardLocal(); err != nil { t.Fatal(err) } m := Dash.Metrics() if m["deprecated_per_day"] != 0 { t.Errorf("跨天未归零: %v", m["deprecated_per_day"]) } if m["recall_hit_rate"] != 0.67 { // 2/3 t.Errorf("其他计数应保留, got %v", m["recall_hit_rate"]) } } func TestDashboardLocalPersistence_MissingOrBrokenFileIsSafe(t *testing.T) { p := filepath.Join(t.TempDir(), "nope.json") t.Setenv("ZHIYI_DASHBOARD_PATH", p) zeroDash() if err := loadDashboardLocal(); err == nil { t.Errorf("缺文件应返回 err(调用方忽略,行为退回仅内存)") } if Dash.Metrics()["recall_hit_rate"] != 0 { t.Errorf("缺文件不应改动 Dash") } if err := os.WriteFile(p, []byte("{ not json"), 0o600); err != nil { t.Fatal(err) } if err := loadDashboardLocal(); err == nil { t.Errorf("坏文件应返回 err") } if Dash.Metrics()["recall_hit_rate"] != 0 { t.Errorf("坏文件不应改动 Dash") } }