package cache_test import ( "testing" "time" "anchorage/internal/pkg/cache" ) func TestCacheRoundTrip(t *testing.T) { c, err := cache.New[string, int](cache.Options{ Name: "test", MaxCost: 1 << 20, // 1 MiB DefaultTTL: time.Minute, }) if err != nil { t.Fatalf("New: %v", err) } defer c.Close() if !c.Set("a", 42, 1, 0) { t.Fatal("Set returned false for fresh key") } c.Wait() v, ok := c.Get("a") if !ok { t.Fatal("Get missed after Set") } if v != 42 { t.Errorf("Get = %d, want 42", v) } c.Delete("a") c.Wait() if _, ok := c.Get("a"); ok { t.Error("Get hit after Delete") } } func TestCacheRejectsBadConfig(t *testing.T) { _, err := cache.New[string, string](cache.Options{Name: "bad", MaxCost: 0}) if err == nil { t.Error("expected error for MaxCost=0") } } func TestNameExposed(t *testing.T) { c, _ := cache.New[string, int](cache.Options{Name: "pin/byid", MaxCost: 1 << 16}) defer c.Close() if c.Name() != "pin/byid" { t.Errorf("Name = %q", c.Name()) } } func TestInvalidatorNilIsNoop(t *testing.T) { var inv *cache.Invalidator if err := inv.Emit("org", "org_xyz"); err != nil { t.Errorf("Emit on nil Invalidator should be a no-op, got %v", err) } }