|
| 1 | +package admin |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "sync/atomic" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/Wei-Shaw/sub2api/internal/pkg/usagestats" |
| 12 | + "github.com/Wei-Shaw/sub2api/internal/service" |
| 13 | + "github.com/gin-gonic/gin" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +type dashboardUsageRepoCacheProbe struct { |
| 18 | + service.UsageLogRepository |
| 19 | + trendCalls atomic.Int32 |
| 20 | + usersTrendCalls atomic.Int32 |
| 21 | +} |
| 22 | + |
| 23 | +func (r *dashboardUsageRepoCacheProbe) GetUsageTrendWithFilters( |
| 24 | + ctx context.Context, |
| 25 | + startTime, endTime time.Time, |
| 26 | + granularity string, |
| 27 | + userID, apiKeyID, accountID, groupID int64, |
| 28 | + model string, |
| 29 | + requestType *int16, |
| 30 | + stream *bool, |
| 31 | + billingType *int8, |
| 32 | +) ([]usagestats.TrendDataPoint, error) { |
| 33 | + r.trendCalls.Add(1) |
| 34 | + return []usagestats.TrendDataPoint{{ |
| 35 | + Date: "2026-03-11", |
| 36 | + Requests: 1, |
| 37 | + TotalTokens: 2, |
| 38 | + Cost: 3, |
| 39 | + ActualCost: 4, |
| 40 | + }}, nil |
| 41 | +} |
| 42 | + |
| 43 | +func (r *dashboardUsageRepoCacheProbe) GetUserUsageTrend( |
| 44 | + ctx context.Context, |
| 45 | + startTime, endTime time.Time, |
| 46 | + granularity string, |
| 47 | + limit int, |
| 48 | +) ([]usagestats.UserUsageTrendPoint, error) { |
| 49 | + r.usersTrendCalls.Add(1) |
| 50 | + return []usagestats.UserUsageTrendPoint{{ |
| 51 | + Date: "2026-03-11", |
| 52 | + UserID: 1, |
| 53 | + Email: "cache@test.dev", |
| 54 | + Requests: 2, |
| 55 | + Tokens: 20, |
| 56 | + Cost: 2, |
| 57 | + ActualCost: 1, |
| 58 | + }}, nil |
| 59 | +} |
| 60 | + |
| 61 | +func resetDashboardReadCachesForTest() { |
| 62 | + dashboardTrendCache = newSnapshotCache(30 * time.Second) |
| 63 | + dashboardUsersTrendCache = newSnapshotCache(30 * time.Second) |
| 64 | + dashboardAPIKeysTrendCache = newSnapshotCache(30 * time.Second) |
| 65 | + dashboardModelStatsCache = newSnapshotCache(30 * time.Second) |
| 66 | + dashboardGroupStatsCache = newSnapshotCache(30 * time.Second) |
| 67 | + dashboardSnapshotV2Cache = newSnapshotCache(30 * time.Second) |
| 68 | +} |
| 69 | + |
| 70 | +func TestDashboardHandler_GetUsageTrend_UsesCache(t *testing.T) { |
| 71 | + t.Cleanup(resetDashboardReadCachesForTest) |
| 72 | + resetDashboardReadCachesForTest() |
| 73 | + |
| 74 | + gin.SetMode(gin.TestMode) |
| 75 | + repo := &dashboardUsageRepoCacheProbe{} |
| 76 | + dashboardSvc := service.NewDashboardService(repo, nil, nil, nil) |
| 77 | + handler := NewDashboardHandler(dashboardSvc, nil) |
| 78 | + router := gin.New() |
| 79 | + router.GET("/admin/dashboard/trend", handler.GetUsageTrend) |
| 80 | + |
| 81 | + req1 := httptest.NewRequest(http.MethodGet, "/admin/dashboard/trend?start_date=2026-03-01&end_date=2026-03-07&granularity=day", nil) |
| 82 | + rec1 := httptest.NewRecorder() |
| 83 | + router.ServeHTTP(rec1, req1) |
| 84 | + require.Equal(t, http.StatusOK, rec1.Code) |
| 85 | + require.Equal(t, "miss", rec1.Header().Get("X-Snapshot-Cache")) |
| 86 | + |
| 87 | + req2 := httptest.NewRequest(http.MethodGet, "/admin/dashboard/trend?start_date=2026-03-01&end_date=2026-03-07&granularity=day", nil) |
| 88 | + rec2 := httptest.NewRecorder() |
| 89 | + router.ServeHTTP(rec2, req2) |
| 90 | + require.Equal(t, http.StatusOK, rec2.Code) |
| 91 | + require.Equal(t, "hit", rec2.Header().Get("X-Snapshot-Cache")) |
| 92 | + require.Equal(t, int32(1), repo.trendCalls.Load()) |
| 93 | +} |
| 94 | + |
| 95 | +func TestDashboardHandler_GetUserUsageTrend_UsesCache(t *testing.T) { |
| 96 | + t.Cleanup(resetDashboardReadCachesForTest) |
| 97 | + resetDashboardReadCachesForTest() |
| 98 | + |
| 99 | + gin.SetMode(gin.TestMode) |
| 100 | + repo := &dashboardUsageRepoCacheProbe{} |
| 101 | + dashboardSvc := service.NewDashboardService(repo, nil, nil, nil) |
| 102 | + handler := NewDashboardHandler(dashboardSvc, nil) |
| 103 | + router := gin.New() |
| 104 | + router.GET("/admin/dashboard/users-trend", handler.GetUserUsageTrend) |
| 105 | + |
| 106 | + req1 := httptest.NewRequest(http.MethodGet, "/admin/dashboard/users-trend?start_date=2026-03-01&end_date=2026-03-07&granularity=day&limit=8", nil) |
| 107 | + rec1 := httptest.NewRecorder() |
| 108 | + router.ServeHTTP(rec1, req1) |
| 109 | + require.Equal(t, http.StatusOK, rec1.Code) |
| 110 | + require.Equal(t, "miss", rec1.Header().Get("X-Snapshot-Cache")) |
| 111 | + |
| 112 | + req2 := httptest.NewRequest(http.MethodGet, "/admin/dashboard/users-trend?start_date=2026-03-01&end_date=2026-03-07&granularity=day&limit=8", nil) |
| 113 | + rec2 := httptest.NewRecorder() |
| 114 | + router.ServeHTTP(rec2, req2) |
| 115 | + require.Equal(t, http.StatusOK, rec2.Code) |
| 116 | + require.Equal(t, "hit", rec2.Header().Get("X-Snapshot-Cache")) |
| 117 | + require.Equal(t, int32(1), repo.usersTrendCalls.Load()) |
| 118 | +} |
0 commit comments