@rotorsoft/act-tck 1.1.0 → 1.2.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.cjs CHANGED
@@ -1550,10 +1550,10 @@ var runStoreTck = (options) => {
1550
1550
  await store.drop();
1551
1551
  await store.seed();
1552
1552
  });
1553
- const asSource = (rows) => (async function* () {
1554
- for (const r of rows) yield r;
1553
+ const asSource = (events) => (async function* () {
1554
+ for (const e of events) yield e;
1555
1555
  })();
1556
- const row = (originalId, stream, version, name, created, data = {}) => ({
1556
+ const event = (originalId, stream, version, name, created, data = {}) => ({
1557
1557
  id: originalId,
1558
1558
  name,
1559
1559
  data,
@@ -1562,11 +1562,19 @@ var runStoreTck = (options) => {
1562
1562
  created,
1563
1563
  meta: { correlation: "restore-tck", causation: {} }
1564
1564
  });
1565
+ const restore = async (source, opts = {}) => {
1566
+ const cache = new import_act.InMemoryCache();
1567
+ const app = (0, import_act.act)().build({ scoped: { store, cache } });
1568
+ try {
1569
+ return await app.restore(source, opts);
1570
+ } finally {
1571
+ await cache.dispose();
1572
+ }
1573
+ };
1565
1574
  (0, import_vitest3.it)("returns kept=0 on an empty source", async () => {
1566
- const result = await store.restore(asSource([]));
1575
+ const result = await restore(asSource([]));
1567
1576
  (0, import_vitest3.expect)(result.kept).toBe(0);
1568
1577
  (0, import_vitest3.expect)(result.duration_ms).toBeGreaterThanOrEqual(0);
1569
- (0, import_vitest3.expect)(result.dry_run).toBe(false);
1570
1578
  (0, import_vitest3.expect)(result.dropped).toEqual({
1571
1579
  closed_streams: 0,
1572
1580
  snapshots: 0,
@@ -1580,12 +1588,12 @@ var runStoreTck = (options) => {
1580
1588
  const t0 = /* @__PURE__ */ new Date("2020-01-01T00:00:00.000Z");
1581
1589
  const t1 = /* @__PURE__ */ new Date("2020-01-02T00:00:00.000Z");
1582
1590
  const t2 = /* @__PURE__ */ new Date("2020-01-03T00:00:00.000Z");
1583
- const rows = [
1584
- row(1, s, 0, "Incremented", t0, { amount: 1 }),
1585
- row(2, s, 1, "Incremented", t1, { amount: 2 }),
1586
- row(3, s, 2, "Decremented", t2, { amount: 1 })
1591
+ const events = [
1592
+ event(1, s, 0, "Incremented", t0, { amount: 1 }),
1593
+ event(2, s, 1, "Incremented", t1, { amount: 2 }),
1594
+ event(3, s, 2, "Decremented", t2, { amount: 1 })
1587
1595
  ];
1588
- const result = await store.restore(asSource(rows));
1596
+ const result = await restore(asSource(events));
1589
1597
  (0, import_vitest3.expect)(result.kept).toBe(3);
1590
1598
  const back = [];
1591
1599
  await store.query(
@@ -1631,13 +1639,13 @@ var runStoreTck = (options) => {
1631
1639
  const a = `restore-multi-a-${uid()}`;
1632
1640
  const b = `restore-multi-b-${uid()}`;
1633
1641
  const t = /* @__PURE__ */ new Date("2020-06-01T00:00:00.000Z");
1634
- const rows = [
1635
- row(1, a, 0, "Incremented", t, { amount: 10 }),
1636
- row(2, b, 0, "Incremented", t, { amount: 20 }),
1637
- row(3, a, 1, "Decremented", t, { amount: 5 }),
1638
- row(4, b, 1, "Incremented", t, { amount: 30 })
1642
+ const events = [
1643
+ event(1, a, 0, "Incremented", t, { amount: 10 }),
1644
+ event(2, b, 0, "Incremented", t, { amount: 20 }),
1645
+ event(3, a, 1, "Decremented", t, { amount: 5 }),
1646
+ event(4, b, 1, "Incremented", t, { amount: 30 })
1639
1647
  ];
1640
- const result = await store.restore(asSource(rows));
1648
+ const result = await restore(asSource(events));
1641
1649
  (0, import_vitest3.expect)(result.kept).toBe(4);
1642
1650
  const aBack = [];
1643
1651
  const bBack = [];
@@ -1656,10 +1664,10 @@ var runStoreTck = (options) => {
1656
1664
  (0, import_vitest3.expect)(aBack.map((e) => e.version)).toEqual([0, 1]);
1657
1665
  (0, import_vitest3.expect)(bBack.map((e) => e.version)).toEqual([0, 1]);
1658
1666
  });
1659
- (0, import_vitest3.it)("accepts ISO string `created` and parses to Date", async () => {
1667
+ (0, import_vitest3.it)("preserves Date `created` verbatim", async () => {
1660
1668
  const s = `restore-isoc-${uid()}`;
1661
1669
  const iso = "2021-07-15T12:34:56.789Z";
1662
- await store.restore(
1670
+ await restore(
1663
1671
  asSource([
1664
1672
  {
1665
1673
  id: 1,
@@ -1667,7 +1675,7 @@ var runStoreTck = (options) => {
1667
1675
  version: 0,
1668
1676
  name: "Incremented",
1669
1677
  data: { amount: 1 },
1670
- created: iso,
1678
+ created: new Date(iso),
1671
1679
  meta: { correlation: "restore-tck", causation: {} }
1672
1680
  }
1673
1681
  ])
@@ -1691,8 +1699,8 @@ var runStoreTck = (options) => {
1691
1699
  );
1692
1700
  const fresh = `restore-fresh-${uid()}`;
1693
1701
  const t = /* @__PURE__ */ new Date("2020-01-01T00:00:00.000Z");
1694
- await store.restore(
1695
- asSource([row(1, fresh, 0, "Incremented", t, { amount: 99 })])
1702
+ await restore(
1703
+ asSource([event(1, fresh, 0, "Incremented", t, { amount: 99 })])
1696
1704
  );
1697
1705
  const oldBack = await collect(store, {
1698
1706
  stream: old,
@@ -1717,14 +1725,14 @@ var runStoreTck = (options) => {
1717
1725
  };
1718
1726
  const before = await collectStreams();
1719
1727
  (0, import_vitest3.expect)(before.includes(sub)).toBe(true);
1720
- await store.restore(asSource([]));
1728
+ await restore(asSource([]));
1721
1729
  const after = await collectStreams();
1722
1730
  (0, import_vitest3.expect)(after.includes(sub)).toBe(false);
1723
1731
  });
1724
1732
  (0, import_vitest3.it)("preserves snapshot events through restore", async () => {
1725
1733
  const s = `restore-snap-${uid()}`;
1726
1734
  const t = /* @__PURE__ */ new Date("2020-04-01T00:00:00.000Z");
1727
- await store.restore(
1735
+ await restore(
1728
1736
  asSource([
1729
1737
  {
1730
1738
  id: 1,
@@ -1748,7 +1756,7 @@ var runStoreTck = (options) => {
1748
1756
  (0, import_vitest3.it)("rewrites causation refs through the old\u2192new id map", async () => {
1749
1757
  const s = `restore-caus-${uid()}`;
1750
1758
  const t = /* @__PURE__ */ new Date("2020-08-01T00:00:00.000Z");
1751
- const rows = [
1759
+ const events = [
1752
1760
  {
1753
1761
  id: 5,
1754
1762
  stream: s,
@@ -1787,7 +1795,7 @@ var runStoreTck = (options) => {
1787
1795
  }
1788
1796
  }
1789
1797
  ];
1790
- await store.restore(asSource(rows));
1798
+ await restore(asSource(events));
1791
1799
  const back = [];
1792
1800
  await store.query(
1793
1801
  (e) => {
@@ -1803,7 +1811,7 @@ var runStoreTck = (options) => {
1803
1811
  (0, import_vitest3.it)("leaves causation refs unmapped when the target isn't in the source", async () => {
1804
1812
  const s = `restore-orphan-${uid()}`;
1805
1813
  const t = /* @__PURE__ */ new Date("2020-09-01T00:00:00.000Z");
1806
- await store.restore(
1814
+ await restore(
1807
1815
  asSource([
1808
1816
  {
1809
1817
  id: 1,
@@ -1838,7 +1846,7 @@ var runStoreTck = (options) => {
1838
1846
  makeMeta({ stream: original })
1839
1847
  );
1840
1848
  const explosive = (async function* () {
1841
- yield row(
1849
+ yield event(
1842
1850
  1,
1843
1851
  `restore-explode-${uid()}`,
1844
1852
  0,
@@ -1848,7 +1856,7 @@ var runStoreTck = (options) => {
1848
1856
  );
1849
1857
  throw new Error("boom");
1850
1858
  })();
1851
- await (0, import_vitest3.expect)(store.restore(explosive)).rejects.toThrow("boom");
1859
+ await (0, import_vitest3.expect)(restore(explosive)).rejects.toThrow("boom");
1852
1860
  const back = [];
1853
1861
  await store.query(
1854
1862
  (e) => {
@@ -1859,6 +1867,50 @@ var runStoreTck = (options) => {
1859
1867
  (0, import_vitest3.expect)(back).toHaveLength(3);
1860
1868
  (0, import_vitest3.expect)(back.map((e) => e.id)).toEqual(committed.map((c) => c.id));
1861
1869
  });
1870
+ (0, import_vitest3.it)("drop_snapshots: skips SNAP_EVENT rows and counts them", async () => {
1871
+ const s = `restore-drop-snap-${uid()}`;
1872
+ const t = /* @__PURE__ */ new Date("2020-10-01T00:00:00.000Z");
1873
+ const result = await restore(
1874
+ asSource([
1875
+ event(1, s, 0, "Incremented", t, { amount: 1 }),
1876
+ {
1877
+ id: 2,
1878
+ stream: s,
1879
+ version: 1,
1880
+ name: import_act.SNAP_EVENT,
1881
+ data: { count: 1 },
1882
+ created: t,
1883
+ meta: { correlation: "snap", causation: {} }
1884
+ },
1885
+ event(3, s, 2, "Incremented", t, { amount: 2 })
1886
+ ]),
1887
+ { drop_snapshots: true }
1888
+ );
1889
+ (0, import_vitest3.expect)(result.kept).toBe(2);
1890
+ (0, import_vitest3.expect)(result.dropped.snapshots).toBe(1);
1891
+ const back = await collect(store, {
1892
+ stream: s,
1893
+ stream_exact: true,
1894
+ with_snaps: true
1895
+ });
1896
+ (0, import_vitest3.expect)(back).toHaveLength(2);
1897
+ (0, import_vitest3.expect)(
1898
+ back.every((e) => e.name !== import_act.SNAP_EVENT)
1899
+ ).toBe(true);
1900
+ });
1901
+ (0, import_vitest3.it)("on_progress fires once per event (caller throttles)", async () => {
1902
+ const calls = [];
1903
+ const s = `restore-progress-${uid()}`;
1904
+ const t = /* @__PURE__ */ new Date("2021-02-01T00:00:00.000Z");
1905
+ await restore(
1906
+ asSource([
1907
+ event(1, s, 0, "Incremented", t, { amount: 1 }),
1908
+ event(2, s, 1, "Incremented", t, { amount: 2 })
1909
+ ]),
1910
+ { on_progress: (p) => calls.push(p.processed) }
1911
+ );
1912
+ (0, import_vitest3.expect)(calls).toEqual([1, 2]);
1913
+ });
1862
1914
  });
1863
1915
  if (caps.notify) {
1864
1916
  (0, import_vitest3.describe)("notify (capability)", () => {