Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
frontend: streak card tallies the coach week and your own days together; day sheet names the queue session
Boris Kurktchiev5aa4e5932d49
diff --git a/frontend/src/lib/queue.js b/frontend/src/lib/queue.jsindex 14279ec70..51f4a9c5a 100644--- a/frontend/src/lib/queue.js+++ b/frontend/src/lib/queue.js@@ -23,7 +23,7 @@// planner may reuse its routine ids and names every week (W1 again after a restart), so a// workout from a past week that happens to carry this week's name must never count.-import { isoOf } from './format.js'+import { isoOf, weekKey, weekStartOf } from './format.js'const routineIdsOf = w => (Array.isArray(w.routineIds) ? w.routineIds : (w.routineId ? [w.routineId] : []))const nameParts = w => String(w.name || '').split(' + ')@@ -43,11 +43,11 @@ const queueOf = S => {return { ...q, ids, startsOn: typeof q.startsOn === 'string' ? q.startsOn : isoOf(new Date(q.since || 0)) }}-const isDone = (S, q, id) => {- const name = S.routines.find(r => r.id === id)?.name- return S.workouts.some(w => routineIdsOf(w).includes(id)- && ((w.start ?? 0) >= q.since || (String(w.d || '') >= q.startsOn && nameParts(w).includes(name))))-}+// The DONE RULE for one workout and one session — `isDone` asks it across the log.+const countsFor = (S, q, w, id) => routineIdsOf(w).includes(id)+ && ((w.start ?? 0) >= q.since+ || (String(w.d || '') >= q.startsOn && nameParts(w).includes(S.routines.find(r => r.id === id)?.name)))+const isDone = (S, q, id) => S.workouts.some(w => countsFor(S, q, w, id))/** The week's sessions already done, in slot order. `[]` without a queue. */export function queueDone(S) {@@ -101,3 +101,35 @@ export function queueView(S, today) {waiting: today < q.startsOn,}}++/**+ * The streak card's fraction, `done / planned`, for the week `today` is in. Without a queue it+ * is what it always was: the calendar week's finished workouts over the weekdays that hold a+ * plan (a combined day is one day, one workout). With an active queue the coach week is the+ * unit, and your own training counts beside it: `planned` is the queue's sessions plus the+ * weekdays you plan yourself (a weekday whose routines the queue already covers adds nothing),+ * and `done` is the queue's finished sessions plus this calendar week's workouts on top of them+ * — your own days, freestyle sessions, a redo of a session already done, a queue-routine workout+ * from before the apply. A merged 'US W1 D2 + Core' session counts for both sides: the queue's+ * session through the queue, your Core day as training on top, the two units `planned` added.+ * A queue still ahead of its `startsOn` (queueView's `waiting`) is next week's plan, so until+ * then the card is the calendar-week count alone and the progress row speaks for the coming week.+ */+export function weekTally(S, today) {+ const ws = weekStartOf(S)+ const thisWeek = S.workouts.filter(w => weekKey(w.d, ws) === weekKey(today, ws))+ const q0 = queueOf(S)+ const q = q0 && today >= q0.startsOn ? q0 : null+ // "Covered by the queue" is tested against the raw ids: a queue routine deleted mid-week is+ // gone from `q.ids`, but a weekday pointer to it is still not a day you planned yourself.+ const ownIds = ids => [].concat(ids || []).filter(id => !(q && S.queue.ids.includes(id)))+ const ownDays = Object.values(S.week || {}).filter(ids => ownIds(ids).length).length+ if (!q) return { done: thisWeek.length, planned: ownDays }+ // The workout that earns each session its credit is the earliest one the DONE RULE accepts+ // (by date, not array position — a backfilled log is not always in order); every other+ // workout this week is training on top, and so is a crediting workout's own half.+ const chrono = (a, b) => ((a.d || '') < (b.d || '') ? -1 : (a.d || '') > (b.d || '') ? 1 : (a.start || 0) - (b.start || 0))+ const credited = new Set(q.ids.map(id => S.workouts.filter(w => countsFor(S, q, w, id)).sort(chrono)[0]).filter(Boolean))+ const other = thisWeek.filter(w => !credited.has(w) || ownIds(routineIdsOf(w)).length).length+ return { done: queueDone(S).length + other, planned: q.ids.length + ownDays }+}diff --git a/frontend/src/lib/queue.test.js b/frontend/src/lib/queue.test.jsindex 9c66a4f1b..e750c7e5d 100644--- a/frontend/src/lib/queue.test.js+++ b/frontend/src/lib/queue.test.js@@ -1,9 +1,9 @@import { describe, it, expect } from 'vitest'-import { queueDone, queueRemaining, queueNext, queueView } from './queue.js'+import { queueDone, queueRemaining, queueNext, queueView, weekTally } from './queue.js'// The DONE RULE is shared with the api's reminder copy (and any planner writing S.queue); these// cases are the ones every copy must agree on. Dates are plain strings so nothing here reads the clock.-const routines = [{ id: 'd1', name: 'US W1 D1' }, { id: 'd2', name: 'US W1 D2' }, { id: 'd3', name: 'US W1 D3' }]+const routines = [{ id: 'd1', name: 'US W1 D1' }, { id: 'd2', name: 'US W1 D2' }, { id: 'd3', name: 'US W1 D3' }, { id: 'own', name: 'Core' }]const SINCE = Date.parse('2026-09-07T08:00:00')const TODAY = '2026-09-09'const S = (over = {}) => ({@@ -153,3 +153,89 @@ describe('queueView', () => {expect(queueNext(nullStart, TODAY, TODAY)).toBe('d1')})})++describe('weekTally — the streak card fraction', () => {+ // Monday-start weeks (no S.weekStart); TODAY is Wednesday 2026-09-09, the queue applied Monday.+ const own = (d, over = {}) => w({ id: 'own-' + d, d, start: Date.parse(d + 'T18:00:00'), routineIds: ['own'], name: 'Core', ...over })++ it('without a queue: this calendar week\'s workouts over the weekdays with a plan, as before', () => {+ const s = S({ queue: null, week: { 1: ['own'], 3: ['own', 'd1'], 5: [] }, workouts: [own('2026-09-07'), own('2026-09-05')] })+ expect(weekTally(s, TODAY)).toEqual({ done: 1, planned: 2 })+ expect(weekTally(S({ queue: null }), TODAY)).toEqual({ done: 0, planned: 0 })+ })++ it('a coach week alone: the queue\'s sessions, done by the DONE RULE, whatever calendar week they fell in', () => {+ expect(weekTally(S(), TODAY)).toEqual({ done: 0, planned: 3 })+ expect(weekTally(S({ workouts: [w({ routineIds: ['d1'] })] }), TODAY)).toEqual({ done: 1, planned: 3 })+ // Ran long: a session logged last week still counts — the coach week is the unit.+ const s = S({ queue: { ...S().queue, since: SINCE - 7 * 86400000, startsOn: '2026-08-31' }, workouts: [w({ routineIds: ['d1'], d: '2026-09-04', start: SINCE - 5 * 86400000 })] })+ expect(weekTally(s, TODAY)).toEqual({ done: 1, planned: 3 })+ })++ it('your own weekday rides along: one more planned, and its workout this week one more done', () => {+ const s = S({ week: { 3: ['own'] }, workouts: [w({ routineIds: ['d1'] }), own('2026-09-09')] })+ expect(weekTally(s, TODAY)).toEqual({ done: 2, planned: 4 })+ // …but last week's Core is last week's.+ expect(weekTally(S({ week: { 3: ['own'] }, workouts: [own('2026-09-02')] }), TODAY)).toEqual({ done: 0, planned: 4 })+ })++ it('a weekday that holds only queue routines (the planner\'s old pointers) adds no day', () => {+ expect(weekTally(S({ week: { 1: ['d1'], 3: ['d2'], 5: ['d3'] } }), TODAY)).toEqual({ done: 0, planned: 3 })+ expect(weekTally(S({ week: { 1: ['d1', 'own'] } }), TODAY)).toEqual({ done: 0, planned: 4 })+ // …even when the planner has since deleted that routine: the pointer is still not your day.+ expect(weekTally(S({ queue: { ...S().queue, ids: ['d1', 'd2', 'gone'] }, week: { 5: ['gone'] } }), TODAY)).toEqual({ done: 0, planned: 2 })+ })++ it('a merged session counts for both sides — following the app\'s own daily proposal all week ends at 4 / 4', () => {+ // Wednesday's today row is 'US W1 D2 + Core' (the queue session with the weekday's own+ // routine riding along) and Start logs it as ONE workout: the coach session is credited+ // through the queue, the Core day as training on top — the two units `planned` counted.+ const merged = w({ routineIds: ['d1', 'own'], name: 'US W1 D1 + Core' })+ expect(weekTally(S({ week: { 3: ['own'] }, workouts: [merged] }), TODAY)).toEqual({ done: 2, planned: 4 })+ const week = [+ w({ id: 'mon', d: '2026-09-07', start: SINCE + 3600000, routineIds: ['d1'], name: 'US W1 D1' }),+ w({ id: 'wed', d: '2026-09-09', start: SINCE + 2 * 86400000, routineIds: ['d2', 'own'], name: 'US W1 D2 + Core' }),+ w({ id: 'fri', d: '2026-09-11', start: SINCE + 4 * 86400000, routineIds: ['d3'], name: 'US W1 D3' }),+ ]+ expect(weekTally(S({ week: { 3: ['own'] }, workouts: week }), '2026-09-13')).toEqual({ done: 4, planned: 4 })+ expect(queueDone(S({ workouts: week }))).toEqual(['d1', 'd2', 'd3'])+ })++ it('every other workout is training on top: freestyle, a redo of a done session, a pre-apply coach-routine one', () => {+ // A second D1 this week is not a second credit, but it is a workout done — never invisible.+ const redo = w({ id: 'w2', d: '2026-09-09', start: SINCE + 2 * 86400000, routineIds: ['d1'], name: 'US W1 D1' })+ expect(weekTally(S({ workouts: [w({ routineIds: ['d1'] }), redo] }), TODAY)).toEqual({ done: 2, planned: 3 })+ expect(queueDone(S({ workouts: [w({ routineIds: ['d1'] }), redo] }))).toEqual(['d1'])+ // The credit goes to the EARLIEST qualifying workout whatever the array order: D1 done last+ // week (the week ran long) and again this week is one credit plus one workout on top.+ const long = { ...S().queue, since: SINCE - 7 * 86400000, startsOn: '2026-08-31' }+ const sat = w({ id: 'sat', d: '2026-09-05', start: SINCE - 2 * 86400000, routineIds: ['d1'], name: 'US W1 D1' })+ expect(weekTally(S({ queue: long, workouts: [sat, redo] }), TODAY)).toEqual({ done: 2, planned: 3 })+ expect(weekTally(S({ queue: long, workouts: [redo, sat] }), TODAY)).toEqual({ done: 2, planned: 3 })+ const freestyle = w({ routineIds: [], name: 'Freestyle', d: '2026-09-08' })+ expect(weekTally(S({ workouts: [freestyle] }), TODAY)).toEqual({ done: 1, planned: 3 })+ // Monday morning's session on a coach routine, before Monday noon's apply: not this week's+ // D1 (the row stays at 0 / 3), but training done this week all the same.+ const before = w({ routineIds: ['d1'], name: 'US W3 D1', d: '2026-09-07', start: SINCE - 3600000 })+ expect(weekTally(S({ workouts: [before] }), TODAY)).toEqual({ done: 1, planned: 3 })+ expect(queueDone(S({ workouts: [before] }))).toEqual([])+ })++ it('a queue waiting on a future startsOn is next week\'s plan: the card counts this calendar week alone', () => {+ // W1 finished Thursday; the planner applied W2 (four sessions) Thursday night for Monday.+ // Friday's card says what this week did — 3 of the 0 own days planned — not 3 / 4 or 3 / 7.+ const w1 = ['2026-09-07', '2026-09-08', '2026-09-10'].map((d, i) => w({ id: 'w' + i, d, start: Date.parse(d + 'T18:00:00'), routineIds: ['d' + (i + 1)], name: 'US W1 D' + (i + 1) }))+ const w2 = { ids: ['d1', 'd2', 'd3', 'own'], since: Date.parse('2026-09-10T21:00:00'), startsOn: '2026-09-14', label: 'US W2' }+ expect(weekTally(S({ queue: w2, workouts: w1 }), '2026-09-11')).toEqual({ done: 3, planned: 0 })+ // Your own weekday is still this week's, and the waiting queue does not hide it.+ expect(weekTally(S({ queue: w2, week: { 5: ['own'] }, workouts: w1 }), '2026-09-11')).toEqual({ done: 3, planned: 1 })+ // Monday it is active: four sessions, and 'own' is now the queue's, so the weekday adds nothing.+ expect(weekTally(S({ queue: w2, week: { 5: ['own'] }, workouts: w1 }), '2026-09-14')).toEqual({ done: 0, planned: 4 })+ })++ it('follows the week-start setting: a Sunday workout is this week\'s for a Sunday start, last week\'s for Monday', () => {+ const s = S({ queue: null, week: { 0: ['own'] }, workouts: [own('2026-09-06')] })+ expect(weekTally({ ...s, weekStart: 0 }, TODAY)).toEqual({ done: 1, planned: 1 })+ expect(weekTally({ ...s, weekStart: 1 }, TODAY)).toEqual({ done: 0, planned: 1 })+ })+})diff --git a/frontend/src/sheets.dayoverride.test.jsx b/frontend/src/sheets.dayoverride.test.jsxnew file mode 100644index 000000000..55930c850--- /dev/null+++ b/frontend/src/sheets.dayoverride.test.jsx@@ -0,0 +1,69 @@+// @vitest-environment happy-dom+// The day sheet's header names the plan the day would have without its override. In a coach+// week that is the queue's session for the day, in front of the weekday's own routines — the+// same answer the Home row gives — not the weekday list alone.+import React, { act } from 'react'+import { afterEach, beforeEach, describe, expect, it } from 'vitest'+import { createRoot } from 'react-dom/client'+import { useStore } from './store/useStore.js'+import { useUI } from './store/useUI.js'+import { dayOverrideSheet } from './sheets.jsx'+import { todayISO, isoOf } from './lib/format.js'++const routines = [+ { id: 'd1', name: 'US W1 D1', emoji: null, ex: [{ id: '0025' }] },+ { id: 'd2', name: 'US W1 D2', emoji: null, ex: [{ id: '0025' }] },+ { id: 'own', name: 'Core', emoji: null, ex: [{ id: '0025' }] },+]+const SINCE = Date.now() - 2 * 86400000+const daysFromToday = n => { const d = new Date(todayISO() + 'T12:00:00'); d.setDate(d.getDate() + n); return isoOf(d) }+const queue = { ids: ['d1', 'd2'], since: SINCE, startsOn: daysFromToday(-1), label: 'US W1' }+const mounted = []++function renderTop() {+ const sheet = useUI.getState().sheets.at(-1)+ const host = document.createElement('div')+ document.body.appendChild(host)+ const root = createRoot(host)+ mounted.push(root)+ act(() => root.render(sheet.render(() => useUI.getState().closeSheet(sheet.id))))+ return host+}+const header = host => host.querySelector('.muted.small').textContent+const setS = (over = {}) => useStore.setState(s => ({ S: { ...s.S, routines, week: {}, dayPlan: {}, workouts: [], active: null, queue, ...over }, user: null }))++beforeEach(() => {+ globalThis.IS_REACT_ACT_ENVIRONMENT = true+ useUI.setState({ sheets: [], toastMsg: '' })+ document.body.innerHTML = ''+})+afterEach(() => { act(() => { mounted.splice(0).forEach(r => r.unmount()) }) })++describe('Day sheet header in a coach week', () => {+ it('names the queue session for today, with the weekday\'s own routine after it', () => {+ setS()+ dayOverrideSheet(todayISO())+ expect(header(renderTop())).toMatch(/^Weekly plan: US W1 D1/)+ setS({ week: { [new Date().getDay()]: ['own'] } })+ dayOverrideSheet(todayISO())+ expect(header(renderTop())).toMatch(/^Weekly plan: US W1 D1 \+ Core/)+ })++ it('a rest override for today still shows the plan it replaced, marked as changed', () => {+ setS({ dayPlan: { [todayISO()]: 'rest' } })+ dayOverrideSheet(todayISO())+ const h = header(renderTop())+ expect(h).toMatch(/^Weekly plan: US W1 D1/)+ expect(h).toContain('changed for this day')+ })++ it('another day has no queue session: the weekday alone, or Rest', () => {+ const tomorrow = daysFromToday(1)+ setS({ week: { [new Date(tomorrow + 'T12:00:00').getDay()]: ['own'] } })+ dayOverrideSheet(tomorrow)+ expect(header(renderTop())).toMatch(/^Weekly plan: Core/)+ setS()+ dayOverrideSheet(tomorrow)+ expect(header(renderTop())).toMatch(/^Weekly plan: Rest/)+ })+})diff --git a/frontend/src/sheets.jsx b/frontend/src/sheets.jsxindex 71020da59..7508426ee 100644--- a/frontend/src/sheets.jsx+++ b/frontend/src/sheets.jsx@@ -1810,9 +1810,11 @@ function PlanImport({ bundle, close }) {/* ============================ day override / assign ============================ */function DayOverride({ iso, close }) {const st = useStore(s => s.S)- const wd = new Date(iso + 'T12:00:00').getDay()- const weeklyNames = [].concat(st.week[wd] || []).map(id => st.routines.find(r => r.id === id)?.name).filter(Boolean)const hasOvr = st.dayPlan[iso] !== undefined+ // The plan the day would have without its override — the weekday's routines, and in a coach+ // week the queue's session for the day in front of them, same as the Home row shows.+ const { [iso]: _ovr, ...noOvr } = st.dayPlan+ const weeklyNames = effectiveRoutineIds({ ...st, dayPlan: noOvr }, iso).map(id => st.routines.find(r => r.id === id)?.name).filter(Boolean)// A weekday can hold several routines; the per-date override stays single-pick, so picking// one here collapses a combined day to it (docs/COMBINE_ROUTINES.md §8). The check marks// show everything currently planned for the day.diff --git a/frontend/src/views/Home.jsx b/frontend/src/views/Home.jsxindex 73a9fc2cd..4cc1ad633 100644--- a/frontend/src/views/Home.jsx+++ b/frontend/src/views/Home.jsx@@ -2,13 +2,13 @@ import { useState } from 'react'import { useNavigate } from 'react-router-dom'import { useStore } from '../store/useStore.js'import { effectiveRoutines, effectiveRoutineIds, nextTrainingDay, streakWeeks, lastBW, setsDoneActive } from '../lib/history.js'-import { fmtNum, fmtDate, todayISO, isoOf, weekKey, weekStartOf, weekDayOffset, DAYS, DAYN } from '../lib/format.js'+import { fmtNum, fmtDate, todayISO, isoOf, weekStartOf, weekDayOffset, DAYS, DAYN } from '../lib/format.js'import { t, dateLocale } from '../lib/i18n.js'import { bwSheet, goalSheet, dayOverrideSheet, calendarSheet, startFlow, starterPlanSheet, bwDeltaColor, weighInsSheet } from '../sheets.jsx'import LineChart from '../components/LineChart.jsx'import Icon from '../components/Icon.jsx'import QueueRow from '../components/QueueRow.jsx'-import { queueView } from '../lib/queue.js'+import { queueView, weekTally } from '../lib/queue.js'import { Button } from '../components/ui.jsx'import { tappable } from '../lib/use-sheet-keyboard.js'import { glyphOf } from '../lib/glyphs.js'@@ -57,16 +57,12 @@ export default function Home() {const wkEnd = new Date(wkStart); wkEnd.setDate(wkStart.getDate() + 6)const wkLabel = weekOffset === 0 ? t('This week') : `${wkStart.getDate()} ${wkStart.toLocaleDateString(dateLocale(), { month: 'short' })} – ${wkEnd.getDate()} ${wkEnd.toLocaleDateString(dateLocale(), { month: 'short' })}`- const wThisWeek = S.workouts.filter(w => weekKey(w.d, ws) === weekKey(todayISO(), ws)).length// A coach week (S.queue) read through the same tolerant reader QueueRow uses, so a malformed// queue from another client shows the weekday dots, never an empty card.const queue = queueView(S, todayISO())- // Days scheduled, not routines — a combined day counts as 1, matching wThisWeek (one w). A- // coach week has no weekdays: its sessions are the plan.- const plannedPerWeek = queue ? queue.items.length : Object.values(S.week).filter(ids => ids?.length).length- // …and the numerator counts the same thing: the queue's done sessions, not the calendar week's- // workouts (a coach week may straddle a weekend, and both fractions sit on one card).- const doneThisWeek = queue ? queue.items.length - queue.remaining.length : wThisWeek+ // The streak card's fraction: this calendar week's workouts over the weekdays with a plan, or,+ // in a coach week, the queue's sessions and your own days together (lib/queue.js weekTally).+ const { done: doneThisWeek, planned: plannedPerWeek } = weekTally(S, todayISO())const bwPoints = S.bodyweight.slice(-30).map(b => ({ t: b.t || new Date(b.d).getTime(), y: b.w, d: b.d }))// today's session shown right under the week stripdiff --git a/frontend/src/views/Home.queue.test.jsx b/frontend/src/views/Home.queue.test.jsxindex 13d20b095..6dac4fee6 100644--- a/frontend/src/views/Home.queue.test.jsx+++ b/frontend/src/views/Home.queue.test.jsx@@ -54,6 +54,7 @@ const chips = () => [...host.querySelectorAll('.queue .chip')]const chipTexts = () => chips().map(c => c.textContent)const status = () => host.querySelector('.queue .queue-status')?.textContentconst todayTitle = () => host.querySelector('.today-row .ttl')?.textContent+const card = () => [...host.querySelectorAll('.card .muted.small')].map(e => e.textContent).find(x => x.includes('this week'))describe('Home — coach week progress row', () => {it('replaces the weekday dots with one chip per session, in slot order, and lights the first', () => {@@ -138,6 +139,24 @@ describe('Home — coach week progress row', () => {expect(todayTitle()).toBe('Core')})+ it('the streak card counts the coach sessions done, wherever in the calendar they fell', () => {+ // Ran long: the queue applied ten days ago and D1 logged the day after — never in this+ // calendar week, whichever weekday today is, so a calendar-week count would show 0 / 3.+ const since = Date.now() - 10 * 86400000, start = since + 3600000+ setS({ queue: queue({ since, startsOn: daysFromToday(-9) }), workouts: [{ ...logged('d1'), d: isoOf(new Date(start)), start, end: start + 3600000 }] })+ mount()+ expect(card()).toMatch(/^1 \/ 3 this week · 1 workout total$/)+ })++ it('the streak card counts your own day beside the coach week, planned and done', () => {+ const now = Date.now()+ const core = { id: 'w-own', d: todayISO(), start: now - 3600000, end: now, routineIds: ['own'], routineId: 'own', name: 'Core', entries: [] }+ setS({ week: { [new Date().getDay()]: ['own'] }, workouts: [logged('d1'), core] })+ mount()+ expect(card()).toMatch(/^2 \/ 4 this week · 2 workouts total$/)+ expect(host.querySelectorAll('.queue .row .small')[1].textContent).toBe('1 / 3')+ })+it('without a queue the weekday dots are back', () => {setS({ queue: null })mount()