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) } }