Files
crosscode-platform/apps/app/backend/tests/app.test.ts
T
joshuaandClaude Opus 5.5 bb36e06fa4 app: the Church App's foundation; docs for Hub 2.26.1 and two years of devotionals
apps/app (not released; app@1.0.0 is built in slices, APP-1.0-PLAN §15):
- backend (Express, Prisma, pg-boss, as the Website): the Hub client
  (CCP-, product app, the active-member band in the heartbeat), links
  and church.changed, sign-in through Accounts (everyone a Member; 8 days
  renewed up to 90; every role only with 2FA; the first Owner from
  HUB_ADMIN_EMAIL), Accounts' user.updated/export/deleted, active members
  once a day, modules on and off, GET /api/v1/app. Migration init.
- frontend (Next.js 16): header, tab bar, footer, manifest and icons from
  the church profile, service worker, Add to home screen, Me, the admin's
  first dashboard.
- env.template.json, PM2 template, README, CHANGELOG, WHATS-NEW; 27 tests.

Docs: the Hub runs 2.26.1 with two years of Cross Code's devotionals; an
App gets 2 weeks, today included. HANDOVER lists the App's slices.

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

89 lines
4.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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);
});
});