Skip to content

Commit

Permalink
chore: making tests actually check plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Feb 16, 2024
1 parent d8b5d72 commit dbb482c
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 27 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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(
Expand Down
117 changes: 90 additions & 27 deletions test/tests/index.ts
Original file line number Diff line number Diff line change
@@ -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<any>,
},
}, 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: {
Expand All @@ -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
});
});
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==

[email protected]:
version "2.1.2"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
Expand Down

0 comments on commit dbb482c

Please sign in to comment.