jazz-tools 0.7.25 → 0.7.26

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,6 @@
1
1
  import { expect, describe, test } from "vitest";
2
2
  import { connectedPeers } from "cojson/src/streamUtils.js";
3
3
  import { newRandomSessionID } from "cojson/src/coValueCore.js";
4
- import { Effect, Queue } from "effect";
5
4
  import {
6
5
  BinaryCoStream,
7
6
  ID,
@@ -10,6 +9,7 @@ import {
10
9
  co,
11
10
  WasmCrypto,
12
11
  isControlledAccount,
12
+ cojsonInternals,
13
13
  } from "../index.js";
14
14
 
15
15
  const Crypto = await WasmCrypto.create();
@@ -83,11 +83,13 @@ describe("CoStream resolution", async () => {
83
83
 
84
84
  test("Loading and availability", async () => {
85
85
  const { me, stream } = await initNodeAndStream();
86
- const [initialAsPeer, secondPeer] = await Effect.runPromise(
87
- connectedPeers("initial", "second", {
86
+ const [initialAsPeer, secondPeer] = connectedPeers(
87
+ "initial",
88
+ "second",
89
+ {
88
90
  peer1role: "server",
89
91
  peer2role: "client",
90
- }),
92
+ },
91
93
  );
92
94
  if (!isControlledAccount(me)) {
93
95
  throw "me is not a controlled account";
@@ -176,12 +178,15 @@ describe("CoStream resolution", async () => {
176
178
  test("Subscription & auto-resolution", async () => {
177
179
  const { me, stream } = await initNodeAndStream();
178
180
 
179
- const [initialAsPeer, secondAsPeer] = await Effect.runPromise(
180
- connectedPeers("initial", "second", {
181
+ const [initialAsPeer, secondAsPeer] = connectedPeers(
182
+ "initial",
183
+ "second",
184
+ {
181
185
  peer1role: "server",
182
186
  peer2role: "client",
183
- }),
187
+ },
184
188
  );
189
+
185
190
  me._raw.core.node.syncManager.addPeer(secondAsPeer);
186
191
  if (!isControlledAccount(me)) {
187
192
  throw "me is not a controlled account";
@@ -195,78 +200,68 @@ describe("CoStream resolution", async () => {
195
200
  crypto: Crypto,
196
201
  });
197
202
 
198
- await Effect.runPromise(
199
- Effect.gen(function* ($) {
200
- const queue = yield* $(Queue.unbounded<TestStream>());
201
-
202
- TestStream.subscribe(
203
- stream.id,
204
- meOnSecondPeer,
205
- [],
206
- (subscribedStream) => {
207
- console.log(
208
- "subscribedStream[me.id]",
209
- subscribedStream[me.id],
210
- );
211
- console.log(
212
- "subscribedStream[me.id]?.value?.[me.id]?.value",
213
- subscribedStream[me.id]?.value?.[me.id]?.value,
214
- );
215
- console.log(
216
- "subscribedStream[me.id]?.value?.[me.id]?.value?.[me.id]?.value",
217
- subscribedStream[me.id]?.value?.[me.id]?.value?.[
218
- me.id
219
- ]?.value,
220
- );
221
- void Effect.runPromise(
222
- Queue.offer(queue, subscribedStream),
223
- );
224
- },
203
+ const queue = new cojsonInternals.Channel();
204
+
205
+ TestStream.subscribe(
206
+ stream.id,
207
+ meOnSecondPeer,
208
+ [],
209
+ (subscribedStream) => {
210
+ console.log("subscribedStream[me.id]", subscribedStream[me.id]);
211
+ console.log(
212
+ "subscribedStream[me.id]?.value?.[me.id]?.value",
213
+ subscribedStream[me.id]?.value?.[me.id]?.value,
225
214
  );
215
+ console.log(
216
+ "subscribedStream[me.id]?.value?.[me.id]?.value?.[me.id]?.value",
217
+ subscribedStream[me.id]?.value?.[me.id]?.value?.[me.id]
218
+ ?.value,
219
+ );
220
+ void queue.push(subscribedStream);
221
+ },
222
+ );
223
+
224
+ const update1 = (await queue.next()).value;
225
+ expect(update1[me.id]?.value).toEqual(null);
226
+
227
+ const update2 = (await queue.next()).value;
228
+ expect(update2[me.id]?.value).toBeDefined();
229
+ expect(update2[me.id]?.value?.[me.id]?.value).toBe(null);
230
+
231
+ const update3 = (await queue.next()).value;
232
+ expect(update3[me.id]?.value?.[me.id]?.value).toBeDefined();
233
+ expect(update3[me.id]?.value?.[me.id]?.value?.[me.id]?.value).toBe(
234
+ "milk",
235
+ );
236
+
237
+ update3[me.id]!.value![me.id]!.value!.push("bread");
238
+
239
+ const update4 = (await queue.next()).value;
240
+ expect(update4[me.id]?.value?.[me.id]?.value?.[me.id]?.value).toBe(
241
+ "bread",
242
+ );
243
+
244
+ // When assigning a new nested stream, we get an update
245
+ const newTwiceNested = TwiceNestedStream.create(["butter"], {
246
+ owner: meOnSecondPeer,
247
+ });
226
248
 
227
- const update1 = yield* $(Queue.take(queue));
228
- expect(update1[me.id]?.value).toEqual(null);
229
-
230
- const update2 = yield* $(Queue.take(queue));
231
- expect(update2[me.id]?.value).toBeDefined();
232
- expect(update2[me.id]?.value?.[me.id]?.value).toBe(null);
233
-
234
- const update3 = yield* $(Queue.take(queue));
235
- expect(update3[me.id]?.value?.[me.id]?.value).toBeDefined();
236
- expect(
237
- update3[me.id]?.value?.[me.id]?.value?.[me.id]?.value,
238
- ).toBe("milk");
239
-
240
- update3[me.id]!.value![me.id]!.value!.push("bread");
241
-
242
- const update4 = yield* $(Queue.take(queue));
243
- expect(
244
- update4[me.id]?.value?.[me.id]?.value?.[me.id]?.value,
245
- ).toBe("bread");
246
-
247
- // When assigning a new nested stream, we get an update
248
- const newTwiceNested = TwiceNestedStream.create(["butter"], {
249
- owner: meOnSecondPeer,
250
- });
251
-
252
- const newNested = NestedStream.create([newTwiceNested], {
253
- owner: meOnSecondPeer,
254
- });
255
-
256
- update4.push(newNested);
257
-
258
- const update5 = yield* $(Queue.take(queue));
259
- expect(
260
- update5[me.id]?.value?.[me.id]?.value?.[me.id]?.value,
261
- ).toBe("butter");
262
-
263
- // we get updates when the new nested stream changes
264
- newTwiceNested.push("jam");
265
- const update6 = yield* $(Queue.take(queue));
266
- expect(
267
- update6[me.id]?.value?.[me.id]?.value?.[me.id]?.value,
268
- ).toBe("jam");
269
- }),
249
+ const newNested = NestedStream.create([newTwiceNested], {
250
+ owner: meOnSecondPeer,
251
+ });
252
+
253
+ update4.push(newNested);
254
+
255
+ const update5 = (await queue.next()).value;
256
+ expect(update5[me.id]?.value?.[me.id]?.value?.[me.id]?.value).toBe(
257
+ "butter",
258
+ );
259
+
260
+ // we get updates when the new nested stream changes
261
+ newTwiceNested.push("jam");
262
+ const update6 = (await queue.next()).value;
263
+ expect(update6[me.id]?.value?.[me.id]?.value?.[me.id]?.value).toBe(
264
+ "jam",
270
265
  );
271
266
  });
272
267
  });
@@ -327,11 +322,13 @@ describe("BinaryCoStream loading & Subscription", async () => {
327
322
 
328
323
  test("Loading and availability", async () => {
329
324
  const { me, stream } = await initNodeAndStream();
330
- const [initialAsPeer, secondAsPeer] = await Effect.runPromise(
331
- connectedPeers("initial", "second", {
325
+ const [initialAsPeer, secondAsPeer] = connectedPeers(
326
+ "initial",
327
+ "second",
328
+ {
332
329
  peer1role: "server",
333
330
  peer2role: "client",
334
- }),
331
+ },
335
332
  );
336
333
  if (!isControlledAccount(me)) {
337
334
  throw "me is not a controlled account";
@@ -360,98 +357,83 @@ describe("BinaryCoStream loading & Subscription", async () => {
360
357
  });
361
358
 
362
359
  test("Subscription", async () => {
363
- await Effect.runPromise(
364
- Effect.gen(function* ($) {
365
- const { me } = yield* Effect.promise(() => initNodeAndStream());
366
-
367
- const stream = BinaryCoStream.create({ owner: me });
360
+ const { me } = await initNodeAndStream();
361
+ const stream = BinaryCoStream.create({ owner: me });
368
362
 
369
- const [initialAsPeer, secondAsPeer] = yield* connectedPeers(
370
- "initial",
371
- "second",
372
- { peer1role: "server", peer2role: "client" },
373
- );
374
- me._raw.core.node.syncManager.addPeer(secondAsPeer);
375
- if (!isControlledAccount(me)) {
376
- throw "me is not a controlled account";
377
- }
378
- const meOnSecondPeer = yield* Effect.promise(() =>
379
- Account.become({
380
- accountID: me.id,
381
- accountSecret: me._raw.agentSecret,
382
- peersToLoadFrom: [initialAsPeer],
383
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
384
- sessionID: newRandomSessionID(me.id as any),
385
- crypto: Crypto,
386
- }),
387
- );
363
+ const [initialAsPeer, secondAsPeer] = connectedPeers(
364
+ "initial",
365
+ "second",
366
+ { peer1role: "server", peer2role: "client" },
367
+ );
368
+ me._raw.core.node.syncManager.addPeer(secondAsPeer);
369
+ if (!isControlledAccount(me)) {
370
+ throw "me is not a controlled account";
371
+ }
372
+ const meOnSecondPeer = await Account.become({
373
+ accountID: me.id,
374
+ accountSecret: me._raw.agentSecret,
375
+ peersToLoadFrom: [initialAsPeer],
376
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
377
+ sessionID: newRandomSessionID(me.id as any),
378
+ crypto: Crypto,
379
+ });
388
380
 
389
- const queue = yield* $(Queue.unbounded<BinaryCoStream>());
390
-
391
- BinaryCoStream.subscribe(
392
- stream.id,
393
- meOnSecondPeer,
394
- [],
395
- (subscribedStream) => {
396
- void Effect.runPromise(
397
- Queue.offer(queue, subscribedStream),
398
- );
399
- },
400
- );
381
+ const queue = new cojsonInternals.Channel();
401
382
 
402
- const update1 = yield* $(Queue.take(queue));
403
- expect(update1.getChunks()).toBe(undefined);
404
-
405
- stream.start({ mimeType: "text/plain" });
406
-
407
- const update2 = yield* $(Queue.take(queue));
408
- expect(update2.getChunks({ allowUnfinished: true })).toEqual({
409
- mimeType: "text/plain",
410
- fileName: undefined,
411
- chunks: [],
412
- totalSizeBytes: undefined,
413
- finished: false,
414
- });
415
-
416
- stream.push(new Uint8Array([1, 2, 3]));
417
-
418
- const update3 = yield* $(Queue.take(queue));
419
- expect(update3.getChunks({ allowUnfinished: true })).toEqual({
420
- mimeType: "text/plain",
421
- fileName: undefined,
422
- chunks: [new Uint8Array([1, 2, 3])],
423
- totalSizeBytes: undefined,
424
- finished: false,
425
- });
426
-
427
- stream.push(new Uint8Array([4, 5, 6]));
428
-
429
- const update4 = yield* $(Queue.take(queue));
430
- expect(update4.getChunks({ allowUnfinished: true })).toEqual({
431
- mimeType: "text/plain",
432
- fileName: undefined,
433
- chunks: [
434
- new Uint8Array([1, 2, 3]),
435
- new Uint8Array([4, 5, 6]),
436
- ],
437
- totalSizeBytes: undefined,
438
- finished: false,
439
- });
440
-
441
- stream.end();
442
-
443
- const update5 = yield* $(Queue.take(queue));
444
- expect(update5.getChunks()).toEqual({
445
- mimeType: "text/plain",
446
- fileName: undefined,
447
- chunks: [
448
- new Uint8Array([1, 2, 3]),
449
- new Uint8Array([4, 5, 6]),
450
- ],
451
- totalSizeBytes: undefined,
452
- finished: true,
453
- });
454
- }),
383
+ BinaryCoStream.subscribe(
384
+ stream.id,
385
+ meOnSecondPeer,
386
+ [],
387
+ (subscribedStream) => {
388
+ void queue.push(subscribedStream);
389
+ },
455
390
  );
391
+
392
+ const update1 = (await queue.next()).value;
393
+ expect(update1.getChunks()).toBe(undefined);
394
+
395
+ stream.start({ mimeType: "text/plain" });
396
+
397
+ const update2 = (await queue.next()).value;
398
+ expect(update2.getChunks({ allowUnfinished: true })).toEqual({
399
+ mimeType: "text/plain",
400
+ fileName: undefined,
401
+ chunks: [],
402
+ totalSizeBytes: undefined,
403
+ finished: false,
404
+ });
405
+
406
+ stream.push(new Uint8Array([1, 2, 3]));
407
+
408
+ const update3 = (await queue.next()).value;
409
+ expect(update3.getChunks({ allowUnfinished: true })).toEqual({
410
+ mimeType: "text/plain",
411
+ fileName: undefined,
412
+ chunks: [new Uint8Array([1, 2, 3])],
413
+ totalSizeBytes: undefined,
414
+ finished: false,
415
+ });
416
+
417
+ stream.push(new Uint8Array([4, 5, 6]));
418
+
419
+ const update4 = (await queue.next()).value;
420
+ expect(update4.getChunks({ allowUnfinished: true })).toEqual({
421
+ mimeType: "text/plain",
422
+ fileName: undefined,
423
+ chunks: [new Uint8Array([1, 2, 3]), new Uint8Array([4, 5, 6])],
424
+ totalSizeBytes: undefined,
425
+ finished: false,
426
+ });
427
+
428
+ stream.end();
429
+
430
+ const update5 = (await queue.next()).value;
431
+ expect(update5.getChunks()).toEqual({
432
+ mimeType: "text/plain",
433
+ fileName: undefined,
434
+ chunks: [new Uint8Array([1, 2, 3]), new Uint8Array([4, 5, 6])],
435
+ totalSizeBytes: undefined,
436
+ finished: true,
437
+ });
456
438
  });
457
439
  });
@@ -14,7 +14,6 @@ import {
14
14
  ID,
15
15
  } from "../index.js";
16
16
  import { newRandomSessionID } from "cojson/src/coValueCore.js";
17
- import { Effect } from "effect";
18
17
 
19
18
  class TestMap extends CoMap {
20
19
  list = co.ref(TestList);
@@ -39,12 +38,11 @@ describe("Deep loading with depth arg", async () => {
39
38
  crypto: Crypto,
40
39
  });
41
40
 
42
- const [initialAsPeer, secondPeer] = await Effect.runPromise(
43
- connectedPeers("initial", "second", {
44
- peer1role: "server",
45
- peer2role: "client",
46
- }),
47
- );
41
+ const [initialAsPeer, secondPeer] = connectedPeers("initial", "second", {
42
+ peer1role: "server",
43
+ peer2role: "client",
44
+ });
45
+
48
46
  if (!isControlledAccount(me)) {
49
47
  throw "me is not a controlled account";
50
48
  }
@@ -140,8 +138,8 @@ describe("Deep loading with depth arg", async () => {
140
138
  throw new Error("map4 is undefined");
141
139
  }
142
140
  expect(map4.list[0]?.stream).not.toBe(null);
143
- expect(map4.list[0]?.stream?.[me.id]?.value).toBe(null);
144
- expect(map4.list[0]?.stream?.byMe?.value).toBe(null);
141
+ expect(map4.list[0]?.stream?.[me.id]).toBe(undefined)
142
+ expect(map4.list[0]?.stream?.byMe?.value).toBe(undefined);
145
143
 
146
144
  const map5 = await TestMap.load(map.id, meOnSecondPeer, {
147
145
  list: [{ stream: [{}] }],
@@ -254,15 +252,15 @@ test("Deep loading a record-like coMap", async () => {
254
252
  crypto: Crypto,
255
253
  });
256
254
 
257
- const [initialAsPeer, secondPeer] = await Effect.runPromise(
258
- connectedPeers("initial", "second", {
259
- peer1role: "server",
260
- peer2role: "client",
261
- }),
262
- );
255
+ const [initialAsPeer, secondPeer] = connectedPeers("initial", "second", {
256
+ peer1role: "server",
257
+ peer2role: "client",
258
+ });
259
+
263
260
  if (!isControlledAccount(me)) {
264
261
  throw "me is not a controlled account";
265
262
  }
263
+
266
264
  me._raw.core.node.syncManager.addPeer(secondPeer);
267
265
  const meOnSecondPeer = await Account.become({
268
266
  accountID: me.id,