MCPcopy Create free account
hub / github.com/astercloud/aster / TestToolCache_FileCache

Function TestToolCache_FileCache

pkg/tools/cache_test.go:136–178  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

134}
135
136func 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
180func TestToolCache_BothStrategy(t *testing.T) {
181 tmpDir := t.TempDir()

Callers

nothing calls this directly

Calls 5

SetMethod · 0.95
GetMethod · 0.95
getCacheFilePathMethod · 0.95
NewToolCacheFunction · 0.85
StatMethod · 0.65

Tested by

no test coverage detected