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
@@ -12,7 +12,7 @@ import {
12
12
  } from "vitest";
13
13
  import { Group, co, subscribeToCoValue, z } from "../exports.js";
14
14
  import { Account } from "../index.js";
15
- import { ID, Loaded, zodSchemaToCoSchema } from "../internal.js";
15
+ import { Loaded, anySchemaToCoSchema } from "../internal.js";
16
16
  import {
17
17
  createJazzTestAccount,
18
18
  getPeerConnectedToTestSyncServer,
@@ -145,7 +145,7 @@ describe("CoMap", async () => {
145
145
  age: z.number(),
146
146
  // TODO: would be nice if this didn't need a type annotation
147
147
  get friend(): z.ZodOptional<typeof Person> {
148
- return z.optional(Person);
148
+ return co.optional(Person);
149
149
  },
150
150
  });
151
151
 
@@ -182,7 +182,7 @@ describe("CoMap", async () => {
182
182
  name: z.string(),
183
183
  age: z.number(),
184
184
  get friend(): z.ZodOptional<typeof Person> {
185
- return z.optional(Person);
185
+ return co.optional(Person);
186
186
  },
187
187
  });
188
188
 
@@ -211,7 +211,7 @@ describe("CoMap", async () => {
211
211
  name: z.string(),
212
212
  age: z.number(),
213
213
  get friend(): z.ZodOptional<typeof Person> {
214
- return z.optional(Person);
214
+ return co.optional(Person);
215
215
  },
216
216
  });
217
217
 
@@ -400,7 +400,7 @@ describe("CoMap", async () => {
400
400
 
401
401
  const MapWithEnumOfMaps = co.map({
402
402
  name: z.string(),
403
- child: z.discriminatedUnion("type", [ChildA, ChildB]),
403
+ child: co.discriminatedUnion("type", [ChildA, ChildB]),
404
404
  });
405
405
 
406
406
  const mapWithEnum = MapWithEnumOfMaps.create({
@@ -801,7 +801,7 @@ describe("CoMap resolution", async () => {
801
801
  const spy = vi.fn((person) => updates.push(person));
802
802
 
803
803
  subscribeToCoValue(
804
- zodSchemaToCoSchema(Person), // TODO: we should get rid of the conversion in the future
804
+ anySchemaToCoSchema(Person), // TODO: we should get rid of the conversion in the future
805
805
  person.id,
806
806
  {
807
807
  syncResolution: true,
@@ -1003,7 +1003,7 @@ describe("CoMap applyDiff", async () => {
1003
1003
  birthday: z.date(),
1004
1004
  nested: NestedMap,
1005
1005
  optionalField: z.string().optional(),
1006
- optionalNested: z.optional(NestedMap),
1006
+ optionalNested: co.optional(NestedMap),
1007
1007
  });
1008
1008
 
1009
1009
  test("Basic applyDiff", () => {
@@ -1698,7 +1698,7 @@ describe("Creating and finding unique CoMaps", async () => {
1698
1698
  dateValue: z.date(),
1699
1699
  });
1700
1700
 
1701
- const AttributeValue = z.discriminatedUnion("type", [
1701
+ const AttributeValue = co.discriminatedUnion("type", [
1702
1702
  StringAttributeValue,
1703
1703
  NumberAttributeValue,
1704
1704
  DateAttributeValue,
@@ -1714,7 +1714,7 @@ describe("Creating and finding unique CoMaps", async () => {
1714
1714
  attributeValue: AttributeValue,
1715
1715
  });
1716
1716
 
1717
- const Tag = z.discriminatedUnion("type", [
1717
+ const Tag = co.discriminatedUnion("type", [
1718
1718
  AttributeTag,
1719
1719
  StringTag,
1720
1720
  DateTag,
@@ -1764,7 +1764,7 @@ describe("Creating and finding unique CoMaps", async () => {
1764
1764
  error: HttpError,
1765
1765
  });
1766
1766
 
1767
- const ErrorResponse = z.discriminatedUnion("type", [
1767
+ const ErrorResponse = co.discriminatedUnion("type", [
1768
1768
  ClientError,
1769
1769
  ServerError,
1770
1770
  NetworkError,
@@ -0,0 +1,39 @@
1
+ import { beforeEach, describe, test } from "vitest";
2
+ import { CoPlainText, co, z } from "../exports.js";
3
+ import { createJazzTestAccount, setupJazzTestSync } from "../testing.js";
4
+
5
+ describe("co.optional", () => {
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.optional with CoValue schemas as values", () => {
16
+ const Person = co.map({
17
+ preferredName: co.optional(co.plainText()),
18
+ });
19
+
20
+ const person = Person.create({});
21
+
22
+ type ExpectedType = {
23
+ preferredName?: CoPlainText;
24
+ };
25
+
26
+ function matches(value: ExpectedType) {
27
+ return value;
28
+ }
29
+
30
+ matches(person);
31
+ });
32
+
33
+ test("cannot use co.optional with zod schemas as values", () => {
34
+ const Person = co.map({
35
+ // @ts-expect-error: cannot use co.optional with a Zod schema
36
+ preferredName: co.optional(z.string()),
37
+ });
38
+ });
39
+ });
@@ -1,4 +1,4 @@
1
- import { AgentSecret, RawCoMap } from "cojson";
1
+ import { AgentSecret } from "cojson";
2
2
  import { WasmCrypto } from "cojson/crypto/WasmCrypto";
3
3
  import { beforeEach, describe, expect, test, vi } from "vitest";
4
4
  import {
@@ -9,6 +9,7 @@ import {
9
9
  ID,
10
10
  InMemoryKVStore,
11
11
  KvStoreContext,
12
+ anySchemaToCoSchema,
12
13
  co,
13
14
  createAnonymousJazzContext,
14
15
  createJazzContext,
@@ -90,7 +91,7 @@ describe("createContext methods", () => {
90
91
  });
91
92
 
92
93
  expect(context.account).toBeInstanceOf(
93
- zodSchemaToCoSchema(CustomAccount),
94
+ anySchemaToCoSchema(CustomAccount),
94
95
  );
95
96
  });
96
97
 
@@ -199,7 +200,7 @@ describe("createContext methods", () => {
199
200
  });
200
201
 
201
202
  expect(context.account).toBeInstanceOf(
202
- zodSchemaToCoSchema(CustomAccount),
203
+ anySchemaToCoSchema(CustomAccount),
203
204
  );
204
205
  });
205
206
 
@@ -350,7 +351,7 @@ describe("createContext methods", () => {
350
351
  });
351
352
 
352
353
  expect(context.account).toBeInstanceOf(
353
- zodSchemaToCoSchema(CustomAccount),
354
+ anySchemaToCoSchema(CustomAccount),
354
355
  );
355
356
  });
356
357
  });
@@ -36,7 +36,7 @@ const TestList = co.list(InnerMap);
36
36
 
37
37
  const TestMap = co.map({
38
38
  list: TestList,
39
- optionalRef: z.optional(InnermostMap),
39
+ optionalRef: co.optional(InnermostMap),
40
40
  });
41
41
 
42
42
  describe("Deep loading with depth arg", async () => {
@@ -599,7 +599,7 @@ describe("Deep loading with unauthorized account", async () => {
599
599
  });
600
600
 
601
601
  const Lv2 = co.map({
602
- lv3: z.optional(Lv3),
602
+ lv3: co.optional(Lv3),
603
603
  });
604
604
 
605
605
  const Lv1 = co.map({
@@ -737,7 +737,7 @@ describe("Deep loading with unauthorized account", async () => {
737
737
  const Person = co.map({
738
738
  name: z.string(),
739
739
  get friends(): z.ZodOptional<typeof Friends> {
740
- return z.optional(Friends);
740
+ return co.optional(Friends);
741
741
  },
742
742
  });
743
743
  const Friends: CoListSchema<typeof Person> = co.list(Person); // TODO: annoying that we have to annotate
@@ -1,14 +1,6 @@
1
1
  import { describe, expect, it, vi } from "vitest";
2
- import {
3
- Account,
4
- CoMap,
5
- Group,
6
- Inbox,
7
- InboxSender,
8
- Profile,
9
- z,
10
- } from "../exports";
11
- import { Loaded, co, coField, zodSchemaToCoSchema } from "../internal";
2
+ import { Group, Inbox, InboxSender, z } from "../exports";
3
+ import { Loaded, anySchemaToCoSchema, co } from "../internal";
12
4
  import { setupTwoNodes, waitFor } from "./utils";
13
5
 
14
6
  const Message = co.map({
@@ -26,7 +18,7 @@ describe("Inbox", () => {
26
18
 
27
19
  const { clientAccount: sender, serverAccount: receiver } =
28
20
  await setupTwoNodes({
29
- ServerAccountSchema: zodSchemaToCoSchema(WorkerAccount),
21
+ ServerAccountSchema: anySchemaToCoSchema(WorkerAccount),
30
22
  });
31
23
 
32
24
  await expect(() => InboxSender.load(receiver.id, sender)).rejects.toThrow(
@@ -33,6 +33,15 @@ test("load a value", async () => {
33
33
  expect(john?.name).toBe("John");
34
34
  });
35
35
 
36
+ test("return null if id is invalid", async () => {
37
+ const Person = co.map({
38
+ name: z.string(),
39
+ });
40
+
41
+ const john = await Person.load("test");
42
+ expect(john).toBeNull();
43
+ });
44
+
36
45
  test("retry an unavailable value", async () => {
37
46
  const Person = co.map({
38
47
  name: z.string(),
@@ -1,5 +1,5 @@
1
1
  import { assert, describe, expect, test } from "vitest";
2
- import { Account, CoList, CoMap, Group, ID, co, z } from "../../exports";
2
+ import { Account, Group, co, z } from "../../exports";
3
3
  import { createJazzTestAccount, linkAccounts } from "../../testing";
4
4
 
5
5
  const RequestToJoin = co.map({
@@ -6,7 +6,6 @@ import {
6
6
  Loaded,
7
7
  anySchemaToCoSchema,
8
8
  co,
9
- loadCoValue,
10
9
  subscribeToCoValue,
11
10
  z,
12
11
  } from "../exports.js";
@@ -24,7 +23,7 @@ const BlueButtonWidget = co.map({
24
23
  blueness: z.number(),
25
24
  });
26
25
 
27
- const ButtonWidget = z.discriminatedUnion("type", [
26
+ const ButtonWidget = co.discriminatedUnion("type", [
28
27
  RedButtonWidget,
29
28
  BlueButtonWidget,
30
29
  ]);
@@ -40,7 +39,7 @@ const CheckboxWidget = co.map({
40
39
  checked: z.boolean(),
41
40
  });
42
41
 
43
- const WidgetUnion = z.discriminatedUnion("type", [
42
+ const WidgetUnion = co.discriminatedUnion("type", [
44
43
  ButtonWidget,
45
44
  SliderWidget,
46
45
  CheckboxWidget,
@@ -72,25 +71,15 @@ describe("SchemaUnion", () => {
72
71
  { owner: me },
73
72
  );
74
73
 
75
- const loadedButtonWidget = await loadCoValue(
76
- anySchemaToCoSchema(WidgetUnion),
77
- buttonWidget.id,
78
- {
79
- loadAs: me,
80
- },
81
- );
82
- const loadedSliderWidget = await loadCoValue(
83
- anySchemaToCoSchema(WidgetUnion),
84
- sliderWidget.id,
85
- {
86
- loadAs: me,
87
- },
88
- );
89
- const loadedCheckboxWidget = await loadCoValue(
90
- anySchemaToCoSchema(WidgetUnion),
91
- checkboxWidget.id,
92
- { loadAs: me },
93
- );
74
+ const loadedButtonWidget = await WidgetUnion.load(buttonWidget.id, {
75
+ loadAs: me,
76
+ });
77
+ const loadedSliderWidget = await WidgetUnion.load(sliderWidget.id, {
78
+ loadAs: me,
79
+ });
80
+ const loadedCheckboxWidget = await WidgetUnion.load(checkboxWidget.id, {
81
+ loadAs: me,
82
+ });
94
83
 
95
84
  expect(loadedButtonWidget?.type).toBe("button");
96
85
  expect(loadedSliderWidget?.type).toBe("slider");
@@ -7,27 +7,13 @@ import {
7
7
  onTestFinished,
8
8
  vi,
9
9
  } from "vitest";
10
+ import { Account, Group, cojsonInternals, z } from "../index.js";
10
11
  import {
11
- Account,
12
- CoFeed,
13
- CoList,
14
- CoMap,
15
- FileStream,
16
- Group,
17
- coField,
18
- cojsonInternals,
19
- z,
20
- } from "../index.js";
21
- import {
22
- CoMapInstance,
23
- ID,
24
- InstanceOrPrimitiveOfSchema,
25
12
  Loaded,
26
- Resolved,
13
+ anySchemaToCoSchema,
27
14
  co,
28
15
  createCoValueObservable,
29
16
  subscribeToCoValue,
30
- zodSchemaToCoSchema,
31
17
  } from "../internal.js";
32
18
  import {
33
19
  createJazzTestAccount,
@@ -43,7 +29,7 @@ const ReactionsFeed = co.feed(z.string());
43
29
  const Message = co.map({
44
30
  text: z.string(),
45
31
  reactions: ReactionsFeed,
46
- attachment: z.optional(co.fileStream()),
32
+ attachment: co.optional(co.fileStream()),
47
33
  });
48
34
 
49
35
  const ChatRoom = co.map({
@@ -84,7 +70,7 @@ describe("subscribeToCoValue", () => {
84
70
  let result = null as Loaded<typeof ChatRoom, true> | null;
85
71
 
86
72
  const unsubscribe = subscribeToCoValue(
87
- zodSchemaToCoSchema(ChatRoom),
73
+ anySchemaToCoSchema(ChatRoom),
88
74
  chatRoom.id,
89
75
  { loadAs: meOnSecondPeer },
90
76
  (value) => {
@@ -131,7 +117,7 @@ describe("subscribeToCoValue", () => {
131
117
  let result = null as Loaded<typeof ChatRoom, {}> | null;
132
118
 
133
119
  const unsubscribe = subscribeToCoValue(
134
- zodSchemaToCoSchema(ChatRoom),
120
+ anySchemaToCoSchema(ChatRoom),
135
121
  chatRoom.id,
136
122
  {
137
123
  loadAs: meOnSecondPeer,
@@ -172,7 +158,7 @@ describe("subscribeToCoValue", () => {
172
158
  messages.push(createMessage(me, "Hello"));
173
159
 
174
160
  const unsubscribe = subscribeToCoValue(
175
- zodSchemaToCoSchema(ChatRoom),
161
+ anySchemaToCoSchema(ChatRoom),
176
162
  chatRoom.id,
177
163
  {
178
164
  loadAs: meOnSecondPeer,
@@ -215,7 +201,7 @@ describe("subscribeToCoValue", () => {
215
201
  const updateFn = vi.fn();
216
202
 
217
203
  const unsubscribe = subscribeToCoValue(
218
- zodSchemaToCoSchema(ChatRoom),
204
+ anySchemaToCoSchema(ChatRoom),
219
205
  chatRoom.id,
220
206
  {
221
207
  loadAs: meOnSecondPeer,
@@ -275,7 +261,7 @@ describe("subscribeToCoValue", () => {
275
261
  >[];
276
262
 
277
263
  const unsubscribe = subscribeToCoValue(
278
- zodSchemaToCoSchema(ChatRoom),
264
+ anySchemaToCoSchema(ChatRoom),
279
265
  chatRoom.id,
280
266
  {
281
267
  loadAs: meOnSecondPeer,
@@ -346,7 +332,7 @@ describe("subscribeToCoValue", () => {
346
332
  const updateFn = vi.fn();
347
333
 
348
334
  const unsubscribe = subscribeToCoValue(
349
- zodSchemaToCoSchema(ChatRoom),
335
+ anySchemaToCoSchema(ChatRoom),
350
336
  chatRoom.id,
351
337
  {
352
338
  loadAs: meOnSecondPeer,
@@ -410,7 +396,7 @@ describe("subscribeToCoValue", () => {
410
396
  const updateFn = vi.fn();
411
397
 
412
398
  const unsubscribe = subscribeToCoValue(
413
- zodSchemaToCoSchema(TestList),
399
+ anySchemaToCoSchema(TestList),
414
400
  list.id,
415
401
  {
416
402
  loadAs: account,
@@ -471,7 +457,7 @@ describe("subscribeToCoValue", () => {
471
457
  const onUnauthorized = vi.fn();
472
458
 
473
459
  const unsubscribe = subscribeToCoValue(
474
- zodSchemaToCoSchema(TestList),
460
+ anySchemaToCoSchema(TestList),
475
461
  list.id,
476
462
  {
477
463
  loadAs: reader,
@@ -542,7 +528,7 @@ describe("subscribeToCoValue", () => {
542
528
  const onUnavailable = vi.fn();
543
529
 
544
530
  const unsubscribe = subscribeToCoValue(
545
- zodSchemaToCoSchema(TestList),
531
+ anySchemaToCoSchema(TestList),
546
532
  list.id,
547
533
  {
548
534
  loadAs: reader,
@@ -617,7 +603,7 @@ describe("subscribeToCoValue", () => {
617
603
  const onUnavailable = vi.fn();
618
604
 
619
605
  const unsubscribe = subscribeToCoValue(
620
- zodSchemaToCoSchema(TestList),
606
+ anySchemaToCoSchema(TestList),
621
607
  list.id,
622
608
  {
623
609
  loadAs: reader,
@@ -654,7 +640,7 @@ describe("subscribeToCoValue", () => {
654
640
  value: z.string(),
655
641
  });
656
642
 
657
- const TestList = co.list(z.optional(TestMap));
643
+ const TestList = co.list(co.optional(TestMap));
658
644
 
659
645
  const reader = await createJazzTestAccount({
660
646
  isCurrentActiveAccount: true,
@@ -687,7 +673,7 @@ describe("subscribeToCoValue", () => {
687
673
  const onUnavailable = vi.fn();
688
674
 
689
675
  const unsubscribe = subscribeToCoValue(
690
- zodSchemaToCoSchema(TestList),
676
+ anySchemaToCoSchema(TestList),
691
677
  list.id,
692
678
  {
693
679
  loadAs: reader,
@@ -718,7 +704,7 @@ describe("subscribeToCoValue", () => {
718
704
  value: z.string(),
719
705
  });
720
706
 
721
- const TestList = co.list(z.optional(TestMap));
707
+ const TestList = co.list(co.optional(TestMap));
722
708
 
723
709
  const creator = await createJazzTestAccount({
724
710
  isCurrentActiveAccount: true,
@@ -739,7 +725,7 @@ describe("subscribeToCoValue", () => {
739
725
  });
740
726
 
741
727
  const unsubscribe = subscribeToCoValue(
742
- zodSchemaToCoSchema(TestList),
728
+ anySchemaToCoSchema(TestList),
743
729
  list.id,
744
730
  {
745
731
  loadAs: creator,
@@ -806,7 +792,7 @@ describe("subscribeToCoValue", () => {
806
792
  });
807
793
 
808
794
  const unsubscribe = subscribeToCoValue(
809
- zodSchemaToCoSchema(TestList),
795
+ anySchemaToCoSchema(TestList),
810
796
  list.id,
811
797
  {
812
798
  loadAs: creator,
@@ -895,7 +881,7 @@ describe("subscribeToCoValue", () => {
895
881
  });
896
882
 
897
883
  const unsubscribe = subscribeToCoValue(
898
- zodSchemaToCoSchema(Person),
884
+ anySchemaToCoSchema(Person),
899
885
  person.id,
900
886
  {
901
887
  loadAs: reader,
@@ -982,7 +968,7 @@ describe("subscribeToCoValue", () => {
982
968
  const onUnavailable = vi.fn();
983
969
 
984
970
  const unsubscribe = subscribeToCoValue(
985
- zodSchemaToCoSchema(TestList),
971
+ anySchemaToCoSchema(TestList),
986
972
  list.id,
987
973
  {
988
974
  loadAs: reader,
@@ -1069,7 +1055,7 @@ describe("subscribeToCoValue", () => {
1069
1055
  const onUnavailable = vi.fn();
1070
1056
 
1071
1057
  const unsubscribe = subscribeToCoValue(
1072
- zodSchemaToCoSchema(PersonList),
1058
+ anySchemaToCoSchema(PersonList),
1073
1059
  list.id,
1074
1060
  {
1075
1061
  loadAs: reader,
@@ -1165,7 +1151,7 @@ describe("subscribeToCoValue", () => {
1165
1151
  const onUnavailable = vi.fn();
1166
1152
 
1167
1153
  const unsubscribe = subscribeToCoValue(
1168
- zodSchemaToCoSchema(PersonList),
1154
+ anySchemaToCoSchema(PersonList),
1169
1155
  list.id,
1170
1156
  {
1171
1157
  loadAs: reader,
@@ -1248,7 +1234,7 @@ describe("subscribeToCoValue", () => {
1248
1234
 
1249
1235
  // Test subscribing to the large coValue
1250
1236
  const unsubscribe = subscribeToCoValue(
1251
- zodSchemaToCoSchema(LargeDataset),
1237
+ anySchemaToCoSchema(LargeDataset),
1252
1238
  largeMap.id,
1253
1239
  {
1254
1240
  loadAs: alice,
@@ -1314,7 +1300,7 @@ describe("createCoValueObservable", () => {
1314
1300
  const mockListener = vi.fn();
1315
1301
 
1316
1302
  const unsubscribe = observable.subscribe(
1317
- zodSchemaToCoSchema(TestMap),
1303
+ anySchemaToCoSchema(TestMap),
1318
1304
  testMap.id,
1319
1305
  {
1320
1306
  loadAs: meOnSecondPeer,
@@ -1343,7 +1329,7 @@ describe("createCoValueObservable", () => {
1343
1329
  const mockListener = vi.fn();
1344
1330
 
1345
1331
  const unsubscribe = observable.subscribe(
1346
- zodSchemaToCoSchema(TestMap),
1332
+ anySchemaToCoSchema(TestMap),
1347
1333
  testMap.id,
1348
1334
  {
1349
1335
  loadAs: meOnSecondPeer,
@@ -0,0 +1,70 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { unlinkSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { SQLiteDatabaseDriverAsync, getSqliteStorageAsync } from "cojson";
6
+ import Database, { type Database as DatabaseT } from "libsql";
7
+ import { onTestFinished } from "vitest";
8
+
9
+ class LibSQLSqliteAsyncDriver implements SQLiteDatabaseDriverAsync {
10
+ private readonly db: DatabaseT;
11
+
12
+ constructor(filename: string) {
13
+ this.db = new Database(filename, {});
14
+ }
15
+
16
+ async initialize() {
17
+ await this.db.pragma("journal_mode = WAL");
18
+ }
19
+
20
+ async run(sql: string, params: unknown[]) {
21
+ this.db.prepare(sql).run(params);
22
+ }
23
+
24
+ async query<T>(sql: string, params: unknown[]): Promise<T[]> {
25
+ return this.db.prepare(sql).all(params) as T[];
26
+ }
27
+
28
+ async get<T>(sql: string, params: unknown[]): Promise<T | undefined> {
29
+ return this.db.prepare(sql).get(params) as T | undefined;
30
+ }
31
+
32
+ async transaction(callback: () => unknown) {
33
+ await this.run("BEGIN TRANSACTION", []);
34
+
35
+ try {
36
+ await callback();
37
+ await this.run("COMMIT", []);
38
+ } catch (error) {
39
+ await this.run("ROLLBACK", []);
40
+ }
41
+ }
42
+
43
+ async closeDb() {
44
+ this.db.close();
45
+ }
46
+ }
47
+
48
+ export async function createAsyncStorage({ filename }: { filename?: string }) {
49
+ const storage = await getSqliteStorageAsync(
50
+ new LibSQLSqliteAsyncDriver(getDbPath(filename)),
51
+ );
52
+
53
+ onTestFinished(() => {
54
+ storage.close();
55
+ });
56
+
57
+ return storage;
58
+ }
59
+
60
+ export function getDbPath(defaultDbPath?: string) {
61
+ const dbPath = defaultDbPath ?? join(tmpdir(), `test-${randomUUID()}.db`);
62
+
63
+ if (!defaultDbPath) {
64
+ onTestFinished(() => {
65
+ unlinkSync(dbPath);
66
+ });
67
+ }
68
+
69
+ return dbPath;
70
+ }
@@ -0,0 +1,70 @@
1
+ import { describe, test } from "vitest";
2
+ import { z } from "../exports";
3
+ import { co } from "../internal";
4
+
5
+ describe("CoValue and Zod schema compatibility", () => {
6
+ test("cannot use z.record with CoValue schemas as values", () => {
7
+ const Dog = co.map({
8
+ type: z.literal("dog"),
9
+ });
10
+
11
+ const Person = co.map({
12
+ // @ts-expect-error: cannot use z.record with a CoValue schema
13
+ // (z.record is not exported by jazz-tools)
14
+ pets: z.record(z.string(), Dog),
15
+ });
16
+ });
17
+
18
+ test("cannot use z.union with CoValue schemas as values", () => {
19
+ const Dog = co.map({
20
+ type: z.literal("dog"),
21
+ });
22
+
23
+ const Cat = co.map({
24
+ type: z.literal("cat"),
25
+ });
26
+
27
+ const Person = co.map({
28
+ // @ts-expect-error: cannot use z.union with a CoValue schema
29
+ pets: z.union([Dog, Cat]),
30
+ });
31
+ });
32
+
33
+ test("cannot use z.intersection with CoValue schemas as values", () => {
34
+ const Dog = co.map({
35
+ type: z.literal("dog"),
36
+ });
37
+
38
+ const Cat = co.map({
39
+ type: z.literal("cat"),
40
+ });
41
+
42
+ const Person = co.map({
43
+ // @ts-expect-error: cannot use z.intersection with a CoValue schema
44
+ // (z.intersection is not exported by jazz-tools)
45
+ pets: z.intersection(Dog, Cat),
46
+ });
47
+ });
48
+
49
+ test("cannot use z.array with CoValue schemas as values", () => {
50
+ const Dog = co.map({
51
+ type: z.literal("dog"),
52
+ });
53
+
54
+ const Person = co.map({
55
+ // @ts-expect-error: cannot use z.array with a CoValue schema
56
+ pets: z.array(Dog),
57
+ });
58
+ });
59
+
60
+ test("cannot use z.tuple with CoValue schemas as values", () => {
61
+ const Dog = co.map({
62
+ type: z.literal("dog"),
63
+ });
64
+
65
+ const Person = co.map({
66
+ // @ts-expect-error: cannot use z.tuple with a CoValue schema
67
+ pets: z.tuple([Dog]),
68
+ });
69
+ });
70
+ });