|
| 1 | +package management |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/gin-gonic/gin" |
| 10 | + "github.com/router-for-me/CLIProxyAPI/v6/internal/redisqueue" |
| 11 | +) |
| 12 | + |
| 13 | +func TestGetUsageQueuePopsRequestedRecords(t *testing.T) { |
| 14 | + gin.SetMode(gin.TestMode) |
| 15 | + withManagementUsageQueue(t, func() { |
| 16 | + redisqueue.Enqueue([]byte(`{"id":1}`)) |
| 17 | + redisqueue.Enqueue([]byte(`{"id":2}`)) |
| 18 | + redisqueue.Enqueue([]byte(`{"id":3}`)) |
| 19 | + |
| 20 | + rec := httptest.NewRecorder() |
| 21 | + ginCtx, _ := gin.CreateTestContext(rec) |
| 22 | + ginCtx.Request = httptest.NewRequest(http.MethodGet, "/v0/management/usage-queue?count=2", nil) |
| 23 | + |
| 24 | + h := &Handler{} |
| 25 | + h.GetUsageQueue(ginCtx) |
| 26 | + |
| 27 | + if rec.Code != http.StatusOK { |
| 28 | + t.Fatalf("status = %d, want %d body=%s", rec.Code, http.StatusOK, rec.Body.String()) |
| 29 | + } |
| 30 | + |
| 31 | + var payload []json.RawMessage |
| 32 | + if errUnmarshal := json.Unmarshal(rec.Body.Bytes(), &payload); errUnmarshal != nil { |
| 33 | + t.Fatalf("unmarshal response: %v", errUnmarshal) |
| 34 | + } |
| 35 | + if len(payload) != 2 { |
| 36 | + t.Fatalf("response records = %d, want 2", len(payload)) |
| 37 | + } |
| 38 | + requireRecordID(t, payload[0], 1) |
| 39 | + requireRecordID(t, payload[1], 2) |
| 40 | + |
| 41 | + remaining := redisqueue.PopOldest(10) |
| 42 | + if len(remaining) != 1 || string(remaining[0]) != `{"id":3}` { |
| 43 | + t.Fatalf("remaining queue = %q, want third item only", remaining) |
| 44 | + } |
| 45 | + }) |
| 46 | +} |
| 47 | + |
| 48 | +func TestGetUsageQueueInvalidCountDoesNotPop(t *testing.T) { |
| 49 | + gin.SetMode(gin.TestMode) |
| 50 | + withManagementUsageQueue(t, func() { |
| 51 | + redisqueue.Enqueue([]byte(`{"id":1}`)) |
| 52 | + |
| 53 | + rec := httptest.NewRecorder() |
| 54 | + ginCtx, _ := gin.CreateTestContext(rec) |
| 55 | + ginCtx.Request = httptest.NewRequest(http.MethodGet, "/v0/management/usage-queue?count=0", nil) |
| 56 | + |
| 57 | + h := &Handler{} |
| 58 | + h.GetUsageQueue(ginCtx) |
| 59 | + |
| 60 | + if rec.Code != http.StatusBadRequest { |
| 61 | + t.Fatalf("status = %d, want %d body=%s", rec.Code, http.StatusBadRequest, rec.Body.String()) |
| 62 | + } |
| 63 | + |
| 64 | + remaining := redisqueue.PopOldest(10) |
| 65 | + if len(remaining) != 1 || string(remaining[0]) != `{"id":1}` { |
| 66 | + t.Fatalf("remaining queue = %q, want original item", remaining) |
| 67 | + } |
| 68 | + }) |
| 69 | +} |
| 70 | + |
| 71 | +func withManagementUsageQueue(t *testing.T, fn func()) { |
| 72 | + t.Helper() |
| 73 | + |
| 74 | + prevQueueEnabled := redisqueue.Enabled() |
| 75 | + redisqueue.SetEnabled(false) |
| 76 | + redisqueue.SetEnabled(true) |
| 77 | + |
| 78 | + defer func() { |
| 79 | + redisqueue.SetEnabled(false) |
| 80 | + redisqueue.SetEnabled(prevQueueEnabled) |
| 81 | + }() |
| 82 | + |
| 83 | + fn() |
| 84 | +} |
| 85 | + |
| 86 | +func requireRecordID(t *testing.T, raw json.RawMessage, want int) { |
| 87 | + t.Helper() |
| 88 | + |
| 89 | + var payload struct { |
| 90 | + ID int `json:"id"` |
| 91 | + } |
| 92 | + if errUnmarshal := json.Unmarshal(raw, &payload); errUnmarshal != nil { |
| 93 | + t.Fatalf("unmarshal record: %v", errUnmarshal) |
| 94 | + } |
| 95 | + if payload.ID != want { |
| 96 | + t.Fatalf("record id = %d, want %d", payload.ID, want) |
| 97 | + } |
| 98 | +} |
0 commit comments