jazz-tools 0.9.13 → 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.
@@ -1,5 +1,5 @@
1
1
 
2
- > jazz-tools@0.9.13 build /home/runner/work/jazz/jazz/packages/jazz-tools
2
+ > jazz-tools@0.9.15 build /home/runner/work/jazz/jazz/packages/jazz-tools
3
3
  > tsup
4
4
 
5
5
  CLI Building entry: {"index.web":"src/index.web.ts","index.native":"src/index.native.ts","testing":"src/testing.ts"}
@@ -11,10 +11,10 @@
11
11
  ESM Build start
12
12
  ESM dist/index.web.js 1.15 KB
13
13
  ESM dist/index.native.js 1.14 KB
14
- ESM dist/testing.js 2.30 KB
14
+ ESM dist/testing.js 3.43 KB
15
15
  ESM dist/chunk-ICWP2U63.js 94.94 KB
16
16
  ESM dist/index.web.js.map 273.00 B
17
17
  ESM dist/index.native.js.map 283.00 B
18
- ESM dist/testing.js.map 4.89 KB
18
+ ESM dist/testing.js.map 6.94 KB
19
19
  ESM dist/chunk-ICWP2U63.js.map 235.54 KB
20
- ESM ⚡️ Build success in 89ms
20
+ ESM ⚡️ Build success in 111ms
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # jazz-tools
2
2
 
3
+ ## 0.9.15
4
+
5
+ ### Patch Changes
6
+
7
+ - 7491711: Testing: correctly set the globalMe before the migration when calling createJazzTestAccount
8
+
9
+ ## 0.9.14
10
+
11
+ ### Patch Changes
12
+
13
+ - 3df93cc: Add API to setup a test sync in the test environment
14
+
3
15
  ## 0.9.13
4
16
 
5
17
  ### Patch Changes
