From 2aa39d7a1115edc77aaf81671a3d65aa6b79a37e Mon Sep 17 00:00:00 2001 From: liangxu Date: Wed, 29 Apr 2026 22:08:08 +0800 Subject: [PATCH] test(ops/kol-subscription): cover cache invalidation on admin actions Verify approve/revoke/manual-bind delete the right tenant-scoped keys via a recording cache double, so the prompt cache stays consistent with the KOL subscription state changes shipped in the previous commit. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../ops/app/kol_subscription_cache_test.go | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 server/internal/ops/app/kol_subscription_cache_test.go diff --git a/server/internal/ops/app/kol_subscription_cache_test.go b/server/internal/ops/app/kol_subscription_cache_test.go new file mode 100644 index 0000000..d7c96ae --- /dev/null +++ b/server/internal/ops/app/kol_subscription_cache_test.go @@ -0,0 +1,52 @@ +package app + +import ( + "context" + "reflect" + "testing" + "time" + + sharedcache "github.com/geo-platform/tenant-api/internal/shared/cache" +) + +type recordingCache struct { + deletedKeys []string + deletedPrefixes []string +} + +func (c *recordingCache) Get(context.Context, string) ([]byte, error) { + return nil, sharedcache.ErrNotFound +} + +func (c *recordingCache) Set(context.Context, string, []byte, time.Duration) error { + return nil +} + +func (c *recordingCache) Delete(_ context.Context, key string) error { + c.deletedKeys = append(c.deletedKeys, key) + return nil +} + +func (c *recordingCache) DeletePrefix(_ context.Context, prefix string) error { + c.deletedPrefixes = append(c.deletedPrefixes, prefix) + return nil +} + +func TestKolSubscriptionInvalidateAccessCaches(t *testing.T) { + cache := &recordingCache{} + svc := NewKolSubscriptionService(nil, nil).WithCache(cache) + + svc.invalidateAccessCaches(context.Background(), 42) + + if want := []string{"workspace:kol_cards:42"}; !reflect.DeepEqual(cache.deletedKeys, want) { + t.Fatalf("deleted keys = %v, want %v", cache.deletedKeys, want) + } + + wantPrefixes := []string{ + "kol:subscription_prompt:list:42:", + "kol:subscription_prompt:schema:42:", + } + if !reflect.DeepEqual(cache.deletedPrefixes, wantPrefixes) { + t.Fatalf("deleted prefixes = %v, want %v", cache.deletedPrefixes, wantPrefixes) + } +}