|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import type { WaitpointRecordInput } from "./storeCoordinator.js"; |
| 3 | +import { toPrismaWaitpoint } from "./waitpointShape.js"; |
| 4 | + |
| 5 | +const record: WaitpointRecordInput = { |
| 6 | + id: "abcdefghijklmnopqrstuvwxmw", |
| 7 | + friendlyId: "waitpoint_abcdefghijklmnopqrstuvwxmw", |
| 8 | + type: "MANUAL", |
| 9 | + environmentId: "env_1", |
| 10 | + projectId: "proj_1", |
| 11 | + createdAt: "2026-08-26T10:00:00.000Z", |
| 12 | + updatedAt: "2026-08-26T10:00:01.000Z", |
| 13 | + userProvidedIdempotencyKey: true, |
| 14 | + tags: ["alpha", "beta"], |
| 15 | + idempotencyKey: "user-key", |
| 16 | +}; |
| 17 | + |
| 18 | +describe("toPrismaWaitpoint", () => { |
| 19 | + it("fills every non-null column on a PENDING waitpoint", () => { |
| 20 | + const waitpoint = toPrismaWaitpoint(record, "PENDING"); |
| 21 | + |
| 22 | + expect(waitpoint.id).toBe(record.id); |
| 23 | + expect(waitpoint.friendlyId).toBe(record.friendlyId); |
| 24 | + expect(waitpoint.type).toBe("MANUAL"); |
| 25 | + expect(waitpoint.status).toBe("PENDING"); |
| 26 | + expect(waitpoint.idempotencyKey).toBe("user-key"); |
| 27 | + expect(waitpoint.userProvidedIdempotencyKey).toBe(true); |
| 28 | + expect(waitpoint.projectId).toBe("proj_1"); |
| 29 | + expect(waitpoint.environmentId).toBe("env_1"); |
| 30 | + expect(waitpoint.tags).toEqual(["alpha", "beta"]); |
| 31 | + expect(waitpoint.createdAt).toEqual(new Date("2026-08-26T10:00:00.000Z")); |
| 32 | + expect(waitpoint.updatedAt).toEqual(new Date("2026-08-26T10:00:01.000Z")); |
| 33 | + |
| 34 | + // The columns with database defaults, which a consumer reads unconditionally. |
| 35 | + expect(waitpoint.outputType).toBe("application/json"); |
| 36 | + expect(waitpoint.outputIsError).toBe(false); |
| 37 | + |
| 38 | + // Nullable columns that must be null rather than undefined: a consumer distinguishes |
| 39 | + // "no value" from "field missing", and `inactiveIdempotencyKey` is not ported at all. |
| 40 | + expect(waitpoint.completedAt).toBeNull(); |
| 41 | + expect(waitpoint.output).toBeNull(); |
| 42 | + expect(waitpoint.inactiveIdempotencyKey).toBeNull(); |
| 43 | + expect(waitpoint.idempotencyKeyExpiresAt).toBeNull(); |
| 44 | + expect(waitpoint.completedByTaskRunId).toBeNull(); |
| 45 | + expect(waitpoint.completedByBatchId).toBeNull(); |
| 46 | + expect(waitpoint.completedAfter).toBeNull(); |
| 47 | + }); |
| 48 | + |
| 49 | + it("carries an inline completion onto a COMPLETED waitpoint", () => { |
| 50 | + const waitpoint = toPrismaWaitpoint(record, "COMPLETED", { |
| 51 | + completedAt: "2026-08-26T11:00:00.000Z", |
| 52 | + outputType: "application/json", |
| 53 | + outputIsError: true, |
| 54 | + output: { inline: '{"boom":true}' }, |
| 55 | + }); |
| 56 | + |
| 57 | + expect(waitpoint.status).toBe("COMPLETED"); |
| 58 | + expect(waitpoint.completedAt).toEqual(new Date("2026-08-26T11:00:00.000Z")); |
| 59 | + expect(waitpoint.output).toBe('{"boom":true}'); |
| 60 | + expect(waitpoint.outputType).toBe("application/json"); |
| 61 | + expect(waitpoint.outputIsError).toBe(true); |
| 62 | + }); |
| 63 | + |
| 64 | + it("carries an offloaded reference in the output column, as the legacy row does", () => { |
| 65 | + const waitpoint = toPrismaWaitpoint(record, "COMPLETED", { |
| 66 | + completedAt: "2026-08-26T11:00:00.000Z", |
| 67 | + outputType: "application/store", |
| 68 | + outputIsError: false, |
| 69 | + output: { ref: "waitpoints/abc/output.json" }, |
| 70 | + }); |
| 71 | + |
| 72 | + expect(waitpoint.output).toBe("waitpoints/abc/output.json"); |
| 73 | + expect(waitpoint.outputType).toBe("application/store"); |
| 74 | + }); |
| 75 | + |
| 76 | + it("leaves output null when the completion carries none", () => { |
| 77 | + // A BATCH completion, and the deriveFromRun case: the value is re-derived at read |
| 78 | + // time and is never copied onto the row. |
| 79 | + const waitpoint = toPrismaWaitpoint({ ...record, type: "BATCH" }, "COMPLETED", { |
| 80 | + completedAt: "2026-08-26T11:00:00.000Z", |
| 81 | + outputType: "application/json", |
| 82 | + outputIsError: false, |
| 83 | + output: null, |
| 84 | + }); |
| 85 | + |
| 86 | + expect(waitpoint.status).toBe("COMPLETED"); |
| 87 | + expect(waitpoint.output).toBeNull(); |
| 88 | + }); |
| 89 | + |
| 90 | + it("maps the optional anchor and timing columns when the record carries them", () => { |
| 91 | + const waitpoint = toPrismaWaitpoint( |
| 92 | + { |
| 93 | + ...record, |
| 94 | + type: "RUN", |
| 95 | + completedByTaskRunId: "run_1", |
| 96 | + completedByBatchId: "batch_1", |
| 97 | + completedAfter: "2026-08-27T00:00:00.000Z", |
| 98 | + idempotencyKeyExpiresAt: "2026-08-28T00:00:00.000Z", |
| 99 | + }, |
| 100 | + "PENDING" |
| 101 | + ); |
| 102 | + |
| 103 | + expect(waitpoint.completedByTaskRunId).toBe("run_1"); |
| 104 | + expect(waitpoint.completedByBatchId).toBe("batch_1"); |
| 105 | + expect(waitpoint.completedAfter).toEqual(new Date("2026-08-27T00:00:00.000Z")); |
| 106 | + expect(waitpoint.idempotencyKeyExpiresAt).toEqual(new Date("2026-08-28T00:00:00.000Z")); |
| 107 | + }); |
| 108 | + |
| 109 | + it("throws when the record carries no idempotency key", () => { |
| 110 | + // The column is non-null and participates in the (environmentId, idempotencyKey) |
| 111 | + // unique index, so inventing a value here could collide. The arm always mints one; |
| 112 | + // an absent key means the arm has a defect, and it must surface as one. |
| 113 | + const { idempotencyKey, ...withoutKey } = record; |
| 114 | + |
| 115 | + expect(() => toPrismaWaitpoint(withoutKey, "PENDING")).toThrow(/idempotency key/i); |
| 116 | + }); |
| 117 | +}); |
0 commit comments