@dudousxd/nestjs-durable-testing 0.23.5 → 0.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -167,7 +167,10 @@ interface TestEngine {
167
167
  store: InMemoryStateStore;
168
168
  transport: InMemoryTransport;
169
169
  clock: MutableClock;
170
- /** Advance the clock by `ms` and resume any durable sleeps now due. */
170
+ /**
171
+ * Advance the clock by `ms`, resume any durable sleeps now due, publish owed lifecycle events and
172
+ * resume `ctx.waitForRun` waits whose run settled.
173
+ */
171
174
  tick(ms: number): Promise<RunResult[]>;
172
175
  /**
173
176
  * Enqueue a run and wait for it to settle (terminal or suspended). `engine.start` only enqueues
@@ -268,6 +271,13 @@ interface StateStoreContext {
268
271
  * sends none of the step's input.
269
272
  */
270
273
  statementLog?: () => string[];
274
+ /**
275
+ * Resolves once {@link statements} and {@link statementLog} count every statement the store sent
276
+ * before the call. For a driver that reports a statement apart from its result (Prisma's query
277
+ * events), so a count read right after a call can miss it or a later count can pick it up. Without
278
+ * it, the contract waits for the log to stop growing.
279
+ */
280
+ flushStatementLog?: () => Promise<void>;
271
281
  /**
272
282
  * The store's client opens a transaction around every write of its own (Prisma does, even for one
273
283
  * `createMany` or `updateMany`). The contract then checks that a run's row is written without a
package/dist/index.js CHANGED
@@ -364,7 +364,9 @@ function createTestEngine() {
364
364
  clock,
365
365
  async tick(ms2) {
366
366
  clock.advance(ms2);
367
- return engine.resumeDueTimers(clock.now());
367
+ const resumed = await engine.resumeDueTimers(clock.now());
368
+ await engine.sweepLifecycleEvents(clock.now());
369
+ return resumed;
368
370
  },
369
371
  async run(workflow, input, runId, opts) {
370
372
  await engine.start(workflow, input, runId, opts);
@@ -442,7 +444,7 @@ async function assertReplayable(register, history) {
442
444
  __name(assertReplayable, "assertReplayable");
443
445
 
444
446
  // src/state-store-conformance.ts
445
- import { SINGLETON_ADMITTED_TAG, SINGLETON_TRACKED_TAG, WorkflowEngine as WorkflowEngine3 } from "@dudousxd/nestjs-durable-core";
447
+ import { LIFECYCLE_PENDING_TAG, LIFECYCLE_REGISTRY_TAG, LIFECYCLE_REGISTRY_WORKFLOW, SINGLETON_ADMITTED_TAG, SINGLETON_TRACKED_TAG, WorkflowEngine as WorkflowEngine3 } from "@dudousxd/nestjs-durable-core";
446
448
  import { beforeEach, describe as describe2, expect as expect3, it as it2 } from "vitest";
447
449
 
448
450
  // src/checkpoint-aggregation-contract.ts
@@ -839,12 +841,22 @@ function sqlOf(logged) {
839
841
  return logged.replace(ANSI_COLOR, "").replace(/^\s*\[query\]\s*/i, "").trim();
840
842
  }
841
843
  __name(sqlOf, "sqlOf");
842
- async function loggedSince(log, before) {
844
+ async function settleLog(log, flush) {
845
+ if (flush) return flush();
843
846
  let seen = -1;
844
847
  for (let i = 0; i < 50 && log().length !== seen; i += 1) {
845
848
  seen = log().length;
846
849
  await new Promise((resolve) => setTimeout(resolve, 10));
847
850
  }
851
+ }
852
+ __name(settleLog, "settleLog");
853
+ async function loggedLength(log, flush) {
854
+ await settleLog(log, flush);
855
+ return log().length;
856
+ }
857
+ __name(loggedLength, "loggedLength");
858
+ async function loggedSince(log, before, flush) {
859
+ await settleLog(log, flush);
848
860
  return log().slice(before);
849
861
  }
850
862
  __name(loggedSince, "loggedSince");
@@ -857,6 +869,7 @@ function runStateStoreContract(name, makeStore) {
857
869
  let supportsTagFilter = true;
858
870
  let statements;
859
871
  let statementLog;
872
+ let flushStatementLog;
860
873
  let implicitTransactions = false;
861
874
  let skipReason;
862
875
  let loggedSkip = false;
@@ -870,6 +883,7 @@ function runStateStoreContract(name, makeStore) {
870
883
  supportsTagFilter = ctx.supportsTagFilter ?? true;
871
884
  statements = ctx.statements;
872
885
  statementLog = ctx.statementLog;
886
+ flushStatementLog = ctx.flushStatementLog;
873
887
  implicitTransactions = ctx.implicitTransactions ?? false;
874
888
  skipReason = void 0;
875
889
  } catch (err) {
@@ -1414,6 +1428,108 @@ function runStateStoreContract(name, makeStore) {
1414
1428
  "c2"
1415
1429
  ]);
1416
1430
  });
