|
| 1 | +import { describe, test, expect } from "vitest"; |
| 2 | +import { mf, mfUrl } from "./mf"; |
| 3 | + |
| 4 | +describe("rate limit", () => { |
| 5 | + test("basic rate limit check", async () => { |
| 6 | + const resp = await mf.dispatchFetch(`${mfUrl}rate-limit/check`); |
| 7 | + expect(resp.status).toBe(200); |
| 8 | + const data = await resp.json() as { success: boolean }; |
| 9 | + expect(data).toHaveProperty("success"); |
| 10 | + expect(data.success).toBe(true); |
| 11 | + }); |
| 12 | + |
| 13 | + test("rate limit with custom key", async () => { |
| 14 | + const key = "test-key-123"; |
| 15 | + const resp = await mf.dispatchFetch(`${mfUrl}rate-limit/key/${key}`); |
| 16 | + expect(resp.status).toBe(200); |
| 17 | + const data = await resp.json() as { success: boolean; key: string }; |
| 18 | + expect(data).toHaveProperty("success"); |
| 19 | + expect(data).toHaveProperty("key"); |
| 20 | + expect(data.key).toBe(key); |
| 21 | + expect(data.success).toBe(true); |
| 22 | + }); |
| 23 | + |
| 24 | + test("different keys have independent limits", async () => { |
| 25 | + // Test that different keys have separate rate limits |
| 26 | + const key1 = "user-1"; |
| 27 | + const key2 = "user-2"; |
| 28 | + |
| 29 | + const resp1 = await mf.dispatchFetch(`${mfUrl}rate-limit/key/${key1}`); |
| 30 | + const resp2 = await mf.dispatchFetch(`${mfUrl}rate-limit/key/${key2}`); |
| 31 | + |
| 32 | + expect(resp1.status).toBe(200); |
| 33 | + expect(resp2.status).toBe(200); |
| 34 | + |
| 35 | + const data1 = await resp1.json() as { success: boolean; key: string }; |
| 36 | + const data2 = await resp2.json() as { success: boolean; key: string }; |
| 37 | + |
| 38 | + expect(data1.success).toBe(true); |
| 39 | + expect(data2.success).toBe(true); |
| 40 | + expect(data1.key).toBe(key1); |
| 41 | + expect(data2.key).toBe(key2); |
| 42 | + }); |
| 43 | + |
| 44 | + test("bulk rate limit test", async () => { |
| 45 | + const resp = await mf.dispatchFetch(`${mfUrl}rate-limit/bulk-test`); |
| 46 | + expect(resp.status).toBe(200); |
| 47 | + const data = await resp.json() as { results: Array<{ index: number; key: string; success: boolean }> }; |
| 48 | + expect(data).toHaveProperty("results"); |
| 49 | + expect(Array.isArray(data.results)).toBe(true); |
| 50 | + expect(data.results.length).toBe(15); |
| 51 | + |
| 52 | + // Check that results have the expected structure |
| 53 | + data.results.forEach((result, index: number) => { |
| 54 | + expect(result).toHaveProperty("index"); |
| 55 | + expect(result).toHaveProperty("key"); |
| 56 | + expect(result).toHaveProperty("success"); |
| 57 | + expect(result.index).toBe(index); |
| 58 | + expect(typeof result.success).toBe("boolean"); |
| 59 | + }); |
| 60 | + |
| 61 | + // We're using 3 different keys (bulk-test-0, bulk-test-1, bulk-test-2) |
| 62 | + // with a limit of 10 per 60 seconds. Each key is used 5 times (15 requests / 3 keys). |
| 63 | + // All requests should succeed since each key stays under the limit of 10. |
| 64 | + |
| 65 | + // Group results by key |
| 66 | + const resultsByKey: Record<string, Array<{ index: number; key: string; success: boolean }>> = {}; |
| 67 | + data.results.forEach((result) => { |
| 68 | + if (!resultsByKey[result.key]) { |
| 69 | + resultsByKey[result.key] = []; |
| 70 | + } |
| 71 | + resultsByKey[result.key].push(result); |
| 72 | + }); |
| 73 | + |
| 74 | + // Should have exactly 3 keys |
| 75 | + expect(Object.keys(resultsByKey).length).toBe(3); |
| 76 | + |
| 77 | + // Each key should have 5 requests, all successful (under limit of 10) |
| 78 | + Object.entries(resultsByKey).forEach(([key, results]) => { |
| 79 | + expect(results.length).toBe(5); |
| 80 | + results.forEach((result) => { |
| 81 | + expect(result.success).toBe(true); |
| 82 | + }); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + test("rate limit reset with unique keys", async () => { |
| 87 | + const resp = await mf.dispatchFetch(`${mfUrl}rate-limit/reset`); |
| 88 | + expect(resp.status).toBe(200); |
| 89 | + const data = await resp.json() as Record<string, boolean>; |
| 90 | + |
| 91 | + // Should have 12 request results |
| 92 | + expect(Object.keys(data).length).toBe(12); |
| 93 | + |
| 94 | + // Check that we have the expected keys |
| 95 | + for (let i = 1; i <= 12; i++) { |
| 96 | + expect(data).toHaveProperty(`request_${i}`); |
| 97 | + expect(typeof data[`request_${i}`]).toBe("boolean"); |
| 98 | + } |
| 99 | + |
| 100 | + // With a limit of 10 per 60 seconds, the first 10 requests MUST succeed |
| 101 | + // and requests 11 and 12 MUST fail |
| 102 | + for (let i = 1; i <= 10; i++) { |
| 103 | + expect(data[`request_${i}`]).toBe(true); |
| 104 | + } |
| 105 | + |
| 106 | + // Requests 11 and 12 must be rate limited |
| 107 | + expect(data["request_11"]).toBe(false); |
| 108 | + expect(data["request_12"]).toBe(false); |
| 109 | + }); |
| 110 | + |
| 111 | + test("multiple rapid requests with same key", async () => { |
| 112 | + // Generate a unique key for this test |
| 113 | + const testKey = `rapid-test-${Date.now()}`; |
| 114 | + |
| 115 | + // Make multiple rapid requests with the same key |
| 116 | + const promises = []; |
| 117 | + for (let i = 0; i < 5; i++) { |
| 118 | + promises.push(mf.dispatchFetch(`${mfUrl}rate-limit/key/${testKey}`)); |
| 119 | + } |
| 120 | + |
| 121 | + const responses = await Promise.all(promises); |
| 122 | + |
| 123 | + // All responses should be successful (200 status) |
| 124 | + responses.forEach(resp => { |
| 125 | + expect(resp.status).toBe(200); |
| 126 | + }); |
| 127 | + |
| 128 | + // Parse the responses |
| 129 | + const results = await Promise.all(responses.map(r => r.json())) as Array<{ success: boolean; key: string }>; |
| 130 | + |
| 131 | + // All should have the same key |
| 132 | + results.forEach(data => { |
| 133 | + expect(data.key).toBe(testKey); |
| 134 | + expect(data).toHaveProperty("success"); |
| 135 | + }); |
| 136 | + |
| 137 | + // With limit of 10, all 5 requests should succeed |
| 138 | + results.forEach((data) => { |
| 139 | + expect(data.success).toBe(true); |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + test("sequential requests enforce rate limit", async () => { |
| 144 | + // Generate a unique key for this test to avoid interference |
| 145 | + const testKey = `sequential-test-${Date.now()}`; |
| 146 | + |
| 147 | + // Make 15 sequential requests with the same key |
| 148 | + // With a limit of 10 per 60 seconds, first 10 should succeed, rest should fail |
| 149 | + const results: Array<{ success: boolean; key: string }> = []; |
| 150 | + for (let i = 0; i < 15; i++) { |
| 151 | + const resp = await mf.dispatchFetch(`${mfUrl}rate-limit/key/${testKey}`); |
| 152 | + expect(resp.status).toBe(200); |
| 153 | + const data = await resp.json() as { success: boolean; key: string }; |
| 154 | + results.push(data); |
| 155 | + } |
| 156 | + |
| 157 | + // Verify first 10 requests succeed |
| 158 | + for (let i = 0; i < 10; i++) { |
| 159 | + expect(results[i].success).toBe(true); |
| 160 | + expect(results[i].key).toBe(testKey); |
| 161 | + } |
| 162 | + |
| 163 | + // Verify requests 11-15 are rate limited |
| 164 | + for (let i = 10; i < 15; i++) { |
| 165 | + expect(results[i].success).toBe(false); |
| 166 | + expect(results[i].key).toBe(testKey); |
| 167 | + } |
| 168 | + }); |
| 169 | +}); |
0 commit comments