@dudousxd/nestjs-durable-testing 0.23.4 → 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) {
@@ -1213,6 +1227,34 @@ function runStateStoreContract(name, makeStore) {
1213
1227
  "c1"
1214
1228
  ]);
1215
1229
  });
1230
+ t("concurrent claims across many runs of one key all answer, one winning", async () => {
1231
+ if (!supportsTagFilter) return;
1232
+ const ids2 = Array.from({
1233
+ length: 24
1234
+ }, (_, i) => `run-${String(i).padStart(2, "0")}`);
1235
+ for (const [i, id] of ids2.entries()) {
1236
+ await store.createRun(run({
1237
+ id,
1238
+ tags: [
1239
+ "singleton:k"
1240
+ ],
1241
+ createdAt: new Date(1e3 + i)
1242
+ }));
1243
+ }
1244
+ for (let round = 0; round < 3; round += 1) {
1245
+ const order = [
1246
+ ...ids2
1247
+ ].sort(() => Math.random() - 0.5);
1248
+ await Promise.all(order.map((id, i) => (i % 2 ? peerStore : store).tryAdmitSingleton?.(id, "singleton:k", "checkout", 1, Date.now() + 1e3)));
1249
+ }
1250
+ const active = await store.listRuns({
1251
+ workflow: "checkout",
1252
+ tag: "singleton:k"
1253
+ });
1254
+ expect3(active.filter((r) => r.tags?.includes(SINGLETON_ADMITTED_TAG)).map((r) => r.id)).toEqual([
1255
+ "run-00"
1256
+ ]);
1257
+ });
1216
1258
  t("creates a run and reads it back with its JSON input", async () => {
1217
1259
  await store.createRun(run());
1218
1260
  const loaded = await store.getRun("r1");
@@ -1386,6 +1428,108 @@ function runStateStoreContract(name, makeStore) {
1386
1428
  "c2"
1387
1429
  ]);
1388
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
+ });
1389
1533
  t("a scoped retention policy only prunes (and counts) the runs its scope matches", async () => {
1390
1534
  await store.createRun(aged("acme-old", 10, {
1391
1535
  namespace: "acme"
@@ -2367,8 +2511,10 @@ function runStateStoreContract(name, makeStore) {
2367
2511
  output: void 0
2368
2512
  }));
2369
2513
  const sent = /* @__PURE__ */ __name(async (work) => {
2514
+ await flushStatementLog?.();
2370
2515
  const before = statements();
2371
2516
  await work();
2517
+ await flushStatementLog?.();
2372
2518
  return statements() - before;
2373
2519
  }, "sent");
2374
2520
  expect3(await sent(() => store.saveCheckpoints?.(rows))).toBeLessThanOrEqual(limit);
@@ -2620,7 +2766,7 @@ function runStateStoreContract(name, makeStore) {
2620
2766
  output: void 0
2621
2767
  }));
2622
2768
  }
