jazz-tools 0.14.5 → 0.14.6

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 (30) hide show
  1. package/.turbo/turbo-build.log +5 -5
  2. package/CHANGELOG.md +7 -0
  3. package/dist/{chunk-52PJ4QZ3.js → chunk-E2B2W453.js} +10 -1
  4. package/dist/{chunk-52PJ4QZ3.js.map → chunk-E2B2W453.js.map} +1 -1
  5. package/dist/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +3 -1
  6. package/dist/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
  7. package/dist/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +3 -1
  8. package/dist/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
  9. package/dist/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +5 -3
  10. package/dist/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
  11. package/dist/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +19 -3
  12. package/dist/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
  13. package/dist/implementation/zodSchema/zodCo.d.ts.map +1 -1
  14. package/dist/index.js +1 -1
  15. package/dist/testing.js +1 -1
  16. package/dist/tests/coFeed.test-d.d.ts +2 -0
  17. package/dist/tests/coFeed.test-d.d.ts.map +1 -0
  18. package/dist/tests/coList.test-d.d.ts +2 -0
  19. package/dist/tests/coList.test-d.d.ts.map +1 -0
  20. package/package.json +1 -1
  21. package/src/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +7 -1
  22. package/src/implementation/zodSchema/schemaTypes/CoListSchema.ts +7 -1
  23. package/src/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +10 -3
  24. package/src/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +37 -3
  25. package/src/implementation/zodSchema/zodCo.ts +23 -4
  26. package/src/tests/coFeed.test-d.ts +256 -0
  27. package/src/tests/coFeed.test.ts +41 -18
  28. package/src/tests/coList.test-d.ts +245 -0
  29. package/src/tests/coMap.record.test-d.ts +33 -0
  30. package/src/tests/coMap.test-d.ts +1 -11
@@ -2,18 +2,14 @@ import { WasmCrypto } from "cojson/crypto/WasmCrypto";
2
2
  import { describe, expect, expectTypeOf, test } from "vitest";
3
3
  import {
4
4
  Account,
5
- CoFeed,
6
5
  FileStream,
7
6
  Group,
8
- ID,
9
7
  co,
10
- coField,
11
8
  cojsonInternals,
12
9
  isControlledAccount,
13
10
  z,
14
11
  } from "../index.js";
