|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { BatchItemNDJSON, BatchTriggerTaskItem, TriggerTaskRequestBody } from "./api.js"; |
| 3 | + |
| 4 | +describe("concurrencyKey coercion", () => { |
| 5 | + // Phase-2 NDJSON used to accept arbitrary shapes for `options`, so a numeric |
| 6 | + // concurrencyKey (a common foot-gun when callers pass |
| 7 | + // `concurrencyKey: payload.userId`) reached Prisma untouched and failed |
| 8 | + // there with PrismaClientValidationError. The schema now coerces |
| 9 | + // number → string at the API boundary across every trigger path. |
| 10 | + describe("BatchItemNDJSON", () => { |
| 11 | + it("coerces a numeric concurrencyKey to a string", () => { |
| 12 | + const result = BatchItemNDJSON.safeParse({ |
| 13 | + index: 0, |
| 14 | + task: "user-workflow-tick", |
| 15 | + payload: { json: { userId: 51262 } }, |
| 16 | + options: { concurrencyKey: 51262 }, |
| 17 | + }); |
| 18 | + |
| 19 | + expect(result.success).toBe(true); |
| 20 | + if (result.success) { |
| 21 | + expect(result.data.options?.concurrencyKey).toBe("51262"); |
| 22 | + } |
| 23 | + }); |
| 24 | + |
| 25 | + it("accepts a string concurrencyKey unchanged", () => { |
| 26 | + const result = BatchItemNDJSON.safeParse({ |
| 27 | + index: 0, |
| 28 | + task: "user-workflow-tick", |
| 29 | + payload: { json: { userId: 51262 } }, |
| 30 | + options: { concurrencyKey: "user-51262" }, |
| 31 | + }); |
| 32 | + |
| 33 | + expect(result.success).toBe(true); |
| 34 | + if (result.success) { |
| 35 | + expect(result.data.options?.concurrencyKey).toBe("user-51262"); |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + it("accepts an item with no options", () => { |
| 40 | + const result = BatchItemNDJSON.safeParse({ |
| 41 | + index: 0, |
| 42 | + task: "user-workflow-tick", |
| 43 | + payload: { json: { userId: 51262 } }, |
| 44 | + }); |
| 45 | + |
| 46 | + expect(result.success).toBe(true); |
| 47 | + }); |
| 48 | + |
| 49 | + it("rejects a non-numeric, non-string concurrencyKey", () => { |
| 50 | + const result = BatchItemNDJSON.safeParse({ |
| 51 | + index: 0, |
| 52 | + task: "user-workflow-tick", |
| 53 | + options: { concurrencyKey: { nested: "object" } }, |
| 54 | + }); |
| 55 | + |
| 56 | + expect(result.success).toBe(false); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe("BatchTriggerTaskItem", () => { |
| 61 | + it("coerces a numeric concurrencyKey to a string", () => { |
| 62 | + const result = BatchTriggerTaskItem.safeParse({ |
| 63 | + task: "user-workflow-tick", |
| 64 | + payload: { userId: 51262 }, |
| 65 | + options: { concurrencyKey: 51262 }, |
| 66 | + }); |
| 67 | + |
| 68 | + expect(result.success).toBe(true); |
| 69 | + if (result.success) { |
| 70 | + expect(result.data.options?.concurrencyKey).toBe("51262"); |
| 71 | + } |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe("TriggerTaskRequestBody", () => { |
| 76 | + it("coerces a numeric concurrencyKey to a string", () => { |
| 77 | + const result = TriggerTaskRequestBody.safeParse({ |
| 78 | + payload: { userId: 51262 }, |
| 79 | + options: { concurrencyKey: 51262 }, |
| 80 | + }); |
| 81 | + |
| 82 | + expect(result.success).toBe(true); |
| 83 | + if (result.success) { |
| 84 | + expect(result.data.options?.concurrencyKey).toBe("51262"); |
| 85 | + } |
| 86 | + }); |
| 87 | + }); |
| 88 | +}); |
0 commit comments