bilirubin-calculator/tests/utils.test.js

99 lines
3.3 KiB
JavaScript

/*
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 <https://www.gnu.org/licenses/>.
*/
// 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');
}
});
});
suite('Bilirubin display', () => {
test('< 10 micromol/L', () => {
assert.equal(prettyPrintBilirubin(0), '0.0');
assert.equal(prettyPrintBilirubin(0.420), '0.4');
assert.equal(prettyPrintBilirubin(3.1415), '3.1');
assert.equal(prettyPrintBilirubin(3.99), '4.0');
});
test('>= 10 micromol/L', () => {
assert.equal(prettyPrintBilirubin(42), '42');
assert.equal(prettyPrintBilirubin(90.210), '90');
assert.equal(prettyPrintBilirubin(90.99), '91');
});
});
suite('Date to ISO string', () => {
test('Date to ISO string', () => {
let date = new Date(2024, 11, 25, 4, 20, 31, 415); // Wed Dec 25 2024 04:20:31 GMT+1100 (Australian Eastern Daylight Time)
assert.equal(dateToISOStringLocal(date), '2024-12-25T04:20');
});
});