source: Klonkt/test/thread.test.js@ 9c9a3d9

main
Last change on this file since 9c9a3d9 was 9c9a3d9, checked in by Robin <roboburr@…>, 5 weeks ago

FEP-9098 emoji ook op de antwoorden in de thread

Barts punt (8-8): de threadroute liet de tag-array vallen, dus een reply
van een Mastodon-account rendert zijn :shortcode:-emoji als kale tekst.
Alleen de echte Emoji-tags gaan nu door -- naam plus geschoond
icoon-adres, gekapt op 30 -- en de rest van de vreemde tags blijft
achter. De byline-emoji zaten er al in via actorInfo.

Test uitgebreid: de emoji komt door, de hashtag en de emoji zonder
bruikbaar icoon niet, en een antwoord zonder emoji draagt geen tag-veld.

Co-Authored-By: Claude Opus 5 <noreply@…>

  • Property mode set to 100644
File size: 7.2 KB
Line 
1// De thread onder een post (shaer-tqz): ophalen, normaliseren, niet bewaren.
2//
3// De vier dingen die hier stil kunnen breken en die een gebruiker allemaal
4// anders voelt: een antwoord dat niet doorkomt (te streng), een vreemde die
5// bij een ward doorkomt (te los), HTML die ongeschoond doorreist (gevaarlijk),
6// en een geblokkeerde die meegeteld wordt in shaer:hidden (een blokkade hoort
7// onzichtbaar te zijn, ook als getal).
8//
9// In-memory SQLite, gestubde fetch op TEST-NET-3. Run: npm test
10
11import { test } from 'node:test';
12import assert from 'node:assert/strict';
13
14process.env.DATABASE_PATH = ':memory:';
15process.env.PUBLIC_BASE_URL = 'https://klonkt.test';
16
17const dbMod = await import('../src/config/database.js');
18const db = dbMod.default;
19dbMod.initializeDatabase();
20const AP = await import('../src/services/ActivityPubService.js');
21
22db.prepare('INSERT INTO users (id, username, email, password_hash, role) VALUES (?,?,?,?,?)')
23 .run('u1', 'u1', 'u1@test', 'x', 'god');
24db.prepare('INSERT INTO sites (id, slug, title, owner_id) VALUES (?,?,?,?)').run('s1', 'kind', 'Kind', 'u1');
25
26const BRON = 'https://203.0.113.10/notes/post-1';
27const TANTE = 'https://203.0.113.20/users/tante';
28const VREEMDE = 'https://203.0.113.30/users/vreemde';
29const GEBLOKT = 'https://203.0.113.40/users/naar';
30
31// De tante is gevolgd (accepted): zij zit in de kring van de guardians.
32db.prepare("INSERT INTO ap_following (slug, actor_uri, status) VALUES ('kind', ?, 'accepted')").run(TANTE);
33db.prepare("INSERT INTO ap_blocks (slug, kind, target) VALUES ('kind', 'actor', ?)").run(GEBLOKT);
34
35const actorDoc = (id, naam) => ({ id, type: 'Person', preferredUsername: naam, name: naam, inbox: `${id}/inbox` });
36const reply = (n, actor, content, published) => ({
37 id: `${BRON}/replies/${n}`, type: 'Note', attributedTo: actor,
38 inReplyTo: BRON, content, published,
39});
40
41const antwoorden = [
42 reply(1, VREEMDE, '<p>Hoi! <script>alert(1)</script></p>', '2026-08-01T10:00:00Z'),
43 { ...reply(2, TANTE, '<p>Dag lieverd :hartje:</p>', '2026-08-01T09:00:00Z'),
44 // FEP-9098 zoals Mastodon hem stuurt, plus een niet-Emoji-tag en een
45 // emoji zonder bruikbaar icoon: alleen de echte hoort erdoor te komen.
46 tag: [
47 { type: 'Emoji', name: ':hartje:', icon: { type: 'Image', url: 'https://203.0.113.20/emoji/hartje.png' } },
48 { type: 'Emoji', name: ':kapot:', icon: {} },
49 { type: 'Hashtag', name: '#muziek' },
50 ] },
51 reply(3, GEBLOKT, '<p>naar bericht</p>', '2026-08-01T11:00:00Z'),
52];
53
54globalThis.fetch = async (url) => {
55 const u = String(url);
56 const json = (o) => new Response(JSON.stringify(o), { status: 200, headers: { 'content-type': 'application/activity+json' } });
57 if (u === BRON) return json({ id: BRON, type: 'Note', attributedTo: TANTE, content: '<p>de post</p>', replies: `${BRON}/replies` });
58 if (u === `${BRON}/replies`) return json({ id: `${BRON}/replies`, type: 'Collection', first: `${BRON}/replies?page=1` });
59 if (u === `${BRON}/replies?page=1`) return json({ type: 'CollectionPage', orderedItems: antwoorden });
60 if (u === TANTE) return json(actorDoc(TANTE, 'tante'));
61 if (u === VREEMDE) return json(actorDoc(VREEMDE, 'vreemde'));
62 if (u === GEBLOKT) return json(actorDoc(GEBLOKT, 'naar'));
63 return new Response('not found', { status: 404 });
64};
65
66test('een gewone lezer krijgt alles behalve de geblokkeerde, oudste eerst, geschoond', async () => {
67 const uit = await AP.getThread('kind', BRON, { isWard: false });
68 assert.equal(uit.found, true);
69 assert.equal(uit.notes.length, 2);
70 // Oudste eerst: een gesprek lees je van boven naar beneden.
71 assert.equal(uit.notes[0]['shaer:author'].name, 'tante');
72 assert.equal(uit.notes[1]['shaer:author'].name, 'vreemde');
73 // De sanitizer heeft het script eruit gehaald, de tekst mag blijven.
74 assert.ok(!uit.notes[1].content.includes('<script'), 'script weggeschoond');
75 assert.ok(uit.notes[1].content.includes('Hoi!'));
76 // Een blokkade is onzichtbaar: niet in de lijst en NIET in de telling.
77 assert.equal(uit.hidden, 0);
78 assert.ok(uit.notes.every((n) => n.attributedTo !== GEBLOKT));
79 // FEP-9098: alleen de echte emoji komt door -- niet de hashtag, niet de
80 // emoji zonder icoon -- en het icoon-adres is geschoond.
81 const tags = uit.notes[0].tag;
82 assert.equal(tags.length, 1);
83 assert.equal(tags[0].name, ':hartje:');
84 assert.equal(tags[0].icon.url, 'https://203.0.113.20/emoji/hartje.png');
85 assert.ok(!uit.notes[1].tag, 'een antwoord zonder emoji draagt geen tag-veld');
86});
87
88test('een ward ziet alleen de kring van de guardians, de rest wordt geteld', async () => {
89 // Eigen cache-sleutel per (slug, uri) zou hier de vorige uitkomst hergeven;
90 // een andere slug dwingt een verse opbouw af zonder aan de cache te morrelen.
91 db.prepare("INSERT INTO sites (id, slug, title, owner_id) VALUES ('s2', 'pupil', 'Pupil', 'u1')").run();
92 db.prepare("INSERT INTO ap_following (slug, actor_uri, status) VALUES ('pupil', ?, 'accepted')").run(TANTE);
93 db.prepare("INSERT INTO ap_blocks (slug, kind, target) VALUES ('pupil', 'actor', ?)").run(GEBLOKT);
94 const uit = await AP.getThread('pupil', BRON, { isWard: true });
95 assert.equal(uit.notes.length, 1);
96 assert.equal(uit.notes[0]['shaer:author'].name, 'tante');
97 // De vreemde is er, en dat mag gezegd: geteld, niet stil weggelaten.
98 assert.equal(uit.hidden, 1);
99});
100
101test('een onbereikbare note zegt dat, in plaats van een lege thread te veinzen', async () => {
102 const uit = await AP.getThread('kind', 'https://203.0.113.99/weg', { isWard: false });
103 assert.equal(uit.found, false);
104 assert.equal(uit.notes.length, 0);
105});
106
107test('de Mastodon-vorm: first is een lege inline-pagina, de antwoorden staan op next', async () => {
108 // Precies wat Mastodon serveert en wat Barts melding (8-8) verklaarde: wie
109 // alleen de eerste pagina leest, ziet op elke Mastodon-post een leeg gesprek.
110 const MPOST = 'https://203.0.113.50/notes/masto-1';
111 const vorige = globalThis.fetch;
112 globalThis.fetch = async (url) => {
113 const u = String(url);
114 const json = (o) => new Response(JSON.stringify(o), { status: 200, headers: { 'content-type': 'application/activity+json' } });
115 if (u === MPOST) return json({
116 id: MPOST, type: 'Note', attributedTo: TANTE, content: '<p>toot</p>',
117 replies: {
118 id: `${MPOST}/replies`, type: 'Collection',
119 first: { type: 'CollectionPage', items: [], next: `${MPOST}/replies?page=true` },
120 },
121 });
122 if (u === `${MPOST}/replies?page=true`) return json({
123 type: 'CollectionPage',
124 items: [{ id: `${MPOST}/r1`, type: 'Note', attributedTo: TANTE, inReplyTo: MPOST, content: '<p>eerste echte antwoord</p>', published: '2026-08-02T10:00:00Z' }],
125 });
126 return vorige(url);
127 };
128 const uit = await AP.getThread('kind', MPOST, { isWard: false });
129 globalThis.fetch = vorige;
130 assert.equal(uit.notes.length, 1, 'het antwoord op de next-pagina is gevonden');
131 assert.ok(uit.notes[0].content.includes('eerste echte antwoord'));
132});
133
134test('de tweede lezing komt uit het geheugen, niet van het netwerk', async () => {
135 let calls = 0;
136 const vorige = globalThis.fetch;
137 globalThis.fetch = async (...a) => { calls += 1; return vorige(...a); };
138 const uit = await AP.getThread('kind', BRON, { isWard: false });
139 assert.equal(uit.notes.length, 2);
140 assert.equal(calls, 0, 'alles uit de cache, nul fetches');
141 globalThis.fetch = vorige;
142});
Note: See TracBrowser for help on using the repository browser.