@rotorsoft/act-tck 1.1.0 → 1.3.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,20 @@ 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;
1555
- })();
1556
- const row = (originalId, stream, version, name, created, data = {}) => ({
1553
+ const asSource = (events) => ({
1554
+ async query(callback) {
1555
+ for (const e of events)
1556
+ await Promise.resolve(
1557
+ callback(
1558
+ e
1559
+ )
1560
+ );
1561
+ return events.length;
1562
+ },
1563
+ async dispose() {
1564
+ }
1565
+ });
1566
+ const event = (originalId, stream, version, name, created, data = {}) => ({
1557
1567
  id: originalId,
1558
1568
  name,
1559
1569
  data,
@@ -1562,11 +1572,20 @@ var runStoreTck = (options) => {
1562
1572
  created,
1563
1573
  meta: { correlation: "restore-tck", causation: {} }
1564
1574
  });
1575
+ const restore = async (source, opts = {}) => {
1576
+ const cache = new import_act.InMemoryCache();
1577
+ const app = (0, import_act.act)().build({ scoped: { store, cache } });
1578
+ try {
1579
+ return await app.restore(source, opts);
1580
+ } finally {
1581
+ await source.dispose();
1582
+ await cache.dispose();
1583
+ }
1584
+ };
1565
1585
  (0, import_vitest3.it)("returns kept=0 on an empty source", async () => {
1566
- const result = await store.restore(asSource([]));
1586
+ const result = await restore(asSource([]));
1567
1587
  (0, import_vitest3.expect)(result.kept).toBe(0);
1568
1588
  (0, import_vitest3.expect)(result.duration_ms).toBeGreaterThanOrEqual(0);
1569
- (0, import_vitest3.expect)(result.dry_run).toBe(false);
1570
1589
  (0, import_vitest3.expect)(result.dropped).toEqual({
1571
1590
  closed_streams: 0,
1572
1591
  snapshots: 0,
@@ -1580,12 +1599,12 @@ var runStoreTck = (options) => {
1580
1599
  const t0 = /* @__PURE__ */ new Date("2020-01-01T00:00:00.000Z");
1581
1600
  const t1 = /* @__PURE__ */ new Date("2020-01-02T00:00:00.000Z");
1582
1601
  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 })
1602
+ const events = [
1603
+ event(1, s, 0, "Incremented", t0, { amount: 1 }),
1604
+ event(2, s, 1, "Incremented", t1, { amount: 2 }),
1605
+ event(3, s, 2, "Decremented", t2, { amount: 1 })
1587
1606
  ];
1588
- const result = await store.restore(asSource(rows));
1607
+ const result = await restore(asSource(events));
1589
1608
  (0, import_vitest3.expect)(result.kept).toBe(3);
1590
1609
  const back = [];
1591
1610
  await store.query(
@@ -1631,13 +1650,13 @@ var runStoreTck = (options) => {
1631
1650
  const a = `restore-multi-a-${uid()}`;
1632
1651
  const b = `restore-multi-b-${uid()}`;
1633
1652
  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 })
1653
+ const events = [
1654
+ event(1, a, 0, "Incremented", t, { amount: 10 }),
1655
+ event(2, b, 0, "Incremented", t, { amount: 20 }),
1656
+ event(3, a, 1, "Decremented", t, { amount: 5 }),
1657
+ event(4, b, 1, "Incremented", t, { amount: 30 })
1639
1658
  ];
1640
- const result = await store.restore(asSource(rows));
1659
+ const result = await restore(asSource(events));
1641
1660
  (0, import_vitest3.expect)(result.kept).toBe(4);
1642
1661
  const aBack = [];
1643
1662
  const bBack = [];
@@ -1656,10 +1675,10 @@ var runStoreTck = (options) => {
1656
1675
  (0, import_vitest3.expect)(aBack.map((e) => e.version)).toEqual([0, 1]);
1657
1676
  (0, import_vitest3.expect)(bBack.map((e) => e.version)).toEqual([0, 1]);
1658
1677
  });
1659
- (0, import_vitest3.it)("accepts ISO string `created` and parses to Date", async () => {
1678
+ (0, import_vitest3.it)("preserves Date `created` verbatim", async () => {
1660
1679
  const s = `restore-isoc-${uid()}`;
1661
1680
  const iso = "2021-07-15T12:34:56.789Z";
1662
- await store.restore(
1681
+ await restore(
1663
1682
  asSource([
1664
1683
  {
1665
1684
  id: 1,
@@ -1667,7 +1686,7 @@ var runStoreTck = (options) => {
1667
1686
  version: 0,
1668
1687
  name: "Incremented",
1669
1688
  data: { amount: 1 },
1670
- created: iso,
1689
+ created: new Date(iso),
1671
1690
  meta: { correlation: "restore-tck", causation: {} }
1672
1691
  }
1673
1692
  ])
@@ -1691,8 +1710,8 @@ var runStoreTck = (options) => {
1691
1710
  );
1692
1711
  const fresh = `restore-fresh-${uid()}`;
1693
1712
  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 })])
1713
+ await restore(
1714
+ asSource([event(1, fresh, 0, "Incremented", t, { amount: 99 })])
1696
1715
  );
1697
1716
  const oldBack = await collect(store, {
1698
1717
  stream: old,
@@ -1717,14 +1736,14 @@ var runStoreTck = (options) => {
1717
1736
  };
1718
1737
  const before = await collectStreams();
1719
1738
  (0, import_vitest3.expect)(before.includes(sub)).toBe(true);
1720
- await store.restore(asSource([]));
1739
+ await restore(asSource([]));
1721
1740
  const after = await collectStreams();
1722
1741
  (0, import_vitest3.expect)(after.includes(sub)).toBe(false);
1723
1742
  });
1724
1743
  (0, import_vitest3.it)("preserves snapshot events through restore", async () => {
1725
1744
  const s = `restore-snap-${uid()}`;
1726
1745
  const t = /* @__PURE__ */ new Date("2020-04-01T00:00:00.000Z");
1727
- await store.restore(
1746
+ await restore(
1728
1747
  asSource([
1729
1748
  {
1730
1749
  id: 1,
@@ -1748,7 +1767,7 @@ var runStoreTck = (options) => {
1748
1767
  (0, import_vitest3.it)("rewrites causation refs through the old\u2192new id map", async () => {
1749
1768
  const s = `restore-caus-${uid()}`;
1750
1769
  const t = /* @__PURE__ */ new Date("2020-08-01T00:00:00.000Z");
1751
- const rows = [
1770
+ const events = [
1752
1771
  {
1753
1772
  id: 5,
1754
1773
  stream: s,
@@ -1787,7 +1806,7 @@ var runStoreTck = (options) => {
1787
1806
  }
1788
1807
  }
1789
1808
  ];
1790
- await store.restore(asSource(rows));
1809
+ await restore(asSource(events));
1791
1810
  const back = [];
1792
1811
  await store.query(
1793
1812
  (e) => {
@@ -1803,7 +1822,7 @@ var runStoreTck = (options) => {
1803
1822
  (0, import_vitest3.it)("leaves causation refs unmapped when the target isn't in the source", async () => {
1804
1823
  const s = `restore-orphan-${uid()}`;
1805
1824
  const t = /* @__PURE__ */ new Date("2020-09-01T00:00:00.000Z");
1806
- await store.restore(
1825
+ await restore(
1807
1826
  asSource([
1808
1827
  {
1809
1828
  id: 1,
@@ -1837,18 +1856,26 @@ var runStoreTck = (options) => {
1837
1856
  [inc(1), inc(2), inc(3)],
1838
1857
  makeMeta({ stream: original })
1839
1858
  );
1840
- const explosive = (async function* () {
1841
- yield row(
1842
- 1,
1843
- `restore-explode-${uid()}`,
1844
- 0,
1845
- "Incremented",
1846
- /* @__PURE__ */ new Date(),
1847
- { amount: 1 }
1848
- );
1849
- throw new Error("boom");
1850
- })();
1851
- await (0, import_vitest3.expect)(store.restore(explosive)).rejects.toThrow("boom");
1859
+ const explosive = {
1860
+ async query(callback) {
1861
+ await Promise.resolve(
1862
+ callback(
1863
+ event(
1864
+ 1,
1865
+ `restore-explode-${uid()}`,
1866
+ 0,
1867
+ "Incremented",
1868
+ /* @__PURE__ */ new Date(),
1869
+ { amount: 1 }
1870
+ )
1871
+ )
1872
+ );
1873
+ throw new Error("boom");
1874
+ },
1875
+ async dispose() {
1876
+ }
1877
+ };
1878
+ await (0, import_vitest3.expect)(restore(explosive)).rejects.toThrow("boom");
1852
1879
  const back = [];
1853
1880
  await store.query(
1854
1881
  (e) => {
@@ -1859,6 +1886,50 @@ var runStoreTck = (options) => {
1859
1886
  (0, import_vitest3.expect)(back).toHaveLength(3);
1860
1887
  (0, import_vitest3.expect)(back.map((e) => e.id)).toEqual(committed.map((c) => c.id));
1861
1888
  });
1889
+ (0, import_vitest3.it)("drop_snapshots: skips SNAP_EVENT rows and counts them", async () => {
1890
+ const s = `restore-drop-snap-${uid()}`;
1891
+ const t = /* @__PURE__ */ new Date("2020-10-01T00:00:00.000Z");
1892
+ const result = await restore(
1893
+ asSource([
1894
+ event(1, s, 0, "Incremented", t, { amount: 1 }),
1895
+ {
1896
+ id: 2,
1897
+ stream: s,
1898
+ version: 1,
1899
+ name: import_act.SNAP_EVENT,
1900
+ data: { count: 1 },
1901
+ created: t,
1902
+ meta: { correlation: "snap", causation: {} }
1903
+ },
1904
+ event(3, s, 2, "Incremented", t, { amount: 2 })
1905
+ ]),
1906
+ { drop_snapshots: true }
1907
+ );
1908
+ (0, import_vitest3.expect)(result.kept).toBe(2);
1909
+ (0, import_vitest3.expect)(result.dropped.snapshots).toBe(1);
1910
+ const back = await collect(store, {
1911
+ stream: s,
1912
+ stream_exact: true,
1913
+ with_snaps: true
1914
+ });
1915
+ (0, import_vitest3.expect)(back).toHaveLength(2);
1916
+ (0, import_vitest3.expect)(
1917
+ back.every((e) => e.name !== import_act.SNAP_EVENT)
1918
+ ).toBe(true);
1919
+ });
1920
+ (0, import_vitest3.it)("on_progress fires once per event (caller throttles)", async () => {
1921
+ const calls = [];
1922
+ const s = `restore-progress-${uid()}`;
1923
+ const t = /* @__PURE__ */ new Date("2021-02-01T00:00:00.000Z");
1924
+ await restore(
1925
+ asSource([
1926
+ event(1, s, 0, "Incremented", t, { amount: 1 }),
1927
+ event(2, s, 1, "Incremented", t, { amount: 2 })
1928
+ ]),
1929
+ { on_progress: (p) => calls.push(p.processed) }
1930
+ );
1931
+ (0, import_vitest3.expect)(calls).toEqual([1, 2]);
1932
+ });
1862
1933
  });
1863
1934
  if (caps.notify) {
1864
1935
  (0, import_vitest3.describe)("notify (capability)", () => {