package db import ( "context" "github.com/redis/go-redis/v9" "testing" ) type budgetRedis struct { redis.UniversalClient calls int batch []string } func (c *budgetRedis) Scan(ctx context.Context, cursor uint64, match string, count int64) *redis.ScanCmd { c.calls++ cmd := redis.NewScanCmd(ctx, nil, "scan") cmd.SetVal(c.batch, uint64(c.calls)) return cmd } func TestRedisScanBudgetSparseMatches(t *testing.T) { c := &budgetRedis{} names, truncated, err := scanAll(context.Background(), c, "missing*", 200, 500, 20) if err != nil || !truncated || len(names) != 0 || c.calls != 20 { t.Fatalf("calls=%d names=%v truncated=%v err=%v", c.calls, names, truncated, err) } } func TestRedisScanCountIsNotHardLimit(t *testing.T) { c := &budgetRedis{batch: []string{"a", "b", "c"}} names, truncated, err := scanAll(context.Background(), c, "*", 1, 2, 20) if err != nil || !truncated || len(names) != 2 || c.calls != 1 { t.Fatalf("names=%v calls=%d truncated=%v err=%v", names, c.calls, truncated, err) } } func TestRedisBrowsePattern(t *testing.T) { for input, want := range map[string]string{"order": "*order*", "order:*": "order:*", "*": "*"} { if got := redisBrowsePattern(input); got != want { t.Fatalf("input=%q got=%q want=%q", input, got, want) } } }