95 lines
No EOL
2.5 KiB
Go
95 lines
No EOL
2.5 KiB
Go
package cache
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/metamorphosis-dev/samsa/internal/contracts"
|
|
)
|
|
|
|
func TestEngineCacheGetSet(t *testing.T) {
|
|
// Create a disabled cache for unit testing (nil client)
|
|
c := &Cache{logger: slog.Default()}
|
|
ec := NewEngineCache(c, nil)
|
|
|
|
ctx := context.Background()
|
|
cached, ok := ec.Get(ctx, "wikipedia", "abc123")
|
|
if ok {
|
|
t.Errorf("Get on disabled cache: expected false, got %v", ok)
|
|
}
|
|
_ = cached // unused when ok=false
|
|
}
|
|
|
|
func TestEngineCacheKeyFormat(t *testing.T) {
|
|
key := engineCacheKey("wikipedia", "abc123")
|
|
if key != "samsa:resp:wikipedia:abc123" {
|
|
t.Errorf("engineCacheKey: expected samsa:resp:wikipedia:abc123, got %s", key)
|
|
}
|
|
}
|
|
|
|
func TestEngineCacheIsStale(t *testing.T) {
|
|
c := &Cache{logger: slog.Default()}
|
|
ec := NewEngineCache(c, nil)
|
|
|
|
// Fresh response (stored 1 minute ago, wikipedia has 24h TTL)
|
|
fresh := CachedEngineResponse{
|
|
Engine: "wikipedia",
|
|
Response: []byte(`{}`),
|
|
StoredAt: time.Now().Add(-1 * time.Minute),
|
|
}
|
|
if ec.IsStale(fresh, "wikipedia") {
|
|
t.Errorf("IsStale: 1-minute-old wikipedia should NOT be stale")
|
|
}
|
|
|
|
// Stale response (stored 25 hours ago)
|
|
stale := CachedEngineResponse{
|
|
Engine: "wikipedia",
|
|
Response: []byte(`{}`),
|
|
StoredAt: time.Now().Add(-25 * time.Hour),
|
|
}
|
|
if !ec.IsStale(stale, "wikipedia") {
|
|
t.Errorf("IsStale: 25-hour-old wikipedia SHOULD be stale (24h TTL)")
|
|
}
|
|
|
|
// Override: 30 minute TTL for reddit
|
|
overrides := map[string]time.Duration{"reddit": 30 * time.Minute}
|
|
ec2 := NewEngineCache(c, overrides)
|
|
|
|
// 20 minutes old with 30m override should NOT be stale
|
|
redditFresh := CachedEngineResponse{
|
|
Engine: "reddit",
|
|
Response: []byte(`{}`),
|
|
StoredAt: time.Now().Add(-20 * time.Minute),
|
|
}
|
|
if ec2.IsStale(redditFresh, "reddit") {
|
|
t.Errorf("IsStale: 20-min reddit with 30m override should NOT be stale")
|
|
}
|
|
|
|
// 45 minutes old with 30m override SHOULD be stale
|
|
redditStale := CachedEngineResponse{
|
|
Engine: "reddit",
|
|
Response: []byte(`{}`),
|
|
StoredAt: time.Now().Add(-45 * time.Minute),
|
|
}
|
|
if !ec2.IsStale(redditStale, "reddit") {
|
|
t.Errorf("IsStale: 45-min reddit with 30m override SHOULD be stale")
|
|
}
|
|
}
|
|
|
|
func TestEngineCacheSetResponseType(t *testing.T) {
|
|
c := &Cache{logger: slog.Default()}
|
|
ec := NewEngineCache(c, nil)
|
|
|
|
ctx := context.Background()
|
|
urlStr := "https://example.com"
|
|
resp := contracts.SearchResponse{
|
|
Results: []contracts.MainResult{
|
|
{Title: "Test", URL: &urlStr},
|
|
},
|
|
}
|
|
|
|
// Should not panic on disabled cache
|
|
ec.Set(ctx, "wikipedia", "abc123", resp)
|
|
} |