15
12
  import {
16
- AnyCoFeedSchema,
17
13
  Loaded,
18
14
  createJazzContextFromExistingCredentials,
19
15
  randomSessionProvider,
@@ -350,23 +346,50 @@ describe("Simple FileStream operations", async () => {
350
346
 
351
347
  const stream = FileStream.create({ owner: me });
352
348
 
353
- test("Construction", () => {
354
- expect(stream.getChunks()).toBe(undefined);
349
+ describe("FileStream", () => {
350
+ test("Construction", () => {
351
+ expect(stream.getChunks()).toBe(undefined);
352
+ });
353
+
354
+ test("Mutation", () => {
355
+ stream.start({ mimeType: "text/plain" });
356
+ stream.push(new Uint8Array([1, 2, 3]));
357
+ stream.push(new Uint8Array([4, 5, 6]));
358
+ stream.end();
359
+
360
+ const chunks = stream.getChunks();
361
+ expect(chunks?.mimeType).toBe("text/plain");
362
+ expect(chunks?.chunks).toEqual([
363
+ new Uint8Array([1, 2, 3]),
364
+ new Uint8Array([4, 5, 6]),
365
+ ]);
366
+ expect(chunks?.finished).toBe(true);
367
+ });
355
368
  });
356
369
 
357
- test("Mutation", () => {
358
- stream.start({ mimeType: "text/plain" });
359
- stream.push(new Uint8Array([1, 2, 3]));
360
- stream.push(new Uint8Array([4, 5, 6]));
361
- stream.end();
370
+ describe("co.fileStream", () => {
371
+ const fs = co.fileStream().create({ owner: me });
362
372
 
363
- const chunks = stream.getChunks();
364
- expect(chunks?.mimeType).toBe("text/plain");
365
- expect(chunks?.chunks).toEqual([
366
- new Uint8Array([1, 2, 3]),
367
- new Uint8Array([4, 5, 6]),
368
- ]);
369
- expect(chunks?.finished).toBe(true);
373
+ test("Construction", () => {
374
+ expect(fs.getChunks()).toBe(undefined);
375
+ });
376
+
377
+ test("Type compatibility", () => {
378
+ // Check base functionality works
379
+ expectTypeOf(co.fileStream()).toHaveProperty("create");
380
+
381
+ // We can acknowledge the type error exists
382
+ // This is a runtime test that verifies that despite the TypeScript error,
383
+ // the functionality still works as expected
384
+ expect(typeof fs.getChunks).toBe("function");
385
+ });
386
+
387
+ test("Mutation", () => {
388
+ fs.start({ mimeType: "text/plain" });
389
+ fs.push(new Uint8Array([1, 2, 3]));
390
+ fs.push(new Uint8Array([4, 5, 6]));
391
+ fs.end();
392
+ });
370
393
  });
371
394
  });
372
395
 
@@ -0,0 +1,245 @@
1
+ import { assert, describe, expectTypeOf, test } from "vitest";
2
+ import { Group, co, z } from "../exports.js";
3
+ import { Account } from "../index.js";
4
+ import { Loaded } from "../internal.js";
5
+
6
+ describe("CoList", () => {
7
+ describe("init", () => {
8
+ test("create a CoList with basic property access", () => {
9
+ const StringList = co.list(z.string());
10
+
11
+ const list = StringList.create(["a", "b", "c"]);
12
+
13
+ type ExpectedType = string[];
14
+
15
+ function matches(value: ExpectedType) {
16
+ return value;
17
+ }
18
+
19
+ matches(list);
20
+ });
21
+
22
+ test("has the _owner property", () => {
23
+ const StringList = co.list(z.string());
24
+
25
+ const list = StringList.create(["a", "b", "c"], Account.getMe());
26
+
27
+ expectTypeOf(list._owner).toEqualTypeOf<Account | Group>();
28
+ });
29
+
30
+ test("CoList with reference", () => {
31
+ const Dog = co.map({
32
+ name: z.string(),
33
+ breed: z.string(),
34
+ });
35
+
36
+ const DogList = co.list(Dog);
37
+
38
+ const list = DogList.create([
39
+ Dog.create({ name: "Rex", breed: "Labrador" }),
40
+ Dog.create({ name: "Fido", breed: "Poodle" }),
41
+ // @ts-expect-error - undefined is not a valid argument
42
+ undefined,
43
+ ]);
44
+
45
+ type ExpectedType = Loaded<typeof Dog>[];
46
+
47
+ function matches(value: ExpectedType) {
48
+ return value;
49
+ }
50
+
51
+ matches(list);
52
+ });
53
+
54
+ test("CoList with optional reference", () => {
55
+ const Dog = co.map({
56
+ name: z.string(),
57
+ breed: z.string(),
58
+ });
59
+
60
+ const DogList = co.list(Dog.optional());
61
+
62
+ const list = DogList.create([
63
+ Dog.create({ name: "Rex", breed: "Labrador" }),
64
+ undefined,
65
+ ]);
66
+
67
+ type ExpectedType = (Loaded<typeof Dog> | undefined)[];
68
+
69
+ function matches(value: ExpectedType) {
70
+ return value;
71
+ }
72
+
73
+ matches(list);
74
+ });
75
+
76
+ test("CoList create with partially loaded, reference and optional", () => {
77
+ const Dog = co.map({
78
+ name: z.string(),
79
+ breed: co.map({ type: z.literal("labrador"), value: z.string() }),
80
+ });
81
+ type Dog = co.loaded<typeof Dog>;
82
+
83
+ const DogList = co.list(Dog.optional());
84
+
85
+ const dog = Dog.create({
86
+ name: "Rex",
87
+ breed: Dog.def.shape.breed.create({
88
+ type: "labrador",
89
+ value: "Labrador",
90
+ }),
91
+ }) as Dog;
92
+
93
+ const list = DogList.create([dog, undefined]);
94
+
95
+ type ExpectedType = (Loaded<typeof Dog> | undefined)[];
96
+
97
+ function matches(value: ExpectedType) {
98
+ return value;
99
+ }
100
+
101
+ matches(list);
102
+ });
103
+
104
+ test("CoList with nested lists", () => {
105
+ const NestedList = co.list(co.list(z.string()));
106
+
107
+ const list = NestedList.create([
108
+ co.list(z.string()).create(["a", "b"]),
109
+ co.list(z.string()).create(["c", "d"]),
110
+ ]);
111
+
112
+ type ExpectedType = string[][];
113
+
114
+ function matches(value: ExpectedType) {
115
+ return value;
116
+ }
117
+
118
+ matches(list);
119
+ });
120
+
121
+ test("CoList with enum type", () => {
122
+ const EnumList = co.list(z.enum(["a", "b", "c"]));
123
+
124
+ const list = EnumList.create(["a", "b", "c"]);
125
+
126
+ type ExpectedType = ("a" | "b" | "c")[];
127
+
128
+ function matches(value: ExpectedType) {
129
+ return value;
130
+ }
131
+
132
+ matches(list);
133
+ });
134
+ });
135
+
136
+ describe("CoList resolution", () => {
137
+ test("loading a list with deep resolve", async () => {
138
+ const Dog = co.map({
139
+ name: z.string(),
140
+ breed: z.string(),
141
+ });
142
+
143
+ const DogList = co.list(Dog);
144
+
145
+ const list = DogList.create([
146
+ Dog.create({ name: "Rex", breed: "Labrador" }),
147
+ Dog.create({ name: "Fido", breed: "Poodle" }),
148
+ ]);
149
+
150
+ const loadedList = await DogList.load(list.id, {
151
+ resolve: {
152
+ $each: true,
153
+ },
154
+ });
155
+
156
+ type ExpectedType = Loaded<typeof Dog>[] | null;
157
+
158
+ function matches(value: ExpectedType) {
159
+ return value;
160
+ }
161
+
162
+ matches(loadedList);
163
+
164
+ assert(loadedList);
165
+ const firstDog = loadedList[0];
166
+ assert(firstDog);
167
+ expectTypeOf(firstDog.name).toEqualTypeOf<string>();
168
+ });
169
+
170
+ test("loading a list with $onError", async () => {
171
+ const Dog = co.map({
172
+ name: z.string(),
173
+ breed: z.string(),
174
+ });
175
+
176
+ const DogList = co.list(Dog);
177
+
178
+ const list = DogList.create([
179
+ Dog.create({ name: "Rex", breed: "Labrador" }),
180
+ Dog.create({ name: "Fido", breed: "Poodle" }),
181
+ ]);
182
+
183
+ const loadedList = await DogList.load(list.id, {
184
+ resolve: {
185
+ $each: { $onError: null },
186
+ },
187
+ });
188
+
189
+ type ExpectedType =
190
+ | (
191
+ | (Loaded<typeof Dog> & {
192
+ $onError: never;
193
+ })
194
+ | null
195
+ )[]
196
+ | null;
197
+
198
+ function matches(value: ExpectedType) {
199
+ return value;
200
+ }
201
+
202
+ matches(loadedList);
203
+ });
204
+
205
+ test("loading a nested list with deep resolve", async () => {
206
+ const Dog = co.map({
207
+ name: z.string(),
208
+ breed: z.string(),
209
+ });
210
+
211
+ const DogList = co.list(Dog);
212
+ const NestedList = co.list(DogList);
213
+
214
+ const list = NestedList.create([
215
+ DogList.create([
216
+ Dog.create({ name: "Rex", breed: "Labrador" }),
217
+ Dog.create({ name: "Fido", breed: "Poodle" }),
218
+ ]),
219
+ ]);
220
+
221
+ const loadedList = await NestedList.load(list.id, {
222
+ resolve: {
223
+ $each: {
224
+ $each: true,
225
+ },
226
+ },
227
+ });
228
+
229
+ type ExpectedType = Loaded<typeof Dog>[][] | null;
230
+
231
+ function matches(value: ExpectedType) {
232
+ return value;
233
+ }
234
+
235
+ matches(loadedList);
236
+
237
+ assert(loadedList);
238
+ const firstList = loadedList[0];
239
+ assert(firstList);
240
+ const firstDog = firstList[0];
241
+ assert(firstDog);
242
+ expectTypeOf(firstDog.name).toEqualTypeOf<string>();
243
+ });
244
+ });
245
+ });
@@ -79,6 +79,39 @@ describe("CoMap.Record", () => {
79
79
 
80
80
  matches(person);
81
81
  });
82
+
83
+ test("Record create with partially loaded, reference and optional", () => {
84
+ const Dog = co.map({
85
+ name: z.string(),
86
+ breed: co.map({ type: z.literal("labrador"), value: z.string() }),
87
+ });
88
+ type Dog = co.loaded<typeof Dog>;
89
+
90
+ const DogRecord = co.record(z.string(), Dog.optional());
91
+
92
+ const dog = Dog.create({
93
+ name: "Rex",
94
+ breed: Dog.def.shape.breed.create({
95
+ type: "labrador",
96
+ value: "Labrador",
97
+ }),
98
+ }) as Dog;
99
+
100
+ const record = DogRecord.create({
101
+ pet1: dog,
102
+ pet2: undefined,
103
+ });
104
+
105
+ type ExpectedType = {
106
+ [key: string]: Loaded<typeof Dog> | undefined;
107
+ };
108
+
109
+ function matches(value: ExpectedType) {
110
+ return value;
111
+ }
112
+
113
+ matches(record);
114
+ });
82
115
  });
83
116
 
84
117
  describe("Record resolution", () => {
@@ -1,13 +1,7 @@
1
1
  import { assert, describe, expectTypeOf, test } from "vitest";
2
2
  import { Group, co, z } from "../exports.js";
3
3
  import { Account } from "../index.js";
4
- import {
5
- CoListSchema,
6
- CoMapInitZod,
7
- Loaded,
8
- optionalKeys,
9
- requiredKeys,
10
- } from "../internal.js";
4
+ import { CoListSchema, Loaded } from "../internal.js";
11
5
 
12
6
  describe("CoMap", async () => {
13
7
  describe("init", () => {
@@ -143,10 +137,6 @@ describe("CoMap", async () => {
143
137
  }),
144
138
  }) as Dog;
145
139
 
146
- type R = requiredKeys<typeof Person.def.shape>;
147
- type O = optionalKeys<typeof Person.def.shape>;
148
- type I = CoMapInitZod<typeof Person.def.shape>;
149
-
150
140
  const person = Person.create({
151
141
  name: "John",
152
142
  age: 20,