File size: 12,592 Bytes
8059bf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
//go:build integration

package repository

import (
	"context"
	"fmt"
	"testing"
	"time"

	"github.com/Wei-Shaw/sub2api/internal/service"
	"github.com/redis/go-redis/v9"
	"github.com/stretchr/testify/require"
	"github.com/stretchr/testify/suite"
)

type BillingCacheSuite struct {
	IntegrationRedisSuite
}

func (s *BillingCacheSuite) TestUserBalance() {
	tests := []struct {
		name string
		fn   func(ctx context.Context, rdb *redis.Client, cache service.BillingCache)
	}{
		{
			name: "missing_key_returns_redis_nil",
			fn: func(ctx context.Context, rdb *redis.Client, cache service.BillingCache) {
				_, err := cache.GetUserBalance(ctx, 1)
				require.ErrorIs(s.T(), err, redis.Nil, "expected redis.Nil for missing balance key")
			},
		},
		{
			name: "deduct_on_nonexistent_is_noop",
			fn: func(ctx context.Context, rdb *redis.Client, cache service.BillingCache) {
				userID := int64(1)
				balanceKey := fmt.Sprintf("%s%d", billingBalanceKeyPrefix, userID)

				require.NoError(s.T(), cache.DeductUserBalance(ctx, userID, 1), "DeductUserBalance should not error")

				_, err := rdb.Get(ctx, balanceKey).Result()
				require.ErrorIs(s.T(), err, redis.Nil, "expected missing key after deduct on non-existent")
			},
		},
		{
			name: "set_and_get_with_ttl",
			fn: func(ctx context.Context, rdb *redis.Client, cache service.BillingCache) {
				userID := int64(2)
				balanceKey := fmt.Sprintf("%s%d", billingBalanceKeyPrefix, userID)

				require.NoError(s.T(), cache.SetUserBalance(ctx, userID, 10.5), "SetUserBalance")

				got, err := cache.GetUserBalance(ctx, userID)
				require.NoError(s.T(), err, "GetUserBalance")
				require.Equal(s.T(), 10.5, got, "balance mismatch")

				ttl, err := rdb.TTL(ctx, balanceKey).Result()
				require.NoError(s.T(), err, "TTL")
				s.AssertTTLWithin(ttl, 1*time.Second, billingCacheTTL)
			},
		},
		{
			name: "deduct_reduces_balance",
			fn: func(ctx context.Context, rdb *redis.Client, cache service.BillingCache) {
				userID := int64(3)

				require.NoError(s.T(), cache.SetUserBalance(ctx, userID, 10.5), "SetUserBalance")
				require.NoError(s.T(), cache.DeductUserBalance(ctx, userID, 2.25), "DeductUserBalance")

				got, err := cache.GetUserBalance(ctx, userID)
				require.NoError(s.T(), err, "GetUserBalance after deduct")
				require.Equal(s.T(), 8.25, got, "deduct mismatch")
			},
		},
		{
			name: "invalidate_removes_key",
			fn: func(ctx context.Context, rdb *redis.Client, cache service.BillingCache) {
				userID := int64(100)
				balanceKey := fmt.Sprintf("%s%d", billingBalanceKeyPrefix, userID)

				require.NoError(s.T(), cache.SetUserBalance(ctx, userID, 50.0), "SetUserBalance")

				exists, err := rdb.Exists(ctx, balanceKey).Result()
				require.NoError(s.T(), err, "Exists")
				require.Equal(s.T(), int64(1), exists, "expected balance key to exist")

				require.NoError(s.T(), cache.InvalidateUserBalance(ctx, userID), "InvalidateUserBalance")

				exists, err = rdb.Exists(ctx, balanceKey).Result()
				require.NoError(s.T(), err, "Exists after invalidate")
				require.Equal(s.T(), int64(0), exists, "expected balance key to be removed after invalidate")

				_, err = cache.GetUserBalance(ctx, userID)
				require.ErrorIs(s.T(), err, redis.Nil, "expected redis.Nil after invalidate")
			},
		},
		{
			name: "deduct_refreshes_ttl",
			fn: func(ctx context.Context, rdb *redis.Client, cache service.BillingCache) {
				userID := int64(103)
				balanceKey := fmt.Sprintf("%s%d", billingBalanceKeyPrefix, userID)

				require.NoError(s.T(), cache.SetUserBalance(ctx, userID, 100.0), "SetUserBalance")

				ttl1, err := rdb.TTL(ctx, balanceKey).Result()
				require.NoError(s.T(), err, "TTL before deduct")
				s.AssertTTLWithin(ttl1, 1*time.Second, billingCacheTTL)

				require.NoError(s.T(), cache.DeductUserBalance(ctx, userID, 25.0), "DeductUserBalance")

				balance, err := cache.GetUserBalance(ctx, userID)
				require.NoError(s.T(), err, "GetUserBalance")
				require.Equal(s.T(), 75.0, balance, "expected balance 75.0")

				ttl2, err := rdb.TTL(ctx, balanceKey).Result()
				require.NoError(s.T(), err, "TTL after deduct")
				s.AssertTTLWithin(ttl2, 1*time.Second, billingCacheTTL)
			},
		},
	}

	for _, tt := range tests {
		s.Run(tt.name, func() {
			rdb := testRedis(s.T())
			cache := NewBillingCache(rdb)
			ctx := context.Background()

			tt.fn(ctx, rdb, cache)
		})
	}
}

