status-desktop/storybook/qmlTests/tests/tst_BCDAppsProvider.qml
Alex Jbanca 106988d534 fix(WC): Refactor dapps service to work with multiple SDKs
This PR is refactoring the dapps service to avoid code duplication between SDKs and also to avoid overlapping requests/responses.
It brings Browser Connect inline with Wallet Connect in terms of session management and sign transactions.

New architecture:

WalletConnectService becomes DAppsService. Its responsibility is to provide dapp access to the app. This is the component currently used by the UI
What does it do:
1. Provide dapp APIs line connect, disconnect, session requests etc
2. Spawn app notifications on dapp events
3. Timeout requests if the dapp does not respons

DAppsRequestHandler becomes DAppsModule. This component is consumed by the DAppService. Its responsibility is to aggregate all the building blocks for the dapps, but does not control any of the dapp features or consume the SDKs requests.
What does it do:
1. Aggregate all the building blocks for dapps (currently known as plugins)

DAppConnectionsPlugin - This component provides the session management features line connect, disconnect and provide a model with the connected dapps.
SignRequestPlugin - This component provides the sign request management. It receives the sign request from the dapp, translates it to what Status understands and manages the lifecycle of the request.
2024-11-20 18:10:29 +02:00

98 lines
4.7 KiB
QML

import QtQuick 2.15
import QtTest 1.15
import AppLayouts.Wallet.services.dapps 1.0
Item {
id: root
Component {
id: bcDAppsProviderComponent
BCDappsProvider {
id: bcDAppsProvider
readonly property SignalSpy connectedSpy: SignalSpy { target: bcDAppsProvider; signalName: "connected" }
readonly property SignalSpy disconnectedSpy: SignalSpy { target: bcDAppsProvider; signalName: "disconnected" }
bcSDK: WalletConnectSDKBase {
enabled: true
projectId: ""
property var activeSessions: {}
getActiveSessions: function(callback) {
callback(activeSessions)
}
}
}
}
function buildSession(dappUrl, dappName, dappIcon, proposalId, account, chains) {
let sessionTemplate = (dappUrl, dappName, dappIcon, proposalId, eipAccount, eipChains) => {
return {
peer: {
metadata: {
description: "-",
icons: [
dappIcon
],
name: dappName,
url: dappUrl
}
},
namespaces: {
eip155: {
accounts: [eipAccount],
chains: eipChains
}
},
pairingTopic: proposalId,
topic: dappUrl
};
}
const eipAccount = account ? `eip155:${account}` : ""
const eipChains = chains ? chains.map((chain) => `eip155:${chain}`) : []
return sessionTemplate(dappUrl, dappName, dappIcon, proposalId, eipAccount, eipChains)
}
TestCase {
id: bcDAppsProviderTest
property BCDappsProvider componentUnderTest: null
function init() {
componentUnderTest = createTemporaryObject(bcDAppsProviderComponent, root)
}
function test_addRemoveSession() {
const newSession = buildSession("https://example.com", "Example", "https://example.com/icon.png", "123", "0x123", ["1"])
componentUnderTest.bcSDK.approveSessionResult("requestID", newSession, null)
compare(componentUnderTest.connectedSpy.count, 1, "Connected signal should be emitted once")
compare(componentUnderTest.connectedSpy.signalArguments[0][0], "requestID", "Connected signal should have correct proposalId")
compare(componentUnderTest.connectedSpy.signalArguments[0][1], "https://example.com", "Connected signal should have correct topic")
compare(componentUnderTest.connectedSpy.signalArguments[0][2], "https://example.com", "Connected signal should have correct dAppUrl")
const dapp = componentUnderTest.getByTopic("https://example.com")
verify(!!dapp, "DApp should be found")
compare(dapp.name, "Example", "DApp should have correct name")
compare(dapp.url, "https://example.com", "DApp should have correct url")
compare(dapp.iconUrl, "https://example.com/icon.png", "DApp should have correct iconUrl")
compare(dapp.topic, "https://example.com", "DApp should have correct topic")
compare(dapp.connectorId, componentUnderTest.connectorId, "DApp should have correct connectorId")
compare(dapp.accountAddresses.count, 1, "DApp should have correct accountAddresses count")
compare(dapp.accountAddresses.get(0).address, "0x123", "DApp should have correct accountAddresses address")
compare(dapp.rawSessions.count, 1, "DApp should have correct rawSessions count")
componentUnderTest.bcSDK.sessionDelete("https://example.com", "")
compare(componentUnderTest.disconnectedSpy.count, 1, "Disconnected signal should be emitted once")
compare(componentUnderTest.disconnectedSpy.signalArguments[0][0], "https://example.com", "Disconnected signal should have correct topic")
compare(componentUnderTest.disconnectedSpy.signalArguments[0][1], "https://example.com", "Disconnected signal should have correct dAppUrl")
}
function test_disabledSDK() {
componentUnderTest.bcSDK.enabled = false
componentUnderTest.bcSDK.approveSessionResult("requestID", buildSession("https://example.com", "Example", "https://example.com/icon.png", "123", "0x123", ["1"]), "")
compare(componentUnderTest.connectedSpy.count, 0, "Connected signal should not be emitted")
componentUnderTest.bcSDK.sessionDelete("https://example.com", "")
compare(componentUnderTest.disconnectedSpy.count, 0, "Disconnected signal should not be emitted")
}
}
}