145 lines
5.2 KiB
Go
145 lines
5.2 KiB
Go
// 织忆 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) {
|
||
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) {
|
||
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")
|
||
}
|
||
} |