1431
+ t("pruneTerminalRuns keeps a run whose lifecycle event is still owed", async () => {
1432
+ await store.createRun(aged("owed", 10, {
1433
+ tags: [
1434
+ "etl",
1435
+ LIFECYCLE_PENDING_TAG
1436
+ ]
1437
+ }));
1438
+ await store.createRun(aged("published", 10, {
1439
+ tags: [
1440
+ "etl",
1441
+ "durable:triggered:refresh"
1442
+ ]
1443
+ }));
1444
+ await store.createRun(aged("owed-over-cap", 2, {
1445
+ tags: [
1446
+ LIFECYCLE_PENDING_TAG
1447
+ ]
1448
+ }));
1449
+ await store.createRun(aged("old", 8));
1450
+ await store.createRun(aged("newest", 1));
1451
+ await store.pruneTerminalRuns?.({
1452
+ statuses: [
1453
+ "completed"
1454
+ ],
1455
+ maxAge: "7d"
1456
+ }, NOW, 100);
1457
+ expect3(await ids()).toEqual([
1458
+ "newest",
1459
+ "owed",
1460
+ "owed-over-cap"
1461
+ ]);
1462
+ await store.createRun(aged("mid", 2));
1463
+ await store.pruneTerminalRuns?.({
1464
+ statuses: [
1465
+ "completed"
1466
+ ],
1467
+ maxCount: 1
1468
+ }, NOW, 100);
1469
+ expect3(await ids()).toEqual([
1470
+ "newest",
1471
+ "owed",
1472
+ "owed-over-cap"
1473
+ ]);
1474
+ });
1475
+ t("pruneTerminalRuns keeps lifecycle registry runs", async () => {
1476
+ await store.createRun(aged("__lifecycle_registry:a", 10, {
1477
+ workflow: LIFECYCLE_REGISTRY_WORKFLOW,
1478
+ tags: [
1479
+ LIFECYCLE_REGISTRY_TAG
1480
+ ]
1481
+ }));
1482
+ await store.createRun(aged("old", 10));
1483
+ await store.pruneTerminalRuns?.({
1484
+ statuses: [
1485
+ "completed"
1486
+ ],
1487
+ maxAge: "7d"
1488
+ }, NOW, 100);
1489
+ expect3(await ids()).toEqual([
1490
+ "__lifecycle_registry:a"
1491
+ ]);
1492
+ await store.pruneTerminalRuns?.({
1493
+ statuses: [
1494
+ "completed"
1495
+ ],
1496
+ maxCount: 0
1497
+ }, NOW, 100);
1498
+ expect3(await ids()).toEqual([
1499
+ "__lifecycle_registry:a"
1500
+ ]);
1501
+ });
1502
+ t("pruneTerminalRuns does not count lifecycle registry runs toward maxCount", async () => {
1503
+ for (const instance of [
1504
+ "a",
1505
+ "b"
1506
+ ]) {
1507
+ await store.createRun(aged(`__lifecycle_registry:${instance}`, 0, {
1508
+ workflow: LIFECYCLE_REGISTRY_WORKFLOW,
1509
+ tags: [
1510
+ LIFECYCLE_REGISTRY_TAG
1511
+ ]
1512
+ }));
1513
+ }
1514
+ await store.createRun(aged("owed", 1, {
1515
+ tags: [
1516
+ LIFECYCLE_PENDING_TAG
1517
+ ]
1518
+ }));
1519
+ for (let i = 2; i <= 4; i++) await store.createRun(aged(`c${i}`, i));
1520
+ await store.pruneTerminalRuns?.({
1521
+ statuses: [
1522
+ "completed"
1523
+ ],
1524
+ maxCount: 2
1525
+ }, NOW, 100);
1526
+ expect3(await ids()).toEqual([
1527
+ "__lifecycle_registry:a",
1528
+ "__lifecycle_registry:b",
1529
+ "c2",
1530
+ "owed"
1531
+ ]);
1532
+ });
1417
1533
  t("a scoped retention policy only prunes (and counts) the runs its scope matches", async () => {
1418
1534
  await store.createRun(aged("acme-old", 10, {
1419
1535
  namespace: "acme"
@@ -2395,8 +2511,10 @@ function runStateStoreContract(name, makeStore) {
2395
2511
  output: void 0
2396
2512
  }));
2397
2513
  const sent = /* @__PURE__ */ __name(async (work) => {
2514
+ await flushStatementLog?.();
2398
2515
  const before = statements();
2399
2516
  await work();
2517
+ await flushStatementLog?.();
2400
2518
  return statements() - before;
2401
2519
  }, "sent");
2402
2520
  expect3(await sent(() => store.saveCheckpoints?.(rows))).toBeLessThanOrEqual(limit);
@@ -2648,7 +2766,7 @@ function runStateStoreContract(name, makeStore) {
2648
2766
  output: void 0
2649
2767
  }));
2650
2768
  }
