|
1 | | -import { batch, idempotencyKeys, logger, task, timeout, usage, wait } from "@trigger.dev/sdk/v3"; |
| 1 | +import { batch, idempotencyKeys, logger, runs, task, timeout, usage, wait } from "@trigger.dev/sdk/v3"; |
2 | 2 | import { setTimeout } from "timers/promises"; |
3 | 3 | import { childTask } from "./example.js"; |
4 | 4 |
|
@@ -388,3 +388,155 @@ export const idempotencyKeyOptionsTest = task({ |
388 | 388 | }; |
389 | 389 | }, |
390 | 390 | }); |
| 391 | + |
| 392 | +// Test task for verifying idempotencyKeys.reset works with the new API (TRI-4352) |
| 393 | +export const idempotencyKeyResetTest = task({ |
| 394 | + id: "idempotency-key-reset-test", |
| 395 | + maxDuration: 120, |
| 396 | + run: async (payload: any, { ctx }) => { |
| 397 | + logger.log("Testing idempotencyKeys.reset feature (TRI-4352)"); |
| 398 | + |
| 399 | + const testResults: Array<{ |
| 400 | + test: string; |
| 401 | + success: boolean; |
| 402 | + details: Record<string, unknown>; |
| 403 | + }> = []; |
| 404 | + |
| 405 | + // Test 1: Reset using IdempotencyKey object (options extracted automatically) |
| 406 | + { |
| 407 | + const key = await idempotencyKeys.create("reset-test-key-1", { scope: "global" }); |
| 408 | + logger.log("Test 1: Created global-scoped key", { key: key.toString() }); |
| 409 | + |
| 410 | + // First trigger - should create a new run |
| 411 | + const result1 = await idempotencyKeyOptionsChild.triggerAndWait( |
| 412 | + { message: "First trigger" }, |
| 413 | + { idempotencyKey: key, idempotencyKeyTTL: "300s" } |
| 414 | + ); |
| 415 | + const firstRunId = result1.ok ? result1.id : null; |
| 416 | + logger.log("Test 1: First trigger", { runId: firstRunId }); |
| 417 | + |
| 418 | + // Second trigger - should be deduplicated (same run ID) |
| 419 | + const result2 = await idempotencyKeyOptionsChild.triggerAndWait( |
| 420 | + { message: "Second trigger (should dedupe)" }, |
| 421 | + { idempotencyKey: key, idempotencyKeyTTL: "300s" } |
| 422 | + ); |
| 423 | + const secondRunId = result2.ok ? result2.id : null; |
| 424 | + logger.log("Test 1: Second trigger (dedupe check)", { runId: secondRunId }); |
| 425 | + |
| 426 | + const wasDeduplicated = firstRunId === secondRunId; |
| 427 | + |
| 428 | + // Reset the idempotency key using the IdempotencyKey object |
| 429 | + logger.log("Test 1: Resetting idempotency key using IdempotencyKey object"); |
| 430 | + await idempotencyKeys.reset("idempotency-key-options-child", key); |
| 431 | + |
| 432 | + // Third trigger - should create a NEW run after reset |
| 433 | + const result3 = await idempotencyKeyOptionsChild.triggerAndWait( |
| 434 | + { message: "Third trigger (after reset)" }, |
| 435 | + { idempotencyKey: key, idempotencyKeyTTL: "300s" } |
| 436 | + ); |
| 437 | + const thirdRunId = result3.ok ? result3.id : null; |
| 438 | + logger.log("Test 1: Third trigger (after reset)", { runId: thirdRunId }); |
| 439 | + |
| 440 | + const wasResetSuccessful = thirdRunId !== firstRunId && thirdRunId !== null; |
| 441 | + |
| 442 | + testResults.push({ |
| 443 | + test: "Reset with IdempotencyKey object (global scope)", |
| 444 | + success: wasDeduplicated && wasResetSuccessful, |
| 445 | + details: { |
| 446 | + firstRunId, |
| 447 | + secondRunId, |
| 448 | + thirdRunId, |
| 449 | + wasDeduplicated, |
| 450 | + wasResetSuccessful, |
| 451 | + }, |
| 452 | + }); |
| 453 | + } |
| 454 | + |
| 455 | + // Test 2: Reset using raw string with scope option |
| 456 | + { |
| 457 | + const keyString = "reset-test-key-2"; |
| 458 | + const key = await idempotencyKeys.create(keyString, { scope: "global" }); |
| 459 | + logger.log("Test 2: Created global-scoped key from string", { key: key.toString() }); |
| 460 | + |
| 461 | + // First trigger |
| 462 | + const result1 = await idempotencyKeyOptionsChild.triggerAndWait( |
| 463 | + { message: "First trigger (raw string test)" }, |
| 464 | + { idempotencyKey: key, idempotencyKeyTTL: "300s" } |
| 465 | + ); |
| 466 | + const firstRunId = result1.ok ? result1.id : null; |
| 467 | + logger.log("Test 2: First trigger", { runId: firstRunId }); |
| 468 | + |
| 469 | + // Reset using raw string + scope option |
| 470 | + logger.log("Test 2: Resetting idempotency key using raw string + scope"); |
| 471 | + await idempotencyKeys.reset("idempotency-key-options-child", keyString, { scope: "global" }); |
| 472 | + |
| 473 | + // Second trigger - should create a NEW run after reset |
| 474 | + const result2 = await idempotencyKeyOptionsChild.triggerAndWait( |
| 475 | + { message: "Second trigger (after reset with raw string)" }, |
| 476 | + { idempotencyKey: key, idempotencyKeyTTL: "300s" } |
| 477 | + ); |
| 478 | + const secondRunId = result2.ok ? result2.id : null; |
| 479 | + logger.log("Test 2: Second trigger (after reset)", { runId: secondRunId }); |
| 480 | + |
| 481 | + const wasResetSuccessful = secondRunId !== firstRunId && secondRunId !== null; |
| 482 | + |
| 483 | + testResults.push({ |
| 484 | + test: "Reset with raw string + scope option (global scope)", |
| 485 | + success: wasResetSuccessful, |
| 486 | + details: { |
| 487 | + firstRunId, |
| 488 | + secondRunId, |
| 489 | + wasResetSuccessful, |
| 490 | + }, |
| 491 | + }); |
| 492 | + } |
| 493 | + |
| 494 | + // Test 3: Reset with run scope (uses current run context) |
| 495 | + { |
| 496 | + const key = await idempotencyKeys.create("reset-test-key-3", { scope: "run" }); |
| 497 | + logger.log("Test 3: Created run-scoped key", { key: key.toString() }); |
| 498 | + |
| 499 | + // First trigger |
| 500 | + const result1 = await idempotencyKeyOptionsChild.triggerAndWait( |
| 501 | + { message: "First trigger (run scope)" }, |
| 502 | + { idempotencyKey: key, idempotencyKeyTTL: "300s" } |
| 503 | + ); |
| 504 | + const firstRunId = result1.ok ? result1.id : null; |
| 505 | + logger.log("Test 3: First trigger", { runId: firstRunId }); |
| 506 | + |
| 507 | + // Reset using IdempotencyKey (run scope - should use current run context) |
| 508 | + logger.log("Test 3: Resetting idempotency key with run scope"); |
| 509 | + await idempotencyKeys.reset("idempotency-key-options-child", key); |
| 510 | + |
| 511 | + // Second trigger - should create a NEW run after reset |
| 512 | + const result2 = await idempotencyKeyOptionsChild.triggerAndWait( |
| 513 | + { message: "Second trigger (after reset, run scope)" }, |
| 514 | + { idempotencyKey: key, idempotencyKeyTTL: "300s" } |
| 515 | + ); |
| 516 | + const secondRunId = result2.ok ? result2.id : null; |
| 517 | + logger.log("Test 3: Second trigger (after reset)", { runId: secondRunId }); |
| 518 | + |
| 519 | + const wasResetSuccessful = secondRunId !== firstRunId && secondRunId !== null; |
| 520 | + |
| 521 | + testResults.push({ |
| 522 | + test: "Reset with IdempotencyKey object (run scope)", |
| 523 | + success: wasResetSuccessful, |
| 524 | + details: { |
| 525 | + firstRunId, |
| 526 | + secondRunId, |
| 527 | + wasResetSuccessful, |
| 528 | + parentRunId: ctx.run.id, |
| 529 | + }, |
| 530 | + }); |
| 531 | + } |
| 532 | + |
| 533 | + // Summary |
| 534 | + const allPassed = testResults.every((r) => r.success); |
| 535 | + logger.log("Test summary", { allPassed, testResults }); |
| 536 | + |
| 537 | + return { |
| 538 | + allPassed, |
| 539 | + testResults, |
| 540 | + }; |
| 541 | + }, |
| 542 | +}); |
0 commit comments