|
| 1 | +// The page posts only the flags its UI manages, so the action's reading of an absent key is the |
| 2 | +// whole bug surface. These drive the real exported action with the auth wrapper unwrapped, and |
| 3 | +// assert on what it hands the writer. |
| 4 | +import { describe, expect, it, vi } from "vitest"; |
| 5 | +import { FEATURE_FLAG } from "~/v3/featureFlags"; |
| 6 | + |
| 7 | +const { replaceGlobalFeatureFlags } = vi.hoisted(() => ({ |
| 8 | + replaceGlobalFeatureFlags: vi.fn().mockResolvedValue(undefined), |
| 9 | +})); |
| 10 | + |
| 11 | +vi.mock("~/services/routeBuilders/dashboardBuilder", () => ({ |
| 12 | + dashboardAction: (_options: unknown, handler: unknown) => handler, |
| 13 | + dashboardLoader: (_options: unknown, handler: unknown) => handler, |
| 14 | +})); |
| 15 | +vi.mock("~/v3/featureFlags.server", () => ({ |
| 16 | + replaceGlobalFeatureFlags, |
| 17 | + flags: vi.fn().mockResolvedValue({}), |
| 18 | +})); |
| 19 | +vi.mock("~/db.server", () => ({ prisma: {}, boundedIn: (v: unknown) => v })); |
| 20 | + |
| 21 | +const { action } = await import("~/routes/admin.feature-flags"); |
| 22 | + |
| 23 | +async function post(host: string, body: unknown) { |
| 24 | + const request = new Request(`https://${host}/admin/feature-flags`, { |
| 25 | + method: "POST", |
| 26 | + body: JSON.stringify(body), |
| 27 | + headers: { "content-type": "application/json" }, |
| 28 | + }); |
| 29 | + return (await (action as any)({ request, params: {}, context: {} })) as Response; |
| 30 | +} |
| 31 | + |
| 32 | +describe("admin feature flags action", () => { |
| 33 | + it("defaults unlockLockedFlags to false when the field is absent", async () => { |
| 34 | + replaceGlobalFeatureFlags.mockClear(); |
| 35 | + const response = await post("localhost:3030", { flags: {} }); |
| 36 | + |
| 37 | + expect(response.status).toBe(200); |
| 38 | + expect(replaceGlobalFeatureFlags).toHaveBeenCalledTimes(1); |
| 39 | + expect(replaceGlobalFeatureFlags.mock.calls[0][1]).toMatchObject({ |
| 40 | + unlockLockedFlags: false, |
| 41 | + isManagedCloud: false, |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + it("passes unlockLockedFlags through when the page says it unlocked them", async () => { |
| 46 | + replaceGlobalFeatureFlags.mockClear(); |
| 47 | + await post("localhost:3030", { flags: {}, unlockLockedFlags: true }); |
| 48 | + |
| 49 | + expect(replaceGlobalFeatureFlags.mock.calls[0][1]).toMatchObject({ unlockLockedFlags: true }); |
| 50 | + }); |
| 51 | + |
| 52 | + it("marks a managed cloud host as such", async () => { |
| 53 | + replaceGlobalFeatureFlags.mockClear(); |
| 54 | + await post("cloud.trigger.dev", { flags: {}, unlockLockedFlags: true }); |
| 55 | + |
| 56 | + expect(replaceGlobalFeatureFlags.mock.calls[0][1]).toMatchObject({ isManagedCloud: true }); |
| 57 | + }); |
| 58 | + |
| 59 | + it("rejects a locked flag submitted to managed cloud without writing", async () => { |
| 60 | + replaceGlobalFeatureFlags.mockClear(); |
| 61 | + const response = await post("cloud.trigger.dev", { |
| 62 | + flags: { [FEATURE_FLAG.defaultWorkerInstanceGroupId]: "clwg0001" }, |
| 63 | + }); |
| 64 | + |
| 65 | + expect(response.status).toBe(400); |
| 66 | + expect(replaceGlobalFeatureFlags).not.toHaveBeenCalled(); |
| 67 | + }); |
| 68 | + |
| 69 | + it("rejects a value that fails the catalog schema without writing", async () => { |
| 70 | + replaceGlobalFeatureFlags.mockClear(); |
| 71 | + const response = await post("localhost:3030", { |
| 72 | + flags: { [FEATURE_FLAG.realtimeBackend]: "not-a-backend" }, |
| 73 | + }); |
| 74 | + |
| 75 | + expect(response.status).toBe(400); |
| 76 | + expect(replaceGlobalFeatureFlags).not.toHaveBeenCalled(); |
| 77 | + }); |
| 78 | + |
| 79 | + it("submits every catalog key so omitted flags are swept", async () => { |
| 80 | + replaceGlobalFeatureFlags.mockClear(); |
| 81 | + await post("localhost:3030", { flags: { [FEATURE_FLAG.mollifierEnabled]: true } }); |
| 82 | + |
| 83 | + const { catalogKeys, requestedFlags } = replaceGlobalFeatureFlags.mock.calls[0][1]; |
| 84 | + expect(catalogKeys).toContain(FEATURE_FLAG.defaultWorkerInstanceGroupId); |
| 85 | + expect(requestedFlags).toEqual({ [FEATURE_FLAG.mollifierEnabled]: true }); |
| 86 | + }); |
| 87 | +}); |
0 commit comments