cojson 0.8.21 → 0.8.23

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.
@@ -807,7 +807,7 @@ test.skip("When replaying creation and transactions of a coValue as new content,
807
807
  sessions: {},
808
808
  } satisfies SyncMessage);
809
809
 
810
- expect(node2.coValues[map.core.id]?.state).toEqual("loading");
810
+ expect(node2.coValuesStore.get(map.core.id).state.type).toEqual("loading");
811
811
 
812
812
  await inTx2.push(mapNewContentMsg);
813
813
 
@@ -875,7 +875,7 @@ test("Can sync a coValue through a server to another client", async () => {
875
875
  {
876
876
  peer1role: "server",
877
877
  peer2role: "client",
878
- trace: true,
878
+ // trace: true,
879
879
  },
880
880
  );
881
881
 
@@ -894,7 +894,7 @@ test("Can sync a coValue through a server to another client", async () => {
894
894
  {
895
895
  peer1role: "server",
896
896
  peer2role: "client",
897
- trace: true,
897
+ // trace: true,
898
898
  },
899
899
  );
900
900
 
@@ -926,7 +926,7 @@ test("Can sync a coValue with private transactions through a server to another c
926
926
  const server = new LocalNode(serverUser, serverSession, Crypto);
927
927
 
928
928
  const [serverAsPeer, client1AsPeer] = connectedPeers("server", "client1", {
929
- trace: true,
929
+ // trace: true,
930
930
  peer1role: "server",
931
931
  peer2role: "client",
932
932
  });
@@ -944,7 +944,7 @@ test("Can sync a coValue with private transactions through a server to another c
944
944
  "server",
945
945
  "client2",
946
946
  {
947
- trace: true,
947
+ // trace: true,
948
948
  peer1role: "server",
949
949
  peer2role: "client",
950
950
  },
@@ -1095,14 +1095,14 @@ test("If we start loading a coValue before connecting to a peer that has it, it
1095
1095
  const [node1asPeer, node2asPeer] = connectedPeers("peer1", "peer2", {
1096
1096
  peer1role: "server",
1097
1097
  peer2role: "client",
1098
- trace: true,
1098
+ // trace: true,
1099
1099
  });
1100
1100
 
1101
1101
  node1.syncManager.addPeer(node2asPeer);
1102
1102
 
1103
1103
  const mapOnNode2Promise = node2.loadCoValueCore(map.core.id);
1104
1104
 
1105
- expect(node2.coValues[map.core.id]?.state.type).toEqual("unknown");
1105
+ expect(node2.coValuesStore.get(map.core.id).state.type).toEqual("unknown");
1106
1106
 
1107
1107
  node2.syncManager.addPeer(node1asPeer);
1108
1108
 
@@ -1116,6 +1116,59 @@ test("If we start loading a coValue before connecting to a peer that has it, it
1116
1116
  );
1117
1117
  });
1118
1118
 
1119
+ test("should keep the peer state when the peer closes", async () => {
1120
+ const {
1121
+ client,
1122
+ jazzCloud,
1123
+ jazzCloudConnectionAsPeer,
1124
+ connectionWithClientAsPeer,
1125
+ } = createTwoConnectedNodes();
1126
+
1127
+ const group = jazzCloud.createGroup();
1128
+ const map = group.createMap();
1129
+ map.set("hello", "world", "trusting");
1130
+
1131
+ await client.loadCoValueCore(map.core.id);
1132
+
1133
+ const syncManager = client.syncManager;
1134
+ const peerState = syncManager.peers[jazzCloudConnectionAsPeer.id];
1135
+
1136
+ // @ts-expect-error Simulating a peer closing, leveraging the direct connection between the client/server peers
1137
+ await connectionWithClientAsPeer.outgoing.push("Disconnected");
1138
+
1139
+ await waitFor(() => peerState?.closed);
1140
+
1141
+ expect(syncManager.peers[jazzCloudConnectionAsPeer.id]).not.toBeUndefined();
1142
+ });
1143
+
1144
+ test("should delete the peer state when the peer closes if deletePeerStateOnClose is true", async () => {
1145
+ const {
1146
+ client,
1147
+ jazzCloud,
1148
+ jazzCloudConnectionAsPeer,
1149
+ connectionWithClientAsPeer,
1150
+ } = createTwoConnectedNodes();
1151
+
1152
+ jazzCloudConnectionAsPeer.deletePeerStateOnClose = true;
1153
+
1154
+ const group = jazzCloud.createGroup();
1155
+ const map = group.createMap();
1156
+ map.set("hello", "world", "trusting");
1157
+
1158
+ await client.loadCoValueCore(map.core.id);
1159
+
1160
+ const syncManager = client.syncManager;
1161
+
1162
+ const peerState = syncManager.peers[jazzCloudConnectionAsPeer.id];
1163
+
1164
+ // @ts-expect-error Simulating a peer closing, leveraging the direct connection between the client/server peers
1165
+ await connectionWithClientAsPeer.outgoing.push("Disconnected");
1166
+
1167
+ await waitFor(() => peerState?.closed);
1168
+
1169
+ expect(syncManager.peers[jazzCloudConnectionAsPeer.id]).toBeUndefined();
1170
+ });
1171
+
1119
1172
  describe("sync - extra tests", () => {
1120
1173
  test("Node handles disconnection and reconnection of a peer gracefully", async () => {
1121
1174
  // Create two nodes
@@ -1519,7 +1572,7 @@ describe("sync - extra tests", () => {
1519
1572
  {
1520
1573
  peer1role: "server",
1521
1574
  peer2role: "client",
1522
- trace: true,
1575
+ // trace: true,
1523
1576
  },
1524
1577
  );
1525
1578
 
@@ -1529,7 +1582,7 @@ describe("sync - extra tests", () => {
1529
1582
  {
1530
1583
  peer1role: "server",
1531
1584
  peer2role: "client",
1532
- trace: true,
1585
+ // trace: true,
1533
1586
  },
1534
1587
  );
1535
1588
 
@@ -1821,6 +1874,63 @@ describe("SyncManager.addPeer", () => {
1821
1874
  });
1822
1875
  });
1823
1876
 
1877
+ describe("loadCoValueCore with retry", () => {
1878
+ test("should load the value if available on the server", async () => {
1879
+ const { client, jazzCloud } = createTwoConnectedNodes();
1880
+
1881
+ const anotherClient = createTestNode();
1882
+ const [
1883
+ connectionWithAnotherClientAsPeer,
1884
+ jazzCloudConnectionAsPeerForAnotherClient,
1885
+ ] = connectedPeers("connectionWithAnotherClient", "jazzCloudConnection", {
1886
+ peer1role: "client",
1887
+ peer2role: "server",
1888
+ });
1889
+
1890
+ jazzCloud.syncManager.addPeer(connectionWithAnotherClientAsPeer);
1891
+
1892
+ const group = anotherClient.createGroup();
1893
+ const map = group.createMap();
1894
+ map.set("key1", "value1", "trusting");
1895
+
1896
+ const promise = client.loadCoValueCore(map.id);
1897
+
1898
+ anotherClient.syncManager.addPeer(
1899
+ jazzCloudConnectionAsPeerForAnotherClient,
1900
+ );
1901
+ await expect(promise).resolves.not.toBe("unavailable");
1902
+ });
1903
+
1904
+ test("should handle correctly two subsequent loads", async () => {
1905
+ const { client, jazzCloud } = createTwoConnectedNodes();
1906
+
1907
+ const anotherClient = createTestNode();
1908
+ const [
1909
+ connectionWithAnotherClientAsPeer,
1910
+ jazzCloudConnectionAsPeerForAnotherClient,
1911
+ ] = connectedPeers("connectionWithAnotherClient", "jazzCloudConnection", {
1912
+ peer1role: "client",
1913
+ peer2role: "server",
1914
+ });
1915
+
1916
+ jazzCloud.syncManager.addPeer(connectionWithAnotherClientAsPeer);
1917
+
1918
+ const group = anotherClient.createGroup();
1919
+ const map = group.createMap();
1920
+ map.set("key1", "value1", "trusting");
1921
+
1922
+ const promise1 = client.loadCoValueCore(map.id);
1923
+ const promise2 = client.loadCoValueCore(map.id);
1924
+
1925
+ anotherClient.syncManager.addPeer(
1926
+ jazzCloudConnectionAsPeerForAnotherClient,
1927
+ );
1928
+
1929
+ await expect(promise1).resolves.not.toBe("unavailable");
1930
+ await expect(promise2).resolves.not.toBe("unavailable");
1931
+ });
1932
+ });
1933
+
1824
1934
  describe("waitForUploadIntoPeer", () => {
1825
1935
  test("should resolve when the coValue is fully uploaded into the peer", async () => {
1826
1936
  const { client, jazzCloudConnectionAsPeer: peer } =
@@ -1896,3 +2006,7 @@ function _admStateEx(adminID: RawAccountID) {
1896
2006
  id: adminID,
1897
2007
  };
1898
2008
  }
2009
+
2010
+ function sleep(ms: number) {
2011
+ return new Promise((resolve) => setTimeout(resolve, ms));
2012
+ }