func (s *BillingCacheSuite) TestSubscriptionCache() {
	tests := []struct {
		name string
		fn   func(ctx context.Context, rdb *redis.Client, cache service.BillingCache)
	}{
		{
			name: "missing_key_returns_redis_nil",
			fn: func(ctx context.Context, rdb *redis.Client, cache service.BillingCache) {
				userID := int64(10)
				groupID := int64(20)

				_, err := cache.GetSubscriptionCache(ctx, userID, groupID)
				require.ErrorIs(s.T(), err, redis.Nil, "expected redis.Nil for missing subscription key")
			},
		},
		{
			name: "update_usage_on_nonexistent_is_noop",
			fn: func(ctx context.Context, rdb *redis.Client, cache service.BillingCache) {
				userID := int64(11)
				groupID := int64(21)
				subKey := fmt.Sprintf("%s%d:%d", billingSubKeyPrefix, userID, groupID)

				require.NoError(s.T(), cache.UpdateSubscriptionUsage(ctx, userID, groupID, 1.0), "UpdateSubscriptionUsage should not error")

				exists, err := rdb.Exists(ctx, subKey).Result()
				require.NoError(s.T(), err, "Exists")
				require.Equal(s.T(), int64(0), exists, "expected missing subscription key after UpdateSubscriptionUsage on non-existent")
			},
		},
		{
			name: "set_and_get_with_ttl",
			fn: func(ctx context.Context, rdb *redis.Client, cache service.BillingCache) {
				userID := int64(12)
				groupID := int64(22)
				subKey := fmt.Sprintf("%s%d:%d", billingSubKeyPrefix, userID, groupID)

				data := &service.SubscriptionCacheData{
					Status:       "active",
					ExpiresAt:    time.Now().Add(1 * time.Hour),
					DailyUsage:   1.0,
					WeeklyUsage:  2.0,
					MonthlyUsage: 3.0,
					Version:      7,
				}
				require.NoError(s.T(), cache.SetSubscriptionCache(ctx, userID, groupID, data), "SetSubscriptionCache")

				gotSub, err := cache.GetSubscriptionCache(ctx, userID, groupID)
				require.NoError(s.T(), err, "GetSubscriptionCache")
				require.Equal(s.T(), "active", gotSub.Status)
				require.Equal(s.T(), int64(7), gotSub.Version)
				require.Equal(s.T(), 1.0, gotSub.DailyUsage)

				ttl, err := rdb.TTL(ctx, subKey).Result()
				require.NoError(s.T(), err, "TTL subKey")
				s.AssertTTLWithin(ttl, 1*time.Second, billingCacheTTL)
			},
		},
		{
			name: "update_usage_increments_all_fields",
			fn: func(ctx context.Context, rdb *redis.Client, cache service.BillingCache) {
				userID := int64(13)
				groupID := int64(23)

				data := &service.SubscriptionCacheData{
					Status:       "active",
					ExpiresAt:    time.Now().Add(1 * time.Hour),
					DailyUsage:   1.0,
					WeeklyUsage:  2.0,
					MonthlyUsage: 3.0,
					Version:      1,
				}
				require.NoError(s.T(), cache.SetSubscriptionCache(ctx, userID, groupID, data), "SetSubscriptionCache")

				require.NoError(s.T(), cache.UpdateSubscriptionUsage(ctx, userID, groupID, 0.5), "UpdateSubscriptionUsage")

				gotSub, err := cache.GetSubscriptionCache(ctx, userID, groupID)
				require.NoError(s.T(), err, "GetSubscriptionCache after update")
				require.Equal(s.T(), 1.5, gotSub.DailyUsage)
				require.Equal(s.T(), 2.5, gotSub.WeeklyUsage)
				require.Equal(s.T(), 3.5, gotSub.MonthlyUsage)
			},
		},
		{
			name: "invalidate_removes_key",
			fn: func(ctx context.Context, rdb *redis.Client, cache service.BillingCache) {
				userID := int64(101)
				groupID := int64(10)
				subKey := fmt.Sprintf("%s%d:%d", billingSubKeyPrefix, userID, groupID)

				data := &service.SubscriptionCacheData{
					Status:       "active",
					ExpiresAt:    time.Now().Add(1 * time.Hour),
					DailyUsage:   1.0,
					WeeklyUsage:  2.0,
					MonthlyUsage: 3.0,
					Version:      1,
				}
				require.NoError(s.T(), cache.SetSubscriptionCache(ctx, userID, groupID, data), "SetSubscriptionCache")

				exists, err := rdb.Exists(ctx, subKey).Result()
				require.NoError(s.T(), err, "Exists")
				require.Equal(s.T(), int64(1), exists, "expected subscription key to exist")

				require.NoError(s.T(), cache.InvalidateSubscriptionCache(ctx, userID, groupID), "InvalidateSubscriptionCache")

				exists, err = rdb.Exists(ctx, subKey).Result()
				require.NoError(s.T(), err, "Exists after invalidate")
				require.Equal(s.T(), int64(0), exists, "expected subscription key to be removed after invalidate")

				_, err = cache.GetSubscriptionCache(ctx, userID, groupID)
				require.ErrorIs(s.T(), err, redis.Nil, "expected redis.Nil after invalidate")
			},
		},
		{
			name: "missing_status_returns_parsing_error",
			fn: func(ctx context.Context, rdb *redis.Client, cache service.BillingCache) {
				userID := int64(102)
				groupID := int64(11)
				subKey := fmt.Sprintf("%s%d:%d", billingSubKeyPrefix, userID, groupID)

				fields := map[string]any{
					"expires_at":    time.Now().Add(1 * time.Hour).Unix(),
					"daily_usage":   1.0,
					"weekly_usage":  2.0,
					"monthly_usage": 3.0,
					"version":       1,
				}
				require.NoError(s.T(), rdb.HSet(ctx, subKey, fields).Err(), "HSet")

				_, err := cache.GetSubscriptionCache(ctx, userID, groupID)
				require.Error(s.T(), err, "expected error for missing status field")
				require.NotErrorIs(s.T(), err, redis.Nil, "expected parsing error, not redis.Nil")
				require.Equal(s.T(), "invalid cache: missing status", err.Error())
			},
		},
	}

	for _, tt := range tests {
		s.Run(tt.name, func() {
			rdb := testRedis(s.T())
			cache := NewBillingCache(rdb)
			ctx := context.Background()

			tt.fn(ctx, rdb, cache)
		})
	}
}

