|
1 | | -import { describe, it, expect } from "vitest"; |
| 1 | +import { describe, it, expect, afterEach, vi } from "vitest"; |
2 | 2 | import { readableStreamToAsyncIterable } from "./shared.js"; |
3 | 3 |
|
| 4 | +let taskIdCounter = 0; |
| 5 | + |
4 | 6 | describe("readableStreamToAsyncIterable", () => { |
5 | 7 | it("yields all values from the stream", async () => { |
6 | 8 | const values = [1, 2, 3, 4, 5]; |
@@ -217,3 +219,114 @@ describe("readableStreamToAsyncIterable", () => { |
217 | 219 | }); |
218 | 220 | }); |
219 | 221 |
|
| 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 | +}); |
0 commit comments