Files
haptika/src/features/ppg/tests/historyAnalytics.test.ts
T
2026-07-31 16:20:10 +02:00

55 lines
2.0 KiB
TypeScript

import {buildDailyTrends, buildFeelingCorrelation} from '../history/historyAnalytics';
import type {StoredMeasurement} from '../storage/storageTypes';
import {makeProcessingResult} from './syntheticMeasurements';
const makeMeasurement = (
id: string,
measuredAt: string,
bpm: number,
feeling: 1 | 2 | 3 | 4 | 5,
): StoredMeasurement => ({
schemaVersion: 1,
id,
measuredAt,
acquisition: {cameraPosition: 'front', torchUsed: false},
atRest: true,
baselineEligible: true,
cameraResult: (() => {
const result = makeProcessingResult();
return {...result, bpm: {...result.bpm, bpm}};
})(),
potentialStress: {
index: 20, confidence: 0.8, status: 'provisional', bpmContribution: 0,
prvContribution: 0, signalQuality: 0.9, baselineMeasurementCount: 5,
healthKitUsedForScore: false, limitations: [],
},
healthKitContext: null,
selfReport: {
feeling, posture: 'seated', recentExercise: false, recentCaffeine: false,
recentMeal: false, movementDuringMeasurement: false, phoneUnusuallyWarm: false,
recordedAt: measuredAt,
},
});
describe('history analytics', () => {
test('aggregates multiple measurements into chronological daily points', () => {
const now = Date.parse('2026-07-17T12:00:00.000Z');
const trends = buildDailyTrends([
makeMeasurement('a', '2026-07-16T08:00:00.000Z', 70, 3),
makeMeasurement('b', '2026-07-16T18:00:00.000Z', 80, 4),
makeMeasurement('c', '2026-07-17T08:00:00.000Z', 90, 4),
], 30, now);
expect(trends.map(point => point.day)).toEqual(['2026-07-16', '2026-07-17']);
expect(trends[0]).toMatchObject({bpm: 75, count: 2});
});
test('groups descriptive BPM and RMSSD averages by feeling', () => {
const correlation = buildFeelingCorrelation([
makeMeasurement('a', '2026-07-16T08:00:00.000Z', 70, 4),
makeMeasurement('b', '2026-07-16T18:00:00.000Z', 80, 4),
]);
expect(correlation).toHaveLength(1);
expect(correlation[0]).toMatchObject({feeling: 4, averageBpm: 75, count: 2});
});
});