102 lines
3.2 KiB
JavaScript
102 lines
3.2 KiB
JavaScript
'use strict';
|
|
|
|
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const { parseHealthAutoExport, workoutMeta } = require('../server/health-auto-export');
|
|
|
|
const PAYLOAD = {
|
|
data: {
|
|
metrics: [
|
|
{
|
|
name: 'step_count',
|
|
units: 'count',
|
|
data: [
|
|
{ qty: 1200, date: '2026-11-06 09:00:00 -0700', source: 'iPhone' },
|
|
{ qty: 800, date: '2026-11-06 18:30:00 -0700', source: 'Watch' },
|
|
{ qty: 5000, date: '2026-11-05 12:00:00 -0700' },
|
|
],
|
|
},
|
|
{
|
|
name: 'heart_rate',
|
|
units: 'count/min',
|
|
data: [{ qty: 70, date: '2026-11-06 09:00:00 -0700' }],
|
|
},
|
|
],
|
|
workouts: [
|
|
{
|
|
id: '550e8400-e29b-41d4-a716-446655440000',
|
|
name: 'Running',
|
|
start: '2026-11-06 07:00:00 -0700',
|
|
end: '2026-11-06 07:30:00 -0700',
|
|
duration: 1800,
|
|
activeEnergyBurned: { qty: 350, units: 'kcal' },
|
|
},
|
|
{
|
|
id: 'workout-2',
|
|
name: 'Traditional Strength Training',
|
|
start: '2026-11-05 18:00:00 -0700',
|
|
end: '2026-11-05 18:45:00 -0700',
|
|
},
|
|
],
|
|
},
|
|
};
|
|
|
|
test('sums step samples per local day and ignores other metrics', () => {
|
|
const result = parseHealthAutoExport(PAYLOAD);
|
|
assert.deepEqual(
|
|
result.days.sort((a, b) => a.date.localeCompare(b.date)),
|
|
[{ date: '2026-11-05', steps: 5000 }, { date: '2026-11-06', steps: 2000 }]
|
|
);
|
|
});
|
|
|
|
test('uses v2 duration seconds and active energy', () => {
|
|
const result = parseHealthAutoExport(PAYLOAD);
|
|
const running = result.workouts.find((workout) => workout.name === 'Running');
|
|
|
|
assert.equal(running.minutes, 30);
|
|
assert.equal(running.kcal, 350);
|
|
assert.equal(running.met, 9);
|
|
assert.equal(running.externalId, 'hae:550e8400-e29b-41d4-a716-446655440000');
|
|
});
|
|
|
|
test('derives minutes from start/end and maps strength training', () => {
|
|
const result = parseHealthAutoExport(PAYLOAD);
|
|
const strength = result.workouts.find((workout) => workout.name === 'Strength training');
|
|
|
|
assert.equal(strength.minutes, 45);
|
|
assert.equal(strength.kcal, 0);
|
|
assert.equal(strength.met, 5);
|
|
assert.equal(strength.date, '2026-11-05');
|
|
});
|
|
|
|
test('accepts v1 energy fields and an intensity override', () => {
|
|
const result = parseHealthAutoExport({
|
|
data: {
|
|
workouts: [
|
|
{
|
|
name: 'Indoor Cycling',
|
|
start: '2026-11-06 06:00:00 -0800',
|
|
end: '2026-11-06 06:20:00 -0800',
|
|
activeEnergy: { qty: 180, units: 'kcal' },
|
|
intensity: { qty: 7.2, units: 'MET' },
|
|
},
|
|
],
|
|
},
|
|
});
|
|
|
|
assert.equal(result.workouts.length, 1);
|
|
assert.equal(result.workouts[0].name, 'Cycling');
|
|
assert.equal(result.workouts[0].minutes, 20);
|
|
assert.equal(result.workouts[0].kcal, 180);
|
|
assert.equal(result.workouts[0].met, 7.2);
|
|
});
|
|
|
|
test('handles payloads without a data wrapper and includes no bogus days', () => {
|
|
assert.deepEqual(parseHealthAutoExport({ metrics: [] }), { days: [], workouts: [] });
|
|
assert.deepEqual(parseHealthAutoExport(null), { days: [], workouts: [] });
|
|
|
|
const meta = workoutMeta('Functional Strength Training');
|
|
assert.equal(meta.name, 'Strength training');
|
|
assert.equal(meta.met, 5);
|
|
});
|