初始化提交
This commit is contained in:
commit
887c1f449e
963 files changed
+166869
No files matched your search
+122
@@ -0,0 +1,122 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/chenxin/hunjing/mcp/web-search/internal/config"
|
||||
"github.com/chenxin/hunjing/mcp/web-search/internal/models"
|
||||
)
|
||||
|
||||
func TestCache_SetAndGet(t *testing.T) {
|
||||
m, err := New(config.CacheConfig{
|
||||
Enabled: true,
|
||||
TTL: 5 * time.Minute,
|
||||
MaxItems: 100,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
req := &models.SearchRequest{Query: "test", MaxResults: 5}
|
||||
result := &models.SearchResult{Query: "test", TotalCount: 1}
|
||||
|
||||
m.Set(req, models.ProviderTavily, result)
|
||||
got := m.Get(req, models.ProviderTavily)
|
||||
if got == nil {
|
||||
t.Fatal("expected cached result")
|
||||
}
|
||||
if !got.Cached {
|
||||
t.Error("expected cached=true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCache_Miss(t *testing.T) {
|
||||
m, err := New(config.CacheConfig{
|
||||
Enabled: true,
|
||||
TTL: 5 * time.Minute,
|
||||
MaxItems: 100,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
got := m.Get(&models.SearchRequest{Query: "nosuch"}, models.ProviderTavily)
|
||||
if got != nil {
|
||||
t.Fatal("expected nil for cache miss")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCache_Disabled(t *testing.T) {
|
||||
m, err := New(config.CacheConfig{Enabled: false})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
m.Set(&models.SearchRequest{Query: "test"}, models.ProviderTavily, &models.SearchResult{})
|
||||
got := m.Get(&models.SearchRequest{Query: "test"}, models.ProviderTavily)
|
||||
if got != nil {
|
||||
t.Fatal("expected nil when cache disabled")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCache_Expired(t *testing.T) {
|
||||
m, err := New(config.CacheConfig{
|
||||
Enabled: true,
|
||||
TTL: -1 * time.Second, // 立即过期
|
||||
MaxItems: 100,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
m.Set(&models.SearchRequest{Query: "test"}, models.ProviderTavily, &models.SearchResult{})
|
||||
got := m.Get(&models.SearchRequest{Query: "test"}, models.ProviderTavily)
|
||||
if got != nil {
|
||||
t.Fatal("expected nil for expired entry")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCache_Stats(t *testing.T) {
|
||||
m, err := New(config.CacheConfig{
|
||||
Enabled: true,
|
||||
TTL: 5 * time.Minute,
|
||||
MaxItems: 100,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
// Miss then hit
|
||||
m.Get(&models.SearchRequest{Query: "miss"}, models.ProviderTavily)
|
||||
m.Set(&models.SearchRequest{Query: "hit"}, models.ProviderTavily, &models.SearchResult{})
|
||||
m.Get(&models.SearchRequest{Query: "hit"}, models.ProviderTavily)
|
||||
|
||||
l1Hits, l2Hits, misses := m.Stats()
|
||||
if l1Hits != 1 {
|
||||
t.Errorf("expected 1 L1 hit, got %d", l1Hits)
|
||||
}
|
||||
if l2Hits != 0 {
|
||||
t.Errorf("expected 0 L2 hits, got %d", l2Hits)
|
||||
}
|
||||
if misses != 1 {
|
||||
t.Errorf("expected 1 miss, got %d", misses)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCache_ProviderIsolation(t *testing.T) {
|
||||
m, err := New(config.CacheConfig{
|
||||
Enabled: true,
|
||||
TTL: 5 * time.Minute,
|
||||
MaxItems: 100,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
req := &models.SearchRequest{Query: "test", MaxResults: 5}
|
||||
m.Set(req, models.ProviderTavily, &models.SearchResult{Query: "test"})
|
||||
|
||||
// Same query, different provider should miss
|
||||
got := m.Get(req, models.ProviderBrave)
|
||||
if got != nil {
|
||||
t.Fatal("expected miss for different provider")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user