|
| 1 | +import { containerTest } from "@internal/testcontainers"; |
| 2 | +import type { WebhookResource } from "@trigger.dev/core/v3"; |
| 3 | +import type { BackgroundWorker, PrismaClient } from "@trigger.dev/database"; |
| 4 | +import { describe, expect, vi } from "vitest"; |
| 5 | +import type { AuthenticatedEnvironment } from "~/services/apiAuth.server"; |
| 6 | +import { syncDeclarativeWebhooks } from "~/v3/services/createBackgroundWorker.server"; |
| 7 | + |
| 8 | +vi.setConfig({ testTimeout: 60_000 }); |
| 9 | + |
| 10 | +type WorkerArg = Parameters<typeof syncDeclarativeWebhooks>[1]; |
| 11 | +const noWorker = {} as unknown as WorkerArg; |
| 12 | + |
| 13 | +async function seedProjectWithEnv(prisma: PrismaClient) { |
| 14 | + const slug = `sdw_${Math.random().toString(36).slice(2, 10)}`; |
| 15 | + const organization = await prisma.organization.create({ data: { title: slug, slug } }); |
| 16 | + const project = await prisma.project.create({ |
| 17 | + data: { name: slug, slug, organizationId: organization.id, externalRef: slug }, |
| 18 | + }); |
| 19 | + const environment = await prisma.runtimeEnvironment.create({ |
| 20 | + data: { |
| 21 | + slug: "prod", |
| 22 | + type: "PRODUCTION", |
| 23 | + projectId: project.id, |
| 24 | + organizationId: organization.id, |
| 25 | + apiKey: `tr_prod_${slug}`, |
| 26 | + pkApiKey: `pk_prod_${slug}`, |
| 27 | + shortcode: `p${slug.slice(0, 5)}`, |
| 28 | + }, |
| 29 | + }); |
| 30 | + return { organization, project, environment }; |
| 31 | +} |
| 32 | + |
| 33 | +async function seedWorkerWithTask( |
| 34 | + prisma: PrismaClient, |
| 35 | + project: { id: string }, |
| 36 | + environment: { id: string }, |
| 37 | + taskSlug: string |
| 38 | +): Promise<BackgroundWorker> { |
| 39 | + const suffix = Math.random().toString(36).slice(2, 10); |
| 40 | + const worker = await prisma.backgroundWorker.create({ |
| 41 | + data: { |
| 42 | + friendlyId: `worker_${suffix}`, |
| 43 | + contentHash: `hash_${suffix}`, |
| 44 | + version: "20260101.1", |
| 45 | + metadata: {}, |
| 46 | + projectId: project.id, |
| 47 | + runtimeEnvironmentId: environment.id, |
| 48 | + }, |
| 49 | + }); |
| 50 | + await prisma.backgroundWorkerTask.create({ |
| 51 | + data: { |
| 52 | + friendlyId: `task_${suffix}`, |
| 53 | + slug: taskSlug, |
| 54 | + filePath: `src/trigger/${taskSlug}.ts`, |
| 55 | + workerId: worker.id, |
| 56 | + projectId: project.id, |
| 57 | + runtimeEnvironmentId: environment.id, |
| 58 | + }, |
| 59 | + }); |
| 60 | + return worker; |
| 61 | +} |
| 62 | + |
| 63 | +async function seedEndpoint( |
| 64 | + prisma: PrismaClient, |
| 65 | + base: { organizationId: string; projectId: string; runtimeEnvironmentId: string }, |
| 66 | + handlerWebhookId: string, |
| 67 | + status: "ACTIVE" | "INACTIVE" |
| 68 | +) { |
| 69 | + const suffix = Math.random().toString(36).slice(2, 10); |
| 70 | + return prisma.webhookEndpoint.create({ |
| 71 | + data: { |
| 72 | + friendlyId: `wh_${suffix}`, |
| 73 | + opaqueId: `op_${suffix}${Math.random().toString(36).slice(2, 10)}`, |
| 74 | + organizationId: base.organizationId, |
| 75 | + projectId: base.projectId, |
| 76 | + runtimeEnvironmentId: base.runtimeEnvironmentId, |
| 77 | + environmentType: "PRODUCTION", |
| 78 | + source: "stripe", |
| 79 | + handlerWebhookId, |
| 80 | + routingTarget: { type: "task", taskId: "handle-stripe" }, |
| 81 | + verifierArtifact: { kind: "bundle", bundleUrl: "https://example.test/v.js", hash: "h" }, |
| 82 | + status, |
| 83 | + }, |
| 84 | + }); |
| 85 | +} |
| 86 | + |
| 87 | +function makeWebhookResource(id: string, taskId: string): WebhookResource { |
| 88 | + return { |
| 89 | + id, |
| 90 | + filePath: `src/trigger/${id}.ts`, |
| 91 | + source: "stripe", |
| 92 | + verifierArtifact: { kind: "bundle", bundleUrl: "https://example.test/v.js", hash: "h" }, |
| 93 | + routingTarget: { type: "task", taskId }, |
| 94 | + }; |
| 95 | +} |
| 96 | + |
| 97 | +const asEnv = (env: unknown) => env as AuthenticatedEnvironment; |
| 98 | + |
| 99 | +describe("syncDeclarativeWebhooks status reconciliation", () => { |
| 100 | + containerTest( |
| 101 | + "an absent webhooks list (older client) does not deactivate existing endpoints", |
| 102 | + async ({ prisma }) => { |
| 103 | + const { organization, project, environment } = await seedProjectWithEnv(prisma); |
| 104 | + const endpoint = await seedEndpoint( |
| 105 | + prisma, |
| 106 | + { |
| 107 | + organizationId: organization.id, |
| 108 | + projectId: project.id, |
| 109 | + runtimeEnvironmentId: environment.id, |
| 110 | + }, |
| 111 | + "declared-webhook", |
| 112 | + "ACTIVE" |
| 113 | + ); |
| 114 | + |
| 115 | + await syncDeclarativeWebhooks(undefined, noWorker, asEnv(environment), prisma, prisma); |
| 116 | + |
| 117 | + const after = await prisma.webhookEndpoint.findUniqueOrThrow({ where: { id: endpoint.id } }); |
| 118 | + expect(after.status).toBe("ACTIVE"); |
| 119 | + } |
| 120 | + ); |
| 121 | + |
| 122 | + containerTest( |
| 123 | + "an explicit empty list deactivates endpoints that are no longer declared", |
| 124 | + async ({ prisma }) => { |
| 125 | + const { organization, project, environment } = await seedProjectWithEnv(prisma); |
| 126 | + const endpoint = await seedEndpoint( |
| 127 | + prisma, |
| 128 | + { |
| 129 | + organizationId: organization.id, |
| 130 | + projectId: project.id, |
| 131 | + runtimeEnvironmentId: environment.id, |
| 132 | + }, |
| 133 | + "declared-webhook", |
| 134 | + "ACTIVE" |
| 135 | + ); |
| 136 | + |
| 137 | + await syncDeclarativeWebhooks([], noWorker, asEnv(environment), prisma, prisma); |
| 138 | + |
| 139 | + const after = await prisma.webhookEndpoint.findUniqueOrThrow({ where: { id: endpoint.id } }); |
| 140 | + expect(after.status).toBe("INACTIVE"); |
| 141 | + } |
| 142 | + ); |
| 143 | + |
| 144 | + containerTest( |
| 145 | + "a redeploy does not re-activate an endpoint disabled via the API", |
| 146 | + async ({ prisma }) => { |
| 147 | + const { organization, project, environment } = await seedProjectWithEnv(prisma); |
| 148 | + const worker = await seedWorkerWithTask(prisma, project, environment, "handle-stripe"); |
| 149 | + const endpoint = await seedEndpoint( |
| 150 | + prisma, |
| 151 | + { |
| 152 | + organizationId: organization.id, |
| 153 | + projectId: project.id, |
| 154 | + runtimeEnvironmentId: environment.id, |
| 155 | + }, |
| 156 | + "declared-webhook", |
| 157 | + "INACTIVE" |
| 158 | + ); |
| 159 | + |
| 160 | + await syncDeclarativeWebhooks( |
| 161 | + [makeWebhookResource("declared-webhook", "handle-stripe")], |
| 162 | + worker, |
| 163 | + asEnv(environment), |
| 164 | + prisma, |
| 165 | + prisma |
| 166 | + ); |
| 167 | + |
| 168 | + const after = await prisma.webhookEndpoint.findUniqueOrThrow({ where: { id: endpoint.id } }); |
| 169 | + expect(after.status).toBe("INACTIVE"); |
| 170 | + } |
| 171 | + ); |
| 172 | + |
| 173 | + containerTest("a newly declared webhook creates an active endpoint", async ({ prisma }) => { |
| 174 | + const { project, environment } = await seedProjectWithEnv(prisma); |
| 175 | + const worker = await seedWorkerWithTask(prisma, project, environment, "handle-stripe"); |
| 176 | + |
| 177 | + await syncDeclarativeWebhooks( |
| 178 | + [makeWebhookResource("brand-new-webhook", "handle-stripe")], |
| 179 | + worker, |
| 180 | + asEnv(environment), |
| 181 | + prisma, |
| 182 | + prisma |
| 183 | + ); |
| 184 | + |
| 185 | + const created = await prisma.webhookEndpoint.findFirst({ |
| 186 | + where: { runtimeEnvironmentId: environment.id, handlerWebhookId: "brand-new-webhook" }, |
| 187 | + }); |
| 188 | + expect(created?.status).toBe("ACTIVE"); |
| 189 | + }); |
| 190 | +}); |
0 commit comments