|
| 1 | +// Pure mapping coverage; storage routing and failures use real stores in the integration suite. |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { generateWaitpointId } from "@trigger.dev/core/v3/isomorphic"; |
| 4 | +import type { |
| 5 | + WaitpointCompletion, |
| 6 | + WaitpointRecordInput, |
| 7 | + WaitpointStoredRecord, |
| 8 | +} from "../waitpointCoordinator/storeCoordinator.js"; |
| 9 | +import { storeRecordToCompletedWaitpointRecord } from "./completedWaitpointRecordAdapter.js"; |
| 10 | + |
| 11 | +function storeRecordInput( |
| 12 | + id: string, |
| 13 | + overrides: Partial<WaitpointRecordInput> = {} |
| 14 | +): WaitpointRecordInput { |
| 15 | + return { |
| 16 | + id, |
| 17 | + friendlyId: `waitpoint_${id}`, |
| 18 | + type: "MANUAL", |
| 19 | + environmentId: "env_1", |
| 20 | + projectId: "proj_1", |
| 21 | + createdAt: "2026-01-01T00:00:00.000Z", |
| 22 | + updatedAt: "2026-01-01T00:00:00.000Z", |
| 23 | + userProvidedIdempotencyKey: false, |
| 24 | + tags: [], |
| 25 | + ...overrides, |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +function storeCompletion(overrides: Partial<WaitpointCompletion> = {}): WaitpointCompletion { |
| 30 | + return { |
| 31 | + completedAt: "2026-01-01T00:00:00.000Z", |
| 32 | + outputType: "application/json", |
| 33 | + outputIsError: false, |
| 34 | + output: null, |
| 35 | + ...overrides, |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +function storeEntry( |
| 40 | + id: string, |
| 41 | + recordOverrides: Partial<WaitpointRecordInput> = {}, |
| 42 | + completion: WaitpointCompletion | undefined = storeCompletion(), |
| 43 | + status: WaitpointStoredRecord["status"] = "COMPLETED" |
| 44 | +): WaitpointStoredRecord { |
| 45 | + return { status, record: storeRecordInput(id, recordOverrides), completion }; |
| 46 | +} |
| 47 | + |
| 48 | +// A fixed effective time for the mapping tests that do not exercise expiry. |
| 49 | +const NOW_MS = Date.parse("2026-08-21T12:00:00.000Z"); |
| 50 | +const mapStoreRecord = (entry: WaitpointStoredRecord, nowMs: number = NOW_MS) => |
| 51 | + storeRecordToCompletedWaitpointRecord(entry, nowMs); |
| 52 | + |
| 53 | +describe("storeRecordToCompletedWaitpointRecord — envelope fidelity", () => { |
| 54 | + it("preserves an inline output", () => { |
| 55 | + const id = generateWaitpointId("MANUAL"); |
| 56 | + const record = mapStoreRecord( |
| 57 | + storeEntry(id, {}, storeCompletion({ output: { inline: '{"value":42}' } })) |
| 58 | + ); |
| 59 | + expect(record.output).toEqual({ inline: '{"value":42}' }); |
| 60 | + }); |
| 61 | + |
| 62 | + it("preserves a referenced (offloaded) output", () => { |
| 63 | + const id = generateWaitpointId("MANUAL"); |
| 64 | + const record = mapStoreRecord( |
| 65 | + storeEntry( |
| 66 | + id, |
| 67 | + {}, |
| 68 | + storeCompletion({ outputType: "application/store", output: { ref: "s3://bucket/key" } }) |
| 69 | + ) |
| 70 | + ); |
| 71 | + expect(record.output).toEqual({ ref: "s3://bucket/key" }); |
| 72 | + expect(record.outputType).toBe("application/store"); |
| 73 | + }); |
| 74 | + |
| 75 | + it("preserves an error output and its flag", () => { |
| 76 | + const id = generateWaitpointId("MANUAL"); |
| 77 | + const record = mapStoreRecord( |
| 78 | + storeEntry( |
| 79 | + id, |
| 80 | + {}, |
| 81 | + storeCompletion({ outputIsError: true, output: { inline: '{"type":"STRING_ERROR"}' } }) |
| 82 | + ) |
| 83 | + ); |
| 84 | + expect(record.outputIsError).toBe(true); |
| 85 | + expect(record.output).toEqual({ inline: '{"type":"STRING_ERROR"}' }); |
| 86 | + }); |
| 87 | + |
| 88 | + it("keeps a RUN success on deriveFromRun, not a copy of its output", () => { |
| 89 | + const id = generateWaitpointId("RUN"); |
| 90 | + const record = mapStoreRecord( |
| 91 | + storeEntry( |
| 92 | + id, |
| 93 | + { type: "RUN", completedByTaskRunId: "run_child" }, |
| 94 | + storeCompletion({ output: { inline: '{"value":42}' } }) |
| 95 | + ) |
| 96 | + ); |
| 97 | + // The stored inline value is dropped: the read side re-derives it from TaskRun.output. |
| 98 | + expect(record.output).toEqual({ deriveFromRun: true }); |
| 99 | + expect(record.completedByTaskRunId).toBe("run_child"); |
| 100 | + }); |
| 101 | + |
| 102 | + it("keeps a RUN success on deriveFromRun even when the stored output is null", () => { |
| 103 | + // The store may keep a re-derivable RUN success's output null rather than copy it. That null must |
| 104 | + // still resolve to deriveFromRun, NOT to no output — the completion is re-read from TaskRun.output. |
| 105 | + const id = generateWaitpointId("RUN"); |
| 106 | + const record = mapStoreRecord( |
| 107 | + storeEntry( |
| 108 | + id, |
| 109 | + { type: "RUN", completedByTaskRunId: "run_child" }, |
| 110 | + storeCompletion({ output: null }) |
| 111 | + ) |
| 112 | + ); |
| 113 | + expect(record.output).toEqual({ deriveFromRun: true }); |
| 114 | + expect(record.completedByTaskRunId).toBe("run_child"); |
| 115 | + }); |
| 116 | + |
| 117 | + it("carries a RUN ERROR inline, never deriveFromRun", () => { |
| 118 | + const id = generateWaitpointId("RUN"); |
| 119 | + const record = mapStoreRecord( |
| 120 | + storeEntry( |
| 121 | + id, |
| 122 | + { type: "RUN", completedByTaskRunId: "run_child" }, |
| 123 | + storeCompletion({ outputIsError: true, output: { inline: '{"type":"BUILT_IN_ERROR"}' } }) |
| 124 | + ) |
| 125 | + ); |
| 126 | + expect(record.output).toEqual({ inline: '{"type":"BUILT_IN_ERROR"}' }); |
| 127 | + }); |
| 128 | + |
| 129 | + it("preserves timestamps and type-specific metadata", () => { |
| 130 | + const id = generateWaitpointId("DATETIME"); |
| 131 | + const record = mapStoreRecord( |
| 132 | + storeEntry( |
| 133 | + id, |
| 134 | + { type: "DATETIME", completedAfter: "2026-02-02T00:00:00.000Z" }, |
| 135 | + storeCompletion({ completedAt: "2026-03-03T04:05:06.000Z" }) |
| 136 | + ) |
| 137 | + ); |
| 138 | + expect(record.type).toBe("DATETIME"); |
| 139 | + expect(record.completedAt).toBe("2026-03-03T04:05:06.000Z"); |
| 140 | + expect(record.completedAfter).toBe("2026-02-02T00:00:00.000Z"); |
| 141 | + }); |
| 142 | + |
| 143 | + it("exposes a user-provided idempotency key and hides a system one", () => { |
| 144 | + const id = generateWaitpointId("MANUAL"); |
| 145 | + const withUserKey = mapStoreRecord( |
| 146 | + storeEntry(id, { userProvidedIdempotencyKey: true, idempotencyKey: "idem_user" }) |
| 147 | + ); |
| 148 | + expect(withUserKey.idempotencyKey).toBe("idem_user"); |
| 149 | + |
| 150 | + const withSystemKey = mapStoreRecord( |
| 151 | + storeEntry(id, { userProvidedIdempotencyKey: false, idempotencyKey: "idem_system" }) |
| 152 | + ); |
| 153 | + expect(withSystemKey.idempotencyKey).toBeUndefined(); |
| 154 | + }); |
| 155 | +}); |
| 156 | + |
| 157 | +describe("storeRecordToCompletedWaitpointRecord — idempotency key expiry", () => { |
| 158 | + const iso = (ms: number) => new Date(ms).toISOString(); |
| 159 | + const userKey = (overrides: Partial<WaitpointRecordInput>) => |
| 160 | + storeEntry(generateWaitpointId("MANUAL"), { |
| 161 | + userProvidedIdempotencyKey: true, |
| 162 | + idempotencyKey: "idem_user", |
| 163 | + ...overrides, |
| 164 | + }); |
| 165 | + |
| 166 | + it("keeps a user key with no expiry visible", () => { |
| 167 | + expect(mapStoreRecord(userKey({}), NOW_MS).idempotencyKey).toBe("idem_user"); |
| 168 | + }); |
| 169 | + |
| 170 | + it("keeps a user key with a future expiry visible", () => { |
| 171 | + const record = mapStoreRecord( |
| 172 | + userKey({ idempotencyKeyExpiresAt: iso(NOW_MS + 60_000) }), |
| 173 | + NOW_MS |
| 174 | + ); |
| 175 | + expect(record.idempotencyKey).toBe("idem_user"); |
| 176 | + }); |
| 177 | + |
| 178 | + it("omits a user key with a past expiry", () => { |
| 179 | + const record = mapStoreRecord( |
| 180 | + userKey({ idempotencyKeyExpiresAt: iso(NOW_MS - 60_000) }), |
| 181 | + NOW_MS |
| 182 | + ); |
| 183 | + expect(record.idempotencyKey).toBeUndefined(); |
| 184 | + }); |
| 185 | + |
| 186 | + it("omits a user key expiring exactly at the effective time (PEXPIREAT semantics)", () => { |
| 187 | + const record = mapStoreRecord(userKey({ idempotencyKeyExpiresAt: iso(NOW_MS) }), NOW_MS); |
| 188 | + expect(record.idempotencyKey).toBeUndefined(); |
| 189 | + }); |
| 190 | + |
| 191 | + it("fails loud on a malformed stored expiry", () => { |
| 192 | + expect(() => |
| 193 | + mapStoreRecord(userKey({ idempotencyKeyExpiresAt: "not-a-date" }), NOW_MS) |
| 194 | + ).toThrow(/malformed idempotencyKeyExpiresAt/); |
| 195 | + }); |
| 196 | + |
| 197 | + it("keeps a system-generated key hidden regardless of expiry (never throws on its expiry)", () => { |
| 198 | + const system = (expiry: string | undefined) => |
| 199 | + storeEntry(generateWaitpointId("MANUAL"), { |
| 200 | + userProvidedIdempotencyKey: false, |
| 201 | + idempotencyKey: "idem_system", |
| 202 | + idempotencyKeyExpiresAt: expiry, |
| 203 | + }); |
| 204 | + expect(mapStoreRecord(system(iso(NOW_MS - 60_000)), NOW_MS).idempotencyKey).toBeUndefined(); |
| 205 | + expect(mapStoreRecord(system(iso(NOW_MS + 60_000)), NOW_MS).idempotencyKey).toBeUndefined(); |
| 206 | + // A malformed expiry on a hidden key is never consulted, so it does not fail loud. |
| 207 | + expect(mapStoreRecord(system("not-a-date"), NOW_MS).idempotencyKey).toBeUndefined(); |
| 208 | + }); |
| 209 | +}); |
0 commit comments