import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import request from 'supertest'; import sharp from 'sharp'; import { churchExportSchema, INTERNAL_ID_KEYS } from '@crosscode/contracts'; import { buildExport } from '../src/export/churchExport.js'; import { cookieHeader, harness, MEMBER, PASTOR, signInAs, type Harness } from './support/harness.js'; const COORD = '55555555-5555-4555-8555-555555555555'; const ORIGIN = 'https://app.hope.test'; /** Every key in the export, at any depth. */ function keys(v: unknown, out: string[] = []): string[] { if (Array.isArray(v)) v.forEach((x) => keys(x, out)); else if (v && typeof v === 'object') for (const [k, x] of Object.entries(v)) { out.push(k); keys(x, out); } return out; } describe('the church export (ยง11)', () => { it('has the App\'s part in plain terms: members with roles, the wall, requests, content and uploads, no internal ids', async () => { const h = await harness({ env: { UPLOAD_DIR: fs.mkdtempSync(path.join(os.tmpdir(), 'cca-uploads-')) } }) as Harness; const as = (c: Record) => ({ post: (p: string, body: object) => request(h.app).post(p).set('Cookie', cookieHeader(c)).set('Origin', ORIGIN).send(body) }); const thandi = as(await signInAs(h, { sub: MEMBER, name: 'Thandi Mokoena', email: 'thandi@hope.test' })); const owner = await signInAs(h, { sub: PASTOR, name: 'Pastor Sam', roles: ['owner', 'prayer_coordinator'] }, true); await signInAs(h, { sub: COORD, name: 'Lerato', roles: ['prayer_coordinator'] }, true); assert.equal((await thandi.post('/api/v1/me/prayer/commitments', { startDate: '2026-12-02', startMinute: 120, durationMinutes: 60, recurrence: 'weekly', reminder: 'evening', channel: 'email', note: 'At home' })).status, 201); const req = await thandi.post('/api/v1/me/requests', { text: 'Please pray for my mother', visibility: 'members', showName: true }); await as(owner).post(`/api/v1/admin/requests/${req.body.id}/approve`, {}); const photo = await sharp({ create: { width: 40, height: 30, channels: 3, background: '#36c' } }).jpeg().toBuffer(); const img = await request(h.app).post('/api/v1/admin/media').set('Cookie', cookieHeader(owner)).set('Origin', ORIGIN).set('Content-Type', 'image/jpeg').send(photo); await h.db.announcement.create({ data: { slug: 'carols', title: 'Carols', body: 'Sunday at 19:00', imageUrl: img.body.url, publishAt: h.clock.now() } }); await h.db.announcement.create({ data: { slug: 'elsewhere', title: 'Elsewhere', body: 'x', imageUrl: 'https://cdn.example.com/a.jpg', publishAt: h.clock.now() } }); await h.db.devotional.create({ data: { source: 'church', date: '2026-12-01', title: 'Ours', reference: 'John 1:5', location: { book: 'JHN', chapter: 1, verse: 5, endChapter: 1, endVerse: 5 }, translation: 'KJV', passageText: 'And the light shineth in darkness', body: 'Text' } }); await h.db.devotional.create({ data: { source: 'crosscode', date: '2026-12-01', title: 'Cross Code\'s', reference: 'John 1:5', location: { book: 'JHN', chapter: 1, verse: 5, endChapter: 1, endVerse: 5 }, translation: 'KJV', passageText: 'x', body: 'x' } }); const x = await buildExport(h.ctx); assert.ok(churchExportSchema.safeParse(x).success); assert.equal(x.product, 'app'); const thandiP = x.people.find((p) => p.name === 'Thandi Mokoena')!; assert.deepEqual(x.people.find((p) => p.name === 'Pastor Sam')!.roles, ['app owner', 'prayer coordinator']); assert.deepEqual(thandiP.roles, ['member']); const app = x.app!; assert.equal(app.commitments.length, 1); assert.equal(app.commitments[0]!.personRef, thandiP.ref); assert.equal(app.commitments[0]!.startsAt, '2026-12-02T00:00:00.000Z', '02:00 in Johannesburg'); assert.equal(app.commitments[0]!.reminder, 'evening'); assert.equal(app.prayerRequests[0]!.status, 'shared'); assert.equal(app.prayerRequests[0]!.text, 'Please pray for my mother'); assert.equal(app.prayerRequests[0]!.moderation[0]!.action, 'approved'); assert.equal(app.prayerRequests[0]!.moderation[0]!.by, 'Pastor Sam'); assert.deepEqual(app.devotionals.map((d) => d.title), ['Ours'], 'Cross Code\'s aren\'t the church\'s'); const [carols, elsewhere] = app.announcements; assert.match(String(carols!.image), /^uploads\/2026\/12\/[0-9a-f-]+\.jpg$/); assert.equal(elsewhere!.image, null); assert.deepEqual(x.files.map((f) => f.path), [carols!.image]); assert.equal(app.safeguarding.emergencyNumbers.length, 3); // No internal ids anywhere: not as keys, not as values. assert.deepEqual(keys(x).filter((k) => INTERNAL_ID_KEYS.test(k)), []); const text = JSON.stringify(x); for (const id of [MEMBER, PASTOR, COORD, req.body.id, img.body.id]) assert.ok(!text.includes(id), `no ${id}`); }); });