Salud: el total del día es el día, y un recibo que explica el número que no cuadra
Tests / test (push) Canceled after 0s
Publish Docker images / build (., ./web/Dockerfile, web) (push) Canceled after 0s
Publish Docker images / build (./api, ./api/Dockerfile, api) (push) Canceled after 0s
Tests / test (push) Canceled after 0s
Publish Docker images / build (., ./web/Dockerfile, web) (push) Canceled after 0s
Publish Docker images / build (./api, ./api/Dockerfile, api) (push) Canceled after 0s
"Las actualizaciones están llegando pero las tarjetas no tienen los mismos números que Salud." Las actualizaciones llegan, entonces: el problema es qué se hace con ellas, y hay dos formas de que un día salga más grande de lo que el reloj dice. **El total del día leído como hora 00.** Si HAE manda el total del día con marca de medianoche en el mismo payload que las horas, ese punto caía en el cubo de las 00:00 y la suma del día lo contaba además de las horas — 499 declarados + 341 de horas = 840, que es exactamente el número reportado. Ahora un punto de medianoche que vale más que todo el resto del payload junto es el total del día (una hora no puede tener más que el día que la contiene) y nunca se convierte en hora 00; un 00:00 chico de verdad sigue siendo la primera hora y se suma como cualquier otra. **Horas sumadas a un total que ya las contenía.** Con el día declarado por el teléfono, cada hora que llegaba después se le sumaba encima — el mismo error, una capa más arriba. Ahora el total declarado **es** el día y las horas nunca se le agregan: son la forma del día, no un segundo número. Su suma queda al lado, en `hours_sum`, y la hoja de la métrica dice cuando los dos difieren, en palabras: tanto el teléfono declaró, tanto suman las horas, y cuál de los dos estás viendo. Un total que no se renueva en seis horas deja de mandar, así que cambiar una automatización de Días a Horas converge en vez de congelarse para el resto del día. Y para la otra forma — horas que en realidad son **totales acumulados** en vez de deltas, que también suman de más — cada envío deja un **recibo por métrica**, visible en la hoja: cuántos datos llegaron, cómo vienen marcados (un total por día, uno por hora, muestras sueltas o mezcla), de qué fuentes, cuánto suman y los tres últimos valores. Una corrida de números que suben (340 → 420 → 499) es la señal de que son acumulados, y eso es la diferencia entre 499 y 840 vista en la pantalla en vez de deducida. Es un recibo, no una lectura: el campo que cita se elige por una lista fija porque este módulo no tiene el catálogo, y nada de él entra en el cálculo de un día. Seis tests nuevos del merge y del recibo, dos de las filas de la hoja y uno de render por la hoja real. Mutation-check de las dos mitades del arreglo: leer el total como hora 00 falla, y sumarle las horas al total declarado falla. API 261 en verde, frontend 1701, build OK.
This commit is contained in:
1 parent
81301523ab
commit
407d7c47c9
9 files changed
+373
-19
No files matched your search
+85
-6
@@ -282,7 +282,7 @@ test('a raw sample that happens to sit on the hour does not wipe that hour', ()
|
||||
assert.equal(again.doc.metrics.step_count.days['2026-05-01'].f.qty.sum, 1000);
|
||||
});
|
||||
|
||||
test('a day total and an hour total for the same day agree on which number is the day', () => {
|
||||
test('the phone\'s own day total is the day, and hours never get added to it', () => {
|
||||
const hours = metric('step_count', 'count', [
|
||||
{ qty: 500, date: at('2026-05-01', '08:00:00') },
|
||||
{ qty: 900, date: at('2026-05-01', '09:00:00') }
|
||||
@@ -290,22 +290,31 @@ test('a day total and an hour total for the same day agree on which number is th
|
||||
let doc = merge(emptyVitals(), parsePayload(hours)).doc;
|
||||
assert.equal(doc.metrics.step_count.days['2026-05-01'].f.qty.sum, 1400);
|
||||
|
||||
// A second automation grouped by Days sends the day itself: it is the better day number, and
|
||||
// the hours are a shape the total cannot contradict, so they stay for the sheet to draw.
|
||||
// A second automation grouped by Days sends the day itself: it is the number the Health app
|
||||
// shows, so it becomes the day — and the hours stay for the sheet to draw.
|
||||
const total = merge(doc, parsePayload(metric('step_count', 'count', [{ qty: 1500, date: at('2026-05-01') }])));
|
||||
doc = total.doc;
|
||||
assert.equal(total.stats.points, 1);
|
||||
assert.equal(doc.metrics.step_count.days['2026-05-01'].f.qty.sum, 1500);
|
||||
assert.equal(Object.keys(doc.metrics.step_count.days['2026-05-01'].h).length, 2, 'the hours rode along');
|
||||
|
||||
// Then the hourly automation sends the next hour: it re-sends the whole day, so from here the
|
||||
// day is the hours again — the two rules converge instead of drifting apart.
|
||||
// Then hour points arrive for the same day. The day does NOT become their sum: the declared total
|
||||
// already contains the hours below it, and adding them is exactly how 499 steps became 840. The
|
||||
// sum rides beside it instead, so a disagreement between the two is visible rather than silent.
|
||||
doc = merge(doc, parsePayload(metric('step_count', 'count', [
|
||||
{ qty: 500, date: at('2026-05-01', '08:00:00') },
|
||||
{ qty: 900, date: at('2026-05-01', '09:00:00') },
|
||||
{ qty: 200, date: at('2026-05-01', '10:00:00') }
|
||||
]))).doc;
|
||||
assert.equal(doc.metrics.step_count.days['2026-05-01'].f.qty.sum, 1600);
|
||||
const day = doc.metrics.step_count.days['2026-05-01'];
|
||||
assert.equal(day.f.qty.sum, 1500, 'the phone\'s total is still the day');
|
||||
assert.equal(day.own, true);
|
||||
assert.equal(Object.keys(day.h).length, 3, 'and the hours still landed');
|
||||
assert.equal(day.hsum.qty, 1600, 'with their sum beside them, for the screen to compare');
|
||||
|
||||
// A newer total replaces it, as it always did.
|
||||
assert.equal(merge(doc, parsePayload(metric('step_count', 'count', [{ qty: 1700, date: at('2026-05-01') }]))).doc
|
||||
.metrics.step_count.days['2026-05-01'].f.qty.sum, 1700);
|
||||
});
|
||||
|
||||
test('a day stored before hours existed keeps its samples when the first hour arrives', () => {
|
||||
@@ -344,6 +353,76 @@ test('vitalsHours answers for one metric and one day, and invents nothing', () =
|
||||
assert.equal(vitalsHours(emptyVitals(), { metric: 'step_count', day: '2026-05-01' }).metric, null);
|
||||
});
|
||||
|
||||
test('a midnight total inside an hourly payload is the day, not hour zero', () => {
|
||||
// The shape that produced "the watch says 499 and the app says 840": HAE sends the day's total
|
||||
// stamped at midnight *and* the hours, in one payload. Read as hour 00, the total is added to the
|
||||
// hours and the day doubles; read as what it is, the day is 499 and the hours are its shape.
|
||||
const p = (qty, time) => ({ qty, date: at('2026-05-01', time) });
|
||||
const payload = metric('step_count', 'count', [
|
||||
p(499, '00:00:00'), // the day's own total, stamped at midnight
|
||||
p(120, '08:00:00'), p(220, '09:00:00') // and two hours of it
|
||||
]);
|
||||
const doc = merge(emptyVitals(), parsePayload(payload)).doc;
|
||||
const day = doc.metrics.step_count.days['2026-05-01'];
|
||||
assert.equal(day.f.qty.sum, 499, 'the day is the total, not 499 + 340');
|
||||
assert.equal(day.own, true);
|
||||
assert.equal(day.h['00'], undefined, 'and the midnight point did not become hour 00');
|
||||
assert.deepEqual(Object.keys(day.h).sort(), ['08', '09']);
|
||||
assert.equal(day.hsum.qty, 340, 'the sum of the hours is kept beside it');
|
||||
|
||||
// The same payload again is a no-op on the day, and the hours converge.
|
||||
const grew = merge(doc, parsePayload(metric('step_count', 'count', [
|
||||
p(499, '00:00:00'), p(120, '08:00:00'), p(260, '09:00:00')
|
||||
]))).doc.metrics.step_count.days['2026-05-01'];
|
||||
assert.equal(grew.f.qty.sum, 499);
|
||||
assert.equal(grew.h['09'].f.qty.sum, 260);
|
||||
|
||||
// A genuine 00:00 bucket — a handful of steps, less than the rest of the day — stays hour 00.
|
||||
const small = merge(emptyVitals(), parsePayload(metric('step_count', 'count', [
|
||||
p(3, '00:00:00'), p(300, '08:00:00'), p(400, '09:00:00')
|
||||
]))).doc.metrics.step_count.days['2026-05-01'];
|
||||
assert.equal(small.f.qty.sum, 703, 'a real first hour is summed like any other');
|
||||
assert.equal(small.h['00'].f.qty.sum, 3);
|
||||
assert.equal(small.own, undefined);
|
||||
});
|
||||
|
||||
test('the receipt records what arrived, so a wrong number can be explained', () => {
|
||||
const doc = merge(emptyVitals(), parsePayload({
|
||||
data: { metrics: [
|
||||
{ name: 'step_count', units: 'count', data: [
|
||||
{ qty: 120, date: at('2026-05-01', '08:00:00'), source: 'Apple Watch' },
|
||||
{ qty: 220, date: at('2026-05-01', '09:00:00'), source: 'Apple Watch' },
|
||||
{ qty: 90, date: at('2026-05-01', '09:30:00'), source: 'iPhone' }
|
||||
] }
|
||||
] }
|
||||
}), NOW).doc;
|
||||
const r = doc.shape.step_count;
|
||||
assert.equal(r.points, 3);
|
||||
assert.equal(r.stamps, 'mixed', 'a payload with an hour and a real instant says so');
|
||||
assert.equal(r.field, 'qty');
|
||||
assert.deepEqual(r.sources, ['Apple Watch', 'iPhone']);
|
||||
assert.equal(r.sum, 430);
|
||||
assert.equal(r.newest.value, 90);
|
||||
assert.equal(r.tail.length, 3);
|
||||
assert.equal(r.head.length, 3);
|
||||
assert.equal(r.at, NOW);
|
||||
|
||||
// Hour-stamped and day-stamped payloads are labelled too, because that is the question the
|
||||
// receipt exists to answer.
|
||||
const hourly = merge(emptyVitals(), parsePayload(metric('step_count', 'count', [
|
||||
{ qty: 120, date: at('2026-05-01', '08:00:00') }, { qty: 220, date: at('2026-05-01', '09:00:00') }
|
||||
]))).doc;
|
||||
assert.equal(hourly.shape.step_count.stamps, 'hour');
|
||||
const dayly = merge(emptyVitals(), parsePayload(metric('step_count', 'count', [{ qty: 499, date: at('2026-05-01') }]))).doc;
|
||||
assert.equal(dayly.shape.step_count.stamps, 'day');
|
||||
// Replaced on every sync, never accumulated.
|
||||
const again = merge(dayly, parsePayload(metric('heart_rate', 'bpm', [{ Avg: 62, date: at('2026-05-01', '09:00:00') }]))).doc;
|
||||
assert.equal(again.shape.step_count.points, 1, 'the metric that did not arrive keeps its receipt');
|
||||
assert.equal(again.shape.heart_rate.field, 'Avg');
|
||||
// It survives a reload of the file, and it is not read by anything that computes a number.
|
||||
assert.equal(normalizeDoc(JSON.parse(JSON.stringify(again))).shape.step_count.points, 1);
|
||||
});
|
||||
|
||||
test('the hourly detail is pruned after a week while the day aggregates live on', () => {
|
||||
const start = Date.parse('2026-05-06T12:00:00Z');
|
||||
const doc = merge(emptyVitals(), parsePayload(metric('step_count', 'count', [
|
||||
|
||||
Reference in new issue
Block a user