jazz-tools 0.15.10 → 0.15.12

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 (110) hide show
  1. package/.turbo/turbo-build.log +37 -37
  2. package/CHANGELOG.md +20 -0
  3. package/dist/{chunk-V54IPL5M.js → chunk-DZFT4EZM.js} +426 -303
  4. package/dist/chunk-DZFT4EZM.js.map +1 -0
  5. package/dist/index.js +1 -1
  6. package/dist/react/index.js +1 -3
  7. package/dist/react/testing.js +1 -3
  8. package/dist/react/testing.js.map +1 -1
  9. package/dist/testing.js +1 -1
  10. package/dist/tools/coValues/CoValueBase.d.ts.map +1 -1
  11. package/dist/tools/coValues/coMap.d.ts.map +1 -1
  12. package/dist/tools/coValues/inbox.d.ts.map +1 -1
  13. package/dist/tools/coValues/schemaUnion.d.ts +27 -1
  14. package/dist/tools/coValues/schemaUnion.d.ts.map +1 -1
  15. package/dist/tools/implementation/ContextManager.d.ts.map +1 -1
  16. package/dist/tools/implementation/zodSchema/coExport.d.ts +1 -1
  17. package/dist/tools/implementation/zodSchema/coExport.d.ts.map +1 -1
  18. package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts +6 -3
  19. package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts.map +1 -1
  20. package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts +6 -25
  21. package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts.map +1 -1
  22. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +2 -1
  23. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -1
  24. package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts +30 -0
  25. package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts.map +1 -0
  26. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +2 -1
  27. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
  28. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +3 -3
  29. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
  30. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +3 -3
  31. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
  32. package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts +11 -0
  33. package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts.map +1 -0
  34. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +2 -3
  35. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
  36. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +5 -2
  37. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
  38. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +5 -2
  39. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -1
  40. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +5 -2
  41. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -1
  42. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +7 -12
  43. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -1
  44. package/dist/tools/implementation/zodSchema/unionUtils.d.ts +2 -3
  45. package/dist/tools/implementation/zodSchema/unionUtils.d.ts.map +1 -1
  46. package/dist/tools/implementation/zodSchema/zodCo.d.ts +6 -6
  47. package/dist/tools/implementation/zodSchema/zodCo.d.ts.map +1 -1
  48. package/dist/tools/implementation/zodSchema/zodReExport.d.ts +8 -1
  49. package/dist/tools/implementation/zodSchema/zodReExport.d.ts.map +1 -1
  50. package/dist/tools/implementation/zodSchema/zodSchema.d.ts +18 -13
  51. package/dist/tools/implementation/zodSchema/zodSchema.d.ts.map +1 -1
  52. package/dist/tools/internal.d.ts +1 -0
  53. package/dist/tools/internal.d.ts.map +1 -1
  54. package/dist/tools/subscribe/CoValueCoreSubscription.d.ts.map +1 -1
  55. package/dist/tools/tests/coDiscriminatedUnion.test-d.d.ts +2 -0
  56. package/dist/tools/tests/coDiscriminatedUnion.test-d.d.ts.map +1 -0
  57. package/dist/tools/tests/coDiscriminatedUnion.test.d.ts +2 -0
  58. package/dist/tools/tests/coDiscriminatedUnion.test.d.ts.map +1 -0
  59. package/dist/tools/tests/coOptional.test.d.ts +2 -0
  60. package/dist/tools/tests/coOptional.test.d.ts.map +1 -0
  61. package/dist/tools/tests/testStorage.d.ts +5 -0
  62. package/dist/tools/tests/testStorage.d.ts.map +1 -0
  63. package/dist/tools/tests/zod.test-d.d.ts +2 -0
  64. package/dist/tools/tests/zod.test-d.d.ts.map +1 -0
  65. package/package.json +5 -4
  66. package/src/react-core/tests/useAccount.test.ts +1 -11
  67. package/src/react-core/tests/useCoState.test.ts +20 -0
  68. package/src/tools/coValues/CoValueBase.ts +2 -2
  69. package/src/tools/coValues/inbox.ts +0 -2
  70. package/src/tools/coValues/schemaUnion.ts +62 -0
  71. package/src/tools/implementation/ContextManager.ts +5 -1
  72. package/src/tools/implementation/zodSchema/coExport.ts +2 -0
  73. package/src/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts +29 -12
  74. package/src/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.ts +93 -63
  75. package/src/tools/implementation/zodSchema/schemaTypes/AccountSchema.ts +62 -3
  76. package/src/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.ts +90 -0
  77. package/src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +27 -2
  78. package/src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts +27 -5
  79. package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +60 -5
  80. package/src/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.ts +33 -0
  81. package/src/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +3 -5
  82. package/src/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +32 -2
  83. package/src/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +30 -2
  84. package/src/tools/implementation/zodSchema/schemaTypes/RichTextSchema.ts +26 -2
  85. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +10 -13
  86. package/src/tools/implementation/zodSchema/unionUtils.ts +22 -14
  87. package/src/tools/implementation/zodSchema/zodCo.ts +68 -256
  88. package/src/tools/implementation/zodSchema/zodReExport.ts +34 -5
  89. package/src/tools/implementation/zodSchema/zodSchema.ts +78 -19
  90. package/src/tools/internal.ts +1 -0
  91. package/src/tools/subscribe/CoValueCoreSubscription.ts +4 -0
  92. package/src/tools/tests/ContextManager.test.ts +84 -10
  93. package/src/tools/tests/coDiscriminatedUnion.test-d.ts +57 -0
  94. package/src/tools/tests/coDiscriminatedUnion.test.ts +93 -0
  95. package/src/tools/tests/coList.test.ts +4 -12
  96. package/src/tools/tests/coMap.record.test.ts +4 -4
  97. package/src/tools/tests/coMap.test-d.ts +3 -3
  98. package/src/tools/tests/coMap.test.ts +10 -10
  99. package/src/tools/tests/coOptional.test.ts +39 -0
  100. package/src/tools/tests/createContext.test.ts +5 -4
  101. package/src/tools/tests/deepLoading.test.ts +3 -3
  102. package/src/tools/tests/inbox.test.ts +3 -11
  103. package/src/tools/tests/load.test.ts +9 -0
  104. package/src/tools/tests/patterns/requestToJoin.test.ts +1 -1
  105. package/src/tools/tests/schemaUnion.test.ts +11 -22
  106. package/src/tools/tests/subscribe.test.ts +25 -39
  107. package/src/tools/tests/testStorage.ts +70 -0
  108. package/src/tools/tests/zod.test-d.ts +70 -0
  109. package/src/tools/tests/zod.test.ts +64 -5
  110. package/dist/chunk-V54IPL5M.js.map +0 -1
