Files
crosscode-platform/apps/website/backend/tests/site.test.ts
T
joshuaandClaude Opus 5.5 5ed7201efa feat(website): the four templates and the public site
Heritage, Bold, Community and Clean drawing all 26 section variants
with their headers, homepage order and style settings; colours made
safe (WCAG AA) by the backend; fonts and corners from the shared brand
style. Publishing into numbered versions, the content API with live
messages and events and the church profile from Accounts, uploads,
every page kind, simple event pages, Coming soon, 404, sitemap, SEO
basics and the church's JSON-LD. The demo church (npm run demo) and
Playwright e2e: every template's pages pass axe and the SEO checks.
website.sh builds packages/seo. Decision 17 (first Owner) recorded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 10:59:03 +02:00

150 lines
7.5 KiB
TypeScript

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 { WEBSITE_TEMPLATES } from '@crosscode/contracts';
import { loadDemo } from '../src/demo/demoChurch.js';
import { siteContent, spotifyEmbed, youtubeId } from '../src/site/content.js';
import { buildPalette, contrast, hexToRgb } from '../src/site/palette.js';
import { parseSectionData } from '../src/site/sections.js';
import { buildSnapshot, cleanDesign, publish } from '../src/site/snapshot.js';
import { defaultDesign } from '../src/site/variants.js';
import { harness } from './support/harness.js';
const rgb = (triple: string) => triple.split(' ').map(Number) as [number, number, number];
const tmp = () => fs.mkdtempSync(path.join(os.tmpdir(), 'ccw-'));
describe('colours from the church, made safe (§3)', () => {
it('keeps a dark colour as it is, with white text', () => {
const p = buildPalette('#1d5c63', '#e0a458');
assert.equal(p.brand, '29 92 99');
assert.equal(p.onBrand, '255 255 255');
assert.deepEqual(p.notes, []);
});
it('darkens a light colour for text on white, and says why', () => {
const p = buildPalette('#f5c542', null);
assert.ok(contrast(rgb(p.brandText), [255, 255, 255]) >= 4.5);
assert.ok(contrast(rgb(p.onBrand), rgb(p.brand)) >= 4.5);
assert.ok(p.notes.some((n) => n.includes('darker shade')));
});
it('always gives readable text on the brand colour, whatever the church picks', () => {
for (const hex of ['#ff0000', '#00ff00', '#0000ff', '#888888', '#ffffff', '#000000', '#ff8800', '#00aaaa']) {
const p = buildPalette(hex, hex);
assert.ok(contrast(rgb(p.onBrand), rgb(p.brand)) >= 4.5, hex);
assert.ok(contrast(rgb(p.brandText), [255, 255, 255]) >= 4.5, hex);
}
assert.deepEqual(hexToRgb('nonsense'), [31, 78, 121]);
});
});
describe('section data', () => {
it('fills defaults and refuses what doesn\'t fit', () => {
assert.deepEqual(parseSectionData('latestMessage', {}), { heading: null });
assert.equal(parseSectionData('hero', { headline: '' }), null);
assert.equal(parseSectionData('hero', { headline: 'Hi', button: { label: 'Go', href: 'javascript:alert(1)' } }), null);
assert.ok(parseSectionData('hero', { headline: 'Hi', button: { label: 'Go', href: '/visit' } }));
assert.equal(parseSectionData('nothing', {}), null);
});
});
describe('publishing (§4)', () => {
it('makes an unknown template, variant or section safe', () => {
const d = cleanDesign({ template: 'fancy', variants: { hero: 'spinning', team: 'list' }, home: [{ section: 'team' }, { section: 'nope' }], style: { spacing: 'compact', bad: 3 } });
assert.equal(d.template, 'heritage');
assert.equal(d.variants.hero, 'photo');
assert.equal(d.variants.team, 'list');
assert.equal(d.home[0]!.section, 'team');
assert.equal(d.home.length, 8);
assert.ok(d.home.slice(1).every((e) => e.hidden));
assert.equal(d.style.spacing, 'compact');
});
it('draws the homepage in the design\'s order and variants, leaving out hidden and broken sections', async () => {
const h = await harness();
const design = defaultDesign('clean');
await h.db.siteDesign.create({ data: { id: 1, template: 'clean', variants: design.variants, home: design.home, style: design.style } });
const home = await h.db.page.create({ data: { kind: 'home', slug: 'home', title: 'Home' } });
await h.db.section.create({ data: { pageId: home.id, type: 'hero', data: { headline: 'Welcome' } } });
await h.db.section.create({ data: { pageId: home.id, type: 'invitation', data: { heading: '' } } });
await h.db.section.create({ data: { pageId: home.id, type: 'team', data: {} } });
const snap = await buildSnapshot(h.ctx);
const types = snap.pages[0]!.sections.map((s) => s.type);
assert.deepEqual(types, ['hero', 'serviceTimes', 'welcome', 'events', 'latestMessage'], 'Clean hides Team; the empty invitation is left out');
assert.equal(snap.pages[0]!.sections[0]!.variant, 'text');
assert.equal(snap.pages[0]!.path, '/');
});
it('numbers versions and keeps every one', async () => {
const h = await harness();
await publish(h.ctx, { id: 'u1', role: 'owner' }, 'First');
const v2 = await publish(h.ctx, { id: 'u1', role: 'owner' });
assert.equal(v2.number, 2);
assert.equal(await h.db.siteVersion.count(), 2);
});
});
describe('the public site\'s content', () => {
it('is Coming soon before the first publish, from the church profile', async () => {
const h = await harness({ linked: false });
const c = await siteContent(h.ctx);
assert.equal(c.mode, 'coming-soon');
assert.equal(c.church, null);
assert.equal(c.style.fontPairing, 'classic', 'Heritage\'s default pairing');
assert.equal(c.style.corners, 'sharp');
});
for (const template of WEBSITE_TEMPLATES) {
it(`loads and publishes the demo church on ${template}`, async () => {
const h = await harness({ linked: false });
const dir = tmp();
await loadDemo(h.ctx, template, { uploadDir: dir });
const c = await siteContent(h.ctx);
assert.equal(c.mode, 'published');
if (c.mode !== 'published') return;
assert.equal(c.snapshot.design.template, template);
assert.equal(c.church?.name, 'Acacia Hill Church');
assert.equal(c.snapshot.pages.length, 9);
assert.equal(c.snapshot.ministries.length, 4);
assert.equal(c.messages.length, 3);
assert.equal(c.events.length, 3);
assert.ok(c.events[0]!.startsAt < c.events[1]!.startsAt);
assert.equal(c.transparentHeader['/'], template === 'bold', 'only Bold\'s header sits over the hero');
const hero = c.snapshot.pages[0]!.sections[0]!;
assert.equal(hero.type, 'hero');
const firstImage = (hero.data.images as string[])[0]!;
assert.match(c.snapshot.media[firstImage]!.url, /^\/api\/uploads\/demo\/hero\.svg$/);
assert.ok(fs.existsSync(path.join(dir, 'demo', 'hero.svg')));
});
}
it('serves the uploads, never a folder or outside it, and the content over HTTP', async () => {
const dir = tmp();
const h = await harness({ linked: false, env: { UPLOAD_DIR: dir } });
await loadDemo(h.ctx, 'community', { uploadDir: dir });
const svg = await request(h.app).get('/api/uploads/demo/hero.svg');
assert.equal(svg.status, 200);
assert.match(String(svg.headers['content-security-policy']), /sandbox/);
assert.equal((await request(h.app).get('/api/uploads/demo/')).status, 404);
assert.ok([403, 404].includes((await request(h.app).get('/api/uploads/..%2f..%2fpackage.json')).status));
assert.equal((await request(h.app).get('/api/uploads/missing.png')).status, 404);
const content = await request(h.app).get('/api/v1/site/content');
assert.equal(content.body.mode, 'published');
assert.equal(content.body.snapshot.design.template, 'community');
});
});
describe('message links', () => {
it('finds YouTube ids and Spotify embeds', () => {
assert.equal(youtubeId('https://www.youtube.com/watch?v=aqz-KE-bpKQ'), 'aqz-KE-bpKQ');
assert.equal(youtubeId('https://youtu.be/abc123'), 'abc123');
assert.equal(youtubeId('https://www.youtube.com/live/xyz'), 'xyz');
assert.equal(youtubeId('https://example.com/watch?v=abc'), null);
assert.equal(spotifyEmbed('https://open.spotify.com/episode/4rOoJ6Egrf8K2IrywzwOMk?si=1'), 'https://open.spotify.com/embed/episode/4rOoJ6Egrf8K2IrywzwOMk');
assert.equal(spotifyEmbed('https://evil.example/episode/x'), null);
});
});