forked from Blendman974/faceclaw
49 lines
2.2 KiB
JavaScript
49 lines
2.2 KiB
JavaScript
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);
|
|
}
|
|
});
|