Skip to content

Instantly share code, notes, and snippets.

@baymac
Created August 17, 2023 05:56
Show Gist options
  • Save baymac/82ab9f498423263d870d678e5c382667 to your computer and use it in GitHub Desktop.
Save baymac/82ab9f498423263d870d678e5c382667 to your computer and use it in GitHub Desktop.
Test to demo that event sequence is not respected
diff --git a/packages/sign-client/package.json b/packages/sign-client/package.json
index 9c61b52b..36c016af 100644
--- a/packages/sign-client/package.json
+++ b/packages/sign-client/package.json
@@ -24,7 +24,7 @@
"build:source": "rollup --config rollup.config.js",
"build": "npm run build:pre; npm run build:source; npm run build:types",
"test:pre": "rm -rf ./test/tmp && mkdir ./test/tmp",
- "test:run": "vitest run --dir test/sdk",
+ "test:run": "vitest run test/sdk/persistence.spec.ts",
"test:concurrency": "vitest run --dir test/concurrency",
"test:xregion": "vitest run --dir test/xregion -- --dangerouslyIgnoreUnhandledErrors --segfault-retry=3",
"test": "npm run test:pre; npm run test:run",
diff --git a/packages/sign-client/test/sdk/persistence.spec.ts b/packages/sign-client/test/sdk/persistence.spec.ts
index 08d858e8..85e9393f 100644
--- a/packages/sign-client/test/sdk/persistence.spec.ts
+++ b/packages/sign-client/test/sdk/persistence.spec.ts
@@ -1,5 +1,5 @@
import { formatJsonRpcError, JsonRpcError } from "@walletconnect/jsonrpc-utils";
-import { generateRandomBytes32, getSdkError } from "@walletconnect/utils";
+import { createExpiringPromise, generateRandomBytes32, getSdkError } from "@walletconnect/utils";
import { describe, expect, it } from "vitest";
import { SignClient } from "../../src";
import {
@@ -202,7 +202,7 @@ describe("Sign Client Persistence", () => {
* this test simulates case where a dapp is offline while the wallet performs normal operations such as adding new accounts & emitting events
* the dapp should receive all requests when it comes back online and process them in the expected order
*/
- it("should process incoming mailbox messages after restart", async () => {
+ it.only("should process incoming mailbox messages after restart", async () => {
const chains = ["eip155:1"];
const accounts = ["0x0000000", "0x1111111", "0x2222222"];
const requiredNamespaces = {
@@ -234,78 +234,87 @@ describe("Sign Client Persistence", () => {
// delete client B
await deleteClients({ A: clients.A, B: undefined });
- await throttle(500);
- clients.B.update({
- topic,
- namespaces: {
- eip155: {
- ...approvedNamespaces.eip155,
- accounts: approvedNamespaces.eip155.accounts.concat([`${chains[0]}:${accounts[1]}`]),
+ await createExpiringPromise(
+ clients.B.update({
+ topic,
+ namespaces: {
+ eip155: {
+ ...approvedNamespaces.eip155,
+ accounts: approvedNamespaces.eip155.accounts.concat([`${chains[0]}:${accounts[1]}`]),
+ },
},
- },
- });
+ }),
+ 2000,
+ );
+
+ await createExpiringPromise(
+ clients.B.emit({
+ topic,
+ event: {
+ name: "accountsChanged",
+ data: [`${chains[0]}:${accounts[1]}`],
+ },
+ chainId: "eip155:1",
+ }),
+ 2000,
+ );
- await throttle(500);
+ const lastAccountsChangedValue = [`eip155:2:${accounts[1]}`];
const lastWalletSessionNamespacesValue = {
eip155: {
...approvedNamespaces.eip155,
accounts: approvedNamespaces.eip155.accounts.concat([
`${chains[0]}:${accounts[1]}`,
- `${chains[0]}:${accounts[2]}`,
+ ...lastAccountsChangedValue,
]),
+ chains: chains.concat["eip155:2"],
},
};
- clients.B.update({
- topic,
- namespaces: lastWalletSessionNamespacesValue,
- });
- const lastAccountsChangedValue = [`${chains[0]}:${accounts[1]}`];
+ await createExpiringPromise(
+ clients.B.update({
+ topic,
+ namespaces: lastWalletSessionNamespacesValue,
+ }),
+ 2000,
+ );
- clients.B.emit({
- topic,
- event: {
- name: "accountsChanged",
- data: [`${chains[0]}:${accounts[1]}`],
- },
- chainId: "eip155:1",
- });
- await throttle(500);
- clients.B.emit({
- topic,
- event: {
- name: "accountsChanged",
- data: [`${chains[0]}:${accounts[2]}`],
- },
- chainId: "eip155:1",
- });
- await throttle(500);
- clients.B.emit({
- topic,
- event: {
- name: "accountsChanged",
- data: lastAccountsChangedValue,
- },
- chainId: "eip155:1",
- });
+ await createExpiringPromise(
+ clients.B.emit({
+ topic,
+ event: {
+ name: "chainChanged",
+ data: lastAccountsChangedValue,
+ },
+ chainId: "eip155:2",
+ }),
+ 2000,
+ );
- await throttle(500);
// restart the client
clients.A = await SignClient.init({
...TEST_SIGN_CLIENT_OPTIONS_A,
storageOptions: { database: db_a },
});
- let lastAccountEvent: any;
+ let lastEvent: any;
+ let lastSessionEvent: any;
clients.A.on("session_event", (event) => {
- lastAccountEvent = event.params.event.data;
+ lastSessionEvent = event.params.event.data;
+ lastEvent = [event.params.event.name, event.params.event.data];
+ });
+
+ clients.A.on("session_update", (event) => {
+ lastEvent = ["session_update", event.params.namespaces.eip155.accounts];
});
+
await throttle(10_000);
const session = clients.A.session.get(topic);
expect(session).toBeDefined();
expect(session.namespaces).toEqual(lastWalletSessionNamespacesValue);
- expect(lastAccountEvent).toEqual(lastAccountsChangedValue);
+ expect(lastSessionEvent).toEqual(lastAccountsChangedValue);
+ expect(lastEvent).toEqual(["chainChanged", lastAccountsChangedValue]);
await deleteClients(clients);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment