Skip to content

Commit

Permalink
feat: unit tests
Browse files Browse the repository at this point in the history
- unit tests from node-hl7-server are the same for node-hl7-client for end to end testing

#15

[ci skip]
  • Loading branch information
Bugs5382 committed Dec 13, 2023
1 parent 4329173 commit cb8d30d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
77 changes: 75 additions & 2 deletions __tests__/hl7.client.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import portfinder from 'portfinder'
import {Hl7Inbound, Server} from "../../node-hl7-server/src";
import {Client, expectEvent, HL7Outbound, Message} from '../src'
import {Client, expectEvent, HL7Outbound, Message, sleep} from '../src'

describe('node hl7 client', () => {

Expand Down Expand Up @@ -227,9 +227,82 @@ describe('node hl7 client', () => {
})

describe('end to end tests', () => {
let LISTEN_PORT: number;

test.skip('...send message, get proper ACK', async () => {
beforeEach(async () => {
LISTEN_PORT = await portfinder.getPortPromise({
port: 3000,
stopPort: 65353
})
})

test('...send message, get proper ACK', async () => {

const server = new Server({bindAddress: '0.0.0.0'})
const IB_ADT = server.createInbound({port: LISTEN_PORT}, async (req, res) => {
const messageReq = req.getMessage()
const messageRes = res.getAckMessage()
expect(messageRes.get('MSA.1').toString()).toBe('AA')
expect(messageReq.get('MSH.12').toString()).toBe('2.7')
})

await sleep(5)

const client = new Client({host: '0.0.0.0'})

const OB_ADT = client.createOutbound({ port: LISTEN_PORT }, async (res) => {
expect(res.toString()).not.toContain('ADT^A01^ADT_A01')
})

await sleep(5)

let message = new Message({
messageHeader: {
msh_9_1: "ADT",
msh_9_2: "A01",
msh_10: 'CONTROL_ID'
}
})

await OB_ADT.sendMessage(message)

await sleep(10)

await OB_ADT.close()
await IB_ADT.close()

})

test('...send message, but the connection was closed -- error out', async () => {

let IB_ADT: Hl7Inbound
try {
const server = new Server({bindAddress: '0.0.0.0'})
IB_ADT = server.createInbound({port: LISTEN_PORT}, async (_req, _res) => {})

await sleep(5)

const client = new Client({host: '0.0.0.0'})
const OB_ADT = client.createOutbound({port: LISTEN_PORT, maxAttempts: 1}, async (_res) => {})

await sleep(5)

let message = new Message({
messageHeader: {
msh_9_1: "ADT",
msh_9_2: "A01",
msh_10: 'CONTROL_ID'
}
})

await OB_ADT.close() // @todo there needs to be a way we kill the server?
await IB_ADT.close() // make sure we close the server

await OB_ADT.sendMessage(message)

} catch (err: any) {
expect(err.message).toBe('In an invalid state to be able to send message.')
}
})

})
Expand Down
3 changes: 2 additions & 1 deletion src/client/hl7Outbound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,11 @@ export class HL7Outbound extends EventEmitter {

socket.on('connect', () => {
this._readyState = READY_STATE.CONNECTED
this.emit('connect')
this.emit('connect', true, this._socket)
})

socket.on('data', (buffer: Buffer) => {
this._awaitingResponse = false
this._handler(buffer)
})

Expand Down

0 comments on commit cb8d30d

Please sign in to comment.