package cache

import (
	"testing"
)

func TestNormalize(t *testing.T) {
	tests := []struct{ input, want string }{
		{"  Hello  World  ", "hello world"},
		{"你好", "你好"},
		{"  你好  呀  ", "你好 呀"},
		{"OK", "ok"},
	}
	for _, tt := range tests {
		got := normalize(tt.input)
		if got != tt.want {
			t.Errorf("normalize(%q) = %q, want %q", tt.input, got, tt.want)
		}
	}
}

func TestIsGreeting(t *testing.T) {
	if !isGreeting("你好") {
		t.Error("'你好' should be a greeting")
	}
	if !isGreeting("hello") {
		t.Error("'hello' should be a greeting")
	}
	if isGreeting("今天天气真好") {
		t.Error("'今天天气真好' should NOT be a greeting")
	}
	if isGreeting("帮我开灯") {
		t.Error("'帮我开灯' should NOT be a greeting")
	}
}

func TestCacheHit(t *testing.T) {
	c := New()
	c.Set("你好呀", "你好呀，开拓者♪ 今天有什么想聊的吗？")

	got, ok := c.Get("你好呀")
	if !ok {
		t.Fatal("expected cache hit")
	}
	if got != "你好呀，开拓者♪ 今天有什么想聊的吗？" {
		t.Errorf("cached response mismatch: %q", got)
	}
}

func TestCacheMiss(t *testing.T) {
	c := New()
	_, ok := c.Get("从未说过的话")
	if ok {
		t.Error("expected cache miss")
	}
}

func TestCacheNormalization(t *testing.T) {
	c := New()
	c.Set("  你好  ", "回复内容")

	// Normalized key should match
	_, ok := c.Get("你好")
	if !ok {
		t.Error("normalized key should produce cache hit")
	}
}

func TestCacheEviction(t *testing.T) {
	c := New()
	c.maxEntries = 3
	c.Set("a", "A")
	c.Set("b", "B")
	c.Set("c", "C")
	c.Set("d", "D") // should evict the oldest

	if c.Size() > 3 {
		t.Errorf("cache should be <= 3 entries, got %d", c.Size())
	}
}

func TestInvalidate(t *testing.T) {
	c := New()
	c.Set("test", "value")
	c.Invalidate()
	if c.Size() != 0 {
		t.Errorf("cache should be empty after invalidate, got %d", c.Size())
	}
}
