Repository navigation
Expand file tree
/
Copy pathtest.js
More file actions
59 lines (53 loc) · 2.24 KB
/
Copy pathtest.js
File metadata and controls
59 lines (53 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import test from 'node:test';
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import {
AgePolicy,
CholesterolUnit,
calculateLab,
calculateNonLab,
calculateNonLabIfStopsSmoking,
classifyRisk,
parseCholesterol,
parseSex,
} from './index.js';
test('parses decimal commas and rejects non-finite values', () => {
assert.equal(parseCholesterol('5,2', CholesterolUnit.MMOL_L).value, 5.2);
assert.throws(() => parseCholesterol('NaN'), /finite/);
});
test('requires supported sex, boolean, unit, and age inputs', () => {
assert.equal(parseSex('laki-laki'), 'male');
assert.throws(() => parseSex('unknown'), /unsupported/);
assert.throws(() => calculateNonLab({ age: 55, sex: 'male', systolicBp: 120, smoker: 'yes', bmi: 20 }), /boolean/);
assert.throws(() => calculateLab({ age: 55, sex: 'male', systolicBp: 120, smoker: false, diabetes: false, totalCholesterol: 200, cholesterolUnit: 'unknown' }), /unsupported/);
assert.throws(() => calculateNonLab({ age: 39, sex: 'male', systolicBp: 120, smoker: false, bmi: 20 }), /40-74/);
});
test('makes endpoint age mapping explicit', () => {
const result = calculateNonLab({
age: 39,
sex: 'male',
systolicBp: 120,
smoker: false,
bmi: 20,
agePolicy: AgePolicy.NEAREST_ENDPOINT,
});
assert.equal(result.usesAgeEndpoint, true);
assert.equal(result.effectiveAge, 40);
});
test('matches every shared cross-runtime golden case', async () => {
const fixtures = JSON.parse(await readFile(new URL('../../fixtures/golden-cases.json', import.meta.url), 'utf8'));
for (const fixture of fixtures) {
const result = fixture.mode === 'lab' ? calculateLab(fixture.input) : calculateNonLab(fixture.input);
assert.equal(result.risk, fixture.expectedRisk, fixture.id);
assert.ok(result.disclaimer);
}
});
test('keeps source-compatible what-if behavior deterministic', () => {
const input = { age: 55, sex: 'male', systolicBp: 145, smoker: true, bmi: 28 };
assert.ok(calculateNonLabIfStopsSmoking(input).risk <= calculateNonLab(input).risk);
});
test('rejects invalid risk fractions', () => {
assert.equal(classifyRisk(0.12), 'high');
assert.throws(() => classifyRisk(Number.NaN), /finite/);
assert.throws(() => classifyRisk(-0.1), /between/);
});