jazz-tools 0.9.14 → 0.9.15
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 +4 -4
- package/CHANGELOG.md +6 -0
- package/dist/testing.js +13 -2
- package/dist/testing.js.map +1 -1
- package/package.json +1 -1
- package/src/testing.ts +16 -1
- package/src/tests/testing.test.ts +26 -1
package/.turbo/turbo-build.log
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
|
2
|
-
> jazz-tools@0.9.
|
2
|
+
> jazz-tools@0.9.15 build /home/runner/work/jazz/jazz/packages/jazz-tools
|
3
3
|
> tsup
|
4
4
|
|
5
5
|
[34mCLI[39m Building entry: {"index.web":"src/index.web.ts","index.native":"src/index.native.ts","testing":"src/testing.ts"}
|
@@ -11,10 +11,10 @@
|
|
11
11
|
[34mESM[39m Build start
|
12
12
|
[32mESM[39m [1mdist/index.web.js [22m[32m1.15 KB[39m
|
13
13
|
[32mESM[39m [1mdist/index.native.js [22m[32m1.14 KB[39m
|
14
|
-
[32mESM[39m [1mdist/testing.js [22m[32m3.
|
14
|
+
[32mESM[39m [1mdist/testing.js [22m[32m3.43 KB[39m
|
15
15
|
[32mESM[39m [1mdist/chunk-ICWP2U63.js [22m[32m94.94 KB[39m
|
16
16
|
[32mESM[39m [1mdist/index.web.js.map [22m[32m273.00 B[39m
|
17
17
|
[32mESM[39m [1mdist/index.native.js.map [22m[32m283.00 B[39m
|
18
|
-
[32mESM[39m [1mdist/testing.js.map [22m[32m6.
|
18
|
+
[32mESM[39m [1mdist/testing.js.map [22m[32m6.94 KB[39m
|
19
19
|
[32mESM[39m [1mdist/chunk-ICWP2U63.js.map [22m[32m235.54 KB[39m
|
20
|
-
[32mESM[39m ⚡️ Build success in
|
20
|
+
[32mESM[39m ⚡️ Build success in 111ms
|
package/CHANGELOG.md
CHANGED
package/dist/testing.js
CHANGED
@@ -5,6 +5,7 @@ import {
|
|
5
5
|
} from "./chunk-ICWP2U63.js";
|
6
6
|
|
7
7
|
// src/testing.ts
|
8
|
+
import { LocalNode } from "cojson";
|
8
9
|
import { cojsonInternals } from "cojson";
|
9
10
|
import { PureJSCrypto } from "cojson/crypto";
|
10
11
|
var syncServer = { current: null };
|
@@ -36,14 +37,24 @@ async function createJazzTestAccount(options) {
|
|
36
37
|
syncServer.current.syncManager.addPeer(aPeer);
|
37
38
|
peers.push(bPeer);
|
38
39
|
}
|
39
|
-
const
|
40
|
+
const { node } = await LocalNode.withNewlyCreatedAccount({
|
40
41
|
creationProps: {
|
41
42
|
name: "Test Account",
|
42
43
|
...options?.creationProps
|
43
44
|
},
|
44
45
|
crypto: await TestJSCrypto.create(),
|
45
|
-
peersToLoadFrom: peers
|
46
|
+
peersToLoadFrom: peers,
|
47
|
+
migration: async (rawAccount, _node, creationProps) => {
|
48
|
+
const account2 = new AccountSchema({
|
49
|
+
fromRaw: rawAccount
|
50
|
+
});
|
51
|
+
if (options?.isCurrentActiveAccount) {
|
52
|
+
activeAccountContext.set(account2);
|
53
|
+
}
|
54
|
+
await account2.applyMigration?.(creationProps);
|
55
|
+
}
|
46
56
|
});
|
57
|
+
const account = AccountSchema.fromNode(node);
|
47
58
|
if (options?.isCurrentActiveAccount) {
|
48
59
|
activeAccountContext.set(account);
|
49
60
|
}
|
package/dist/testing.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/testing.ts"],"sourcesContent":["import { AgentSecret, CryptoProvider, LocalNode, Peer } from \"cojson\";\nimport { cojsonInternals } from \"cojson\";\nimport { PureJSCrypto } from \"cojson/crypto\";\nimport { Account, type AccountClass } from \"./exports.js\";\nimport { activeAccountContext } from \"./implementation/activeAccountContext.js\";\nimport {\n type AnonymousJazzAgent,\n type CoValueClass,\n createAnonymousJazzContext,\n} from \"./internal.js\";\n\nconst syncServer: { current: LocalNode | null } = { current: null };\n\ntype TestAccountSchema<Acc extends Account> = CoValueClass<Acc> & {\n fromNode: (typeof Account)[\"fromNode\"];\n create: (options: {\n creationProps: { name: string };\n initialAgentSecret?: AgentSecret;\n peersToLoadFrom?: Peer[];\n crypto: CryptoProvider;\n }) => Promise<Acc>;\n};\n\nclass TestJSCrypto extends PureJSCrypto {\n static async create() {\n if (\"navigator\" in globalThis && navigator.userAgent.includes(\"jsdom\")) {\n // Mocking crypto seal & encrypt to make it work with JSDom. Getting \"Error: Uint8Array expected\" there\n const crypto = new PureJSCrypto();\n\n crypto.seal = (options) =>\n `sealed_U${cojsonInternals.stableStringify(options.message)}` as any;\n crypto.unseal = (sealed) =>\n JSON.parse(sealed.substring(\"sealed_U\".length));\n crypto.encrypt = (message) =>\n `encrypted_U${cojsonInternals.stableStringify(message)}` as any;\n crypto.decryptRaw = (encrypted) =>\n encrypted.substring(\"encrypted_U\".length) as any;\n\n return crypto;\n }\n\n // For non-jsdom environments, we use the real crypto\n return new PureJSCrypto();\n }\n}\n\nexport async function createJazzTestAccount<Acc extends Account>(options?: {\n isCurrentActiveAccount?: boolean;\n AccountSchema?: CoValueClass<Acc>;\n creationProps?: Record<string, unknown>;\n}): Promise<Acc> {\n const AccountSchema = (options?.AccountSchema ??\n Account) as unknown as TestAccountSchema<Acc>;\n const peers = [];\n if (syncServer.current) {\n const [aPeer, bPeer] = cojsonInternals.connectedPeers(\n Math.random().toString(),\n Math.random().toString(),\n {\n peer1role: \"server\",\n peer2role: \"server\",\n },\n );\n syncServer.current.syncManager.addPeer(aPeer);\n peers.push(bPeer);\n }\n const
|
1
|
+
{"version":3,"sources":["../src/testing.ts"],"sourcesContent":["import { AgentSecret, CryptoProvider, LocalNode, Peer } from \"cojson\";\nimport { cojsonInternals } from \"cojson\";\nimport { PureJSCrypto } from \"cojson/crypto\";\nimport { Account, type AccountClass } from \"./exports.js\";\nimport { activeAccountContext } from \"./implementation/activeAccountContext.js\";\nimport {\n type AnonymousJazzAgent,\n type CoValueClass,\n createAnonymousJazzContext,\n} from \"./internal.js\";\n\nconst syncServer: { current: LocalNode | null } = { current: null };\n\ntype TestAccountSchema<Acc extends Account> = CoValueClass<Acc> & {\n fromNode: (typeof Account)[\"fromNode\"];\n create: (options: {\n creationProps: { name: string };\n initialAgentSecret?: AgentSecret;\n peersToLoadFrom?: Peer[];\n crypto: CryptoProvider;\n }) => Promise<Acc>;\n};\n\nclass TestJSCrypto extends PureJSCrypto {\n static async create() {\n if (\"navigator\" in globalThis && navigator.userAgent.includes(\"jsdom\")) {\n // Mocking crypto seal & encrypt to make it work with JSDom. Getting \"Error: Uint8Array expected\" there\n const crypto = new PureJSCrypto();\n\n crypto.seal = (options) =>\n `sealed_U${cojsonInternals.stableStringify(options.message)}` as any;\n crypto.unseal = (sealed) =>\n JSON.parse(sealed.substring(\"sealed_U\".length));\n crypto.encrypt = (message) =>\n `encrypted_U${cojsonInternals.stableStringify(message)}` as any;\n crypto.decryptRaw = (encrypted) =>\n encrypted.substring(\"encrypted_U\".length) as any;\n\n return crypto;\n }\n\n // For non-jsdom environments, we use the real crypto\n return new PureJSCrypto();\n }\n}\n\nexport async function createJazzTestAccount<Acc extends Account>(options?: {\n isCurrentActiveAccount?: boolean;\n AccountSchema?: CoValueClass<Acc>;\n creationProps?: Record<string, unknown>;\n}): Promise<Acc> {\n const AccountSchema = (options?.AccountSchema ??\n Account) as unknown as TestAccountSchema<Acc>;\n const peers = [];\n if (syncServer.current) {\n const [aPeer, bPeer] = cojsonInternals.connectedPeers(\n Math.random().toString(),\n Math.random().toString(),\n {\n peer1role: \"server\",\n peer2role: \"server\",\n },\n );\n syncServer.current.syncManager.addPeer(aPeer);\n peers.push(bPeer);\n }\n\n const { node } = await LocalNode.withNewlyCreatedAccount({\n creationProps: {\n name: \"Test Account\",\n ...options?.creationProps,\n },\n crypto: await TestJSCrypto.create(),\n peersToLoadFrom: peers,\n migration: async (rawAccount, _node, creationProps) => {\n const account = new AccountSchema({\n fromRaw: rawAccount,\n });\n\n if (options?.isCurrentActiveAccount) {\n activeAccountContext.set(account);\n }\n\n await account.applyMigration?.(creationProps);\n },\n });\n\n const account = AccountSchema.fromNode(node);\n\n if (options?.isCurrentActiveAccount) {\n activeAccountContext.set(account);\n }\n\n return account;\n}\n\nexport function setActiveAccount(account: Account) {\n activeAccountContext.set(account);\n}\n\nexport async function createJazzTestGuest() {\n const ctx = await createAnonymousJazzContext({\n crypto: await PureJSCrypto.create(),\n peersToLoadFrom: [],\n });\n\n return {\n guest: ctx.agent,\n };\n}\n\nexport function getJazzContextShape<Acc extends Account>(\n account: Acc | { guest: AnonymousJazzAgent },\n) {\n if (\"guest\" in account) {\n return {\n guest: account.guest,\n AccountSchema: Account,\n logOut: () => account.guest.node.gracefulShutdown(),\n done: () => account.guest.node.gracefulShutdown(),\n };\n }\n\n return {\n me: account,\n AccountSchema: account.constructor as AccountClass<Acc>,\n logOut: () => account._raw.core.node.gracefulShutdown(),\n done: () => account._raw.core.node.gracefulShutdown(),\n };\n}\n\nexport function linkAccounts(\n a: Account,\n b: Account,\n aRole: \"server\" | \"client\" = \"server\",\n bRole: \"server\" | \"client\" = \"server\",\n) {\n const [aPeer, bPeer] = cojsonInternals.connectedPeers(b.id, a.id, {\n peer1role: aRole,\n peer2role: bRole,\n });\n\n a._raw.core.node.syncManager.addPeer(aPeer);\n b._raw.core.node.syncManager.addPeer(bPeer);\n}\n\nexport async function setupJazzTestSync() {\n if (syncServer.current) {\n syncServer.current.gracefulShutdown();\n }\n\n const account = await Account.create({\n creationProps: {\n name: \"Test Account\",\n },\n crypto: await TestJSCrypto.create(),\n });\n\n syncServer.current = account._raw.core.node;\n\n return account;\n}\n"],"mappings":";;;;;;;AAAA,SAAsC,iBAAuB;AAC7D,SAAS,uBAAuB;AAChC,SAAS,oBAAoB;AAS7B,IAAM,aAA4C,EAAE,SAAS,KAAK;AAYlE,IAAM,eAAN,cAA2B,aAAa;AAAA,EACtC,aAAa,SAAS;AACpB,QAAI,eAAe,cAAc,UAAU,UAAU,SAAS,OAAO,GAAG;AAEtE,YAAM,SAAS,IAAI,aAAa;AAEhC,aAAO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CHANGED
package/src/testing.ts
CHANGED
@@ -64,14 +64,29 @@ export async function createJazzTestAccount<Acc extends Account>(options?: {
|
|
64
64
|
syncServer.current.syncManager.addPeer(aPeer);
|
65
65
|
peers.push(bPeer);
|
66
66
|
}
|
67
|
-
|
67
|
+
|
68
|
+
const { node } = await LocalNode.withNewlyCreatedAccount({
|
68
69
|
creationProps: {
|
69
70
|
name: "Test Account",
|
70
71
|
...options?.creationProps,
|
71
72
|
},
|
72
73
|
crypto: await TestJSCrypto.create(),
|
73
74
|
peersToLoadFrom: peers,
|
75
|
+
migration: async (rawAccount, _node, creationProps) => {
|
76
|
+
const account = new AccountSchema({
|
77
|
+
fromRaw: rawAccount,
|
78
|
+
});
|
79
|
+
|
80
|
+
if (options?.isCurrentActiveAccount) {
|
81
|
+
activeAccountContext.set(account);
|
82
|
+
}
|
83
|
+
|
84
|
+
await account.applyMigration?.(creationProps);
|
85
|
+
},
|
74
86
|
});
|
87
|
+
|
88
|
+
const account = AccountSchema.fromNode(node);
|
89
|
+
|
75
90
|
if (options?.isCurrentActiveAccount) {
|
76
91
|
activeAccountContext.set(account);
|
77
92
|
}
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { beforeEach, describe, expect, test } from "vitest";
|
2
|
-
import { CoMap, Group } from "../exports";
|
2
|
+
import { Account, CoMap, Group, co } from "../exports";
|
3
3
|
import { createJazzTestAccount, setupJazzTestSync } from "../testing";
|
4
4
|
|
5
5
|
describe("Jazz Test Sync", () => {
|
@@ -23,4 +23,29 @@ describe("Jazz Test Sync", () => {
|
|
23
23
|
expect(loadedMap).toBeDefined();
|
24
24
|
expect(loadedMap?._raw.get("test")).toBe("value");
|
25
25
|
});
|
26
|
+
|
27
|
+
test("correctly set the globalMe before starting the migration", async () => {
|
28
|
+
class MyRoot extends CoMap {
|
29
|
+
value = co.string;
|
30
|
+
}
|
31
|
+
|
32
|
+
class CustomAccount extends Account {
|
33
|
+
root = co.ref(MyRoot);
|
34
|
+
|
35
|
+
migrate() {
|
36
|
+
if (this.root === undefined) {
|
37
|
+
this.root = MyRoot.create({
|
38
|
+
value: "ok",
|
39
|
+
});
|
40
|
+
}
|
41
|
+
}
|
42
|
+
}
|
43
|
+
|
44
|
+
const account1 = await createJazzTestAccount({
|
45
|
+
AccountSchema: CustomAccount,
|
46
|
+
isCurrentActiveAccount: true,
|
47
|
+
});
|
48
|
+
|
49
|
+
expect(account1.root?.value).toBe("ok");
|
50
|
+
});
|
26
51
|
});
|