Files
crosscode-platform/apps/website/backend/tests/happenings.test.ts
T
joshuaandClaude Opus 5.5 37a1f6acf1 feat(website): people and roles, messages with the YouTube feed, simple events
People: Owners and Admins add someone by email (people.ensure through
Accounts), change roles, remove access; only Owners touch Owners,
nobody changes themselves, a lower role ends sessions at once.
Messages: from a YouTube channel or playlist every hour (public feed,
no key) or by hand with a YouTube or Spotify link; own publish, hide.
Simple events: own publish, cancel, remove. Tests and e2e with axe on
the new screens.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 13:05:33 +02:00

112 lines
6.5 KiB
TypeScript

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 = `<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns:yt="http://www.youtube.com/xml/schemas/2015" xmlns:media="http://search.yahoo.com/mrss/" xmlns="http://www.w3.org/2005/Atom">
<title>Hope Family Church</title>
<entry>
<id>yt:video:abcDEF12345</id>
<yt:videoId>abcDEF12345</yt:videoId>
<title>Hope &amp; joy | Sunday 23 Nov</title>
<published>2026-11-23T08:00:00+00:00</published>
<media:group><media:thumbnail url="https://i1.ytimg.com/vi/abcDEF12345/hqdefault.jpg" width="480" height="360"/></media:group>
</entry>
<entry>
<yt:videoId>zzzYYY98765</yt:videoId>
<title>Bread for the road</title>
<published>2026-11-16T08:00:00+00:00</published>
</entry>
<entry><title>No id</title><published>2026-11-01T08:00:00+00:00</published></entry>
</feed>`;
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);
});
});