|
| 1 | +import "@databuddy/test/env"; |
| 2 | +import { Elysia } from "elysia"; |
| 3 | +import { describe, expect, it, vi } from "vitest"; |
| 4 | + |
| 5 | +const state = vi.hoisted(() => ({ |
| 6 | + flags: [ |
| 7 | + { |
| 8 | + defaultValue: true, |
| 9 | + dependencies: null, |
| 10 | + flagsToTargetGroups: [], |
| 11 | + key: "enabled-for-everyone", |
| 12 | + payload: null, |
| 13 | + rolloutBy: null, |
| 14 | + rolloutPercentage: null, |
| 15 | + rules: null, |
| 16 | + status: "active", |
| 17 | + type: "boolean", |
| 18 | + variants: null, |
| 19 | + }, |
| 20 | + ], |
| 21 | +})); |
| 22 | + |
| 23 | +vi.mock("@databuddy/db", async (importOriginal) => ({ |
| 24 | + ...(await importOriginal<typeof import("@databuddy/db")>()), |
| 25 | + db: { |
| 26 | + query: { |
| 27 | + flags: { |
| 28 | + findMany: vi.fn(async () => state.flags), |
| 29 | + }, |
| 30 | + }, |
| 31 | + }, |
| 32 | +})); |
| 33 | + |
| 34 | +vi.mock("@databuddy/redis", async (importOriginal) => ({ |
| 35 | + ...(await importOriginal<typeof import("@databuddy/redis")>()), |
| 36 | + cacheable: (fn: (...args: never[]) => unknown) => fn, |
| 37 | +})); |
| 38 | + |
| 39 | +vi.mock("@databuddy/redis/rate-limit", () => ({ |
| 40 | + getRateLimitHeaders: () => ({}), |
| 41 | + ratelimit: async () => ({ success: true }), |
| 42 | +})); |
| 43 | + |
| 44 | +const { flagsRoute } = await import("./flags"); |
| 45 | +const app = new Elysia().use(flagsRoute); |
| 46 | + |
| 47 | +function request(path: string, body?: unknown) { |
| 48 | + return app.handle( |
| 49 | + new Request(`http://localhost${path}`, { |
| 50 | + body: body === undefined ? undefined : JSON.stringify(body), |
| 51 | + headers: |
| 52 | + body === undefined ? undefined : { "content-type": "application/json" }, |
| 53 | + method: body === undefined ? "GET" : "POST", |
| 54 | + }) |
| 55 | + ); |
| 56 | +} |
| 57 | + |
| 58 | +describe("public bulk flags boundary", () => { |
| 59 | + it("returns all flags only when the key filter is omitted", async () => { |
| 60 | + for (const response of [ |
| 61 | + await request("/v1/flags/bulk?clientId=site_1"), |
| 62 | + await request("/v1/flags/bulk", { clientId: "site_1" }), |
| 63 | + ]) { |
| 64 | + expect(response.status).toBe(200); |
| 65 | + expect(await response.json()).toMatchObject({ |
| 66 | + count: 1, |
| 67 | + flags: { "enabled-for-everyone": { enabled: true } }, |
| 68 | + }); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + it("returns no flags for explicitly empty or blank key lists", async () => { |
| 73 | + for (const response of [ |
| 74 | + await request("/v1/flags/bulk?clientId=site_1&keys="), |
| 75 | + await request("/v1/flags/bulk?clientId=site_1&keys=%20,%20"), |
| 76 | + await request("/v1/flags/bulk", { clientId: "site_1", keys: [] }), |
| 77 | + await request("/v1/flags/bulk", { |
| 78 | + clientId: "site_1", |
| 79 | + keys: ["", " "], |
| 80 | + }), |
| 81 | + ]) { |
| 82 | + expect(response.status).toBe(200); |
| 83 | + expect(await response.json()).toEqual({ count: 0, flags: {} }); |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + it("rejects more than 100 requested keys", async () => { |
| 88 | + const keys = Array.from({ length: 101 }, (_, index) => `flag-${index}`); |
| 89 | + const getResponse = await request( |
| 90 | + `/v1/flags/bulk?clientId=site_1&keys=${keys.join(",")}` |
| 91 | + ); |
| 92 | + expect(getResponse.status).toBe(400); |
| 93 | + |
| 94 | + const postResponse = await request("/v1/flags/bulk", { |
| 95 | + clientId: "site_1", |
| 96 | + keys, |
| 97 | + }); |
| 98 | + expect(postResponse.status).toBe(422); |
| 99 | + }); |
| 100 | + |
| 101 | + it("rejects keys longer than 128 characters", async () => { |
| 102 | + const key = "x".repeat(129); |
| 103 | + const getResponse = await request( |
| 104 | + `/v1/flags/bulk?clientId=site_1&keys=${key}` |
| 105 | + ); |
| 106 | + expect(getResponse.status).toBe(400); |
| 107 | + |
| 108 | + const postResponse = await request("/v1/flags/bulk", { |
| 109 | + clientId: "site_1", |
| 110 | + keys: [key], |
| 111 | + }); |
| 112 | + expect(postResponse.status).toBe(422); |
| 113 | + }); |
| 114 | +}); |
0 commit comments