-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracker-tests.html
More file actions
606 lines (535 loc) · 21.8 KB
/
tracker-tests.html
File metadata and controls
606 lines (535 loc) · 21.8 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BAC Tracker - Core Algorithm Tests</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Consolas', 'Monaco', 'Lucida Console', monospace;
background: linear-gradient(180deg, #ffffff 0%, #f0f4f8 100%);
padding: 24px;
font-size: 14px;
line-height: 1.6;
}
.container {
max-width: 1000px;
margin: 0 auto;
}
h1 {
font-size: 2rem;
color: #1f2937;
margin-bottom: 24px;
text-align: center;
}
.test-suite {
background: white;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
padding: 24px;
margin-bottom: 16px;
}
.test-suite h2 {
font-size: 1.3rem;
color: #14b8a6;
margin-bottom: 16px;
border-bottom: 2px solid #14b8a6;
padding-bottom: 8px;
}
.test-case {
background: #f9fafb;
padding: 12px;
border-radius: 6px;
margin-bottom: 8px;
border-left: 4px solid #9ca3af;
}
.test-case.pass {
border-left-color: #10b981;
background: #ecfdf5;
}
.test-case.fail {
border-left-color: #ef4444;
background: #fee2e2;
}
.test-name {
font-weight: bold;
margin-bottom: 4px;
}
.test-result {
font-size: 12px;
color: #6b7280;
}
.test-error {
color: #dc2626;
font-size: 12px;
margin-top: 4px;
font-weight: bold;
}
.summary {
background: #1f2937;
color: white;
padding: 16px;
border-radius: 8px;
text-align: center;
font-size: 1.2rem;
margin-bottom: 24px;
}
.summary.all-pass {
background: #10b981;
}
.summary.some-fail {
background: #ef4444;
}
button {
background: #14b8a6;
color: white;
border: none;
padding: 12px 24px;
font-size: 16px;
font-family: inherit;
font-weight: bold;
border-radius: 6px;
cursor: pointer;
width: 100%;
margin-bottom: 24px;
}
button:hover {
background: #0d9488;
}
</style>
</head>
<body>
<div class="container">
<h1>BAC Tracker - Core Algorithm Tests</h1>
<button id="runTests">Run All Tests</button>
<div id="summary"></div>
<div id="results"></div>
</div>
<script>
// Test framework
class TestRunner {
constructor() {
this.suites = [];
this.totalTests = 0;
this.passedTests = 0;
this.failedTests = 0;
}
suite(name, tests) {
this.suites.push({ name, tests });
}
run() {
this.totalTests = 0;
this.passedTests = 0;
this.failedTests = 0;
const resultsContainer = document.getElementById('results');
resultsContainer.innerHTML = '';
this.suites.forEach(suite => {
const suiteEl = document.createElement('div');
suiteEl.className = 'test-suite';
suiteEl.innerHTML = `<h2>${suite.name}</h2>`;
suite.tests.forEach(test => {
this.totalTests++;
const testEl = document.createElement('div');
testEl.className = 'test-case';
try {
const result = test.fn();
if (result.pass) {
this.passedTests++;
testEl.classList.add('pass');
testEl.innerHTML = `
<div class="test-name">✓ ${test.name}</div>
<div class="test-result">${result.message || 'Pass'}</div>
`;
} else {
this.failedTests++;
testEl.classList.add('fail');
testEl.innerHTML = `
<div class="test-name">✗ ${test.name}</div>
<div class="test-error">${result.message || 'Failed'}</div>
`;
}
} catch (error) {
this.failedTests++;
testEl.classList.add('fail');
testEl.innerHTML = `
<div class="test-name">✗ ${test.name}</div>
<div class="test-error">Error: ${error.message}</div>
`;
}
suiteEl.appendChild(testEl);
});
resultsContainer.appendChild(suiteEl);
});
this.showSummary();
}
showSummary() {
const summaryEl = document.getElementById('summary');
const passRate = ((this.passedTests / this.totalTests) * 100).toFixed(1);
summaryEl.className = 'summary';
if (this.failedTests === 0) {
summaryEl.classList.add('all-pass');
summaryEl.innerHTML = `✓ All ${this.totalTests} tests passed (100%)`;
} else {
summaryEl.classList.add('some-fail');
summaryEl.innerHTML = `${this.passedTests}/${this.totalTests} tests passed (${passRate}%) - ${this.failedTests} failed`;
}
}
}
// Helper function for approximate equality
function approxEqual(actual, expected, tolerance = 0.0001) {
return Math.abs(actual - expected) < tolerance;
}
// Core algorithm classes (copied from tracker.html for testing)
class BACCalculator {
constructor() {
this.widmarkRatio = {
male: 0.68,
female: 0.55
};
this.vmaxByGender = {
male: 0.017,
female: 0.013
};
this.km = 0.015;
}
getAbsorptionFactor(minutesSinceDrink, stomachFullness = 'empty', beverageType = 'wine') {
const lagByFullness = {
empty: 12,
moderate: 35,
full: 75
};
const lambdaByBeverage = {
beer: 45,
wine: 35,
seltzer: 30,
spirits: 25
};
const lag = lagByFullness[stomachFullness] || lagByFullness.empty;
const lambda = lambdaByBeverage[beverageType] || lambdaByBeverage.wine;
const k = 2.8;
if (minutesSinceDrink < lag) {
return 0;
}
const t = minutesSinceDrink - lag;
const absorption = 1 - Math.exp(-Math.pow(t / lambda, k));
return Math.min(absorption, 1.0);
}
getAbsorptionRate(minutesSinceDrink, stomachFullness = 'empty', beverageType = 'wine') {
const lagByFullness = {
empty: 12,
moderate: 35,
full: 75
};
const lambdaByBeverage = {
beer: 45,
wine: 35,
seltzer: 30,
spirits: 25
};
const lag = lagByFullness[stomachFullness] || lagByFullness.empty;
const lambda = lambdaByBeverage[beverageType] || lambdaByBeverage.wine;
const k = 2.8;
if (minutesSinceDrink < lag) {
return 0;
}
const t = minutesSinceDrink - lag;
const rate = (k / lambda) * Math.pow(t / lambda, k - 1) * Math.exp(-Math.pow(t / lambda, k));
return rate;
}
calculateGramsAlcohol(volumeOz, abv) {
const mlPerOz = 29.5735;
const alcoholDensity = 0.789;
const volumeMl = volumeOz * mlPerOz;
const gramsAlcohol = volumeMl * (abv / 100) * alcoholDensity;
return gramsAlcohol;
}
calculateWidmarkBAC(gramsAlcohol, weightLbs, gender) {
const weightKg = weightLbs * 0.453592;
const r = this.widmarkRatio[gender];
return gramsAlcohol / (weightKg * r * 10);
}
}
// Initialize test runner
const runner = new TestRunner();
// Test Suite 1: Weibull Distribution
runner.suite('Weibull Distribution (Absorption)', [
{
name: 'Absorption is zero before lag time',
fn: () => {
const calc = new BACCalculator();
const absorption = calc.getAbsorptionFactor(5, 'empty', 'wine');
return {
pass: absorption === 0,
message: `Absorption at 5 min (lag=12): ${absorption} (expected 0)`
};
}
},
{
name: 'Absorption starts after lag time',
fn: () => {
const calc = new BACCalculator();
const absorption = calc.getAbsorptionFactor(10, 'empty', 'wine');
return {
pass: absorption > 0 && absorption < 1,
message: `Absorption at 10 min: ${absorption.toFixed(4)} (expected > 0 and < 1)`
};
}
},
{
name: 'Absorption approaches 1.0 at long times',
fn: () => {
const calc = new BACCalculator();
const absorption = calc.getAbsorptionFactor(300, 'empty', 'wine');
return {
pass: absorption > 0.99,
message: `Absorption at 300 min: ${absorption.toFixed(4)} (expected > 0.99)`
};
}
},
{
name: 'Absorption rate is zero before lag',
fn: () => {
const calc = new BACCalculator();
const rate = calc.getAbsorptionRate(5, 'empty', 'wine');
return {
pass: rate === 0,
message: `Rate at 5 min (lag=12): ${rate} (expected 0)`
};
}
},
{
name: 'Absorption rate peaks then decreases',
fn: () => {
const calc = new BACCalculator();
const rate30 = calc.getAbsorptionRate(30, 'empty', 'wine');
const rate60 = calc.getAbsorptionRate(60, 'empty', 'wine');
const rate150 = calc.getAbsorptionRate(150, 'empty', 'wine');
const peakIsBetween = rate60 > rate30 && rate60 > rate150;
return {
pass: peakIsBetween,
message: `Rates at 30/60/150 min: ${rate30.toFixed(5)}/${rate60.toFixed(5)}/${rate150.toFixed(5)} (peak should be around 60 min)`
};
}
},
{
name: 'Spirits absorb faster than beer (smaller lambda)',
fn: () => {
const calc = new BACCalculator();
const spiritsAbsorption = calc.getAbsorptionFactor(30, 'empty', 'spirits');
const beerAbsorption = calc.getAbsorptionFactor(30, 'empty', 'beer');
return {
pass: spiritsAbsorption > beerAbsorption,
message: `At 30 min - Spirits: ${spiritsAbsorption.toFixed(4)}, Beer: ${beerAbsorption.toFixed(4)}`
};
}
},
{
name: 'Full stomach delays absorption (larger lag)',
fn: () => {
const calc = new BACCalculator();
const emptyAbsorption = calc.getAbsorptionFactor(15, 'empty', 'wine');
const fullAbsorption = calc.getAbsorptionFactor(15, 'full', 'wine');
return {
pass: emptyAbsorption > fullAbsorption,
message: `At 15 min - Empty: ${emptyAbsorption.toFixed(4)}, Full: ${fullAbsorption.toFixed(4)}`
};
}
}
]);
// Test Suite 2: Alcohol Calculations
runner.suite('Alcohol Grams & Widmark Conversion', [
{
name: 'Standard beer (12oz, 5% ABV) = ~14g alcohol',
fn: () => {
const calc = new BACCalculator();
const grams = calc.calculateGramsAlcohol(12, 5);
return {
pass: approxEqual(grams, 14.0, 0.5),
message: `12oz @ 5% = ${grams.toFixed(2)}g (expected ~14g)`
};
}
},
{
name: 'Standard wine (5oz, 12% ABV) = ~14g alcohol',
fn: () => {
const calc = new BACCalculator();
const grams = calc.calculateGramsAlcohol(5, 12);
return {
pass: approxEqual(grams, 14.0, 0.5),
message: `5oz @ 12% = ${grams.toFixed(2)}g (expected ~14g)`
};
}
},
{
name: 'Standard shot (1.5oz, 40% ABV) = ~14g alcohol',
fn: () => {
const calc = new BACCalculator();
const grams = calc.calculateGramsAlcohol(1.5, 40);
return {
pass: approxEqual(grams, 14.0, 0.5),
message: `1.5oz @ 40% = ${grams.toFixed(2)}g (expected ~14g)`
};
}
},
{
name: 'Widmark formula: 14g for 180lb male ≈ 0.025% BAC',
fn: () => {
const calc = new BACCalculator();
const bac = calc.calculateWidmarkBAC(14, 180, 'male');
return {
pass: approxEqual(bac, 0.025, 0.005),
message: `14g / 180lb male = ${(bac * 100).toFixed(3)}% (expected ~0.025%)`
};
}
},
{
name: 'Females have lower distribution ratio (higher BAC)',
fn: () => {
const calc = new BACCalculator();
const maleBac = calc.calculateWidmarkBAC(14, 150, 'male');
const femaleBac = calc.calculateWidmarkBAC(14, 150, 'female');
return {
pass: femaleBac > maleBac,
message: `14g / 150lb - Male: ${(maleBac * 100).toFixed(3)}%, Female: ${(femaleBac * 100).toFixed(3)}%`
};
}
}
]);
// Test Suite 3: Michaelis-Menten Kinetics
runner.suite('Michaelis-Menten Enzyme Kinetics', [
{
name: 'At very low BAC: first-order kinetics (rate ∝ BAC)',
fn: () => {
const calc = new BACCalculator();
const Vmax = 0.017;
const Km = 0.015;
const bac1 = 0.001;
const bac2 = 0.002;
const rate1 = (Vmax * bac1) / (Km + bac1);
const rate2 = (Vmax * bac2) / (Km + bac2);
const ratio = rate2 / rate1;
return {
pass: approxEqual(ratio, 2.0, 0.1),
message: `Rate ratio (BAC doubled): ${ratio.toFixed(3)} (expected ~2.0 for first-order)`
};
}
},
{
name: 'At high BAC: zero-order kinetics (rate ≈ Vmax)',
fn: () => {
const Vmax = 0.017;
const Km = 0.015;
const bac = 0.15;
const rate = (Vmax * bac) / (Km + bac);
const percentOfVmax = (rate / Vmax) * 100;
return {
pass: percentOfVmax > 90,
message: `At BAC=0.15%: rate = ${rate.toFixed(4)} (${percentOfVmax.toFixed(1)}% of Vmax)`
};
}
},
{
name: 'At BAC = Km: rate = Vmax/2 (definition of Km)',
fn: () => {
const Vmax = 0.017;
const Km = 0.015;
const bac = Km;
const rate = (Vmax * bac) / (Km + bac);
return {
pass: approxEqual(rate, Vmax / 2, 0.0001),
message: `At BAC=Km: rate = ${rate.toFixed(4)}, Vmax/2 = ${(Vmax/2).toFixed(4)}`
};
}
},
{
name: 'Males metabolize ~30% faster than females (Vmax difference)',
fn: () => {
const calc = new BACCalculator();
const maleVmax = calc.vmaxByGender.male;
const femaleVmax = calc.vmaxByGender.female;
const ratio = maleVmax / femaleVmax;
return {
pass: approxEqual(ratio, 1.31, 0.05),
message: `Male/Female Vmax ratio: ${ratio.toFixed(2)} (expected ~1.31)`
};
}
},
{
name: 'Enzyme saturation is nonlinear',
fn: () => {
const Vmax = 0.017;
const Km = 0.015;
const bac1 = 0.05;
const bac2 = 0.10;
const rate1 = (Vmax * bac1) / (Km + bac1);
const rate2 = (Vmax * bac2) / (Km + bac2);
const ratio = rate2 / rate1;
return {
pass: ratio < 2.0 && ratio > 1.0,
message: `BAC doubled (0.05→0.10), rate ratio: ${ratio.toFixed(3)} (expected < 2 due to saturation)`
};
}
}
]);
// Test Suite 4: Integration Test
runner.suite('Full Integration Test (Single Drink)', [
{
name: 'Peak BAC occurs during absorption phase (not immediately)',
fn: () => {
const calc = new BACCalculator();
// Simulate one beer: 12oz @ 5%
const gramsAlcohol = calc.calculateGramsAlcohol(12, 5);
const profile = { weightLbs: 180, gender: 'male' };
const weightKg = profile.weightLbs * 0.453592;
const r = calc.widmarkRatio.male;
let peakBAC = 0;
let peakTime = 0;
// Simulate 3 hours
for (let minute = 0; minute < 180; minute++) {
const absorptionFactor = calc.getAbsorptionFactor(minute, 'empty', 'beer');
const absorbedGrams = gramsAlcohol * absorptionFactor;
const theoreticalBAC = absorbedGrams / (weightKg * r * 10);
if (theoreticalBAC > peakBAC) {
peakBAC = theoreticalBAC;
peakTime = minute;
}
}
return {
pass: peakTime > 30 && peakTime < 120,
message: `Peak at ${peakTime} minutes (expected 30-120 min for beer)`
};
}
},
{
name: 'Two drinks have higher peak than one drink',
fn: () => {
const calc = new BACCalculator();
const gramsPerDrink = calc.calculateGramsAlcohol(12, 5);
const profile = { weightLbs: 180, gender: 'male' };
const weightKg = profile.weightLbs * 0.453592;
const r = calc.widmarkRatio.male;
const oneDrinkPeak = (gramsPerDrink * calc.getAbsorptionFactor(60, 'empty', 'beer')) / (weightKg * r * 10);
const twoDrinkPeak = (gramsPerDrink * 2 * calc.getAbsorptionFactor(60, 'empty', 'beer')) / (weightKg * r * 10);
return {
pass: twoDrinkPeak > oneDrinkPeak * 1.9,
message: `One drink: ${(oneDrinkPeak * 100).toFixed(3)}%, Two drinks: ${(twoDrinkPeak * 100).toFixed(3)}%`
};
}
}
]);
// Run tests on button click
document.getElementById('runTests').addEventListener('click', () => {
runner.run();
});
// Auto-run on page load
window.addEventListener('load', () => {
runner.run();
});
</script>
</body>
</html>