jazz-tools 0.13.30 → 0.14.0
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.
- package/.turbo/turbo-build.log +8 -8
- package/CHANGELOG.md +23 -3
- package/dist/auth/DemoAuth.d.ts.map +1 -1
- package/dist/auth/PassphraseAuth.d.ts +1 -3
- package/dist/auth/PassphraseAuth.d.ts.map +1 -1
- package/dist/{chunk-LMV6J7GN.js → chunk-2ASOGEYA.js} +3531 -3269
- package/dist/chunk-2ASOGEYA.js.map +1 -0
- package/dist/coValues/CoValueBase.d.ts +22 -0
- package/dist/coValues/CoValueBase.d.ts.map +1 -0
- package/dist/coValues/account.d.ts +12 -12
- package/dist/coValues/account.d.ts.map +1 -1
- package/dist/coValues/coFeed.d.ts +24 -25
- package/dist/coValues/coFeed.d.ts.map +1 -1
- package/dist/coValues/coList.d.ts +10 -13
- package/dist/coValues/coList.d.ts.map +1 -1
- package/dist/coValues/coMap.d.ts +32 -35
- package/dist/coValues/coMap.d.ts.map +1 -1
- package/dist/coValues/coPlainText.d.ts.map +1 -1
- package/dist/coValues/deepLoading.d.ts +28 -22
- package/dist/coValues/deepLoading.d.ts.map +1 -1
- package/dist/coValues/extensions/imageDef.d.ts +12 -11
- package/dist/coValues/extensions/imageDef.d.ts.map +1 -1
- package/dist/coValues/group.d.ts +5 -9
- package/dist/coValues/group.d.ts.map +1 -1
- package/dist/coValues/inbox.d.ts +2 -3
- package/dist/coValues/inbox.d.ts.map +1 -1
- package/dist/coValues/interfaces.d.ts +8 -34
- package/dist/coValues/interfaces.d.ts.map +1 -1
- package/dist/coValues/profile.d.ts +4 -14
- package/dist/coValues/profile.d.ts.map +1 -1
- package/dist/coValues/registeredSchemas.d.ts +1 -3
- package/dist/coValues/registeredSchemas.d.ts.map +1 -1
- package/dist/coValues/schemaUnion.d.ts +6 -6
- package/dist/exports.d.ts +12 -16
- package/dist/exports.d.ts.map +1 -1
- package/dist/implementation/ContextManager.d.ts +1 -1
- package/dist/implementation/ContextManager.d.ts.map +1 -1
- package/dist/implementation/activeAccountContext.d.ts +1 -1
- package/dist/implementation/activeAccountContext.d.ts.map +1 -1
- package/dist/implementation/createContext.d.ts +10 -10
- package/dist/implementation/createContext.d.ts.map +1 -1
- package/dist/implementation/invites.d.ts +6 -6
- package/dist/implementation/invites.d.ts.map +1 -1
- package/dist/implementation/refs.d.ts +2 -2
- package/dist/implementation/refs.d.ts.map +1 -1
- package/dist/implementation/schema.d.ts +21 -28
- package/dist/implementation/schema.d.ts.map +1 -1
- package/dist/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts +9 -0
- package/dist/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts.map +1 -0
- package/dist/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts +28 -0
- package/dist/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +65 -0
- package/dist/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +28 -0
- package/dist/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +24 -0
- package/dist/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +41 -0
- package/dist/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +35 -0
- package/dist/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +9 -0
- package/dist/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +20 -0
- package/dist/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +18 -0
- package/dist/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +24 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts +21 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts.map +1 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts +29 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts +29 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts.map +1 -0
- package/dist/implementation/zodSchema/unionUtils.d.ts +6 -0
- package/dist/implementation/zodSchema/unionUtils.d.ts.map +1 -0
- package/dist/implementation/zodSchema/zodCo.d.ts +35 -0
- package/dist/implementation/zodSchema/zodCo.d.ts.map +1 -0
- package/dist/implementation/zodSchema/zodSchema.d.ts +38 -0
- package/dist/implementation/zodSchema/zodSchema.d.ts.map +1 -0
- package/dist/index.js +295 -10
- package/dist/index.js.map +1 -1
- package/dist/internal.d.ts +34 -0
- package/dist/internal.d.ts.map +1 -1
- package/dist/subscribe/SubscriptionScope.d.ts +2 -2
- package/dist/subscribe/SubscriptionScope.d.ts.map +1 -1
- package/dist/subscribe/utils.d.ts +2 -2
- package/dist/subscribe/utils.d.ts.map +1 -1
- package/dist/testing.d.ts +10 -8
- package/dist/testing.d.ts.map +1 -1
- package/dist/testing.js +1 -1
- package/dist/testing.js.map +1 -1
- package/dist/tests/utils.d.ts +6 -2
- package/dist/tests/utils.d.ts.map +1 -1
- package/dist/types.d.ts +1 -7
- package/dist/types.d.ts.map +1 -1
- package/package.json +3 -2
- package/src/auth/DemoAuth.ts +1 -2
- package/src/auth/PassphraseAuth.ts +1 -1
- package/src/coValues/CoValueBase.ts +83 -0
- package/src/coValues/account.ts +53 -43
- package/src/coValues/coFeed.ts +65 -83
- package/src/coValues/coList.ts +28 -21
- package/src/coValues/coMap.ts +54 -38
- package/src/coValues/coPlainText.ts +4 -1
- package/src/coValues/deepLoading.ts +46 -36
- package/src/coValues/extensions/imageDef.ts +21 -19
- package/src/coValues/group.ts +37 -38
- package/src/coValues/inbox.ts +24 -11
- package/src/coValues/interfaces.ts +29 -93
- package/src/coValues/profile.ts +12 -13
- package/src/coValues/registeredSchemas.ts +1 -3
- package/src/coValues/schemaUnion.ts +6 -6
- package/src/exports.ts +47 -25
- package/src/implementation/activeAccountContext.ts +1 -1
- package/src/implementation/createContext.ts +39 -24
- package/src/implementation/invites.ts +15 -12
- package/src/implementation/refs.ts +6 -4
- package/src/implementation/schema.ts +22 -34
- package/src/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts +101 -0
- package/src/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.ts +191 -0
- package/src/implementation/zodSchema/schemaTypes/AccountSchema.ts +102 -0
- package/src/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +70 -0
- package/src/implementation/zodSchema/schemaTypes/CoListSchema.ts +59 -0
- package/src/implementation/zodSchema/schemaTypes/CoMapSchema.ts +126 -0
- package/src/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +98 -0
- package/src/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +9 -0
- package/src/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +27 -0
- package/src/implementation/zodSchema/schemaTypes/RichTextSchema.ts +25 -0
- package/src/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +61 -0
- package/src/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.ts +77 -0
- package/src/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.ts +90 -0
- package/src/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.ts +103 -0
- package/src/implementation/zodSchema/unionUtils.ts +139 -0
- package/src/implementation/zodSchema/zodCo.ts +409 -0
- package/src/implementation/zodSchema/zodSchema.ts +116 -0
- package/src/internal.ts +38 -0
- package/src/subscribe/SubscriptionScope.ts +30 -3
- package/src/subscribe/utils.ts +7 -2
- package/src/testing.ts +14 -16
- package/src/tests/ContextManager.test.ts +73 -47
- package/src/tests/DemoAuth.test.ts +1 -1
- package/src/tests/account.test.ts +6 -9
- package/src/tests/coFeed.test.ts +102 -63
- package/src/tests/coList.test.ts +82 -95
- package/src/tests/coMap.record.test.ts +53 -87
- package/src/tests/coMap.test.ts +297 -312
- package/src/tests/coPlainText.test.ts +19 -39
- package/src/tests/createContext.test.ts +33 -15
- package/src/tests/deepLoading.test.ts +397 -131
- package/src/tests/groupsAndAccounts.test.ts +81 -72
- package/src/tests/imageDef.test.ts +22 -13
- package/src/tests/inbox.test.ts +36 -29
- package/src/tests/load.test.ts +10 -10
- package/src/tests/patterns/requestToJoin.test.ts +31 -31
- package/src/tests/schema.test.ts +37 -38
- package/src/tests/schemaUnion.test.ts +54 -64
- package/src/tests/subscribe.test.ts +118 -116
- package/src/tests/testing.test.ts +33 -33
- package/src/tests/utils.ts +3 -2
- package/src/types.ts +1 -8
- package/dist/chunk-LMV6J7GN.js.map +0 -1
package/src/testing.ts
CHANGED
@@ -4,17 +4,14 @@ import { PureJSCrypto } from "cojson/dist/crypto/PureJSCrypto";
|
|
4
4
|
import {
|
5
5
|
Account,
|
6
6
|
AccountClass,
|
7
|
+
type AnonymousJazzAgent,
|
7
8
|
AuthCredentials,
|
8
|
-
|
9
|
-
|
10
|
-
import {
|
9
|
+
type CoValueClass,
|
10
|
+
CoValueFromRaw,
|
11
11
|
JazzContextManager,
|
12
|
+
JazzContextManagerAuthProps,
|
12
13
|
JazzContextManagerBaseProps,
|
13
|
-
|
14
|
-
import { activeAccountContext } from "./implementation/activeAccountContext.js";
|
15
|
-
import {
|
16
|
-
type AnonymousJazzAgent,
|
17
|
-
type CoValueClass,
|
14
|
+
activeAccountContext,
|
18
15
|
createAnonymousJazzContext,
|
19
16
|
createJazzContext,
|
20
17
|
randomSessionProvider,
|
@@ -76,13 +73,13 @@ export function getPeerConnectedToTestSyncServer() {
|
|
76
73
|
const SecretSeedMap = new Map<string, Uint8Array>();
|
77
74
|
let isMigrationActive = false;
|
78
75
|
|
79
|
-
export async function createJazzTestAccount<
|
76
|
+
export async function createJazzTestAccount<A extends Account>(options?: {
|
80
77
|
isCurrentActiveAccount?: boolean;
|
81
|
-
AccountSchema?: CoValueClass<
|
78
|
+
AccountSchema?: CoValueClass<A>;
|
82
79
|
creationProps?: Record<string, unknown>;
|
83
|
-
}): Promise<
|
80
|
+
}): Promise<A> {
|
84
81
|
const AccountSchema = (options?.AccountSchema ??
|
85
|
-
Account) as unknown as TestAccountSchema<
|
82
|
+
Account) as unknown as TestAccountSchema<Account>;
|
86
83
|
const peers = [];
|
87
84
|
if (syncServer.current) {
|
88
85
|
peers.push(getPeerConnectedToTestSyncServer());
|
@@ -134,7 +131,7 @@ export async function createJazzTestAccount<Acc extends Account>(options?: {
|
|
134
131
|
activeAccountContext.set(account);
|
135
132
|
}
|
136
133
|
|
137
|
-
return account;
|
134
|
+
return account as A;
|
138
135
|
}
|
139
136
|
|
140
137
|
export function setActiveAccount(account: Account) {
|
@@ -155,7 +152,7 @@ export async function createJazzTestGuest() {
|
|
155
152
|
export type TestJazzContextManagerProps<Acc extends Account> =
|
156
153
|
JazzContextManagerBaseProps<Acc> & {
|
157
154
|
defaultProfileName?: string;
|
158
|
-
AccountSchema?: AccountClass<Acc>;
|
155
|
+
AccountSchema?: AccountClass<Acc> & CoValueFromRaw<Acc>;
|
159
156
|
isAuthenticated?: boolean;
|
160
157
|
};
|
161
158
|
|
@@ -194,7 +191,8 @@ export class TestJazzContextManager<
|
|
194
191
|
|
195
192
|
context.updateContext(
|
196
193
|
{
|
197
|
-
AccountSchema: account.constructor as AccountClass<Acc
|
194
|
+
AccountSchema: account.constructor as AccountClass<Acc> &
|
195
|
+
CoValueFromRaw<Acc>,
|
198
196
|
...props,
|
199
197
|
},
|
200
198
|
{
|
@@ -247,7 +245,7 @@ export class TestJazzContextManager<
|
|
247
245
|
);
|
248
246
|
}
|
249
247
|
|
250
|
-
const context = await createJazzContext
|
248
|
+
const context = await createJazzContext({
|
251
249
|
credentials: authProps?.credentials,
|
252
250
|
defaultProfileName: props.defaultProfileName,
|
253
251
|
newAccountProps: authProps?.newAccountProps,
|
@@ -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
|
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
|
-
|
230
|
-
value
|
231
|
-
}
|
237
|
+
const AccountRoot = co.map({
|
238
|
+
value: z.string(),
|
239
|
+
});
|
232
240
|
|
233
241
|
let lastRootId: string | undefined;
|
234
242
|
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
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 =
|
243
|
-
}
|
244
|
-
|
245
|
-
const customManager = new TestJazzContextManager<
|
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<
|
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
|
-
|
275
|
-
value
|
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
|
-
|
282
|
-
|
283
|
-
|
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
|
303
|
+
const { root } = await account.ensureLoaded({
|
304
|
+
resolve: { root: true },
|
305
|
+
});
|
288
306
|
|
289
307
|
root.value = 2;
|
290
308
|
}
|
291
|
-
}
|
292
|
-
|
293
|
-
|
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<
|
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
|
-
|
319
|
-
value
|
320
|
-
transferredRoot
|
321
|
-
|
322
|
-
|
323
|
-
|
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
|
-
|
327
|
-
|
328
|
-
|
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<
|
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 "../
|
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,
|
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
|
-
|
18
|
-
name
|
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
|
-
|
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 () => {
|
package/src/tests/coFeed.test.ts
CHANGED
@@ -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
|
-
|
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
|
-
|
71
|
-
|
72
|
-
|
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
|
-
|
102
|
-
|
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
|
-
|
132
|
-
|
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(
|
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(
|
147
|
-
|
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
|
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?.
|
163
|
-
).toEqual(
|
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(
|
169
|
-
|
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(
|
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]?.
|
183
|
-
otherNestedStream
|
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?.
|
187
|
-
).toEqual(
|
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(
|
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(
|
232
|
-
|
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
|
251
|
+
update3.perAccount[me.id]!.value!.perAccount[me.id]!.value!.push("bread");
|
235
252
|
|
236
253
|
const update4 = (await queue.next()).value;
|
237
|
-
expect(
|
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(
|
252
|
-
|
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(
|
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(
|
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(
|
297
|
-
|
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
|
334
|
+
update3.perAccount[me.id]!.value!.perAccount[me.id]!.value!.push("bread");
|
300
335
|
|
301
336
|
const update4 = (await queue.next()).value;
|
302
|
-
expect(
|
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
|
-
|
653
|
+
const TestStream = co.feed(z.string());
|
615
654
|
|
616
655
|
const { clientNode, serverNode, clientAccount } = await setupTwoNodes();
|
617
656
|
|