Skip to content

Commit

Permalink
refactor: improve basic bill validator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
siiky committed Dec 11, 2024
1 parent 77167f0 commit c28c577
Showing 1 changed file with 55 additions and 9 deletions.
64 changes: 55 additions & 9 deletions lib/hardware-testing/bill-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,41 @@ const BOXES = {
recyclers: 0,
}

const addEventListeners = (validator, reject, eventListenersToAdd) => {
const allEvents = [
'actionRequiredMaintenance',
'billsAccepted',
'billsRead',
'billsRejected',
'billsValid',
'cashSlotRemoveBills',
'disconnected',
'enabled',
'error',
'jam',
'leftoverBillsInCashSlot',
'stackerClosed',
'stackerOpen',
'standby',
]

const handleUnexpected = eventName => (...eventArguments) => {
const error = new Error("Unexpected event received")
error.eventName = eventName
error.eventArguments = [...eventArguments]
reject(error)
}

allEvents.forEach(
ev => validator.on(ev, eventListenersToAdd[ev] ?? handleUnexpected)
)
}

const doFinally = validator => () => {
validator.removeAllListeners()
validator.disable()
}

const testEnable = validator => () => new Promise(
(resolve, reject) => {
let accepted = false
Expand All @@ -23,14 +58,15 @@ const testEnable = validator => () => new Promise(
validator.enable()
}

validator.on('billsAccepted', () => { accepted = true })
validator.on('billsRead', bills => resolve({ accepted, bills }))
validator.on('billsRejected', () => enable())
validator.on('error', error => reject({ accepted, error }))
addEventListeners(validator, reject, {
billsAccepted: () => { accepted = true },
billsRead: bills => resolve({ accepted, bills }),
billsRejected: () => enable(),
})

enable()
}
).finally(() => validator.removeAllListeners())
).finally(doFinally(validator))

const steps = () =>
new Promise((resolve, reject) => {
Expand All @@ -51,9 +87,14 @@ const steps = () =>

{
name: 'reject',
instructionMessage: `Try to insert a ${FIAT_CODE} bill...`,
confirmationMessage: "Did the validator reject the bill?",
test: () => validator.reject(),
test: () =>
new Promise((resolve, reject) => {
addEventListeners(validator, reject, {
billsRejected: () => resolve(),
})
validator.reject()
}).finally(doFinally(validator)),
},

{
Expand All @@ -65,9 +106,14 @@ const steps = () =>

{
name: 'stack',
instructionMessage: `Try to insert a ${FIAT_CODE} bill...`,
confirmationMessage: "Did the validator stack the bill?",
test: () => validator.stack(),
test: () =>
new Promise((resolve, reject) => {
addEventListeners(validator, reject, {
billsValid: () => resolve(),
})
validator.stack()
}).finally(doFinally(validator)),
},
])

Expand Down

0 comments on commit c28c577

Please sign in to comment.