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.

13.0 KB
Raw Blame
/* What bounds the Coach's spend, pinned against the real queue and the fixture provider.
 *
 * Forget keeps today's per-profile count. The instance count is reserved at enqueue and is
 * not read off the capped job log. A scheduled review covers a batch of workouts once, on the
 * workout's own clock rather than the phone's local date, and an answer the model was paid
 * for counts as that review even when it was unusable. */
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { tempData, writeState, sampleState } from './helpers.mjs';

const DIR = tempData();
const cfg = await import('../coach/config.js');
const jobs = await import('../coach/jobs.js');
const cadence = await import('../coach/cadence.js');
const { forcePrivilegeVerdict } = await import('../coach/adapters/spawn.js');

cfg.save({ enabled: true, provider: 'fixture' });
forcePrivilegeVerdict({ ok: true, dropped: false, why: 'pinned by the test suite' });

const today = new Date().toISOString().slice(0, 10);
const userFile = uid => `${DIR}/coach/${uid}.json`;

async function settle(uid, ms = 15000) {
  const until = Date.now() + ms;
  while (Date.now() < until) {
    const s = jobs.status(uid);
    if (!s.job) return s;
    await new Promise(r => setTimeout(r, 25));
  }
  throw new Error('job never finished');
}
const lastOutcome = uid => jobs.readUser(uid).history.at(-1);

/* ---------- the per-profile cap survives forget ---------- */

test('forgetting a profile does not hand it a fresh daily cap', async () => {
  const uid = 'u-forget-cap';
  writeState(DIR, uid, sampleState());
  cfg.save({ caps: { perProfileDaily: 1, instanceDaily: 0 } });
  try {
    jobs.enqueue(uid, { kind: 'review' });
    await settle(uid);
    assert.equal(jobs.capState(uid).used, 1);

    jobs.clearUser(uid);
    const s = jobs.status(uid);
    assert.equal(s.pending, null, 'the proposal is gone');
    assert.deepEqual(jobs.readUser(uid).history, [], 'the history is gone');
    assert.equal(s.cap.used, 1, 'the day\'s count is a spending record, not the profile\'s data');
    assert.throws(() => jobs.enqueue(uid, { kind: 'review' }), e => e.code === 'cap');
  } finally {
    cfg.save({ caps: { perProfileDaily: 10, instanceDaily: 0 } });
  }
});

test('with nothing spent today, forget leaves no file at all', () => {
  const uid = 'u-forget-empty';
  jobs.setShare(uid, true);
  assert.ok(fs.existsSync(userFile(uid)));
  jobs.clearUser(uid);
  assert.equal(fs.existsSync(userFile(uid)), false);

  // Yesterday's count is already worth nothing to the cap, so it does not keep the file either.
  fs.writeFileSync(userFile(uid), JSON.stringify({ daily: { date: '2000-01-01', count: 5 }, history: [] }));
  jobs.clearUser(uid);
  assert.equal(fs.existsSync(userFile(uid)), false);
});

/* ---------- the instance cap ---------- */

test('the instance cap is reserved at enqueue, so a job still running already counts', async () => {
  cfg.save({ caps: { perProfileDaily: 10, instanceDaily: 1 }, daily: null, log: [] });
  try {
    writeState(DIR, 'u-inst-1', sampleState());
    writeState(DIR, 'u-inst-2', sampleState());
    jobs.enqueue('u-inst-1', { kind: 'review' });
    assert.ok(jobs.status('u-inst-1').job, 'the first job is on its way');
    assert.throws(() => jobs.enqueue('u-inst-2', { kind: 'review' }), e => e.code === 'cap',
      'the second profile is refused before the first job has finished');
    await settle('u-inst-1');
    assert.equal(lastOutcome('u-inst-1').outcome, 'ready');
    assert.deepEqual(cfg.load().daily, { date: today, count: 1 });
  } finally {
    cfg.save({ caps: { perProfileDaily: 10, instanceDaily: 0 } });
  }
});

test('the instance count is not bounded by the length of the job log', () => {
  const uid = 'u-inst-150';
  writeState(DIR, uid, sampleState());
  cfg.save({ caps: { perProfileDaily: 10, instanceDaily: 150 }, daily: { date: today, count: 150 }, log: [] });
  try {
    assert.throws(() => jobs.enqueue(uid, { kind: 'review' }), e => e.code === 'cap',
      'a hundred and fifty jobs today trip a cap of a hundred and fifty, empty log or not');
    // Yesterday's count is nobody's business today.
    cfg.save({ daily: { date: '2000-01-01', count: 150 } });
    assert.doesNotThrow(() => jobs.enqueue(uid, { kind: 'review' }));
    assert.deepEqual(cfg.load().daily, { date: today, count: 1 });
  } finally {
    cfg.save({ caps: { perProfileDaily: 10, instanceDaily: 0 } });
  }
  return settle(uid);
});

