Skip to content

Commit

Permalink
refactor: adds tests to deeplink builder
Browse files Browse the repository at this point in the history
  • Loading branch information
ganchoradkov committed Sep 30, 2024
1 parent c728abf commit f0b91c5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
27 changes: 15 additions & 12 deletions packages/utils/src/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,19 +368,9 @@ export async function handleDeeplinkRedirect({
if (!wcDeepLink) return;

const json = typeof wcDeepLink === "string" ? JSON.parse(wcDeepLink) : wcDeepLink;
let deeplink = json?.href;
const deeplink = json?.href;
if (typeof deeplink !== "string") return;

const payload = `requestId=${id}&sessionTopic=${topic}`;
if (deeplink.endsWith("/")) deeplink = deeplink.slice(0, -1);
let link = `${deeplink}`;
if (deeplink.startsWith("https://t.me")) {
const startApp = deeplink.includes("?") ? "&startapp=" : "?startapp=";
link = `${link}${startApp}${toBase64(payload, true)}`;
} else {
link = `${link}/wc?${payload}`;
}

const link = formatDeeplinkUrl(deeplink, id, topic);
const env = getEnvironment();

if (env === ENV_MAP.browser) {
Expand All @@ -407,6 +397,19 @@ export async function handleDeeplinkRedirect({
}
}

export function formatDeeplinkUrl(deeplink: string, requestId: number, sessionTopic: string) {
const payload = `requestId=${requestId}&sessionTopic=${sessionTopic}`;
if (deeplink.endsWith("/")) deeplink = deeplink.slice(0, -1);
let link = `${deeplink}`;
if (deeplink.startsWith("https://t.me")) {
const startApp = deeplink.includes("?") ? "&startapp=" : "?startapp=";
link = `${link}${startApp}${toBase64(payload, true)}`;
} else {
link = `${link}/wc?${payload}`;
}
return link;
}

export async function getDeepLink(storage: IKeyValueStorage, key: string) {
let link: string | undefined = "";
try {
Expand Down
31 changes: 31 additions & 0 deletions packages/utils/test/misc.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
calcExpiry,
formatDeeplinkUrl,
formatRelayRpcUrl,
formatUA,
getSearchParamFromURL,
hasOverlap,
isExpired,
toBase64,
} from "../src";

const RELAY_URL = "wss://relay.walletconnect.org";
Expand Down Expand Up @@ -121,4 +123,33 @@ describe("Misc", () => {
});
});
});
describe("deep links", () => {
it("should format universal link", () => {
const deepLink = "https://example.com";
const requestId = 123;
const sessionTopic = "randomSessionTopic";
const expectedDeepLink = `${deepLink}/wc?requestId=${requestId}&sessionTopic=${sessionTopic}`;
const formatted = formatDeeplinkUrl(deepLink, requestId, sessionTopic);
expect(formatted).to.eql(expectedDeepLink);
});
it("should format deep link", () => {
const deepLink = "trust://";
const requestId = 123;
const sessionTopic = "randomSessionTopic";
const expectedDeepLink = `${deepLink}wc?requestId=${requestId}&sessionTopic=${sessionTopic}`;
const formatted = formatDeeplinkUrl(deepLink, requestId, sessionTopic);
expect(formatted).to.eql(expectedDeepLink);
});
it("should format telegram universal link", async () => {
const deepLink = "https://t.me";
const requestId = 123;
const sessionTopic = "randomSessionTopic";
const partToEncode = `requestId=${requestId}&sessionTopic=${sessionTopic}`;
const expectedDeepLink = `${deepLink}?startapp=${toBase64(partToEncode, true)}`;
const formatted = formatDeeplinkUrl(deepLink, requestId, sessionTopic);
expect(formatted).to.eql(expectedDeepLink);
const decoded = atob(formatted.split("startapp=")[1]);
expect(decoded).to.eql(partToEncode);
});
});
});

0 comments on commit f0b91c5

Please sign in to comment.