mirror of
https://git.undefined-dev.fr/Blendman974/faceclaw.git
synced 2026-11-19 21:03:09 +00:00
123 lines
7.1 KiB
JavaScript
123 lines
7.1 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const { loader } = require('./helpers/load-typescript.cjs');
|
|
|
|
function setup() {
|
|
const files = new Map();
|
|
const backend = { list: () => [...files.keys()], read: name => files.get(name) ?? null,
|
|
exists: name => files.has(name), append: (name, text) => files.set(name, (files.get(name) ?? '') + text),
|
|
write: (name, text) => files.set(name, text) };
|
|
const { HealthStore } = loader()('app/health/health-store.ts');
|
|
const store = new HealthStore(backend);
|
|
const settings = new Map();
|
|
let url = 'https://health.example/v1/health', key = 'private-test-key', enabled = true, fixture = false;
|
|
let response = { status: 200, json: async () => ({ accepted: true }) };
|
|
const requests = [];
|
|
const exporter = loader({ setTimeout, setInterval }, {
|
|
'./health-store-files': { healthStore: () => store },
|
|
'./health-seed': { isFixtureData: () => fixture },
|
|
'../ui/dashboard-settings': {
|
|
healthExportEnabledSetting: { get: () => enabled },
|
|
healthExportUrlSetting: { get: () => url, validationError: () => url.startsWith('https://') ? null : 'HTTPS required' },
|
|
healthExportKeySetting: { get: () => key },
|
|
},
|
|
'../native/settings-store': { getStringSetting: (k, d) => settings.get(k) ?? d, setStringSetting: (k, v) => settings.set(k, v) },
|
|
'../util/http': { fetchTextWithUserAgent: async (url, options) => { requests.push({ url, options }); return typeof response === 'function' ? response() : response; } },
|
|
})('app/health/health-export.ts');
|
|
return { store, files, settings, exporter, requests,
|
|
configure: options => { if ('url' in options) url=options.url; if ('enabled' in options) enabled=options.enabled; if ('fixture' in options) fixture=options.fixture; if ('response' in options) response=options.response; } };
|
|
}
|
|
const sample = { metric:'heartRate',startMs:1700000000000,spanMs:3600000,min:60,max:90,avg:75,total:0 };
|
|
|
|
test('exports original measurements, acknowledges durably and catches revisions to old buckets', async () => {
|
|
const h=setup();h.store.ingestSamples([sample]);
|
|
await h.exporter.syncHealthExport();
|
|
assert.equal(h.requests.length,1);
|
|
assert.deepEqual(JSON.parse(h.requests[0].options.body),{version:1,samples:[sample],sleep:[]});
|
|
assert.equal(h.requests[0].options.dontFollowRedirects,true);
|
|
assert.ok(!h.settings.get('health.export.cursor').includes('private-test-key'));
|
|
await h.exporter.syncHealthExport();assert.equal(h.requests.length,1);
|
|
h.store.ingestSamples([{...sample,avg:80}]);await h.exporter.syncHealthExport();
|
|
assert.equal(JSON.parse(h.requests[1].options.body).samples[0].avg,80);
|
|
h.configure({url:'https://another.example/v1/health'});await h.exporter.syncHealthExport();
|
|
assert.equal(h.requests.length,3);
|
|
});
|
|
|
|
test('failed or malformed acknowledgements retain cursor for replay; no secret in status', async () => {
|
|
for (const response of [{status:401},{status:503},{status:302},{status:200,json:async()=>({})},()=>Promise.reject(new Error('private-test-key'))]) {
|
|
const h=setup();h.store.ingestSamples([sample]);h.configure({response});await h.exporter.syncHealthExport();
|
|
assert.equal(h.settings.has('health.export.cursor'),false);
|
|
assert.ok(!h.exporter.healthExportStatus().includes('private-test-key'));
|
|
h.configure({response:{status:200,json:async()=>({accepted:true})}});
|
|
await h.exporter.syncHealthExport();assert.equal(h.requests.length,2);
|
|
}
|
|
});
|
|
|
|
test('disabled export, fixtures, missing key configuration and HTTP never upload', async () => {
|
|
for (const config of [{enabled:false},{fixture:true},{url:'http://health.example/v1/health'},{url:''}]) {
|
|
const h=setup();h.store.ingestSamples([sample]);h.configure(config);
|
|
await h.exporter.syncHealthExport();assert.equal(h.requests.length,0);
|
|
}
|
|
});
|
|
|
|
test('bounded batches preserve partial lines, unresolved sleep and late appends', () => {
|
|
const h=setup();h.store.ingestSamples([sample]);
|
|
const name=[...h.files.keys()].find(n=>n.startsWith('samples-'));
|
|
const first=h.store.exportBatch({},1);assert.equal(first.scanned,1);
|
|
const second=h.store.exportBatch(first.cursor,200);assert.equal(second.samples.length,1);
|
|
h.files.set(name,h.files.get(name)+'{"m":');
|
|
const partial=h.store.exportBatch(second.cursor);assert.equal(partial.scanned,0);
|
|
h.files.set(name,h.files.get(name)+'"hrv","t":1700000000000,"s":3600000,"n":1,"x":3,"a":2,"u":0}\n');
|
|
assert.equal(h.store.exportBatch(partial.cursor).samples[0].metric,'hrv');
|
|
h.files.set('sleep.jsonl',JSON.stringify({timeResolved:false,startMs:12})+'\n');
|
|
assert.equal(h.store.exportBatch({}).sleep.length,0);
|
|
});
|
|
|
|
test('concurrent ticks and changed credentials cannot incorrectly acknowledge a batch', async () => {
|
|
const h=setup();h.store.ingestSamples([sample]);let release;
|
|
h.configure({response:()=>new Promise(resolve=>{release=resolve;})});
|
|
const first=h.exporter.syncHealthExport();await h.exporter.syncHealthExport();
|
|
assert.equal(h.requests.length,1);
|
|
h.settings.set('health.export.generation','1');release({status:200,json:async()=>({accepted:true})});
|
|
await first;assert.equal(h.settings.has('health.export.cursor'),false);
|
|
});
|
|
|
|
test('large sleep histories split into bounded bodies without losing sessions', async () => {
|
|
const h=setup();
|
|
const sessions=Array.from({length:6},(_,i)=>({timeResolved:true,startMs:1700000000000+i*86400000,
|
|
endMs:1700028800000+i*86400000,dayStartMs:1699920000000+i*86400000,
|
|
totalSec:28000,wakeSec:800,remSec:4000,lightSec:18000,deepSec:6000,
|
|
segments:Array.from({length:2000},()=>({stageId:2,halfMinutes:1}))}));
|
|
h.files.set('sleep.jsonl',sessions.map(s=>JSON.stringify(s)+'\n').join(''));
|
|
for(let i=0;i<10;i++) await h.exporter.syncHealthExport();
|
|
assert.ok(h.requests.length>1);
|
|
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);
|
|
});
|