MCPcopy Create free account
hub / github.com/Facets-cloud/flow / TestCacheHitMissAndPersist

Function TestCacheHitMissAndPersist

internal/stats/cache_test.go:9–57  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

7)
8
9func TestCacheHitMissAndPersist(t *testing.T) {
10 dir := t.TempDir()
11 jsonl := filepath.Join(dir, "s.jsonl")
12 line := `{"type":"assistant","timestamp":"2026-06-01T10:00:00.000Z","message":{"role":"assistant","content":[{"type":"tool_use","name":"Bash","input":{"command":"flow show task"}}]}}`
13 if err := os.WriteFile(jsonl, []byte(line), 0o644); err != nil {
14 t.Fatal(err)
15 }
16
17 c := LoadCache(filepath.Join(dir, "cache.json")) // missing → empty
18 r1, err := c.ScanFile(jsonl, "mine")
19 if err != nil {
20 t.Fatal(err)
21 }
22 if len(r1.Lookups) != 1 {
23 t.Fatalf("first scan lookups = %d, want 1", len(r1.Lookups))
24 }
25 if len(c.Entries) != 1 {
26 t.Fatalf("cache should have 1 entry, got %d", len(c.Entries))
27 }
28
29 // Second scan, unchanged file → served from cache (entry count stable),
30 // and the cached rollup is returned (not an empty FileRollup).
31 r2, err := c.ScanFile(jsonl, "mine")
32 if err != nil {
33 t.Fatal(err)
34 }
35 if len(r2.Lookups) != 1 {
36 t.Fatalf("cache hit lookups = %d, want 1 (hit must return cached rollup)", len(r2.Lookups))
37 }
38 if len(c.Entries) != 1 {
39 t.Fatalf("cache entry count changed unexpectedly: %d", len(c.Entries))
40 }
41
42 // Persist + reload round-trips.
43 cpath := filepath.Join(dir, "cache.json")
44 if err := c.Save(cpath); err != nil {
45 t.Fatal(err)
46 }
47 c2 := LoadCache(cpath)
48 if len(c2.Entries) != 1 {
49 t.Fatalf("reloaded cache entries = %d, want 1", len(c2.Entries))
50 }
51
52 // Prune drops unseen entries.
53 c2.Prune(map[string]bool{}) // nothing seen
54 if len(c2.Entries) != 0 {
55 t.Fatalf("prune left %d entries, want 0", len(c2.Entries))
56 }
57}
58
59// TestLoadCacheCorrupt verifies the binding constraint that LoadCache never
60// errors: a corrupt cache file must yield an empty, usable cache (not nil,

Callers

nothing calls this directly

Calls 4

LoadCacheFunction · 0.85
ScanFileMethod · 0.80
SaveMethod · 0.80
PruneMethod · 0.80

Tested by

no test coverage detected