mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-05 05:15:48 +00:00
Compare commits
2 Commits
@transia/r
...
mv/broadca
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
44e860f617 | ||
|
|
14d8753c15 |
@@ -16,9 +16,7 @@ describe('BroadcastClient', function () {
|
|||||||
afterEach(teardownClient)
|
afterEach(teardownClient)
|
||||||
|
|
||||||
it('base', async function () {
|
it('base', async function () {
|
||||||
this.mocks.forEach((mock) => {
|
this.mockRippled.addResponse('server_info', rippled.server_info.normal)
|
||||||
mock.addResponse('server_info', rippled.server_info.normal)
|
|
||||||
})
|
|
||||||
assert(this.client.isConnected())
|
assert(this.client.isConnected())
|
||||||
this.client
|
this.client
|
||||||
.request({ command: 'server_info' })
|
.request({ command: 'server_info' })
|
||||||
@@ -29,9 +27,7 @@ describe('BroadcastClient', function () {
|
|||||||
|
|
||||||
it('error propagation', function (done) {
|
it('error propagation', function (done) {
|
||||||
const data = { error: 'type', error_message: 'info' }
|
const data = { error: 'type', error_message: 'info' }
|
||||||
this.mocks.forEach((mock) => {
|
this.mockRippled.addResponse('echo', data)
|
||||||
mock.addResponse('echo', data)
|
|
||||||
})
|
|
||||||
this.client.once('error', (type, info) => {
|
this.client.once('error', (type, info) => {
|
||||||
assert.strictEqual(type, 'type')
|
assert.strictEqual(type, 'type')
|
||||||
assert.strictEqual(info, 'info')
|
assert.strictEqual(info, 'info')
|
||||||
|
|||||||
@@ -47,6 +47,38 @@ export interface PortResponse extends BaseResponse {
|
|||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- typing is too complicated otherwise
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- typing is too complicated otherwise
|
||||||
type MockedWebSocketServer = any
|
type MockedWebSocketServer = any
|
||||||
|
|
||||||
|
class MockedWebSocketBroadcastServer {
|
||||||
|
public mocks: MockedWebSocketServer[]
|
||||||
|
public constructor(mocks: MockedWebSocketServer[]) {
|
||||||
|
this.mocks = mocks
|
||||||
|
}
|
||||||
|
|
||||||
|
public addResponse(
|
||||||
|
command: string,
|
||||||
|
response:
|
||||||
|
| Response
|
||||||
|
| ErrorResponse
|
||||||
|
| ((r: Request) => Response | ErrorResponse),
|
||||||
|
): void {
|
||||||
|
this.mocks.forEach((mock) => {
|
||||||
|
mock.addResponse(command, response)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public close(): void {
|
||||||
|
this.mocks.forEach((mock: { close: () => void }) => mock.close())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createBroadcastMockRippled(
|
||||||
|
ports: number[],
|
||||||
|
): MockedWebSocketBroadcastServer {
|
||||||
|
// eslint-disable-next-line max-len -- Too many rules to disable
|
||||||
|
// eslint-disable-next-line @typescript-eslint/promise-function-async, @typescript-eslint/no-unsafe-return -- Typing is too complicated, not an async function
|
||||||
|
const mocks = ports.map((port) => createMockRippled(port))
|
||||||
|
return new MockedWebSocketBroadcastServer(mocks)
|
||||||
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/promise-function-async -- Not a promise that's returned
|
// eslint-disable-next-line @typescript-eslint/promise-function-async -- Not a promise that's returned
|
||||||
export default function createMockRippled(port: number): MockedWebSocketServer {
|
export default function createMockRippled(port: number): MockedWebSocketServer {
|
||||||
const mock = new WebSocketServer({ port }) as MockedWebSocketServer
|
const mock = new WebSocketServer({ port }) as MockedWebSocketServer
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types -- Necessary for test setup */
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types -- Necessary for test setup */
|
||||||
import { Client, BroadcastClient } from 'xrpl-local'
|
import { Client, BroadcastClient } from 'xrpl-local'
|
||||||
|
|
||||||
import createMockRippled from './mockRippled'
|
import createMockRippled, { createBroadcastMockRippled } from './mockRippled'
|
||||||
import { getFreePort } from './testUtils'
|
import { getFreePort } from './testUtils'
|
||||||
|
|
||||||
async function setupMockRippledConnection(
|
async function setupMockRippledConnection(
|
||||||
@@ -25,9 +25,7 @@ async function setupMockRippledConnectionForBroadcast(
|
|||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
return new Promise<void>((resolve, reject) => {
|
return new Promise<void>((resolve, reject) => {
|
||||||
const servers = ports.map((port) => `ws://localhost:${port}`)
|
const servers = ports.map((port) => `ws://localhost:${port}`)
|
||||||
// eslint-disable-next-line max-len -- Too many rules to disable
|
testcase.mockRippled = createBroadcastMockRippled(ports)
|
||||||
// eslint-disable-next-line @typescript-eslint/promise-function-async, @typescript-eslint/no-unsafe-return -- Typing is too complicated, not an async function
|
|
||||||
testcase.mocks = ports.map((port) => createMockRippled(port))
|
|
||||||
testcase.client = new BroadcastClient(servers)
|
testcase.client = new BroadcastClient(servers)
|
||||||
testcase.client.connect().then(resolve).catch(reject)
|
testcase.client.connect().then(resolve).catch(reject)
|
||||||
})
|
})
|
||||||
@@ -50,15 +48,16 @@ function teardownClient(this: any, done: () => void): void {
|
|||||||
this.client
|
this.client
|
||||||
.disconnect()
|
.disconnect()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
// eslint-disable-next-line no-negated-condition -- Easier to read with negation
|
this.mockRippled.close()
|
||||||
if (this.mockRippled != null) {
|
|
||||||
this.mockRippled.close()
|
|
||||||
} else {
|
|
||||||
this.mocks.forEach((mock: { close: () => void }) => mock.close())
|
|
||||||
}
|
|
||||||
setImmediate(done)
|
setImmediate(done)
|
||||||
})
|
})
|
||||||
.catch(done)
|
.catch(done)
|
||||||
}
|
}
|
||||||
|
|
||||||
export { setupClient, teardownClient, setupBroadcast, createMockRippled }
|
export {
|
||||||
|
setupBroadcast,
|
||||||
|
teardownClient,
|
||||||
|
setupBroadcast as setupClient,
|
||||||
|
setupClient as setupClientActual,
|
||||||
|
createMockRippled,
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user