Files

112 lines
3.9 KiB
Go
Raw Permalink 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 —— 2 条同组合(<3)→ 不产 tool_combo(AC2 反例)。
func TestDetect_BelowThreshold(t *testing.T) {
recs := comboRecords(2, "jq git curl")
pats := DetectFromRecords(recs, DefaultConfig())
for _, p := range pats {
if p.Type == "tool_combo" {
t.Fatalf("2 条同组合不应产 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)
}
}