import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; import request from 'supertest'; import { hub } from '@crosscode/contracts'; import { createApp } from '../src/app.js'; import { createCtx, type Db } from '../src/context.js'; import { stats } from '../src/hub/client.js'; import { loadConfig } from '../src/lib/config.js'; import { appVersion } from '../src/lib/version.js'; import { shortNameFor } from '../src/public/app.js'; import { harness, MEMBER } from './support/harness.js'; const DAY = 24 * 3600e3; describe('the app', () => { it('answers /api/health with the product and version', async () => { const { app } = await harness(); const res = await request(app).get('/api/health'); assert.equal(res.status, 200); assert.deepEqual(res.body, { status: 'ok', product: 'app', version: appVersion(), db: true }); }); it('says 503 when the database is down', async () => { const db = { $queryRaw: async () => { throw new Error('down'); } } as unknown as Db; const res = await request(createApp(createCtx(loadConfig({}), db))).get('/api/health'); assert.equal(res.status, 503); assert.equal(res.body.db, false); }); it('answers unknown API paths with a JSON 404 and keeps the API out of search', async () => { const { app } = await harness(); const res = await request(app).get('/api/nothing'); assert.equal(res.status, 404); assert.equal(res.body.error.code, 'NOT_FOUND'); assert.equal(res.headers['x-robots-tag'], 'noindex'); assert.equal(res.headers['x-powered-by'], undefined); }); it('gives every page the church, its colours and the modules on (defaults before a profile)', async () => { const { app } = await harness(); const res = await request(app).get('/api/v1/app'); assert.equal(res.status, 200); assert.equal(res.body.church, null); assert.equal(res.body.shortName, 'Church App'); assert.equal(res.body.palette.brand, '31 78 121'); assert.equal(res.body.palette.notes, undefined); assert.deepEqual(res.body.style, { fontPairing: 'warm', corners: 'soft' }); assert.deepEqual(res.body.modules, ['house-of-prayer', 'prayer-requests', 'announcements', 'devotionals', 'events']); assert.equal(res.body.timezone, 'Africa/Johannesburg'); }); it('cuts a long name to 12 characters for the home screen, unless there is a short name', () => { assert.equal(shortNameFor({ name: 'Acacia Hill Community Church', shortName: null }), 'Acacia Hill…'); assert.equal(shortNameFor({ name: 'Northpoint', shortName: null }), 'Northpoint'); assert.equal(shortNameFor({ name: 'Acacia Hill Community Church', shortName: 'Acacia Hill' }), 'Acacia Hill'); }); }); describe('the heartbeat', () => { it('reports the active-member band and the modules, never a name', async () => { const h = await harness(); const now = h.ctx.now().getTime(); for (let i = 0; i < 120; i++) { await h.db.member.create({ data: { id: `m-${i}`, name: `Person ${i}`, email: `p${i}@hope.test`, lastActiveAt: new Date(now - (i < 101 ? 89 : 91) * DAY) } }); } await h.db.member.create({ data: { id: MEMBER, name: 'Never used it', email: 'never@hope.test' } }); await h.ctx.settings.set('modules', JSON.stringify({ events: false })); const s = await stats(h.ctx); assert.ok(hub.appStatsSchema.safeParse(s).success); assert.equal(s.activeMembers90d, '101–300'); assert.deepEqual(s.modules, ['house-of-prayer', 'prayer-requests', 'announcements', 'devotionals']); assert.doesNotMatch(JSON.stringify(s), /Person|hope\.test/); }); }); describe('config', () => { it('refuses production without a secret', () => { assert.throws(() => loadConfig({ NODE_ENV: 'production' }), /APP_SECRET/); }); it('defaults to the development ports and an 8-day sign-in up to 90 days', () => { const c = loadConfig({}); assert.equal(c.port, 5400); assert.equal(c.publicUrl, 'http://localhost:3400'); assert.equal(c.sessionDays, 8); assert.equal(c.sessionMaxDays, 90); }); });