Skip to content

Commit

Permalink
fix: another fix is parsing batch
Browse files Browse the repository at this point in the history
* two or more MSH in the Batch function should work
* if one MSH is passed in Batch, fail
* moved unit tests for sanity checks to new file
* updated unit tests
* using HL&FatalError class for issues inside normalizedBuilder.ts and normalizedClient.ts
* added jest verbose as an option
  • Loading branch information
Bugs5382 committed Jan 11, 2024
1 parent d605d42 commit 2037298
Show file tree
Hide file tree
Showing 9 changed files with 359 additions and 261 deletions.
15 changes: 15 additions & 0 deletions .github/pr-title-checker-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"LABEL": {
"name": "title needs formatting",
"color": "EEEEEE"
},
"CHECKS": {
"prefixes": ["[Bot] docs: "],
"regexp": "^(feat|fix|docs|test|ci|chore)!?(\\(.*\\))?!?:.*"
},
"MESSAGES": {
"success": "PR title is valid",
"failure": "PR title is invalid",
"notice": "PR Title needs to pass regex '^(feat|fix|docs|test|ci|chore)!?(\\(.*\\))?!?:.*"
}
}
29 changes: 29 additions & 0 deletions .github/workflows/pr-title-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: "Lint PR"

on:
pull_request_target:
types: [opened, edited, reopened, synchronize]

# IMPORTANT: No checkout actions, scripts, or builds should be added to this workflow. Permissions should always be used
# with extreme caution. https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_target
permissions: {}

# PR updates can happen in quick succession, leading to this
# workflow being trigger a number of times. This limits it
# to one run per PR.
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}


jobs:
validate:
permissions:
contents: read
pull-requests: read
name: Validate PR Title
runs-on: ubuntu-latest
steps:
- uses: thehanimo/pr-title-checker@0cf5902181e78341bb97bb06646396e5bd354b3f # v1.4.0
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
configuration_path: ".github/pr-title-checker-config.json"
7 changes: 7 additions & 0 deletions __tests__/__data__/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { HL7_2_7_MSH } from '../../src/specification/2.7'

export const MSH_HEADER: HL7_2_7_MSH = {
msh_9_1: 'ADT',
msh_9_2: 'A01',
msh_11_1: 'D'
}
247 changes: 3 additions & 244 deletions __tests__/hl7.build.test.ts
Original file line number Diff line number Diff line change
@@ -1,133 +1,12 @@
import { randomUUID } from 'crypto'
import fs from 'fs'
import path from 'path'
import {
FileBatch, Batch, Message, createHL7Date, isBatch, isFile
, HL7Node, EmptyNode
} from '../src'
import { HL7_2_7_MSH, HL7_2_1, HL7_2_7, HL7_2_2, HL7_2_3, HL7_2_3_1, HL7_2_4, HL7_2_5, HL7_2_5_1, HL7_2_6, HL7_2_7_1, HL7_2_8 } from '../src/hl7'
import { FileBatch, Batch, Message, createHL7Date, HL7Node, EmptyNode } from '../src'
import { HL7_2_1, HL7_2_7, HL7_2_2, HL7_2_3, HL7_2_3_1, HL7_2_4, HL7_2_5, HL7_2_5_1, HL7_2_6, HL7_2_7_1, HL7_2_8 } from '../src/hl7'
import {MSH_HEADER} from "./__data__/constants";
import { sleep } from './__utils__'

const MSH_HEADER: HL7_2_7_MSH = {
msh_9_1: 'ADT',
msh_9_2: 'A01',
msh_11_1: 'D'
}