// TestDeductUserBalance_ErrorPropagation 验证 P2-12 修复:
// Redis 真实错误应传播,key 不存在(redis.Nil)应返回 nil。
func (s *BillingCacheSuite) TestDeductUserBalance_ErrorPropagation() {
	tests := []struct {
		name      string
		fn        func(ctx context.Context, cache service.BillingCache)
		expectErr bool
	}{
		{
			name: "key_not_exists_returns_nil",
			fn: func(ctx context.Context, cache service.BillingCache) {
				// key 不存在时,Lua 脚本返回 0(redis.Nil),应返回 nil 而非错误
				err := cache.DeductUserBalance(ctx, 99999, 1.0)
				require.NoError(s.T(), err, "DeductUserBalance on non-existent key should return nil")
			},
		},
		{
			name: "existing_key_deducts_successfully",
			fn: func(ctx context.Context, cache service.BillingCache) {
				require.NoError(s.T(), cache.SetUserBalance(ctx, 200, 50.0))
				err := cache.DeductUserBalance(ctx, 200, 10.0)
				require.NoError(s.T(), err, "DeductUserBalance should succeed")

				bal, err := cache.GetUserBalance(ctx, 200)
				require.NoError(s.T(), err)
				require.Equal(s.T(), 40.0, bal, "余额应为 40.0")
			},
		},
		{
			name: "cancelled_context_propagates_error",
			fn: func(ctx context.Context, cache service.BillingCache) {
				require.NoError(s.T(), cache.SetUserBalance(ctx, 201, 50.0))

				cancelCtx, cancel := context.WithCancel(ctx)
				cancel() // 立即取消

				err := cache.DeductUserBalance(cancelCtx, 201, 10.0)
				require.Error(s.T(), err, "cancelled context should propagate error")
			},
		},
	}

	for _, tt := range tests {
		s.Run(tt.name, func() {
			rdb := testRedis(s.T())
			cache := NewBillingCache(rdb)
			ctx := context.Background()
			tt.fn(ctx, cache)
		})
	}
}

// TestUpdateSubscriptionUsage_ErrorPropagation 验证 P2-12 修复:
// Redis 真实错误应传播,key 不存在(redis.Nil)应返回 nil。
func (s *BillingCacheSuite) TestUpdateSubscriptionUsage_ErrorPropagation() {
	s.Run("key_not_exists_returns_nil", func() {
		rdb := testRedis(s.T())
		cache := NewBillingCache(rdb)
		ctx := context.Background()

		err := cache.UpdateSubscriptionUsage(ctx, 88888, 77777, 1.0)
		require.NoError(s.T(), err, "UpdateSubscriptionUsage on non-existent key should return nil")
	})

	s.Run("cancelled_context_propagates_error", func() {
		rdb := testRedis(s.T())
		cache := NewBillingCache(rdb)
		ctx := context.Background()

		data := &service.SubscriptionCacheData{
			Status:    "active",
			ExpiresAt: time.Now().Add(1 * time.Hour),
			Version:   1,
		}
		require.NoError(s.T(), cache.SetSubscriptionCache(ctx, 301, 401, data))

		cancelCtx, cancel := context.WithCancel(ctx)
		cancel()

		err := cache.UpdateSubscriptionUsage(cancelCtx, 301, 401, 1.0)
		require.Error(s.T(), err, "cancelled context should propagate error")
	})
}

func TestBillingCacheSuite(t *testing.T) {
	suite.Run(t, new(BillingCacheSuite))
}