| 1 | // Rich replies (klonkt-demo-c7f fase 2): deliverReply accepts editor HTML,
|
|---|
| 2 | // sanitizes it, injects the parent mention into the first paragraph, stores the
|
|---|
| 3 | // reply language, and buildReplyNote carries contentMap. In-memory DB; the
|
|---|
| 4 | // parent actor lives on an unresolvable host, so delivery just queues.
|
|---|
| 5 |
|
|---|
| 6 | import { test } from 'node:test';
|
|---|
| 7 | import assert from 'node:assert/strict';
|
|---|
| 8 |
|
|---|
| 9 | process.env.DATABASE_PATH = ':memory:';
|
|---|
| 10 | process.env.PUBLIC_BASE_URL = 'https://klonkt.test';
|
|---|
| 11 |
|
|---|
| 12 | const dbMod = await import('../src/config/database.js');
|
|---|
| 13 | const db = dbMod.default;
|
|---|
| 14 | dbMod.initializeDatabase();
|
|---|
| 15 | const AP = await import('../src/services/ActivityPubService.js');
|
|---|
| 16 |
|
|---|
| 17 | db.prepare('INSERT INTO users (id, username, email, password_hash, role) VALUES (?,?,?,?,?)')
|
|---|
| 18 | .run('u1', 'robin', 'r@test', 'x', 'god');
|
|---|
| 19 | db.prepare('INSERT INTO sites (id, slug, title, owner_id) VALUES (?,?,?,?)').run('s1', 'me', 'Me', 'u1');
|
|---|
| 20 | db.prepare(`INSERT INTO posts (id, site_id, slug, author_id, title, content, status, created_at, updated_at)
|
|---|
| 21 | VALUES ('p1','s1','hallo','u1','Hallo','<p>x</p>','published',CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)`).run();
|
|---|
| 22 |
|
|---|
| 23 | const site = db.prepare('SELECT * FROM sites WHERE slug = ?').get('me');
|
|---|
| 24 | const parent = {
|
|---|
| 25 | id: 1, post_id: 'p1', actor_uri: 'https://unresolvable.invalid/u/alice',
|
|---|
| 26 | actor_url: 'https://unresolvable.invalid/@alice', actor_handle: '@alice@unresolvable.invalid',
|
|---|
| 27 | object_uri: 'https://unresolvable.invalid/notes/1',
|
|---|
| 28 | };
|
|---|
| 29 |
|
|---|
| 30 | test('rich html is sanitized, mention lands in the first paragraph, language stored', async () => {
|
|---|
| 31 | const r = await AP.deliverReply(site, {
|
|---|
| 32 | postId: 'p1', postSlug: 'hallo', parent,
|
|---|
| 33 | text: '', html: '<p>Dag <strong>Alice</strong>!</p><script>evil()</script>',
|
|---|
| 34 | language: 'nl',
|
|---|
| 35 | });
|
|---|
| 36 | assert.ok(r && r.id, 'reply stored');
|
|---|
| 37 | const row = db.prepare('SELECT * FROM ap_outbox WHERE id = ?').get(r.id);
|
|---|
| 38 | assert.match(row.content, /<strong>Alice<\/strong>/);
|
|---|
| 39 | assert.doesNotMatch(row.content, /<script/i);
|
|---|
| 40 | assert.match(row.content, /^<p><a [^>]*class="u-url mention"/); // mention in first <p>
|
|---|
| 41 | assert.equal(row.language, 'nl');
|
|---|
| 42 |
|
|---|
| 43 | const note = AP.buildReplyNote('https://klonkt.test', site, row);
|
|---|
| 44 | assert.equal(note.type, 'Note');
|
|---|
| 45 | assert.deepEqual(Object.keys(note.contentMap), ['nl']);
|
|---|
| 46 | assert.equal(note.contentMap.nl, note.content);
|
|---|
| 47 | });
|
|---|
| 48 |
|
|---|
| 49 | test('rich html without a leading <p> gets the mention as its own paragraph', async () => {
|
|---|
| 50 | const r = await AP.deliverReply(site, {
|
|---|
| 51 | postId: 'p1', postSlug: 'hallo', parent,
|
|---|
| 52 | text: '', html: '<blockquote>quote</blockquote>', language: '',
|
|---|
| 53 | });
|
|---|
| 54 | const row = db.prepare('SELECT * FROM ap_outbox WHERE id = ?').get(r.id);
|
|---|
| 55 | assert.match(row.content, /^<p><a .*<\/p><blockquote>/s);
|
|---|
| 56 | assert.equal(row.language, null);
|
|---|
| 57 | assert.equal(AP.buildReplyNote('https://klonkt.test', site, row).contentMap, undefined);
|
|---|
| 58 | });
|
|---|
| 59 |
|
|---|
| 60 | test('the plain-text path is unchanged (escaped, br for newlines)', async () => {
|
|---|
| 61 | const r = await AP.deliverReply(site, {
|
|---|
| 62 | postId: 'p1', postSlug: 'hallo', parent, text: 'plain <b>niet</b>\ntweede',
|
|---|
| 63 | });
|
|---|
| 64 | const row = db.prepare('SELECT * FROM ap_outbox WHERE id = ?').get(r.id);
|
|---|
| 65 | assert.match(row.content, /plain <b>niet<\/b><br>tweede/);
|
|---|
| 66 | });
|
|---|
| 67 |
|
|---|
| 68 | test('empty rich html (only tags/whitespace) is rejected like empty text', async () => {
|
|---|
| 69 | const r = await AP.deliverReply(site, {
|
|---|
| 70 | postId: 'p1', postSlug: 'hallo', parent, text: '', html: '<p> </p><script>x()</script>',
|
|---|
| 71 | });
|
|---|
| 72 | assert.equal(r, null);
|
|---|
| 73 | });
|
|---|
| 74 |
|
|---|
| 75 | test('a bogus language code is dropped, not stored', async () => {
|
|---|
| 76 | const r = await AP.deliverReply(site, {
|
|---|
| 77 | postId: 'p1', postSlug: 'hallo', parent, text: '', html: '<p>taalcheck</p>', language: 'not a lang!',
|
|---|
| 78 | });
|
|---|
| 79 | const row = db.prepare('SELECT * FROM ap_outbox WHERE id = ?').get(r.id);
|
|---|
| 80 | assert.equal(row.language, null);
|
|---|
| 81 | });
|
|---|