|
| 1 | +import { BackgroundWorkerMetadata } from "@trigger.dev/core/v3"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { stripBackgroundWorkerMetadataForStorage } from "~/v3/services/stripBackgroundWorkerMetadataForStorage.server"; |
| 4 | + |
| 5 | +const baseMetadata: BackgroundWorkerMetadata = { |
| 6 | + packageVersion: "4.8.1", |
| 7 | + contentHash: "hash", |
| 8 | + tasks: [ |
| 9 | + { id: "plain", filePath: "src/trigger/plain.ts", exportName: "plain" }, |
| 10 | + { |
| 11 | + id: "nightly", |
| 12 | + filePath: "src/trigger/nightly.ts", |
| 13 | + exportName: "nightly", |
| 14 | + schedule: { cron: "0 0 * * *", timezone: "UTC" }, |
| 15 | + }, |
| 16 | + ], |
| 17 | +}; |
| 18 | + |
| 19 | +function roundTrip(metadata: BackgroundWorkerMetadata) { |
| 20 | + const stored = JSON.parse(JSON.stringify(stripBackgroundWorkerMetadataForStorage(metadata))); |
| 21 | + return BackgroundWorkerMetadata.parse(stored); |
| 22 | +} |
| 23 | + |
| 24 | +describe("stripBackgroundWorkerMetadataForStorage", () => { |
| 25 | + it("keeps only scheduled tasks", () => { |
| 26 | + const parsed = roundTrip(baseMetadata); |
| 27 | + |
| 28 | + expect(parsed.tasks.map((t) => t.id)).toEqual(["nightly"]); |
| 29 | + expect(parsed.tasks[0].schedule).toEqual({ cron: "0 0 * * *", timezone: "UTC" }); |
| 30 | + }); |
| 31 | + |
| 32 | + it("keeps webhook endpoints and subscribers so promotion can re-sync them", () => { |
| 33 | + const webhookEndpoints: BackgroundWorkerMetadata["webhookEndpoints"] = [ |
| 34 | + { |
| 35 | + id: "stripe-events", |
| 36 | + filePath: "src/trigger/stripe.ts", |
| 37 | + source: "stripe", |
| 38 | + verifierArtifact: { kind: "bundle", bundleUrl: "https://example.test/v.js", hash: "h" }, |
| 39 | + }, |
| 40 | + ]; |
| 41 | + const webhookSubscribers: BackgroundWorkerMetadata["webhookSubscribers"] = [ |
| 42 | + { |
| 43 | + endpointId: "stripe-events", |
| 44 | + target: { type: "task", id: "plain", taskId: "plain" }, |
| 45 | + }, |
| 46 | + ]; |
| 47 | + |
| 48 | + const parsed = roundTrip({ ...baseMetadata, webhookEndpoints, webhookSubscribers }); |
| 49 | + |
| 50 | + expect(parsed.webhookEndpoints).toEqual(webhookEndpoints); |
| 51 | + expect(parsed.webhookSubscribers).toEqual(webhookSubscribers); |
| 52 | + }); |
| 53 | + |
| 54 | + it("keeps an empty endpoint list so promotion removes endpoints the worker no longer declares", () => { |
| 55 | + const parsed = roundTrip({ ...baseMetadata, webhookEndpoints: [], webhookSubscribers: [] }); |
| 56 | + |
| 57 | + expect(parsed.webhookEndpoints).toEqual([]); |
| 58 | + expect(parsed.webhookSubscribers).toEqual([]); |
| 59 | + }); |
| 60 | + |
| 61 | + it("leaves webhook fields absent when the worker didn't declare them", () => { |
| 62 | + const parsed = roundTrip(baseMetadata); |
| 63 | + |
| 64 | + expect(parsed.webhookEndpoints).toBeUndefined(); |
| 65 | + expect(parsed.webhookSubscribers).toBeUndefined(); |
| 66 | + }); |
| 67 | +}); |
0 commit comments