jazz-tools 0.9.13 → 0.9.14
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 +33 -3
- package/dist/testing.js.map +1 -1
- package/package.json +1 -1
- package/src/testing.ts +36 -1
- package/src/tests/testing.test.ts +26 -0
package/.turbo/turbo-build.log
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
|
2
|
-
> jazz-tools@0.9.
|
2
|
+
> jazz-tools@0.9.14 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[
|
14
|
+
[32mESM[39m [1mdist/testing.js [22m[32m3.05 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[
|
18
|
+
[32mESM[39m [1mdist/testing.js.map [22m[32m6.31 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 103ms
|
package/CHANGELOG.md
CHANGED
package/dist/testing.js
CHANGED
@@ -7,6 +7,7 @@ import {
|
|
7
7
|
// src/testing.ts
|
8
8
|
import { cojsonInternals } from "cojson";
|
9
9
|
import { PureJSCrypto } from "cojson/crypto";
|
10
|
+
var syncServer = { current: null };
|
10
11
|
var TestJSCrypto = class extends PureJSCrypto {
|
11
12
|
static async create() {
|
12
13
|
if ("navigator" in globalThis && navigator.userAgent.includes("jsdom")) {
|
@@ -22,11 +23,26 @@ var TestJSCrypto = class extends PureJSCrypto {
|
|
22
23
|
};
|
23
24
|
async function createJazzTestAccount(options) {
|
24
25
|
const AccountSchema = options?.AccountSchema ?? Account;
|
26
|
+
const peers = [];
|
27
|
+
if (syncServer.current) {
|
28
|
+
const [aPeer, bPeer] = cojsonInternals.connectedPeers(
|
29
|
+
Math.random().toString(),
|
30
|
+
Math.random().toString(),
|
31
|
+
{
|
32
|
+
peer1role: "server",
|
33
|
+
peer2role: "server"
|
34
|
+
}
|
35
|
+
);
|
36
|
+
syncServer.current.syncManager.addPeer(aPeer);
|
37
|
+
peers.push(bPeer);
|
38
|
+
}
|
25
39
|
const account = await AccountSchema.create({
|
26
40
|
creationProps: {
|
27
|
-
name: "Test Account"
|
41
|
+
name: "Test Account",
|
42
|
+
...options?.creationProps
|
28
43
|
},
|
29
|
-
crypto: await TestJSCrypto.create()
|
44
|
+
crypto: await TestJSCrypto.create(),
|
45
|
+
peersToLoadFrom: peers
|
30
46
|
});
|
31
47
|
if (options?.isCurrentActiveAccount) {
|
32
48
|
activeAccountContext.set(account);
|
@@ -69,11 +85,25 @@ function linkAccounts(a, b, aRole = "server", bRole = "server") {
|
|
69
85
|
a._raw.core.node.syncManager.addPeer(aPeer);
|
70
86
|
b._raw.core.node.syncManager.addPeer(bPeer);
|
71
87
|
}
|
88
|
+
async function setupJazzTestSync() {
|
89
|
+
if (syncServer.current) {
|
90
|
+
syncServer.current.gracefulShutdown();
|
91
|
+
}
|
92
|
+
const account = await Account.create({
|
93
|
+
creationProps: {
|
94
|
+
name: "Test Account"
|
95
|
+
},
|
96
|
+
crypto: await TestJSCrypto.create()
|
97
|
+
});
|
98
|
+
syncServer.current = account._raw.core.node;
|
99
|
+
return account;
|
100
|
+
}
|
72
101
|
export {
|
73
102
|
createJazzTestAccount,
|
74
103
|
createJazzTestGuest,
|
75
104
|
getJazzContextShape,
|
76
105
|
linkAccounts,
|
77
|
-
setActiveAccount
|
106
|
+
setActiveAccount,
|
107
|
+
setupJazzTestSync
|
78
108
|
};
|
79
109
|
//# sourceMappingURL=testing.js.map
|
package/dist/testing.js.map
CHANGED
@@ -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;
|
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 account = await AccountSchema.create({\n creationProps: {\n name: \"Test Account\",\n ...options?.creationProps,\n },\n crypto: await TestJSCrypto.create(),\n peersToLoadFrom: peers,\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":";;;;;;;AACA,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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ClD,MAAM,MAAM,QAAQ,MAAM,KAAK,iBAAiB;AAAA,IAClD;AAAA,EACF;AAEA,SAAO;AAAA,IACL,IAAI;AAAA,IACJ,eAAe,QAAQ;AAAA,IACvB,QAAQ,MAAM,QAAQ,KAAK,KAAK,KAAK,iBAAiB;AAAA,IACtD,MAAM,MAAM,QAAQ,KAAK,KAAK,KAAK,iBAAiB;AAAA,EACtD;AACF;AAEO,SAAS,aACd,GACA,GACA,QAA6B,UAC7B,QAA6B,UAC7B;AACA,QAAM,CAAC,OAAO,KAAK,IAAI,gBAAgB,eAAe,EAAE,IAAI,EAAE,IAAI;AAAA,IAChE,WAAW;AAAA,IACX,WAAW;AAAA,EACb,CAAC;AAED,IAAE,KAAK,KAAK,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":[]}
|
package/package.json
CHANGED
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,14 +47,30 @@ 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>;
|
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
|
+
}
|
51
67
|
const account = await AccountSchema.create({
|
52
68
|
creationProps: {
|
53
69
|
name: "Test Account",
|
70
|
+
...options?.creationProps,
|
54
71
|
},
|
55
72
|
crypto: await TestJSCrypto.create(),
|
73
|
+
peersToLoadFrom: peers,
|
56
74
|
});
|
57
75
|
if (options?.isCurrentActiveAccount) {
|
58
76
|
activeAccountContext.set(account);
|
@@ -110,3 +128,20 @@ export function linkAccounts(
|
|
110
128
|
a._raw.core.node.syncManager.addPeer(aPeer);
|
111
129
|
b._raw.core.node.syncManager.addPeer(bPeer);
|
112
130
|
}
|
131
|
+
|
132
|
+
export async function setupJazzTestSync() {
|
133
|
+
if (syncServer.current) {
|
134
|
+
syncServer.current.gracefulShutdown();
|
135
|
+
}
|
136
|
+
|
137
|
+
const account = await Account.create({
|
138
|
+
creationProps: {
|
139
|
+
name: "Test Account",
|
140
|
+
},
|
141
|
+
crypto: await TestJSCrypto.create(),
|
142
|
+
});
|
143
|
+
|
144
|
+
syncServer.current = account._raw.core.node;
|
145
|
+
|
146
|
+
return account;
|
147
|
+
}
|
@@ -0,0 +1,26 @@
|
|
1
|
+
import { beforeEach, describe, expect, test } from "vitest";
|
2
|
+
import { CoMap, Group } 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
|
+
});
|