jazz-tools 0.13.31 → 0.14.1

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.
Files changed (166) hide show
  1. package/.turbo/turbo-build.log +8 -8
  2. package/CHANGELOG.md +22 -3
  3. package/dist/auth/DemoAuth.d.ts.map +1 -1
  4. package/dist/auth/PassphraseAuth.d.ts +1 -3
  5. package/dist/auth/PassphraseAuth.d.ts.map +1 -1
  6. package/dist/{chunk-IJU4XPFS.js → chunk-WLOZKDOH.js} +3536 -3291
  7. package/dist/chunk-WLOZKDOH.js.map +1 -0
  8. package/dist/coValues/CoValueBase.d.ts +22 -0
  9. package/dist/coValues/CoValueBase.d.ts.map +1 -0
  10. package/dist/coValues/account.d.ts +12 -12
  11. package/dist/coValues/account.d.ts.map +1 -1
  12. package/dist/coValues/coFeed.d.ts +24 -25
  13. package/dist/coValues/coFeed.d.ts.map +1 -1
  14. package/dist/coValues/coList.d.ts +10 -13
  15. package/dist/coValues/coList.d.ts.map +1 -1
  16. package/dist/coValues/coMap.d.ts +32 -35
  17. package/dist/coValues/coMap.d.ts.map +1 -1
  18. package/dist/coValues/coPlainText.d.ts.map +1 -1
  19. package/dist/coValues/deepLoading.d.ts +17 -21
  20. package/dist/coValues/deepLoading.d.ts.map +1 -1
  21. package/dist/coValues/extensions/imageDef.d.ts +12 -11
  22. package/dist/coValues/extensions/imageDef.d.ts.map +1 -1
  23. package/dist/coValues/group.d.ts +5 -9
  24. package/dist/coValues/group.d.ts.map +1 -1
  25. package/dist/coValues/inbox.d.ts +2 -3
  26. package/dist/coValues/inbox.d.ts.map +1 -1
  27. package/dist/coValues/interfaces.d.ts +8 -34
  28. package/dist/coValues/interfaces.d.ts.map +1 -1
  29. package/dist/coValues/profile.d.ts +4 -14
  30. package/dist/coValues/profile.d.ts.map +1 -1
  31. package/dist/coValues/registeredSchemas.d.ts +1 -3
  32. package/dist/coValues/registeredSchemas.d.ts.map +1 -1
  33. package/dist/coValues/schemaUnion.d.ts +6 -6
  34. package/dist/exports.d.ts +12 -16
  35. package/dist/exports.d.ts.map +1 -1
  36. package/dist/implementation/ContextManager.d.ts +1 -1
  37. package/dist/implementation/ContextManager.d.ts.map +1 -1
  38. package/dist/implementation/activeAccountContext.d.ts +1 -1
  39. package/dist/implementation/activeAccountContext.d.ts.map +1 -1
  40. package/dist/implementation/createContext.d.ts +10 -10
  41. package/dist/implementation/createContext.d.ts.map +1 -1
  42. package/dist/implementation/invites.d.ts +6 -6
  43. package/dist/implementation/invites.d.ts.map +1 -1
  44. package/dist/implementation/refs.d.ts +2 -2
  45. package/dist/implementation/refs.d.ts.map +1 -1
  46. package/dist/implementation/schema.d.ts +21 -28
  47. package/dist/implementation/schema.d.ts.map +1 -1
  48. package/dist/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts +9 -0
  49. package/dist/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts.map +1 -0
  50. package/dist/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts +28 -0
  51. package/dist/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts.map +1 -0
  52. package/dist/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +65 -0
  53. package/dist/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -0
  54. package/dist/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +28 -0
  55. package/dist/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -0
  56. package/dist/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +24 -0
  57. package/dist/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -0
  58. package/dist/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +41 -0
  59. package/dist/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -0
  60. package/dist/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +35 -0
  61. package/dist/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -0
  62. package/dist/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +9 -0
  63. package/dist/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -0
  64. package/dist/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +20 -0
  65. package/dist/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -0
  66. package/dist/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +18 -0
  67. package/dist/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -0
  68. package/dist/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +24 -0
  69. package/dist/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -0
  70. package/dist/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts +21 -0
  71. package/dist/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts.map +1 -0
  72. package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts +29 -0
  73. package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts.map +1 -0
  74. package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts +29 -0
  75. package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts.map +1 -0
  76. package/dist/implementation/zodSchema/unionUtils.d.ts +6 -0
  77. package/dist/implementation/zodSchema/unionUtils.d.ts.map +1 -0
  78. package/dist/implementation/zodSchema/zodCo.d.ts +35 -0
  79. package/dist/implementation/zodSchema/zodCo.d.ts.map +1 -0
  80. package/dist/implementation/zodSchema/zodSchema.d.ts +38 -0
  81. package/dist/implementation/zodSchema/zodSchema.d.ts.map +1 -0
  82. package/dist/index.js +295 -10
  83. package/dist/index.js.map +1 -1
  84. package/dist/internal.d.ts +34 -0
  85. package/dist/internal.d.ts.map +1 -1
  86. package/dist/subscribe/SubscriptionScope.d.ts +1 -2
  87. package/dist/subscribe/SubscriptionScope.d.ts.map +1 -1
  88. package/dist/subscribe/utils.d.ts +2 -2
  89. package/dist/subscribe/utils.d.ts.map +1 -1
  90. package/dist/testing.d.ts +10 -8
  91. package/dist/testing.d.ts.map +1 -1
  92. package/dist/testing.js +1 -1
  93. package/dist/testing.js.map +1 -1
  94. package/dist/tests/utils.d.ts +6 -2
  95. package/dist/tests/utils.d.ts.map +1 -1
  96. package/dist/types.d.ts +1 -7
  97. package/dist/types.d.ts.map +1 -1
  98. package/package.json +3 -2
  99. package/src/auth/DemoAuth.ts +1 -2
  100. package/src/auth/PassphraseAuth.ts +1 -1
  101. package/src/coValues/CoValueBase.ts +88 -0
  102. package/src/coValues/account.ts +53 -43
  103. package/src/coValues/coFeed.ts +65 -83
  104. package/src/coValues/coList.ts +28 -21
  105. package/src/coValues/coMap.ts +54 -38
  106. package/src/coValues/coPlainText.ts +4 -1
  107. package/src/coValues/deepLoading.ts +35 -43
  108. package/src/coValues/extensions/imageDef.ts +21 -19
  109. package/src/coValues/group.ts +37 -38
  110. package/src/coValues/inbox.ts +24 -11
  111. package/src/coValues/interfaces.ts +29 -93
  112. package/src/coValues/profile.ts +12 -13
  113. package/src/coValues/registeredSchemas.ts +1 -3
  114. package/src/coValues/schemaUnion.ts +6 -6
  115. package/src/exports.ts +47 -25
  116. package/src/implementation/activeAccountContext.ts +1 -1
  117. package/src/implementation/createContext.ts +39 -24
  118. package/src/implementation/invites.ts +15 -12
  119. package/src/implementation/refs.ts +6 -4
  120. package/src/implementation/schema.ts +22 -34
  121. package/src/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts +101 -0
  122. package/src/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.ts +191 -0
  123. package/src/implementation/zodSchema/schemaTypes/AccountSchema.ts +102 -0
  124. package/src/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +70 -0
  125. package/src/implementation/zodSchema/schemaTypes/CoListSchema.ts +59 -0
  126. package/src/implementation/zodSchema/schemaTypes/CoMapSchema.ts +126 -0
  127. package/src/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +98 -0
  128. package/src/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +9 -0
  129. package/src/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +27 -0
  130. package/src/implementation/zodSchema/schemaTypes/RichTextSchema.ts +25 -0
  131. package/src/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +61 -0
  132. package/src/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.ts +77 -0
  133. package/src/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.ts +90 -0
  134. package/src/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.ts +103 -0
  135. package/src/implementation/zodSchema/unionUtils.ts +139 -0
  136. package/src/implementation/zodSchema/zodCo.ts +409 -0
  137. package/src/implementation/zodSchema/zodSchema.ts +116 -0
  138. package/src/internal.ts +38 -0
  139. package/src/subscribe/SubscriptionScope.ts +3 -1
  140. package/src/subscribe/utils.ts +7 -2
  141. package/src/testing.ts +14 -16
  142. package/src/tests/ContextManager.test.ts +73 -47
  143. package/src/tests/DemoAuth.test.ts +1 -1
  144. package/src/tests/account.test.ts +6 -9
  145. package/src/tests/coFeed.test.ts +102 -63
  146. package/src/tests/coList.test.ts +82 -95
  147. package/src/tests/coMap.record.test.ts +53 -87
  148. package/src/tests/coMap.test.ts +297 -312
  149. package/src/tests/coPlainText.test.ts +19 -39
  150. package/src/tests/createContext.test.ts +33 -15
  151. package/src/tests/deepLoading.test.ts +196 -179
  152. package/src/tests/groupsAndAccounts.test.ts +81 -72
  153. package/src/tests/imageDef.test.ts +22 -13
  154. package/src/tests/inbox.test.ts +36 -29
  155. package/src/tests/load.test.ts +10 -10
  156. package/src/tests/patterns/requestToJoin.test.ts +31 -31
  157. package/src/tests/schema.test.ts +37 -38
  158. package/src/tests/schemaUnion.test.ts +54 -64
  159. package/src/tests/subscribe.test.ts +118 -116
  160. package/src/tests/testing.test.ts +33 -33
  161. package/src/tests/utils.ts +3 -2
  162. package/src/types.ts +1 -8
  163. package/dist/chunk-IJU4XPFS.js.map +0 -1
  164. package/dist/tests/deepLoading.test-d.d.ts +0 -2
  165. package/dist/tests/deepLoading.test-d.d.ts.map +0 -1
  166. package/src/tests/deepLoading.test-d.ts +0 -393
