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 { thumbnail } from '../src/site/thumbnails.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("the messages' thumbnails, served from the site (§7)", () => { const jpeg = Buffer.from([0xff, 0xd8, 0xff, 0xe0, 1, 2, 3]); const youtube = (body: Buffer | string, type = 'image/jpeg', status = 200) => { const asked: string[] = []; const fetcher = async (url: string) => { asked.push(url); return new Response(typeof body === 'string' ? body : new Uint8Array(body), { status, headers: { 'content-type': type } }); }; return { asked, fetcher }; }; it("gives visitors the site's own address, never YouTube's", async () => { const dir = tmp(); const h = await harness({ linked: false, env: { UPLOAD_DIR: dir } }); await loadDemo(h.ctx, 'heritage', { uploadDir: dir }); const content = await siteContent(h.ctx); assert.ok(content.mode === 'published' && content.messages.length > 0); for (const m of content.messages) assert.equal(m.thumbnail, '/api/thumbnails/aqz-KE-bpKQ.jpg'); }); it("fetches a message's thumbnail once and keeps it; nothing for a video that isn't the site's", async () => { const dir = tmp(); const h = await harness({ linked: false, env: { UPLOAD_DIR: dir } }); await loadDemo(h.ctx, 'heritage', { uploadDir: dir }); const yt = youtube(jpeg); assert.deepEqual(await thumbnail(h.ctx, 'aqz-KE-bpKQ', yt.fetcher), jpeg); assert.deepEqual(await thumbnail(h.ctx, 'aqz-KE-bpKQ', yt.fetcher), jpeg); assert.deepEqual(yt.asked, ['https://i.ytimg.com/vi/aqz-KE-bpKQ/hqdefault.jpg']); assert.ok(fs.existsSync(path.join(dir, '.thumbnails', 'aqz-KE-bpKQ.jpg'))); // Kept out of /api/uploads (a dot folder). assert.ok([403, 404].includes((await request(h.app).get('/api/uploads/.thumbnails/aqz-KE-bpKQ.jpg')).status)); // Not one of the site's messages, or not an id: YouTube isn't asked. assert.equal(await thumbnail(h.ctx, 'someoneElse1', yt.fetcher), null); assert.equal(await thumbnail(h.ctx, '../../etc/x', yt.fetcher), null); assert.equal(yt.asked.length, 1); }); it("keeps nothing that isn't a JPEG, and says 404 when YouTube has none", async () => { const dir = tmp(); const h = await harness({ linked: false, env: { UPLOAD_DIR: dir } }); await loadDemo(h.ctx, 'heritage', { uploadDir: dir }); assert.equal(await thumbnail(h.ctx, 'aqz-KE-bpKQ', youtube('', 'text/html').fetcher), null); assert.equal(await thumbnail(h.ctx, 'aqz-KE-bpKQ', youtube(Buffer.from('GIF89a'), 'image/jpeg').fetcher), null); assert.equal(await thumbnail(h.ctx, 'aqz-KE-bpKQ', youtube('', 'image/jpeg', 404).fetcher), null); assert.ok(!fs.existsSync(path.join(dir, '.thumbnails', 'aqz-KE-bpKQ.jpg'))); h.ctx.fetch = youtube('', 'image/jpeg', 404).fetcher; const missing = await request(h.app).get('/api/thumbnails/aqz-KE-bpKQ.jpg'); assert.equal(missing.status, 404); assert.equal(missing.headers['cache-control'], 'no-store'); h.ctx.fetch = youtube(jpeg).fetcher; const ok = await request(h.app).get('/api/thumbnails/aqz-KE-bpKQ.jpg'); assert.equal(ok.status, 200); assert.equal(ok.headers['content-type'], 'image/jpeg'); assert.equal((await request(h.app).get('/api/thumbnails/aqz-KE-bpKQ.png')).status, 404); }); }); 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); }); });