|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { flow, trigger } from "."; |
| 3 | +import { invokeFlow, invokeTrigger } from "./testing"; |
| 4 | +import type { BatchInfo } from "./types/BatchContext"; |
| 5 | + |
| 6 | +describe("context.batch", () => { |
| 7 | + const echoBatch = trigger({ |
| 8 | + display: { label: "Echo Batch", description: "Returns the batch context it saw." }, |
| 9 | + inputs: {}, |
| 10 | + perform: async (context) => ({ |
| 11 | + payload: { |
| 12 | + ...({} as never), |
| 13 | + body: { data: context.batch ?? null }, |
| 14 | + }, |
| 15 | + }), |
| 16 | + scheduleSupport: "invalid", |
| 17 | + synchronousResponseSupport: "invalid", |
| 18 | + }); |
| 19 | + |
| 20 | + it("reaches a component trigger's perform", async () => { |
| 21 | + const { result } = await invokeTrigger(echoBatch, { |
| 22 | + batch: { enabled: true, batchSize: 50 }, |
| 23 | + }); |
| 24 | + expect(result?.payload.body.data).toEqual({ enabled: true, batchSize: 50 }); |
| 25 | + }); |
| 26 | + |
| 27 | + it("is undefined when the context omits it", async () => { |
| 28 | + const { result } = await invokeTrigger(echoBatch); |
| 29 | + expect(result?.payload.body.data).toBeNull(); |
| 30 | + }); |
| 31 | + |
| 32 | + it("carries enabled false through to the perform", async () => { |
| 33 | + const { result } = await invokeTrigger(echoBatch, { batch: { enabled: false } }); |
| 34 | + expect(result?.payload.body.data).toEqual({ enabled: false }); |
| 35 | + }); |
| 36 | + |
| 37 | + it("exposes batchSize on the enabled variant", () => { |
| 38 | + const disabled: BatchInfo = { enabled: false }; |
| 39 | + const enabled: BatchInfo = { enabled: true, batchSize: 25 }; |
| 40 | + // @ts-expect-error batchSize belongs to the enabled variant. |
| 41 | + expect(disabled.batchSize).toBeUndefined(); |
| 42 | + expect(enabled.enabled ? enabled.batchSize : 0).toBe(25); |
| 43 | + }); |
| 44 | + |
| 45 | + it("reaches a CNI flow's onExecution", async () => { |
| 46 | + const seen: unknown[] = []; |
| 47 | + const cniFlow = flow({ |
| 48 | + name: "Batch Aware Flow", |
| 49 | + stableKey: "batch-aware-flow", |
| 50 | + description: "Records the batch context its step body saw.", |
| 51 | + onExecution: async (context) => { |
| 52 | + seen.push(context.batch ?? null); |
| 53 | + return Promise.resolve({ data: null }); |
| 54 | + }, |
| 55 | + }); |
| 56 | + |
| 57 | + await invokeFlow(cniFlow, { context: { batch: { enabled: true, batchSize: 25 } } }); |
| 58 | + |
| 59 | + expect(seen).toEqual([{ enabled: true, batchSize: 25 }]); |
| 60 | + }); |
| 61 | +}); |
0 commit comments