@@ -10,6 +10,8 @@ import {
10
10
  JazzAuthContext,
11
11
  KvStoreContext,
12
12
  co,
13
+ coField,
14
+ z,
13
15
  } from "../exports";
14
16
  import {
15
17
  JazzContextManager,
@@ -20,6 +22,12 @@ import {
20
22
  createJazzContext,
21
23
  randomSessionProvider,
22
24
  } from "../implementation/createContext";
25
+ import {
26
+ CoValueFromRaw,
27
+ InstanceOfSchema,
28
+ Loaded,
29
+ zodSchemaToCoSchema,
30
+ } from "../internal";
23
31
  import {
24
32
  createJazzTestAccount,
25
33
  getPeerConnectedToTestSyncServer,
@@ -38,11 +46,11 @@ class TestJazzContextManager<Acc extends Account> extends JazzContextManager<
38
46
  async getNewContext(
39
47
  props: JazzContextManagerBaseProps<Acc> & {
40
48
  defaultProfileName?: string;
41
- AccountSchema?: AccountClass<Acc>;
49
+ AccountSchema?: AccountClass<Acc> & CoValueFromRaw<Acc>;
42
50
  },
43
51
  authProps?: JazzContextManagerAuthProps,
44
52
  ) {
45
- const context = await createJazzContext<Acc>({
53
+ const context = await createJazzContext({
46
54
  credentials: authProps?.credentials,
47
55
  defaultProfileName: props.defaultProfileName,
48
56
  newAccountProps: authProps?.newAccountProps,
@@ -226,31 +234,37 @@ describe("ContextManager", () => {
226
234
  });
227
235
 
228
236
  test("the migration should be applied correctly on existing accounts", async () => {
229
- class AccountRoot extends CoMap {
230
- value = co.string;
231
- }
237
+ const AccountRoot = co.map({
238
+ value: z.string(),
239
+ });
232
240
 
233
241
  let lastRootId: string | undefined;
234
242
 
235
- class CustomAccount extends Account {
236
- root = co.ref(AccountRoot);
237
-
238
- migrate() {
239
- this.root = AccountRoot.create({
243
+ const CustomAccount = co
244
+ .account({
245
+ root: AccountRoot,
246
+ profile: co.profile(),
247
+ })
248
+ .withMigration(async (account) => {
249
+ account.root = AccountRoot.create({
240
250
  value: "Hello",
241
251
  });
242
- lastRootId = this.root.id;
243
- }
244
- }
245
- const customManager = new TestJazzContextManager<CustomAccount>();
252
+ lastRootId = account.root.id;
253
+ });
254
+
255
+ const customManager = new TestJazzContextManager<
256
+ InstanceOfSchema<typeof CustomAccount>
257
+ >();
246
258
 
247
259
  // Create initial anonymous context
248
260
  await customManager.createContext({
249
- AccountSchema: CustomAccount,
261
+ AccountSchema: zodSchemaToCoSchema(CustomAccount),
250
262
  });
251
263
 
252
264
  const account = (
253
- customManager.getCurrentValue() as JazzAuthContext<CustomAccount>
265
+ customManager.getCurrentValue() as JazzAuthContext<
266
+ InstanceOfSchema<typeof CustomAccount>
267
+ >
254
268
  ).me;
255
269
 
256
270
  console.log("before", account._refs.root?.id);
@@ -271,34 +285,41 @@ describe("ContextManager", () => {
271
285
  });
272
286
 
273
287
  test("the migration should be applied correctly on existing accounts (2)", async () => {
274
- class AccountRoot extends CoMap {
275
- value = co.number;
276
- }
277
-
278
- class CustomAccount extends Account {
279
- root = co.ref(AccountRoot);
288
+ const AccountRoot = co.map({
289
+ value: z.number(),
290
+ });
280
291
 
281
- async migrate(this: CustomAccount) {
282
- if (this.root === undefined) {
283
- this.root = AccountRoot.create({
292
+ const CustomAccount = co
293
+ .account({
294
+ root: AccountRoot,
295
+ profile: co.profile(),
296
+ })
297
+ .withMigration(async (account) => {
298
+ if (account.root === undefined) {
299
+ account.root = AccountRoot.create({
284
300
  value: 1,
285
301
  });
286
302
  } else {
287
- const { root } = await this.ensureLoaded({ resolve: { root: true } });
303
+ const { root } = await account.ensureLoaded({
304
+ resolve: { root: true },
305
+ });
288
306
 
289
307
  root.value = 2;
290
308
  }
291
- }
292
- }
293
- const customManager = new TestJazzContextManager<CustomAccount>();
309
+ });
310
+ const customManager = new TestJazzContextManager<
311
+ InstanceOfSchema<typeof CustomAccount>
312
+ >();
294
313
 
295
314
  // Create initial anonymous context
296
315
  await customManager.createContext({
297
- AccountSchema: CustomAccount,
316
+ AccountSchema: zodSchemaToCoSchema(CustomAccount),
298
317
  });
299
318
 
300
319
  const account = (
301
- customManager.getCurrentValue() as JazzAuthContext<CustomAccount>
320
+ customManager.getCurrentValue() as JazzAuthContext<
321
+ InstanceOfSchema<typeof CustomAccount>
322
+ >
302
323
  ).me;
303
324
 
304
325
  await customManager.authenticate({
@@ -315,25 +336,28 @@ describe("ContextManager", () => {
315
336
  });
316
337
 
317
338
  test("onAnonymousAccountDiscarded should work on transfering data between accounts", async () => {
318
- class AccountRoot extends CoMap {
319
- value = co.string;
320
- transferredRoot = co.optional.ref(AccountRoot);
321
- }
322
-
323
- class CustomAccount extends Account {
324
- root = co.ref(AccountRoot);
339
+ const AccountRoot = co.map({
340
+ value: z.string(),
341
+ get transferredRoot(): z.ZodOptional<typeof AccountRoot> {
342
+ return z.optional(AccountRoot);
343
+ },
344
+ });
325
345
 
326
- migrate() {
327
- if (this.root === undefined) {
328
- this.root = AccountRoot.create({
346
+ const CustomAccount = co
347
+ .account({
348
+ root: AccountRoot,
349
+ profile: co.profile(),
350
+ })
351
+ .withMigration(async (account) => {
352
+ if (account.root === undefined) {
353
+ account.root = AccountRoot.create({
329
354
  value: "Hello",
330
355
  });
331
356
  }
332
- }
333
- }
357
+ });
334
358
 
335
359
  const onAnonymousAccountDiscarded = async (
336
- anonymousAccount: CustomAccount,
360
+ anonymousAccount: Loaded<typeof CustomAccount, { root: true }>,
337
361
  ) => {
338
362
  const anonymousAccountWithRoot = await anonymousAccount.ensureLoaded({
339
363
  resolve: {
@@ -354,17 +378,19 @@ describe("ContextManager", () => {
354
378
  meWithRoot.root.transferredRoot = rootToTransfer;
355
379
  };
356
380
 
357
- const customManager = new TestJazzContextManager<CustomAccount>();
381
+ const customManager = new TestJazzContextManager<
382
+ InstanceOfSchema<typeof CustomAccount>
383
+ >();
358
384
 
359
385
  // Create initial anonymous context
360
386
  await customManager.createContext({
361
387
  onAnonymousAccountDiscarded,
362
- AccountSchema: CustomAccount,
388
+ AccountSchema: zodSchemaToCoSchema(CustomAccount),
363
389
  });
364
390
 
365
391
  const account = await createJazzTestAccount({
366
392
  isCurrentActiveAccount: true,
367
- AccountSchema: CustomAccount,
393
+ AccountSchema: zodSchemaToCoSchema(CustomAccount),
368
394
  });
369
395
 
370
396
  await customManager.authenticate({
@@ -4,7 +4,7 @@ import { AuthSecretStorage } from "../auth/AuthSecretStorage";
4
4
  import { DemoAuth } from "../auth/DemoAuth";
5
5
  import { InMemoryKVStore } from "../auth/InMemoryKVStore";
6
6
  import { KvStoreContext } from "../auth/KvStoreContext";
7
- import { Account } from "../coValues/account";
7
+ import { Account } from "../exports";
8
8
  import { ID } from "../internal";
9
9
  import { createJazzTestAccount } from "../testing";
10
10
 
@@ -1,6 +1,5 @@
1
- import { LocalNode } from "cojson";
2
1
  import { assert, beforeEach, expect, test } from "vitest";
3
- import { Account, CoMap, Group, co } from "../exports.js";
2
+ import { Account, Group, co, z } from "../exports.js";
4
3
  import {
5
4
  createJazzTestAccount,
6
5
  linkAccounts,
@@ -14,9 +13,9 @@ beforeEach(async () => {
14
13
  });
15
14
 
16
15
  test("waitForAllCoValuesSync should resolve when all the values are synced", async () => {
17
- class TestMap extends CoMap {
18
- name = co.string;
19
- }
16
+ const TestMap = co.map({
17
+ name: z.string(),
18
+ });
20
19
 
21
20
  const { clientNode, serverNode, clientAccount } = await setupTwoNodes();
22
21
 
@@ -99,10 +98,8 @@ test("accounts should sync correctly", async () => {
99
98
 
100
99
  group.addMember(otherAccount, "writer");
101
100
 
102
- const { members } = group;
103
-
104
- expect(members[0]?.account.profile!.name).toBe("test 1");
105
- expect(members[1]?.account.profile!.name).toBe("test 2");
101
+ expect(group.members[0]?.account.profile!.name).toBe("test 1");
102
+ expect(group.members[1]?.account.profile!.name).toBe("test 2");
106
103
  });
107
104
 
108
105
  test("loading accounts should work", async () => {
@@ -1,5 +1,5 @@
1
1
  import { WasmCrypto } from "cojson/crypto/WasmCrypto";
2
- import { describe, expect, test } from "vitest";
2
+ import { describe, expect, expectTypeOf, test } from "vitest";
3
3
  import {
4
4
  Account,
5
5
  CoFeed,
@@ -7,10 +7,14 @@ import {
7
7
  Group,
8
8
  ID,
9
9
  co,
10
+ coField,
10
11
  cojsonInternals,
11
12
  isControlledAccount,
13
+ z,
12
14
  } from "../index.js";
13
15
  import {
16
+ AnyCoFeedSchema,
17
+ Loaded,
14
18
  createJazzContextFromExistingCredentials,
15
19
  randomSessionProvider,
16
20
  } from "../internal.js";
@@ -29,19 +33,19 @@ describe("Simple CoFeed operations", async () => {
29
33
  if (!isControlledAccount(me)) {
30
34
  throw "me is not a controlled account";
31
35
  }
32
- class TestStream extends CoFeed.Of(co.string) {}
36
+ const TestStream = co.feed(z.string());
33
37
 
34
38
  const stream = TestStream.create(["milk"], { owner: me });
35
39
 
36
40
  test("Construction", () => {
37
- expect(stream[me.id]?.value).toEqual("milk");
41
+ expect(stream.perAccount[me.id]?.value).toEqual("milk");
38
42
  expect(stream.perSession[me.sessionID]?.value).toEqual("milk");
39
43
  });
40
44
 
41
45
  test("Construction with an Account", () => {
42
46
  const stream = TestStream.create(["milk"], me);
43
47
 
44
- expect(stream[me.id]?.value).toEqual("milk");
48
+ expect(stream.perAccount[me.id]?.value).toEqual("milk");
45
49
  expect(stream.perSession[me.sessionID]?.value).toEqual("milk");
46
50
  });
47
51
 
@@ -49,33 +53,27 @@ describe("Simple CoFeed operations", async () => {
49
53
  const group = Group.create(me);
50
54
  const stream = TestStream.create(["milk"], group);
51
55
 
52
- expect(stream[me.id]?.value).toEqual("milk");
56
+ expect(stream.perAccount[me.id]?.value).toEqual("milk");
53
57
  expect(stream.perSession[me.sessionID]?.value).toEqual("milk");
54
58
  });
55
59
 
56
60
  describe("Mutation", () => {
57
61
  test("pushing", () => {
58
62
  stream.push("bread");
59
- expect(stream[me.id]?.value).toEqual("bread");
63
+ expect(stream.perAccount[me.id]?.value).toEqual("bread");
60
64
  expect(stream.perSession[me.sessionID]?.value).toEqual("bread");
61
65
 
62
66
  stream.push("butter");
63
- expect(stream[me.id]?.value).toEqual("butter");
67
+ expect(stream.perAccount[me.id]?.value).toEqual("butter");
64
68
  expect(stream.perSession[me.sessionID]?.value).toEqual("butter");
65
69
  });
66
70
  });
67
71
  });
68
72
 
69
73
  describe("CoFeed resolution", async () => {
70
- class TwiceNestedStream extends CoFeed.Of(co.string) {
71
- fancyValueOf(account: ID<Account>) {
72
- return "Sir " + this[account]?.value;
73
- }
74
- }
75
-
76
- class NestedStream extends CoFeed.Of(co.ref(TwiceNestedStream)) {}
77
-
78
- class TestStream extends CoFeed.Of(co.ref(NestedStream)) {}
74
+ const TwiceNestedStream = co.feed(z.string());
75
+ const NestedStream = co.feed(TwiceNestedStream);
76
+ const TestStream = co.feed(NestedStream);
79
77
 
80
78
  const initNodeAndStream = async () => {
81
79
  const me = await Account.create({
@@ -98,9 +96,15 @@ describe("CoFeed resolution", async () => {
98
96
 
99
97
  test("Construction", async () => {
100
98
  const { me, stream } = await initNodeAndStream();
101
- expect(stream[me.id]?.value?.[me.id]?.value?.[me.id]?.value).toEqual(
102
- "milk",
103
- );
99
+
100
+ // TODO: fix this
101
+ // expectTypeOf(stream[me.id]).not.toBeAny();
102
+
103
+ expect(
104
+ stream.perAccount[me.id]?.value?.perAccount[me.id]?.value?.perAccount[
105
+ me.id
106
+ ]?.value,
107
+ ).toEqual("milk");
104
108
  });
105
109
 
106
110
  test("Loading and availability", async () => {
@@ -128,46 +132,52 @@ describe("CoFeed resolution", async () => {
128
132
  loadAs: meOnSecondPeer,
129
133
  });
130
134
 
131
- expect(loadedStream?.[me.id]?.value).toEqual(null);
132
- expect(loadedStream?.[me.id]?.ref?.id).toEqual(stream[me.id]?.value?.id);
135
+ // TODO: fix this
136
+ // expectTypeOf(loadedStream?.[me.id]).not.toBeAny();
137
+
138
+ expect(loadedStream?.perAccount[me.id]?.value).toEqual(null);
139
+ expect(loadedStream?.perAccount[me.id]?.ref?.id).toEqual(
140
+ stream.perAccount[me.id]?.value?.id,
141
+ );
133
142
 
134
143
  const loadedNestedStream = await NestedStream.load(
135
- stream[me.id]!.value!.id,
144
+ stream.perAccount[me.id]!.value!.id,
136
145
  { loadAs: meOnSecondPeer },
137
146
  );
138
147
 
139
148
  // expect(loadedStream?.[me.id]?.value).toEqual(loadedNestedStream);
140
- expect(loadedStream?.[me.id]?.value?.id).toEqual(loadedNestedStream?.id);
141
- expect(loadedStream?.[me.id]?.value?.[me.id]?.value).toEqual(null);
142
- // expect(loadedStream?.[me.id]?.ref?.value).toEqual(loadedNestedStream);
143
- expect(loadedStream?.[me.id]?.ref?.value?.id).toEqual(
149
+ expect(loadedStream?.perAccount[me.id]?.value?.id).toEqual(
144
150
  loadedNestedStream?.id,
145
151
  );
146
- expect(loadedStream?.[me.id]?.value?.[me.id]?.ref?.id).toEqual(
147
- stream[me.id]?.value?.[me.id]?.value?.id,
152
+ expect(
153
+ loadedStream?.perAccount[me.id]?.value?.perAccount[me.id]?.value,
154
+ ).toEqual(null);
155
+ // expect(loadedStream?.[me.id]?.ref?.value).toEqual(loadedNestedStream);
156
+ expect(loadedStream?.perAccount[me.id]?.ref?.value?.id).toEqual(
157
+ loadedNestedStream?.id,
148
158
  );
159
+ expect(
160
+ loadedStream?.perAccount[me.id]?.value?.perAccount[me.id]?.ref?.id,
161
+ ).toEqual(stream.perAccount[me.id]?.value?.perAccount[me.id]?.value?.id);
149
162
 
150
163
  const loadedTwiceNestedStream = await TwiceNestedStream.load(
151
- stream[me.id]!.value![me.id]!.value!.id,
164
+ stream.perAccount[me.id]!.value!.perAccount[me.id]!.value!.id,
152
165
  { loadAs: meOnSecondPeer },
153
166
  );
154
167
 
155
168
  // expect(loadedStream?.[me.id]?.value?.[me.id]?.value).toEqual(
156
169
  // loadedTwiceNestedStream
157
170
  // );
158
- expect(loadedStream?.[me.id]?.value?.[me.id]?.value?.id).toEqual(
159
- loadedTwiceNestedStream?.id,
160
- );
161
171
  expect(
162
- loadedStream?.[me.id]?.value?.[me.id]?.value?.fancyValueOf(me.id),
163
- ).toEqual("Sir milk");
172
+ loadedStream?.perAccount[me.id]?.value?.perAccount[me.id]?.value?.id,
173
+ ).toEqual(loadedTwiceNestedStream?.id);
164
174
  // expect(loadedStream?.[me.id]?.ref?.value).toEqual(loadedNestedStream);
165
- expect(loadedStream?.[me.id]?.ref?.value?.id).toEqual(
175
+ expect(loadedStream?.perAccount[me.id]?.ref?.value?.id).toEqual(
166
176
  loadedNestedStream?.id,
167
177
  );
168
- expect(loadedStream?.[me.id]?.value?.[me.id]?.ref?.value?.id).toEqual(
169
- loadedTwiceNestedStream?.id,
170
- );
178
+ expect(
179
+ loadedStream?.perAccount[me.id]?.value?.perAccount[me.id]?.ref?.value?.id,
180
+ ).toEqual(loadedTwiceNestedStream?.id);
171
181
 
172
182
  const otherNestedStream = NestedStream.create(
173
183
  [TwiceNestedStream.create(["butter"], { owner: meOnSecondPeer })],
@@ -175,16 +185,15 @@ describe("CoFeed resolution", async () => {
175
185
  );
176
186
  loadedStream?.push(otherNestedStream);
177
187
  // expect(loadedStream?.[me.id]?.value).toEqual(otherNestedStream);
178
- expect(loadedStream?.[me.id]?.value?.id).toEqual(otherNestedStream?.id);
179
- expect(loadedStream?.[me.id]?.ref?.value?.id).toEqual(
188
+ expect(loadedStream?.perAccount[me.id]?.value?.id).toEqual(
180
189
  otherNestedStream?.id,
181
190
  );
182
- expect(loadedStream?.[me.id]?.value?.[me.id]?.value?.id).toEqual(
183
- otherNestedStream[me.id]?.value?.id,
191
+ expect(loadedStream?.perAccount[me.id]?.ref?.value?.id).toEqual(
192
+ otherNestedStream?.id,
184
193
  );
185
194
  expect(
186
- loadedStream?.[me.id]?.value?.[me.id]?.value?.fancyValueOf(me.id),
187
- ).toEqual("Sir butter");
195
+ loadedStream?.perAccount[me.id]?.value?.perAccount[me.id]?.value?.id,
196
+ ).toEqual(otherNestedStream.perAccount[me.id]?.value?.id);
188
197
  });
189
198
 
190
199
  test("Subscription & auto-resolution", async () => {
@@ -221,20 +230,32 @@ describe("CoFeed resolution", async () => {
221
230
  );
222
231
 
223
232
  const update1 = (await queue.next()).value;
224
- expect(update1[me.id]?.value).toEqual(null);
233
+ expect(update1.perAccount[me.id]?.value).toEqual(null);
225
234
 
226
235
  const update2 = (await queue.next()).value;
227
- expect(update2[me.id]?.value).toBeDefined();
228
- expect(update2[me.id]?.value?.[me.id]?.value).toBe(null);
236
+ expect(update2.perAccount[me.id]?.value).toBeDefined();
237
+ expect(update2.perAccount[me.id]?.value?.perAccount[me.id]?.value).toBe(
238
+ null,
239
+ );
229
240
 
230
241
  const update3 = (await queue.next()).value;
231
- expect(update3[me.id]?.value?.[me.id]?.value).toBeDefined();
232
- expect(update3[me.id]?.value?.[me.id]?.value?.[me.id]?.value).toBe("milk");
242
+ expect(
243
+ update3.perAccount[me.id]?.value?.perAccount[me.id]?.value,
244
+ ).toBeDefined();
245
+ expect(
246
+ update3.perAccount[me.id]?.value?.perAccount[me.id]?.value?.perAccount[
247
+ me.id
248
+ ]?.value,
249
+ ).toBe("milk");
233
250
 
234
- update3[me.id]!.value![me.id]!.value!.push("bread");
251
+ update3.perAccount[me.id]!.value!.perAccount[me.id]!.value!.push("bread");
235
252
 
236
253
  const update4 = (await queue.next()).value;
237
- expect(update4[me.id]?.value?.[me.id]?.value?.[me.id]?.value).toBe("bread");
254
+ expect(
255
+ update4.perAccount[me.id]?.value?.perAccount[me.id]?.value?.perAccount[
256
+ me.id
257
+ ]?.value,
258
+ ).toBe("bread");
238
259
 
239
260
  // When assigning a new nested stream, we get an update
240
261
  const newTwiceNested = TwiceNestedStream.create(["butter"], {
@@ -248,14 +269,20 @@ describe("CoFeed resolution", async () => {
248
269
  update4.push(newNested);
249
270
 
250
271
  const update5 = (await queue.next()).value;
251
- expect(update5[me.id]?.value?.[me.id]?.value?.[me.id]?.value).toBe(
252
- "butter",
253
- );
272
+ expect(
273
+ update5.perAccount[me.id]?.value?.perAccount[me.id]?.value?.perAccount[
274
+ me.id
275
+ ]?.value,
276
+ ).toBe("butter");
254
277
 
255
278
  // we get updates when the new nested stream changes
256
279
  newTwiceNested.push("jam");
257
280
  const update6 = (await queue.next()).value;
258
- expect(update6[me.id]?.value?.[me.id]?.value?.[me.id]?.value).toBe("jam");
281
+ expect(
282
+ update6.perAccount[me.id]?.value?.perAccount[me.id]?.value?.perAccount[
283
+ me.id
284
+ ]?.value,
285
+ ).toBe("jam");
259
286
  });
260
287
 
261
288
  test("Subscription without options", async () => {
@@ -280,26 +307,38 @@ describe("CoFeed resolution", async () => {
280
307
  crypto: Crypto,
281
308
  });
282
309
 
283
- const queue = new cojsonInternals.Channel();
310
+ const queue = new cojsonInternals.Channel<Loaded<typeof TestStream>>();
284
311
 
285
312
  TestStream.subscribe(stream.id, (subscribedStream) => {
286
313
  void queue.push(subscribedStream);
287
314
  });
288
315
 
289
316
  const update1 = (await queue.next()).value;
290
- expect(update1[me.id]?.value).toEqual(null);
317
+ expect(update1.perAccount[me.id]?.value).toEqual(null);
291
318
 
292
319
  const update2 = (await queue.next()).value;
293
- expect(update2[me.id]?.value?.[me.id]?.value).toEqual(null);
320
+ expect(update2.perAccount[me.id]?.value?.perAccount[me.id]?.value).toEqual(
321
+ null,
322
+ );
294
323
 
295
324
  const update3 = (await queue.next()).value;
296
- expect(update3[me.id]?.value?.[me.id]?.value).toBeDefined();
297
- expect(update3[me.id]?.value?.[me.id]?.value?.[me.id]?.value).toBe("milk");
325
+ expect(
326
+ update3.perAccount[me.id]?.value?.perAccount[me.id]?.value,
327
+ ).toBeDefined();
328
+ expect(
329
+ update3.perAccount[me.id]?.value?.perAccount[me.id]?.value?.perAccount[
330
+ me.id
331
+ ]?.value,
332
+ ).toBe("milk");
298
333
 
299
- update3[me.id]!.value![me.id]!.value!.push("bread");
334
+ update3.perAccount[me.id]!.value!.perAccount[me.id]!.value!.push("bread");
300
335
 
301
336
  const update4 = (await queue.next()).value;
302
- expect(update4[me.id]?.value?.[me.id]?.value?.[me.id]?.value).toBe("bread");
337
+ expect(
338
+ update4.perAccount[me.id]?.value?.perAccount[me.id]?.value?.perAccount[
339
+ me.id
340
+ ]?.value,
341
+ ).toBe("bread");
303
342
  });
304
343
  });
305
344
 
@@ -611,7 +650,7 @@ describe("FileStream progress tracking", async () => {
611
650
 
612
651
  describe("waitForSync", async () => {
613
652
  test("CoFeed: should resolve when the value is uploaded", async () => {
614
- class TestStream extends CoFeed.Of(co.string) {}
653
+ const TestStream = co.feed(z.string());
615
654
 
616
655
  const { clientNode, serverNode, clientAccount } = await setupTwoNodes();
617
656