test('a job the daily caps refuse does not bind the instance credential', () => {
  // A personal credential belongs to the first profile that spends it. A job refused for the
  // day spends nothing, but the bind used to come before the caps, so a profile that asked on a
  // day the caps were already reached took the owner's account with it, and from then on every
  // other profile was refused as sharing it.
  const prev = cfg.load();
  cfg.save({
    provider: 'claude', authMode: 'instance', boundUid: {},
    auth: { claude: { type: 'cli-token', account: '[email protected]', data: cfg.encrypt({ token: 'tok-instance' }) } }
  });
  try {
    const own = 'u-cap-bind-own';
    writeState(DIR, own, sampleState());
    fs.mkdirSync(`${DIR}/coach`, { recursive: true });
    fs.writeFileSync(userFile(own), JSON.stringify({ daily: { date: today, count: 1 }, history: [] }));
    cfg.save({ caps: { perProfileDaily: 1, instanceDaily: 0 } });
    assert.throws(() => jobs.enqueue(own, { kind: 'review' }), e => e.code === 'cap' && e.message === 'daily limit reached');
    assert.equal(cfg.boundUidFor(), null, 'a profile over its own cap did not take the credential');

    const inst = 'u-cap-bind-inst';
    writeState(DIR, inst, sampleState());
    cfg.save({ caps: { perProfileDaily: 10, instanceDaily: 1 }, daily: { date: today, count: 1 } });
    assert.throws(() => jobs.enqueue(inst, { kind: 'review' }), e => e.code === 'cap' && /instance/.test(e.message));
    assert.equal(cfg.boundUidFor(), null, 'nor did one the instance cap refused');

    // Not vacuous: this is a credential that binds on spend.
    cfg.bindInstanceCredential(own);
    assert.equal(cfg.boundUidFor(), own);

    // The refusals ahead of the caps still come first, caps reached or not: a profile the
    // credential is bound away from hears that it is someone else's account, and a profile with
    // no account of its own hears that, rather than "try again tomorrow".
    assert.throws(() => jobs.enqueue(inst, { kind: 'review' }), e => e.code === 'shared');
    cfg.save({ authMode: 'profile' });
    assert.throws(() => jobs.enqueue(inst, { kind: 'review' }), e => e.code === 'off' && e.message === 'this profile has no provider account connected');
  } finally {
    cfg.save({ provider: prev.provider, authMode: prev.authMode, boundUid: prev.boundUid, auth: prev.auth, daily: prev.daily, caps: { perProfileDaily: 10, instanceDaily: 0 } });
  }
});

/* ---------- scheduled reviews ---------- */

/** startCadence arms a setInterval; take the callback so the tests can tick it by hand. */
function captureTick(deps) {
  const real = globalThis.setInterval;
  let fn = null;
  globalThis.setInterval = f => { fn = f; return { unref() {} }; };
  try { cadence.startCadence(deps); } finally { globalThis.setInterval = real; }
  return fn;
}
// `end` lands a second past now, so the workout is after any review that has just finished.
const newWorkout = id => ({ ...sampleState().workouts[0], id, d: today, start: Date.now() - 60000, end: Date.now() + 1000 });

test('a scheduled review reads a batch of workouts once, not once per tick', async () => {
  const uid = 'u-cadence';
  const S = sampleState({ coach: { ...sampleState().coach, cadence: { everyWorkouts: 1 } } });
  writeState(DIR, uid, S);
  const tick = captureTick({ users: () => [{ id: uid }], userNow: () => ({ date: today, hhmm: '18:00', weekday: 4 }) });
  process.env.FIXTURE_MODE = 'nochange';
  try {
    tick();
    assert.ok(jobs.status(uid).job, 'the first tick queues a review');
    tick();
    await settle(uid);
    assert.equal(lastOutcome(uid).outcome, 'nochange');
    assert.equal(lastOutcome(uid).trigger, 'scheduled');

    // The phone never opened, so lastReview in the synced state is still unset — and the same
    // workout must not be reviewed again on every tick until the daily cap is gone.
    tick(); tick(); tick();
    assert.equal(jobs.status(uid).job, null);
    assert.equal(jobs.readUser(uid).history.length, 1);
    assert.equal(jobs.capState(uid).used, 1);

    // A new workout is new news.
    S.workouts.push(newWorkout('w2'));
    writeState(DIR, uid, S);
    tick();
    assert.ok(jobs.status(uid).job, 'a fresh workout is due');
    await settle(uid);
    assert.equal(jobs.readUser(uid).history.length, 2);
  } finally {
    delete process.env.FIXTURE_MODE;
  }
});