2623
- const before = statementLog().length;
2769
+ const before = await loggedLength(statementLog, flushStatementLog);
2624
2770
  const settle2 = /* @__PURE__ */ __name((seqs) => store.settleCheckpointsIfPending?.(seqs.map((seq) => ({
2625
2771
  runId: "r1",
2626
2772
  seq,
@@ -2643,7 +2789,7 @@ function runStateStoreContract(name, makeStore) {
2643
2789
  true,
2644
2790
  false
2645
2791
  ]);
2646
- const sent = await loggedSince(statementLog, before);
2792
+ const sent = await loggedSince(statementLog, before, flushStatementLog);
2647
2793
  expect3(sent.length).toBeGreaterThan(0);
2648
2794
  expect3(sent.some((statement) => statement.includes("xxxxxxxxxx"))).toBe(false);
2649
2795
  expect3(sent.join("").length).toBeLessThan(2e4);
@@ -2713,9 +2859,9 @@ function runStateStoreContract(name, makeStore) {
2713
2859
  await store.createRun(run({
2714
2860
  id: "warm"
2715
2861
  }));
2716
- const before = statementLog().length;
2862
+ const before = await loggedLength(statementLog, flushStatementLog);
2717
2863
  await store.createRun(run());
2718
- const sent = await loggedSince(statementLog, before);
2864
+ const sent = await loggedSince(statementLog, before, flushStatementLog);
2719
2865
  expect3(sent.length).toBeGreaterThan(0);
2720
2866
  expect3(sent.map(sqlOf).filter((q) => /^select/i.test(q))).toEqual([]);
2721
2867
  if (!implicitTransactions) {
@@ -2730,13 +2876,13 @@ function runStateStoreContract(name, makeStore) {
2730
2876
  status: "running",
2731
2877
  updatedAt: /* @__PURE__ */ new Date()
2732
2878
  });
2733
- const before = statementLog().length;
2879
+ const before = await loggedLength(statementLog, flushStatementLog);
2734
2880
  await store.updateRun("r1", {
2735
2881
  status: "suspended",
2736
2882
  wakeAt: 5e3,
2737
2883
  updatedAt: /* @__PURE__ */ new Date()
2738
2884
  });
2739
- const sent = await loggedSince(statementLog, before);
2885
+ const sent = await loggedSince(statementLog, before, flushStatementLog);
2740
2886
  expect3(sent.map(sqlOf).filter((q) => /^update/i.test(q))).toHaveLength(1);
2741
2887
  expect3(sent.map(sqlOf).filter((q) => /^select/i.test(q))).toEqual([]);
2742
2888
  if (!implicitTransactions) {
@@ -2764,12 +2910,12 @@ function runStateStoreContract(name, makeStore) {
2764
2910
  output: void 0
2765
2911
  }));
2766
2912
  }
2767
- const before = statementLog().length;
2913
+ const before = await loggedLength(statementLog, flushStatementLog);
2768
2914
  const read = await store.getCheckpointsForSettle("r1", [
2769
2915
  0,
2770
2916
  1
2771
2917
  ]);
2772
- const sent = await loggedSince(statementLog, before);
2918
+ const sent = await loggedSince(statementLog, before, flushStatementLog);
2773
2919
  expect3(read).toHaveLength(2);
2774
2920
  expect3(sent.length).toBeGreaterThan(0);
2775
2921
  expect3(sent.filter((statement) => /\binput\b|\*\s+from/i.test(statement))).toEqual([]);
@@ -2916,9 +3062,9 @@ function runStateStoreContract(name, makeStore) {
2916
3062
  }
2917
3063
  }));
2918
3064
  }
2919
- const before = statementLog().length;
3065
+ const before = await loggedLength(statementLog, flushStatementLog);
2920
3066
  const replay = await store.listCheckpointsForReplay("r1");
2921
- const sent = await loggedSince(statementLog, before);
3067
+ const sent = await loggedSince(statementLog, before, flushStatementLog);
2922
3068
  expect3(replay.map((c) => c.output)).toEqual([
2923
3069
  {
2924
3070
  answer: 0
@@ -3501,6 +3647,196 @@ function runStateStoreContract(name, makeStore) {
3501
3647
  "c"
3502
3648
  ]);
3503
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
+ });
3504
3840
  t("carries the absent-origin bucket INSIDE a set of origins", async () => {
3505
3841
  await store.createRun(run({
3506
3842
  id: "a",
@@ -4956,8 +5292,10 @@ function runStateStoreContract(name, makeStore) {
4956
5292
  const engine = new WorkflowEngine3({
4957
5293
  store
4958
5294
  });
5295
+ await flushStatementLog?.();
4959
5296
  const before = statements();
4960
5297
  const deleted = await engine.sweepBufferedSignals();
5298
+ await flushStatementLog?.();
4961
5299
  const sent = statements() - before;
4962
5300
  expect3(deleted).toBe(rows);
4963
5301
  expect3(await store.listBufferedSignals("child:", void 0, 10)).toEqual([]);