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
Follow-up to #313: a Coach change that brings in an exercise the routine already has is left out only when no other change in that routine names the exercise it swapped out or brought in; otherwise the answer goes back for repair instead of being applied with a different meaning
Duarte Santos406e07c90661
diff --git a/api/coach/core/validate.js b/api/coach/core/validate.jsindex 1886fb04c..8d43a4adb 100644--- a/api/coach/core/validate.js+++ b/api/coach/core/validate.js@@ -260,8 +260,16 @@ export function validateReview(data, plan, ctx = {}) {// depend on an exercise that never arrives — and costing a whole review over it, when the// lifter can simply not be shown it, helps nobody. When it is the only thing proposed, or// something else is also wrong, it is reported with the rest so the repair round fixes both.+ // Self-contained is checked, not assumed: another change in the same routine that names the+ // exercise it swapped out or brought in was written with it in mind — "swap 0001 for 0007"+ // next to "remove 0007" means something else once the swap is gone — so then it is fatal.const dropped = [];- const dropDuplicate = msg => { errors.push(msg); dropped.push(msg); };+ const droppedRefs = [];+ const dropDuplicate = (msg, where, c) => {+ errors.push(msg); dropped.push(msg);+ const t = c.target || {};+ droppedRefs.push({ where, rid: t.routineId, ids: [t.exId, (c.after || {}).id].filter(isStr) });+ };list.slice(0, MAX_CHANGES).forEach((c, i) => {const where = `changes[${i}]`;@@ -322,7 +330,7 @@ export function validateReview(data, plan, ctx = {}) {// duplicate makes every later reorder unsatisfiable, makes each targeted change resolve// to whichever copy comes first, and makes one "drop it" delete both.if ((routine.ex || []).some(e => e.id === (c.after || {}).id)) {- dropDuplicate(ALREADY_IN(where, c, routine)); return;+ dropDuplicate(ALREADY_IN(where, c, routine), where, c); return;}const a = c.after || {};if (!isStr(a.id) || !knownEx(a.id)) { errors.push(`${where}.after.id must be an exercise id from the library`); return; }@@ -349,7 +357,7 @@ export function validateReview(data, plan, ctx = {}) {// Same rule, minus the exercise being swapped out — replacing A with B when B is// already there is a duplicate, not a swap.if ((routine.ex || []).some(e => e.id === (c.after || {}).id && e.id !== target.exId)) {- dropDuplicate(ALREADY_IN(where, c, routine)); return;+ dropDuplicate(ALREADY_IN(where, c, routine), where, c); return;}const a = c.after || {};if (!isStr(a.id) || !knownEx(a.id)) { errors.push(`${where}.after.id must be an exercise id from the library`); return; }@@ -467,6 +475,18 @@ export function validateReview(data, plan, ctx = {}) {changes.push(out);});+ const namesOf = ch => {+ const a = ch.after && typeof ch.after === 'object' && !Array.isArray(ch.after) ? ch.after : {};+ return [ch.target.exId, a.id, a.with].filter(isStr);+ };+ droppedRefs.forEach(d => {+ const dep = changes.find(ch => ch.target.routineId === d.rid && namesOf(ch).some(id => d.ids.includes(id)));+ if (dep) {+ const id = namesOf(dep).find(x => d.ids.includes(x));+ errors.push(`${d.where} cannot just be left out: change "${dep.id}" (${dep.type}) in the same routine also names exercise "${id}", `+ + `and without ${d.where} it would do something other than what was meant — propose these changes again as one set that holds together`);+ }+ });if (errors.length > dropped.length) return fail(errors);if (dropped.length && !changes.length) return fail(errors);errors.length = 0;diff --git a/api/test/validate.test.js b/api/test/validate.test.jsindex 8ebc2bb54..38de8df9b 100644--- a/api/test/validate.test.js+++ b/api/test/validate.test.js@@ -482,7 +482,7 @@ test('#313: a swap onto an exercise already in the routine, alone, goes back for});test('#313: alongside sound changes, the duplicate is dropped and the rest reaches the screen', () => {- const r = review([change(), swapIn('0007')]);+ const r = review([change({ target: { routineId: 'r2', exId: '0009' } }), swapIn('0007')]);assert.equal(r.ok, true);assert.deepEqual(r.proposal.changes.map(c => c.id), ['c1']);// A dropped duplicate does not turn a paired reorder into a "reordered and restructured" refusal.@@ -492,6 +492,28 @@ test('#313: alongside sound changes, the duplicate is dropped and the rest reachassert.deepEqual(r2.proposal.changes.map(c => c.type), ['reorder']);});+test('#313: a duplicate is not dropped when another change in the routine names what it swapped out or brought in', () => {+ // Swap 0001 for 0007 (already there) next to "remove 0007": dropping the swap would leave only+ // the removal — the opposite of what was meant.+ const remove = change({ id: 'c2', type: 'remove-exercise', target: { routineId: 'r1', exId: '0007' }, before: null, after: null });+ const r = review([swapIn('0007'), remove]);+ assert.equal(r.ok, false);+ assert.ok(r.errors.some(e => /already in routine/.test(e)));+ assert.ok(r.errors.some(e => /cannot just be left out: change "c2" \(remove-exercise\).*"0007"/.test(e)), r.errors.join(' | '));+ // A chain: 0001 → 0007 dropped, 0007 → 0009 kept would swap out the wrong exercise.+ const chain = change({ id: 'c3', type: 'swap-exercise', target: { routineId: 'r1', exId: '0007' }, after: { id: '0009' } });+ const r2 = review([swapIn('0007'), chain]);+ assert.equal(r2.ok, false);+ assert.ok(r2.errors.some(e => /change "c3"/.test(e)));+ // A change to the exercise the swap would have replaced, or a superset with it, depends on it too.+ assert.equal(review([change(), swapIn('0007')]).ok, false);+ assert.equal(review([swapIn('0007'), change({ id: 'c4', type: 'superset', target: { routineId: 'r1', exId: '0007' }, after: { link: true, with: '0001' } })]).ok, false);+ // The same change in another routine is unrelated: the duplicate is dropped as before.+ const other = review([swapIn('0007'), change({ id: 'c5', type: 'remove-exercise', target: { routineId: 'r2', exId: '0009' }, before: null, after: null })]);+ assert.equal(other.ok, true);+ assert.deepEqual(other.proposal.changes.map(c => c.id), ['c5']);+});+test('#313: a duplicate next to a change that is wrong for another reason is reported with it, not dropped', () => {const r = review([change({ type: 'sets', after: 99 }), swapIn('0007')]);assert.equal(r.ok, false);