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

Function TestCachedTool_Execute

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

Source from the content-addressed store, hash-verified

373}
374
375func TestCachedTool_Execute(t *testing.T) {
376 config := &CacheConfig{
377 Enabled: true,
378 Strategy: CacheStrategyMemory,
379 TTL: 1 * time.Hour,
380 }
381
382 cache := NewToolCache(config)
383
384 // 创建 mock 工具
385 mockTool := &MockTool{
386 name: "test_tool",
387 description: "Test tool",
388 }
389
390 // 创建带缓存的工具
391 cachedTool := NewCachedTool(mockTool, cache)
392
393 ctx := context.Background()
394 input := map[string]any{"test": "value"}
395
396 // 第一次执行
397 result1, err := cachedTool.Execute(ctx, input, nil)
398 if err != nil {
399 t.Fatalf("Failed to execute: %v", err)
400 }
401
402 if mockTool.callCount != 1 {
403 t.Errorf("Expected 1 call, got: %d", mockTool.callCount)
404 }
405
406 // 第二次执行(应该使用缓存)
407 result2, err := cachedTool.Execute(ctx, input, nil)
408 if err != nil {
409 t.Fatalf("Failed to execute: %v", err)
410 }
411
412 if mockTool.callCount != 1 {
413 t.Errorf("Expected 1 call (cached), got: %d", mockTool.callCount)
414 }
415
416 // 验证结果相同
417 result1Map := result1.(map[string]any)
418 result2Map := result2.(map[string]any)
419
420 if result1Map["result"] != result2Map["result"] {
421 t.Error("Expected same result from cache")
422 }
423
424 // 检查缓存统计
425 stats := cache.GetStats()
426 if stats.Hits != 1 {
427 t.Errorf("Expected 1 cache hit, got: %d", stats.Hits)
428 }
429}
430
431func TestCachedTool_DifferentInputs(t *testing.T) {
432 config := &CacheConfig{

Callers

nothing calls this directly

Calls 5

ExecuteMethod · 0.95
GetStatsMethod · 0.95
NewToolCacheFunction · 0.85
NewCachedToolFunction · 0.85
ErrorMethod · 0.65

Tested by

no test coverage detected