diff --git a/frontend/src/lib/vitals-view.js b/frontend/src/lib/vitals-view.js index 010966d..3f579ce 100644 --- a/frontend/src/lib/vitals-view.js +++ b/frontend/src/lib/vitals-view.js @@ -43,6 +43,22 @@ export const GROUP_TINT = { } export const tintOf = group => GROUP_TINT[group] || GROUP_TINT.other +/* Two names for one measurement. + * + * HAE has sent Apple's "Walking + Running Distance" under both `walking_running_distance` and + * `distance_walking_running`, and water under `water` and `dietary_water` — depending on the export + * version and the iOS release, and both names can arrive in a single export. Catalogued separately + * they became two cards with the same label and the same number, which reads as a bug because it + * is one: the alias resolves to the canonical key and only one of them is shown (the one with more + * days of data, so the choice does not depend on which name came first). + * + * Deliberately NOT a sum: these are the same samples under two names, not two sources. */ +export const ALIASES = { + walking_running_distance: 'distance_walking_running', + dietary_water: 'water' +} +export const canonicalMetric = name => ALIASES[name] || name + const spec = (label, group, o = {}) => ({ label, group, @@ -216,7 +232,27 @@ export function metricCards(data, { days = 90 } = {}) { requestedDays: days }) } - return sortCards(cards) + return sortCards(mergeAliases(cards)) +} + +/** + * One card per measurement, even when two catalogue names describe it (see ALIASES). + * + * The richer entry wins — more days of data, then more samples — because that is the one whose + * chart and trend mean something; the name it was filed under is what the detail sheet then looks + * up, so whichever survives is a key the document really has. + */ +function mergeAliases(cards) { + const byCanonical = new Map() + for (const c of cards) { + const key = canonicalMetric(c.name) + const seen = byCanonical.get(key) + if (!seen) { byCanonical.set(key, c); continue } + const better = c.days > seen.days || (c.days === seen.days && c.samples > seen.samples) + // A tie goes to the canonical name itself, so the card is labelled from the documented key. + if (better || (c.days === seen.days && c.samples === seen.samples && c.name === key)) byCanonical.set(key, c) + } + return [...byCanonical.values()] } /** Catalogue order first (Activity, Heart, Body…), then the unknowns alphabetically. Stable. */export function sortCards(cards) { diff --git a/frontend/src/lib/vitals-view.test.js b/frontend/src/lib/vitals-view.test.js index affa066..22f90f5 100644 --- a/frontend/src/lib/vitals-view.test.js +++ b/frontend/src/lib/vitals-view.test.js @@ -174,6 +174,44 @@ describe('detailRows', () => { }) }) +describe('aliases', () => { + /* Two names for one measurement, both of which HAE has used, and both of which can arrive in the + same export: two cards with the same label and the same number is not a display quirk, it is + one measurement shown twice. */ + const distance = (name, days) => ({ metrics: { [name]: { units: 'km', days: days.map(d => day(d, { qty: bucket(5) })) } } }) + + it('shows one card when both names for the same measurement arrive', () => { + const doc2 = { metrics: { + distance_walking_running: { units: 'km', days: [day('2026-05-03', { qty: bucket(5) })] }, + walking_running_distance: { units: 'km', days: [day('2026-05-01', { qty: bucket(4) }), day('2026-05-02', { qty: bucket(4) }), day('2026-05-03', { qty: bucket(5) })] } + } } + const cards = metricCards(doc2) + expect(cards.filter(c => c.label === 'Distance').length).toBe(1) + // The richer one wins: three days beat one, whichever name it was filed under. + expect(cards[0].name).toBe('walking_running_distance') + expect(cards[0].days).toBe(3) + }) + + it('keeps the canonical name on a tie, and is a no-op when only one name is there', () => { + const both = { metrics: { + distance_walking_running: { units: 'km', days: [day('2026-05-03', { qty: bucket(5) })] }, + walking_running_distance: { units: 'km', days: [day('2026-05-03', { qty: bucket(5) })] } + } } + expect(metricCards(both).map(c => c.name)).toEqual(['distance_walking_running']) + expect(metricCards(distance('walking_running_distance', ['2026-05-03'])).map(c => c.name)).toEqual(['walking_running_distance']) + // Not a sum: the two names carry the same samples, so the day's 5 km must not become 10. + expect(metricCards(both)[0].value).toBe(5) + }) + + it('resolves the water pair the same way', () => { + const both = { metrics: { + water: { units: 'L', days: [day('2026-05-03', { qty: bucket(2) })] }, + dietary_water: { units: 'L', days: [day('2026-05-03', { qty: bucket(2) })] } + } } + expect(metricCards(both).map(c => c.label)).toEqual(['Water']) + }) +}) + describe('dayLabel / trendCards / seriesStats', () => { const now = new Date('2026-05-03T09:00:00') diff --git a/frontend/src/views/Health.jsx b/frontend/src/views/Health.jsx index c995d82..1786bba 100644 --- a/frontend/src/views/Health.jsx +++ b/frontend/src/views/Health.jsx @@ -84,7 +84,7 @@ function MetricCard({ card, onOpen }) { * and the card's colour, so a panel and its tile are recognisably the same thing. */ function TrendSection({ cards, windowLabel, onOpen }) { - const picked = trendCards(cards, { limit: 4 }) + const picked = cards if (!picked.length) return null return <>

{t('Trends')} · {windowLabel}

@@ -313,7 +313,12 @@ export default function Health() { const fresh = all.filter(c => c.day === todayISO_) const cards = days === 1 && fresh.length ? fresh : all const fellBack = days === 1 && !fresh.length && all.length > 0 - const groups = groupedCards(cards) + /* A metric appears exactly once. The panels above are drawn from the same set as the grid, and + the ones they cover are taken out of it — before this, steps (say) was a chart panel and a + tile, which read as the screen showing everything twice. */ + const featured = trendCards(cards, { limit: 4 }) + const featuredNames = new Set(featured.map(c => c.name)) + const groups = groupedCards(cards.filter(c => !featuredNames.has(c.name))) const workouts = workoutRows(data, { limit: 20 }) const last = data?.last?.ts const windowLabel = chartDays >= 365 ? t('the last year') : t('the last {0} days', chartDays) @@ -352,7 +357,7 @@ export default function Health() { {t('Nothing has come in today yet — this is the latest reading of each metric.')} } - + {groups.map(g =>

{g.label}

diff --git a/frontend/src/views/Health.render.test.jsx b/frontend/src/views/Health.render.test.jsx index 5eb7131..b2d55ec 100644 --- a/frontend/src/views/Health.render.test.jsx +++ b/frontend/src/views/Health.render.test.jsx @@ -57,30 +57,44 @@ const mount = async () => { await act(async () => {}) // let the fetch's promise settle } const text = () => host.textContent +/* Everything on the screen that is a metric: the chart panels at the top (`.vh-tchart`) and the + tiles in the grid (`.vh-card`), in the order they are painted. The screen is supposed to show + each metric exactly once across the two — it did not, which is why `seenOnce` exists below. */ +const entries = () => [...host.querySelectorAll('.vh-card, .vh-tchart')] const cards = () => [...host.querySelectorAll('.vh-card')] +const entryText = el => el.textContent +const seenOnce = () => { + const labels = entries().map(e => e.querySelector('.l, .vh-tchart-t').textContent) + return labels.filter((l, i) => labels.indexOf(l) !== i) +} describe('Salud', () => { it('draws one card per metric, grouped, with the number the catalogue says to show', async () => { api.mockResolvedValue(VITALS) await mount() - expect(cards().length).toBe(5) - const labels = cards().map(c => c.querySelector('.l').textContent) - expect(labels).toEqual(['Steps', 'Heart rate', 'Sleep', 'Walking steadiness', 'Some metric from the future']) + const shown = entries().map(e => e.querySelector('.l, .vh-tchart-t').textContent) + expect(shown.sort()).toEqual(['Heart rate', 'Sleep', 'Some metric from the future', 'Steps', 'Walking steadiness']) + // The one that was reported: every metric once, never a chart panel AND its tile. + expect(seenOnce()).toEqual([]) // Steps show the day's total, heart rate the day's average, sleep hours asleep. The unknown // metric falls back to the field with the most samples (its average), rather than a dash. - expect(cards()[0].querySelector('.v').textContent).toContain('12,481') - expect(cards()[1].querySelector('.v').textContent).toContain('69') - expect(cards()[1].querySelector('.v').textContent).toContain('bpm') - expect(cards()[2].querySelector('.v').textContent).toContain('7.4') - expect(cards()[3].querySelector('.v').textContent).toContain('91') - expect(cards()[4].querySelector('.v').textContent).toContain('120') + const byLabel = Object.fromEntries(entries().map(e => [e.querySelector('.l, .vh-tchart-t').textContent, entryText(e)])) + expect(byLabel['Steps']).toContain('12,481') + expect(byLabel['Heart rate']).toContain('69') + expect(byLabel['Heart rate']).toContain('bpm') + expect(byLabel['Sleep']).toContain('7.4') + expect(byLabel['Walking steadiness']).toContain('91') + expect(byLabel['Some metric from the future']).toContain('120') - // Group headings, the range control, and the read-only workout list. - expect(text()).toContain('Activity') + // Group headings, the range control, and the read-only workout list. Activity is absent on + // purpose: in this fixture its only metric is Steps, which the panels took, and a heading with + // nothing under it is worse than no heading. + expect(text()).not.toContain('Activity') expect(text()).toContain('Heart') expect(text()).toContain('Vitals') expect(text()).toContain('Other') + expect(text()).toContain('Trends') expect(text()).toContain('Workouts from your iPhone') expect(text()).toContain('Functional Strength Training') expect(text()).toContain('50 min') @@ -95,9 +109,10 @@ describe('Salud', () => { // behind them all come from one property — this checks the wiring, not the shades. api.mockResolvedValue(VITALS) await mount() - const tints = cards().map(c => c.style.getPropertyValue('--tint')) - expect(tints).toEqual(['var(--orange)', 'var(--red)', 'var(--indigo)', 'var(--green)', 'var(--grey)']) - expect(cards().every(c => c.getAttribute('style').includes('--tint'))).toBe(true) + // Panels and tiles carry the same property, so the colours come out in the order they paint. + expect(entries().map(e => e.style.getPropertyValue('--tint'))) + .toEqual(['var(--orange)', 'var(--red)', 'var(--indigo)', 'var(--green)', 'var(--grey)']) + expect(entries().every(e => e.getAttribute('style').includes('--tint'))).toBe(true) }) it('offers the setup steps instead of a blank screen when nothing has arrived', async () => {