Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1362,14 +1362,15 @@ describe('transformHealthRecords', () => {
expect(result[0].value).toBe(16);
});

test('Hydration extracts volume.inLiters', () => {
test('Hydration converts volume.inLiters to integer ml and maps to water intake', () => {
const records = [
{ startTime: '2024-01-15T08:00:00Z', volume: { inLiters: 0.5 } },
];
const result = transformHealthRecords(records, { recordType: 'Hydration', unit: 'L', type: 'hydration' }) as TransformedRecord[];
const result = transformHealthRecords(records, { recordType: 'Hydration', unit: 'ml', type: 'water' }) as TransformedRecord[];

expect(result).toHaveLength(1);
expect(result[0].value).toBe(0.5);
expect(result[0].value).toBe(500);
expect(result[0].type).toBe('water');
});

test('IntermenstrualBleeding returns value 1', () => {
Expand Down Expand Up @@ -1670,9 +1671,9 @@ describe('own-app exclusion (writeback feedback-loop guard)', () => {
{ startTime: '2024-01-15T08:00:00Z', volume: { inLiters: 0.5 }, metadata: { dataOrigin: OWN } },
{ startTime: '2024-01-15T09:00:00Z', volume: { inLiters: 0.3 }, metadata: { dataOrigin: 'com.other.app' } },
];
const result = transformHealthRecords(records, { recordType: 'Hydration', unit: 'L', type: 'hydration' }) as TransformedRecord[];
const result = transformHealthRecords(records, { recordType: 'Hydration', unit: 'ml', type: 'water' }) as TransformedRecord[];
expect(result).toHaveLength(1);
expect(result[0].value).toBe(0.3);
expect(result[0].value).toBe(300);
});

test('with no own package set, nothing is excluded', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,23 @@ describe('aggregateByDay', () => {
expect(result[1]).toEqual({ value: 300, type: 'step', date: '2024-01-16', unit: 'count' });
});

test('sum strategy combines same-day hydration drinks into one daily water total (ml)', () => {
// Hydration is synced as type 'water', which the server upserts per (date, source),
// overwriting on conflict. Summing per day before upload prevents same-day drinks
// from overwriting each other.
const records: TransformedRecord[] = [
{ value: 200, type: 'water', date: '2024-01-15', unit: 'ml' },
{ value: 300, type: 'water', date: '2024-01-15', unit: 'ml' },
{ value: 250, type: 'water', date: '2024-01-16', unit: 'ml' },
];

const result = aggregateByDay(records, 'water', 'ml', 'sum');

expect(result).toHaveLength(2);
expect(result[0]).toEqual({ value: 500, type: 'water', date: '2024-01-15', unit: 'ml' });
expect(result[1]).toEqual({ value: 250, type: 'water', date: '2024-01-16', unit: 'ml' });
});

test('last strategy returns 1 record per day with the newest value (first in newest-first order)', () => {
// Records arrive newest-first from HealthKit/Health Connect queries
const records: TransformedRecord[] = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -720,9 +720,9 @@ describe('own-app exclusion (writeback feedback-loop guard)', () => {
{ startTime: '2024-01-15T08:00:00Z', volume: { inLiters: 0.5 }, sourceBundleId: 'com.sparky.app' },
{ startTime: '2024-01-15T12:00:00Z', volume: { inLiters: 0.25 }, sourceBundleId: 'com.other.app' },
];
const result = transformHealthRecords(records, { recordType: 'Hydration', unit: 'L', type: 'water' });
const result = transformHealthRecords(records, { recordType: 'Hydration', unit: 'ml', type: 'water' });
expect(result).toHaveLength(1);
expect((result[0] as TransformOutput & { value: number }).value).toBe(0.25);
expect((result[0] as TransformOutput & { value: number }).value).toBe(250);
});

test('keeps own dietary samples when no own bundle id is set', () => {
Expand Down
2 changes: 1 addition & 1 deletion SparkyFitnessMobile/src/HealthMetrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const ALL_HEALTH_METRICS: HealthMetric[] = [
{ id: 'exerciseSession', label: 'Exercise Session', stateKey: 'isExerciseSessionSyncEnabled', preferenceKey: 'syncExerciseSessionEnabled', recordType: 'ExerciseSession', unit: 'min', icon: require('../assets/icons/health-metrics/exercise_session.png'), permissions: [{ accessType: 'read', recordType: 'ExerciseSession' }, { accessType: 'read', recordType: 'ActiveCaloriesBurned' }, { accessType: 'read', recordType: 'TotalCaloriesBurned' }, { accessType: 'read', recordType: 'Distance' }], type: 'exercise_session', category: 'Common', backgroundDeliveryFrequency: 'hourly' },
{ id: 'floorsClimbed', label: 'Floors Climbed', stateKey: 'isFloorsClimbedSyncEnabled', preferenceKey: 'syncFloorsClimbedEnabled', recordType: 'FloorsClimbed', unit: 'count', icon: require('../assets/icons/health-metrics/floors_climbed.png'), permissions: [{ accessType: 'read', recordType: 'FloorsClimbed' }], type: 'floors_climbed', category: 'Activity', backgroundDeliveryFrequency: 'hourly' },
{ id: 'height', label: 'Height', stateKey: 'isHeightSyncEnabled', preferenceKey: 'syncHeightEnabled', recordType: 'Height', unit: 'm', icon: require('../assets/icons/health-metrics/height.png'), permissions: [{ accessType: 'read', recordType: 'Height' }], type: 'height', category: 'Body Measurements' },
{ id: 'hydration', label: 'Hydration', stateKey: 'isHydrationSyncEnabled', preferenceKey: 'syncHydrationEnabled', recordType: 'Hydration', unit: 'L', icon: require('../assets/icons/health-metrics/hydration.png'), permissions: [{ accessType: 'read', recordType: 'Hydration' }], type: 'hydration', category: 'Nutrition' },
{ id: 'hydration', label: 'Hydration', stateKey: 'isHydrationSyncEnabled', preferenceKey: 'syncHydrationEnabled', recordType: 'Hydration', unit: 'ml', icon: require('../assets/icons/health-metrics/hydration.png'), permissions: [{ accessType: 'read', recordType: 'Hydration' }], type: 'water', category: 'Nutrition', aggregationStrategy: 'sum' },
{ id: 'leanBodyMass', label: 'Lean Body Mass', stateKey: 'isLeanBodyMassSyncEnabled', preferenceKey: 'syncLeanBodyMassEnabled', recordType: 'LeanBodyMass', unit: 'kg', icon: require('../assets/icons/health-metrics/lean_body_mass.png'), permissions: [{ accessType: 'read', recordType: 'LeanBodyMass' }], type: 'lean_body_mass', category: 'Body Measurements' },

{ id: 'respiratoryRate', label: 'Respiratory Rate', stateKey: 'isRespiratoryRateSyncEnabled', preferenceKey: 'syncRespiratoryRateEnabled', recordType: 'RespiratoryRate', unit: 'breaths/min', icon: require('../assets/icons/health-metrics/respiratory_rate.png'), permissions: [{ accessType: 'read', recordType: 'RespiratoryRate' }], type: 'respiratory_rate', category: 'Vitals', aggregationStrategy: 'min-max-avg' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,10 @@ const VALUE_TRANSFORMERS: Record<string, ValueTransformer> = {

Hydration: (rec) => {
if (isOwnRecord(rec)) return null; // don't re-import water Sparky wrote
const value = extractNestedValue(rec, 'volume', 'inLiters');
const liters = extractNestedValue(rec, 'volume', 'inLiters');
const date = getDateString(rec.startTime);
return value !== null && date ? { value, date } : null;
// Convert L -> integer ml: synced as water intake (type 'water') which the server stores in ml.
return liters !== null && date ? { value: Math.round(liters * 1000), date } : null;
Comment thread
CodeWithCJ marked this conversation as resolved.
},

BodyTemperature: (rec) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,10 @@ const VALUE_TRANSFORMERS: Record<string, ValueTransformer> = {

Hydration: (rec) => {
if (isOwnRecord(rec)) return null; // don't re-import water Sparky wrote
const value = extractNestedValue(rec, 'volume', 'inLiters');
const liters = extractNestedValue(rec, 'volume', 'inLiters');
const date = getDateString(rec.startTime);
return value !== null && date ? { value, date } : null;
// Convert L -> integer ml: synced as water intake (type 'water') which the server stores in ml.
return liters !== null && date ? { value: Math.round(liters * 1000), date } : null;
Comment thread
CodeWithCJ marked this conversation as resolved.
},

BodyTemperature: (rec) => {
Expand Down
Loading