| [8cf8b5f] | 1 | // Cover art on federated audio — a fedi_open track's Audio attachment carries the track
|
|---|
| 2 | // cover (else the post cover) as an AS2 `icon`, so Mastodon's native audio player can show
|
|---|
| 3 | // artwork instead of a blank tile. In-memory SQLite. Run: npm test
|
|---|
| 4 | import { test } from 'node:test';
|
|---|
| 5 | import assert from 'node:assert/strict';
|
|---|
| 6 |
|
|---|
| 7 | process.env.DATABASE_PATH = ':memory:';
|
|---|
| 8 | process.env.PUBLIC_BASE_URL = 'https://test.example';
|
|---|
| 9 |
|
|---|
| 10 | const dbMod = await import('../src/config/database.js');
|
|---|
| 11 | const db = dbMod.default;
|
|---|
| 12 | dbMod.initializeDatabase();
|
|---|
| 13 | const AP = (await import('../src/services/ActivityPubService.js')).default;
|
|---|
| 14 |
|
|---|
| 15 | const BASE = 'https://test.example';
|
|---|
| 16 | db.prepare('INSERT INTO users (id, username, email, password_hash, role) VALUES (?,?,?,?,?)').run('u1', 'u1', 'u1@test', 'x', 'god');
|
|---|
| 17 | db.prepare('INSERT INTO sites (id, slug, title, owner_id, is_primary) VALUES (?,?,?,?,?)').run('s1', 'demo', 'Demo', 'u1', 1);
|
|---|
| 18 | const site = db.prepare('SELECT * FROM sites WHERE id = ?').get('s1');
|
|---|
| 19 | site.primary_slug = 'demo';
|
|---|
| 20 |
|
|---|
| 21 | db.prepare('INSERT INTO media (id, site_id, filename, mime_type, size, storage_path) VALUES (?,?,?,?,?,?)')
|
|---|
| 22 | .run('m1', 's1', 'song.mp3', 'audio/mpeg', 1000, '/x/song.mp3');
|
|---|
| 23 | db.prepare('INSERT INTO audio_tracks (id, site_id, title, media_id, cover_url, fedi_open) VALUES (?,?,?,?,?,?)')
|
|---|
| 24 | .run('t1', 's1', 'Song One', 'm1', '/media/art.webp', 1);
|
|---|
| 25 | db.prepare('INSERT INTO media (id, site_id, filename, mime_type, size, storage_path) VALUES (?,?,?,?,?,?)')
|
|---|
| 26 | .run('m2', 's1', 'bare.mp3', 'audio/mpeg', 1000, '/x/bare.mp3');
|
|---|
| 27 | db.prepare('INSERT INTO audio_tracks (id, site_id, title, media_id, fedi_open) VALUES (?,?,?,?,?)')
|
|---|
| 28 | .run('t2', 's1', 'Bare Track', 'm2', 1);
|
|---|
| 29 |
|
|---|
| 30 | test('a fedi_open track carries its cover as the Audio attachment icon', () => {
|
|---|
| 31 | const post = { id: 'p1', slug: 'x', title: '', content: '<p>[[track:t1]]</p>', tags: '[]', created_at: '2026-01-01T00:00:00Z' };
|
|---|
| 32 | const att = (AP.buildNote(BASE, site, post).attachment || []).find((a) => a.type === 'Audio');
|
|---|
| 33 | assert.ok(att, 'Audio attachment present');
|
|---|
| 34 | assert.ok(att.icon, 'icon present');
|
|---|
| 35 | assert.equal(att.icon.type, 'Image');
|
|---|
| 36 | assert.equal(att.icon.url, `${BASE}/media/art.webp`);
|
|---|
| 37 | });
|
|---|
| 38 |
|
|---|
| 39 | test('a coverless track falls back to the post cover', () => {
|
|---|
| 40 | const post = { id: 'p2', slug: 'y', title: '', content: '<p>[[track:t2]]</p>', tags: '[]', cover_image_url: '/media/post.webp', created_at: '2026-01-01T00:00:00Z' };
|
|---|
| 41 | const att = (AP.buildNote(BASE, site, post).attachment || []).find((a) => a.type === 'Audio');
|
|---|
| 42 | assert.equal(att.icon.url, `${BASE}/media/post.webp`);
|
|---|
| 43 | });
|
|---|
| 44 |
|
|---|
| 45 | test('no track cover and no post cover → no icon', () => {
|
|---|
| 46 | const post = { id: 'p3', slug: 'z', title: '', content: '<p>[[track:t2]]</p>', tags: '[]', created_at: '2026-01-01T00:00:00Z' };
|
|---|
| 47 | const att = (AP.buildNote(BASE, site, post).attachment || []).find((a) => a.type === 'Audio');
|
|---|
| 48 | assert.ok(!('icon' in att));
|
|---|
| 49 | });
|
|---|