|
| 1 | +package desktop |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +// withTempCacheDir points the detection cache at an isolated temp directory |
| 12 | +// for the test and restores the original value on cleanup. |
| 13 | +func withTempCacheDir(t *testing.T) string { |
| 14 | + t.Helper() |
| 15 | + dir := t.TempDir() |
| 16 | + previous := os.Getenv("CAM_CACHE_DIR") |
| 17 | + os.Setenv("CAM_CACHE_DIR", dir) |
| 18 | + t.Cleanup(func() { os.Setenv("CAM_CACHE_DIR", previous) }) |
| 19 | + return dir |
| 20 | +} |
| 21 | + |
| 22 | +func TestDetectionCacheGetMissing(t *testing.T) { |
| 23 | + cache := newDetectionCache() |
| 24 | + cache.loadOnce() |
| 25 | + |
| 26 | + if _, fresh := cache.get("does-not-exist"); fresh { |
| 27 | + t.Fatal("missing entry should not be fresh") |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func TestDetectionCachePutThenGetFresh(t *testing.T) { |
| 32 | + cache := newDetectionCache() |
| 33 | + cache.loadOnce() |
| 34 | + |
| 35 | + cache.put("claude-code", true, "1.2.3") |
| 36 | + entry, fresh := cache.get("claude-code") |
| 37 | + if !fresh { |
| 38 | + t.Fatal("just-cached entry should be fresh") |
| 39 | + } |
| 40 | + if !entry.Installed || entry.Version != "1.2.3" { |
| 41 | + t.Fatalf("entry = %+v, want {Installed:true Version:1.2.3}", entry) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestDetectionCacheStaleAfterTTL(t *testing.T) { |
| 46 | + cache := newDetectionCache() |
| 47 | + cache.loadOnce() |
| 48 | + cache.put("gemini-cli", true, "9.9.9") |
| 49 | + |
| 50 | + // Backdate the entry past detectionTTL so it reads as stale. |
| 51 | + cache.mu.Lock() |
| 52 | + e := cache.entries["gemini-cli"] |
| 53 | + e.DetectedAt = time.Now().Add(-detectionTTL - time.Second) |
| 54 | + cache.entries["gemini-cli"] = e |
| 55 | + cache.mu.Unlock() |
| 56 | + |
| 57 | + if _, fresh := cache.get("gemini-cli"); fresh { |
| 58 | + t.Fatal("backdated entry should be stale") |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestDetectionCachePersistLoadRoundTrip(t *testing.T) { |
| 63 | + withTempCacheDir(t) |
| 64 | + cache := newDetectionCache() |
| 65 | + cache.loadOnce() |
| 66 | + |
| 67 | + cache.put("claude-code", true, "1.2.3") |
| 68 | + cache.put("gemini-cli", false, "") |
| 69 | + cache.persist() |
| 70 | + |
| 71 | + // A fresh cache over the same on-disk file should see both entries. |
| 72 | + loaded := newDetectionCache() |
| 73 | + loaded.loadOnce() |
| 74 | + if _, fresh := loaded.get("claude-code"); !fresh { |
| 75 | + t.Fatal("claude-code should load from disk fresh") |
| 76 | + } |
| 77 | + if _, fresh := loaded.get("gemini-cli"); !fresh { |
| 78 | + t.Fatal("gemini-cli should load from disk fresh") |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestDetectionCacheWritesToToolsSubdir(t *testing.T) { |
| 83 | + dir := withTempCacheDir(t) |
| 84 | + cache := newDetectionCache() |
| 85 | + cache.loadOnce() |
| 86 | + cache.put("claude-code", true, "1.0.0") |
| 87 | + cache.persist() |
| 88 | + |
| 89 | + path := filepath.Join(dir, "tools", "detection.json") |
| 90 | + raw, err := os.ReadFile(path) |
| 91 | + if err != nil { |
| 92 | + t.Fatalf("read cache file: %v", err) |
| 93 | + } |
| 94 | + var file detectionCacheFile |
| 95 | + if err := json.Unmarshal(raw, &file); err != nil { |
| 96 | + t.Fatalf("unmarshal cache file: %v", err) |
| 97 | + } |
| 98 | + if entry, ok := file.Tools["claude-code"]; !ok || entry.Version != "1.0.0" { |
| 99 | + t.Fatalf("cache file = %+v, want claude-code 1.0.0", file.Tools) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func TestDetectionCacheLoadMissingFileIsNoOp(t *testing.T) { |
| 104 | + withTempCacheDir(t) |
| 105 | + cache := newDetectionCache() |
| 106 | + cache.loadOnce() // no file exists yet |
| 107 | + if _, fresh := cache.get("anything"); fresh { |
| 108 | + t.Fatal("empty cache should report nothing fresh") |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func TestDetectionCacheLoadCorruptFileIsNoOp(t *testing.T) { |
| 113 | + dir := withTempCacheDir(t) |
| 114 | + if err := os.MkdirAll(filepath.Join(dir, "tools"), 0o700); err != nil { |
| 115 | + t.Fatal(err) |
| 116 | + } |
| 117 | + if err := os.WriteFile(filepath.Join(dir, "tools", "detection.json"), []byte("{not json"), 0o600); err != nil { |
| 118 | + t.Fatal(err) |
| 119 | + } |
| 120 | + cache := newDetectionCache() |
| 121 | + cache.loadOnce() // corrupt file must not panic or pollute |
| 122 | + if _, fresh := cache.get("anything"); fresh { |
| 123 | + t.Fatal("corrupt cache file should leave nothing fresh") |
| 124 | + } |
| 125 | +} |
0 commit comments