Files
memoryweave/go/internal/pattern/pattern_test.go
T

213 lines
7.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 pattern
import (
"strings"
"testing"
"github.com/xiaoxue/memoryweave/internal/models"
)
// 构造 N 条同一工具组合的记忆(内容里带 jq/git/curl 三个已知工具)。
func comboRecords(n int, combo string) []models.MemoryRecord {
// combo 形如 "jq git curl"——内容里出现这三个工具名(空格分隔,保证词边界命中)。
// 上下文用纯中文(非 ascii token),避免回退正则把上下文词误当工具混进组合键。
recs := make([]models.MemoryRecord, 0, n)
for i := 0; i < n; i++ {
recs = append(recs, models.MemoryRecord{
ID: "m_" + string(rune('a'+i)),
Content: "用 " + combo + " 处理第" + string(rune('一'+i)) + "项",
Category: "distilled",
Vector: nil, // 走 bigram Jaccard 回退(确定性,不依赖网络)
})
}
return recs
}
// TestDetect_ToolCombo —— 3 条同工具组合记忆 → 检出 1 个 tool_combo pattern(AC2)。
func TestDetect_ToolCombo(t *testing.T) {
recs := comboRecords(3, "jq git curl")
pats := DetectFromRecords(recs, DefaultConfig())
var toolPatterns []Pattern
for _, p := range pats {
if p.Type == "tool_combo" {
toolPatterns = append(toolPatterns, p)
}
}
if len(toolPatterns) != 1 {
t.Fatalf("期望 1 个 tool_combo pattern,实际 %d: %#v", len(toolPatterns), toolPatterns)
}
p := toolPatterns[0]
if p.Name != "tool_combo:curl+git+jq" {
t.Fatalf("命名键不符: got %q (工具应排序去重为 curl,git,jq)", p.Name)
}
if len(p.Tools) != 3 {
t.Fatalf("Tools 应含 3 个,实际 %v", p.Tools)
}
// confidence = min(freq*0.1,1.0) = min(3*0.1,1.0) ≈ 0.3(浮点容差 1e-9)
if p.Confidence < 0.3-1e-9 || p.Confidence > 0.3+1e-9 {
t.Fatalf("confidence 期望 ≈0.3,实际 %f", p.Confidence)
}
if len(p.SourceTasks) != 3 {
t.Fatalf("SourceTasks 应含 3 条记忆 ID,实际 %v", p.SourceTasks)
}
}
// TestDetect_BelowThreshold —— 1 条同组合(<2)→ 不产 tool_combo(AC2 反例;门槛 t_639c5a03=2)。
func TestDetect_BelowThreshold(t *testing.T) {
recs := comboRecords(1, "jq git curl")
pats := DetectFromRecords(recs, DefaultConfig())
for _, p := range pats {
if p.Type == "tool_combo" {
t.Fatalf("1 条同组合不应产 tool_combo,实际产出: %#v", p)
}
}
}
// TestDetect_Workflow —— 3 条近同文(向量近似 1.0 或 bigram 高度重合)→ 1 个 workflow 簇。
func TestDetect_Workflow(t *testing.T) {
// 向量相同(cosine=1.0 ≥ 0.85)且 3 条 → 应聚成 1 簇。
vec := []float32{1.0, 0.0, 0.0, 0.0}
recs := []models.MemoryRecord{
{ID: "w1", Content: "重启服务后拉日志", Category: "distilled", Vector: vec},
{ID: "w2", Content: "重启服务后拉日志", Category: "distilled", Vector: vec},
{ID: "w3", Content: "重启服务后拉日志", Category: "distilled", Vector: vec},
}
pats := DetectFromRecords(recs, DefaultConfig())
var wf []Pattern
for _, p := range pats {
if p.Type == "workflow" {
wf = append(wf, p)
}
}
if len(wf) != 1 {
t.Fatalf("3 条近同文应聚 1 个 workflow pattern,实际 %d: %#v", len(wf), pats)
}
if !strings.HasPrefix(wf[0].Name, "workflow:cluster-3-") {
t.Fatalf("workflow 命名应含 cluster-3-<fp>,实际 %q", wf[0].Name)
}
}
// TestDetect_Empty —— 空输入 → 空输出不报错(AC1 空库前提)。
func TestDetect_Empty(t *testing.T) {
if pats := DetectFromRecords(nil, DefaultConfig()); len(pats) != 0 {
t.Fatalf("空输入应 0 pattern,实际 %d", len(pats))
}
}
// TestExtractTools —— 词边界:「google」不应命中工具 "go"。
func TestExtractTools_WordBoundary(t *testing.T) {
tools := ExtractTools("I like google and grep the log with jq")
// 应含 grep、jq;不应含 go("google" 被词边界排除)
found := map[string]bool{}
for _, s := range tools {
found[s] = true
}
if !found["grep"] || !found["jq"] {
t.Fatalf("应识别 grep/jq,实际 %v", tools)
}
if found["go"] {
t.Fatalf("'google' 误命中工具 'go'(词边界失效): %v", tools)
}
}
// ── t_639c5a03:垃圾 pattern 判据单测(AC2)──────────────────────────────
// fakeMem 一条带指定 ID 的假蒸馏记忆(向量 nil → 走 bigram 回退,确定性)。
func fakeMem(id, content string) models.MemoryRecord {
return models.MemoryRecord{ID: id, Content: content, Category: "distilled", Vector: nil}
}
// TestDetect_JunkTokensRejected —— 3 条含 .md / 2026 / 标点 的假记忆 ⇒ 0 pattern(AC2 第一条)。
// 覆盖卡现象里的垃圾样本:.md、.md+2026-09-23、2026+23 全不得被当工具组合。
func TestDetect_JunkTokensRejected(t *testing.T) {
recs := []models.MemoryRecord{
fakeMem("j1", "今天把结果导出成 .md 文件"),
fakeMem("j2", "版本 .md+2026-09-23 的构建产物"),
fakeMem("j3", "数字 2026+23 是什么意义"),
}
pats := DetectFromRecords(recs, DefaultConfig())
for _, p := range pats {
t.Fatalf("垃圾记忆不应产出任何 pattern,实际 %#v", p)
}
}
// TestDetect_RealToolComboNeeds2Sources —— 2 条真工具共现(jq+git)⇒ 1 pattern(AC2 第二条)。
// 同时验证门槛 ≥2 条独立来源(单条不产)。
func TestDetect_RealToolComboNeeds2Sources(t *testing.T) {
// 单条真工具组合 → 不产(门槛 2)
one := DetectFromRecords([]models.MemoryRecord{
fakeMem("o1", "用 jq 和 git 解析 json"),
}, DefaultConfig())
for _, p := range one {
if p.Type == "tool_combo" {
t.Fatalf("单条真工具组合不应产 pattern(门槛≥2),实际 %#v", p)
}
}
// 2 条同真组合 → 1 个 tool_combo,成员 jq+git,支撑 2 条
two := DetectFromRecords([]models.MemoryRecord{
fakeMem("a1", "先用 jq 解析再 git 提交"),
fakeMem("a2", "jq 提取字段后 git commit"),
}, DefaultConfig())
var tc []Pattern
for _, p := range two {
if p.Type == "tool_combo" {
tc = append(tc, p)
}
}
if len(tc) != 1 {
t.Fatalf("2 条同真组合应产 1 个 tool_combo,实际 %d: %#v", len(tc), two)
}
if tc[0].Name != "tool_combo:git+jq" {
t.Fatalf("命名键不符: got %q", tc[0].Name)
}
if len(tc[0].SourceTasks) != 2 {
t.Fatalf("支撑应为 2 条独立来源,实际 %v", tc[0].SourceTasks)
}
}
// TestExtractTools_JunkTokenFilter —— 形态级:.md/2026/日期/纯标点/扩展名/停用词全被滤。
func TestExtractTools_JunkTokenFilter(t *testing.T) {
content := "处理 .md 和 2026 与 2026-09-23 以及 2026+23 与 report.md 与 a.py" +
" 还有 git status 和 curl -I https://x 和 ... 和 step 1"
tools := ExtractTools(content)
found := map[string]bool{}
for _, s := range tools {
found[s] = true
}
// 只应识别 git / curl 三个真标识(https 是 URL scheme 停用词,-i 是 flag 不识别)
for _, junk := range []string{".md", "2026", "2026-09-23", "2026+23", "report", "a", "step", "https", "-i"} {
if found[junk] {
t.Fatalf("垃圾 token %q 不应被当工具,实际 %v", junk, tools)
}
}
for _, want := range []string{"git", "curl"} {
if !found[want] {
t.Fatalf("应识别真标识 %q,实际 %v", want, tools)
}
}
}
// TestIsValidPatternName_Gate —— 写入闸:垃圾命名键全拒、真工具键/工作流键放行。
func TestIsValidPatternName_Gate(t *testing.T) {
bad := []string{
"tool_combo:.md", "tool_combo:2026+23", "tool_combo:.md+2026-09-23",
"tool_combo:2026", "tool_combo:report", "tool_combo:2026-09-23",
"tool_combo:usage_track", "tool_combo:t_9143c968", "tool_combo:a+b",
"tool_combo:log", "tool_combo:",
}
for _, name := range bad {
if IsValidPatternName(name) {
t.Fatalf("垃圾命名键 %q 不应通过闸门", name)
}
}
good := []string{
"tool_combo:git+jq", "tool_combo:curl+git+jq", "tool_combo:sqlite3",
"workflow:cluster-3-abcdef12", "workflow:cluster-2-00aa11bb",
}
for _, name := range good {
if !IsValidPatternName(name) {
t.Fatalf("合法命名键 %q 应通过闸门", name)
}
}
}