2021-02-05 18:59:00 -08:00
|
|
|
import {Client as LiveClient} from 'rpc-websockets';
|
|
|
|
import {expect} from 'chai';
|
2021-09-25 12:25:14 -07:00
|
|
|
import {createSandbox} from 'sinon';
|
2021-02-07 08:57:12 -08:00
|
|
|
|
2021-02-05 18:59:00 -08:00
|
|
|
import {Connection} from '../../src';
|
|
|
|
|
|
|
|
type RpcRequest = {
|
2021-03-14 22:08:10 -07:00
|
|
|
method: string;
|
|
|
|
params?: Array<any>;
|
2021-02-05 18:59:00 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
type RpcResponse = {
|
|
|
|
context: {
|
2021-03-14 22:08:10 -07:00
|
|
|
slot: number;
|
|
|
|
};
|
|
|
|
value: any;
|
2021-02-05 18:59:00 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
const mockRpcSocket: Array<[RpcRequest, RpcResponse]> = [];
|
2021-09-25 12:25:14 -07:00
|
|
|
const sandbox = createSandbox();
|
2021-02-05 18:59:00 -08:00
|
|
|
|
|
|
|
export const mockRpcMessage = ({
|
|
|
|
method,
|
|
|
|
params,
|
|
|
|
result,
|
|
|
|
}: {
|
2021-03-14 22:08:10 -07:00
|
|
|
method: string;
|
|
|
|
params: Array<any>;
|
|
|
|
result: any;
|
2021-02-05 18:59:00 -08:00
|
|
|
}) => {
|
|
|
|
mockRpcSocket.push([
|
|
|
|
{method, params},
|
|
|
|
{
|
|
|
|
context: {slot: 11},
|
|
|
|
value: result,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const stubRpcWebSocket = (connection: Connection) => {
|
|
|
|
const rpcWebSocket = connection._rpcWebSocket;
|
|
|
|
const mockClient = new MockClient(rpcWebSocket);
|
|
|
|
sandbox.stub(rpcWebSocket, 'connect').callsFake(() => {
|
|
|
|
mockClient.connect();
|
|
|
|
});
|
|
|
|
sandbox.stub(rpcWebSocket, 'close').callsFake(() => {
|
|
|
|
mockClient.close();
|
|
|
|
});
|
2021-03-14 22:08:10 -07:00
|
|
|
sandbox
|
|
|
|
.stub(rpcWebSocket, 'call')
|
|
|
|
.callsFake((method: string, params: any) => {
|
|
|
|
return mockClient.call(method, params);
|
|
|
|
});
|
2021-02-05 18:59:00 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
export const restoreRpcWebSocket = (connection: Connection) => {
|
|
|
|
connection._rpcWebSocket.close();
|
|
|
|
if (connection._rpcWebSocketIdleTimeout !== null) {
|
|
|
|
clearTimeout(connection._rpcWebSocketIdleTimeout);
|
|
|
|
connection._rpcWebSocketIdleTimeout = null;
|
|
|
|
}
|
|
|
|
sandbox.restore();
|
|
|
|
};
|
|
|
|
|
|
|
|
class MockClient {
|
2021-02-07 08:57:12 -08:00
|
|
|
client: LiveClient;
|
2021-02-05 18:59:00 -08:00
|
|
|
mockOpen = false;
|
|
|
|
subscriptionCounter = 0;
|
|
|
|
|
|
|
|
constructor(rpcWebSocket: LiveClient) {
|
|
|
|
this.client = rpcWebSocket;
|
|
|
|
}
|
|
|
|
|
|
|
|
connect() {
|
|
|
|
if (!this.mockOpen) {
|
|
|
|
this.mockOpen = true;
|
|
|
|
this.client.emit('open');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
|
|
|
if (this.mockOpen) {
|
|
|
|
this.mockOpen = false;
|
|
|
|
this.client.emit('close');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
call(method: string, params: Array<any>): Promise<Object> {
|
|
|
|
expect(mockRpcSocket.length).to.be.at.least(1);
|
2021-03-14 22:08:10 -07:00
|
|
|
const [mockRequest, mockResponse] = mockRpcSocket.shift() as [
|
|
|
|
RpcRequest,
|
|
|
|
RpcResponse,
|
|
|
|
];
|
2021-02-05 18:59:00 -08:00
|
|
|
|
|
|
|
expect(method).to.eq(mockRequest.method);
|
|
|
|
expect(params).to.eql(mockRequest.params);
|
|
|
|
|
|
|
|
let id = ++this.subscriptionCounter;
|
|
|
|
const response = {
|
|
|
|
subscription: id,
|
|
|
|
result: mockResponse,
|
|
|
|
};
|
|
|
|
|
|
|
|
setImmediate(() => {
|
|
|
|
const eventName = method.replace('Subscribe', 'Notification');
|
|
|
|
this.client.emit(eventName, response);
|
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.resolve(id);
|
|
|
|
}
|
|
|
|
}
|