xonecas.io
62159
Sy

symb / files

symb / internal / store / store_test.go

6.0 KB
1package store
2
3import (
4	"path/filepath"
5	"testing"
6	"time"
7)
8
9func openTestCache(t *testing.T, ttl time.Duration) *Cache {
10	t.Helper()
11	dbPath := filepath.Join(t.TempDir(), "test.db")
12	c, err := Open(dbPath, ttl)
13	if err != nil {
14		t.Fatalf("Open: %v", err)
15	}
16	t.Cleanup(func() { c.Close() })
17	return c
18}
19
20func TestFetchCache_SetGet(t *testing.T) {
21	c := openTestCache(t, 24*time.Hour)
22
23	// Miss on empty.
24	if _, ok := c.GetFetch("https://example.com"); ok {
25		t.Fatal("expected miss")
26	}
27
28	c.SetFetch("https://example.com", "page content")
29
30	got, ok := c.GetFetch("https://example.com")
31	if !ok {
32		t.Fatal("expected hit")
33	}
34	if got != "page content" {
35		t.Errorf("got %q, want %q", got, "page content")
36	}
37}
38
39func TestFetchCache_Expiry(t *testing.T) {
40	c := openTestCache(t, 1*time.Second)
41	c.SetFetch("https://example.com", "content")
42
43	// Backdate the entry.
44	c.db.Exec("UPDATE fetch_cache SET created = ? WHERE url = ?",
45		time.Now().Add(-2*time.Second).Unix(), "https://example.com")
46
47	if _, ok := c.GetFetch("https://example.com"); ok {
48		t.Fatal("expected stale miss")
49	}
50}
51
52func TestSearchCache_SetGet(t *testing.T) {
53	c := openTestCache(t, 24*time.Hour)
54
55	if _, ok := c.GetSearch("golang context"); ok {
56		t.Fatal("expected miss")
57	}
58
59	c.SetSearch("Golang Context", "results about context")
60
61	// Exact match (case-insensitive via normalization).
62	got, ok := c.GetSearch("golang context")
63	if !ok {
64		t.Fatal("expected hit")
65	}
66	if got != "results about context" {
67		t.Errorf("got %q", got)
68	}
69}
70
71func TestSearchCache_Expiry(t *testing.T) {
72	c := openTestCache(t, 1*time.Second)
73	c.SetSearch("golang context", "results")
74
75	c.db.Exec("UPDATE search_cache SET created = ? WHERE query = ?",
76		time.Now().Add(-2*time.Second).Unix(), "golang context")
77
78	if _, ok := c.GetSearch("golang context"); ok {
79		t.Fatal("expected stale miss")
80	}
81}
82
83func TestSearchCachedContent(t *testing.T) {
84	c := openTestCache(t, 24*time.Hour)
85
86	// Seed cache with a result that contains content about Charm TUI tools.
87	c.SetSearch("charm coding assistant tui", `Found 3 result(s):
88
89--- 1. Crush - Charmbracelet coding assistant ---
90URL: https://github.com/charmbracelet/crush
91A terminal-based AI coding agent built with Bubbletea.
92
93--- 2. Charm blog ---
94URL: https://charm.sh/blog
95Charm builds tools for the terminal including Bubbletea TUI framework.
96
97--- 3. Z.AI docs ---
98URL: https://z.ai/docs
99AI agent for terminal workflows.
100`)
101
102	tests := []struct {
103		name  string
104		query string
105		want  bool
106	}{
107		// These keywords appear in the cached result text:
108		{"same topic different words", "charmbracelet terminal agent", true},
109		{"synonym match", "charm bubbletea framework", true},
110		{"partial content match", "coding agent terminal", true},
111		// These don't appear enough in the cached content:
112		{"unrelated", "python flask deployment", false},
113		{"single keyword too few", "charm performance", false}, // only 1 hit < min 3
114		{"below threshold", "react native mobile flutter", false},
115	}
116
117	for _, tt := range tests {
118		t.Run(tt.name, func(t *testing.T) {
119			_, ok := c.SearchCachedContent(tt.query)
120			if ok != tt.want {
121				t.Errorf("SearchCachedContent(%q) = _, %v; want %v", tt.query, ok, tt.want)
122			}
123		})
124	}
125}
126
127func TestSearchCachedContent_Empty(t *testing.T) {
128	c := openTestCache(t, 24*time.Hour)
129
130	if _, ok := c.SearchCachedContent("anything here"); ok {
131		t.Fatal("expected miss on empty cache")
132	}
133}
134
135func TestSearchCachedContent_StaleIgnored(t *testing.T) {
136	c := openTestCache(t, 1*time.Second)
137	c.SetSearch("golang context package", "golang context cancellation patterns")
138
139	c.db.Exec("UPDATE search_cache SET created = ? WHERE query = ?",
140		time.Now().Add(-2*time.Second).Unix(), "golang context package")
141
142	if _, ok := c.SearchCachedContent("golang context patterns"); ok {
143		t.Fatal("expected stale entries to be ignored")
144	}
145}
146
147func TestTokenize(t *testing.T) {
148	tests := []struct {
149		input string
150		want  []string
151	}{
152		{"Go context package", []string{"go", "context", "package"}},
153		{"the best practices for Go", []string{"best", "practices", "go"}},
154		{"a an the", nil},
155		{"", nil},
156		{"  React.js, hooks!  ", []string{"react.js", "hooks"}},
157	}
158
159	for _, tt := range tests {
160		got := tokenize(tt.input)
161		if !sliceEqual(got, tt.want) {
162			t.Errorf("tokenize(%q) = %v, want %v", tt.input, got, tt.want)
163		}
164	}
165}
166
167func TestContentOverlap(t *testing.T) {
168	content := "charmbracelet crush is a terminal-based ai coding agent built with bubbletea"
169	tests := []struct {
170		query     []string
171		wantScore float64
172		wantHits  int
173	}{
174		{[]string{"charmbracelet", "terminal"}, 1.0, 2},
175		{[]string{"charm", "terminal"}, 1.0, 2}, // "charm" is substring of "charmbracelet"
176		{[]string{"python", "flask"}, 0.0, 0},
177		{[]string{"terminal", "coding", "agent", "react"}, 0.75, 3},
178	}
179
180	for _, tt := range tests {
181		score, hits := contentOverlap(tt.query, content)
182		if diff := score - tt.wantScore; diff > 0.01 || diff < -0.01 {
183			t.Errorf("contentOverlap(%v) score = %f, want %f", tt.query, score, tt.wantScore)
184		}
185		if hits != tt.wantHits {
186			t.Errorf("contentOverlap(%v) hits = %d, want %d", tt.query, hits, tt.wantHits)
187		}
188	}
189}
190
191func TestPurgeStale(t *testing.T) {
192	c := openTestCache(t, 1*time.Second)
193	c.SetFetch("https://old.com", "old")
194	c.SetSearch("old query", "old result")
195
196	// Backdate both.
197	past := time.Now().Add(-2 * time.Second).Unix()
198	c.db.Exec("UPDATE fetch_cache SET created = ?", past)
199	c.db.Exec("UPDATE search_cache SET created = ?", past)
200
201	// Add fresh entries.
202	c.SetFetch("https://new.com", "new")
203	c.SetSearch("new query", "new result")
204
205	c.purgeStale()
206
207	if _, ok := c.GetFetch("https://old.com"); ok {
208		t.Error("stale fetch should be purged")
209	}
210	if _, ok := c.GetFetch("https://new.com"); !ok {
211		t.Error("fresh fetch should survive purge")
212	}
213	if _, ok := c.GetSearch("old query"); ok {
214		t.Error("stale search should be purged")
215	}
216	if _, ok := c.GetSearch("new query"); !ok {
217		t.Error("fresh search should survive purge")
218	}
219}
220
221func sliceEqual(a, b []string) bool {
222	if len(a) != len(b) {
223		return false
224	}
225	for i := range a {
226		if a[i] != b[i] {
227			return false
228		}
229	}
230	return true
231}