source: Klonkt/test/messages.test.js@ 544e9c2

main
Last change on this file since 544e9c2 was 544e9c2, checked in by Robin <roboburr@…>, 7 weeks ago

Feature: Messages "poll finished" notification (klonkt-demo-93o)

Closed own polls surface a poll_done item in Messages with the final tally (bar+pct per option, voter count), derived read-time from poll_json.closed via ownPollView. i18n msg.poll_done/msg.poll_total (NL/EN/DE). Tests in messages.test.js.

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

  • Property mode set to 100644
File size: 5.3 KB
Line 
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
7import { test } from 'node:test';
8import assert from 'node:assert/strict';
9
10process.env.DATABASE_PATH = ':memory:';
11process.env.PUBLIC_BASE_URL = 'https://klonkt.test';
12
13const dbMod = await import('../src/config/database.js');
14const db = dbMod.default;
15const AP = await import('../src/services/ActivityPubService.js');
16
17dbMod.initializeDatabase();
18
19// ── Seed: site + post + interacties + eigen reply ────────────────────────
20db.prepare('INSERT INTO users (id, username, email, password_hash, role) VALUES (?,?,?,?,?)')
21 .run('u1', 'u1', 'u1@test', 'x', 'god');
22db.prepare('INSERT INTO sites (id, slug, title, owner_id) VALUES (?,?,?,?)').run('s1', 'me', 'Me', 'u1');
23db.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
26function 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
33addInter('like', 'Anna', '2026-07-18 10:00:00');
34addInter('like', 'Ben', '2026-07-18 10:01:00');
35addInter('like', 'Cas', '2026-07-18 10:02:00');
36// een privé-reply (direct) ertussen, later
37addInter('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
39db.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
42const msgs = AP.getMessages('me', 50);
43
44test('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
52test('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
59test('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
66test('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});
71
72// ── Poll afgelopen → "resultaten zijn binnen"-item met tally ─────────────
73test('een afgelopen eigen peiling levert een poll_done-item met tally', () => {
74 db.prepare(`INSERT INTO posts (id, site_id, slug, author_id, title, content, status, type, poll_json, created_at, updated_at, published_at)
75 VALUES ('pp','s1','fav','u1','Favoriet?','<p>kies</p>','published','post',?,datetime('now'),datetime('now'),datetime('now'))`)
76 .run(JSON.stringify({ multiple: false, options: [{ name: 'A' }, { name: 'B' }], endTime: '2026-07-19T00:00:00Z', closed: true }));
77 const iv = db.prepare('INSERT OR IGNORE INTO poll_votes (post_id, actor_uri, choice) VALUES (?,?,?)');
78 iv.run('pp', 'https://r.test/u/x', 'A');
79 iv.run('pp', 'https://r.test/u/y', 'A');
80 iv.run('pp', 'https://r.test/u/z', 'B');
81
82 const fresh = AP.getMessages('me', 50);
83 const done = fresh.find((m) => m.type === 'poll_done');
84 assert.ok(done, 'poll_done-item ontbreekt');
85 assert.equal(done.post_slug, 'fav');
86 assert.equal(done.poll.voters, 3);
87 const a = done.poll.options.find((o) => o.name === 'A');
88 assert.equal(a.count, 2);
89 assert.equal(a.pct, 67); // round(2/3*100)
90});
91
92// Een nog lopende peiling (closed != 1) verschijnt NIET.
93test('een lopende peiling levert géén poll_done', () => {
94 db.prepare(`INSERT INTO posts (id, site_id, slug, author_id, title, content, status, type, poll_json, created_at, updated_at, published_at)
95 VALUES ('pp2','s1','open','u1','Open?','<p>kies</p>','published','post',?,datetime('now'),datetime('now'),datetime('now'))`)
96 .run(JSON.stringify({ multiple: false, options: [{ name: 'A' }, { name: 'B' }], endTime: '2030-01-01T00:00:00Z', closed: false }));
97 const fresh = AP.getMessages('me', 50);
98 assert.ok(!fresh.some((m) => m.type === 'poll_done' && m.post_slug === 'open'), 'lopende peiling hoort niet als poll_done te tonen');
99});
Note: See TracBrowser for help on using the repository browser.