|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const mocks = vi.hoisted(() => ({ |
| 4 | + consumeLocalRateLimit: vi.fn(), |
| 5 | + generateSubscriptionYaml: vi.fn(), |
| 6 | + hashLocalRateLimitKey: vi.fn(() => "token-hash"), |
| 7 | + localRateLimitResponse: vi.fn( |
| 8 | + () => new Response(JSON.stringify({ error: "limited", code: "RATE_LIMITED" }), { status: 429 }) |
| 9 | + ), |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock("@local/lib/rate-limit", () => ({ |
| 13 | + consumeLocalRateLimit: mocks.consumeLocalRateLimit, |
| 14 | + hashLocalRateLimitKey: mocks.hashLocalRateLimitKey, |
| 15 | + localRateLimitResponse: mocks.localRateLimitResponse, |
| 16 | +})); |
| 17 | +vi.mock("@local/lib/subscription-service", () => ({ |
| 18 | + generateSubscriptionYaml: mocks.generateSubscriptionYaml, |
| 19 | +})); |
| 20 | + |
| 21 | +import { GET } from "./route"; |
| 22 | + |
| 23 | +describe("local subscription YAML route", () => { |
| 24 | + beforeEach(() => { |
| 25 | + vi.clearAllMocks(); |
| 26 | + mocks.consumeLocalRateLimit.mockReturnValue({ allowed: true, retryAfterSeconds: 0 }); |
| 27 | + mocks.generateSubscriptionYaml.mockResolvedValue({ |
| 28 | + yaml: "mixed-port: 7890\n", |
| 29 | + name: "Test", |
| 30 | + subscriptionInfo: {}, |
| 31 | + cacheExpirySeconds: 3600, |
| 32 | + autoUpdateIntervalSeconds: null, |
| 33 | + isAdmin: true, |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + it("applies global and per-token limits before generating YAML", async () => { |
| 38 | + const response = await GET(new Request("https://local.test/config.yaml"), { |
| 39 | + params: Promise.resolve({ id: "secret-token" }), |
| 40 | + }); |
| 41 | + |
| 42 | + expect(response.status).toBe(200); |
| 43 | + expect(mocks.hashLocalRateLimitKey).toHaveBeenCalledWith("secret-token"); |
| 44 | + expect(mocks.consumeLocalRateLimit).toHaveBeenNthCalledWith( |
| 45 | + 1, |
| 46 | + "subscription-yaml-global", |
| 47 | + "all", |
| 48 | + { limit: 600, windowMs: 60_000 } |
| 49 | + ); |
| 50 | + expect(mocks.consumeLocalRateLimit).toHaveBeenNthCalledWith( |
| 51 | + 2, |
| 52 | + "subscription-yaml-token", |
| 53 | + "token-hash", |
| 54 | + { limit: 120, windowMs: 60_000 } |
| 55 | + ); |
| 56 | + expect(mocks.generateSubscriptionYaml).toHaveBeenCalledWith("secret-token"); |
| 57 | + }); |
| 58 | + |
| 59 | + it("returns 429 before touching subscription data", async () => { |
| 60 | + mocks.consumeLocalRateLimit.mockReturnValueOnce({ allowed: false, retryAfterSeconds: 17 }); |
| 61 | + |
| 62 | + const response = await GET(new Request("https://local.test/config.yaml"), { |
| 63 | + params: Promise.resolve({ id: "secret-token" }), |
| 64 | + }); |
| 65 | + |
| 66 | + expect(response.status).toBe(429); |
| 67 | + expect(mocks.localRateLimitResponse).toHaveBeenCalledWith( |
| 68 | + "Too many subscription requests. Try again later.", |
| 69 | + 17 |
| 70 | + ); |
| 71 | + expect(mocks.generateSubscriptionYaml).not.toHaveBeenCalled(); |
| 72 | + }); |
| 73 | +}); |
0 commit comments