| 1 | // Een playlist als AP-collectie (shaer-ayc, stap 1 van het Funkwhale-spoor).
|
|---|
| 2 | //
|
|---|
| 3 | // Vier beloftes: de collectie bestaat op een stabiele URI; alleen fedi_open-
|
|---|
| 4 | // tracks staan erin, met echte bestands-URL, en totalItems telt alleen die --
|
|---|
| 5 | // wie leest kan niet aftellen wat er achter de poort staat; de volgorde is de
|
|---|
| 6 | // playlist-volgorde, niet de invoegvolgorde; en een playlist van site A is op
|
|---|
| 7 | // de actor van site B een 404, want tenancy loopt door alles heen.
|
|---|
| 8 | import { test } from 'node:test';
|
|---|
| 9 | import assert from 'node:assert/strict';
|
|---|
| 10 |
|
|---|
| 11 | process.env.DATABASE_PATH = ':memory:';
|
|---|
| 12 | process.env.PUBLIC_BASE_URL = 'https://test.example';
|
|---|
| 13 |
|
|---|
| 14 | const dbMod = await import('../src/config/database.js');
|
|---|
| 15 | const db = dbMod.default;
|
|---|
| 16 | dbMod.initializeDatabase();
|
|---|
| 17 | const express = (await import('express')).default;
|
|---|
| 18 | const routes = (await import('../src/routes/activitypub.js')).default;
|
|---|
| 19 |
|
|---|
| 20 | db.prepare('INSERT INTO users (id, username, email, password_hash, role) VALUES (?,?,?,?,?)')
|
|---|
| 21 | .run('u1', 'u1', 'u1@t', 'x', 'god');
|
|---|
| 22 | db.prepare('INSERT INTO sites (id, slug, title, owner_id, is_public) VALUES (?,?,?,?,1)')
|
|---|
| 23 | .run('s1', 'band', 'De Band', 'u1');
|
|---|
| 24 | db.prepare('INSERT INTO sites (id, slug, title, owner_id, is_public) VALUES (?,?,?,?,1)')
|
|---|
| 25 | .run('s2', 'ander', 'Andere Site', 'u1');
|
|---|
| 26 |
|
|---|
| 27 | // Drie tracks: twee open, een gated. De gated staat MIDDEN in de volgorde,
|
|---|
| 28 | // zodat een fout die hem als lege rij meetelt ook de volgorde zou breken.
|
|---|
| 29 | const insMedia = db.prepare('INSERT INTO media (id, site_id, filename, storage_path, mime_type, size) VALUES (?,?,?,?,?,1000)');
|
|---|
| 30 | const insTrack = db.prepare('INSERT INTO audio_tracks (id, site_id, title, artist, duration, media_id, fedi_open, cover_url) VALUES (?,?,?,?,?,?,?,?)');
|
|---|
| 31 | insMedia.run('m1', 's1', 'een.mp3', 'audio/een.mp3', 'audio/mpeg');
|
|---|
| 32 | insMedia.run('m2', 's1', 'twee.mp3', 'audio/twee.mp3', 'audio/mpeg');
|
|---|
| 33 | insMedia.run('m3', 's1', 'drie.mp3', 'audio/drie.mp3', 'audio/mpeg');
|
|---|
| 34 | insTrack.run('t1', 's1', 'Opening', 'De Band', 215, 'm1', 1, '/media/hoes1.jpg');
|
|---|
| 35 | insTrack.run('t2', 's1', 'Achter de poort', 'De Band', 180, 'm2', 0, null);
|
|---|
| 36 | insTrack.run('t3', 's1', 'Finale', 'De Band', 240, 'm3', 1, null);
|
|---|
| 37 |
|
|---|
| 38 | db.prepare("INSERT INTO playlists (id, site_id, title, artist, year, cover_url, kind) VALUES ('nachtlicht','s1','Nachtlicht','De Band',2026,'/media/album.jpg','album')").run();
|
|---|
| 39 | const insPT = db.prepare('INSERT INTO playlist_tracks (playlist_id, track_id, position) VALUES (?,?,?)');
|
|---|
| 40 | // Invoegvolgorde is met opzet NIET de positievolgorde.
|
|---|
| 41 | insPT.run('nachtlicht', 't3', 3);
|
|---|
| 42 | insPT.run('nachtlicht', 't1', 1);
|
|---|
| 43 | insPT.run('nachtlicht', 't2', 2);
|
|---|
| 44 |
|
|---|
| 45 | const app = express();
|
|---|
| 46 | app.use(routes);
|
|---|
| 47 | const server = app.listen(0);
|
|---|
| 48 | await new Promise((r) => server.once('listening', r));
|
|---|
| 49 | const base = `http://127.0.0.1:${server.address().port}`;
|
|---|
| 50 | const getJson = async (path) => {
|
|---|
| 51 | const r = await fetch(base + path, { headers: { Accept: 'application/activity+json' } });
|
|---|
| 52 | return { status: r.status, body: r.status === 200 ? await r.json() : null, type: r.headers.get('content-type') || '' };
|
|---|
| 53 | };
|
|---|
| 54 |
|
|---|
| 55 | test('de collectie bestaat, als AS2, met stabiele id en attributedTo de actor', async () => {
|
|---|
| 56 | const { status, body, type } = await getJson('/ap/users/band/playlists/nachtlicht');
|
|---|
| 57 | assert.equal(status, 200);
|
|---|
| 58 | assert.match(type, /application\/activity\+json/);
|
|---|
| 59 | assert.equal(body.type, 'OrderedCollection');
|
|---|
| 60 | assert.equal(body.id, 'https://test.example/ap/users/band/playlists/nachtlicht');
|
|---|
| 61 | assert.equal(body.attributedTo, 'https://test.example/ap/users/band');
|
|---|
| 62 | assert.equal(body.name, 'Nachtlicht');
|
|---|
| 63 | assert.equal(body.summary, 'De Band · 2026');
|
|---|
| 64 | assert.equal(body.icon.url, 'https://test.example/media/album.jpg');
|
|---|
| 65 | });
|
|---|
| 66 |
|
|---|
| 67 | test('alleen fedi_open-tracks, totalItems telt alleen die, en de poort lekt geen bestandsnaam', async () => {
|
|---|
| 68 | const { body } = await getJson('/ap/users/band/playlists/nachtlicht');
|
|---|
| 69 | assert.equal(body.totalItems, 2, 'de gated track telt niet mee');
|
|---|
| 70 | assert.equal(body.orderedItems.length, 2);
|
|---|
| 71 | const alles = JSON.stringify(body);
|
|---|
| 72 | assert.ok(!alles.includes('twee.mp3'), 'de gated bestandsnaam staat nergens in de collectie');
|
|---|
| 73 | assert.ok(!alles.includes('Achter de poort'), 'ook de titel van de gated track niet');
|
|---|
| 74 | });
|
|---|
| 75 |
|
|---|
| 76 | test('de volgorde is de playlist-volgorde, en elke rij is een speelbare Audio', async () => {
|
|---|
| 77 | const { body } = await getJson('/ap/users/band/playlists/nachtlicht');
|
|---|
| 78 | assert.deepEqual(body.orderedItems.map((a) => a.name), ['Opening', 'Finale']);
|
|---|
| 79 | for (const a of body.orderedItems) {
|
|---|
| 80 | assert.equal(a.type, 'Audio');
|
|---|
| 81 | assert.match(a.url, /^https:\/\/test\.example\/audio\/stream\//);
|
|---|
| 82 | assert.equal(a.mediaType, 'audio/mpeg');
|
|---|
| 83 | }
|
|---|
| 84 | assert.equal(body.orderedItems[0].duration, 'PT215S');
|
|---|
| 85 | assert.equal(body.orderedItems[0].summary, 'De Band');
|
|---|
| 86 | // Track zonder eigen hoes erft de albumhoes -- zelfde regel als post-audio,
|
|---|
| 87 | // waar de postcover invalt.
|
|---|
| 88 | assert.equal(body.orderedItems[1].icon.url, 'https://test.example/media/album.jpg');
|
|---|
| 89 | });
|
|---|
| 90 |
|
|---|
| 91 | test('tenancy: dezelfde playlist op de verkeerde actor is een 404', async () => {
|
|---|
| 92 | const { status } = await getJson('/ap/users/ander/playlists/nachtlicht');
|
|---|
| 93 | assert.equal(status, 404);
|
|---|
| 94 | });
|
|---|
| 95 |
|
|---|
| 96 | test('onbekende playlist en onbekende site zijn allebei een 404', async () => {
|
|---|
| 97 | assert.equal((await getJson('/ap/users/band/playlists/bestaat-niet')).status, 404);
|
|---|
| 98 | assert.equal((await getJson('/ap/users/niemand/playlists/nachtlicht')).status, 404);
|
|---|
| 99 | });
|
|---|
| 100 |
|
|---|
| 101 | test.after(() => server.close());
|
|---|