| 1 | // External embeds are a gated feature (FEP-633c): a ward's world outside the
|
|---|
| 2 | // fediverse is the guardians' call, and the gate is applied server-side.
|
|---|
| 3 | import { test } from 'node:test';
|
|---|
| 4 | import assert from 'node:assert/strict';
|
|---|
| 5 | process.env.DATABASE_PATH = ':memory:';
|
|---|
| 6 | process.env.PUBLIC_BASE_URL = 'https://test.example';
|
|---|
| 7 | const dbMod = await import('../src/config/database.js');
|
|---|
| 8 | dbMod.initializeDatabase();
|
|---|
| 9 | const { externalEmbedsAllowed } = await import('../src/services/guardianship/notes.js');
|
|---|
| 10 | const { firstExternalUrl, timelineEmbed } = await import('../src/services/ActivityPubService.js');
|
|---|
| 11 |
|
|---|
| 12 | test('auto (no setting): off for a ward, on for anyone else', () => {
|
|---|
| 13 | assert.equal(externalEmbedsAllowed(null, true), false, 'a ward gets no external embeds by default');
|
|---|
| 14 | assert.equal(externalEmbedsAllowed(null, false), true, 'a free actor does');
|
|---|
| 15 | assert.equal(externalEmbedsAllowed(undefined, true), false);
|
|---|
| 16 | });
|
|---|
| 17 |
|
|---|
| 18 | test('an explicit guardian decision wins over the default, both ways', () => {
|
|---|
| 19 | assert.equal(externalEmbedsAllowed(1, true), true, 'guardians may open it for a ward');
|
|---|
| 20 | assert.equal(externalEmbedsAllowed(0, false), false, 'and may close it for anyone');
|
|---|
| 21 | });
|
|---|
| 22 |
|
|---|
| 23 | test('firstExternalUrl picks the first real link, skipping mentions and hashtags', () => {
|
|---|
| 24 | const html = '<p><a href="https://s/@bob" class="u-url mention">@bob</a> '
|
|---|
| 25 | + '<a href="https://s/tags/x" class="mention hashtag">#x</a> '
|
|---|
| 26 | + 'kijk: <a href="https://v.example/watch/1">dit</a> en <a href="https://later.example/">dat</a></p>';
|
|---|
| 27 | assert.equal(firstExternalUrl(html), 'https://v.example/watch/1');
|
|---|
| 28 | });
|
|---|
| 29 |
|
|---|
| 30 | test('firstExternalUrl ignores non-http hrefs and empty content', () => {
|
|---|
| 31 | assert.equal(firstExternalUrl('<a href="javascript:alert(1)">x</a>'), null);
|
|---|
| 32 | assert.equal(firstExternalUrl('<p>geen links</p>'), null);
|
|---|
| 33 | assert.equal(firstExternalUrl(null), null);
|
|---|
| 34 | });
|
|---|
| 35 |
|
|---|
| 36 | test('timelineEmbed round-trips a stored card and refuses junk', () => {
|
|---|
| 37 | const stored = JSON.stringify({ url: 'https://v.example/1', kind: 'oembed', title: 'A talk', media: [{ url: 'https://v.example/t.jpg' }] });
|
|---|
| 38 | const back = timelineEmbed(stored);
|
|---|
| 39 | assert.equal(back.title, 'A talk');
|
|---|
| 40 | assert.equal(back.media[0].url, 'https://v.example/t.jpg');
|
|---|
| 41 | assert.equal(timelineEmbed(null), undefined);
|
|---|
| 42 | assert.equal(timelineEmbed('not json'), undefined);
|
|---|
| 43 | assert.equal(timelineEmbed('{"title":"no url"}'), undefined, 'a card without a url is not a card');
|
|---|
| 44 | });
|
|---|