2651
- const before = statementLog().length;
2769
+ const before = await loggedLength(statementLog, flushStatementLog);
2652
2770
  const settle2 = /* @__PURE__ */ __name((seqs) => store.settleCheckpointsIfPending?.(seqs.map((seq) => ({
2653
2771
  runId: "r1",
2654
2772
  seq,
@@ -2671,7 +2789,7 @@ function runStateStoreContract(name, makeStore) {
2671
2789
  true,
2672
2790
  false
2673
2791
  ]);
2674
- const sent = await loggedSince(statementLog, before);
2792
+ const sent = await loggedSince(statementLog, before, flushStatementLog);
2675
2793
  expect3(sent.length).toBeGreaterThan(0);
2676
2794
  expect3(sent.some((statement) => statement.includes("xxxxxxxxxx"))).toBe(false);
2677
2795
  expect3(sent.join("").length).toBeLessThan(2e4);
@@ -2741,9 +2859,9 @@ function runStateStoreContract(name, makeStore) {
2741
2859
  await store.createRun(run({
2742
2860
  id: "warm"
2743
2861
  }));
2744
- const before = statementLog().length;
2862
+ const before = await loggedLength(statementLog, flushStatementLog);
2745
2863
  await store.createRun(run());
2746
- const sent = await loggedSince(statementLog, before);
2864
+ const sent = await loggedSince(statementLog, before, flushStatementLog);
2747
2865
  expect3(sent.length).toBeGreaterThan(0);
2748
2866
  expect3(sent.map(sqlOf).filter((q) => /^select/i.test(q))).toEqual([]);
2749
2867
  if (!implicitTransactions) {
@@ -2758,13 +2876,13 @@ function runStateStoreContract(name, makeStore) {
2758
2876
  status: "running",
2759
2877
  updatedAt: /* @__PURE__ */ new Date()
2760
2878
  });
2761
- const before = statementLog().length;
2879
+ const before = await loggedLength(statementLog, flushStatementLog);
2762
2880
  await store.updateRun("r1", {
2763
2881
  status: "suspended",
2764
2882
  wakeAt: 5e3,
2765
2883
  updatedAt: /* @__PURE__ */ new Date()
2766
2884
  });
2767
- const sent = await loggedSince(statementLog, before);
2885
+ const sent = await loggedSince(statementLog, before, flushStatementLog);
2768
2886
  expect3(sent.map(sqlOf).filter((q) => /^update/i.test(q))).toHaveLength(1);
2769
2887
  expect3(sent.map(sqlOf).filter((q) => /^select/i.test(q))).toEqual([]);
2770
2888
  if (!implicitTransactions) {
@@ -2792,12 +2910,12 @@ function runStateStoreContract(name, makeStore) {
2792
2910
  output: void 0
2793
2911
  }));
2794
2912
  }
2795
- const before = statementLog().length;
2913
+ const before = await loggedLength(statementLog, flushStatementLog);
2796
2914
  const read = await store.getCheckpointsForSettle("r1", [
2797
2915
  0,
2798
2916
  1
2799
2917
  ]);
2800
- const sent = await loggedSince(statementLog, before);
2918
+ const sent = await loggedSince(statementLog, before, flushStatementLog);
2801
2919
  expect3(read).toHaveLength(2);
2802
2920
  expect3(sent.length).toBeGreaterThan(0);
2803
2921
  expect3(sent.filter((statement) => /\binput\b|\*\s+from/i.test(statement))).toEqual([]);
@@ -2944,9 +3062,9 @@ function runStateStoreContract(name, makeStore) {
2944
3062
  }
2945
3063
  }));