@@ -1,13 +1,19 @@
1
+ import {
2
+ ZodArray,
3
+ ZodTuple,
4
+ ZodUnion,
5
+ core,
6
+ array as zodArray,
7
+ tuple as zodTuple,
8
+ union as zodUnion,
9
+ } from "zod/v4";
1
10
  export {
2
11
  string,
3
12
  number,
4
13
  boolean,
5
- union,
6
14
  object,
7
- array,
8
15
  templateLiteral,
9
16
  json,
10
- tuple,
11
17
  date,
12
18
  emoji,
13
19
  base64,
@@ -29,8 +35,8 @@ export {
29
35
  int32,
30
36
  strictObject,
31
37
  discriminatedUnion,
32
- // intersection,
33
- // record,
38
+ // record,
39
+ // intersection,
34
40
  int,
35
41
  optional,
36
42
  type ZodOptional,
@@ -42,3 +48,26 @@ export {
42
48
  type ZodDiscriminatedUnion,
43
49
  z,
44
50
  } from "zod/v4";
51
+
52
+ type NonCoZodType = core.$ZodType & { collaborative?: false };
53
+
54
+ export function union<const T extends readonly NonCoZodType[]>(
55
+ options: T,
56
+ params?: string | core.$ZodUnionParams,
57
+ ): ZodUnion<T> {
58
+ return zodUnion(options, params);
59
+ }
60
+
61
+ export function array<T extends NonCoZodType>(
62
+ element: T,
63
+ params?: string | core.$ZodArrayParams,
64
+ ): ZodArray<T> {
65
+ return zodArray(element, params);
66
+ }
67
+
68
+ export function tuple<T extends readonly [NonCoZodType, ...NonCoZodType[]]>(
69
+ options: T,
70
+ params?: string | core.$ZodTupleParams,
71
+ ): ZodTuple<T> {
72
+ return zodTuple(options, params);
73
+ }
@@ -2,23 +2,47 @@ import { LocalNode, RawAccount } from "cojson";
2
2
  import {
3
3
  Account,
4
4
  AccountClass,
5
- CoValue,
6
5
  CoValueClass,
7
6
  CoValueFromRaw,
7
+ InstanceOfSchema,
8
8
  RefsToResolve,
9
9
  RefsToResolveStrict,
10
10
  Resolved,
11
11
  Simplify,
12
12
  } from "../../internal.js";
13
- import { AnyAccountSchema } from "./schemaTypes/AccountSchema.js";
14
- import { AnyCoFeedSchema } from "./schemaTypes/CoFeedSchema.js";
15
- import { AnyCoListSchema } from "./schemaTypes/CoListSchema.js";
16
- import { AnyCoMapSchema, CoMapInitZod } from "./schemaTypes/CoMapSchema.js";
17
- import { AnyCoRecordSchema } from "./schemaTypes/CoRecordSchema.js";
18
- import { FileStreamSchema } from "./schemaTypes/FileStreamSchema.js";
19
- import { PlainTextSchema } from "./schemaTypes/PlainTextSchema.js";
20
- import { RichTextSchema } from "./schemaTypes/RichTextSchema.js";
21
- import { InstanceOfSchema } from "./typeConverters/InstanceOfSchema.js";
13
+ import {
14
+ AccountSchema,
15
+ AnyAccountSchema,
16
+ BaseAccountShape,
17
+ } from "./schemaTypes/AccountSchema.js";
18
+ import {
19
+ AnyDiscriminableCoSchema,
20
+ CoDiscriminatedUnionSchema,
21
+ } from "./schemaTypes/CoDiscriminatedUnionSchema.js";
22
+ import { AnyCoFeedSchema, CoFeedSchema } from "./schemaTypes/CoFeedSchema.js";
23
+ import { AnyCoListSchema, CoListSchema } from "./schemaTypes/CoListSchema.js";
24
+ import {
25
+ AnyCoMapSchema,
26
+ CoMapInitZod,
27
+ CoMapSchema,
28
+ } from "./schemaTypes/CoMapSchema.js";
29
+ import { AnyCoOptionalSchema } from "./schemaTypes/CoOptionalSchema.js";
30
+ import {
31
+ AnyCoRecordSchema,
32
+ CoRecordSchema,
33
+ } from "./schemaTypes/CoRecordSchema.js";
34
+ import {
35
+ AnyFileStreamSchema,
36
+ FileStreamSchema,
37
+ } from "./schemaTypes/FileStreamSchema.js";
38
+ import {
39
+ AnyPlainTextSchema,
40
+ PlainTextSchema,
41
+ } from "./schemaTypes/PlainTextSchema.js";
42
+ import {
43
+ AnyRichTextSchema,
44
+ RichTextSchema,
45
+ } from "./schemaTypes/RichTextSchema.js";
22
46
  import { InstanceOfSchemaCoValuesNullable } from "./typeConverters/InstanceOfSchemaCoValuesNullable.js";
23
47
  import { z } from "./zodReExport.js";
24
48
 
@@ -47,7 +71,7 @@ export type AnyCoUnionSchema = z.core.$ZodDiscriminatedUnion<
47
71
  // this is a series of hacks to work around z4 removing _zod at runtime from z.core.$ZodType
48
72
  export function isZodObject(
49
73
  schema: z.core.$ZodType,
50
- ): schema is z.core.$ZodObject<any, any> {
74
+ ): schema is z.ZodObject<any, any> {
51
75
  return (schema as any).def?.type === "object";
52
76
  }
53
77
 
@@ -67,19 +91,53 @@ export function getDef<S extends z.core.$ZodType>(schema: S): S["_zod"]["def"] {
67
91
  return (schema as any).def;
68
92
  }
69
93
 
94
+ // TODO rename. This represents a CoValue class or a CoValue schema
70
95
  export type CoValueOrZodSchema = CoValueClass | AnyCoSchema;
71
96
 
72
- export type CoValueClassFromZodSchema<S extends z.core.$ZodType> = CoValueClass<
73
- InstanceOfSchema<S>
74
- > &
75
- CoValueFromRaw<InstanceOfSchema<S>> &
76
- (S extends AnyAccountSchema ? AccountClassEssentials : {});
97
+ // TODO rename to CoValueSchemaFromCoProtoSchema
98
+ export type CoValueSchemaFromZodSchema<S extends z.core.$ZodType> =
99
+ S extends z.core.$ZodType
100
+ ? S extends AnyAccountSchema<infer Shape extends BaseAccountShape>
101
+ ? AccountSchema<Shape>
102
+ : S extends AnyCoRecordSchema<infer K, infer V>
103
+ ? CoRecordSchema<K, V>
104
+ : S extends AnyCoMapSchema<infer Shape, infer Config>
105
+ ? CoMapSchema<Shape, Config>
106
+ : S extends AnyCoListSchema<infer T>
107
+ ? CoListSchema<T>
108
+ : S extends AnyCoFeedSchema<infer T>
109
+ ? CoFeedSchema<T>
110
+ : S extends AnyPlainTextSchema
111
+ ? PlainTextSchema
112
+ : S extends AnyRichTextSchema
113
+ ? RichTextSchema
114
+ : S extends AnyFileStreamSchema
115
+ ? FileStreamSchema
116
+ : S extends z.core.$ZodOptional<infer Inner>
117
+ ? CoValueSchemaFromZodSchema<Inner>
118
+ : S extends z.core.$ZodUnion<
119
+ infer Members extends readonly [
120
+ AnyDiscriminableCoSchema,
121
+ ...AnyDiscriminableCoSchema[],
122
+ ]
123
+ >
124
+ ? CoDiscriminatedUnionSchema<Members>
125
+ : never
126
+ : never;
127
+
128
+ export type CoValueClassFromAnySchema<S extends CoValueOrZodSchema> =
129
+ S extends CoValueClass<any>
130
+ ? S
131
+ : CoValueClass<InstanceOfSchema<S>> &
132
+ CoValueFromRaw<InstanceOfSchema<S>> &
133
+ (S extends AnyAccountSchema ? AccountClassEssentials : {});
77
134
 
78
135
  type AccountClassEssentials = {
79
136
  fromRaw: <A extends Account>(this: AccountClass<A>, raw: RawAccount) => A;
80
137
  fromNode: <A extends Account>(this: AccountClass<A>, node: LocalNode) => A;
81
138
  };
82
139
 
140
+ // TODO rename to ProtoCoSchema?
83
141
  export type AnyCoSchema =
84
142
  | AnyCoMapSchema
85
143
  | AnyAccountSchema
@@ -87,9 +145,10 @@ export type AnyCoSchema =
87
145
  | AnyCoListSchema
88
146
  | AnyCoFeedSchema
89
147
  | AnyCoUnionSchema
90
- | PlainTextSchema
91
- | RichTextSchema
92
- | FileStreamSchema;
148
+ | AnyCoOptionalSchema
149
+ | AnyPlainTextSchema
150
+ | AnyRichTextSchema
151
+ | AnyFileStreamSchema;
93
152
 
94
153
  export type Loaded<
95
154
  T extends CoValueClass | AnyCoSchema,
@@ -33,6 +33,7 @@ export * from "./implementation/zodSchema/zodSchema.js";
33
33
  export * from "./implementation/zodSchema/zodCo.js";
34
34
  export * as co from "./implementation/zodSchema/coExport.js";
35
35
  export * from "./implementation/zodSchema/schemaTypes/CoMapSchema.js";
36
+ export * from "./implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.js";
36
37
  export * from "./implementation/zodSchema/schemaTypes/CoRecordSchema.js";
37
38
  export * from "./implementation/zodSchema/schemaTypes/CoListSchema.js";
38
39
  export * from "./implementation/zodSchema/schemaTypes/CoFeedSchema.js";
@@ -28,6 +28,10 @@ export class CoValueCoreSubscription {
28
28
  this.subscribeToState();
29
29
  this.listener("unavailable");
30
30
  }
31
+ })
32
+ .catch((error) => {
33
+ console.error("Unexpected error loading CoValue: ", error);
34
+ this.listener("unavailable");
31
35
  });
