Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/reference/backend-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`).

Expand Down
16 changes: 13 additions & 3 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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(`
Expand Down
86 changes: 86 additions & 0 deletions server/tests/bonusChoreSplitAssign.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '..');
Expand Down Expand Up @@ -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 });
Expand Down Expand Up @@ -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);
});