package session

import (
	"testing"

	"github.com/google/uuid"
)

// TestCache exercises the write-through cache's add/get/remove/size/ready cycle.
func TestCache(t *testing.T) {
	c := NewCache()
	if c.Ready() {
		t.Error("a fresh cache must not report ready")
	}
	if _, ok := c.Get("h1"); ok {
		t.Error("get on empty cache must miss")
	}

	s := Session{ID: uuid.New(), AccountID: uuid.New(), TokenHash: "h1", Status: StatusActive}
	c.Add(s)
	if got, ok := c.Get("h1"); !ok || got.ID != s.ID {
		t.Fatalf("get after add: got %v ok=%v", got.ID, ok)
	}
	if c.Size() != 1 {
		t.Errorf("size = %d, want 1", c.Size())
	}

	c.Remove("h1")
	if _, ok := c.Get("h1"); ok {
		t.Error("get after remove must miss")
	}
	if c.Size() != 0 {
		t.Errorf("size = %d, want 0", c.Size())
	}
}

// TestCacheNilSafe checks that the cache methods are safe on a nil receiver,
// which the readiness probe relies on before the cache is constructed.
func TestCacheNilSafe(t *testing.T) {
	var c *Cache
	if c.Ready() {
		t.Error("nil cache must not be ready")
	}
	if _, ok := c.Get("x"); ok {
		t.Error("nil cache get must miss")
	}
	if c.Size() != 0 {
		t.Error("nil cache size must be 0")
	}
	c.Add(Session{TokenHash: "x"}) // must not panic
	c.Remove("x")                  // must not panic
}
