| 1 | package serve |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestTitleCacheKeepsTitleAcrossMtimeChanges(t *testing.T) { |
| 11 | dir := t.TempDir() |
| 12 | c := newTitleCache(dir) |
| 13 | |
| 14 | if _, ok := c.get("a.jsonl", "first prompt", 100); ok { |
| 15 | t.Fatal("empty cache should miss") |
| 16 | } |
| 17 | |
| 18 | c.put("a.jsonl", "First Title", "first prompt", 100) |
| 19 | if got, ok := c.get("a.jsonl", "first prompt", 200); !ok || got != "First Title" { |
| 20 | t.Fatalf("hit after mtime change = %q,%v, want First Title,true", got, ok) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | func TestTitleCacheInvalidatesWhenFirstMessageChanges(t *testing.T) { |
| 25 | dir := t.TempDir() |
| 26 | c := newTitleCache(dir) |
| 27 | c.put("a.jsonl", "First Title", "first prompt", 100) |
| 28 | |
| 29 | if _, ok := c.get("a.jsonl", "replacement prompt", 100); ok { |
| 30 | t.Fatal("a replaced first message must invalidate the cached title") |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func TestTitleCachePersistsAcrossInstances(t *testing.T) { |
| 35 | dir := t.TempDir() |
| 36 | newTitleCache(dir).put("a.jsonl", "Persisted", "first prompt", 7) |
| 37 | |
| 38 | if _, err := os.Stat(filepath.Join(dir, ".session-titles.json")); err != nil { |
| 39 | t.Fatalf("cache file not written: %v", err) |
| 40 | } |
| 41 | if got, ok := newTitleCache(dir).get("a.jsonl", "first prompt", 8); !ok || got != "Persisted" { |
| 42 | t.Fatalf("fresh instance get = %q,%v, want Persisted,true", got, ok) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | func TestTitleCacheReadsLegacyMtimeEntries(t *testing.T) { |
| 47 | dir := t.TempDir() |
| 48 | path := filepath.Join(dir, ".session-titles.json") |
| 49 | if err := os.WriteFile(path, []byte(`{"a.jsonl":{"title":"Legacy","mod":7}}`), 0o600); err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | |
| 53 | if got, ok := newTitleCache(dir).get("a.jsonl", "first prompt", 7); !ok || got != "Legacy" { |
| 54 | t.Fatalf("matching legacy entry = %q,%v, want Legacy,true", got, ok) |
| 55 | } |
| 56 | if _, ok := newTitleCache(dir).get("a.jsonl", "first prompt", 8); ok { |
| 57 | t.Fatal("legacy entry with changed mtime must regenerate once before becoming sticky") |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func TestTitleCacheWritesRemainReadableByOlderVersions(t *testing.T) { |
| 62 | dir := t.TempDir() |
| 63 | newTitleCache(dir).put("a.jsonl", "Compatible", "first prompt", 7) |
| 64 | data, err := os.ReadFile(filepath.Join(dir, ".session-titles.json")) |
| 65 | if err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | var legacy map[string]struct { |
| 69 | Title string `json:"title"` |
| 70 | Mod int64 `json:"mod"` |
| 71 | } |
| 72 | if err := json.Unmarshal(data, &legacy); err != nil { |
| 73 | t.Fatalf("legacy unmarshal: %v", err) |
| 74 | } |
| 75 | if got := legacy["a.jsonl"]; got.Title != "Compatible" || got.Mod != 7 { |
| 76 | t.Fatalf("legacy entry = %+v, want title and mod preserved", got) |
| 77 | } |
| 78 | } |
| 79 |