import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; import request from 'supertest'; import { tidyRequests } from '../src/requests/service.js'; import { cookieHeader, harness, MEMBER, signInAs, type Harness } from './support/harness.js'; const OTHER = '44444444-4444-4444-8444-444444444444'; const COORD = '55555555-5555-4555-8555-555555555555'; const CARE = '66666666-6666-4666-8666-666666666666'; const DAY = 24 * 3600e3; const SECRET = 'My brother Themba is in hospital after the accident'; async function setup() { const h = await harness() as Harness; const as = (cookies: Record) => ({ get: (path: string) => request(h.app).get(path).set('Cookie', cookieHeader(cookies)), post: (path: string, body: object = {}) => request(h.app).post(path).set('Cookie', cookieHeader(cookies)).set('Origin', 'https://app.hope.test').send(body), put: (path: string, body: object) => request(h.app).put(path).set('Cookie', cookieHeader(cookies)).set('Origin', 'https://app.hope.test').send(body), del: (path: string) => request(h.app).delete(path).set('Cookie', cookieHeader(cookies)).set('Origin', 'https://app.hope.test'), }); const thandi = as(await signInAs(h, { sub: MEMBER, name: 'Thandi Mokoena', email: 'thandi@hope.test' })); const pieter = as(await signInAs(h, { sub: OTHER, name: 'Pieter Botha', email: 'pieter@hope.test' })); const coord = as(await signInAs(h, { sub: COORD, name: 'Lerato Dlamini', roles: ['prayer_coordinator'] }, true)); const care = as(await signInAs(h, { sub: CARE, name: 'Pastor Sam', roles: ['pastoral_care'] }, true)); return { h, thandi, pieter, coord, care, visitor: as({}) }; } const outbox = (h: Harness) => [...(h.db.tables.get('outboxMessage')?.values() ?? [])] as { memberId: string; kind: string; channel: string; payload: Record }[]; describe('asking for prayer', () => { it('is private by default: the prayer team sees it with the sender, members and visitors never do', async () => { const { h, thandi, pieter, coord, visitor } = await setup(); const res = await thandi.post('/api/v1/me/requests', { text: SECRET }); assert.equal(res.status, 201); assert.equal(res.body.status, 'with_team'); assert.equal((await visitor.get('/api/v1/requests')).status, 401); assert.deepEqual((await pieter.get('/api/v1/requests')).body.requests, []); const team = await coord.get('/api/v1/admin/requests?view=open'); assert.equal(team.body.requests[0].text, SECRET); assert.equal(team.body.requests[0].submitter.name, 'Thandi Mokoena'); const log = [...h.db.tables.get('accessLog')!.values()] as { actorId: string; action: string }[]; assert.deepEqual(log.map((l) => [l.actorId, l.action]), [[COORD, 'prayer_requests_viewed']]); // The coordinator is told there's a request, never what it says. const [m] = outbox(h); assert.equal(m!.memberId, COORD); assert.equal(m!.kind, 'request-new'); assert.ok(!JSON.stringify(m).includes('Themba')); }); it('shows the form\'s facts publicly: emergency numbers and the closing days, nothing else', async () => { const { visitor } = await setup(); const form = await visitor.get('/api/v1/requests/form'); assert.equal(form.status, 200); assert.equal(form.body.anonymousAllowed, false); assert.deepEqual(form.body.emergencyNumbers.map((n: { number: string }) => n.number), ['0861 322 322', '0800 567 567', '116']); assert.equal(form.body.closeAfterDays, 30); }); it('refuses a fully anonymous request unless the church allows them; then hides the sender from the team too', async () => { const { thandi, coord } = await setup(); assert.equal((await thandi.post('/api/v1/me/requests', { text: SECRET, anonymous: true })).body.error.code, 'ANONYMOUS_OFF'); assert.equal((await coord.put('/api/v1/admin/requests/settings', { anonymousAllowed: true })).status, 200); assert.equal((await thandi.post('/api/v1/me/requests', { text: SECRET, anonymous: true })).status, 201); const team = await coord.get('/api/v1/admin/requests?view=open'); assert.equal(team.body.requests[0].submitter, null); assert.ok(!JSON.stringify(team.body).includes('Thandi')); assert.ok(!JSON.stringify(team.body).includes(MEMBER)); }); it('a shared request waits for approval, then members see it with the first name or "Anonymous"', async () => { const { h, thandi, pieter, coord } = await setup(); const { id } = (await thandi.post('/api/v1/me/requests', { text: 'Pray for our exams next week', visibility: 'members', showName: true })).body; assert.deepEqual((await pieter.get('/api/v1/requests')).body.requests, []); assert.equal((await coord.get('/api/v1/admin/requests?view=queue')).body.requests.length, 1); assert.equal((await pieter.post(`/api/v1/admin/requests/${id}/approve`)).status, 403); assert.equal((await coord.post(`/api/v1/admin/requests/${id}/approve`)).status, 200); const list = (await pieter.get('/api/v1/requests')).body.requests; assert.equal(list[0].by, 'Thandi'); assert.equal(list[0].text, 'Pray for our exams next week'); assert.deepEqual(Object.keys(list[0]).sort(), ['by', 'createdAt', 'iPrayed', 'id', 'mine', 'prayedCount', 'text', 'updates']); assert.equal((await thandi.get('/api/v1/me/requests')).body.requests[0].status, 'shared'); assert.ok(outbox(h).some((m) => m.memberId === MEMBER && m.payload.what === 'approved')); }); it('counts "I prayed" once per member and shows the sender how many, not who', async () => { const { thandi, pieter, coord } = await setup(); const { id } = (await thandi.post('/api/v1/me/requests', { text: 'Pray for rain on the farm', visibility: 'members' })).body; assert.equal((await pieter.post(`/api/v1/requests/${id}/prayed`)).status, 404); await coord.post(`/api/v1/admin/requests/${id}/approve`); await pieter.post(`/api/v1/requests/${id}/prayed`); const again = await pieter.post(`/api/v1/requests/${id}/prayed`); assert.deepEqual(again.body, { prayedCount: 1, iPrayed: true }); const mine = (await thandi.get('/api/v1/me/requests')).body.requests[0]; assert.equal(mine.prayedCount, 1); assert.ok(!JSON.stringify(mine).includes('Pieter')); assert.ok(!JSON.stringify(mine).includes(OTHER)); }); it('redacts with a note to the sender, or removes with a reason', async () => { const { thandi, coord, pieter } = await setup(); const a = (await thandi.post('/api/v1/me/requests', { text: SECRET, visibility: 'members' })).body; assert.equal((await coord.post(`/api/v1/admin/requests/${a.id}/redact`, { text: 'Pray for my brother in hospital', note: '' })).status, 400); await coord.post(`/api/v1/admin/requests/${a.id}/redact`, { text: 'Pray for my brother in hospital', note: 'We took out his name.' }); await coord.post(`/api/v1/admin/requests/${a.id}/approve`); assert.equal((await pieter.get('/api/v1/requests')).body.requests[0].text, 'Pray for my brother in hospital'); const b = (await thandi.post('/api/v1/me/requests', { text: 'Something about a colleague', visibility: 'members' })).body; await coord.post(`/api/v1/admin/requests/${b.id}/remove`, { reason: 'It names someone at work.' }); const mine = (await thandi.get('/api/v1/me/requests')).body.requests; const byId = Object.fromEntries(mine.map((r: { id: string }) => [r.id, r])); assert.equal(byId[a.id].redactionNote, 'We took out his name.'); assert.equal(byId[b.id].status, 'not_shared'); assert.equal(byId[b.id].removedReason, 'It names someone at work.'); }); it('escalates to Pastoral care by email and WhatsApp at once; it leaves the shared list; the sender sees nothing different', async () => { const { h, thandi, pieter, coord, care } = await setup(); const { id } = (await thandi.post('/api/v1/me/requests', { text: 'I don\'t feel safe at home', visibility: 'members' })).body; await coord.post(`/api/v1/admin/requests/${id}/approve`); const before = (await thandi.get('/api/v1/me/requests')).body.requests[0]; const res = await coord.post(`/api/v1/admin/requests/${id}/escalate`, { note: 'Danger at home' }); assert.deepEqual([res.body.notified, res.body.fallback], [1, false]); const sent = outbox(h).find((m) => m.kind === 'request-escalated')!; assert.deepEqual([sent.memberId, sent.channel], [CARE, 'both']); assert.ok(!JSON.stringify(sent).includes('safe')); assert.deepEqual((await pieter.get('/api/v1/requests')).body.requests, []); assert.deepEqual((await thandi.get('/api/v1/me/requests')).body.requests[0], before); const esc = await care.get('/api/v1/admin/requests?view=escalated'); assert.equal(esc.body.requests[0].escalated.note, 'Danger at home'); assert.equal((await care.get('/api/v1/admin/requests?view=queue')).status, 403); }); it('tells an Owner or Church admin when nobody has the Pastoral care role', async () => { const h = await harness() as Harness; const thandi = await signInAs(h, { sub: MEMBER, name: 'Thandi' }); const coord = await signInAs(h, { sub: COORD, name: 'Lerato', roles: ['prayer_coordinator'] }, true); await h.db.member.create({ data: { id: 'owner-1', name: 'Pastor Sam', email: 'p@hope.test', roles: ['owner'] } }); const { id } = (await request(h.app).post('/api/v1/me/requests').set('Cookie', cookieHeader(thandi)).set('Origin', 'https://app.hope.test').send({ text: 'Please help me, urgently' })).body; const res = await request(h.app).post(`/api/v1/admin/requests/${id}/escalate`).set('Cookie', cookieHeader(coord)).set('Origin', 'https://app.hope.test').send({}); assert.deepEqual([res.body.notified, res.body.fallback], [1, true]); }); it('lets the sender add "Answered!", read the team\'s private reply, close it and delete it', async () => { const { thandi, pieter, coord } = await setup(); const { id } = (await thandi.post('/api/v1/me/requests', { text: 'Pray for my job interview', visibility: 'members' })).body; await coord.post(`/api/v1/admin/requests/${id}/approve`); await coord.post(`/api/v1/admin/requests/${id}/reply`, { text: 'We are praying. Let us know how it goes.' }); await thandi.post(`/api/v1/me/requests/${id}/updates`, { text: 'Answered! I got the job.' }); const shared = (await pieter.get('/api/v1/requests')).body.requests[0]; assert.deepEqual(shared.updates.map((u: { text: string }) => u.text), ['Answered! I got the job.']); const mine = (await thandi.get('/api/v1/me/requests')).body.requests[0]; assert.deepEqual(mine.updates.map((u: { author: string }) => u.author), ['team', 'submitter']); await thandi.post(`/api/v1/me/requests/${id}/close`); assert.deepEqual((await pieter.get('/api/v1/requests')).body.requests, []); assert.equal((await pieter.del(`/api/v1/me/requests/${id}`)).status, 404); assert.equal((await thandi.del(`/api/v1/me/requests/${id}`)).status, 200); }); it('closes requests after 30 days and deletes them 12 months after closing', async () => { const { h, thandi } = await setup(); await thandi.post('/api/v1/me/requests', { text: 'Pray for my family' }); h.clock.add(29 * DAY); assert.deepEqual(await tidyRequests(h.ctx, 365), { closed: 0, deleted: 0 }); h.clock.add(2 * DAY); assert.deepEqual(await tidyRequests(h.ctx, 365), { closed: 1, deleted: 0 }); h.clock.add(366 * DAY); assert.deepEqual(await tidyRequests(h.ctx, 365), { closed: 0, deleted: 1 }); }); it('is switched off with its module', async () => { const { h, thandi } = await setup(); await h.ctx.settings.set('modules', JSON.stringify({ 'prayer-requests': false })); assert.equal((await thandi.post('/api/v1/me/requests', { text: SECRET })).body.error.code, 'MODULE_OFF'); }); it('never puts a request in the heartbeat; exports and deletes a member\'s requests', async () => { const { h, thandi, pieter, coord } = await setup(); const { id } = (await thandi.post('/api/v1/me/requests', { text: SECRET, visibility: 'members' })).body; await coord.post(`/api/v1/admin/requests/${id}/approve`); await pieter.post(`/api/v1/requests/${id}/prayed`); const { stats } = await import('../src/hub/client.js'); assert.ok(!JSON.stringify(await stats(h.ctx)).includes('Themba')); const call = (type: string, extra: object = {}) => request(h.app).post('/api/accounts/v1/events').set('x-test-signed', 'yes').set('Content-Type', 'application/json') .send(JSON.stringify({ schema: 'crosscode.identity/1', callId: '77777777-7777-4777-8777-777777777777', occurredAt: '2026-12-01T06:00:00.000Z', sub: MEMBER, type, ...extra })); const exp = await call('user.export', { exportId: '88888888-8888-4888-8888-888888888888' }); const section = exp.body.sections.find((s: { key: string }) => s.key === 'app_prayer_requests'); assert.equal(section.records[0].text, SECRET); assert.equal(section.records[0].whoSees, 'Members of the church'); await call('user.deleted'); assert.equal(await h.db.prayerRequest.count(), 0); assert.equal(await h.db.requestPrayer.count(), 0); }); });