109 lines
3 KiB
Go
109 lines
3 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 }
|
|
|
|
func TestQueryHash(t *testing.T) {
|
|
// Same params should produce same hash
|
|
hash1 := QueryHash("golang", 1, 0, "en", "")
|
|
hash2 := QueryHash("golang", 1, 0, "en", "")
|
|
if hash1 != hash2 {
|
|
t.Errorf("QueryHash: same params should produce same hash, got %s != %s", hash1, hash2)
|
|
}
|
|
|
|
// Different query should produce different hash
|
|
hash3 := QueryHash("rust", 1, 0, "en", "")
|
|
if hash1 == hash3 {
|
|
t.Errorf("QueryHash: different queries should produce different hash")
|
|
}
|
|
|
|
// Different pageno should produce different hash
|
|
hash4 := QueryHash("golang", 2, 0, "en", "")
|
|
if hash1 == hash4 {
|
|
t.Errorf("QueryHash: different pageno should produce different hash")
|
|
}
|
|
|
|
// time_range should affect hash
|
|
hash5 := QueryHash("golang", 1, 0, "en", "day")
|
|
if hash1 == hash5 {
|
|
t.Errorf("QueryHash: different time_range should produce different hash")
|
|
}
|
|
|
|
// Hash should be 16 characters (truncated SHA-256)
|
|
if len(hash1) != 16 {
|
|
t.Errorf("QueryHash: expected 16 char hash, got %d", len(hash1))
|
|
}
|
|
}
|