Files
memoryweave/go/internal/pattern/store_test.go
T
hermes-prof-dev 99cb22ba93 fix(pattern): 二轮判据收紧——白名单唯一来源+通用词黑名单+hash/数值/目录碎片过滤+conf≥0.5写入门槛+组合≥2成员 (t_639c5a03)
小怡 2026-10-30 二轮裁定 5 条全落:
1. ExtractTools 移除'命令式标识'兜底(hash/uuid/目录/文件名碎片的进入路径),成员=knownTools 白名单唯一来源
2. genericTools 黑名单(git/go/curl/grep/bot-chat/mcp/api 等)即使形态合格也拒
3. isValidTool 新增 isHashFragment/isNumericUnitFragment/isNumberedDirName 形态闸
4. UpsertPatterns conf<0.5 一律不写(MinWriteConfidence)
5. tool_combo 需≥2成员(组合语义);IsValidPatternName 同步收紧
单测:junk 类别逐项回归 + 写入门槛 + 幂等/门限值钉死
2026-10-30 20:59:04 +08:00

117 lines
3.6 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 pattern
import (
"database/sql"
"encoding/json"
"testing"
_ "modernc.org/sqlite"
)
// newTestStore 指向 t.TempDir() 的空库(modernc 纯 Go,无需 CGO/网络)。
func newTestStore(t *testing.T) *GraphPatternStore {
t.Helper()
dir := t.TempDir()
s := NewGraphPatternStore(dir + "/graph.db")
t.Cleanup(s.Close)
return s
}
// TestStore_EmptyList —— 空库 ListPatterns → 0 条不报错(AC1 判据:SELECT count ≥0 不报错)。
func TestStore_EmptyList(t *testing.T) {
ts := newTestStore(t)
got, _, err := ts.ListPatterns(10)
if err != nil {
t.Fatalf("空库 List 不应报错: %v", err)
}
if len(got) != 0 {
t.Fatalf("空库应 0 条 pattern,实际 %d", len(got))
}
}
// TestStore_UpsertEmpty —— 空输入 Upsert → 0,0,nil(AC1 判据)。
func TestStore_UpsertEmpty(t *testing.T) {
s := newTestStore(t)
created, updated, err := s.UpsertPatterns(nil)
if err != nil {
t.Fatalf("空输入 Upsert 不应报错: %v", err)
}
if created != 0 || updated != 0 {
t.Fatalf("空输入应 0/0,实际 %d/%d", created, updated)
}
}
// TestStore_UpsertCreate —— 新 pattern → INSERT 1 条,occurrence_count=1(AC2 前置)。
func TestStore_UpsertCreate(t *testing.T) {
s := newTestStore(t)
p := Pattern{
ID: "pat_tool_combo:jq+rsync+sqlite3",
Type: "tool_combo",
Name: "tool_combo:jq+rsync+sqlite3",
Data: "jq+rsync+sqlite3",
Confidence: 0.6,
Tools: []string{"jq", "rsync", "sqlite3"},
SourceTasks: []string{"m_a", "m_b", "m_c", "m_d", "m_e", "m_f"},
}
created, updated, err := s.UpsertPatterns([]Pattern{p})
if err != nil {
t.Fatalf("upsert 报错: %v", err)
}
if created != 1 || updated != 0 {
t.Fatalf("新 pattern 应 created=1/updated=0,实际 %d/%d", created, updated)
}
listed, _, _ := s.ListPatterns(10)
if len(listed) != 1 {
t.Fatalf("应 1 条,实际 %d", len(listed))
}
}
// TestStore_UpsertIdempotent —— 同名 pattern 二次 upsert → updated=1、occurrence_count=2。
// 验证幂等语义(occurrence_count 驱动,AC2 sqlite 查判据):跑两次检测,不产生重复行。
func TestStore_UpsertIdempotent(t *testing.T) {
s := newTestStore(t)
p := Pattern{
ID: "pat_tool_combo:jq+rsync+sqlite3",
Type: "tool_combo",
Name: "tool_combo:jq+rsync+sqlite3",
Data: "jq+rsync+sqlite3",
Confidence: 0.6,
Tools: []string{"jq", "rsync", "sqlite3"},
SourceTasks: []string{"m_a", "m_b", "m_c", "m_d", "m_e"},
}
c1, u1, _ := s.UpsertPatterns([]Pattern{p}) // 新建
p.Confidence = 0.8 // 第二次检测:同组合再次出现(freq 抬升)
p.SourceTasks = []string{"m_f"}
c2, u2, _ := s.UpsertPatterns([]Pattern{p}) // 应幂等更新(updated=1)
if c1 != 1 || u1 != 0 {
t.Fatalf("首次应 created=1/updated=0,实际 %d/%d", c1, u1)
}
if c2 != 0 || u2 != 1 {
t.Fatalf("二次应 created=0/updated=1,实际 %d/%d", c2, u2)
}
// 读库验:仍是 1 行、occurrence_count=2(幂等,非重复)
db, err := sql.Open("sqlite", s.dbPath)
if err != nil {
t.Fatalf("open: %v", err)
}
defer db.Close()
var (
cnt int
occ int
)
if err := db.QueryRow(`SELECT count(*) FROM graph_nodes WHERE type='pattern'`).Scan(&cnt); err != nil {
t.Fatal(err)
}
if cnt != 1 {
t.Fatalf("两次同 pattern 检测后应仍 1 行,实际 %d(幂等失败)", cnt)
}
var raw string
_ = db.QueryRow(`SELECT properties FROM graph_nodes WHERE type='pattern'`).Scan(&raw)
var props map[string]interface{}
_ = json.Unmarshal([]byte(raw), &props)
occ = int(props["occurrence_count"].(float64))
if occ != 2 {
t.Fatalf("occurrence_count 应累计到 2,实际 %d", occ)
}
}