Files
hope-food-pantry-app/tests/settings.test.ts
blake 17ca2f9dfb feat: enhance reminder functionality with version tracking for shifts
Introduced a new column `version_at` in the shifts table to track the last change time of a shift's version. Updated reminder logic to skip reminders that are due before the shift's version change or the signup time. Modified related functions and tests to ensure correct behavior when managing reminders for shifts and signups, including scenarios for late signups and shift changes.
2026-11-02 12:00:42 -06:00

245 lines
10 KiB
TypeScript

import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { afterEach, describe, expect, it } from 'vitest';
import { createApp } from '../src/app.js';
import { bootstrapAdmin } from '../src/auth/bootstrap.js';
import { loadConfig } from '../src/config.js';
import { openAppDatabase } from '../src/db.js';
import { queueDueReminders } from '../src/reminders.js';
import { devEnv } from './env.js';
const NOW = '2026-10-14T18:00:00.000Z';
const ORIGIN = 'http://127.0.0.1:3000';
const MAIL = {
baseUrl: ORIGIN,
basePath: '',
from: 'Hope Food Pantry <volunteer@example.invalid>',
replyTo: 'volunteer@example.invalid',
};
const dirs: string[] = [];
const databases: { close: () => void }[] = [];
afterEach(() => {
for (const database of databases.splice(0)) database.close();
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
});
describe('coordinator settings', () => {
it('asks a signed-out visitor to sign in', async () => {
const { app } = await buildApp();
const response = await app.request('/admin/settings');
expect(response.status).toBe(303);
expect(response.headers.get('location')).toBe('/login');
});
it('shows the defaults, then saves reminder times, reply-to, and retention', async () => {
const { app, db } = await buildApp();
const cookie = await signIn(app, db);
const first = await app.request('/admin/settings', { headers: { cookie } });
const firstHtml = await first.text();
expect(first.status).toBe(200);
expect(firstHtml).toContain('value="48"');
expect(firstHtml).toContain('2 days before (usual)');
expect(firstHtml).toContain('value="2"');
expect(firstHtml).toContain('2 hours before (usual)');
expect(firstHtml).toContain('value="volunteer@example.invalid"');
expect(firstHtml).toContain('value="24"');
expect(firstHtml).toContain('2 years (usual)');
expect(firstHtml).toContain('Volunteers get an email 2 days before the shift, and another 2 hours before the shift.');
expect(firstHtml).toContain('A spreadsheet already downloaded is left as it is.');
expect(firstHtml).not.toContain('name="privacy_text"');
expect(firstHtml).not.toContain('Privacy');
const rejected = await postSettings(app, cookie, validFields({ reminder_far: '12', reminder_near: '12' }));
expect(rejected.status).toBe(400);
expect(await rejected.text()).toContain('Choose two different times.');
expect(count(db, "SELECT COUNT(*) AS n FROM settings")).toBe(0);
const named = await postSettings(app, cookie, validFields({ reply_to: 'Hope <pantry@example.invalid>' }));
expect(named.status).toBe(400);
expect(await named.text()).toContain('Enter just the email address, without a name in front of it.');
const stale = await app.request('/admin/settings', {
method: 'POST',
headers: { origin: ORIGIN, 'content-type': 'application/x-www-form-urlencoded', cookie },
body: new URLSearchParams({ csrf: 'wrong', ...validFields() }),
});
expect(stale.status).toBe(403);
db.prepare(`INSERT INTO settings (key, value_json, updated_at) VALUES ('privacy_text', '"We keep names."', ?)`).run(NOW);
const saved = await postSettings(
app,
cookie,
validFields({
reminder_far: '3',
reminder_near: '24',
reply_to: 'Pantry@Example.invalid',
retention_months: '12',
}),
);
expect(saved.status).toBe(303);
expect(saved.headers.get('location')).toBe('/admin/settings?notice=settings-saved');
const page = await app.request('/admin/settings?notice=settings-saved', { headers: { cookie } });
const html = await page.text();
expect(html).toContain('Settings saved.');
expect(html).toContain('value="24"');
expect(html).toContain('1 day before');
expect(html).toContain('value="3"');
expect(html).toContain('3 hours before');
expect(html).toContain('value="pantry@example.invalid"');
expect(html).toContain('value="12"');
expect(html).toContain('1 year');
expect(html).not.toContain('We keep names.');
expect(html).not.toContain('name="privacy_text"');
expect(count(db, "SELECT COUNT(*) AS n FROM settings WHERE key = 'privacy_text'")).toBe(0);
const missing = await app.request('/privacy');
expect(missing.status).toBe(404);
expect(await missing.text()).toContain('That page is not available');
const audit = db.prepare("SELECT detail_json AS detail_json FROM audit WHERE action = 'settings.update'").all() as { detail_json: string }[];
expect(audit).toHaveLength(1);
expect(audit[0]?.detail_json).toContain('"reminderEarlier":24');
expect(audit[0]?.detail_json).toContain('"reminderCloser":3');
expect(audit[0]?.detail_json).toContain('"retentionMonths":12');
expect(audit[0]?.detail_json).toContain('"replyChanged":1');
expect(audit[0]?.detail_json).not.toContain('privacyChanged');
expect(audit[0]?.detail_json).not.toContain('@');
const again = await postSettings(
app,
cookie,
validFields({
reminder_far: '24',
reminder_near: '3',
reply_to: 'pantry@example.invalid',
retention_months: '12',
}),
);
expect(again.status).toBe(303);
expect(count(db, "SELECT COUNT(*) AS n FROM audit WHERE action = 'settings.update'")).toBe(1);
await postLogin(app, 'coordinator@example.invalid');
const letter = db
.prepare("SELECT headers_json AS headers_json FROM outbox WHERE kind = 'magic_link' AND status = 'pending' ORDER BY id DESC LIMIT 1")
.get() as { headers_json: string };
expect(JSON.parse(letter.headers_json)).toMatchObject({ 'Reply-To': 'pantry@example.invalid' });
const shiftId = insertShift(db);
insertSignup(db, shiftId);
expect(count(db, 'SELECT COUNT(*) AS n FROM signups')).toBe(1);
expect(queueDueReminders(db, MAIL, 'dev-only-change-me-not-for-production-use', NOW)).toEqual({ queued: 1, suppressed: 0 });
expect(db.prepare('SELECT kind FROM reminders_sent').get()).toEqual({ kind: '24h' });
const reminder = db.prepare("SELECT headers_json AS headers_json FROM outbox WHERE kind = 'reminder'").get() as { headers_json: string };
expect(JSON.parse(reminder.headers_json)['Reply-To']).toBe('pantry@example.invalid');
expect(count(db, 'SELECT COUNT(*) AS n FROM signups')).toBe(1);
});
});
async function buildApp() {
const root = mkdtempSync(path.join(tmpdir(), 'hope-settings-'));
dirs.push(root);
const config = loadConfig(devEnv({ DATA_DIR: path.join(root, 'data') }));
const db = await openAppDatabase(config);
databases.push(db);
bootstrapAdmin(db, config.bootstrapAdminEmail, NOW);
const app = createApp(config, db, {
now: NOW,
clientIp: () => '203.0.113.10',
verifyTurnstile: async () => ({ ok: true }),
});
return { app, db };
}
async function signIn(app: Awaited<ReturnType<typeof buildApp>>['app'], db: Awaited<ReturnType<typeof buildApp>>['db']) {
await postLogin(app, 'coordinator@example.invalid');
const token = magicToken(db);
const page = await app.request(`/login/link/${token}`);
const signedIn = await postLink(app, token, field(await page.text(), 'proof'));
return cookieHeader(signedIn);
}
function postLogin(app: Awaited<ReturnType<typeof buildApp>>['app'], email: string) {
return app.request('/login', {
method: 'POST',
headers: { origin: ORIGIN, 'content-type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({ email, company_website: '', 'cf-turnstile-response': 'token' }),
});
}
function postLink(app: Awaited<ReturnType<typeof buildApp>>['app'], token: string, proof: string) {
return app.request(`/login/link/${token}`, {
method: 'POST',
headers: { origin: ORIGIN, 'content-type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({ proof }),
});
}
async function postSettings(app: Awaited<ReturnType<typeof buildApp>>['app'], cookie: string, fields: Record<string, string>) {
const current = await app.request('/admin/settings', { headers: { cookie } });
const csrf = field(await current.text(), 'csrf');
return app.request('/admin/settings', {
method: 'POST',
headers: { origin: ORIGIN, 'content-type': 'application/x-www-form-urlencoded', cookie },
body: new URLSearchParams({ csrf, ...fields }),
});
}
function validFields(overrides: Record<string, string> = {}): Record<string, string> {
return {
reminder_far: '48',
reminder_near: '2',
reply_to: 'volunteer@example.invalid',
retention_months: '24',
...overrides,
};
}
function insertShift(db: Awaited<ReturnType<typeof buildApp>>['db']): number {
const eventId = Number(
db.prepare(
`INSERT INTO events (slug, title, description, location, tz, published, created_at)
VALUES ('weekly', 'Weekly pantry', '', 'Sioux Center, Iowa', 'America/Chicago', 1, ?)`,
).run(NOW).lastInsertRowid,
);
return Number(
db.prepare(
`INSERT INTO shifts (event_id, role, starts_at_utc, ends_at_utc, capacity, notes, cancelled)
VALUES (?, 'Sorting', '2026-10-15T14:00:00.000Z', '2026-10-15T17:00:00.000Z', 4, '', 0)`,
).run(eventId).lastInsertRowid,
);
}
function insertSignup(db: Awaited<ReturnType<typeof buildApp>>['db'], shiftId: number): void {
db.prepare(
`INSERT INTO signups (
shift_id, name, email, email_norm, is_minor, group_size, status, reminders_off, created_at, updated_at
) VALUES (?, 'Ada', 'ada@example.invalid', 'ada@example.invalid', 0, 1, 'confirmed', 0, ?, ?)`,
).run(shiftId, '2026-10-13T18:00:00.000Z', '2026-10-13T18:00:00.000Z');
}
function magicToken(db: Awaited<ReturnType<typeof buildApp>>['db']): string {
const row = db
.prepare("SELECT body_text AS body_text FROM outbox WHERE kind = 'magic_link' AND status = 'pending' ORDER BY id DESC LIMIT 1")
.get() as { body_text: string } | undefined;
const token = /\/login\/link\/([A-Za-z0-9_-]+)/.exec(row?.body_text ?? '')?.[1];
if (!token) throw new Error('The sign-in link was not queued.');
return token;
}
function cookieHeader(response: Response): string {
const match = /hope_session=([^;]+)/.exec(response.headers.get('set-cookie') ?? '');
if (!match?.[1]) throw new Error('The session cookie was not set.');
return `hope_session=${match[1]}`;
}
function field(html: string, name: string): string {
const tag = new RegExp(`<input\\b[^>]*\\bname="${name}"[^>]*>`).exec(html)?.[0] ?? '';
return /value="([^"]*)"/.exec(tag)?.[1] ?? '';
}
function count(db: Awaited<ReturnType<typeof buildApp>>['db'], sql: string): number {
return Number((db.prepare(sql).get() as { n: number }).n);
}