Skip to content

Commit

Permalink
feat: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Gancho Radkov committed Dec 21, 2023
1 parent 5b0fa77 commit 3bba512
Showing 1 changed file with 83 additions and 1 deletion.
84 changes: 83 additions & 1 deletion packages/sign-client/test/sdk/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from "@walletconnect/jsonrpc-utils";
import { RelayerTypes } from "@walletconnect/types";
import { getSdkError, parseUri } from "@walletconnect/utils";
import { expect, describe, it, vi, beforeEach, afterEach } from "vitest";
import { expect, describe, it, vi } from "vitest";
import SignClient, { WALLETCONNECT_DEEPLINK_CHOICE } from "../../src";

import {
Expand Down Expand Up @@ -346,6 +346,88 @@ describe("Sign Client Integration", () => {
await throttle(1000);
await deleteClients(clients);
});
/**
* this test simulates the case where a session is disconnected
* while session request is being approved
* the queue should continue operating normally after the `respond` rejection
*/
it("should handle session disconnect durign request approval", async () => {
// create the clients and pair them
const {
clients,
sessionA: { topic: topicA },
} = await initTwoPairedClients({}, {}, { logger: "error" });
const dapp = clients.A as SignClient;
const wallet = clients.B as SignClient;
const { uri, approval } = await dapp.connect({
requiredNamespaces: {},
});

let topicB = "";
await Promise.all([
new Promise<void>((resolve) => {
wallet.once("session_proposal", async (args) => {
const { id } = args.params;
await wallet.approve({
id,
namespaces: TEST_NAMESPACES,
});
resolve();
});
}),
wallet.pair({ uri: uri! }),
new Promise<void>(async (resolve) => {
const session = await approval();
topicB = session.topic;
resolve();
}),
]);

const expectedRequests = 5;
let receivedRequests = 0;
await Promise.all([
new Promise<void>((resolve) => {
clients.B.on("session_request", async (args) => {
receivedRequests++;
const { id, topic } = args;

// capture the request on topicB, disconnect and try to approve the request
if (topic === topicB) {
await new Promise<void>(async (_resolve) => {
await wallet.disconnect({
topic,
reason: getSdkError("USER_DISCONNECTED"),
});
_resolve();
});
}
await clients.B.respond({
topic,
response: formatJsonRpcResult(id, "ok"),
}).catch((err) => {
// eslint-disable-next-line no-console
console.log("respond error", err);
});
if (receivedRequests >= expectedRequests) resolve();
});
}),
new Promise<void>((resolve) => {
clients.A.request({
topic: topicB,
...TEST_REQUEST_PARAMS,
});
resolve();
}),
Array.from(Array(expectedRequests).keys()).map(() =>
clients.A.request({
topic: topicA,
...TEST_REQUEST_PARAMS,
}),
),
]);
await throttle(1000);
await deleteClients(clients);
});
});
});
});
Expand Down

0 comments on commit 3bba512

Please sign in to comment.