From 1702f9b506961cc61377bc2a158acbe3ac04bbf2 Mon Sep 17 00:00:00 2001 From: adamecker Date: Tue, 1 Dec 2026 21:03:25 +0000 Subject: [PATCH 1/2] fix(chores): prune a half-done split once its any-day period ends MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A split on an any-day card never expired: the nightly prune only took a split once every share was ticked, and the split return kept a one-time split with a ticked share, so an expired any-day card stayed visible for weeks — its open share could still be ticked and paid, and it blocked its holder's bonus claims. The period ending now prunes the card regardless of its ticks: a ticked share stays earned in history, and the next period rolls a fresh card like an unsplit any-day chore. Regression test: a half-done split on an any-day chore expires with its period (server/tests/bonusChoreSplitAssign.test.js). --- server/index.js | 16 +++- server/tests/bonusChoreSplitAssign.test.js | 86 ++++++++++++++++++++++ 2 files changed, 99 insertions(+), 3 deletions(-) diff --git a/server/index.js b/server/index.js index d5e9f38c..25318f43 100644 --- a/server/index.js +++ b/server/index.js @@ -2638,7 +2638,8 @@ async function dailyBackgroundProcessing() { // next period starts fresh, and an undone one was logged as one miss on // its last day, above (services/anyDayChores.js). const schedulesToPrune = db.prepare(` - SELECT cs.id, cs.chore_id, cs.user_id, cs.parent_schedule_id, cs.split_user_ids, cs.one_time_after_history_id, c.title + SELECT cs.id, cs.chore_id, cs.user_id, cs.parent_schedule_id, cs.split_user_ids, cs.one_time_after_history_id, c.title, + (ps.duration = 'any-day' AND cs.due_date IS NOT NULL AND cs.due_date < ?) AS any_day_expired FROM chore_schedules cs JOIN chores c ON cs.chore_id = c.id LEFT JOIN chore_schedules ps ON ps.id = cs.parent_schedule_id @@ -2653,7 +2654,14 @@ async function dailyBackgroundProcessing() { ) OR (ps.duration = 'any-day' AND cs.due_date IS NOT NULL AND cs.due_date < ?) ) - `).all(today).filter((schedule) => { + `).all(today, today).filter((schedule) => { + // An expired any-day card goes at the end of its period whether or + // not its split is finished (issue #407 follow-up): the period is + // over, so the open shares end with it — a ticked share stays + // earned in history, and the next period starts fresh. A split + // that is only done, not expired, still waits for every share, as + // below. + if (schedule.any_day_expired) return true; // A split schedule is done only when every person in the split has // ticked their share: one person's completion must not prune the // schedule out from under the others (issue #407). A partially done @@ -2797,7 +2805,9 @@ async function dailyBackgroundProcessing() { // would put the whole chore back in the pool, and the next night the // prune would take it as "done" on the one ticked share. It stays // split until every share is ticked, when the prune takes it as it - // already does; untouched, it returns like any other split. A + // already does; untouched, it returns like any other split. An + // expired any-day card never reaches here: the prune above takes it + // first, split or not, so its period can end. A // recurring split still returns every night — tomorrow is a new day // of the chore, worth its full Stars again. const splitBonusSchedules = db.prepare(` diff --git a/server/tests/bonusChoreSplitAssign.test.js b/server/tests/bonusChoreSplitAssign.test.js index e319fb2d..77d2ed84 100644 --- a/server/tests/bonusChoreSplitAssign.test.js +++ b/server/tests/bonusChoreSplitAssign.test.js @@ -17,6 +17,7 @@ const assert = require('node:assert/strict'); const { spawn } = require('node:child_process'); const fs = require('node:fs'); const path = require('node:path'); +const Database = require('better-sqlite3'); const { freePort } = require('./freePort'); const serverDir = path.resolve(__dirname, '..'); @@ -44,6 +45,16 @@ const nightly = () => api('/api/system/backgroundTasks'); // The server runs in UTC here, so "today" is the UTC date. const shift = (days) => new Date(Date.now() + days * 86400000).toISOString().slice(0, 10); const today = shift(0); +// This week's Saturday, and last week's: the period that just ended. +const dayOfWeek = new Date().getUTCDay(); +const thisSaturday = shift(6 - dayOfWeek); +const daysSinceLastSaturday = ((dayOfWeek + 1) % 7) || 7; +const lastSaturday = shift(-daysSinceLastSaturday); +const lastFriday = shift(-daysSinceLastSaturday - 1); +const withDb = (work) => { + const db = new Database(testDbPath); + try { return work(db); } finally { db.close(); } +}; test.before(async () => { fs.mkdirSync(tmpDir, { recursive: true }); @@ -693,3 +704,78 @@ test('splitting an already-split chore is refused', async () => { assert.deepEqual(after.split_shares, [5, 5]); assert.equal(after.pool_claimed_on, today); }); + +// A half-done split on an any-day chore expires with its period (mrramam, +// Dec 1, on #407): a split Ava + Ben on a weekly any-day card, Ava ticked, +// Saturday passed — the card stayed visible for weeks, Ben could still tick +// it and be paid, and his open share blocked his bonus claims. The period +// ending prunes the card regardless of its ticks: Ava's Stars stay earned, +// Ben cannot tick the stale card, the next week's card rolls, and Ben is +// free to claim again. +test('a half-done split on an any-day chore expires with its period', async () => { + const ava = await makeKid('Ava'); + const ben = await makeKid('Ben'); + + // Weekly any-day 5-Star bonus chore in the pool: one card, due this + // week's Saturday. + const chore = (await post('/api/chores', { title: 'Practice piano', clam_value: 5 })).body.id; + const parent = await post('/api/chore-schedules', { chore_id: chore, user_id: null, crontab: '0 0 * * *', duration: 'any-day', interval: '1w' }); + assert.equal(parent.status, 200, JSON.stringify(parent.body)); + const parentId = parent.body.id; + const cards = (await api('/api/chore-schedules')).body.filter((s) => s.parent_schedule_id === parentId); + assert.equal(cards.length, 1); + const card = cards[0]; + assert.equal(card.due_date, thisSaturday); + + // Split it Ava + Ben: 5 Stars is 3 and 2. + const split = await splitSchedule(card.id, [ava, ben]); + assert.equal(split.status, 200, JSON.stringify(split.body)); + assert.deepEqual(split.body.schedule.split_shares, [3, 2]); + + // Last week, as it happened: the split was made on Friday, Ava ticked + // her share on Saturday, the card's last day. + withDb((db) => db.prepare('UPDATE chore_schedules SET pool_claimed_on = ?, due_date = ? WHERE id = ?') + .run(lastFriday, lastSaturday, card.id)); + await completeSchedule(card.id, ava, lastSaturday); + const avaTick = (await historyOf(ava, lastSaturday)).filter((h) => h.chore_schedule_id === card.id); + assert.equal(avaTick.length, 1); + assert.equal(avaTick[0].clam_value, 3); + + // Ben's open share blocks his bonus claims while the card is stale. + const other = await makePoolChore({ title: 'Wash the windows', clamValue: 4 }); + const blocked = await post(`/api/chore-schedules/${other.scheduleId}/claim`, { user_id: ben }); + assert.equal(blocked.status, 409, JSON.stringify(blocked.body)); + + // The period is over: the nightly job runs in the new week. + await nightly(); + + // (1) The stale card is gone, split and all. + assert.equal(await scheduleById(card.id), undefined); + // (2) Ava's Stars stay earned — the history row survives, moved to the + // parent schedule. + const avaHistory = (await historyOf(ava, lastSaturday)).filter((h) => h.kind === 'completion'); + assert.equal(avaHistory.length, 1); + assert.equal(avaHistory[0].clam_value, 3); + assert.equal(avaHistory[0].chore_schedule_id, parentId); + // (3) Ben cannot tick the stale card after its due date. + const late = await post('/api/chores/complete', { chore_schedule_id: card.id, user_id: ben, date: today }); + assert.equal(late.status, 404, JSON.stringify(late.body)); + // (4) The chore rolls to a fresh card for the new week, one unsplit + // chore in the pool again. + const fresh = (await api('/api/chore-schedules')).body.filter((s) => s.parent_schedule_id === parentId); + assert.equal(fresh.length, 1); + assert.equal(fresh[0].due_date, thisSaturday); + assert.equal(fresh[0].user_id, null); + assert.equal(fresh[0].split_user_ids, null); + assert.equal(fresh[0].visible, 1); + // (5) Ben's outstanding-bonus block is gone: he can claim again. + const claim = await post(`/api/chore-schedules/${other.scheduleId}/claim`, { user_id: ben }); + assert.equal(claim.status, 200, JSON.stringify(claim.body)); + + // A second night changes nothing: the fresh card is this week's, and + // no second card rolls while it is open. + await nightly(); + const stillFresh = (await api('/api/chore-schedules')).body.filter((s) => s.parent_schedule_id === parentId); + assert.equal(stillFresh.length, 1); + assert.equal(stillFresh[0].id, fresh[0].id); +}); From 3876101e84aa59b9385ffedcfc3f1d505d6587be Mon Sep 17 00:00:00 2001 From: adamecker Date: Tue, 1 Dec 2026 23:04:24 +0000 Subject: [PATCH 2/2] docs(chores): an any-day split card goes when its period ends The nightly-job description still said a half-done one-time split stays split until every share is ticked (issue #407), with no exception for any-day cards. PR #410 prunes those when their period ends, split or not: a ticked share stays earned, and the next period starts with a fresh card. --- docs/reference/backend-api.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/reference/backend-api.md b/docs/reference/backend-api.md index 9c5a2145..81f5ab74 100644 --- a/docs/reference/backend-api.md +++ b/docs/reference/backend-api.md @@ -65,7 +65,9 @@ Runs at local midnight and also exposed manually via `kind='completion'` row; a missed row never counts) and orphaned chores. - Resets day-to-day **bonus** chores back to unassigned. A split bonus chore returns to one chore in the pool — except a half-done one-time - split, which stays split until every share is ticked (issue #407). + split, which stays split until every share is ticked (issue #407). An + any-day card goes when its period ends, split or not: a ticked share + stays earned, and the next period starts with a fresh card. - Generates one-time child instances for `until-completed` / `once-completed` "sticky" schedules whose cron fires that day (using `cron-parser`).