| 1 | // Media alt text — the cover's alt and inline <img alt="…"> federate as the AS2 attachment
|
|---|
| 2 | // `name` (accessibility). In-memory SQLite. Run: npm test
|
|---|
| 3 | import { test } from 'node:test';
|
|---|
| 4 | import assert from 'node:assert/strict';
|
|---|
| 5 |
|
|---|
| 6 | process.env.DATABASE_PATH = ':memory:';
|
|---|
| 7 | process.env.PUBLIC_BASE_URL = 'https://test.example';
|
|---|
| 8 |
|
|---|
| 9 | const dbMod = await import('../src/config/database.js');
|
|---|
| 10 | const db = dbMod.default;
|
|---|
| 11 | dbMod.initializeDatabase();
|
|---|
| 12 | const AP = (await import('../src/services/ActivityPubService.js')).default;
|
|---|
| 13 |
|
|---|
| 14 | const BASE = 'https://test.example';
|
|---|
| 15 | db.prepare('INSERT INTO users (id, username, email, password_hash, role) VALUES (?,?,?,?,?)').run('u1', 'u1', 'u1@test', 'x', 'god');
|
|---|
| 16 | db.prepare('INSERT INTO sites (id, slug, title, owner_id, is_primary) VALUES (?,?,?,?,?)').run('s1', 'demo', 'Demo', 'u1', 1);
|
|---|
| 17 | const site = db.prepare('SELECT * FROM sites WHERE id = ?').get('s1');
|
|---|
| 18 | site.primary_slug = 'demo';
|
|---|
| 19 |
|
|---|
| 20 | test('the cover alt federates as the attachment name', () => {
|
|---|
| 21 | const post = { id: 'a1', slug: 'x', title: '', content: '<p>hi</p>', tags: '[]', cover_image_url: '/media/c.webp', cover_alt: 'A red bicycle', created_at: '2026-01-01T00:00:00Z' };
|
|---|
| 22 | const note = AP.buildNote(BASE, site, post);
|
|---|
| 23 | const att = (note.attachment || []).find((a) => a.url.endsWith('c.webp'));
|
|---|
| 24 | assert.ok(att, 'cover attachment present');
|
|---|
| 25 | assert.equal(att.name, 'A red bicycle');
|
|---|
| 26 | });
|
|---|
| 27 |
|
|---|
| 28 | test('an inline <img alt="…"> federates its alt as the attachment name', () => {
|
|---|
| 29 | const post = { id: 'a2', slug: 'y', title: '', content: '<p><img src="/media/cat.png" alt="a sleeping cat"></p>', tags: '[]', created_at: '2026-01-01T00:00:00Z' };
|
|---|
| 30 | const note = AP.buildNote(BASE, site, post);
|
|---|
| 31 | const att = (note.attachment || []).find((a) => a.url.endsWith('cat.png'));
|
|---|
| 32 | assert.ok(att, 'inline image attachment present');
|
|---|
| 33 | assert.equal(att.name, 'a sleeping cat');
|
|---|
| 34 | });
|
|---|
| 35 |
|
|---|
| 36 | test('no alt → no name field (stays valid AS2)', () => {
|
|---|
| 37 | const post = { id: 'a3', slug: 'z', title: '', content: '<p>x</p>', tags: '[]', cover_image_url: '/media/n.webp', created_at: '2026-01-01T00:00:00Z' };
|
|---|
| 38 | const note = AP.buildNote(BASE, site, post);
|
|---|
| 39 | const att = (note.attachment || []).find((a) => a.url.endsWith('n.webp'));
|
|---|
| 40 | assert.ok(att, 'cover attachment present');
|
|---|
| 41 | assert.ok(!('name' in att), 'no name when no alt');
|
|---|
| 42 | });
|
|---|