2946
3064
  }
2947
- const before = statementLog().length;
3065
+ const before = await loggedLength(statementLog, flushStatementLog);
2948
3066
  const replay = await store.listCheckpointsForReplay("r1");
2949
- const sent = await loggedSince(statementLog, before);
3067
+ const sent = await loggedSince(statementLog, before, flushStatementLog);
2950
3068
  expect3(replay.map((c) => c.output)).toEqual([
2951
3069
  {
2952
3070
  answer: 0
@@ -3529,6 +3647,196 @@ function runStateStoreContract(name, makeStore) {
3529
3647
  "c"
3530
3648
  ]);
3531
3649
  });
3650
+ t("keeps only the runs updated at or after updatedAfter", async () => {
3651
+ const at2 = /* @__PURE__ */ __name((ms2) => new Date(17e11 + ms2), "at");
3652
+ await store.createRun(run({
3653
+ id: "old",
3654
+ status: "completed",
3655
+ createdAt: at2(0),
3656
+ updatedAt: at2(0)
3657
+ }));
3658
+ await store.createRun(run({
3659
+ id: "edge",
3660
+ status: "completed",
3661
+ createdAt: at2(0),
3662
+ updatedAt: at2(5e3)
3663
+ }));
3664
+ await store.createRun(run({
3665
+ id: "new",
3666
+ status: "failed",
3667
+ createdAt: at2(0),
3668
+ updatedAt: at2(9e3)
3669
+ }));
3670
+ const after = at2(5e3).getTime();
3671
+ expect3((await store.listRuns({
3672
+ updatedAfter: after
3673
+ })).map((r) => r.id).sort()).toEqual([
3674
+ "edge",
3675
+ "new"
3676
+ ]);
3677
+ expect3((await store.listRuns({
3678
+ updatedAfter: after,
3679
+ statuses: [
3680
+ "completed"
3681
+ ]
3682
+ })).map((r) => r.id)).toEqual([
3683
+ "edge"
3684
+ ]);
3685
+ if (store.countRuns) expect3(await store.countRuns({
3686
+ updatedAfter: after
3687
+ })).toBe(2);
3688
+ });
3689
+ t("leaves out the runs of excludeWorkflows, in lists and in counts", async () => {
3690
+ await store.createRun(run({
3691
+ id: "a",
3692
+ status: "failed",
3693
+ workflow: "wa"
3694
+ }));
3695
+ await store.createRun(run({
3696
+ id: "b",
3697
+ status: "completed",
3698
+ workflow: "wb"
3699
+ }));
3700
+ await store.createRun(run({
3701
+ id: "c",
3702
+ status: "completed",
3703
+ workflow: "wc"
3704
+ }));
3705
+ expect3((await store.listRuns({
3706
+ excludeWorkflows: [
3707
+ "wb",
3708
+ "wc"
3709
+ ]
3710
+ })).map((r) => r.id).sort()).toEqual([
3711
+ "a"
3712
+ ]);
3713
+ expect3((await store.listRuns({
3714
+ excludeWorkflows: [
3715
+ "wb"
3716
+ ],
3717
+ statuses: [
3718
+ "completed"
3719
+ ]
3720
+ })).map((r) => r.id)).toEqual([
3721
+ "c"
3722
+ ]);
3723
+ expect3((await store.listRuns({
3724
+ excludeWorkflows: []
3725
+ })).map((r) => r.id).sort()).toEqual([
3726
+ "a",
3727
+ "b",
3728
+ "c"
3729
+ ]);
3730
+ if (store.countRuns) expect3(await store.countRuns({
3731
+ excludeWorkflows: [
3732
+ "wa"
3733
+ ]
3734
+ })).toBe(2);
3735
+ if (store.runFacets) {
3736
+ const rows = await store.runFacets({
3737
+ excludeWorkflows: [
3738
+ "wb"
3739
+ ]
3740
+ });
3741
+ expect3(rows.reduce((sum, row) => sum + row.count, 0)).toBe(2);
3742
+ }
3743
+ if (store.runValueFacets) {
3744
+ const rows = await store.runValueFacets({
3745
+ field: "workflow"
3746
+ }, {
3747
+ excludeWorkflows: [
3748
+ "wb"
3749
+ ]
3750
+ });
3751
+ expect3(rows.map((row) => row.value).sort()).toEqual([
3752
+ "wa",
3753
+ "wc"
3754
+ ]);
3755
+ }
3756
+ });
3757
+ t("holds a lifecycle registry run: created, refreshed in place, listed by workflow", async () => {
3758
+ const id = "__lifecycle_registry:instance-1";
3759
+ const subscriptions = [
3760
+ {
3761
+ workflow: "notify",
3762
+ events: [
3763
+ "run.completed:ingest"
3764
+ ]
3765
+ }
3766
+ ];
3767
+ expect3(await store.updateRunIf?.(id, [
3768
+ "completed"
3769
+ ], {
3770
+ updatedAt: new Date(NOW)
3771
+ })).not.toBe(true);
3772
+ await store.createRun(run({
3773
+ id,
3774
+ workflow: LIFECYCLE_REGISTRY_WORKFLOW,
3775
+ status: "completed",
3776
+ input: {
3777
+ instanceId: "instance-1",
3778
+ runtime: "node",
3779
+ subscriptions
3780
+ },
3781
+ tags: [
3782
+ LIFECYCLE_REGISTRY_TAG
3783
+ ],
3784
+ createdAt: new Date(NOW - 2e4),
3785
+ updatedAt: new Date(NOW - 2e4)
3786
+ }));
3787
+ await store.createRun(run({
3788
+ id: "app",
3789
+ workflow: "ingest",
3790
+ status: "completed"
3791
+ }));
3792
+ const more = [
3793
+ ...subscriptions,
3794
+ {
3795
+ workflow: "audit",
3796
+ events: [
3797
+ "run.failed:ingest"
3798
+ ]
3799
+ }
3800
+ ];
3801
+ const input = {
3802
+ instanceId: "instance-1",
3803
+ runtime: "node",
3804
+ subscriptions: more
3805
+ };
3806
+ if (store.updateRunIf) {
3807
+ expect3(await store.updateRunIf(id, [
3808
+ "completed"
3809
+ ], {
3810
+ input,
3811
+ updatedAt: new Date(NOW)
3812
+ })).toBe(true);
3813
+ } else {
3814
+ await store.updateRun(id, {
3815
+ input,
3816
+ updatedAt: new Date(NOW)
3817
+ });
3818
+ }
3819
+ const listed = await store.listRuns({
3820
+ workflow: LIFECYCLE_REGISTRY_WORKFLOW
3821
+ });
3822
+ expect3(listed.map((r) => r.id)).toEqual([
3823
+ id
3824
+ ]);
3825
+ expect3(listed[0]?.input).toEqual(input);
3826
+ expect3(listed[0]?.updatedAt.getTime()).toBe(NOW);
3827
+ expect3(listed[0]?.status).toBe("completed");
3828
+ expect3(listed[0]?.tags).toEqual([
3829
+ LIFECYCLE_REGISTRY_TAG
3830
+ ]);
3831
+ expect3(await store.listIncompleteRuns()).toEqual([]);
3832
+ expect3((await store.listRuns({
3833
+ excludeWorkflows: [
3834
+ LIFECYCLE_REGISTRY_WORKFLOW
3835
+ ]
3836
+ })).map((r) => r.id)).toEqual([
3837
+ "app"
3838
+ ]);
3839
+ });
3532
3840
  t("carries the absent-origin bucket INSIDE a set of origins", async () => {
3533
3841
  await store.createRun(run({
3534
3842
  id: "a",
@@ -4984,8 +5292,10 @@ function runStateStoreContract(name, makeStore) {
4984
5292
  const engine = new WorkflowEngine3({
4985
5293
  store
4986
5294
  });
5295
+ await flushStatementLog?.();
4987
5296
  const before = statements();
4988
5297
  const deleted = await engine.sweepBufferedSignals();
5298
+ await flushStatementLog?.();
4989
5299
  const sent = statements() - before;
4990
5300
  expect3(deleted).toBe(rows);
4991
5301
  expect3(await store.listBufferedSignals("child:", void 0, 10)).toEqual([]);