From dbb482c49342f4b75d7d0aab1a7e1199831b3522 Mon Sep 17 00:00:00 2001 From: dafuga Date: Thu, 15 Feb 2024 22:03:44 -0800 Subject: [PATCH] chore: making tests actually check plugin --- package.json | 1 + src/index.ts | 10 ++++ test/tests/index.ts | 117 ++++++++++++++++++++++++++++++++++---------- yarn.lock | 5 ++ 4 files changed, 106 insertions(+), 27 deletions(-) diff --git a/package.json b/package.json index 53e1287..1eadb82 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "@wharfkit/antelope": "^1.0.5", "@wharfkit/protocol-esr": "^1.3.0", "isomorphic-ws": "^5.0.0", + "mock-socket": "^9.3.1", "ws": "^8.13.0" }, "resolutions": { diff --git a/src/index.ts b/src/index.ts index d8e038d..797ad0b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -147,9 +147,13 @@ export class WalletPluginAnchor extends AbstractWalletPlugin { console.info('Modal closed') }) + console.log('before esr') + // Await a promise race to wait for either the wallet response or the cancel const callbackResponse: CallbackPayload = await waitForCallback(callback, this.buoyWs, t) + console.log({ callbackResponse }) verifyLoginCallbackResponse(callbackResponse, context) + console.log('after verifyLoginCallbackResponse', { callbackResponse }) if (!callbackResponse.cid || !callbackResponse.sa || !callbackResponse.sp) { throw new Error('Invalid callback response') @@ -180,8 +184,12 @@ export class WalletPluginAnchor extends AbstractWalletPlugin { context.esrOptions ) + console.log({ resolvedResponse, callbackResponse }) + const identityProof = resolvedResponse.getIdentityProof(callbackResponse.sig) + console.log({ identityProof}) + return { chain: Checksum256.from(callbackResponse.cid), permissionLevel: PermissionLevel.from({ @@ -361,6 +369,8 @@ export class WalletPluginAnchor extends AbstractWalletPlugin { isCallback(callbackResponse) && extractSignaturesFromCallback(callbackResponse).length > 0 + console.log({ callbackResponse, wasSuccessful }) + if (wasSuccessful) { // If the callback was resolved, create a new request from the response const resolvedRequest = await ResolvedSigningRequest.fromPayload( diff --git a/test/tests/index.ts b/test/tests/index.ts index bbce770..4980b98 100644 --- a/test/tests/index.ts +++ b/test/tests/index.ts @@ -1,36 +1,93 @@ -import chai, {expect} from 'chai' -import {PermissionLevel, SessionKit} from '@wharfkit/session' -import sinon from 'sinon' -import sinonChai from 'sinon-chai' -import * as buoy from '@greymass/buoy' +import chai, { expect } from 'chai'; +import sinon from 'sinon'; +import sinonChai from 'sinon-chai'; +import { Server as MockServer } from 'mock-socket'; +import * as buoy from '@greymass/buoy'; +import { SessionKit, IdentityProof, PermissionLevel, Cancelable, ResolvedSigningRequest, PrivateKey } from '@wharfkit/session'; -chai.use(sinonChai) +chai.use(sinonChai); -import {mockSessionKitArgs, mockSessionKitOptions} from '@wharfkit/mock-data' -import {mockCallbackPayload} from '$test/utils/mock-esr' -import {mockChainId} from '$test/utils/mock-config' +import { mockSessionKitArgs, mockSessionKitOptions } from '@wharfkit/mock-data'; +import { SigningRequest } from '@wharfkit/session'; +import { mockChainId, mockPrivateKey as mockPrivateKeyString } from '$test/utils/mock-config'; +import { WalletPluginAnchor } from '$lib'; +import { mockCallbackPayload as v2MockCallbackPayload } from '$test/utils/mock-esr'; -const mockPermissionLevel = PermissionLevel.from('wharfkit1115@test') +const fakeURL = 'ws://localhost:8080'; + +const mockPermissionLevel = PermissionLevel.from('@test'); + +const mockRequest = SigningRequest.identity({ + callback: fakeURL, + account: mockPermissionLevel.actor, + permission: mockPermissionLevel.permission, +}); + +const mockResolvedRequest: ResolvedSigningRequest = mockRequest.resolve(new Map(), mockPermissionLevel); + +const mockPrivateKey = PrivateKey.from(mockPrivateKeyString) + +const signature = mockPrivateKey.signDigest(mockResolvedRequest.signingDigest); + +const mockCallbackPayload = mockResolvedRequest.getCallback([signature])?.payload suite('wallet plugin', function () { - this.timeout(120 * 1000) - this.slow(5 * 1000) + this.timeout(120 * 1000); + this.slow(5 * 1000); + + let mockServer; + + setup(() => { + // Setup mock WebSocket server + mockServer = new MockServer(fakeURL); + + // Replace the global WebSocket with the mock-socket's WebSocket + global.WebSocket = WebSocket; + + console.log({mockCallbackPayload}) + + // Mock buoy's receive and send to simulate WebSocket interactions + sinon.stub(buoy, 'receive').resolves(JSON.stringify(mockCallbackPayload)); + sinon.stub(buoy, 'send'); + }); - // TODO: Implement a real test, this currently open a socket and expects Anchor to respond. test('login and sign', async function () { - sinon.stub(buoy, 'receive').resolves(JSON.stringify(mockCallbackPayload)) - sinon.stub(buoy, 'send') + const kit = new SessionKit({ + ...mockSessionKitArgs, + walletPlugins: [new WalletPluginAnchor({ buoyWs: new WebSocket(fakeURL), buoyUrl: fakeURL })], + ui: { + ...mockSessionKitArgs.ui, + translate: (error) => error, + getTranslate: () => (error) => error, + onError: async () => {}, + onLogin: async () => {}, + onLoginComplete: async () => {}, + onTransact: async () => {}, + onTransactComplete: async () => {}, + prompt: () => new Promise(() => {}) as Cancelable, + }, + }, mockSessionKitOptions); + + // Simulate WebSocket message from the server + mockServer.on('connection', socket => { + socket.on('message', () => { + // Respond with a mocked callback payload when a message is received + socket.send(JSON.stringify(mockCallbackPayload)); + }); + }); - const kit = new SessionKit(mockSessionKitArgs, mockSessionKitOptions) - const {session} = await kit.login({ + const { session, response } = await kit.login({ chain: mockChainId, permissionLevel: mockPermissionLevel, - }) + }); - expect(String(session.chain.id)).to.equal(mockChainId) - expect(String(session.actor)).to.equal(String(mockPermissionLevel.actor)) - expect(String(session.permission)).to.equal(String(mockPermissionLevel.permission)) + expect(String(session.chain.id)).to.equal(mockChainId); + expect(String(session.actor)).to.equal(String(mockPermissionLevel.actor)); + expect(String(session.permission)).to.equal(String(mockPermissionLevel.permission)); + expect(response.identityProof).to.be.instanceOf(IdentityProof); + // Add additional assertions as needed + // Simulate a transact request const result = await session.transact( { action: { @@ -48,10 +105,16 @@ suite('wallet plugin', function () { { broadcast: false, } - ) + ); + + // Add assertions to validate the result of the transact request + expect(String(result.signer.actor)).to.equal(String(mockPermissionLevel.actor)); + expect(String(result.signer.permission)).to.equal(String(mockPermissionLevel.permission)); + expect(result.signatures).to.be.length(1); + }); - expect(String(result.signer.actor)).to.equal(String(mockPermissionLevel.actor)) - expect(String(result.signer.permission)).to.equal(String(mockPermissionLevel.permission)) - expect(result.signatures).to.be.length(1) - }) -}) + teardown(() => { + mockServer.stop(); // Stop the mock WebSocket server + sinon.restore(); // Restore original functions + }); +}); diff --git a/yarn.lock b/yarn.lock index e0534bc..35bd4d7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2099,6 +2099,11 @@ mocha@^10.0.0: yargs-parser "20.2.4" yargs-unparser "2.0.0" +mock-socket@^9.3.1: + version "9.3.1" + resolved "https://registry.yarnpkg.com/mock-socket/-/mock-socket-9.3.1.tgz#24fb00c2f573c84812aa4a24181bb025de80cc8e" + integrity sha512-qxBgB7Qa2sEQgHFjj0dSigq7fX4k6Saisd5Nelwp2q8mlbAFh5dHV9JTTlF8viYJLSSWgMCZFUom8PJcMNBoJw== + ms@2.1.2: version "2.1.2" resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"