describe('node hl7 client - builder tests', () => {
describe('sanity checks', () => {
test('error - Message Object - nothing passed', async () => {
try {
const message = new Message()
message.set('MSH.5', '1')
} catch (err) {
expect(err).toEqual(new Error('mshHeader must be set if no HL7 message is being passed.'))
}
})

test('error - Message Object - text empty ', async () => {
try {
new Message({ text: '' })
} catch (err) {
expect(err).toEqual(new Error('mshHeader must be set if no HL7 message is being passed.'))
}
})

test('error - Message Object - text must start with MSH ', async () => {
try {
new Message({ text: 'PV1|||||||^Jones\r' })
} catch (err) {
expect(err).toEqual(new Error('text must begin with the MSH segment.'))
}
})

test('error - Message Object - msh 9.1 is empty ', async () => {
try {
new Message({
// @ts-expect-error 9.1 should be not empty
messageHeader: {
msh_9_1: ''
}
})
} catch (err) {
expect(err).toEqual(new Error('MSH.9.1 & MSH 9.2 must be defined.'))
}
})

test('error - Message Object - msh 9.2 is empty ', async () => {
try {
new Message({
// @ts-expect-error 9.2 should be not empty
messageHeader: {
msh_9_1: 'ADT',
msh_9_2: ''
}
})
} catch (err) {
expect(err).toEqual(new Error('MSH.9.2 must be 3 characters in length.'))
}
})

test('error - Message Object - msh 9.1 is not 3 character long ', async () => {
try {
new Message({
// @ts-expect-error not filling this out for unit testing
messageHeader: {
msh_9_1: 'ADTY',
msh_9_2: 'A01',
msh_10: '123456'
}
})
} catch (err) {
expect(err).toEqual(new Error('MSH.9.1 must be 3 characters in length.'))
}
})

test('error - Message Object - msh 9.2 is not 3 character long ', async () => {
try {
new Message({
// @ts-expect-error not filling this out for unit testing
messageHeader: {
msh_9_1: 'ADT',
msh_9_2: 'A01Y',
msh_10: '123456'
}
})
} catch (err) {
expect(err).toEqual(new Error('MSH.9.2 must be 3 characters in length.'))
}
})

test('error - Message Object - msh 10 is more than 199 characters ', async () => {
try {
new Message({
// @ts-expect-error not filling this out for unit testing
messageHeader: {
msh_9_1: 'ADT',
msh_9_2: 'A01',
msh_10: 'AaasdfjlsdjflskdfsdflkjjsdlkflkasdflsjdflkjasdlfjsldkjlskjdfksajflksjdfAaasdfjlsdjflskdfsdflkjjsdlkflkasdflsjdflkjasdlfjsldkjlskjdfksajflksjdfAaasdfjlsdjflskdfsdflkjjsdlkflkasdflsjdflkjasdlfjsldkjlskjdfksajflksjdf'
}
})
} catch (err) {
expect(err).toEqual(new Error('MSH.10 must be greater than 0 and less than 199 characters.'))
}
})

test('error - Message Object - msh 10 can not be blank', async () => {
try {
new Message({
// @ts-expect-error not filling this out for unit testing
messageHeader: {
msh_9_1: 'ADT',
msh_9_2: 'A01',
msh_10: ''
}
})
} catch (err) {
expect(err).toEqual(new Error('MSH.10 must be greater than 0 and less than 199 characters.'))
}
})
})

describe('build message basics', () => {
let message: Message
Expand Down Expand Up @@ -904,126 +783,6 @@ describe('node hl7 client - builder tests', () => {
expect(count).toBe(2)
})

test('...isFile - on Message - false', async () => {
const message = new Message({
messageHeader: {
...MSH_HEADER,
msh_10: 'CONTROL_ID'
}
})
expect(isFile(message.toString())).toBe(false)
})

test('...isBatch - on Message - false', async () => {
const message = new Message({
messageHeader: {
...MSH_HEADER,
msh_10: 'CONTROL_ID'
}
})
expect(isBatch(message.toString())).toBe(false)
})

test('...isBatch -should be false', async () => {
const batch = new Batch()
batch.start()
batch.end()
expect(isFile(batch.toString())).toBe(false)
})

test('...isBatch - should now be true', async () => {
const batch = new Batch()
batch.start()
batch.end()
expect(isBatch(batch.toString())).toBe(true)
})

test('...isFile - should now be true', async () => {
const file = new FileBatch()
file.start()
file.end()
expect(isFile(file.toString())).toBe(true)
})
})

describe('parse message, batch, and file', () => {
const hl7_string: string = 'MSH|^~\\&|||||20081231||ADT^A01^ADT_A01|12345||2.7\rEVN||20081231'
const hl7_batch_msh_string: string = 'MSH|^~\\&|||||20081231||ADT^A01^ADT_A01|12345||2.7\rEVN||20081231\rMSH|^~\\&|||||20081231||ADT^A01^ADT_A01|12345||2.7\rEVN||20081231'
const hl7_non_standard: string = 'MSH:-+?*:field2:field3component1-field3component2:field4repeat1+field4repeat2:field5subcomponent1*field5subcomponent2:field6?R?'
const hl7_batch: string = 'BHS|^~\\&|||||20231208\rMSH|^~\\&|||||20231208||ADT^A01^ADT_A01|CONTROL_ID||2.7\rEVN||20081231\rEVN||20081231\rBTS|1'
const hl7_batch_non_standard: string = 'BHS:-+?*:::::20231208\rMSH|^~\\&|||||20231208||ADT^A01^ADT_A01|CONTROL_ID||2.7\rEVN||20081231\rEVN||20081231\rBTS|1'


test('...verify MSH input', async () => {
const message = new Message({ text: hl7_string })

expect(message.toString().substring(0, 8)).toBe('MSH|^~\\&')
expect(message.get('MSH.3').exists('')).toBe(false)
expect(message.get('MSH.9.1').toString()).toBe('ADT')
expect(message.get('MSH.9.2').toString()).toBe('A01')
expect(message.get('MSH.9.3').toString()).toBe('ADT_A01')
expect(message.get('MSH.10').toString()).toBe('12345')
expect(message.get('MSH.11.1').toString()).toBe('')
expect(message.get('MSH.12').toString()).toBe('2.7')
expect(message.get('EVN.2').toString()).toBe('20081231')

let count: number = 0
message.get('EVN').forEach((segment: HL7Node): void => {
expect(segment.name).toBe('EVN')
count++
})

expect(count).toBe(1)
})

test('...parse non standard delimiters defined in MSH header', async () => {
const message = new Message({ text: hl7_non_standard })

expect(message.get('MSH.3').toString()).toBe('field2')
expect(message.get('MSH.4.2').toString()).toBe('field3component2')
expect(message.get('MSH.5').get(0).toString()).toBe('field4repeat1')
expect(message.get('MSH.5').get(1).toString()).toBe('field4repeat2')
expect(message.get('MSH.6.1.1').toString()).toBe('field5subcomponent1')
expect(message.get('MSH.6.1.2').toString()).toBe('field5subcomponent2')
expect(message.get('MSH.7').toString()).toBe('field6+')
})

test('...verify BHS input', async () => {
const batch = new Batch({ text: hl7_batch })

expect(batch.toString().substring(0, 8)).toBe('BHS|^~\\&')
})

test('...parse non standard delimiters defined in BHS header', async () => {
const batch = new Batch({ text: hl7_batch_non_standard })

expect(batch.toString().substring(0, 8)).toBe('BHS:-+?*')
})

test('...verify BHS input ... 1 message should exist ... 2 EVN segments inside', async () => {
const batch = new Batch({ text: hl7_batch })
const messages = batch.messages()
expect(messages.length).toBe(1)

messages.forEach((message: Message): void => {
let count: number = 0
message.get('EVN').forEach((segment: HL7Node): void => {
expect(segment.name).toBe('EVN')
count++
})
expect(count).toBe(2)
})
})

test('...should be used as a batch', async () => {
try {
// @ts-expect-error
const message = new Message({ text: hl7_batch_msh_string })
}catch (err) {
expect(err).toEqual(new Error('Multiple MSH segments found. Use Batch.'))
}
})

})

describe('file tests', () => {
Expand Down
Loading

0 comments on commit 2037298

Please sign in to comment.