package/dist/testing.js CHANGED
@@ -5,8 +5,10 @@ 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";
11
+ var syncServer = { current: null };
10
12
  var TestJSCrypto = class extends PureJSCrypto {
11
13
  static async create() {
12
14
  if ("navigator" in globalThis && navigator.userAgent.includes("jsdom")) {
@@ -22,12 +24,37 @@ var TestJSCrypto = class extends PureJSCrypto {
22
24
  };
23
25
  async function createJazzTestAccount(options) {
24
26
  const AccountSchema = options?.AccountSchema ?? Account;
25
- const account = await AccountSchema.create({
27
+ const peers = [];
28
+ if (syncServer.current) {
29
+ const [aPeer, bPeer] = cojsonInternals.connectedPeers(
30
+ Math.random().toString(),
31
+ Math.random().toString(),
32
+ {
33
+ peer1role: "server",
34
+ peer2role: "server"
35
+ }
36
+ );
37
+ syncServer.current.syncManager.addPeer(aPeer);
38
+ peers.push(bPeer);
39
+ }
40
+ const { node } = await LocalNode.withNewlyCreatedAccount({
26
41
  creationProps: {
27
- name: "Test Account"
42
+ name: "Test Account",
43
+ ...options?.creationProps
28
44
  },
29
- crypto: await TestJSCrypto.create()
45
+ crypto: await TestJSCrypto.create(),
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
+ }
30
56
  });
57
+ const account = AccountSchema.fromNode(node);
31
58
  if (options?.isCurrentActiveAccount) {
32
59
  activeAccountContext.set(account);
33
60
  }
@@ -69,11 +96,25 @@ function linkAccounts(a, b, aRole = "server", bRole = "server") {
69
96
  a._raw.core.node.syncManager.addPeer(aPeer);
70
97
  b._raw.core.node.syncManager.addPeer(bPeer);
71
98
  }
99
+ async function setupJazzTestSync() {
100
+ if (syncServer.current) {
101
+ syncServer.current.gracefulShutdown();
102
+ }
103
+ const account = await Account.create({
104
+ creationProps: {
105
+ name: "Test Account"
106
+ },
107
+ crypto: await TestJSCrypto.create()
108
+ });
109
+ syncServer.current = account._raw.core.node;
110
+ return account;
111
+ }
72
112
  export {
73
113
  createJazzTestAccount,
74
114
  createJazzTestGuest,
75
115
  getJazzContextShape,
76
116
  linkAccounts,
77
- setActiveAccount
117
+ setActiveAccount,
118
+ setupJazzTestSync
78
119
  };
79
120
  //# sourceMappingURL=testing.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/testing.ts"],"sourcesContent":["import { AgentSecret, CryptoProvider, 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\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}): Promise<Acc> {\n const AccountSchema = (options?.AccountSchema ??\n Account) as unknown as TestAccountSchema<Acc>;\n const account = await AccountSchema.create({\n creationProps: {\n name: \"Test Account\",\n },\n crypto: await TestJSCrypto.create(),\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"],"mappings":";;;;;;;AACA,SAAS,uBAAuB;AAChC,SAAS,oBAAoB;AAmB7B,IAAM,eAAN,cAA2B,aAAa;AAAA,EACtC,aAAa,SAAS;AACpB,QAAI,eAAe,cAAc,UAAU,UAAU,SAAS,OAAO,GAAG;AAEtE,YAAM,SAAS,IAAI,aAAa;AAEhC,aAAO,OAAO,CAAC,YACb,WAAW,gBAAgB,gBAAgB,QAAQ,OAAO,CAAC;AAC7D,aAAO,SAAS,CAAC,WACf,KAAK,MAAM,OAAO,UAAU,WAAW,MAAM,CAAC;AAChD,aAAO,UAAU,CAAC,YAChB,cAAc,gBAAgB,gBAAgB,OAAO,CAAC;AACxD,aAAO,aAAa,CAAC,cACnB,UAAU,UAAU,cAAc,M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KAAK;AAC1C,IAAE,KAAK,KAAK,KAAK,YAAY,QAAQ,KAAK;AAC5C;","names":[]}
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,OAAO,CAAC,YACb,WAAW,gBAAgB,gBAAgB,QAAQ,OAAO,CAAC;AAC7D,aAAO,SAAS,CAAC,WACf,KAAK,MAAM,OAAO,UAAU,WAAW,MAAM,CAAC;AAChD,aAAO,UAAU,CAAC,YAChB,cAAc,gBAAgB,gBAAgB,OAAO,CAAC;AACxD,aAAO,aAAa,CAAC,cACnB,UAAU,UAAU,cAAc,MAAM;AAE1C,aAAO;AAAA,IACT;AAGA,WAAO,IAAI,aAAa;AAAA,EAC1B;AACF;AAEA,eAAsB,sBAA2C,SAIhD;AACf,QAAM,gBAAiB,SAAS,iBAC9B;AACF,QAAM,QAAQ,CAAC;AACf,MAAI,WAAW,SAAS;AACtB,UAAM,CAAC,OAAO,KAAK,IAAI,gBAAgB;AAAA,MACrC,KAAK,OAAO,EAAE,SAAS;AAAA,MACvB,KAAK,OAAO,EAAE,SAAS;AAAA,MACvB;AAAA,QACE,WAAW;AAAA,QACX,WAAW;AAAA,MACb;AAAA,IACF;AACA,eAAW,QAAQ,YAAY,QAAQ,KAAK;AAC5C,UAAM,KAAK,KAAK;AAAA,EAClB;AAEA,QAAM,EAAE,KAAK,IAAI,MAAM,UAAU,wBAAwB;AAAA,IACvD,eAAe;AAAA,MACb,MAAM;AAAA,MACN,GAAG,SAAS;AAAA,IACd;AAAA,IACA,QAAQ,MAAM,aAAa,OAAO;AAAA,IA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K,KAAK,YAAY,QAAQ,KAAK;AAC1C,IAAE,KAAK,KAAK,KAAK,YAAY,QAAQ,KAAK;AAC5C;AAEA,eAAsB,oBAAoB;AACxC,MAAI,WAAW,SAAS;AACtB,eAAW,QAAQ,iBAAiB;AAAA,EACtC;AAEA,QAAM,UAAU,MAAM,QAAQ,OAAO;AAAA,IACnC,eAAe;AAAA,MACb,MAAM;AAAA,IACR;AAAA,IACA,QAAQ,MAAM,aAAa,OAAO;AAAA,EACpC,CAAC;AAED,aAAW,UAAU,QAAQ,KAAK,KAAK;AAEvC,SAAO;AACT;","names":["account"]}
package/package.json CHANGED
@@ -23,7 +23,7 @@
23
23
  },
24
24
  "type": "module",
25
25
  "license": "MIT",
26
- "version": "0.9.13",
26
+ "version": "0.9.15",
27
27
  "dependencies": {
28
28
  "cojson": "0.9.13"
29
29
  },
package/src/testing.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AgentSecret, CryptoProvider, Peer } from "cojson";
1
+ import { AgentSecret, CryptoProvider, LocalNode, Peer } from "cojson";
2
2
  import { cojsonInternals } from "cojson";
3
3
  import { PureJSCrypto } from "cojson/crypto";
4
4
  import { Account, type AccountClass } from "./exports.js";
@@ -9,6 +9,8 @@ import {
9
9
  createAnonymousJazzContext,
10
10
  } from "./internal.js";
11
11
 
12
+ const syncServer: { current: LocalNode | null } = { current: null };
13
+
12
14
  type TestAccountSchema<Acc extends Account> = CoValueClass<Acc> & {
13
15
  fromNode: (typeof Account)["fromNode"];
14
16
  create: (options: {
@@ -45,15 +47,46 @@ class TestJSCrypto extends PureJSCrypto {
45
47
  export async function createJazzTestAccount<Acc extends Account>(options?: {
46
48
  isCurrentActiveAccount?: boolean;
47
49
  AccountSchema?: CoValueClass<Acc>;
50
+ creationProps?: Record<string, unknown>;
48
51
  }): Promise<Acc> {
49
52
  const AccountSchema = (options?.AccountSchema ??
50
53
  Account) as unknown as TestAccountSchema<Acc>;
51
- const account = await AccountSchema.create({
54
+ const peers = [];
55
+ if (syncServer.current) {
56
+ const [aPeer, bPeer] = cojsonInternals.connectedPeers(
57
+ Math.random().toString(),
58
+ Math.random().toString(),
59
+ {
60
+ peer1role: "server",
61
+ peer2role: "server",
62
+ },
63
+ );
64
+ syncServer.current.syncManager.addPeer(aPeer);
65
+ peers.push(bPeer);
66
+ }
67
+
68
+ const { node } = await LocalNode.withNewlyCreatedAccount({
52
69
  creationProps: {
53
70
  name: "Test Account",
71
+ ...options?.creationProps,
54
72
  },
55
73
  crypto: await TestJSCrypto.create(),
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
+ },
56
86
  });
87
+
88
+ const account = AccountSchema.fromNode(node);
89
+
57
90
  if (options?.isCurrentActiveAccount) {
58
91
  activeAccountContext.set(account);
59
92
  }
@@ -110,3 +143,20 @@ export function linkAccounts(
110
143
  a._raw.core.node.syncManager.addPeer(aPeer);
111
144
  b._raw.core.node.syncManager.addPeer(bPeer);
112
145
  }
146
+
147
+ export async function setupJazzTestSync() {
148
+ if (syncServer.current) {
149
+ syncServer.current.gracefulShutdown();
150
+ }
151
+
152
+ const account = await Account.create({
153
+ creationProps: {
154
+ name: "Test Account",
155
+ },
156
+ crypto: await TestJSCrypto.create(),
157
+ });
158
+
159
+ syncServer.current = account._raw.core.node;
160
+
161
+ return account;
162
+ }
@@ -0,0 +1,51 @@
1
+ import { beforeEach, describe, expect, test } from "vitest";
2
+ import { Account, CoMap, Group, co } from "../exports";
3
+ import { createJazzTestAccount, setupJazzTestSync } from "../testing";
4
+
5
+ describe("Jazz Test Sync", () => {
6
+ beforeEach(async () => {
7
+ await setupJazzTestSync();
8
+ });
9
+
10
+ test("two test accounts can sync through sync server", async () => {
11
+ const account1 = await createJazzTestAccount();
12
+ const account2 = await createJazzTestAccount();
13
+
14
+ // Create a test group in account1
15
+ const group = Group.create(account1);
16
+ group.addMember("everyone", "reader");
17
+
18
+ const map = CoMap.create({}, group);
19
+ map._raw.set("test", "value");
20
+
21
+ // Verify account2 can see the group
22
+ const loadedMap = await CoMap.load(map.id, account2, {});
23
+ expect(loadedMap).toBeDefined();
24
+ expect(loadedMap?._raw.get("test")).toBe("value");
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
+ });
51
+ });