Files
crosscode-platform/apps/app/backend/tests/prayerRules.test.ts
T
joshuaandClaude Opus 5.5 5213bb36c0 app: the House of Prayer's backend (APP-1.0-PLAN §3)
Commitments stored as rules (start, length, once/daily/weekly, end, skips) in the
church's timezone, daylight saving included; the day's coverage as counts only,
cached until a booking changes, live over server-sent events (X-Accel-Buffering
off, a comment every 25 s). Booking, skipping, changing, cancelling within the
8-hour rule, renewing (a signed single-use link), "I prayed" (self-reported).
Campaigns and moderated testimonies; the admin's bookings with names, always
access-logged; booking on a member's behalf. Reminders (every minute) and
renewal notices (14 and 3 days before) queued in an outbox for the messaging
slice. The heartbeat's prayer counts; export and delete for a member's prayer.
Migration 20261202000000_house_of_prayer (checked on PostgreSQL 18: applies, no
diff). 66 backend tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 10:29:39 +02:00

187 lines
10 KiB
TypeScript

import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { addMonths, localParts, zonedTime } from '../src/lib/zoned.js';
import {
cancelledEndDate, checkBooking, coveragePercent, dayCoverage, daySlots, DEFAULT_WALL, isLocked, nextOccurrence,
occurrencesBetween, reminderAt, renewedEndDate, type Rule,
} from '../src/prayer/rules.js';
const SA = 'Africa/Johannesburg';
const LONDON = 'Europe/London';
const rule = (over: Partial<Rule>): Rule => ({
id: 'r1', memberId: 'm1', startDate: '2026-12-01', startMinute: 120, durationMinutes: 60, recurrence: 'once', endDate: '2026-12-01', skips: [], ...over,
});
describe('local times in the church\'s timezone', () => {
it('turns a local date and minute into an instant and back', () => {
const t = zonedTime('2026-12-01', 120, SA)!;
assert.equal(t.toISOString(), '2026-12-01T00:00:00.000Z');
assert.deepEqual(localParts(t, SA), { date: '2026-12-01', minute: 120, weekday: 2 });
assert.equal(zonedTime('2026-12-01', 1440 + 60, SA)!.toISOString(), '2026-12-01T23:00:00.000Z');
});
it('has no instant for a time the clocks jump over, and the first of a time that happens twice', () => {
assert.equal(zonedTime('2027-03-28', 60, LONDON), null);
assert.equal(zonedTime('2027-03-28', 120, LONDON)!.toISOString(), '2027-03-28T01:00:00.000Z');
assert.equal(zonedTime('2026-10-25', 60, LONDON)!.toISOString(), '2026-10-25T00:00:00.000Z');
});
it('keeps a month\'s end inside the month', () => {
assert.equal(addMonths('2027-01-31', 1), '2027-02-28');
assert.equal(addMonths('2026-12-01', 12), '2027-12-01');
});
});
describe('slots', () => {
it('cuts the day into 24, 48 or 96 slots', () => {
assert.equal(daySlots('2026-12-01', 60, SA).length, 24);
assert.equal(daySlots('2026-12-01', 30, SA).length, 48);
assert.equal(daySlots('2026-12-01', 15, SA).length, 96);
assert.equal(daySlots('2026-12-01', 60, SA)[0]!.start.toISOString(), '2026-11-30T22:00:00.000Z');
});
it('gives a day with daylight saving whole hours: 23 when the clocks jump forward, 24 when they go back', () => {
const spring = daySlots('2027-03-28', 60, LONDON);
assert.equal(spring.length, 23);
assert.ok(!spring.some((s) => s.minute === 60));
for (const s of spring) assert.equal(s.end.getTime() - s.start.getTime(), 3600e3);
const autumn = daySlots('2026-10-25', 60, LONDON);
assert.equal(autumn.length, 24);
// 01:00 happens twice: its slot runs to the (first) 02:00, two hours later.
assert.equal(autumn.find((s) => s.minute === 60)!.end.getTime() - autumn.find((s) => s.minute === 60)!.start.getTime(), 2 * 3600e3);
});
});
describe('commitments as rules', () => {
it('works out daily, weekly and once occurrences, without the skipped dates', () => {
const from = zonedTime('2026-12-01', 0, SA)!;
const to = zonedTime('2026-12-15', 0, SA)!;
const daily = rule({ recurrence: 'daily', endDate: '2027-11-30', skips: ['2026-12-03'] });
assert.equal(occurrencesBetween(daily, from, to, SA).length, 13);
const weekly = rule({ recurrence: 'weekly', endDate: '2027-11-30' });
assert.deepEqual(occurrencesBetween(weekly, from, to, SA).map((o) => o.date), ['2026-12-01', '2026-12-08']);
assert.equal(occurrencesBetween(rule({}), from, to, SA).length, 1);
});
it('keeps a weekly rule on its weekday and stops at its end date', () => {
const r = rule({ recurrence: 'weekly', startDate: '2026-12-03', endDate: '2026-12-24' });
const all = occurrencesBetween(r, zonedTime('2026-11-01', 0, SA)!, zonedTime('2027-02-01', 0, SA)!, SA);
assert.deepEqual(all.map((o) => o.date), ['2026-12-03', '2026-12-10', '2026-12-17', '2026-12-24']);
});
it('a year of daily slots is one rule', () => {
const r = rule({ recurrence: 'daily', endDate: '2027-11-30' });
assert.equal(occurrencesBetween(r, zonedTime('2026-12-01', 0, SA)!, zonedTime('2027-12-01', 0, SA)!, SA).length, 365);
});
it('finds the next occurrence', () => {
const r = rule({ recurrence: 'daily', endDate: '2026-12-10' });
assert.equal(nextOccurrence(r, new Date('2026-12-01T06:00:00Z'), SA)!.date, '2026-12-02');
assert.equal(nextOccurrence(r, new Date('2026-12-11T06:00:00Z'), SA), null);
});
it('moves a start the clocks jump over to just after the jump', () => {
const r = rule({ startDate: '2027-03-28', endDate: '2027-03-28', startMinute: 60 });
const [o] = occurrencesBetween(r, zonedTime('2027-03-27', 0, LONDON)!, zonedTime('2027-03-29', 0, LONDON)!, LONDON);
assert.equal(o!.start.toISOString(), '2027-03-28T01:00:00.000Z');
});
});
describe('coverage', () => {
it('counts people per slot, once each, and marks the asker\'s own; no names', () => {
const rules = [
rule({ id: 'a', memberId: 'm1', startMinute: 120, durationMinutes: 120 }),
rule({ id: 'b', memberId: 'm1', startMinute: 120, durationMinutes: 60 }),
rule({ id: 'c', memberId: 'm2', startMinute: 180, durationMinutes: 60, recurrence: 'daily', endDate: '2027-01-01' }),
rule({ id: 'd', memberId: null, startMinute: 600, durationMinutes: 60 }),
];
const day = dayCoverage('2026-12-01', rules, { slotMinutes: 60 }, SA, 'm2');
const at = (h: number) => day.slots.find((s) => s.minute === h * 60)!;
assert.equal(at(2).count, 1);
assert.equal(at(2).mine, false);
assert.equal(at(3).count, 2);
assert.equal(at(3).mine, true);
assert.equal(at(10).count, 1);
assert.equal(day.covered, 3);
assert.equal(day.total, 24);
assert.deepEqual(Object.keys(at(3)).sort(), ['count', 'covered', 'end', 'mine', 'minute', 'start']);
assert.ok(!JSON.stringify(day).includes('m1'));
});
it('counts a slot that runs past midnight on the next day too', () => {
const r = rule({ startMinute: 23 * 60, durationMinutes: 120 });
const next = dayCoverage('2026-12-02', [r], { slotMinutes: 60 }, SA);
assert.equal(next.slots[0]!.count, 1);
assert.equal(next.covered, 1);
});
it('counts a 1-hour commitment in both 30-minute slots it covers', () => {
const day = dayCoverage('2026-12-01', [rule({ startMinute: 120 })], { slotMinutes: 30 }, SA);
assert.equal(day.covered, 2);
assert.equal(day.total, 48);
});
it('gives the share of slots covered', () => {
const one = dayCoverage('2026-12-01', [rule({ durationMinutes: 12 * 60, startMinute: 0 })], { slotMinutes: 60 }, SA);
assert.equal(coveragePercent([one]), 50);
assert.equal(coveragePercent([]), 0);
});
});
describe('booking rules', () => {
const now = new Date('2026-12-01T06:00:00Z'); // 08:00 SAST
it('books once, every day or every week, up to 12 months', () => {
assert.deepEqual(checkBooking({ startDate: '2026-12-02', startMinute: 120, durationMinutes: 60, recurrence: 'once' }, now, DEFAULT_WALL, SA), { endDate: '2026-12-02' });
assert.deepEqual(checkBooking({ startDate: '2026-12-02', startMinute: 120, durationMinutes: 120, recurrence: 'daily' }, now, DEFAULT_WALL, SA), { endDate: '2027-12-01' });
assert.deepEqual(checkBooking({ startDate: '2026-12-02', startMinute: 120, durationMinutes: 60, recurrence: 'weekly', endDate: '2027-03-01' }, now, DEFAULT_WALL, SA), { endDate: '2027-03-01' });
});
it('refuses what the rules don\'t allow', () => {
const code = (input: Parameters<typeof checkBooking>[0], wall = DEFAULT_WALL) => {
const r = checkBooking(input, now, wall, SA);
return 'error' in r ? r.error.code : 'ok';
};
assert.equal(code({ startDate: '2026-12-02', startMinute: 90, durationMinutes: 60, recurrence: 'once' }), 'NOT_A_SLOT');
assert.equal(code({ startDate: '2026-12-02', startMinute: 120, durationMinutes: 45, recurrence: 'once' }), 'BAD_LENGTH');
assert.equal(code({ startDate: '2026-12-02', startMinute: 120, durationMinutes: 13 * 60, recurrence: 'once' }), 'BAD_LENGTH');
assert.equal(code({ startDate: '2027-12-02', startMinute: 120, durationMinutes: 60, recurrence: 'once' }), 'TOO_FAR_AHEAD');
assert.equal(code({ startDate: '2026-12-02', startMinute: 120, durationMinutes: 60, recurrence: 'daily', endDate: '2027-12-02' }), 'TOO_LONG');
assert.equal(code({ startDate: '2026-12-02', startMinute: 120, durationMinutes: 60, recurrence: 'daily', endDate: '2026-12-01' }), 'END_BEFORE_START');
assert.equal(code({ startDate: '2026-12-01', startMinute: 7 * 60, durationMinutes: 60, recurrence: 'once' }), 'IN_THE_PAST');
assert.equal(code({ startDate: '2026-12-01', startMinute: 30, durationMinutes: 30, recurrence: 'once' }, { ...DEFAULT_WALL, slotMinutes: 30 }), 'IN_THE_PAST');
assert.equal(code({ startDate: '2026-12-02', startMinute: 30, durationMinutes: 30, recurrence: 'once' }, { ...DEFAULT_WALL, slotMinutes: 30 }), 'ok');
assert.equal(code({ startDate: '2027-03-01', startMinute: 0, durationMinutes: 60, recurrence: 'once' }, { ...DEFAULT_WALL, bookAheadMonths: 2 }), 'TOO_FAR_AHEAD');
});
it('locks an occurrence within the cancel window', () => {
assert.equal(isLocked({ start: new Date('2026-12-01T13:00:00Z') }, now, 8), true);
assert.equal(isLocked({ start: new Date('2026-12-01T14:00:00Z') }, now, 8), false);
assert.equal(isLocked({ start: new Date('2026-12-01T07:00:00Z') }, now, 0), false);
});
it('ends a cancelled rule after its last locked occurrence, or removes it when nothing happened yet', () => {
// Daily at 02:00 from 25 November: 1 December's has happened, 2 December's is 18 hours away.
const daily = rule({ startDate: '2026-11-25', recurrence: 'daily', endDate: '2027-11-24' });
assert.equal(cancelledEndDate(daily, now, DEFAULT_WALL, SA), '2026-12-01');
// With a 24-hour window, 2 December's 02:00 is locked too.
assert.equal(cancelledEndDate(daily, now, { cancelHours: 24 }, SA), '2026-12-02');
assert.equal(cancelledEndDate(rule({ startDate: '2026-12-05', endDate: '2026-12-05' }), now, DEFAULT_WALL, SA), null);
});
});
describe('reminders and renewals', () => {
it('reminds 15 minutes or an hour before, or at 20:00 the evening before', () => {
const o = { date: '2026-12-02', start: zonedTime('2026-12-02', 120, SA)! };
assert.equal(reminderAt('none', o, SA), null);
assert.equal(reminderAt('min15', o, SA)!.toISOString(), '2026-12-01T23:45:00.000Z');
assert.equal(reminderAt('hour1', o, SA)!.toISOString(), '2026-12-01T23:00:00.000Z');
assert.equal(reminderAt('evening', o, SA)!.toISOString(), '2026-12-01T18:00:00.000Z');
});
it('renews for another year from the current end', () => {
assert.equal(renewedEndDate({ endDate: '2027-11-30' }, DEFAULT_WALL), '2028-11-30');
});
});