32
36
  }
33
37
  }
@@ -1,16 +1,15 @@
1
+ import { StorageAPI } from "cojson";
1
2
  import { WasmCrypto } from "cojson/crypto/WasmCrypto";
2
3
  import { beforeEach, describe, expect, test, vi } from "vitest";
3
4
  import {
4
5
  Account,
5
6
  AccountClass,
6
7
  AuthSecretStorage,
7
- CoMap,
8
8
  Group,
9
9
  InMemoryKVStore,
10
10
  JazzAuthContext,
11
11
  KvStoreContext,
12
12
  co,
13
- coField,
14
13
  z,
15
14
  } from "../exports";
16
15
  import {
@@ -26,6 +25,7 @@ import {
26
25
  CoValueFromRaw,
27
26
  InstanceOfSchema,
28
27
  Loaded,
28
+ anySchemaToCoSchema,
29
29
  zodSchemaToCoSchema,
30
30
  } from "../internal";
31
31
  import {
@@ -33,6 +33,7 @@ import {
33
33
  getPeerConnectedToTestSyncServer,
34
34
  setupJazzTestSync,
35
35
  } from "../testing";
36
+ import { createAsyncStorage, getDbPath } from "./testStorage";
36
37
 
37
38
  const Crypto = await WasmCrypto.create();
38
39
 
@@ -41,12 +42,14 @@ class TestJazzContextManager<Acc extends Account> extends JazzContextManager<
41
42
  JazzContextManagerBaseProps<Acc> & {
42
43
  defaultProfileName?: string;
43
44
  AccountSchema?: AccountClass<Acc>;
45
+ storage?: string;
44
46
  }
45
47
  > {
46
48
  async getNewContext(
47
49
  props: JazzContextManagerBaseProps<Acc> & {
48
50
  defaultProfileName?: string;
49
51
  AccountSchema?: AccountClass<Acc> & CoValueFromRaw<Acc>;
52
+ storage?: string;
50
53
  },
51
54
  authProps?: JazzContextManagerAuthProps,
52
55
  ) {
@@ -59,6 +62,7 @@ class TestJazzContextManager<Acc extends Account> extends JazzContextManager<
59
62
  sessionProvider: randomSessionProvider,
60
63
  authSecretStorage: this.getAuthSecretStorage(),
61
64
  AccountSchema: props.AccountSchema,
65
+ storage: await createAsyncStorage({ filename: props.storage }),
62
66
  });
63
67
 
64
68
  return {
@@ -77,12 +81,14 @@ class TestJazzContextManager<Acc extends Account> extends JazzContextManager<
77
81
  describe("ContextManager", () => {
78
82
  let manager: TestJazzContextManager<Account>;
79
83
  let authSecretStorage: AuthSecretStorage;
84
+ let storage: StorageAPI;
80
85
 
81
86
  function getCurrentValue() {
82
87
  return manager.getCurrentValue() as JazzAuthContext<Account>;
83
88
  }
84
89
 
85
90
  beforeEach(async () => {
91
+ storage = await createAsyncStorage({});
86
92
  KvStoreContext.getInstance().initialize(new InMemoryKVStore());
87
93
  authSecretStorage = new AuthSecretStorage();
88
94
  await authSecretStorage.clear();
@@ -233,6 +239,78 @@ describe("ContextManager", () => {
233
239
  expect(onAnonymousAccountDiscarded).not.toHaveBeenCalled();
234
240
  });
235
241
 
242
+ test("onAnonymousAccountDiscarded should not block the authentication when storage is active", async () => {
243
+ const dbFilename = getDbPath();
244
+
245
+ const AccountRoot = co.map({
246
+ value: z.string(),
247
+ get transferredRoot(): z.ZodOptional<typeof AccountRoot> {
248
+ return co.optional(AccountRoot);
249
+ },
250
+ });
251
+
252
+ let lastRootId: string | undefined;
253
+
254
+ const CustomAccount = co
255
+ .account({
256
+ root: AccountRoot,
257
+ profile: co.profile(),
258
+ })
259
+ .withMigration(async (account) => {
260
+ account.root = AccountRoot.create(
261
+ {
262
+ value: "Hello",
263
+ },
264
+ Group.create(this).makePublic(),
265
+ );
266
+ });
267
+
268
+ const customManager = new TestJazzContextManager<
269
+ InstanceOfSchema<typeof CustomAccount>
270
+ >();
271
+
272
+ await customManager.createContext({
273
+ AccountSchema: anySchemaToCoSchema(CustomAccount),
274
+ storage: dbFilename,
275
+ onAnonymousAccountDiscarded: async (anonymousAccount) => {
276
+ const anonymousAccountWithRoot = await anonymousAccount.ensureLoaded({
277
+ resolve: { root: true },
278
+ });
279
+
280
+ const me = await CustomAccount.getMe().ensureLoaded({
281
+ resolve: { root: true },
282
+ });
283
+
284
+ me.root.transferredRoot = anonymousAccountWithRoot.root;
285
+ },
286
+ });
287
+
288
+ const prevContextNode = customManager.getCurrentValue()!.node;
289
+
290
+ expect(prevContextNode.storage).toBeDefined();
291
+
292
+ const account = (
293
+ customManager.getCurrentValue() as JazzAuthContext<
294
+ InstanceOfSchema<typeof CustomAccount>
295
+ >
296
+ ).me;
297
+
298
+ await customManager.authenticate({
299
+ accountID: account.id,
300
+ accountSecret: account._raw.core.node.getCurrentAgent().agentSecret,
301
+ provider: "test",
302
+ });
303
+
304
+ // The storage should be closed and set to undefined
305
+ expect(prevContextNode.storage).toBeUndefined();
306
+
307
+ const me = await CustomAccount.getMe().ensureLoaded({
308
+ resolve: { root: { transferredRoot: true } },
309
+ });
310
+
311
+ expect(me.root.transferredRoot?.value).toBe("Hello");
312
+ });
313
+
236
314
  test("the migration should be applied correctly on existing accounts", async () => {
237
315
  const AccountRoot = co.map({
238
316
  value: z.string(),
@@ -258,7 +336,7 @@ describe("ContextManager", () => {
258
336
 
259
337
  // Create initial anonymous context
260
338
  await customManager.createContext({
261
- AccountSchema: zodSchemaToCoSchema(CustomAccount),
339
+ AccountSchema: anySchemaToCoSchema(CustomAccount),
262
340
  });
263
341
 
264
342
  const account = (
@@ -267,8 +345,6 @@ describe("ContextManager", () => {
267
345
  >
268
346
  ).me;
269
347
 
270
- console.log("before", account._refs.root?.id);
271
-
272
348
  await customManager.authenticate({
273
349
  accountID: account.id,
274
350
  accountSecret: account._raw.core.node.getCurrentAgent().agentSecret,
@@ -279,8 +355,6 @@ describe("ContextManager", () => {
279
355
  resolve: { root: true },
280
356
  });
281
357
 
282
- console.log("after", me._refs.root?.id);
283
-
284
358
  expect(me.root.id).toBe(lastRootId);
285
359
  });
286
360
 
@@ -313,7 +387,7 @@ describe("ContextManager", () => {
313
387
 
314
388
  // Create initial anonymous context
315
389
  await customManager.createContext({
316
- AccountSchema: zodSchemaToCoSchema(CustomAccount),
390
+ AccountSchema: anySchemaToCoSchema(CustomAccount),
317
391
  });
318
392
 
319
393
  const account = (
@@ -339,7 +413,7 @@ describe("ContextManager", () => {
339
413
  const AccountRoot = co.map({
340
414
  value: z.string(),
341
415
  get transferredRoot(): z.ZodOptional<typeof AccountRoot> {
342
- return z.optional(AccountRoot);
416
+ return co.optional(AccountRoot);
343
417
  },
344
418
  });
345
419
 
@@ -385,7 +459,7 @@ describe("ContextManager", () => {
385
459
  // Create initial anonymous context
386
460
  await customManager.createContext({
387
461
  onAnonymousAccountDiscarded,
388
- AccountSchema: zodSchemaToCoSchema(CustomAccount),
462
+ AccountSchema: anySchemaToCoSchema(CustomAccount),
389
463
  });
390
464
 
391
465
  const account = await createJazzTestAccount({
@@ -0,0 +1,57 @@
1
+ import { beforeEach, describe, test } from "vitest";
2
+ import { Loaded, co, z } from "../exports.js";
3
+ import { createJazzTestAccount, setupJazzTestSync } from "../testing.js";
4
+
5
+ describe("co.discriminatedUnion", () => {
6
+ beforeEach(async () => {
7
+ await setupJazzTestSync();
8
+
9
+ await createJazzTestAccount({
10
+ isCurrentActiveAccount: true,
11
+ creationProps: { name: "Hermes Puggington" },
12
+ });
13
+ });
14
+
15
+ test("can use co.discriminatedUnion with CoValue schemas as values", () => {
16
+ const Dog = co.map({
17
+ type: z.literal("dog"),
18
+ });
19
+ const Cat = co.map({
20
+ type: z.literal("cat"),
21
+ });
22
+ const Person = co.map({
23
+ pet: co.discriminatedUnion("type", [Dog, Cat]),
24
+ });
25
+
26
+ const person = Person.create({
27
+ pet: Dog.create({
28
+ type: "dog",
29
+ }),
30
+ });
31
+
32
+ person.pet = Cat.create({
33
+ type: "cat",
34
+ });
35
+
36
+ type ExpectedType = {
37
+ pet: Loaded<typeof Dog> | Loaded<typeof Cat>;
38
+ };
39
+
40
+ function matches(value: ExpectedType) {
41
+ return value;
42
+ }
43
+
44
+ matches(person);
45
+ });
46
+
47
+ test("cannot use co.discriminatedUnion with zod schemas as values", () => {
48
+ const Person = co.map({
49
+ pet: co.discriminatedUnion("type", [
50
+ // @ts-expect-error: cannot use co.discriminatedUnion with a Zod schema
51
+ z.object({
52
+ type: z.string(),
53
+ }),
54
+ ]),
55
+ });
56
+ });
57
+ });
@@ -0,0 +1,93 @@
1
+ import { beforeEach, describe, expect, test, vi } from "vitest";
2
+ import { Loaded, co, z } from "../exports.js";
3
+ import { createJazzTestAccount, setupJazzTestSync } from "../testing.js";
4
+ import { waitFor } from "./utils.js";
5
+
6
+ describe("co.discriminatedUnion", () => {
7
+ beforeEach(async () => {
8
+ await setupJazzTestSync();
9
+
10
+ await createJazzTestAccount({
11
+ isCurrentActiveAccount: true,
12
+ creationProps: { name: "Hermes Puggington" },
13
+ });
14
+ });
15
+
16
+ test("use co.discriminatedUnion with CoValue schemas as values", () => {
17
+ const Dog = co.map({
18
+ type: z.literal("dog"),
19
+ });
20
+ const Cat = co.map({
21
+ type: z.literal("cat"),
22
+ });
23
+ const Person = co.map({
24
+ pet: co.discriminatedUnion("type", [Dog, Cat]),
25
+ });
26
+
27
+ const person = Person.create({
28
+ pet: Dog.create({
29
+ type: "dog",
30
+ }),
31
+ });
32
+
33
+ expect(person.pet.type).toEqual("dog");
34
+
35
+ person.pet = Cat.create({
36
+ type: "cat",
37
+ });
38
+
39
+ expect(person.pet.type).toEqual("cat");
40
+ });
41
+
42
+ test("load CoValue instances using the DiscriminatedUnion schema", async () => {
43
+ const Dog = co.map({
44
+ type: z.literal("dog"),
45
+ });
46
+ const Cat = co.map({
47
+ type: z.literal("cat"),
48
+ });
49
+ const Pet = co.discriminatedUnion("type", [Dog, Cat]);
50
+
51
+ const dog = Dog.create({ type: "dog" });
52
+ const loadedPet = await Pet.load(dog.id);
53
+ expect(loadedPet?.type).toEqual("dog");
54
+ });
55
+
56
+ test("subscribe to CoValue instances using the DiscriminatedUnion schema", async () => {
57
+ const Person = co.map({
58
+ name: z.string(),
59
+ });
60
+ const Dog = co.map({
61
+ type: z.literal("dog"),
62
+ name: z.string(),
63
+ owner: Person,
64
+ });
65
+ const Cat = co.map({
66
+ type: z.literal("cat"),
67
+ name: z.string(),
68
+ });
69
+ const Pet = co.discriminatedUnion("type", [Dog, Cat]);
70
+
71
+ const dog = Dog.create({
72
+ type: "dog",
73
+ name: "Rex",
74
+ owner: Person.create({ name: "John Doe" }),
75
+ });
76
+
77
+ const updates: Loaded<typeof Pet>[] = [];
78
+ const spy = vi.fn((pet) => updates.push(pet));
79
+
80
+ Pet.subscribe(dog.id, {}, (pet) => {
81
+ expect(pet.type).toEqual("dog");
82
+ spy(pet);
83
+ });
84
+
85
+ expect(spy).not.toHaveBeenCalled();
86
+
87
+ await waitFor(() => expect(spy).toHaveBeenCalled());
88
+
89
+ expect(spy).toHaveBeenCalledTimes(1);
90
+
91
+ expect(updates[0]?.name).toEqual("Rex");
92
+ });
93
+ });
@@ -1,15 +1,7 @@
1
1
  import { WasmCrypto } from "cojson/crypto/WasmCrypto";
2
2
  import { beforeEach, describe, expect, test, vi } from "vitest";
3
- import {
4
- Account,
5
- CoList,
6
- CoMap,
7
- Group,
8
- coField,
9
- subscribeToCoValue,
10
- z,
11
- } from "../index.js";
12
- import { Loaded, co, zodSchemaToCoSchema } from "../internal.js";
3
+ import { Account, Group, subscribeToCoValue, z } from "../index.js";
4
+ import { Loaded, anySchemaToCoSchema, co } from "../internal.js";
13
5
  import { createJazzTestAccount, setupJazzTestSync } from "../testing.js";
14
6
  import { waitFor } from "./utils.js";
15
7
 
@@ -127,7 +119,7 @@ describe("Simple CoList operations", async () => {
127
119
  name: z.string(),
128
120
  });
129
121
 
130
- const Recipe = co.list(z.optional(Ingredient));
122
+ const Recipe = co.list(co.optional(Ingredient));
131
123
 
132
124
  const recipe = Recipe.create(
133
125
  [
@@ -588,7 +580,7 @@ describe("CoList subscription", async () => {
588
580
  const spy = vi.fn((list) => updates.push(list));
589
581
 
590
582
  subscribeToCoValue(
591
- zodSchemaToCoSchema(TestList),
583
+ anySchemaToCoSchema(TestList),
592
584
  list.id,
593
585
  {
594
586
  syncResolution: true,
@@ -281,7 +281,7 @@ describe("CoMap.Record", async () => {
281
281
  });
282
282
 
283
283
  test("Is ok to omit optional fields", () => {
284
- const TestRecord = co.record(z.string(), z.optional(NestedRecord));
284
+ const TestRecord = co.record(z.string(), co.optional(NestedRecord));
285
285
 
286
286
  expectTypeOf<typeof TestRecord.create>().toBeCallableWith(
287
287
  {
@@ -315,7 +315,7 @@ describe("CoMap.Record", async () => {
315
315
 
316
316
  const PersonRecord = co.record(
317
317
  z.string(),
318
- z.discriminatedUnion("type", [Base, IssueRepro]),
318
+ co.discriminatedUnion("type", [Base, IssueRepro]),
319
319
  );
320
320
 
321
321
  const person = IssueRepro.create({
@@ -350,7 +350,7 @@ describe("CoMap.Record", async () => {
350
350
 
351
351
  const PersonRecord = co.record(
352
352
  z.string(),
353
- z.discriminatedUnion("type", [Base, IssueRepro]),
353
+ co.discriminatedUnion("type", [Base, IssueRepro]),
354
354
  );
355
355
 
356
356
  const person = IssueRepro.create({
@@ -385,7 +385,7 @@ describe("CoMap.Record", async () => {
385
385
 
386
386
  const PersonRecord = co.record(
387
387
  z.string(),
388
- z.discriminatedUnion("type", [Base, IssueRepro]),
388
+ co.discriminatedUnion("type", [Base, IssueRepro]),
389
389
  );
390
390
 
391
391
  const person = IssueRepro.create({
@@ -159,7 +159,7 @@ describe("CoMap", async () => {
159
159
  test("Comap with recursive optional reference", () => {
160
160
  const Recursive = co.map({
161
161
  get child(): z.ZodOptional<typeof Recursive> {
162
- return z.optional(Recursive);
162
+ return co.optional(Recursive);
163
163
  },
164
164
  });
165
165
 
@@ -185,7 +185,7 @@ describe("CoMap", async () => {
185
185
  age: z.number(),
186
186
  // TODO: would be nice if this didn't need a type annotation
187
187
  get friend(): z.ZodOptional<typeof Person> {
188
- return z.optional(Person);
188
+ return co.optional(Person);
189
189
  },
190
190
  });
191
191
 
@@ -280,7 +280,7 @@ describe("CoMap", async () => {
280
280
 
281
281
  const MapWithEnumOfMaps = co.map({
282
282
  name: z.string(),
283
- child: z.discriminatedUnion("type", [ChildA, ChildB]),
283
+ child: co.discriminatedUnion("type", [ChildA, ChildB]),
284
284
  });
285
285
 
286
286
  const mapWithEnum = MapWithEnumOfMaps.create({