Skip to content

Commit 6802690

Browse files
Gautam ManchandaniGautam Manchandani
authored andcommitted
Fix batch wait debounce forwarding
Signed-off-by: Gautam Manchandani <gautammanch@Gautams-MacBook-Air.local>
1 parent 8dd1fc1 commit 6802690

3 files changed

Lines changed: 120 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@trigger.dev/sdk": patch
3+
---
4+
5+
Forward per-item debounce options in array batchTriggerAndWait calls.

packages/trigger-sdk/src/v3/shared.test.ts

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { describe, it, expect } from "vitest";
1+
import { describe, it, expect, afterEach, vi } from "vitest";
22
import { readableStreamToAsyncIterable } from "./shared.js";
33

4+
let taskIdCounter = 0;
5+
46
describe("readableStreamToAsyncIterable", () => {
57
it("yields all values from the stream", async () => {
68
const values = [1, 2, 3, 4, 5];
@@ -217,3 +219,114 @@ describe("readableStreamToAsyncIterable", () => {
217219
});
218220
});
219221

222+
describe("batchTriggerAndWait debounce forwarding", () => {
223+
afterEach(() => {
224+
vi.doUnmock("@trigger.dev/core/v3");
225+
vi.resetModules();
226+
vi.restoreAllMocks();
227+
});
228+
229+
async function setupBatchTriggerAndWaitHarness() {
230+
vi.resetModules();
231+
232+
const capturedItems: any[] = [];
233+
234+
const createBatch = vi.fn(async ({ runCount }: { runCount: number }) => ({
235+
id: "batch_test123",
236+
runCount,
237+
publicAccessToken: "access_token",
238+
isCached: false,
239+
}));
240+
241+
const streamBatchItems = vi.fn(async (_batchId: string, items: any[]) => {
242+
capturedItems.push(...items);
243+
244+
return {
245+
id: "batch_test123",
246+
itemsAccepted: items.length,
247+
itemsDeduplicated: 0,
248+
sealed: true,
249+
};
250+
});
251+
252+
const waitForBatch = vi.fn(async ({ id }: { id: string }) => ({
253+
id,
254+
items: [],
255+
}));
256+
257+
vi.doMock("@trigger.dev/core/v3", async (importOriginal) => {
258+
const original = await importOriginal<typeof import("@trigger.dev/core/v3")>();
259+
260+
return {
261+
...original,
262+
apiClientManager: {
263+
clientOrThrow: vi.fn(() => ({
264+
createBatch,
265+
streamBatchItems,
266+
})),
267+
} as any,
268+
runtime: {
269+
...original.runtime,
270+
waitForBatch,
271+
} as any,
272+
taskContext: {
273+
ctx: {
274+
run: {
275+
id: "run_123",
276+
isTest: false,
277+
},
278+
},
279+
worker: {
280+
version: "worker_123",
281+
},
282+
} as any,
283+
};
284+
});
285+
286+
const tasksModule = await import("./tasks.js");
287+
288+
return {
289+
...tasksModule,
290+
capturedItems,
291+
createBatch,
292+
streamBatchItems,
293+
waitForBatch,
294+
};
295+
}
296+
297+
it("forwards per-item debounce for task.batchTriggerAndWait array items", async () => {
298+
const { task, capturedItems, streamBatchItems, waitForBatch } =
299+
await setupBatchTriggerAndWaitHarness();
300+
const debounce = { key: "same-key", delay: "30s", mode: "trailing" as const };
301+
const taskId = `batch-debounce-task-${++taskIdCounter}`;
302+
303+
const myTask = task({
304+
id: taskId,
305+
run: async (payload: { id: string }) => payload,
306+
});
307+
308+
await myTask.batchTriggerAndWait([{ payload: { id: "a" }, options: { debounce } }]);
309+
310+
expect(streamBatchItems).toHaveBeenCalledTimes(1);
311+
expect(waitForBatch).toHaveBeenCalledTimes(1);
312+
expect(capturedItems).toHaveLength(1);
313+
expect(capturedItems[0]?.task).toBe(taskId);
314+
expect(capturedItems[0]?.options?.debounce).toEqual(debounce);
315+
});
316+
317+
it("forwards per-item debounce for tasks.batchTriggerAndWait array items", async () => {
318+
const { tasks, capturedItems, streamBatchItems, waitForBatch } =
319+
await setupBatchTriggerAndWaitHarness();
320+
const debounce = { key: "same-key", delay: "30s", mode: "trailing" as const };
321+
322+
await tasks.batchTriggerAndWait("batch-debounce-by-id-task", [
323+
{ payload: { id: "a" }, options: { debounce } },
324+
]);
325+
326+
expect(streamBatchItems).toHaveBeenCalledTimes(1);
327+
expect(waitForBatch).toHaveBeenCalledTimes(1);
328+
expect(capturedItems).toHaveLength(1);
329+
expect(capturedItems[0]?.task).toBe("batch-debounce-by-id-task");
330+
expect(capturedItems[0]?.options?.debounce).toEqual(debounce);
331+
});
332+
});

packages/trigger-sdk/src/v3/shared.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2507,6 +2507,7 @@ async function batchTriggerAndWait_internal<TIdentifier extends string, TPayload
25072507
machine: item.options?.machine,
25082508
priority: item.options?.priority,
25092509
region: item.options?.region,
2510+
debounce: item.options?.debounce,
25102511
},
25112512
};
25122513
})

0 commit comments

Comments
 (0)