Files
memoryweave/go/internal/api/routes/patterns_test.go
T

133 lines
4.8 KiB
Go
Raw 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.
package routes
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/xiaoxue/memoryweave/internal/models"
"github.com/xiaoxue/memoryweave/internal/pattern"
"github.com/xiaoxue/memoryweave/internal/storage"
)
// 织忆 — L2 模式 API 路由测试(t_d32e5f8a)
// AC1: 空库 detect/list → 不报错(count=0)
// AC2: 3 条同工具组合记忆 → detect 后 list 能查到 tool_combo pattern
// newPatternTestAPI 构造一个用临时 graph.db + 假 LanceDB 后端的 PatternAPI(无网络、无 CGO)。
// 假后端只实现 PatternAPI 实际调用的 Scan(其余方法嵌 nil 接口,测试路径不会触达)。
func newPatternTestAPI(t *testing.T, mems []models.MemoryRecord) *PatternAPI {
t.Helper()
store := pattern.NewGraphPatternStore(t.TempDir() + "/graph.db")
t.Cleanup(store.Close)
mlc := &patternScanLance{rows: mems}
pemb := pattern.NewEmbedderProvider(&fakePatternEmbedder{})
return NewPatternAPI(store, pattern.NewPatternProvider(store, pemb), func() storage.LanceDB { return mlc })
}
// patternScanLance 覆写 Scan(PatternAPI.Detect 唯一用到的 LanceDB 方法);
// 其余方法嵌 nil 接口 —— 本测试路径不会调用,触达即 panic(暴露误用)。
type patternScanLance struct {
storage.LanceDB
rows []models.MemoryRecord
}
func (f *patternScanLance) Scan(limit int, namespaceFilter string) ([]models.MemoryRecord, error) {
if limit > 0 && len(f.rows) > limit {
return f.rows[:limit], nil
}
return f.rows, nil
}
// fakePatternEmbedder 确定性假向量(pattern.Provider 匹配测试用;注入测试走 storage 侧不走这里)。
type fakePatternEmbedder struct{}
func (fakePatternEmbedder) EncodeSingle(text string) ([]float32, error) {
v := make([]float32, 8)
for i := 0; i < 8; i++ {
v[i] = 0.1
}
return v, nil
}
// TestPatterns_ListEmpty —— AC1:空库 list → 200 + count=0(不报错)。
func TestPatterns_ListEmpty(t *testing.T) {
api := newPatternTestAPI(t, nil)
rec := httptest.NewRecorder()
api.List(rec, httptest.NewRequest("GET", "/api/v1/patterns", nil))
if rec.Code != http.StatusOK {
t.Fatalf("空库 list 期望 200,实际 %d body=%s", rec.Code, rec.Body.String())
}
var resp map[string]interface{}
_ = json.Unmarshal(rec.Body.Bytes(), &resp)
if int(resp["count"].(float64)) != 0 {
t.Fatalf("空库 count 期望 0,实际 %v", resp["count"])
}
}
// TestPatterns_DetectEmpty —— AC1:空库 detect → 200 + scanned=0/created=0(不报错)。
func TestPatterns_DetectEmpty(t *testing.T) {
api := newPatternTestAPI(t, nil)
rec := httptest.NewRecorder()
api.Detect(rec, httptest.NewRequest("POST", "/api/v1/patterns/detect", nil))
if rec.Code != http.StatusOK {
t.Fatalf("空库 detect 期望 200,实际 %d body=%s", rec.Code, rec.Body.String())
}
var resp map[string]interface{}
_ = json.Unmarshal(rec.Body.Bytes(), &resp)
if int(resp["scanned"].(float64)) != 0 || int(resp["created"].(float64)) != 0 {
t.Fatalf("空库 detect 期望 0/0,实际 %v/%v", resp["scanned"], resp["created"])
}
}
// TestPatterns_DetectToolCombo —— AC2:3 条同工具组合蒸馏记忆 → detect 产出 1 个 tool_combo,
// 且 list 能查到(sqlite graph_nodes 判据)。
func TestPatterns_DetectToolCombo(t *testing.T) {
mems := make([]models.MemoryRecord, 0, 3)
for i, id := range []string{"pc_a", "pc_b", "pc_c"} {
mems = append(mems, models.MemoryRecord{
ID: id,
Content: "用 jq git curl 处理第" + string(rune('一'+i)) + "项",
Category: "distilled",
})
}
api := newPatternTestAPI(t, mems)
rec := httptest.NewRecorder()
api.Detect(rec, httptest.NewRequest("POST", "/api/v1/patterns/detect", nil))
if rec.Code != http.StatusOK {
t.Fatalf("detect 期望 200,实际 %d body=%s", rec.Code, rec.Body.String())
}
var dresp map[string]interface{}
_ = json.Unmarshal(rec.Body.Bytes(), &dresp)
if int(dresp["detected"].(float64)) < 1 {
t.Fatalf("3 条同组合应检出 ≥1 个 pattern,实际 detected=%v", dresp["detected"])
}
// 用 list 再核一遍 sqlite 判据:确有 tool_combo 行
lrec := httptest.NewRecorder()
api.List(lrec, httptest.NewRequest("GET", "/api/v1/patterns", nil))
var lresp struct {
Count int `json:"count"`
Patterns []struct {
Type string `json:"type"`
Name string `json:"name"`
} `json:"patterns"`
}
if err := json.Unmarshal(lrec.Body.Bytes(), &lresp); err != nil {
t.Fatalf("list 解析: %v (body=%s)", err, lrec.Body.String())
}
if lresp.Count < 1 {
t.Fatalf("AC2 判据:detect 后 list count 应 ≥1,实际 %d (detected=%v)", lresp.Count, dresp["detected"])
}
var foundToolCombo bool
for _, p := range lresp.Patterns {
if p.Type == "tool_combo" {
foundToolCombo = true
}
}
if !foundToolCombo {
t.Fatalf("AC2 判据:list 应含 tool_combo pattern,实际 %d 条均非 tool_combo", lresp.Count)
}
}