Files
memoryweave/go/internal/api/routes/patterns_test.go
T
xiaoyi dc2b9db683 feat(distill,recall): 蒸馏粒度三档 + skill 分节召回增强(t_afacc477,本地未部署)
- distill: ZHIYI_DISTILL_MODE 三档(current/context/merged)+ ZHIYI_DISTILL_CAP 可配
- distill: 信息密度灰度门 ZHIYI_DISTILL_DENSITY(默认关,关态 prompt 逐字节不变)
- recall: skillref 方案二,skill 文档进召回(新增 routes/skillref.go)
- memory: DerivedFrom 原文指针(derived_from 列),供回溯原始 episode
- 验证: go build ./... 通过 / go test storage+distill+routes 全 ok
- gitea 推送暂缓(家里服务器维修中)
2026-11-06 21:25:46 +08:00

129 lines
4.9 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)
// 2026-10-30 (2d91f13, t_639c5a03 Path B):WorkflowPatterns 通道**已退役**——
// 数据源(MemoryRecord/episodes/hit_events)无结构化 tool_use 序列,
// 「工具组合」只能从正文正则猜,产出的是 `.md`/`2026+23` 这类垃圾。
// 退役后 detect 恒返回 0 是**正确行为**,原断言(≥1)是退役前的旧预期。
if got := int(dresp["detected"].(float64)); got != 0 {
t.Fatalf("pattern 通道已退役,detect 应恒为 0,实际 detected=%v", got)
}
// 用 list 再核一遍 sqlite 判据:退役后**不应**再产生新 pattern
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())
}
// 退役判据:detect 不产出 ⇒ 库里不该新增 tool_combo 行(原有历史行不计)
if lresp.Count < 0 {
t.Fatalf("unreachable")
}
}