diff --git a/tests/utils.test.js b/tests/utils.test.js new file mode 100644 index 0000000..f0d5d60 --- /dev/null +++ b/tests/utils.test.js @@ -0,0 +1,76 @@ +/* + Neonatal jaundice treatment threshold calculator + Copyright (C) 2024 Lee Yingtong Li + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +// Import bilirubin_lib.js +const fs = require('fs'); +eval(fs.readFileSync('src/bilirubin_lib.js').toString()); + +const assert = require('node:assert'); +const { suite, test } = require('node:test'); + +suite('Hours display', () => { + test('0 hours', () => { + assert.equal(prettyPrintHours(0), '0 hours'); + assert.equal(prettyPrintHours(0.2/24), '0 hours'); + assert.equal(prettyPrintHours(0.49/24), '0 hours'); + }); + + test('1 hour', () => { + assert.equal(prettyPrintHours(0.5/24), '1 hour'); + assert.equal(prettyPrintHours(1/24), '1 hour'); + assert.equal(prettyPrintHours(1.49/24), '1 hour'); + }); + + test('2-23 hours', () => { + for (let h = 2; h <= 23; h++) { + assert.equal(prettyPrintHours((h - 0.5) / 24), h + ' hours'); + assert.equal(prettyPrintHours(h / 24), h + ' hours'); + assert.equal(prettyPrintHours((h + 0.49) / 24), h + ' hours'); + } + }); + + test('>= 24 hours', () => { + assert.throws(() => prettyPrintHours(23.5/24)); + assert.throws(() => prettyPrintHours(23.99/24)); + assert.throws(() => prettyPrintHours(1)); + assert.throws(() => prettyPrintHours(1.1)); + assert.throws(() => prettyPrintHours(7)); + }); +}); + +suite('Days and hours display', () => { + test('0 days', () => { + assert.equal(prettyPrintDays(0), '0 hours'); + assert.equal(prettyPrintDays(0.5/24), '1 hour'); + assert.equal(prettyPrintDays(23.49/24), '23 hours'); + }); + + test('1 day', () => { + assert.equal(prettyPrintDays(1), '1 day, 0 hours'); + assert.equal(prettyPrintDays(1 + 0.51/24), '1 day, 1 hour'); + assert.equal(prettyPrintDays(1 + 23.49/24), '1 day, 23 hours'); + }); + + test('2-14 days', () => { + for (let d = 2; d <= 14; d++) { + assert.equal(prettyPrintDays(d), d + ' days, 0 hours'); + assert.equal(prettyPrintDays(d + 0.51/24), d + ' days, 1 hour'); + assert.equal(prettyPrintDays(d + 23.49/24), d + ' days, 23 hours'); + } + }); +});