Initial commit of counter
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
'use strict';
|
||||
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const {
|
||||
bmrKcal, kcalFromSteps, kcalFromExercise, dailyTargets,
|
||||
imperialToMetric, metricToImperial, roundLbWeek,
|
||||
KCAL_PER_LB, KCAL_PER_KG, LB_PER_KG,
|
||||
} = require('../server/energy');
|
||||
|
||||
test('imperial <-> metric conversions', () => {
|
||||
const m = imperialToMetric({ feet: 5, inches: 9, pounds: 165 });
|
||||
assert.equal(m.heightCm, Math.round(69 * 2.54 * 100) / 100);
|
||||
assert.equal(m.weightKg, Math.round(165 / 2.20462 * 100) / 100);
|
||||
const imp = metricToImperial({ weightKg: 75, heightCm: 175 });
|
||||
assert.equal(imp.pounds, Math.round(75 * 2.20462 * 10) / 10);
|
||||
assert.equal(imp.feet, Math.floor(Math.round(175 / 2.54) / 12));
|
||||
assert.equal(imp.inches, Math.round(175 / 2.54) % 12);
|
||||
});
|
||||
|
||||
test('Mifflin-St Jeor BMR for men and women (metric inputs)', () => {
|
||||
assert.equal(bmrKcal({ sex: 'male', weightKg: 75, heightCm: 175, age: 30 }), 1699);
|
||||
assert.equal(bmrKcal({ sex: 'female', weightKg: 60, heightCm: 165, age: 30 }), 1320);
|
||||
});
|
||||
|
||||
test('BMR from imperial profile rounds to same value', () => {
|
||||
const m = imperialToMetric({ feet: 5, inches: 9, pounds: 165 });
|
||||
assert.equal(bmrKcal({ sex: 'male', weightKg: m.weightKg, heightCm: m.heightCm, age: 30 }), 1699);
|
||||
});
|
||||
|
||||
test('step calories scale with body weight', () => {
|
||||
assert.equal(kcalFromSteps(10000, 75), 375);
|
||||
assert.equal(kcalFromSteps(10000, 60), 300);
|
||||
assert.equal(kcalFromSteps(0, 75), 0);
|
||||
});
|
||||
|
||||
test('exercise calories use MET values', () => {
|
||||
assert.equal(kcalFromExercise({ met: 8, minutes: 45, weightKg: 75 }), 473);
|
||||
assert.equal(kcalFromExercise({ met: 2.5, minutes: 60, weightKg: 75 }), 197);
|
||||
});
|
||||
|
||||
test('daily targets treat 1.0 lb/week as ~3500 kcal/week deficit', () => {
|
||||
const profile = {
|
||||
sex: 'male', weight_kg: 165 * LB_PER_KG, height_cm: (5 * 12 + 9) * 2.54,
|
||||
age: 30, goal: 'maintain', rate_lb_week: 1.0,
|
||||
};
|
||||
const targets = dailyTargets(profile, { steps: 0, exerciseKcal: 0 });
|
||||
assert.equal(targets.rateLbWeek, 1);
|
||||
assert.ok(Math.abs(targets.adjustKcal + 500) <= 1);
|
||||
});
|
||||
|
||||
test('daily targets treat 1.5 lb/week as ~5250 kcal/week deficit', () => {
|
||||
const profile = {
|
||||
sex: 'male', weight_kg: 165 * LB_PER_KG, height_cm: (5 * 12 + 9) * 2.54,
|
||||
age: 30, goal: 'maintain', rate_lb_week: 1.5,
|
||||
};
|
||||
const targets = dailyTargets(profile, { steps: 0, exerciseKcal: 0 });
|
||||
assert.equal(targets.rateLbWeek, 1.5);
|
||||
assert.ok(Math.abs(targets.adjustKcal + 750) <= 1);
|
||||
});
|
||||
|
||||
test('daily targets with kg/week still work via rate_kg_week', () => {
|
||||
const profile = {
|
||||
sex: 'male', weight_kg: 75, height_cm: 175, age: 30,
|
||||
goal: 'lose', rate_kg_week: 0.5, rate_lb_week: 0, protein_target_g: null,
|
||||
};
|
||||
const targets = dailyTargets(profile, { steps: 0, exerciseKcal: 0 });
|
||||
assert.ok(Math.abs(targets.adjustKcal + 550) <= 1);
|
||||
// With only rate_kg_week provided, rateLbWeek is the rounded lb equivalent.
|
||||
assert.ok(Math.abs(targets.rateLbWeek - (0.5 * LB_PER_KG)) < 0.05);
|
||||
});
|
||||
|
||||
test('rate_lb_week is preserved when provided, with kg/week override', () => {
|
||||
const profile = {
|
||||
sex: 'male', weight_kg: 75, height_cm: 175, age: 30,
|
||||
goal: 'lose', rate_lb_week: 1.25, rate_kg_week: 0.5, protein_target_g: null,
|
||||
};
|
||||
const targets = dailyTargets(profile, { steps: 0, exerciseKcal: 0 });
|
||||
// rate_kg_week is also provided, so it wins and rate_lb_week is ignored.
|
||||
assert.ok(Math.abs(targets.adjustKcal + 550) <= 1);
|
||||
assert.ok(Math.abs(targets.rateLbWeek - (0.5 * LB_PER_KG)) < 0.05);
|
||||
});
|
||||
|
||||
test('daily targets add baseline, steps and workouts', () => {
|
||||
const profile = {
|
||||
sex: 'male', weight_kg: 75, height_cm: 175, age: 30,
|
||||
goal: 'maintain', rate_lb_week: 0, protein_target_g: null,
|
||||
};
|
||||
const targets = dailyTargets(profile, { steps: 10000, exerciseKcal: 473 });
|
||||
|
||||
assert.equal(targets.bmrKcal, 1699);
|
||||
assert.equal(targets.basalKcal, 2039);
|
||||
assert.equal(targets.stepKcal, 375);
|
||||
assert.equal(targets.exerciseKcal, 473);
|
||||
assert.equal(targets.totalBurnKcal, 2887);
|
||||
assert.equal(targets.adjustKcal, 0);
|
||||
assert.equal(targets.targetKcal, 2887);
|
||||
assert.equal(targets.protein_g, 120);
|
||||
});
|
||||
|
||||
test('a weight-loss goal subtracts the weekly rate and respects the floor', () => {
|
||||
const profile = {
|
||||
sex: 'female', weight_kg: 60, height_cm: 165, age: 30,
|
||||
goal: 'lose', rate_lb_week: 0.5 * 2.20462, protein_target_g: null,
|
||||
};
|
||||
const targets = dailyTargets(profile, { steps: 8000, exerciseKcal: 0 });
|
||||
|
||||
assert.ok(Math.abs(targets.adjustKcal + 550) <= 1);
|
||||
assert.equal(targets.protein_g, Math.round(60 * 2.0));
|
||||
assert.ok(targets.targetKcal >= 1200);
|
||||
});
|
||||
|
||||
test('roundLbWeek snaps to nearest 0.5 lb', () => {
|
||||
assert.equal(roundLbWeek(0.6), 0.5);
|
||||
assert.equal(roundLbWeek(0.8), 1);
|
||||
assert.equal(roundLbWeek(1.2), 1);
|
||||
assert.equal(roundLbWeek(1.3), 1.5);
|
||||
});
|
||||
|
||||
test('KCAL_PER_LB and KCAL_PER_KG are consistent', () => {
|
||||
assert.equal(KCAL_PER_LB, 3500);
|
||||
assert.ok(Math.abs(KCAL_PER_KG - 3500 * 2.20462) < 1e-6);
|
||||
});
|
||||
|
||||
test('a custom protein target overrides the default', () => {
|
||||
const profile = {
|
||||
sex: 'male', weight_kg: 80, height_cm: 180, age: 40,
|
||||
goal: 'maintain', rate_lb_week: 0, protein_target_g: 170,
|
||||
};
|
||||
assert.equal(dailyTargets(profile).protein_g, 170);
|
||||
});
|
||||
Reference in New Issue
Block a user