test('a workout the phone dated tomorrow is not re-read once the review has covered it', async () => {
  const uid = 'u-cadence-tz';
  // The phone stamps `d` in its own timezone: a morning session in Tokyo already carries
  // tomorrow's date by the server's UTC clock. Its `end` is on the clock the review is timed on.
  const tomorrow = new Date(Date.now() + 86400000).toISOString().slice(0, 10);
  const S = sampleState({ coach: { ...sampleState().coach, cadence: { everyWorkouts: 1 } } });
  S.workouts.push({ ...newWorkout('w-ahead'), d: tomorrow, start: Date.now() - 45 * 60000, end: Date.now() - 60000 });
  writeState(DIR, uid, S);
  const tick = captureTick({ users: () => [{ id: uid }], userNow: () => ({ date: today, hhmm: '18:00', weekday: 4 }) });
  process.env.FIXTURE_MODE = 'nochange';
  try {
    tick();
    await settle(uid);
    assert.equal(lastOutcome(uid).outcome, 'nochange');
    tick();
    assert.equal(jobs.status(uid).job, null, 'the review already read that workout, whatever date the phone gave it');
    assert.equal(jobs.readUser(uid).history.length, 1);
  } finally {
    delete process.env.FIXTURE_MODE;
  }
});

test('an answer the model was paid for counts as the review, even when it was unusable', async () => {
  const uid = 'u-cadence-unusable';
  const S = sampleState({ coach: { ...sampleState().coach, cadence: { everyWorkouts: 1 } } });
  writeState(DIR, uid, S);
  const tick = captureTick({ users: () => [{ id: uid }], userNow: () => ({ date: today, hhmm: '18:00', weekday: 4 }) });
  process.env.FIXTURE_MODE = 'invalid';
  try {
    tick();
    await settle(uid);
    assert.equal(lastOutcome(uid).outcome, 'failed');
    assert.equal(lastOutcome(uid).errorClass, 'unusable');
    tick();
    assert.equal(jobs.status(uid).job, null, 'the model read those workouts; sending them again pays twice for the same data');
    assert.equal(jobs.capState(uid).used, 1);

    // A call that never reached the model read nothing, so the same workouts are asked about again.
    process.env.FIXTURE_MODE = 'crash';
    S.workouts.push(newWorkout('w2'));
    writeState(DIR, uid, S);
    tick();
    await settle(uid);
    assert.equal(lastOutcome(uid).errorClass, 'provider');
    tick();
    assert.ok(jobs.status(uid).job, 'a provider that fell over is retried on the next tick');
    await settle(uid);
  } finally {
    delete process.env.FIXTURE_MODE;
  }
});

test('a proposal nobody has answered is not replaced by the next scheduled review', async () => {
  const uid = 'u-cadence-pending';
  const S = sampleState({ coach: { ...sampleState().coach, cadence: { everyWorkouts: 1 } } });
  writeState(DIR, uid, S);
  const tick = captureTick({ users: () => [{ id: uid }], userNow: () => ({ date: today, hhmm: '18:00', weekday: 4 }) });

  tick();
  const first = await settle(uid);
  assert.equal(lastOutcome(uid).outcome, 'ready');
  assert.ok(first.pending, 'a proposal is waiting');
  // Nobody asked for it, so the phone app (no web push) is told by the status it reads (Discord).
  assert.equal(first.pending.trigger, 'scheduled');
  assert.equal(first.last.trigger, 'scheduled');

  S.workouts.push(newWorkout('w2'));
  writeState(DIR, uid, S);
  tick();
  assert.equal(jobs.status(uid).job, null, 'not while the proposal is unread');
  assert.equal(jobs.status(uid).pending.id, first.pending.id, 'the unread proposal is still the same one');

  // Once it is answered, the workout logged since is due.
  jobs.resolvePending(uid, { dismissed: true });
  tick();
  assert.ok(jobs.status(uid).job, 'the workout since the last review is due');
  await settle(uid);
  assert.equal(jobs.readUser(uid).history.filter(h => h.outcome === 'ready').length, 2);

  // One somebody asked for is not marked.
  jobs.resolvePending(uid, { dismissed: true });
  jobs.enqueue(uid, { kind: 'review' });
  const asked = await settle(uid);
  assert.equal(asked.pending.trigger, undefined);
  assert.equal(asked.last.trigger, undefined);
});

test('a rejected answer tells the admin which rule broke, without quoting the plan', async () => {
  const { whyUnusable } = await import('../coach/jobs.js');
  assert.equal(whyUnusable([]), null);
  const why = whyUnusable(['routines[0].ex[1].id "0009" needs equipment the user does not have', 'routines[1].name is required']);
  assert.ok(why.includes('needs equipment') && why.includes('routines[1].name is required'));
  assert.ok(!why.includes('0009'), 'quoted values are cut out');
});