forked from Blendman974/faceclaw
Merge detailed heart-rate collection and export
# Conflicts: # docs/validation-health-export.md
This commit is contained in:
8 files changed
+218
-18
No files matched your search
@@ -94,3 +94,29 @@ test('large sleep histories split into bounded bodies without losing sessions',
|
||||
assert.ok(h.requests.every(r=>r.options.body.length<=240000));
|
||||
assert.equal(h.requests.reduce((n,r)=>n+JSON.parse(r.options.body).sleep.length,0),sessions.length);
|
||||
});
|
||||
|
||||
test('point heart rates and hourly summaries export separately with original timestamps', async () => {
|
||||
const h = setup();
|
||||
const point = { ...sample, spanMs: 1, startMs: sample.startMs + 123456, min: 72, max: 72, avg: 72, total: 72 };
|
||||
h.store.ingestSamples([sample, point]);
|
||||
await h.exporter.syncHealthExport();
|
||||
const body = JSON.parse(h.requests[0].options.body);
|
||||
assert.deepEqual(body.samples, [sample, point]);
|
||||
h.store.resetCache();
|
||||
assert.equal(h.store.ingestSamples([point]), 0);
|
||||
await h.exporter.syncHealthExport();
|
||||
assert.equal(h.requests.length, 1);
|
||||
});
|
||||
|
||||
test('a corrected point replaces its old value without changing hourly summaries', async () => {
|
||||
const h = setup();
|
||||
const point = { ...sample, spanMs: 1, min: 72, max: 72, avg: 72, total: 72 };
|
||||
h.store.ingestSamples([sample, point]);
|
||||
await h.exporter.syncHealthExport();
|
||||
const correction = { ...point, min: 74, max: 74, avg: 74, total: 74 };
|
||||
h.store.ingestSamples([correction]);
|
||||
await h.exporter.syncHealthExport();
|
||||
assert.deepEqual(JSON.parse(h.requests[1].options.body).samples, [correction]);
|
||||
assert.equal(h.store.samplesInRange(sample.startMs, sample.startMs + 1, true).length, 2);
|
||||
assert.equal(h.store.samplesInRange(sample.startMs, sample.startMs + 1)[0].avg, sample.avg);
|
||||
});
|
||||
@@ -0,0 +1,48 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const { loader } = require('./helpers/load-typescript.cjs');
|
||||
const { convertRecords } = loader()('app/health/health-ingest.ts');
|
||||
const receivedAtMs = Date.UTC(2026, 10, 8, 12);
|
||||
const page = {
|
||||
kind: 'hourly', metric: 'heartRate', anchorMs: receivedAtMs,
|
||||
groups: [{ hourIndex: 0, min: 50, max: 90, avg: 65 }],
|
||||
latest: { timestampSeconds: (receivedAtMs - 60000) / 1000, value: 72, receivedAtMs, clockOffsetMs: 0 },
|
||||
};
|
||||
|
||||
test('latest observation remains a point, independent of hourly averages and page anchor', () => {
|
||||
const result = convertRecords([{ ...page, anchorMs: null }]);
|
||||
assert.equal(result.samples.length, 1);
|
||||
assert.equal(result.samples[0].spanMs, 1);
|
||||
assert.equal(result.samples[0].startMs, receivedAtMs - 60000);
|
||||
assert.equal(result.samples[0].min, 72);
|
||||
assert.equal(result.samples[0].max, 72);
|
||||
assert.equal(result.samples[0].avg, 72);
|
||||
});
|
||||
|
||||
test('legacy handshake timezone offset is inverted once, preserving exact timestamps', () => {
|
||||
for (const clockOffsetMs of [-3600000, -7200000, 7 * 3600000, 0, -19800000]) {
|
||||
const latest = { ...page.latest, clockOffsetMs,
|
||||
timestampSeconds: (receivedAtMs - 60000 + clockOffsetMs) / 1000 };
|
||||
const samples = convertRecords([{ ...page, latest }]).samples;
|
||||
assert.equal(samples[0].startMs, receivedAtMs - 60000);
|
||||
assert.equal(samples[1].spanMs, 3600000);
|
||||
}
|
||||
});
|
||||
|
||||
test('invalid, stale and future latest values never corrupt hourly history', () => {
|
||||
for (const patch of [
|
||||
{ value: 0 }, { value: -1 }, { value: 301 }, { value: NaN }, { value: 72.5 },
|
||||
{ timestampSeconds: -1 }, { timestampSeconds: 0 }, { timestampSeconds: NaN },
|
||||
{ timestampSeconds: receivedAtMs / 1000 + 121 },
|
||||
{ timestampSeconds: receivedAtMs / 1000 - 1201 },
|
||||
{ receivedAtMs: NaN }, { clockOffsetMs: NaN },
|
||||
]) {
|
||||
const result = convertRecords([{ ...page, latest: { ...page.latest, ...patch } }]);
|
||||
assert.equal(result.samples.length, 1, JSON.stringify(patch));
|
||||
assert.equal(result.samples[0].spanMs, 3600000);
|
||||
assert.equal(result.skipped.length, 1);
|
||||
}
|
||||
for (const metric of ['spo2', 'hrv']) {
|
||||
assert.equal(convertRecords([{ ...page, metric }]).samples.length, 1);
|
||||
}
|
||||
});
|
||||
@@ -210,3 +210,56 @@ test("a failed journal replay is retried on the next sync and never blocks the l
|
||||
live.syncLiveRecords();
|
||||
assert.equal(failures, -1, "replay was not retried");
|
||||
});
|
||||
|
||||
test("live latest heart rate retains measurement time and survives restart without duplicates", () => {
|
||||
const measuredAt = Math.floor(Date.now() / 1000) * 1000 - 60_000;
|
||||
function sync(previous, time) {
|
||||
const env = liveEnvironment();
|
||||
if (previous) for (const [name, text] of previous.files) env.files.set(name, text);
|
||||
Object.assign(env.record, {
|
||||
receivedAtMs: time + 60_000,
|
||||
tagRaw: (time + new Date(time + 60_000).getTimezoneOffset() * 60_000) / 1000,
|
||||
current: 72,
|
||||
});
|
||||
env.load("health-live").syncLiveRecords();
|
||||
return env;
|
||||
}
|
||||
const first = sync(null, measuredAt);
|
||||
const repeated = sync(first, measuredAt);
|
||||
const next = sync(repeated, measuredAt + 600_000);
|
||||
const store = next.load("health-store-files").healthStore();
|
||||
const batch = store.exportBatch({});
|
||||
const points = Array.from(batch.samples).filter(s => s.spanMs === 1);
|
||||
assert.deepEqual(points.map(s => [s.startMs, s.avg]), [[measuredAt, 72], [measuredAt + 600_000, 72]]);
|
||||
assert.equal(batch.samples.filter(s => s.spanMs === 3600000).length, 1);
|
||||
assert.equal(store.samplesInRange(measuredAt - 86400000, measuredAt + 86400000).length, 1,
|
||||
"existing health charts only see bucket samples");
|
||||
const counts = () => Array.from(store.dailyRollups('heartRate', measuredAt - 86400000, measuredAt + 86400000).values()).reduce((n, r) => n + r.count, 0);
|
||||
assert.equal(counts(), 1);
|
||||
store.rebuildRollups();
|
||||
assert.equal(counts(), 1, "rebuilding must not mix point readings with hourly aggregates");
|
||||
});
|
||||
|
||||
test("aligned health collection requests all six ten-minute slots without timer drift", () => {
|
||||
const { msUntilNextAlignedPull, ALIGNED_PULL_MINUTES } = liveEnvironment().load('health-live').__alignedPullInternals;
|
||||
assert.deepEqual(Array.from(ALIGNED_PULL_MINUTES), [1, 11, 21, 31, 41, 51]);
|
||||
for (const minute of ALIGNED_PULL_MINUTES) {
|
||||
assert.equal(msUntilNextAlignedPull(new Date(2026, 10, 8, 12, minute, 0)), 600000);
|
||||
assert.equal(msUntilNextAlignedPull(new Date(2026, 10, 8, 12, minute - 1, 30)), 30000);
|
||||
}
|
||||
assert.equal(msUntilNextAlignedPull(new Date(2026, 10, 8, 12, 59, 0)), 120000);
|
||||
});
|
||||
|
||||
test("journal replay restores hourly history without assigning a clock to old point readings", () => {
|
||||
const receivedAtMs = Math.floor(Date.now() / 1000) * 1000;
|
||||
let replay;
|
||||
const ringProtocol = { decodeFrameJournal: () => ({ size: () => 1, get: () => replay }) };
|
||||
const env = liveEnvironment({ ringProtocol });
|
||||
replay = { ...env.record, current: 80, receivedAtMs,
|
||||
tagRaw: (receivedAtMs + new Date(receivedAtMs).getTimezoneOffset() * 60000) / 1000 };
|
||||
env.files.set('/health/ring-frames.jsonl', 'synthetic journal');
|
||||
env.load('health-live').syncLiveRecords();
|
||||
const samples = env.load('health-store-files').healthStore().exportBatch({}).samples;
|
||||
assert.equal(samples.length, 1);
|
||||
assert.equal(samples[0].spanMs, 3600000);
|
||||
});
|
||||
Reference in new issue
Block a user