| [f1a23b8] | 1 | // Messages-centrum: getMessages voegt notificaties + eigen outbound replies samen
|
|---|
| 2 | // in één stroom, groepeert opeenvolgende likes/boosts op dezelfde post, en geeft
|
|---|
| 3 | // visibility door (voor de privé-badge). Besluit Robin+Bart 2026-07-16.
|
|---|
| 4 | //
|
|---|
| 5 | // Run: npm test (= node --test)
|
|---|
| 6 |
|
|---|
| 7 | import { test } from 'node:test';
|
|---|
| 8 | import assert from 'node:assert/strict';
|
|---|
| 9 |
|
|---|
| 10 | process.env.DATABASE_PATH = ':memory:';
|
|---|
| 11 | process.env.PUBLIC_BASE_URL = 'https://klonkt.test';
|
|---|
| 12 |
|
|---|
| 13 | const dbMod = await import('../src/config/database.js');
|
|---|
| 14 | const db = dbMod.default;
|
|---|
| 15 | const AP = await import('../src/services/ActivityPubService.js');
|
|---|
| 16 |
|
|---|
| 17 | dbMod.initializeDatabase();
|
|---|
| 18 |
|
|---|
| 19 | // ── Seed: site + post + interacties + eigen reply ────────────────────────
|
|---|
| 20 | db.prepare('INSERT INTO users (id, username, email, password_hash, role) VALUES (?,?,?,?,?)')
|
|---|
| 21 | .run('u1', 'u1', 'u1@test', 'x', 'god');
|
|---|
| 22 | db.prepare('INSERT INTO sites (id, slug, title, owner_id) VALUES (?,?,?,?)').run('s1', 'me', 'Me', 'u1');
|
|---|
| 23 | db.prepare(`INSERT INTO posts (id, site_id, slug, author_id, title, content, status, type, created_at, updated_at, published_at)
|
|---|
| 24 | VALUES ('p1','s1','mijn-post','u1','Mijn post','<p>x</p>','published','post',datetime('now'),datetime('now'),datetime('now'))`).run();
|
|---|
| 25 |
|
|---|
| 26 | function addInter(kind, actor, createdAt, extra = {}) {
|
|---|
| 27 | db.prepare(`INSERT INTO ap_interactions (kind, post_id, object_uri, actor_uri, actor_name, actor_icon, content, visibility, created_at)
|
|---|
| 28 | VALUES (?,?,?,?,?,?,?,?,?)`)
|
|---|
| 29 | .run(kind, 'p1', extra.object_uri || '', 'https://r.test/u/' + actor, actor, extra.icon || null,
|
|---|
| 30 | extra.content || null, extra.visibility || 'public', createdAt);
|
|---|
| 31 | }
|
|---|
| 32 | // drie likes kort na elkaar (zelfde post) → moeten groeperen tot één item
|
|---|
| 33 | addInter('like', 'Anna', '2026-07-18 10:00:00');
|
|---|
| 34 | addInter('like', 'Ben', '2026-07-18 10:01:00');
|
|---|
| 35 | addInter('like', 'Cas', '2026-07-18 10:02:00');
|
|---|
| 36 | // een privé-reply (direct) ertussen, later
|
|---|
| 37 | addInter('reply', 'Dana', '2026-07-18 11:00:00', { object_uri: 'https://r.test/n/1', content: '<p>psst geheim</p>', visibility: 'direct' });
|
|---|
| 38 | // eigen outbound reply, nog later
|
|---|
| 39 | db.prepare(`INSERT INTO ap_outbox (id, site_slug, post_id, post_slug, in_reply_to, to_actor, to_handle, content, created_at)
|
|---|
| 40 | VALUES ('out1','me','p1','mijn-post','https://r.test/n/1','https://r.test/u/Dana','@dana@r.test','<p>mijn antwoord</p>','2026-07-18 12:00:00')`).run();
|
|---|
| 41 |
|
|---|
| 42 | const msgs = AP.getMessages('me', 50);
|
|---|
| 43 |
|
|---|
| 44 | test('eigen outbound reply zit als "sent" in de stroom (nieuwste eerst)', () => {
|
|---|
| 45 | const sent = msgs.find((m) => m.type === 'sent');
|
|---|
| 46 | assert.ok(sent, 'sent-item ontbreekt');
|
|---|
| 47 | assert.equal(sent.outboxId, 'out1');
|
|---|
| 48 | assert.equal(sent.to_handle, '@dana@r.test');
|
|---|
| 49 | assert.equal(msgs[0].type, 'sent', 'nieuwste item hoort bovenaan');
|
|---|
| 50 | });
|
|---|
| 51 |
|
|---|
| 52 | test('opeenvolgende likes op dezelfde post groeperen tot één item met count', () => {
|
|---|
| 53 | const likes = msgs.filter((m) => m.type === 'like');
|
|---|
| 54 | assert.equal(likes.length, 1, 'likes horen gegroepeerd te zijn');
|
|---|
| 55 | assert.equal(likes[0].count, 3);
|
|---|
| 56 | assert.equal(likes[0].actors.length, 3);
|
|---|
| 57 | });
|
|---|
| 58 |
|
|---|
| 59 | test('privé-reply draagt visibility voor de badge en heeft post-context', () => {
|
|---|
| 60 | const reply = msgs.find((m) => m.type === 'reply');
|
|---|
| 61 | assert.equal(reply.visibility, 'direct');
|
|---|
| 62 | assert.equal(reply.post_slug, 'mijn-post');
|
|---|
| 63 | assert.equal(reply.post_title, 'Mijn post');
|
|---|
| 64 | });
|
|---|
| 65 |
|
|---|
| 66 | test('notificationsSeenAt: 0 zonder watermark, daarna > 0', () => {
|
|---|
| 67 | assert.equal(AP.notificationsSeenAt('me'), 0);
|
|---|
| 68 | AP.markNotificationsSeen('me');
|
|---|
| 69 | assert.ok(AP.notificationsSeenAt('me') > 0);
|
|---|
| 70 | });
|
|---|