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
API tests: fixture dates follow the calendar
Duarte Santos0526d6572597
diff --git a/api/test/helpers.mjs b/api/test/helpers.mjsindex 418380719..383b665f2 100644--- a/api/test/helpers.mjs+++ b/api/test/helpers.mjs@@ -53,6 +53,27 @@ export function writeState(dir, uid, S) {fs.writeFileSync(path.join(dir, 'state-' + uid + '.json'), JSON.stringify(S));}+/* Fixture dates that follow the calendar.+ *+ * The Coach reads a window that ends today (MAX_WEEKS back from now), so a literal fixture date+ * walks out of it one day and every review test reads an empty log: ten tests failed on+ * 2026-10-12 for no other reason than the date. D() moves a fixture date by whole weeks, the+ * same number for every date in a run, so the newest fixture day (FIXTURE_ANCHOR) lands in the+ * last seven days before today (UTC, as the payload's own dates are). Weekdays and the gaps+ * between fixture dates stay exactly as written, and nothing lands in the future.+ * Write fixtures as D('2026-07-20'), and assertions about them the same way. */+export const FIXTURE_ANCHOR = '2026-07-20';+const DAY_MS = 86400000;+const shiftWeeks = (() => {+ const today = Date.parse(new Date().toISOString().slice(0, 10) + 'T00:00:00Z');+ const anchor = Date.parse(FIXTURE_ANCHOR + 'T00:00:00Z');+ return Math.floor((today - DAY_MS - anchor) / (7 * DAY_MS));+})();+export function D(iso) {+ const t = Date.parse(String(iso).slice(0, 10) + 'T00:00:00Z');+ return new Date(t + shiftWeeks * 7 * DAY_MS).toISOString().slice(0, 10) + String(iso).slice(10);+}+/** A profile that has consented and has some history — the usual starting point. */export function sampleState(over = {}) {return {@@ -68,10 +89,10 @@ export function sampleState(over = {}) {week: { 1: 'r1', 3: 'r1', 5: 'r1' },dayPlan: {},exWeights: { '0001': { w: 20 } },- bodyweight: [{ d: '2026-07-01', w: 78 }, { d: '2026-07-20', w: 78.5 }],+ bodyweight: [{ d: D('2026-07-01'), w: 78 }, { d: D('2026-07-20'), w: 78.5 }],customEx: [],workouts: [{- id: 'w1', d: '2026-07-20', name: 'Full body A', start: 1000, end: 1000 + 45 * 60000, vol: 600, prs: [],+ id: 'w1', d: D('2026-07-20'), name: 'Full body A', start: 1000, end: 1000 + 45 * 60000, vol: 600, prs: [],entries: [{id: '0001', target: { sets: 3, reps: 10, weight: 20 },sets: [{ w: 20, r: 10, done: true, rpe: 9.5 }, { w: 20, r: 9, done: true, rpe: 10 }, { w: 20, r: 8, done: true, rpe: 10 }]diff --git a/api/test/payload-bounds.test.js b/api/test/payload-bounds.test.jsindex 0a0b12baf..0be2ceeba 100644--- a/api/test/payload-bounds.test.js+++ b/api/test/payload-bounds.test.js@@ -7,7 +7,7 @@import test from 'node:test';import assert from 'node:assert/strict';import fs from 'node:fs';-import { tempData, sampleState } from './helpers.mjs';+import { tempData, sampleState, D } from './helpers.mjs';tempData();const payload = await import('../coach/core/payload.js');@@ -144,7 +144,7 @@ const planAndLogFields = {'logged entry id': S => { S.workouts[0].entries[0].id = INJECT; },'logged target': S => { S.workouts[0].entries[0].target = { sets: INJECT, reps: INJECT, sec: INJECT, weight: INJECT }; },'logged set values': S => { Object.assign(S.workouts[0].entries[0].sets[0], { w: INJECT, r: INJECT, sec: INJECT, min: INJECT, speed: INJECT, rir: INJECT, rpe: INJECT }); },- 'body-weight weigh-in': S => { S.bodyweight.push({ d: '2026-07-21', w: INJECT }, { d: '2026-07-21' + INJECT, w: 80 }); },+ 'body-weight weigh-in': S => { S.bodyweight.push({ d: D('2026-07-21'), w: INJECT }, { d: D('2026-07-21') + INJECT, w: 80 }); },'target body weight': S => { S.targetW = INJECT; }};@@ -204,7 +204,7 @@ test('the plan and the log a real app writes pass through exactly as before', ()});assert.deepEqual(p.window.workouts.at(-1).entries[0].sets[0], { done: true, w: 20, r: 10, rpe: 9.5 });assert.deepEqual(p.window.workouts.at(-1).entries[0].target, { sets: 3, reps: 10, sec: undefined, weight: 20 });- assert.deepEqual(p.bodyweight, { goal: 80, series: [{ d: '2026-07-20', w: 78.5 }] });+ assert.deepEqual(p.bodyweight, { goal: 80, series: [{ d: D('2026-07-20'), w: 78.5 }] });});test('the cohort is bounded where it joins the payload, whoever built it', () => {diff --git a/api/test/payload.test.js b/api/test/payload.test.jsindex e2cd95a15..abffe2f62 100644--- a/api/test/payload.test.js+++ b/api/test/payload.test.js@@ -1,6 +1,6 @@import test from 'node:test';import assert from 'node:assert/strict';-import { tempData, sampleState } from './helpers.mjs';+import { tempData, sampleState, D } from './helpers.mjs';tempData();const payload = await import('../coach/core/payload.js');@@ -63,7 +63,7 @@ test('review payload carries the plan, the window, effort and aggregates', () =>test('a stalling exercise shows up in the aggregates the way the engine counts it', () => {const S = sampleState();// Three sessions that all fell short of the 10-rep target.- S.workouts = ['2026-07-06', '2026-07-13', '2026-07-20'].map((d, i) => ({+ S.workouts = ['2026-07-06', '2026-07-13', '2026-07-20'].map(D).map((d, i) => ({id: 'w' + i, d, name: 'A', start: 0, end: 60000, entries: [{id: '0001', target: { sets: 3, reps: 10, weight: 20 },sets: [{ w: 20, r: 9, done: true }, { w: 20, r: 8, done: true }, { w: 20, r: 7, done: true }]@@ -78,7 +78,7 @@ test('a stalling exercise shows up in the aggregates the way the engine counts itest('a set that was never ticked off is a miss, not a gap', () => {const S = sampleState();S.workouts = [{- id: 'w1', d: '2026-07-20', name: 'A', start: 0, end: 60000, entries: [{+ id: 'w1', d: D('2026-07-20'), name: 'A', start: 0, end: 60000, entries: [{id: '0001', target: { sets: 3, reps: 10, weight: 20 },sets: [{ w: 20, r: 10, done: true }, { w: 20, r: 10, done: true }, { w: 20, r: 10, done: false }]}]@@ -212,10 +212,10 @@ test('declined changes are carried forward so the Coach does not nag', () => {/* ---------- debriefs and the cohort ---------- */const debriefState = () => sampleState({workouts: [- { id: 'w0', d: '2026-07-06', name: 'Full body A', start: 1000, end: 1000 + 40 * 60000, vol: 500, prs: [], entries: [{ id: '0001', sets: [{ w: 18, r: 10, done: true }] }] },- { id: 'wx', d: '2026-07-10', name: 'Other', start: 1000, end: 1000 + 30 * 60000, vol: 100, prs: [], entries: [{ id: '0007', sets: [{ sec: 45, done: true }] }] },- { id: 'w1', d: '2026-07-13', name: 'Full body A', start: 1000, end: 1000 + 42 * 60000, vol: 560, prs: [], entries: [{ id: '0001', sets: [{ w: 20, r: 9, done: true }] }] },- { id: 'w2', d: '2026-07-20', name: 'Full body A', start: 1000, end: 1000 + 45 * 60000, vol: 600, prs: ['0001'], entries: [{ id: '0001', target: { sets: 3, reps: 10, weight: 20 }, sets: [{ w: 20, r: 10, done: true, warmup: true }, { w: 20, r: 10, done: true }, { w: 20, r: 9, done: true }, { w: 20, r: 8, done: false }] }] }+ { id: 'w0', d: D('2026-07-06'), name: 'Full body A', start: 1000, end: 1000 + 40 * 60000, vol: 500, prs: [], entries: [{ id: '0001', sets: [{ w: 18, r: 10, done: true }] }] },+ { id: 'wx', d: D('2026-07-10'), name: 'Other', start: 1000, end: 1000 + 30 * 60000, vol: 100, prs: [], entries: [{ id: '0007', sets: [{ sec: 45, done: true }] }] },+ { id: 'w1', d: D('2026-07-13'), name: 'Full body A', start: 1000, end: 1000 + 42 * 60000, vol: 560, prs: [], entries: [{ id: '0001', sets: [{ w: 20, r: 9, done: true }] }] },+ { id: 'w2', d: D('2026-07-20'), name: 'Full body A', start: 1000, end: 1000 + 45 * 60000, vol: 600, prs: ['0001'], entries: [{ id: '0001', target: { sets: 3, reps: 10, weight: 20 }, sets: [{ w: 20, r: 10, done: true, warmup: true }, { w: 20, r: 10, done: true }, { w: 20, r: 9, done: true }, { w: 20, r: 8, done: false }] }] }]});@@ -224,11 +224,11 @@ test('a debrief payload carries one session, its predecessors of the same routinassert.equal(p.task, 'debrief');assert.equal(p.session.id, 'w2');assert.equal(p.session.entries[0].sets.length, 4);- assert.deepEqual(p.previous.map(w => w.d), ['2026-07-06', '2026-07-13']);+ assert.deepEqual(p.previous.map(w => w.d), [D('2026-07-06'), D('2026-07-13')]);assert.equal('library' in p, false);assert.equal('window' in p, false);assert.ok(p.aggregates && Array.isArray(p.aggregates.exercises));- assert.ok(p.bodyweight.series.every(b => b.d <= '2026-07-20'));+ assert.ok(p.bodyweight.series.every(b => b.d <= D('2026-07-20')));});test('a set taken to failure reaches the coach as failure, a warm-up never does', () => {@@ -253,10 +253,10 @@ test('an unknown workout id falls back to the latest session', () => {test('workoutMeta counts done work sets, keeps the stored volume and the PR count', () => {const m = payload.workoutMeta(debriefState(), 'w2');- assert.deepEqual(m, { id: 'w2', d: '2026-07-20', name: 'Full body A', minutes: 45, vol: 600, sets: 2, prs: 1 });+ assert.deepEqual(m, { id: 'w2', d: D('2026-07-20'), name: 'Full body A', minutes: 45, vol: 600, sets: 2, prs: 1 });assert.equal(payload.workoutMeta(sampleState({ workouts: [] }), 'w2'), null);// No stored volume: computed from the work sets.- const S = sampleState({ workouts: [{ id: 'q', d: '2026-07-01', start: 1, end: 60001, entries: [{ id: '0001', sets: [{ w: 10, r: 10, done: true }, { w: 10, r: 10, done: true, phase: 'warmup' }] }] }] });+ const S = sampleState({ workouts: [{ id: 'q', d: D('2026-07-01'), start: 1, end: 60001, entries: [{ id: '0001', sets: [{ w: 10, r: 10, done: true }, { w: 10, r: 10, done: true, phase: 'warmup' }] }] }] });assert.equal(payload.workoutMeta(S, 'q').vol, 100);});@@ -401,7 +401,7 @@ test('a session note reaches the model whole, bounded only at the app\'s own capid: 'w' + d, d, name: 'A', start: 0, end: 60000, prs: [], note,entries: [{ id: '0001', target: { sets: 3, reps: 10, weight: 20 }, sets: [{ w: 20, r: 10, done: true }] }]});- S.workouts = [mk('2026-09-02', whole), mk('2026-09-03', 'y'.repeat(payload.NOTE_MAX + 100))]; // only a document edited outside the app carries the second+ S.workouts = [mk(D('2026-07-19'), whole), mk(D('2026-07-20'), 'y'.repeat(payload.NOTE_MAX + 100))]; // only a document edited outside the app carries the secondconst p = payload.build(S, { handle: handleFor('u1'), kind: 'review' });const [full, edited] = p.window.workouts;assert.equal(full.compact, undefined, 'both sessions are in full detail');diff --git a/api/test/routes.test.js b/api/test/routes.test.jsindex 0016a0cec..528e60b08 100644--- a/api/test/routes.test.js+++ b/api/test/routes.test.js@@ -181,7 +181,7 @@ test('the disclosure says which host a job would go to, and nothing more of thetest('a debrief is enqueued as its own kind, and the cohort routes gate on the admin switch and the opt-in', async () => {fresh({ community: false });const jobs = await import('../coach/jobs.js');- const { writeState, sampleState } = await import('./helpers.mjs');+ const { writeState, sampleState, D } = await import('./helpers.mjs');const { forcePrivilegeVerdict } = await import('../coach/adapters/spawn.js');forcePrivilegeVerdict({ ok: true, dropped: false, why: 'pinned by the test suite' });writeState(process.env.DATA_DIR, 'admin-1', sampleState());@@ -213,7 +213,7 @@ test('a debrief is enqueued as its own kind, and the cohort routes gate on the awhile (jobs.status('admin-1').job && Date.now() < until) await new Promise(res => setTimeout(res, 25));const s = jobs.status('admin-1');assert.equal(s.pending.kind, 'debrief');- assert.deepEqual(s.pending.workout, { id: 'w1', d: '2026-07-20', name: 'Full body A', minutes: 45, vol: 600, sets: 3, prs: 0 });+ assert.deepEqual(s.pending.workout, { id: 'w1', d: D('2026-07-20'), name: 'Full body A', minutes: 45, vol: 600, sets: 3, prs: 0 });assert.equal(s.pending.score, 8);assert.ok(s.pending.summary.includes('3 sets'));assert.ok(Array.isArray(s.pending.nextTime) && s.pending.nextTime.length);