samsa/internal/cache/cache_test.go
Franz Kafka 8e9aae062b
Some checks failed
Build and Push Docker Image / build-and-push (push) Failing after 11s
Mirror to GitHub / mirror (push) Failing after 5s
Tests / test (push) Successful in 42s
rename: kafka → samsa
Full project rename from kafka to samsa (after Gregor Samsa, who
woke one morning from uneasy dreams to find himself transformed).

- Module: github.com/metamorphosis-dev/kafka → samsa
- Binary: cmd/kafka/ → cmd/samsa/
- CSS: kafka.css → samsa.css
- UI: all 'kafka' product names, titles, localStorage keys → samsa
- localStorage keys: kafka-theme → samsa-theme, kafka-engines → samsa-engines
- OpenSearch: ShortName, LongName, description, URLs updated
- AGPL headers: 'kafka' → 'samsa'
- Docs, configs, examples updated
- Cache key prefix: kafka: → samsa:
2026-03-22 23:44:55 +00:00

77 lines
2 KiB
Go

package cache
import (
"testing"
"github.com/metamorphosis-dev/samsa/internal/contracts"
)
func TestKey_Deterministic(t *testing.T) {
req := contracts.SearchRequest{
Format: contracts.FormatJSON,
Query: "samsa metamorphosis",
Pageno: 1,
Safesearch: 0,
Language: "auto",
Engines: []string{"wikipedia", "braveapi"},
Categories: []string{"general"},
}
key1 := Key(req)
key2 := Key(req)
if key1 != key2 {
t.Errorf("Key should be deterministic: %q != %q", key1, key2)
}
if len(key1) != 32 {
t.Errorf("expected 32-char key, got %d", len(key1))
}
}
func TestKey_DifferentQueries(t *testing.T) {
reqA := contracts.SearchRequest{Query: "samsa", Format: contracts.FormatJSON}
reqB := contracts.SearchRequest{Query: "orwell", Format: contracts.FormatJSON}
if Key(reqA) == Key(reqB) {
t.Error("different queries should produce different keys")
}
}
func TestKey_DifferentPageno(t *testing.T) {
req1 := contracts.SearchRequest{Query: "test", Pageno: 1}
req2 := contracts.SearchRequest{Query: "test", Pageno: 2}
if Key(req1) == Key(req2) {
t.Error("different pageno should produce different keys")
}
}
func TestKey_DifferentEngines(t *testing.T) {
req1 := contracts.SearchRequest{Query: "test", Engines: []string{"wikipedia"}}
req2 := contracts.SearchRequest{Query: "test", Engines: []string{"braveapi"}}
if Key(req1) == Key(req2) {
t.Error("different engines should produce different keys")
}
}
func TestKey_TimeRange(t *testing.T) {
req1 := contracts.SearchRequest{Query: "test"}
req2 := contracts.SearchRequest{Query: "test", TimeRange: strPtr("week")}
if Key(req1) == Key(req2) {
t.Error("with/without time_range should produce different keys")
}
}
func TestNew_NopWithoutAddress(t *testing.T) {
c := New(Config{}, nil)
if c.Enabled() {
t.Error("cache should be disabled when no address is configured")
}
if err := c.Close(); err != nil {
t.Errorf("Close on nop cache should not error: %v", err)
}
}
func strPtr(s string) *string { return &s }