package query
import (
"testing"
"time"
)
func TestResponseCache_GetSetAndEvict(t *testing.T) {
// Cache with max 2 entries
cache := NewResponseCache(2)
entry1 := CacheEntry{
ContentType: "text/plain",
Body: []byte("one"),
StatusCode: 200,
ExpiresAt: time.Now().Add(time.Minute),
ETag: "\"1\"",
}
entry2 := CacheEntry{
ContentType: "text/plain",
Body: []byte("two"),
StatusCode: 200,
ExpiresAt: time.Now().Add(time.Minute),
ETag: "\"2\"",
}
entry3 := CacheEntry{
ContentType: "text/plain",
Body: []byte("three"),
StatusCode: 200,
ExpiresAt: time.Now().Add(time.Minute),
ETag: "\"3\"",
}
cache.Set("k1", entry1)
cache.Set("k2", entry2)
// Hit k1
got1, ok := cache.Get("k1")
if !ok || string(got1.Body) != "one" {
t.Fatalf("expected hit on k1, got %v", got1)
}
// Insert k3 -> should evict k2 (since k1 was recently accessed)
cache.Set("k3", entry3)
// k2 must be evicted
_, ok = cache.Get("k2")
if ok {
t.Fatalf("expected k2 to be evicted")
}
// k1 and k3 must be present
got1, ok = cache.Get("k1")
if !ok || string(got1.Body) != "one" {
t.Fatalf("expected k1 to be present")
}
got3, ok := cache.Get("k3")
if !ok || string(got3.Body) != "three" {
t.Fatalf("expected k3 to be present")
}
stats := cache.Stats()
if stats.Hits < 2 || stats.Evictions != 1 || stats.Items != 2 {
t.Errorf("unexpected stats: %+v", stats)
}
}
func TestResponseCache_Expiry(t *testing.T) {
cache := NewResponseCache(10)
entry := CacheEntry{
ContentType: "text/plain",
Body: []byte("ephemeral"),
StatusCode: 200,
ExpiresAt: time.Now().Add(10 * time.Millisecond),
}
cache.Set("short", entry)
// Immediately present
got, ok := cache.Get("short")
if !ok || string(got.Body) != "ephemeral" {
t.Fatalf("expected immediate hit")
}
// Sleep past expiration
time.Sleep(20 * time.Millisecond)
// Must be expired
_, ok = cache.Get("short")
if ok {
t.Fatalf("expected entry to be expired")
}
stats := cache.Stats()
if stats.Items != 0 {
t.Errorf("expected 0 items after expiry removal, got %d", stats.Items)
}
}
func TestResponseCache_ComputeQuantization(t *testing.T) {
now := int64(1700000000000)
// 1. Rolling 1-year query ending at 'now'
yearMs := int64(365 * 24 * 3600 * 1000)
qFrom, qTo, ttl, isRolling := computeQuantization(now-yearMs, now, now)
if !isRolling {
t.Errorf("expected rolling query for now")
}
// Interval is 1 hour = 3600000 ms
if qTo%3600000 != 0 {
t.Errorf("expected qTo aligned to 1 hour, got %d", qTo)
}
if qTo-qFrom != yearMs {
t.Errorf("expected window preserved, got %d vs %d", qTo-qFrom, yearMs)
}
if ttl <= 0 {
t.Errorf("expected positive TTL, got %v", ttl)
}
// 2. Rolling 1-day query ending at 'now'
dayMs := int64(24 * 3600 * 1000)
qFrom, qTo, ttl, isRolling = computeQuantization(now-dayMs, now, now)
if !isRolling {
t.Errorf("expected rolling query for now")
}
// Interval is 1 minute = 60000 ms
if qTo%60000 != 0 {
t.Errorf("expected qTo aligned to 1 minute, got %d", qTo)
}
if qTo-qFrom != dayMs {
t.Errorf("expected window preserved, got %d vs %d", qTo-qFrom, dayMs)
}
if ttl <= 0 {
t.Errorf("expected positive TTL, got %v", ttl)
}
// 3. Snapped 7-day query ending at start of hour
weekMs := int64(7 * 24 * 3600 * 1000)
snappedTo := (now / 3600000) * 3600000
qFrom, qTo, ttl, isRolling = computeQuantization(snappedTo-weekMs, snappedTo, now)
if !isRolling {
t.Errorf("expected rolling query for snapped 7-day query")
}
if qTo != snappedTo {
t.Errorf("expected qTo to match snappedTo %d, got %d", snappedTo, qTo)
}
if qFrom != snappedTo-weekMs {
t.Errorf("expected qFrom to match snappedFrom %d, got %d", snappedTo-weekMs, qFrom)
}
if ttl <= 0 {
t.Errorf("expected positive TTL, got %v", ttl)
}
// 4. Repeated query 17 minutes later with the same snapped range produces identical cache key
nowLater := now + 17*60*1000
qFromLater, qToLater, _, isRollingLater := computeQuantization(snappedTo-weekMs, snappedTo, nowLater)
if !isRollingLater {
t.Errorf("expected rolling query 17m later within 2h window")
}
if qToLater != snappedTo || qFromLater != snappedTo-weekMs {
t.Errorf("expected identical cache key 17m later: [%d, %d] vs [%d, %d]", qFromLater, qToLater, snappedTo-weekMs, snappedTo)
}
// 5. Fixed historical query (10 days in the past)
pastTo := now - (10 * 24 * 3600 * 1000)
pastFrom := pastTo - dayMs
qFrom, qTo, ttl, isRolling = computeQuantization(pastFrom, pastTo, now)
if isRolling {
t.Errorf("expected historical query, not rolling")
}
if qFrom != pastFrom || qTo != pastTo {
t.Errorf("historical timestamps should not be quantized: got [%d, %d], want [%d, %d]", qFrom, qTo, pastFrom, pastTo)
}
if ttl != 1*time.Hour {
t.Errorf("expected 1h TTL for historical query, got %v", ttl)
}
}
func TestComputeETag(t *testing.T) {
b1 := []byte("")
b2 := []byte("")
etag1 := computeETag(b1)
etag1Repeat := computeETag(b1)
etag2 := computeETag(b2)
if etag1 != etag1Repeat {
t.Errorf("ETags for identical payload must match")
}
if etag1 == etag2 {
t.Errorf("ETags for different payloads must differ")
}
}