|
| 1 | +// The v1 PAT route enables an org's snapshot dial with merge semantics. It must stamp the one-way |
| 2 | +// per-org residency latch (snapshotStoreOrgEverEnabled) exactly as the v2 route does; without it the |
| 3 | +// census keeps the org classified definitely-never-enabled and its resident runs' transitions are |
| 4 | +// skipped. These drive the real exported action against a real Postgres and assert the stored blob. |
| 5 | +// The guard is stubbed to a pass so the test isolates the stamp wiring (the guard has its own tests); |
| 6 | +// only peripheral module boundaries are substituted, the database is the genuine article. |
| 7 | +import type { PrismaClient } from "@trigger.dev/database"; |
| 8 | +import { postgresTest } from "@internal/testcontainers"; |
| 9 | +import { describe, expect, vi } from "vitest"; |
| 10 | +import { FEATURE_FLAG } from "~/v3/featureFlags"; |
| 11 | + |
| 12 | +vi.setConfig({ testTimeout: 60_000 }); |
| 13 | + |
| 14 | +const db = vi.hoisted(() => ({ client: null as unknown as PrismaClient })); |
| 15 | + |
| 16 | +vi.mock("~/services/personalAccessToken.server", () => ({ |
| 17 | + requireAdminApiRequest: async () => ({}), |
| 18 | +})); |
| 19 | + |
| 20 | +vi.mock("~/db.server", () => ({ |
| 21 | + get prisma() { |
| 22 | + return db.client; |
| 23 | + }, |
| 24 | + get $replica() { |
| 25 | + return db.client; |
| 26 | + }, |
| 27 | +})); |
| 28 | + |
| 29 | +// Bypass the host/arming-latch gate so the test exercises the stamp path directly. The guard's own |
| 30 | +// behaviour (reject without a host or before the global latch) is covered in its own tests. |
| 31 | +vi.mock("~/v3/snapshotStoreFlagGuard.server", () => ({ |
| 32 | + snapshotStoreFlagSaveError: () => undefined, |
| 33 | +})); |
| 34 | + |
| 35 | +vi.mock("~/v3/runOpsMigration/controlPlaneResolver.server", () => ({ |
| 36 | + controlPlaneResolver: { invalidateOrganization: () => {} }, |
| 37 | +})); |
| 38 | + |
| 39 | +// Only used to seed the mint-flip baseline; irrelevant to the residency latch under test. |
| 40 | +vi.mock("~/v3/featureFlags.server", () => ({ |
| 41 | + flags: async () => ({}), |
| 42 | +})); |
| 43 | + |
| 44 | +import { action } from "~/routes/admin.api.v1.orgs.$organizationId.feature-flags"; |
| 45 | + |
| 46 | +const MODE = FEATURE_FLAG.snapshotStoreOrgMode; |
| 47 | +const LATCH = FEATURE_FLAG.snapshotStoreOrgEverEnabled; |
| 48 | + |
| 49 | +let orgSeq = 0; |
| 50 | + |
| 51 | +async function seedOrg(prisma: PrismaClient, featureFlags?: Record<string, unknown>) { |
| 52 | + db.client = prisma; |
| 53 | + const id = `org_v1route_${orgSeq++}`; |
| 54 | + await prisma.organization.create({ |
| 55 | + data: { |
| 56 | + id, |
| 57 | + title: "V1 route test org", |
| 58 | + slug: `v1-route-${id}`, |
| 59 | + ...(featureFlags ? { featureFlags } : {}), |
| 60 | + }, |
| 61 | + }); |
| 62 | + return id; |
| 63 | +} |
| 64 | + |
| 65 | +async function post(organizationId: string, body: unknown) { |
| 66 | + const request = new Request( |
| 67 | + `https://localhost:3030/admin/api/v1/orgs/${organizationId}/feature-flags`, |
| 68 | + { |
| 69 | + method: "POST", |
| 70 | + body: JSON.stringify(body), |
| 71 | + headers: { "content-type": "application/json" }, |
| 72 | + } |
| 73 | + ); |
| 74 | + return (await (action as any)({ |
| 75 | + request, |
| 76 | + params: { organizationId }, |
| 77 | + context: {}, |
| 78 | + })) as Response; |
| 79 | +} |
| 80 | + |
| 81 | +async function readFlags(prisma: PrismaClient, id: string) { |
| 82 | + const row = await prisma.organization.findFirst({ |
| 83 | + where: { id }, |
| 84 | + select: { featureFlags: true }, |
| 85 | + }); |
| 86 | + return (row?.featureFlags ?? null) as Record<string, unknown> | null; |
| 87 | +} |
| 88 | + |
| 89 | +describe("admin v1 org feature-flags route stamps the per-org residency latch", () => { |
| 90 | + postgresTest("stamps the latch when the dial is enabled past off", async ({ prisma }) => { |
| 91 | + const id = await seedOrg(prisma); |
| 92 | + |
| 93 | + const response = await post(id, { [MODE]: "redis-read" }); |
| 94 | + |
| 95 | + expect(response.status).toBe(200); |
| 96 | + const flags = await readFlags(prisma, id); |
| 97 | + expect(flags?.[MODE]).toBe("redis-read"); |
| 98 | + expect(flags?.[LATCH]).toBe(true); |
| 99 | + }); |
| 100 | + |
| 101 | + postgresTest( |
| 102 | + "keeps the latch when a later save sets the dial back to off", |
| 103 | + async ({ prisma }) => { |
| 104 | + const id = await seedOrg(prisma); |
| 105 | + |
| 106 | + await post(id, { [MODE]: "redis-read" }); |
| 107 | + const response = await post(id, { [MODE]: "off" }); |
| 108 | + |
| 109 | + expect(response.status).toBe(200); |
| 110 | + const flags = await readFlags(prisma, id); |
| 111 | + expect(flags?.[MODE]).toBe("off"); |
| 112 | + // One-way: the latch survives the roll-back so a run still resident keeps mirroring. |
| 113 | + expect(flags?.[LATCH]).toBe(true); |
| 114 | + } |
| 115 | + ); |
| 116 | + |
| 117 | + postgresTest( |
| 118 | + "does not stamp the latch for an off save with no prior latch", |
| 119 | + async ({ prisma }) => { |
| 120 | + const id = await seedOrg(prisma); |
| 121 | + |
| 122 | + const response = await post(id, { [MODE]: "off" }); |
| 123 | + |
| 124 | + expect(response.status).toBe(200); |
| 125 | + const flags = await readFlags(prisma, id); |
| 126 | + expect(flags?.[LATCH]).toBeUndefined(); |
| 127 | + } |
| 128 | + ); |
| 129 | +}); |
0 commit comments