|
| 1 | +import { containerTest } from "@internal/testcontainers"; |
| 2 | +import { trace } from "@internal/tracing"; |
| 3 | +import { setTimeout } from "timers/promises"; |
| 4 | +import { describe, expect, vi } from "vitest"; |
| 5 | +import { TriggerScheduledTaskParams } from "../src/engine/types.js"; |
| 6 | +import { ScheduleEngine } from "../src/index.js"; |
| 7 | + |
| 8 | +describe("ScheduleEngine Integration (part 2)", () => { |
| 9 | + // Deploy-moment backward compatibility. At deploy time, in-flight Redis jobs |
| 10 | + // were enqueued by the old engine — their payload has no `lastScheduleTime` |
| 11 | + // field — and `instance.lastScheduledTimestamp` is still populated (last |
| 12 | + // written by the old engine pre-deploy). The new engine must report that DB |
| 13 | + // value as `payload.lastTimestamp` so customers don't see a transient |
| 14 | + // `undefined` for the one fire per schedule that drains the legacy queue. |
| 15 | + containerTest( |
| 16 | + "should fall back to instance.lastScheduledTimestamp when payload lacks lastScheduleTime", |
| 17 | + { timeout: 30_000 }, |
| 18 | + async ({ prisma, redisOptions }) => { |
| 19 | + const triggerCalls: TriggerScheduledTaskParams[] = []; |
| 20 | + const engine = new ScheduleEngine({ |
| 21 | + prisma, |
| 22 | + redis: redisOptions, |
| 23 | + distributionWindow: { seconds: 10 }, |
| 24 | + worker: { |
| 25 | + concurrency: 1, |
| 26 | + disabled: true, // Don't actually run the worker — calling triggerScheduledTask directly |
| 27 | + pollIntervalMs: 1000, |
| 28 | + }, |
| 29 | + tracer: trace.getTracer("test", "0.0.0"), |
| 30 | + onTriggerScheduledTask: async (params) => { |
| 31 | + triggerCalls.push(params); |
| 32 | + return { success: true }; |
| 33 | + }, |
| 34 | + isDevEnvironmentConnectedHandler: vi.fn().mockResolvedValue(true), |
| 35 | + }); |
| 36 | + |
| 37 | + try { |
| 38 | + const organization = await prisma.organization.create({ |
| 39 | + data: { title: "Legacy Payload Org", slug: "legacy-payload-org" }, |
| 40 | + }); |
| 41 | + |
| 42 | + const project = await prisma.project.create({ |
| 43 | + data: { |
| 44 | + name: "Legacy Payload Project", |
| 45 | + slug: "legacy-payload-project", |
| 46 | + externalRef: "legacy-payload-ref", |
| 47 | + organizationId: organization.id, |
| 48 | + }, |
| 49 | + }); |
| 50 | + |
| 51 | + const environment = await prisma.runtimeEnvironment.create({ |
| 52 | + data: { |
| 53 | + slug: "legacy-payload-env", |
| 54 | + type: "PRODUCTION", |
| 55 | + projectId: project.id, |
| 56 | + organizationId: organization.id, |
| 57 | + apiKey: "tr_legacy_1234", |
| 58 | + pkApiKey: "pk_legacy_1234", |
| 59 | + shortcode: "legacy-short", |
| 60 | + }, |
| 61 | + }); |
| 62 | + |
| 63 | + const taskSchedule = await prisma.taskSchedule.create({ |
| 64 | + data: { |
| 65 | + friendlyId: "sched_legacy_payload", |
| 66 | + taskIdentifier: "legacy-payload-task", |
| 67 | + projectId: project.id, |
| 68 | + deduplicationKey: "legacy-payload-dedup", |
| 69 | + userProvidedDeduplicationKey: false, |
| 70 | + generatorExpression: "*/5 * * * *", |
| 71 | + generatorDescription: "Every 5 minutes", |
| 72 | + timezone: "UTC", |
| 73 | + type: "DECLARATIVE", |
| 74 | + active: true, |
| 75 | + externalId: "legacy-ext", |
| 76 | + }, |
| 77 | + }); |
| 78 | + |
| 79 | + // Pre-populate lastScheduledTimestamp on the instance — simulates the |
| 80 | + // value the old engine wrote to the DB before this PR deployed. |
| 81 | + const preDeployLastFire = new Date("2026-04-30T10:00:00.000Z"); |
| 82 | + const scheduleInstance = await prisma.taskScheduleInstance.create({ |
| 83 | + data: { |
| 84 | + taskScheduleId: taskSchedule.id, |
| 85 | + environmentId: environment.id, |
| 86 | + projectId: project.id, |
| 87 | + active: true, |
| 88 | + lastScheduledTimestamp: preDeployLastFire, |
| 89 | + }, |
| 90 | + }); |
| 91 | + |
| 92 | + // Call triggerScheduledTask directly without lastScheduleTime, |
| 93 | + // simulating an in-flight Redis job enqueued by the old engine. |
| 94 | + const exactScheduleTime = new Date("2026-04-30T10:05:00.000Z"); |
| 95 | + await engine.triggerScheduledTask({ |
| 96 | + instanceId: scheduleInstance.id, |
| 97 | + finalAttempt: false, |
| 98 | + exactScheduleTime, |
| 99 | + // lastScheduleTime intentionally omitted — legacy payload shape |
| 100 | + }); |
| 101 | + |
| 102 | + expect(triggerCalls.length).toBe(1); |
| 103 | + expect(triggerCalls[0].payload.timestamp).toEqual(exactScheduleTime); |
| 104 | + // Falls back to instance.lastScheduledTimestamp from the DB rather |
| 105 | + // than reporting undefined for this one transitional fire. |
| 106 | + expect(triggerCalls[0].payload.lastTimestamp).toEqual(preDeployLastFire); |
| 107 | + } finally { |
| 108 | + await engine.quit(); |
| 109 | + } |
| 110 | + } |
| 111 | + ); |
| 112 | +}); |
0 commit comments