Files
memoryweave/go/internal/api/routes/patterns_skillgen_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

150 lines
5.7 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.
// 织忆 MemoryWeave — L3 skill 草稿 API 路由测试(t_fcadee04)
//
// 覆盖端点:POST /api/v1/patterns/{id}/draft + POST /api/v1/patterns/{id}/promote
// 共享同一临时 graph.db(pattern 在 graph_nodes、skill 在 skills 表同库),零网络/零 CGO。
package routes
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"github.com/xiaoxue/memoryweave/internal/pattern"
"github.com/xiaoxue/memoryweave/internal/skillgen"
"github.com/xiaoxue/memoryweave/internal/storage"
)
// newSkillgenTestAPI 构造共享同一 temp graph.db 的 PatternAPI(含 L3 装配)。
func newSkillgenTestAPI(t *testing.T) (*PatternAPI, string) {
t.Helper()
dbDir := t.TempDir()
graphPath := filepath.Join(dbDir, "graph.db")
pstore := pattern.NewGraphPatternStore(graphPath)
t.Cleanup(pstore.Close)
sstore := skillgen.NewStore(graphPath)
t.Cleanup(sstore.Close)
attRoot := filepath.Join(t.TempDir(), "attachments")
api := NewPatternAPI(pstore, nil, func() storage.LanceDB { return nil })
api.SkillStore = sstore
api.Generator = &skillgen.FakeGenerator{Out: "## 适用场景\n重复处理"}
api.AttachmentsDir = attRoot
// 种一个 conf=0.9 的 pattern 进 graph_nodes
hi := pattern.Pattern{
ID: "pat_tool_combo:curl+git+jq", Type: "tool_combo",
Name: "tool_combo:curl+git+jq", Data: "curl+git+jq",
Confidence: 0.9, Tools: []string{"curl", "git", "jq"},
SourceTasks: []string{"m_a", "m_b", "m_c"},
}
if _, _, err := pstore.UpsertPatterns([]pattern.Pattern{hi}); err != nil {
t.Fatalf("seed pattern: %v", err)
}
return api, attRoot
}
// TestSkillgen_DraftRoute —— AC1 API 层:draft 端点 → 200 + skills 表 1 行 draft + 附件文件。
func TestSkillgen_DraftRoute(t *testing.T) {
t.Skip("pattern 通道已退役 2026-10-30 (2d91f13, t_639c5a03 Path B):tool_combo 不再被 detect 产出," +
"本用例依赖 seed 的 tool_combo pattern 可被 draft/promote 消费;通道退役后该路径无实际意义")
api, attRoot := newSkillgenTestAPI(t)
id := "pat_tool_combo:curl+git+jq"
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/api/v1/patterns/"+id+"/draft", nil)
api.Sub(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("draft 期望 200,实际 %d body=%s", rec.Code, rec.Body.String())
}
// skills 表 1 行 draft
if n, _ := api.SkillStore.Count("draft"); n != 1 {
t.Fatalf("AC1 判据:draft 行数应 1,实际 %d", n)
}
sk, err := api.SkillStore.Get(id)
if err != nil || sk.Status != "draft" {
t.Fatalf("skill 应存在且 draft,实际 %v %+v", err, sk)
}
// 附件文件存在 + 含 [AUTO-DRAFT]
path := filepath.Join(attRoot, skillgen.DraftSubDir("tool_combo:curl+git+jq"), "skill-draft.md")
data, rerr := os.ReadFile(path)
if rerr != nil {
t.Fatalf("附件文件不存在 %s: %v", path, rerr)
}
if !strings.Contains(string(data), skillgen.AUTO_DRAFT_MARKER) {
t.Fatalf("附件应含 [AUTO-DRAFT] 标记")
}
t.Logf("route draft 落盘: %s", path)
}
// TestSkillgen_PromoteRoute —— AC3 API 层:promote 端点 → 200 + status active。
func TestSkillgen_PromoteRoute(t *testing.T) {
t.Skip("pattern 通道已退役 2026-10-30 (2d91f13, t_639c5a03 Path B):tool_combo 不再被 detect 产出," +
"本用例依赖 seed 的 tool_combo pattern 可被 draft/promote 消费;通道退役后该路径无实际意义")
api, _ := newSkillgenTestAPI(t)
id := "pat_tool_combo:curl+git+jq"
// 先 draft
dr := httptest.NewRecorder()
api.Sub(dr, httptest.NewRequest("POST", "/api/v1/patterns/"+id+"/draft", nil))
if dr.Code != http.StatusOK {
t.Fatalf("前置 draft 应 200,实际 %d", dr.Code)
}
// promote
pr := httptest.NewRecorder()
api.Sub(pr, httptest.NewRequest("POST", "/api/v1/patterns/"+id+"/promote", nil))
if pr.Code != http.StatusOK {
t.Fatalf("promote 期望 200,实际 %d body=%s", pr.Code, pr.Body.String())
}
var resp struct {
Skill struct {
Status string `json:"status"`
} `json:"skill"`
}
if err := json.Unmarshal(pr.Body.Bytes(), &resp); err != nil {
t.Fatalf("promote 解析: %v", err)
}
if resp.Skill.Status != "active" {
t.Fatalf("AC3 判据:promote 后 status 应 active,实际 %s", resp.Skill.Status)
}
// 库复核(AC3 判据:sqlite 查)
if n, _ := api.SkillStore.Count("active"); n != 1 {
t.Fatalf("AC3 判据:active 行数应 1,实际 %d", n)
}
}
// TestSkillgen_DraftRoute_NotFound —— 不存在的 pattern → 404。
func TestSkillgen_DraftRoute_NotFound(t *testing.T) {
api, _ := newSkillgenTestAPI(t)
rec := httptest.NewRecorder()
api.Sub(rec, httptest.NewRequest("POST", "/api/v1/patterns/nope/draft", nil))
if rec.Code != http.StatusNotFound {
t.Fatalf("不存在 pattern 的 draft 应 404,实际 %d", rec.Code)
}
}
// TestSkillgen_PromoteRoute_NoDraft —— 无草稿即 promote → 404。
func TestSkillgen_PromoteRoute_NoDraft(t *testing.T) {
api, _ := newSkillgenTestAPI(t)
rec := httptest.NewRecorder()
api.Sub(rec, httptest.NewRequest("POST", "/api/v1/patterns/pat_tool_combo:curl+git+jq/promote", nil))
if rec.Code != http.StatusNotFound {
t.Fatalf("无草稿 promote 应 404,实际 %d body=%s", rec.Code, rec.Body.String())
}
}
// TestSkillgen_SubPath —— 子路径解析单元(含 URL 编码 id)。
func TestSkillgen_SubPath(t *testing.T) {
id, action := patternSubPath("/api/v1/patterns/pat_tool_combo:curl+git+jq/promote")
if id != "pat_tool_combo:curl+git+jq" || action != "promote" {
t.Fatalf("解析失败 id=%q action=%q", id, action)
}
if _, a := patternSubPath("/api/v1/patterns"); a != "" {
t.Fatalf("无子路径应 action 空")
}
if _, a := patternSubPath("/api/v1/patterns/detect"); a != "" {
t.Fatalf("/patterns/detect 是精确路由,不应走 Sub")
}
}