import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; import request from 'supertest'; import { hashToken } from '../src/lib/crypto.js'; import { siteContent } from '../src/site/content.js'; import { publish } from '../src/site/snapshot.js'; import { parseFeed, parseSource, syncYoutube } from '../src/site/youtube.js'; import { harness, SITE } from './support/harness.js'; const FEED = ` Hope Family Church yt:video:abcDEF12345 abcDEF12345 Hope & joy | Sunday 23 Nov 2026-11-23T08:00:00+00:00 zzzYYY98765 Bread for the road 2026-11-16T08:00:00+00:00 No id2026-11-01T08:00:00+00:00 `; async function signedIn() { const h = await harness({ linked: false }); await h.db.websiteUser.create({ data: { id: '00000000-0000-4000-8000-000000000003', name: 'Ed', email: 'ed@hope.test', role: 'editor' } }); await h.db.session.create({ data: { tokenHash: hashToken('t'), userId: '00000000-0000-4000-8000-000000000003', expiresAt: new Date(Date.now() + 864e5) } }); const go = (m: 'get' | 'post' | 'put' | 'delete', p: string, b?: object) => { const r = request(h.app)[m](`/api/v1/admin${p}`).set('Cookie', 'ccw_session=t').set('Origin', SITE); return b ? r.send(b) : r; }; await publish(h.ctx, { id: 'x', role: 'system' }); return { ...h, go }; } describe('messages from YouTube (ยง7)', () => { it('reads a channel id, a channel address or a playlist', () => { assert.deepEqual(parseSource('UCaBcDeFgHiJkLmNoPqRsTuV'), { kind: 'channel', id: 'UCaBcDeFgHiJkLmNoPqRsTuV' }); assert.deepEqual(parseSource('https://www.youtube.com/channel/UCaBcDeFgHiJkLmNoPqRsTuV/videos'), { kind: 'channel', id: 'UCaBcDeFgHiJkLmNoPqRsTuV' }); assert.deepEqual(parseSource('https://www.youtube.com/playlist?list=PLabcdefghij123'), { kind: 'playlist', id: 'PLabcdefghij123' }); assert.equal(parseSource('https://www.youtube.com/@hopefamily'), null); }); it('parses the feed, leaving out entries it can\'t use', () => { const v = parseFeed(FEED); assert.equal(v.length, 2); assert.equal(v[0]!.title, 'Hope & joy | Sunday 23 Nov'); assert.equal(v[0]!.thumbnail, 'https://i1.ytimg.com/vi/abcDEF12345/hqdefault.jpg'); }); it('adds new videos as live messages, once, and says when YouTube can\'t be reached', async () => { const h = await harness({ linked: false }); await h.ctx.settings.set('youtube_source', JSON.stringify({ kind: 'channel', id: 'UCaBcDeFgHiJkLmNoPqRsTuV' })); let asked = ''; const ok = async (u: string) => { asked = u; return { ok: true, status: 200, text: async () => FEED }; }; assert.deepEqual(await syncYoutube(h.ctx, ok), { added: 2 }); assert.equal(asked, 'https://www.youtube.com/feeds/videos.xml?channel_id=UCaBcDeFgHiJkLmNoPqRsTuV'); assert.deepEqual(await syncYoutube(h.ctx, ok), { added: 0 }); await publish(h.ctx, { id: 'x', role: 'system' }); const c = await siteContent(h.ctx); assert.equal(c.mode === 'published' && c.messages[0]!.title, 'Hope & joy | Sunday 23 Nov'); const down = await syncYoutube(h.ctx, async () => ({ ok: false, status: 503, text: async () => '' })); assert.match(String(down.error), /503/); assert.match(String(await h.ctx.settings.get('youtube_last_error')), /503/); }); }); describe('messages and events in the admin (decision 9)', () => { it('adds a message by link, publishes it on its own, and refuses a duplicate or a stranger link', async () => { const { go, ctx } = await signedIn(); const m = await go('post', '/messages', { title: 'Grace', date: '2026-11-22', mediaUrl: 'https://youtu.be/abcDEF12345', speaker: 'Thandi', series: 'Hope' }); assert.equal(m.status, 200); let c = await siteContent(ctx); assert.equal(c.mode === 'published' && c.messages.length, 0, 'not live until its own Publish'); await go('post', `/messages/${m.body.id}/publish`, { live: true }); c = await siteContent(ctx); assert.equal(c.mode === 'published' && c.messages[0]!.series, 'Hope'); assert.equal((await go('post', '/messages', { title: 'Again', date: '2026-11-22', mediaUrl: 'https://www.youtube.com/watch?v=abcDEF12345' })).body.error.code, 'DUPLICATE'); assert.equal((await go('post', '/messages', { title: 'X', date: '2026-11-22', mediaUrl: 'https://evil.example/v' })).status, 400); const spotify = await go('post', '/messages', { title: 'Podcast', date: '2026-11-22', mediaUrl: 'https://open.spotify.com/episode/4rOoJ6Egrf8K2IrywzwOMk' }); assert.equal(spotify.body.source, 'spotify'); }); it('keeps a feed message from being deleted (it would come back): hide it instead', async () => { const { go, db } = await signedIn(); const row = await db.message.create({ data: { slug: 'x', title: 'From YouTube', date: new Date(), source: 'youtube_feed', mediaUrl: 'https://www.youtube.com/watch?v=abcDEF12345', externalId: 'abcDEF12345' } }); assert.equal((await go('delete', `/messages/${row.id}`)).body.error.code, 'FROM_FEED'); }); it('creates, publishes, cancels and removes a simple event', async () => { const { go, ctx } = await signedIn(); const start = new Date(Date.now() + 7 * 864e5).toISOString(); const bad = await go('post', '/events', { title: 'Picnic', startsAt: start, endsAt: new Date(Date.now()).toISOString() }); assert.ok(bad.body.error.fields.endsAt); const e = await go('post', '/events', { title: 'Picnic', startsAt: start, location: 'The park', link: 'https://forms.example/picnic' }); assert.equal(e.status, 200); assert.equal(e.body.slug, 'picnic'); await go('post', `/events/${e.body.id}/state`, { published: true }); let c = await siteContent(ctx); assert.equal(c.mode === 'published' && c.events[0]!.url, 'https://forms.example/picnic'); await go('post', `/events/${e.body.id}/state`, { cancelled: true }); c = await siteContent(ctx); assert.equal(c.mode === 'published' && c.events.length, 0); assert.equal((await go('delete', `/events/${e.body.id}`)).status, 200); assert.equal((await go('get', '/events')).body.length, 0); }); });