| 1 | // C2S inbox read (owner only): the route maps ap_timeline rows to Create(Note)
|
|---|
| 2 | // items; here we cover the data path (fields the mapping relies on). The
|
|---|
| 3 | // bearer gate itself follows the followers-route pattern (verified live).
|
|---|
| 4 | import { test } from 'node:test';
|
|---|
| 5 | import assert from 'node:assert/strict';
|
|---|
| 6 |
|
|---|
| 7 | process.env.DATABASE_PATH = ':memory:';
|
|---|
| 8 | process.env.PUBLIC_BASE_URL = 'https://klonkt.test';
|
|---|
| 9 |
|
|---|
| 10 | const dbMod = await import('../src/config/database.js');
|
|---|
| 11 | const db = dbMod.default;
|
|---|
| 12 | dbMod.initializeDatabase();
|
|---|
| 13 | const AP = (await import('../src/services/ActivityPubService.js')).default;
|
|---|
| 14 |
|
|---|
| 15 | db.prepare(`INSERT INTO ap_timeline (id, slug, author_uri, author_name, content, url, published, nsfw, cw, created_at)
|
|---|
| 16 | VALUES ('https://r.test/n/1','me','https://r.test/u/a','A','<p>hoi</p>','https://r.test/@a/1','2026-07-01T10:00:00Z',1,'let op',CURRENT_TIMESTAMP)`).run();
|
|---|
| 17 |
|
|---|
| 18 | test('timeline rows carry the fields the inbox mapping needs', () => {
|
|---|
| 19 | const [t] = AP.getTimeline('me', 10);
|
|---|
| 20 | assert.equal(t.id, 'https://r.test/n/1');
|
|---|
| 21 | assert.equal(t.author_uri, 'https://r.test/u/a');
|
|---|
| 22 | assert.equal(t.content, '<p>hoi</p>');
|
|---|
| 23 | assert.equal(t.url, 'https://r.test/@a/1');
|
|---|
| 24 | assert.equal(t.nsfw, 1);
|
|---|
| 25 | assert.equal(t.cw, 'let op');
|
|---|
| 26 | assert.ok(t.published);
|
|---|
| 27 | });
|
|---|
| 28 |
|
|---|
| 29 | // Friends' media must reach the client: media_json ([{url, type}]) becomes the
|
|---|
| 30 | // AS2 attachment array on the inbox item, like own outbox posts (Shaer P2).
|
|---|
| 31 | test('media_json maps to AS2 attachments', () => {
|
|---|
| 32 | const rows = AP.timelineAttachments(JSON.stringify([
|
|---|
| 33 | { url: 'https://r.test/m/p.png', type: 'image/png' },
|
|---|
| 34 | { url: 'https://r.test/m/a.mp3', type: 'audio/mpeg' },
|
|---|
| 35 | ]));
|
|---|
| 36 | assert.equal(rows.length, 2);
|
|---|
| 37 | assert.deepEqual(rows[0], { type: 'Document', mediaType: 'image/png', url: 'https://r.test/m/p.png' });
|
|---|
| 38 | assert.deepEqual(rows[1], { type: 'Document', mediaType: 'audio/mpeg', url: 'https://r.test/m/a.mp3' });
|
|---|
| 39 | });
|
|---|
| 40 |
|
|---|
| 41 | test('unknown mediaType stays undefined, bad rows drop', () => {
|
|---|
| 42 | const rows = AP.timelineAttachments(JSON.stringify([
|
|---|
| 43 | { url: 'https://r.test/m/x.bin', type: '' },
|
|---|
| 44 | { type: 'image/png' }, // no url -> dropped
|
|---|
| 45 | ]));
|
|---|
| 46 | assert.equal(rows.length, 1);
|
|---|
| 47 | assert.equal(rows[0].mediaType, undefined);
|
|---|
| 48 | });
|
|---|
| 49 |
|
|---|
| 50 | test('empty or malformed media_json yields undefined and never throws', () => {
|
|---|
| 51 | assert.equal(AP.timelineAttachments(null), undefined);
|
|---|
| 52 | assert.equal(AP.timelineAttachments('[]'), undefined);
|
|---|
| 53 | assert.equal(AP.timelineAttachments('not json'), undefined);
|
|---|
| 54 | assert.equal(AP.timelineAttachments('{"not":"a list"}'), undefined);
|
|---|
| 55 | });
|
|---|