@dudousxd/nestjs-durable-testing 0.23.2 → 0.23.4

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
@@ -268,6 +268,12 @@ interface StateStoreContext {
268
268
  * sends none of the step's input.
269
269
  */
270
270
  statementLog?: () => string[];
271
+ /**
272
+ * The store's client opens a transaction around every write of its own (Prisma does, even for one
273
+ * `createMany` or `updateMany`). The contract then checks that a run's row is written without a
274
+ * read of it first, but not that no transaction is opened.
275
+ */
276
+ implicitTransactions?: boolean;
271
277
  }
272
278
  /** Builds a fresh, empty store for one test. Called once per `it` (in `beforeEach`). */
273
279
  type StateStoreFactory = () => Promise<StateStoreContext>;
package/dist/index.js CHANGED
@@ -834,6 +834,11 @@ var checkpoint = /* @__PURE__ */ __name((over = {}) => ({
834
834
  finishedAt: at,
835
835
  ...over
836
836
  }), "checkpoint");
837
+ var ANSI_COLOR = new RegExp(`${String.fromCharCode(27)}\\[[0-9;]*m`, "g");
838
+ function sqlOf(logged) {
839
+ return logged.replace(ANSI_COLOR, "").replace(/^\s*\[query\]\s*/i, "").trim();
840
+ }
841
+ __name(sqlOf, "sqlOf");
837
842
  async function loggedSince(log, before) {
838
843
  let seen = -1;
839
844
  for (let i = 0; i < 50 && log().length !== seen; i += 1) {
@@ -852,6 +857,7 @@ function runStateStoreContract(name, makeStore) {
852
857
  let supportsTagFilter = true;
853
858
  let statements;
854
859
  let statementLog;
860
+ let implicitTransactions = false;
855
861
  let skipReason;
856
862
  let loggedSkip = false;
857
863
  beforeEach(async () => {
@@ -864,6 +870,7 @@ function runStateStoreContract(name, makeStore) {
864
870
  supportsTagFilter = ctx.supportsTagFilter ?? true;
865
871
  statements = ctx.statements;
866
872
  statementLog = ctx.statementLog;
873
+ implicitTransactions = ctx.implicitTransactions ?? false;
867
874
  skipReason = void 0;
868
875
  } catch (err) {
869
876
  if (isUnavailable(err) && !process.env.DURABLE_TEST_REQUIRE_BACKENDS) {
@@ -1746,6 +1753,71 @@ function runStateStoreContract(name, makeStore) {
1746
1753
  ])).toBe(0);
1747
1754
  expect3(await store.countCheckpoints("r1", 0, 4, [])).toBe(0);
1748
1755
  });
1756
+ t("hasCheckpoints answers whether a seq range holds a status", async () => {
1757
+ expect3(store.hasCheckpoints).toBeDefined();
1758
+ if (!store.hasCheckpoints) return;
1759
+ await store.createRun(run());
1760
+ for (const [seq, status] of [
1761
+ [
1762
+ 0,
1763
+ "completed"
1764
+ ],
1765
+ [
1766
+ 1,
1767
+ "pending"
1768
+ ],
1769
+ [
1770
+ 2,
1771
+ "completed"
1772
+ ],
1773
+ [
1774
+ 3,
1775
+ "failed"
1776
+ ],
1777
+ [
1778
+ 4,
1779
+ "completed"
1780
+ ]
1781
+ ]) {
1782
+ await store.saveCheckpoint(checkpoint({
1783
+ seq,
1784
+ status,
1785
+ stepId: `r1:${seq}`
1786
+ }));
1787
+ }
1788
+ await store.createRun(run({
1789
+ id: "other"
1790
+ }));
1791
+ await store.saveCheckpoint(checkpoint({
1792
+ runId: "other",
1793
+ seq: 4,
1794
+ status: "pending",
1795
+ stepId: "other:4"
1796
+ }));
1797
+ expect3(await store.hasCheckpoints("r1", 0, 4, [
1798
+ "pending"
1799
+ ])).toBe(true);
1800
+ expect3(await store.hasCheckpoints("r1", 1, 1, [
1801
+ "pending"
1802
+ ])).toBe(true);
1803
+ expect3(await store.hasCheckpoints("r1", 2, 4, [
1804
+ "pending"
1805
+ ])).toBe(false);
1806
+ expect3(await store.hasCheckpoints("r1", 2, 4, [
1807
+ "pending",
1808
+ "failed"
1809
+ ])).toBe(true);
1810
+ expect3(await store.hasCheckpoints("r1", 4, 4, [
1811
+ "pending"
1812
+ ])).toBe(false);
1813
+ expect3(await store.hasCheckpoints("other", 0, 9, [
1814
+ "pending"
1815
+ ])).toBe(true);
1816
+ expect3(await store.hasCheckpoints("r1", 5, 9, [
1817
+ "completed"
1818
+ ])).toBe(false);
1819
+ expect3(await store.hasCheckpoints("r1", 0, 4, [])).toBe(false);
1820
+ });
1749
1821
  registerCheckpointAggregationCases(t, () => store, run);
1750
1822
  t("listOrphanedRuns returns incomplete runs with a free or expired lease, oldest first", async () => {
1751
1823
  expect3(store.listOrphanedRuns).toBeDefined();
@@ -2636,6 +2708,44 @@ function runStateStoreContract(name, makeStore) {
2636
2708
  prompt: "p2"
2637
2709
  });
2638
2710
  });
2711
+ t("createRun of a run without search attributes reads nothing and opens no transaction", async () => {
2712
+ if (!statementLog) return;
2713
+ await store.createRun(run({
2714
+ id: "warm"
2715
+ }));
2716
+ const before = statementLog().length;
2717
+ await store.createRun(run());
2718
+ const sent = await loggedSince(statementLog, before);
2719
+ expect3(sent.length).toBeGreaterThan(0);
2720
+ expect3(sent.map(sqlOf).filter((q) => /^select/i.test(q))).toEqual([]);
2721
+ if (!implicitTransactions) {
2722
+ expect3(sent.map(sqlOf).filter((q) => /^(begin|start transaction)/i.test(q))).toEqual([]);
2723
+ }
2724
+ expect3((await store.getRun("r1"))?.status).toBe(run().status);
2725
+ });
2726
+ t("updateRun with a non-terminal patch is one UPDATE, with no read of the row", async () => {
2727
+ if (!statementLog) return;
2728
+ await store.createRun(run());
2729
+ await store.updateRun("r1", {
2730
+ status: "running",
2731
+ updatedAt: /* @__PURE__ */ new Date()
2732
+ });
2733
+ const before = statementLog().length;
2734
+ await store.updateRun("r1", {
2735
+ status: "suspended",
2736
+ wakeAt: 5e3,
2737
+ updatedAt: /* @__PURE__ */ new Date()
2738
+ });
2739
+ const sent = await loggedSince(statementLog, before);
2740
+ expect3(sent.map(sqlOf).filter((q) => /^update/i.test(q))).toHaveLength(1);
2741
+ expect3(sent.map(sqlOf).filter((q) => /^select/i.test(q))).toEqual([]);
2742
+ if (!implicitTransactions) {
2743
+ expect3(sent.map(sqlOf).filter((q) => /^(begin|start transaction)/i.test(q))).toEqual([]);
2744
+ }
2745
+ const read = await store.getRun("r1");
2746
+ expect3(read?.status).toBe("suspended");
2747
+ expect3(read?.wakeAt).toBe(5e3);
2748
+ });
2639
2749
  t("getCheckpointsForSettle reads no input column", async () => {
2640
2750
  if (!statementLog || !store.getCheckpointsForSettle) return;
2641
2751
  await store.createRun(run());