@rotorsoft/act-tck 0.4.0 → 1.1.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
@@ -235,7 +235,7 @@ var import_vitest3 = require("vitest");
235
235
  var runStoreTck = (options) => {
236
236
  (0, import_vitest3.describe)(`TCK / Store / ${options.name}`, () => {
237
237
  let store;
238
- const caps = options.capabilities ?? {};
238
+ const caps = { ...options.capabilities };
239
239
  (0, import_vitest3.beforeAll)(async () => {
240
240
  store = await options.factory();
241
241
  await store.drop();
@@ -1545,6 +1545,321 @@ var runStoreTck = (options) => {
1545
1545
  }
1546
1546
  });
1547
1547
  });
1548
+ import_vitest3.describe.skipIf(!caps.restore)("restore (capability)", () => {
1549
+ beforeEach(async () => {
1550
+ await store.drop();
1551
+ await store.seed();
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 = {}) => ({
1557
+ id: originalId,
1558
+ name,
1559
+ data,
1560
+ stream,
1561
+ version,
1562
+ created,
1563
+ meta: { correlation: "restore-tck", causation: {} }
1564
+ });
1565
+ (0, import_vitest3.it)("returns kept=0 on an empty source", async () => {
1566
+ const result = await store.restore(asSource([]));
1567
+ (0, import_vitest3.expect)(result.kept).toBe(0);
1568
+ (0, import_vitest3.expect)(result.duration_ms).toBeGreaterThanOrEqual(0);
1569
+ (0, import_vitest3.expect)(result.dry_run).toBe(false);
1570
+ (0, import_vitest3.expect)(result.dropped).toEqual({
1571
+ closed_streams: 0,
1572
+ snapshots: 0,
1573
+ empty_streams: 0
1574
+ });
1575
+ const events = await collect(store, { limit: 10 });
1576
+ (0, import_vitest3.expect)(events).toHaveLength(0);
1577
+ });
1578
+ (0, import_vitest3.it)("rebuilds a single stream and preserves `created` verbatim", async () => {
1579
+ const s = `restore-single-${uid()}`;
1580
+ const t0 = /* @__PURE__ */ new Date("2020-01-01T00:00:00.000Z");
1581
+ const t1 = /* @__PURE__ */ new Date("2020-01-02T00:00:00.000Z");
1582
+ 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 })
1587
+ ];
1588
+ const result = await store.restore(asSource(rows));
1589
+ (0, import_vitest3.expect)(result.kept).toBe(3);
1590
+ const back = [];
1591
+ await store.query(
1592
+ (e) => {
1593
+ back.push(e);
1594
+ },
1595
+ { stream: s, stream_exact: true }
1596
+ );
1597
+ (0, import_vitest3.expect)(back).toHaveLength(3);
1598
+ (0, import_vitest3.expect)(
1599
+ back.map((e) => ({
1600
+ stream: e.stream,
1601
+ version: e.version,
1602
+ name: e.name,
1603
+ created: e.created.toISOString(),
1604
+ data: e.data
1605
+ }))
1606
+ ).toEqual([
1607
+ {
1608
+ stream: s,
1609
+ version: 0,
1610
+ name: "Incremented",
1611
+ created: t0.toISOString(),
1612
+ data: { amount: 1 }
1613
+ },
1614
+ {
1615
+ stream: s,
1616
+ version: 1,
1617
+ name: "Incremented",
1618
+ created: t1.toISOString(),
1619
+ data: { amount: 2 }
1620
+ },
1621
+ {
1622
+ stream: s,
1623
+ version: 2,
1624
+ name: "Decremented",
1625
+ created: t2.toISOString(),
1626
+ data: { amount: 1 }
1627
+ }
1628
+ ]);
1629
+ });
1630
+ (0, import_vitest3.it)("rebuilds multiple streams interleaved", async () => {
1631
+ const a = `restore-multi-a-${uid()}`;
1632
+ const b = `restore-multi-b-${uid()}`;
1633
+ 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 })
1639
+ ];
1640
+ const result = await store.restore(asSource(rows));
1641
+ (0, import_vitest3.expect)(result.kept).toBe(4);
1642
+ const aBack = [];
1643
+ const bBack = [];
1644
+ await store.query(
1645
+ (e) => {
1646
+ aBack.push(e);
1647
+ },
1648
+ { stream: a, stream_exact: true }
1649
+ );
1650
+ await store.query(
1651
+ (e) => {
1652
+ bBack.push(e);
1653
+ },
1654
+ { stream: b, stream_exact: true }
1655
+ );
1656
+ (0, import_vitest3.expect)(aBack.map((e) => e.version)).toEqual([0, 1]);
1657
+ (0, import_vitest3.expect)(bBack.map((e) => e.version)).toEqual([0, 1]);
1658
+ });
1659
+ (0, import_vitest3.it)("accepts ISO string `created` and parses to Date", async () => {
1660
+ const s = `restore-isoc-${uid()}`;
1661
+ const iso = "2021-07-15T12:34:56.789Z";
1662
+ await store.restore(
1663
+ asSource([
1664
+ {
1665
+ id: 1,
1666
+ stream: s,
1667
+ version: 0,
1668
+ name: "Incremented",
1669
+ data: { amount: 1 },
1670
+ created: iso,
1671
+ meta: { correlation: "restore-tck", causation: {} }
1672
+ }
1673
+ ])
1674
+ );
1675
+ const back = [];
1676
+ await store.query(
1677
+ (e) => {
1678
+ back.push(e);
1679
+ },
1680
+ { stream: s, stream_exact: true }
1681
+ );
1682
+ (0, import_vitest3.expect)(back).toHaveLength(1);
1683
+ (0, import_vitest3.expect)(back[0].created.toISOString()).toBe(iso);
1684
+ });
1685
+ (0, import_vitest3.it)("wipes pre-existing events before inserting", async () => {
1686
+ const old = `restore-old-${uid()}`;
1687
+ await store.commit(
1688
+ old,
1689
+ [inc(1), inc(2)],
1690
+ makeMeta({ stream: old })
1691
+ );
1692
+ const fresh = `restore-fresh-${uid()}`;
1693
+ 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 })])
1696
+ );
1697
+ const oldBack = await collect(store, {
1698
+ stream: old,
1699
+ stream_exact: true
1700
+ });
1701
+ (0, import_vitest3.expect)(oldBack).toHaveLength(0);
1702
+ const freshBack = await collect(store, {
1703
+ stream: fresh,
1704
+ stream_exact: true
1705
+ });
1706
+ (0, import_vitest3.expect)(freshBack).toHaveLength(1);
1707
+ });
1708
+ (0, import_vitest3.it)("clears subscription/stream-position metadata", async () => {
1709
+ const sub = `restore-sub-${uid()}`;
1710
+ await store.subscribe([{ stream: sub, source: "anything" }]);
1711
+ const collectStreams = async () => {
1712
+ const out = [];
1713
+ await store.query_streams((p) => {
1714
+ out.push(p.stream);
1715
+ });
1716
+ return out;
1717
+ };
1718
+ const before = await collectStreams();
1719
+ (0, import_vitest3.expect)(before.includes(sub)).toBe(true);
1720
+ await store.restore(asSource([]));
1721
+ const after = await collectStreams();
1722
+ (0, import_vitest3.expect)(after.includes(sub)).toBe(false);
1723
+ });
1724
+ (0, import_vitest3.it)("preserves snapshot events through restore", async () => {
1725
+ const s = `restore-snap-${uid()}`;
1726
+ const t = /* @__PURE__ */ new Date("2020-04-01T00:00:00.000Z");
1727
+ await store.restore(
1728
+ asSource([
1729
+ {
1730
+ id: 1,
1731
+ stream: s,
1732
+ version: 0,
1733
+ name: import_act.SNAP_EVENT,
1734
+ data: { count: 42 },
1735
+ created: t,
1736
+ meta: { correlation: "snap", causation: {} }
1737
+ }
1738
+ ])
1739
+ );
1740
+ const back = await collect(store, {
1741
+ stream: s,
1742
+ stream_exact: true,
1743
+ with_snaps: true
1744
+ });
1745
+ (0, import_vitest3.expect)(back).toHaveLength(1);
1746
+ (0, import_vitest3.expect)(back[0].name).toBe(import_act.SNAP_EVENT);
1747
+ });
1748
+ (0, import_vitest3.it)("rewrites causation refs through the old\u2192new id map", async () => {
1749
+ const s = `restore-caus-${uid()}`;
1750
+ const t = /* @__PURE__ */ new Date("2020-08-01T00:00:00.000Z");
1751
+ const rows = [
1752
+ {
1753
+ id: 5,
1754
+ stream: s,
1755
+ version: 0,
1756
+ name: "Incremented",
1757
+ data: { amount: 1 },
1758
+ created: t,
1759
+ meta: { correlation: "c", causation: {} }
1760
+ },
1761
+ {
1762
+ id: 7,
1763
+ stream: s,
1764
+ version: 1,
1765
+ name: "Incremented",
1766
+ data: { amount: 2 },
1767
+ created: t,
1768
+ meta: {
1769
+ correlation: "c",
1770
+ causation: {
1771
+ event: { id: 5, name: "Incremented", stream: s }
1772
+ }
1773
+ }
1774
+ },
1775
+ {
1776
+ id: 9,
1777
+ stream: s,
1778
+ version: 2,
1779
+ name: "Decremented",
1780
+ data: { amount: 1 },
1781
+ created: t,
1782
+ meta: {
1783
+ correlation: "c",
1784
+ causation: {
1785
+ event: { id: 7, name: "Incremented", stream: s }
1786
+ }
1787
+ }
1788
+ }
1789
+ ];
1790
+ await store.restore(asSource(rows));
1791
+ const back = [];
1792
+ await store.query(
1793
+ (e) => {
1794
+ back.push(e);
1795
+ },
1796
+ { stream: s, stream_exact: true }
1797
+ );
1798
+ (0, import_vitest3.expect)(back).toHaveLength(3);
1799
+ (0, import_vitest3.expect)(back[0].meta.causation.event).toBeUndefined();
1800
+ (0, import_vitest3.expect)(back[1].meta.causation.event?.id).toBe(back[0].id);
1801
+ (0, import_vitest3.expect)(back[2].meta.causation.event?.id).toBe(back[1].id);
1802
+ });
1803
+ (0, import_vitest3.it)("leaves causation refs unmapped when the target isn't in the source", async () => {
1804
+ const s = `restore-orphan-${uid()}`;
1805
+ const t = /* @__PURE__ */ new Date("2020-09-01T00:00:00.000Z");
1806
+ await store.restore(
1807
+ asSource([
1808
+ {
1809
+ id: 1,
1810
+ stream: s,
1811
+ version: 0,
1812
+ name: "Incremented",
1813
+ data: { amount: 1 },
1814
+ created: t,
1815
+ meta: {
1816
+ correlation: "c",
1817
+ causation: {
1818
+ event: { id: 999, name: "Phantom", stream: "ghost" }
1819
+ }
1820
+ }
1821
+ }
1822
+ ])
1823
+ );
1824
+ const back = [];
1825
+ await store.query(
1826
+ (e) => {
1827
+ back.push(e);
1828
+ },
1829
+ { stream: s, stream_exact: true }
1830
+ );
1831
+ (0, import_vitest3.expect)(back[0].meta.causation.event?.id).toBe(999);
1832
+ });
1833
+ (0, import_vitest3.it)("rolls back atomically when the source throws mid-iteration", async () => {
1834
+ const original = `restore-pre-${uid()}`;
1835
+ const committed = await store.commit(
1836
+ original,
1837
+ [inc(1), inc(2), inc(3)],
1838
+ makeMeta({ stream: original })
1839
+ );
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");
1852
+ const back = [];
1853
+ await store.query(
1854
+ (e) => {
1855
+ back.push(e);
1856
+ },
1857
+ { stream: original, stream_exact: true }
1858
+ );
1859
+ (0, import_vitest3.expect)(back).toHaveLength(3);
1860
+ (0, import_vitest3.expect)(back.map((e) => e.id)).toEqual(committed.map((c) => c.id));
1861
+ });
1862
+ });
1548
1863
  if (caps.notify) {
1549
1864
  (0, import_vitest3.describe)("notify (capability)", () => {
1550
1865
  (0, import_vitest3.it)("delivers a notification when a different instance commits", async () => {