(t *testing.T)
| 134 | } |
| 135 | |
| 136 | func TestToolCache_FileCache(t *testing.T) { |
| 137 | // 创建临时目录 |
| 138 | tmpDir := t.TempDir() |
| 139 | |
| 140 | config := &CacheConfig{ |
| 141 | Enabled: true, |
| 142 | Strategy: CacheStrategyFile, |
| 143 | TTL: 1 * time.Hour, |
| 144 | CacheDir: tmpDir, |
| 145 | } |
| 146 | |
| 147 | cache := NewToolCache(config) |
| 148 | ctx := context.Background() |
| 149 | key := "test_key" |
| 150 | value := map[string]any{"data": "test"} |
| 151 | |
| 152 | // 设置缓存 |
| 153 | err := cache.Set(ctx, key, value, config.TTL) |
| 154 | if err != nil { |
| 155 | t.Fatalf("Failed to set cache: %v", err) |
| 156 | } |
| 157 | |
| 158 | // 获取缓存 |
| 159 | cached, ok := cache.Get(ctx, key) |
| 160 | if !ok { |
| 161 | t.Fatal("Expected cache hit") |
| 162 | } |
| 163 | |
| 164 | cachedMap, ok := cached.(map[string]any) |
| 165 | if !ok { |
| 166 | t.Fatal("Expected map[string]any") |
| 167 | } |
| 168 | |
| 169 | if cachedMap["data"] != "test" { |
| 170 | t.Errorf("Expected 'test', got: %v", cachedMap["data"]) |
| 171 | } |
| 172 | |
| 173 | // 验证文件存在 |
| 174 | filePath := cache.getCacheFilePath(key) |
| 175 | if _, err := os.Stat(filePath); os.IsNotExist(err) { |
| 176 | t.Fatal("Cache file should exist") |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | func TestToolCache_BothStrategy(t *testing.T) { |
| 181 | tmpDir := t.TempDir() |
nothing calls this directly
no test coverage detected