Initial commit of counter
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
'use strict';
|
||||
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const { createParser } = require('../public/health');
|
||||
|
||||
const XML = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<HealthData locale="en_US">
|
||||
<Record type="HKQuantityTypeIdentifierStepCount" sourceName="iPhone" unit="count"
|
||||
creationDate="2026-11-06 08:00:00 -0700" startDate="2026-11-06 07:00:00 -0700"
|
||||
endDate="2026-11-06 08:00:00 -0700" value="1200"/>
|
||||
<Record type="HKQuantityTypeIdentifierStepCount" sourceName="Watch" unit="count"
|
||||
startDate="2026-11-06 09:00:00 -0700" endDate="2026-11-06 10:00:00 -0700" value="800"/>
|
||||
<Record type="HKQuantityTypeIdentifierActiveEnergyBurned" sourceName="Watch" unit="kcal"
|
||||
startDate="2026-11-06 09:00:00 -0700" endDate="2026-11-06 10:00:00 -0700" value="450"/>
|
||||
<Record type="HKQuantityTypeIdentifierStepCount" sourceName="iPhone" unit="count"
|
||||
startDate="2026-11-05 12:00:00 -0700" endDate="2026-11-05 13:00:00 -0700" value="5000"/>
|
||||
<Workout workoutActivityType="HKWorkoutActivityTypeRunning" duration="32.5"
|
||||
totalEnergyBurned="310" startDate="2026-11-06 07:12:00 -0700"
|
||||
endDate="2026-11-06 07:44:30 -0700"/>
|
||||
<Workout workoutActivityType="HKWorkoutActivityTypeTraditionalStrengthTraining"
|
||||
startDate="2026-11-05 18:00:00 -0700" endDate="2026-11-05 18:45:00 -0700"/>
|
||||
</HealthData>`;
|
||||
|
||||
test('sums step records per day and maps workouts', () => {
|
||||
const parser = createParser({ since: '2026-11-05' });
|
||||
parser.push(XML);
|
||||
const result = parser.finish();
|
||||
|
||||
assert.deepEqual(
|
||||
result.days.sort((a, b) => a.date.localeCompare(b.date)),
|
||||
[{ date: '2026-11-05', steps: 5000 }, { date: '2026-11-06', steps: 2000 }]
|
||||
);
|
||||
|
||||
const running = result.workouts.find((workout) => workout.name === 'Running');
|
||||
assert.equal(running.kcal, 310);
|
||||
assert.equal(running.minutes, 32.5);
|
||||
assert.equal(running.met, 9);
|
||||
|
||||
const strength = result.workouts.find((workout) => workout.name === 'Strength training');
|
||||
assert.equal(strength.kcal, 0);
|
||||
assert.equal(strength.minutes, 45);
|
||||
assert.equal(strength.met, 5);
|
||||
});
|
||||
|
||||
test('ignores records before the requested range', () => {
|
||||
const parser = createParser({ since: '2026-11-06' });
|
||||
parser.push(XML);
|
||||
const result = parser.finish();
|
||||
|
||||
assert.deepEqual(result.days, [{ date: '2026-11-06', steps: 2000 }]);
|
||||
assert.equal(result.workouts.length, 1);
|
||||
});
|
||||
|
||||
test('handles tags split across chunks', () => {
|
||||
const whole = createParser();
|
||||
whole.push(XML);
|
||||
const expected = whole.finish();
|
||||
|
||||
const chunked = createParser();
|
||||
for (let offset = 0; offset < XML.length; offset += 97) {
|
||||
chunked.push(XML.slice(offset, offset + 97));
|
||||
}
|
||||
const actual = chunked.finish();
|
||||
const byDate = (days) => days.sort((a, b) => a.date.localeCompare(b.date));
|
||||
|
||||
assert.deepEqual(byDate(actual.days), byDate(expected.days));
|
||||
assert.equal(actual.workouts.length, expected.workouts.length);
|
||||
});
|
||||
Reference in New Issue
Block a user