Skip to content

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.

queue: queueOf is the shared, exported projection — dedupe, the combined-day pin, and the malformed-queue fallback, in both runtimes

giulioleuciece0da25cf59
diff --git a/api/queue.js b/api/queue.js
index d453495d3..6b654f6b5 100644
--- a/api/queue.js
+++ b/api/queue.js
@@ -2,7 +2,8 @@
// imported) from frontend/src/lib/queue.js / lib/history.js: the same rules on both sides, kept
// as a tiny standalone module here (rather than inline in server.js) so it can be unit-tested
// without booting the whole server. `S.queue` is written by a planner — any API client that PUTs
-// state with a queue (a coaching agent, a script) — never by the app itself. Three rules live here:
+// state with a queue (a coaching agent, a script) — or by the frontend's own rotation feature
+// (marked by `rotationId`; see frontend/src/lib/rotation.js). Three rules live here:
//
// DONE RULE (shared with the frontend, and the rule a planner has to mirror): a queue session
// counts as done once a finished workout on its routine exists, started at/after the queue was
@@ -53,7 +54,9 @@ function queueOf(S) {
const q = S.queue;
if (!q || typeof q !== 'object' || !Array.isArray(q.ids)) return null;
const startsOn = typeof q.startsOn === 'string' ? q.startsOn : dayIn(q.since || 0, S.reminder?.tz);
- const ids = q.ids.filter(id => (S.routines || []).some(r => r?.id === id));
+ // First valid occurrence of each id — the same normalization as the frontend's queueOf.
+ const ids = q.ids.filter((id, i) => q.ids.indexOf(id) === i && (S.routines || []).some(r => r?.id === id));
+ if (!ids.length) return null;
return { ids, startsOn };
}
// The reminder tick only ever asks "what's next right now" — there is no separate "today" to
diff --git a/api/test/queue.test.js b/api/test/queue.test.js
index 3219875c6..ce9d5e1f1 100644
--- a/api/test/queue.test.js
+++ b/api/test/queue.test.js
@@ -225,3 +225,18 @@ test('a queue without startsOn starts on the day of the apply where the USER is
// An explicit startsOn is never second-guessed.
assert.equal(effectiveRoutineId({ ...base, queue: { ...q, startsOn: '2026-09-10' }, reminder: { tz: 'America/New_York' } }, '2026-09-09'), null);
});
+
+test('queue: a malformed queue falls back to the weekday plan for the reminder', () => {
+ const S = { ...base, week: { 1: 'r1' }, queue: { ids: ['gone'], since: 1000 } };
+ assert.equal(effectiveRoutineId(S, '2026-09-14'), 'r1'); // Monday
+});
+
+test('queue: a rotation pass hides only the weekdays it covers', () => {
+ const S = {
+ ...base,
+ week: { 1: ['r1', 'r3'] },
+ queue: { ids: ['r1', 'r2'], since: 1000, startsOn: '2026-09-07', label: 'My split', rotationId: 'x' },
+ };
+ // r1 is the queue's own session, so the weekday's r1 adds nothing; r3 is the athlete's own day.
+ assert.equal(effectiveRoutineId(S, '2026-09-14'), 'r1');
+});
diff --git a/frontend/src/lib/history.test.js b/frontend/src/lib/history.test.js
index 033b8e2f0..6735c74f0 100644
--- a/frontend/src/lib/history.test.js
+++ b/frontend/src/lib/history.test.js
@@ -1492,3 +1492,26 @@ describe('per-side volume and legacy timed sets (QA round 2026-09-12)', () => {
expect(setLabel('0025', { w: 60, r: 10, done: true })).toBe('60×10')
})
})
+
+describe('a rotation day and a fixed weekday day are one combined day', () => {
+ const routines = [{ id: 'a', name: 'A', ex: [] }, { id: 'core', name: 'Core', ex: [] }]
+ const TODAY = '2026-09-14' // a Monday
+ const S = over => ({
+ routines, workouts: [], dayPlan: {}, week: { 1: ['core'] },
+ queue: { ids: ['a'], since: Date.parse('2026-09-13T08:00:00'), startsOn: '2026-09-13', label: 'My split', rotationId: 'r1' },
+ rotation: { id: 'r1', sequence: ['a'], label: 'My split' }, ...over,
+ })
+
+ it('the queue session comes first, the weekday routine rides along', () => {
+ expect(effectiveRoutineIds(S(), TODAY, TODAY)).toEqual(['a', 'core'])
+ })
+
+ it('a malformed queue falls back to the plain weekday plan', () => {
+ expect(effectiveRoutineIds(S({ queue: { ids: ['gone'], since: 1 } }), TODAY, TODAY)).toEqual(['core'])
+ })
+
+ it('a repeated queue id does not repeat the day', () => {
+ const dup = S({ queue: { ids: ['a', 'a'], since: Date.parse('2026-09-13T08:00:00'), startsOn: '2026-09-13', label: 'My split' } })
+ expect(effectiveRoutineIds(dup, TODAY, TODAY)).toEqual(['a', 'core'])
+ })
+})
diff --git a/frontend/src/lib/queue.js b/frontend/src/lib/queue.js
index 64248a97b..d4c335bad 100644
--- a/frontend/src/lib/queue.js
+++ b/frontend/src/lib/queue.js
@@ -8,10 +8,14 @@
// since: number ms epoch of the apply; a workout STARTED at/after it is this week's
// startsOn: 'YYYY-MM-DD' local date the queue becomes active (the planner's calendar-week rule)
// label: string progress-row title, e.g. 'US W1'
+// rotationId?: string set only by the in-app rotation editor (lib/rotation.js): this pass
+// is managed here and refills itself when complete. A planner's queue
+// omits it and is never rewritten by the app.
// }
//
-// The app never writes `S.queue`; a planner does — any API client that PUTs state with a queue
-// (a coaching agent, a script). null = the weekday model alone.
+// The app writes `S.queue` itself only for the in-app rotation feature (lib/rotation.js, marked
+// by `rotationId`); otherwise a planner does — any API client that PUTs state with a queue (a
+// coaching agent, a script). null = the weekday model alone.
//
// PINS — a session can be given a day. The day sheet writes the per-date override that already
// exists (`S.dayPlan[iso] = <queue routine id>`); nothing new is stored. A pin re-dates the
@@ -39,15 +43,19 @@ const nameParts = w => String(w.name || '').split(' + ')
// Tolerant reader: whole-state sync is last-writer-wins, so a half-written or older-shaped
// queue can arrive from another client. A bad shape reads as "no queue" rather than taking
-// Home down with it. A session whose routine no longer exists (deleted in the editor — the
-// app never rewrites S.queue) is dropped from the week: it could never be started or logged,
+// Home down with it. A session whose routine no longer exists (deleted in the editor — a
+// planner's queue is never rewritten by the app; a rotation-managed one is, by lib/rotation.js)
+// is dropped from the week: it could never be started or logged,
// and keeping it would pin the whole week on it; a queue left with no session at all reads as
// no queue. A missing startsOn means active since the day of the apply.
-const queueOf = S => {
+export const queueOf = S => {
const q = S?.queue
if (!q || typeof q !== 'object' || !Array.isArray(q.ids)) return null
const routines = Array.isArray(S.routines) ? S.routines : []
- const ids = q.ids.filter(id => routines.some(r => r.id === id))
+ // First valid occurrence of each id: a queue is a set of sessions, and a repeated id would
+ // otherwise show twice in the progress row and count twice in the week's tally. The persisted
+ // payload is never rewritten — normalizing is a read, not a migration.
+ const ids = q.ids.filter((id, i) => q.ids.indexOf(id) === i && routines.some(r => r.id === id))
if (!ids.length) return null
return { ...q, ids, startsOn: typeof q.startsOn === 'string' ? q.startsOn : isoOf(new Date(q.since || 0)) }
}
diff --git a/frontend/src/lib/queue.test.js b/frontend/src/lib/queue.test.js
index e61de4f1e..c5358aaa9 100644
--- a/frontend/src/lib/queue.test.js
+++ b/frontend/src/lib/queue.test.js
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'
-import { queueDone, queueRemaining, queueNext, queueView, weekTally, pinState } from './queue.js'
+import { queueDone, queueRemaining, queueNext, queueView, weekTally, pinState, queueOf } 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.
@@ -315,3 +315,23 @@ describe('weekTally — the streak card fraction', () => {
expect(weekTally({ ...s, weekStart: 1 }, TODAY)).toEqual({ done: 0, planned: 1 })
})
})
+
+describe('queueOf — the shared normalization', () => {
+ it('keeps the first occurrence of a repeated id and drops deleted ones', () => {
+ const st = S({ queue: { ids: ['d1', 'd2', 'd1', 'gone'], since: SINCE, startsOn: '2026-09-07', label: 'US W1' } })
+ expect(queueOf(st).ids).toEqual(['d1', 'd2'])
+ // the persisted payload is left exactly as it arrived
+ expect(st.queue.ids).toEqual(['d1', 'd2', 'd1', 'gone'])
+ })
+
+ it('a malformed, duplicate-only or all-deleted queue reads as no queue', () => {
+ expect(queueOf(S({ queue: null }))).toBe(null)
+ expect(queueOf(S({ queue: { ids: 'nope' } }))).toBe(null)
+ expect(queueOf(S({ queue: { ids: ['gone'], since: SINCE } }))).toBe(null)
+ })
+
+ it('a repeated id is one chip, not two', () => {
+ const st = S({ queue: { ids: ['d1', 'd1', 'd2'], since: SINCE, startsOn: '2026-09-07', label: 'US W1' } })
+ expect(queueView(st, TODAY).items.map(i => i.id)).toEqual(['d1', 'd2'])
+ })
+})