main pico / pkg / apps / pgs / web_cache_test.go
Antonio Mika  ·  2026-10-09
 1package pgs
 2
 3import (
 4	"slices"
 5	"testing"
 6	"time"
 7)
 8
 9func TestSiteCachePurgeSite(t *testing.T) {
10	evicted := []string{}
11	c := NewSiteCache(0, time.Minute, func(key string, _ []byte) { evicted = append(evicted, key) })
12	keys := []string{
13		"antonio-blog__GET__/",
14		"antonio-blog__GET__/about",
15		"antonio-blog2__GET__/",
16		"antonio-blog-v2__GET__/",
17	}
18	for _, key := range keys {
19		c.Add(key, []byte("x"))
20	}
21
22	c.PurgeSite("antonio-blog")
23	got := c.Keys()
24	slices.Sort(got)
25	want := []string{"antonio-blog-v2__GET__/", "antonio-blog2__GET__/"}
26	if !slices.Equal(got, want) {
27		t.Fatalf("after purge got %q, want %q", got, want)
28	}
29	if len(evicted) != 2 {
30		t.Fatalf("eviction callback ran %d times, want 2", len(evicted))
31	}
32
33	// Entries that leave the cache on their own leave the index too.
34	c.Remove("antonio-blog2__GET__/")
35	if _, ok := c.sites["antonio-blog2"]; ok {
36		t.Fatal("removed entry still indexed")
37	}
38	c.Purge()
39	if len(c.sites) != 0 {
40		t.Fatalf("index holds %d sites after a full purge", len(c.sites))
41	}
42}