refactor(management): rename GetUsage to GetUsageQueue and update routes/tests

- Renamed handler and test methods for better clarity on functionality.
- Updated route from `/v0/management/usage` to `/v0/management/usage-queue`.
- Adjusted integration and unit tests to reflect new naming and routes.
This commit is contained in:
Luis Pater
2026-05-05 03:02:25 +08:00
parent 61b39d49bd
commit da6c599efd
4 changed files with 19 additions and 11 deletions
+2 -2
View File
@@ -20,8 +20,8 @@ func (r usageQueueRecord) MarshalJSON() ([]byte, error) {
return json.Marshal(string(r))
}
// GetUsage pops queued usage records from the Redis-compatible usage queue.
func (h *Handler) GetUsage(c *gin.Context) {
// GetUsageQueue pops queued usage records from the usage queue.
func (h *Handler) GetUsageQueue(c *gin.Context) {
if h == nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "handler unavailable"})
return
@@ -10,7 +10,7 @@ import (
"github.com/router-for-me/CLIProxyAPI/v6/internal/redisqueue"
)
func TestGetUsagePopsRequestedRecords(t *testing.T) {
func TestGetUsageQueuePopsRequestedRecords(t *testing.T) {
gin.SetMode(gin.TestMode)
withManagementUsageQueue(t, func() {
redisqueue.Enqueue([]byte(`{"id":1}`))
@@ -19,10 +19,10 @@ func TestGetUsagePopsRequestedRecords(t *testing.T) {
rec := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(rec)
ginCtx.Request = httptest.NewRequest(http.MethodGet, "/v0/management/usage?count=2", nil)
ginCtx.Request = httptest.NewRequest(http.MethodGet, "/v0/management/usage-queue?count=2", nil)
h := &Handler{}
h.GetUsage(ginCtx)
h.GetUsageQueue(ginCtx)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want %d body=%s", rec.Code, http.StatusOK, rec.Body.String())
@@ -45,17 +45,17 @@ func TestGetUsagePopsRequestedRecords(t *testing.T) {
})
}
func TestGetUsageInvalidCountDoesNotPop(t *testing.T) {
func TestGetUsageQueueInvalidCountDoesNotPop(t *testing.T) {
gin.SetMode(gin.TestMode)
withManagementUsageQueue(t, func() {
redisqueue.Enqueue([]byte(`{"id":1}`))
rec := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(rec)
ginCtx.Request = httptest.NewRequest(http.MethodGet, "/v0/management/usage?count=0", nil)
ginCtx.Request = httptest.NewRequest(http.MethodGet, "/v0/management/usage-queue?count=0", nil)
h := &Handler{}
h.GetUsage(ginCtx)
h.GetUsageQueue(ginCtx)
if rec.Code != http.StatusBadRequest {
t.Fatalf("status = %d, want %d body=%s", rec.Code, http.StatusBadRequest, rec.Body.String())
+1 -1
View File
@@ -551,7 +551,7 @@ func (s *Server) registerManagementRoutes() {
mgmt.PATCH("/api-keys", s.mgmt.PatchAPIKeys)
mgmt.DELETE("/api-keys", s.mgmt.DeleteAPIKeys)
mgmt.GET("/api-key-usage", s.mgmt.GetAPIKeyUsage)
mgmt.GET("/usage", s.mgmt.GetUsage)
mgmt.GET("/usage-queue", s.mgmt.GetUsageQueue)
mgmt.GET("/gemini-api-key", s.mgmt.GetGeminiKeys)
mgmt.PUT("/gemini-api-key", s.mgmt.PutGeminiKeys)
+10 -2
View File
@@ -100,14 +100,22 @@ func TestManagementUsageRequiresManagementAuthAndPopsArray(t *testing.T) {
redisqueue.Enqueue([]byte(`{"id":1}`))
redisqueue.Enqueue([]byte(`{"id":2}`))
missingKeyReq := httptest.NewRequest(http.MethodGet, "/v0/management/usage?count=2", nil)
missingKeyReq := httptest.NewRequest(http.MethodGet, "/v0/management/usage-queue?count=2", nil)
missingKeyRR := httptest.NewRecorder()
server.engine.ServeHTTP(missingKeyRR, missingKeyReq)
if missingKeyRR.Code != http.StatusUnauthorized {
t.Fatalf("missing key status = %d, want %d body=%s", missingKeyRR.Code, http.StatusUnauthorized, missingKeyRR.Body.String())
}
authReq := httptest.NewRequest(http.MethodGet, "/v0/management/usage?count=2", nil)
legacyReq := httptest.NewRequest(http.MethodGet, "/v0/management/usage?count=2", nil)
legacyReq.Header.Set("Authorization", "Bearer test-management-key")
legacyRR := httptest.NewRecorder()
server.engine.ServeHTTP(legacyRR, legacyReq)
if legacyRR.Code != http.StatusNotFound {
t.Fatalf("legacy usage status = %d, want %d body=%s", legacyRR.Code, http.StatusNotFound, legacyRR.Body.String())
}
authReq := httptest.NewRequest(http.MethodGet, "/v0/management/usage-queue?count=2", nil)
authReq.Header.Set("Authorization", "Bearer test-management-key")
authRR := httptest.NewRecorder()
server.engine.ServeHTTP(authRR, authReq)