jazz-tools 0.14.1 → 0.14.2
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 +5 -5
- package/CHANGELOG.md +7 -0
- package/dist/testing.d.ts +4 -4
- package/dist/testing.d.ts.map +1 -1
- package/dist/testing.js +4 -5
- package/dist/testing.js.map +1 -1
- package/dist/tests/patterns/notifications.test.d.ts +2 -0
- package/dist/tests/patterns/notifications.test.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/testing.ts +17 -10
- package/src/tests/patterns/notifications.test.ts +89 -0
- package/src/tests/patterns/requestToJoin.test.ts +8 -5
package/.turbo/turbo-build.log
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
|
2
|
-
> jazz-tools@0.14.
|
2
|
+
> jazz-tools@0.14.2 build /home/runner/_work/jazz/jazz/packages/jazz-tools
|
3
3
|
> tsup && pnpm types
|
4
4
|
|
5
5
|
[34mCLI[39m Building entry: {"index":"src/index.ts","testing":"src/testing.ts"}
|
@@ -9,14 +9,14 @@
|
|
9
9
|
[34mCLI[39m Target: es2021
|
10
10
|
[34mCLI[39m Cleaning output folder
|
11
11
|
[34mESM[39m Build start
|
12
|
-
[32mESM[39m [1mdist/testing.js [22m[32m6.27 KB[39m
|
13
12
|
[32mESM[39m [1mdist/index.js [22m[32m11.03 KB[39m
|
13
|
+
[32mESM[39m [1mdist/testing.js [22m[32m6.31 KB[39m
|
14
14
|
[32mESM[39m [1mdist/chunk-WLOZKDOH.js [22m[32m130.46 KB[39m
|
15
|
-
[32mESM[39m [1mdist/testing.js.map [22m[32m12.31 KB[39m
|
16
15
|
[32mESM[39m [1mdist/index.js.map [22m[32m20.74 KB[39m
|
16
|
+
[32mESM[39m [1mdist/testing.js.map [22m[32m12.57 KB[39m
|
17
17
|
[32mESM[39m [1mdist/chunk-WLOZKDOH.js.map [22m[32m304.21 KB[39m
|
18
|
-
[32mESM[39m ⚡️ Build success in
|
18
|
+
[32mESM[39m ⚡️ Build success in 104ms
|
19
19
|
|
20
|
-
> jazz-tools@0.14.
|
20
|
+
> jazz-tools@0.14.2 types /home/runner/_work/jazz/jazz/packages/jazz-tools
|
21
21
|
> tsc --outDir dist
|
22
22
|
|
package/CHANGELOG.md
CHANGED
package/dist/testing.d.ts
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
import { LocalNode, Peer } from "cojson";
|
2
2
|
import { PureJSCrypto } from "cojson/dist/crypto/PureJSCrypto";
|
3
|
-
import { Account, AccountClass, type AnonymousJazzAgent, type CoValueClass, CoValueFromRaw, JazzContextManager, JazzContextManagerAuthProps, JazzContextManagerBaseProps } from "./internal.js";
|
3
|
+
import { Account, AccountClass, type AnonymousJazzAgent, AnyAccountSchema, type CoValueClass, CoValueFromRaw, InstanceOfSchema, JazzContextManager, JazzContextManagerAuthProps, JazzContextManagerBaseProps } from "./internal.js";
|
4
4
|
export declare class TestJSCrypto extends PureJSCrypto {
|
5
5
|
static create(): Promise<PureJSCrypto>;
|
6
6
|
}
|
7
7
|
export declare function getPeerConnectedToTestSyncServer(): Peer;
|
8
|
-
export declare function createJazzTestAccount<
|
8
|
+
export declare function createJazzTestAccount<S extends (AccountClass<Account> & CoValueFromRaw<Account>) | AnyAccountSchema>(options?: {
|
9
9
|
isCurrentActiveAccount?: boolean;
|
10
|
-
AccountSchema?:
|
10
|
+
AccountSchema?: S;
|
11
11
|
creationProps?: Record<string, unknown>;
|
12
|
-
}): Promise<
|
12
|
+
}): Promise<InstanceOfSchema<S>>;
|
13
13
|
export declare function setActiveAccount(account: Account): void;
|
14
14
|
export declare function createJazzTestGuest(): Promise<{
|
15
15
|
guest: AnonymousJazzAgent;
|
package/dist/testing.d.ts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"testing.d.ts","sourceRoot":"","sources":["../src/testing.ts"],"names":[],"mappings":"AAAA,OAAO,EAA+B,SAAS,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AA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O,CAAC;CAC3B,CAAC;AAEJ,qBAAa,sBAAsB,CACjC,GAAG,SAAS,OAAO,CACnB,SAAQ,kBAAkB,CAAC,GAAG,EAAE,2BAA2B,CAAC,GAAG,CAAC,CAAC;IACjE,MAAM,CAAC,kBAAkB,CAAC,GAAG,SAAS,OAAO,EAC3C,OAAO,CAAC,EAAE,GAAG,GAAG;QAAE,KAAK,EAAE,kBAAkB,CAAA;KAAE,EAC7C,KAAK,CAAC,EAAE,2BAA2B,CAAC,GAAG,CAAC;IAS1C,MAAM,CAAC,WAAW,CAAC,GAAG,SAAS,OAAO,EACpC,OAAO,EAAE,GAAG,EACZ,KAAK,CAAC,EAAE,2BAA2B,CAAC,GAAG,CAAC;IA0C1C,MAAM,CAAC,SAAS,CAAC,GAAG,SAAS,OAAO,EAClC,EAAE,KAAK,EAAE,EAAE;QAAE,KAAK,EAAE,kBAAkB,CAAA;KAAE,EACxC,KAAK,GAAE,2BAA2B,CAAC,GAAG,CAAM;IAmBxC,aAAa,CACjB,KAAK,EAAE,2BAA2B,CAAC,GAAG,CAAC,EACvC,SAAS,CAAC,EAAE,2BAA2B;;uBAkEg/K,cAAc;;;;;;;;CApCxiL;AAED,wBAAsB,YAAY,CAChC,CAAC,EAAE,OAAO,EACV,CAAC,EAAE,OAAO,EACV,KAAK,GAAE,QAAQ,GAAG,QAAmB,EACrC,KAAK,GAAE,QAAQ,GAAG,QAAmB,iBAYtC;AAED,wBAAsB,iBAAiB,qBAetC"}
|
package/dist/testing.js
CHANGED
@@ -2,6 +2,7 @@ import {
|
|
2
2
|
Account,
|
3
3
|
JazzContextManager,
|
4
4
|
activeAccountContext,
|
5
|
+
anySchemaToCoSchema,
|
5
6
|
createAnonymousJazzContext,
|
6
7
|
createJazzContext,
|
7
8
|
randomSessionProvider
|
@@ -43,7 +44,7 @@ function getPeerConnectedToTestSyncServer() {
|
|
43
44
|
var SecretSeedMap = /* @__PURE__ */ new Map();
|
44
45
|
var isMigrationActive = false;
|
45
46
|
async function createJazzTestAccount(options) {
|
46
|
-
const
|
47
|
+
const AccountClass2 = options?.AccountSchema ? anySchemaToCoSchema(options.AccountSchema) : Account;
|
47
48
|
const peers = [];
|
48
49
|
if (syncServer.current) {
|
49
50
|
peers.push(getPeerConnectedToTestSyncServer());
|
@@ -65,9 +66,7 @@ async function createJazzTestAccount(options) {
|
|
65
66
|
);
|
66
67
|
}
|
67
68
|
isMigrationActive = true;
|
68
|
-
const account2 =
|
69
|
-
fromRaw: rawAccount
|
70
|
-
});
|
69
|
+
const account2 = AccountClass2.fromRaw(rawAccount);
|
71
70
|
const prevActiveAccount = activeAccountContext.maybeGet();
|
72
71
|
activeAccountContext.set(account2);
|
73
72
|
await account2.applyMigration?.(creationProps);
|
@@ -77,7 +76,7 @@ async function createJazzTestAccount(options) {
|
|
77
76
|
isMigrationActive = false;
|
78
77
|
}
|
79
78
|
});
|
80
|
-
const account =
|
79
|
+
const account = AccountClass2.fromNode(node);
|
81
80
|
SecretSeedMap.set(account.id, secretSeed);
|
82
81
|
if (options?.isCurrentActiveAccount) {
|
83
82
|
activeAccountContext.set(account);
|
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/dist/crypto/PureJSCrypto\";\nimport {\n Account,\n AccountClass,\n type AnonymousJazzAgent,\n AuthCredentials,\n type CoValueClass,\n CoValueFromRaw,\n JazzContextManager,\n JazzContextManagerAuthProps,\n JazzContextManagerBaseProps,\n activeAccountContext,\n createAnonymousJazzContext,\n createJazzContext,\n randomSessionProvider,\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\nexport class 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 function getPeerConnectedToTestSyncServer() {\n if (!syncServer.current) {\n throw new Error(\"Sync server not initialized\");\n }\n\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\n return bPeer;\n}\n\nconst SecretSeedMap = new Map<string, Uint8Array>();\nlet isMigrationActive = false;\n\nexport async function createJazzTestAccount<A extends Account>(options?: {\n isCurrentActiveAccount?: boolean;\n AccountSchema?: CoValueClass<A>;\n creationProps?: Record<string, unknown>;\n}): Promise<A> {\n const AccountSchema = (options?.AccountSchema ??\n Account) as unknown as TestAccountSchema<Account>;\n const peers = [];\n if (syncServer.current) {\n peers.push(getPeerConnectedToTestSyncServer());\n }\n\n const crypto = await TestJSCrypto.create();\n const secretSeed = crypto.newRandomSecretSeed();\n\n const { node } = await LocalNode.withNewlyCreatedAccount({\n creationProps: {\n name: \"Test Account\",\n ...options?.creationProps,\n },\n initialAgentSecret: crypto.agentSecretFromSecretSeed(secretSeed),\n crypto,\n peersToLoadFrom: peers,\n migration: async (rawAccount, _node, creationProps) => {\n if (isMigrationActive) {\n throw new Error(\n \"It is not possible to create multiple accounts in parallel inside the test environment.\",\n );\n }\n\n isMigrationActive = true;\n\n const account = new AccountSchema({\n fromRaw: rawAccount,\n });\n\n // We need to set the account as current because the migration\n // will probably rely on the global me\n const prevActiveAccount = activeAccountContext.maybeGet();\n activeAccountContext.set(account);\n\n await account.applyMigration?.(creationProps);\n\n if (!options?.isCurrentActiveAccount) {\n activeAccountContext.set(prevActiveAccount);\n }\n\n isMigrationActive = false;\n },\n });\n\n const account = AccountSchema.fromNode(node);\n SecretSeedMap.set(account.id, secretSeed);\n\n if (options?.isCurrentActiveAccount) {\n activeAccountContext.set(account);\n }\n\n return account as A;\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 type TestJazzContextManagerProps<Acc extends Account> =\n JazzContextManagerBaseProps<Acc> & {\n defaultProfileName?: string;\n AccountSchema?: AccountClass<Acc> & CoValueFromRaw<Acc>;\n isAuthenticated?: boolean;\n };\n\nexport class TestJazzContextManager<\n Acc extends Account,\n> extends JazzContextManager<Acc, TestJazzContextManagerProps<Acc>> {\n static fromAccountOrGuest<Acc extends Account>(\n account?: Acc | { guest: AnonymousJazzAgent },\n props?: TestJazzContextManagerProps<Acc>,\n ) {\n if (account && \"guest\" in account) {\n return this.fromGuest<Acc>(account, props);\n }\n\n return this.fromAccount<Acc>(account ?? (Account.getMe() as Acc), props);\n }\n\n static fromAccount<Acc extends Account>(\n account: Acc,\n props?: TestJazzContextManagerProps<Acc>,\n ) {\n const context = new TestJazzContextManager<Acc>();\n\n const provider = props?.isAuthenticated ? \"testProvider\" : \"anonymous\";\n const storage = context.getAuthSecretStorage();\n const node = account._raw.core.node;\n\n const credentials = {\n accountID: account.id,\n accountSecret: node.getCurrentAgent().agentSecret,\n secretSeed: SecretSeedMap.get(account.id),\n provider,\n } satisfies AuthCredentials;\n\n storage.set(credentials);\n\n context.updateContext(\n {\n AccountSchema: account.constructor as AccountClass<Acc> &\n CoValueFromRaw<Acc>,\n ...props,\n },\n {\n me: account,\n node,\n done: () => {\n node.gracefulShutdown();\n },\n logOut: async () => {\n await storage.clear();\n node.gracefulShutdown();\n },\n },\n {\n credentials,\n },\n );\n\n return context;\n }\n\n static fromGuest<Acc extends Account>(\n { guest }: { guest: AnonymousJazzAgent },\n props: TestJazzContextManagerProps<Acc> = {},\n ) {\n const context = new TestJazzContextManager<Acc>();\n const node = guest.node;\n\n context.updateContext(props, {\n guest,\n node,\n done: () => {\n node.gracefulShutdown();\n },\n logOut: async () => {\n node.gracefulShutdown();\n },\n });\n\n return context;\n }\n\n async getNewContext(\n props: TestJazzContextManagerProps<Acc>,\n authProps?: JazzContextManagerAuthProps,\n ) {\n if (!syncServer.current) {\n throw new Error(\n \"You need to setup a test sync server with setupJazzTestSync to use the Auth functions\",\n );\n }\n\n const context = await createJazzContext({\n credentials: authProps?.credentials,\n defaultProfileName: props.defaultProfileName,\n newAccountProps: authProps?.newAccountProps,\n peersToLoadFrom: [getPeerConnectedToTestSyncServer()],\n crypto: await TestJSCrypto.create(),\n sessionProvider: randomSessionProvider,\n authSecretStorage: this.getAuthSecretStorage(),\n AccountSchema: props.AccountSchema,\n });\n\n return {\n me: context.account,\n node: context.node,\n done: () => {\n context.done();\n },\n logOut: () => {\n return context.logOut();\n },\n };\n }\n}\n\nexport async 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 await a.waitForAllCoValuesSync();\n await b.waitForAllCoValuesSync();\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;AAiB7B,IAAM,aAA4C,EAAE,SAAS,KAAK;AAY3D,IAAM,eAAN,cAA2B,aAAa;AAAA,EAC7C,aAAa,SAAS;AACpB,QAAI,eAAe,cAAc,UAAU,WAAW,SAAS,OAAO,GAAG;AAEvE,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;AAEO,SAAS,mCAAmC;AACjD,MAAI,CAAC,WAAW,SAAS;AACvB,UAAM,IAAI,MAAM,6BAA6B;AAAA,EAC/C;AAEA,QAAM,CAAC,OAAO,KAAK,IAAI,gBAAgB;AAAA,IACrC,KAAK,OAAO,EAAE,SAAS;AAAA,IACvB,KAAK,OAAO,EAAE,SAAS;AAAA,IACvB;AAAA,MACE,WAAW;AAAA,MACX,WAAW;AAAA,IACb;AAAA,EACF;AACA,aAAW,QAAQ,YAAY,QAAQ,KAAK;AAE5C,SAAO;AACT;AAEA,IAAM,gBAAgB,oBAAI,IAAwB;AAClD,IAAI,oBAAoB;AAExB,eAAsB,sBAAyC,SAIhD;AACb,QAAM,gBAAiB,SAAS,iBAC9B;AACF,QAAM,QAAQ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QACR;AAAA,MACF;AAAA,IACF;AAEA,UAAM,UAAU,MAAM,kBAAkB;AAAA,MACtC,aAAa,WAAW;AAAA,MACxB,oBAAoB,MAAM;AAAA,MAC1B,iBAAiB,WAAW;AAAA,MAC5B,iBAAiB,CAAC,iCAAiC,CAAC;AAAA,MACpD,QAAQ,MAAM,aAAa,OAAO;AAAA,MAClC,iBAAiB;AAAA,MACjB,mBAAmB,KAAK,qBAAqB;AAAA,MAC7C,eAAe,MAAM;AAAA,IACvB,CAAC;AAED,WAAO;AAAA,MACL,IAAI,QAAQ;AAAA,MACZ,MAAM,QAAQ;AAAA,MACd,MAAM,MAAM;AACV,gBAAQ,KAAK;AAAA,MACf;AAAA,MACA,QAAQ,MAAM;AACZ,eAAO,QAAQ,OAAO;AAAA,MACxB;AAAA,IACF;AAAA,EACF;AACF;AAEA,eAAsB,aACpB,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;AAE1C,QAAM,EAAE,uBAAuB;AAC/B,QAAM,EAAE,uBAAuB;AACjC;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"]}
|
1
|
+
{"version":3,"sources":["../src/testing.ts"],"sourcesContent":["import { AgentSecret, CryptoProvider, LocalNode, Peer } from \"cojson\";\nimport { cojsonInternals } from \"cojson\";\nimport { PureJSCrypto } from \"cojson/dist/crypto/PureJSCrypto\";\nimport {\n Account,\n AccountClass,\n type AnonymousJazzAgent,\n AnyAccountSchema,\n AuthCredentials,\n type CoValueClass,\n CoValueFromRaw,\n InstanceOfSchema,\n JazzContextManager,\n JazzContextManagerAuthProps,\n JazzContextManagerBaseProps,\n activeAccountContext,\n anySchemaToCoSchema,\n createAnonymousJazzContext,\n createJazzContext,\n randomSessionProvider,\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\nexport class 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 function getPeerConnectedToTestSyncServer() {\n if (!syncServer.current) {\n throw new Error(\"Sync server not initialized\");\n }\n\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\n return bPeer;\n}\n\nconst SecretSeedMap = new Map<string, Uint8Array>();\nlet isMigrationActive = false;\n\nexport async function createJazzTestAccount<\n S extends\n | (AccountClass<Account> & CoValueFromRaw<Account>)\n | AnyAccountSchema,\n>(options?: {\n isCurrentActiveAccount?: boolean;\n AccountSchema?: S;\n creationProps?: Record<string, unknown>;\n}): Promise<InstanceOfSchema<S>> {\n const AccountClass = options?.AccountSchema\n ? anySchemaToCoSchema(options.AccountSchema)\n : Account;\n const peers = [];\n if (syncServer.current) {\n peers.push(getPeerConnectedToTestSyncServer());\n }\n\n const crypto = await TestJSCrypto.create();\n const secretSeed = crypto.newRandomSecretSeed();\n\n const { node } = await LocalNode.withNewlyCreatedAccount({\n creationProps: {\n name: \"Test Account\",\n ...options?.creationProps,\n },\n initialAgentSecret: crypto.agentSecretFromSecretSeed(secretSeed),\n crypto,\n peersToLoadFrom: peers,\n migration: async (rawAccount, _node, creationProps) => {\n if (isMigrationActive) {\n throw new Error(\n \"It is not possible to create multiple accounts in parallel inside the test environment.\",\n );\n }\n\n isMigrationActive = true;\n\n // @ts-expect-error - AccountClass doesn't infer the fromRaw static method\n const account = AccountClass.fromRaw(rawAccount) as InstanceOfSchema<S>;\n\n // We need to set the account as current because the migration\n // will probably rely on the global me\n const prevActiveAccount = activeAccountContext.maybeGet();\n activeAccountContext.set(account);\n\n await account.applyMigration?.(creationProps);\n\n if (!options?.isCurrentActiveAccount) {\n activeAccountContext.set(prevActiveAccount);\n }\n\n isMigrationActive = false;\n },\n });\n\n const account = AccountClass.fromNode(node);\n SecretSeedMap.set(account.id, secretSeed);\n\n if (options?.isCurrentActiveAccount) {\n activeAccountContext.set(account);\n }\n\n return account as InstanceOfSchema<S>;\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 type TestJazzContextManagerProps<Acc extends Account> =\n JazzContextManagerBaseProps<Acc> & {\n defaultProfileName?: string;\n AccountSchema?: AccountClass<Acc> & CoValueFromRaw<Acc>;\n isAuthenticated?: boolean;\n };\n\nexport class TestJazzContextManager<\n Acc extends Account,\n> extends JazzContextManager<Acc, TestJazzContextManagerProps<Acc>> {\n static fromAccountOrGuest<Acc extends Account>(\n account?: Acc | { guest: AnonymousJazzAgent },\n props?: TestJazzContextManagerProps<Acc>,\n ) {\n if (account && \"guest\" in account) {\n return this.fromGuest<Acc>(account, props);\n }\n\n return this.fromAccount<Acc>(account ?? (Account.getMe() as Acc), props);\n }\n\n static fromAccount<Acc extends Account>(\n account: Acc,\n props?: TestJazzContextManagerProps<Acc>,\n ) {\n const context = new TestJazzContextManager<Acc>();\n\n const provider = props?.isAuthenticated ? \"testProvider\" : \"anonymous\";\n const storage = context.getAuthSecretStorage();\n const node = account._raw.core.node;\n\n const credentials = {\n accountID: account.id,\n accountSecret: node.getCurrentAgent().agentSecret,\n secretSeed: SecretSeedMap.get(account.id),\n provider,\n } satisfies AuthCredentials;\n\n storage.set(credentials);\n\n context.updateContext(\n {\n AccountSchema: account.constructor as AccountClass<Acc> &\n CoValueFromRaw<Acc>,\n ...props,\n },\n {\n me: account,\n node,\n done: () => {\n node.gracefulShutdown();\n },\n logOut: async () => {\n await storage.clear();\n node.gracefulShutdown();\n },\n },\n {\n credentials,\n },\n );\n\n return context;\n }\n\n static fromGuest<Acc extends Account>(\n { guest }: { guest: AnonymousJazzAgent },\n props: TestJazzContextManagerProps<Acc> = {},\n ) {\n const context = new TestJazzContextManager<Acc>();\n const node = guest.node;\n\n context.updateContext(props, {\n guest,\n node,\n done: () => {\n node.gracefulShutdown();\n },\n logOut: async () => {\n node.gracefulShutdown();\n },\n });\n\n return context;\n }\n\n async getNewContext(\n props: TestJazzContextManagerProps<Acc>,\n authProps?: JazzContextManagerAuthProps,\n ) {\n if (!syncServer.current) {\n throw new Error(\n \"You need to setup a test sync server with setupJazzTestSync to use the Auth functions\",\n );\n }\n\n const context = await createJazzContext({\n credentials: authProps?.credentials,\n defaultProfileName: props.defaultProfileName,\n newAccountProps: authProps?.newAccountProps,\n peersToLoadFrom: [getPeerConnectedToTestSyncServer()],\n crypto: await TestJSCrypto.create(),\n sessionProvider: randomSessionProvider,\n authSecretStorage: this.getAuthSecretStorage(),\n AccountSchema: props.AccountSchema,\n });\n\n return {\n me: context.account,\n node: context.node,\n done: () => {\n context.done();\n },\n logOut: () => {\n return context.logOut();\n },\n };\n }\n}\n\nexport async 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 await a.waitForAllCoValuesSync();\n await b.waitForAllCoValuesSync();\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;AAoB7B,IAAM,aAA4C,EAAE,SAAS,KAAK;AAY3D,IAAM,eAAN,cAA2B,aAAa;AAAA,EAC7C,aAAa,SAAS;AACpB,QAAI,eAAe,cAAc,UAAU,WAAW,SAAS,OAAO,GAAG;AAEvE,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;AAC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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;AAE1C,QAAM,EAAE,uBAAuB;AAC/B,QAAM,EAAE,uBAAuB;AACjC;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":["AccountClass","account"]}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"notifications.test.d.ts","sourceRoot":"","sources":["../../../src/tests/patterns/notifications.test.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
package/src/testing.ts
CHANGED
@@ -5,13 +5,16 @@ import {
|
|
5
5
|
Account,
|
6
6
|
AccountClass,
|
7
7
|
type AnonymousJazzAgent,
|
8
|
+
AnyAccountSchema,
|
8
9
|
AuthCredentials,
|
9
10
|
type CoValueClass,
|
10
11
|
CoValueFromRaw,
|
12
|
+
InstanceOfSchema,
|
11
13
|
JazzContextManager,
|
12
14
|
JazzContextManagerAuthProps,
|
13
15
|
JazzContextManagerBaseProps,
|
14
16
|
activeAccountContext,
|
17
|
+
anySchemaToCoSchema,
|
15
18
|
createAnonymousJazzContext,
|
16
19
|
createJazzContext,
|
17
20
|
randomSessionProvider,
|
@@ -73,13 +76,18 @@ export function getPeerConnectedToTestSyncServer() {
|
|
73
76
|
const SecretSeedMap = new Map<string, Uint8Array>();
|
74
77
|
let isMigrationActive = false;
|
75
78
|
|
76
|
-
export async function createJazzTestAccount<
|
79
|
+
export async function createJazzTestAccount<
|
80
|
+
S extends
|
81
|
+
| (AccountClass<Account> & CoValueFromRaw<Account>)
|
82
|
+
| AnyAccountSchema,
|
83
|
+
>(options?: {
|
77
84
|
isCurrentActiveAccount?: boolean;
|
78
|
-
AccountSchema?:
|
85
|
+
AccountSchema?: S;
|
79
86
|
creationProps?: Record<string, unknown>;
|
80
|
-
}): Promise<
|
81
|
-
const
|
82
|
-
|
87
|
+
}): Promise<InstanceOfSchema<S>> {
|
88
|
+
const AccountClass = options?.AccountSchema
|
89
|
+
? anySchemaToCoSchema(options.AccountSchema)
|
90
|
+
: Account;
|
83
91
|
const peers = [];
|
84
92
|
if (syncServer.current) {
|
85
93
|
peers.push(getPeerConnectedToTestSyncServer());
|
@@ -105,9 +113,8 @@ export async function createJazzTestAccount<A extends Account>(options?: {
|
|
105
113
|
|
106
114
|
isMigrationActive = true;
|
107
115
|
|
108
|
-
|
109
|
-
|
110
|
-
});
|
116
|
+
// @ts-expect-error - AccountClass doesn't infer the fromRaw static method
|
117
|
+
const account = AccountClass.fromRaw(rawAccount) as InstanceOfSchema<S>;
|
111
118
|
|
112
119
|
// We need to set the account as current because the migration
|
113
120
|
// will probably rely on the global me
|
@@ -124,14 +131,14 @@ export async function createJazzTestAccount<A extends Account>(options?: {
|
|
124
131
|
},
|
125
132
|
});
|
126
133
|
|
127
|
-
const account =
|
134
|
+
const account = AccountClass.fromNode(node);
|
128
135
|
SecretSeedMap.set(account.id, secretSeed);
|
129
136
|
|
130
137
|
if (options?.isCurrentActiveAccount) {
|
131
138
|
activeAccountContext.set(account);
|
132
139
|
}
|
133
140
|
|
134
|
-
return account as
|
141
|
+
return account as InstanceOfSchema<S>;
|
135
142
|
}
|
136
143
|
|
137
144
|
export function setActiveAccount(account: Account) {
|
@@ -0,0 +1,89 @@
|
|
1
|
+
import { describe, expect, test, vi } from "vitest";
|
2
|
+
import { InstanceOfSchema, Loaded, co, z } from "../../exports";
|
3
|
+
import { createJazzTestAccount } from "../../testing";
|
4
|
+
|
5
|
+
const QueuedNotification = co.map({
|
6
|
+
content: z.string(),
|
7
|
+
sent: z.boolean(),
|
8
|
+
});
|
9
|
+
|
10
|
+
const WorkerRoot = co.map({
|
11
|
+
notificationQueue: co.list(QueuedNotification),
|
12
|
+
});
|
13
|
+
|
14
|
+
const WorkerAccount = co
|
15
|
+
.account({
|
16
|
+
root: WorkerRoot,
|
17
|
+
profile: co.profile(),
|
18
|
+
})
|
19
|
+
.withMigration((account) => {
|
20
|
+
if (account.root === undefined) {
|
21
|
+
account.root = WorkerRoot.create({
|
22
|
+
notificationQueue: co.list(QueuedNotification).create([]),
|
23
|
+
});
|
24
|
+
}
|
25
|
+
});
|
26
|
+
|
27
|
+
async function pushNotification(
|
28
|
+
worker: InstanceOfSchema<typeof WorkerAccount>,
|
29
|
+
content: string,
|
30
|
+
) {
|
31
|
+
const workerAccount = await worker.ensureLoaded({
|
32
|
+
resolve: {
|
33
|
+
root: {
|
34
|
+
notificationQueue: {
|
35
|
+
$each: true,
|
36
|
+
},
|
37
|
+
},
|
38
|
+
},
|
39
|
+
});
|
40
|
+
|
41
|
+
const notification = QueuedNotification.create({
|
42
|
+
content,
|
43
|
+
sent: false,
|
44
|
+
});
|
45
|
+
|
46
|
+
workerAccount.root.notificationQueue.push(notification);
|
47
|
+
|
48
|
+
return notification;
|
49
|
+
}
|
50
|
+
|
51
|
+
async function sendAllThePendingNotifications(
|
52
|
+
worker: InstanceOfSchema<typeof WorkerAccount>,
|
53
|
+
sender: (notification: Loaded<typeof QueuedNotification>) => Promise<void>,
|
54
|
+
) {
|
55
|
+
const workerAccount = await worker.ensureLoaded({
|
56
|
+
resolve: {
|
57
|
+
root: {
|
58
|
+
notificationQueue: {
|
59
|
+
$each: true,
|
60
|
+
},
|
61
|
+
},
|
62
|
+
},
|
63
|
+
});
|
64
|
+
|
65
|
+
for (const notification of workerAccount.root.notificationQueue) {
|
66
|
+
if (!notification.sent) {
|
67
|
+
notification.sent = true;
|
68
|
+
await sender(notification);
|
69
|
+
}
|
70
|
+
}
|
71
|
+
}
|
72
|
+
|
73
|
+
describe("Notifications", () => {
|
74
|
+
test("when pushing a notification, it should be added to the queue", async () => {
|
75
|
+
const worker = await createJazzTestAccount({
|
76
|
+
isCurrentActiveAccount: true,
|
77
|
+
AccountSchema: WorkerAccount,
|
78
|
+
});
|
79
|
+
|
80
|
+
const notification = await pushNotification(worker, "Hello");
|
81
|
+
|
82
|
+
const spy = vi.fn();
|
83
|
+
|
84
|
+
await sendAllThePendingNotifications(worker, spy);
|
85
|
+
|
86
|
+
expect(notification.sent).toBe(true);
|
87
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
88
|
+
});
|
89
|
+
});
|
@@ -20,6 +20,7 @@ const Organization = co.map({
|
|
20
20
|
statuses: RequestsStatus,
|
21
21
|
projects: co.list(z.string()),
|
22
22
|
mainGroup: Group,
|
23
|
+
adminsGroup: Group,
|
23
24
|
});
|
24
25
|
|
25
26
|
async function setup() {
|
@@ -65,6 +66,7 @@ async function setup() {
|
|
65
66
|
// but this is the source of truth for admins
|
66
67
|
statuses: RequestsStatus.create({}, adminsGroup),
|
67
68
|
mainGroup: organizationGroup,
|
69
|
+
adminsGroup,
|
68
70
|
},
|
69
71
|
publicGroup,
|
70
72
|
);
|
@@ -84,7 +86,7 @@ async function setup() {
|
|
84
86
|
|
85
87
|
async function sendRequestToJoin(organizationId: string, account: Account) {
|
86
88
|
const organization = await Organization.load(organizationId, {
|
87
|
-
resolve: { requests: true },
|
89
|
+
resolve: { requests: true, adminsGroup: true },
|
88
90
|
loadAs: account,
|
89
91
|
});
|
90
92
|
|
@@ -92,12 +94,15 @@ async function sendRequestToJoin(organizationId: string, account: Account) {
|
|
92
94
|
throw new Error("RequestsMap not found");
|
93
95
|
}
|
94
96
|
|
97
|
+
const group = Group.create(account);
|
98
|
+
group.extend(organization.adminsGroup);
|
99
|
+
|
95
100
|
const request = RequestToJoin.create(
|
96
101
|
{
|
97
102
|
account,
|
98
103
|
status: "pending",
|
99
104
|
},
|
100
|
-
|
105
|
+
group,
|
101
106
|
);
|
102
107
|
|
103
108
|
organization.requests[account.id] = request;
|
@@ -264,8 +269,6 @@ describe("Request to join", () => {
|
|
264
269
|
|
265
270
|
// With the writeOnly permission, the user can download the request
|
266
271
|
// but not it's content
|
267
|
-
|
268
|
-
expect(requestOnUser2.status).toBe(undefined);
|
269
|
-
expect(requestOnUser2.account).toBe(undefined);
|
272
|
+
expect(requestOnUser2).toBeNull();
|
270
273
|
});
|
271
274
|
});
|