| 1 | // Content language — a post's language federates as an AS2 contentMap (Mastodon's language
|
|---|
| 2 | // filter + translate button read the map's key). In-memory SQLite. Run: npm test
|
|---|
| 3 | import { test } from 'node:test';
|
|---|
| 4 | import assert from 'node:assert/strict';
|
|---|
| 5 |
|
|---|
| 6 | process.env.DATABASE_PATH = ':memory:';
|
|---|
| 7 | process.env.PUBLIC_BASE_URL = 'https://test.example';
|
|---|
| 8 |
|
|---|
| 9 | const dbMod = await import('../src/config/database.js');
|
|---|
| 10 | const db = dbMod.default;
|
|---|
| 11 | dbMod.initializeDatabase();
|
|---|
| 12 | const AP = (await import('../src/services/ActivityPubService.js')).default;
|
|---|
| 13 |
|
|---|
| 14 | const BASE = 'https://test.example';
|
|---|
| 15 | db.prepare('INSERT INTO users (id, username, email, password_hash, role) VALUES (?,?,?,?,?)').run('u1', 'u1', 'u1@test', 'x', 'god');
|
|---|
| 16 | db.prepare('INSERT INTO sites (id, slug, title, owner_id, is_primary) VALUES (?,?,?,?,?)').run('s1', 'demo', 'Demo', 'u1', 1);
|
|---|
| 17 | const site = db.prepare('SELECT * FROM sites WHERE id = ?').get('s1');
|
|---|
| 18 | site.primary_slug = 'demo';
|
|---|
| 19 |
|
|---|
| 20 | test('a post language federates as a contentMap keyed by the language', () => {
|
|---|
| 21 | const post = { id: 'l1', slug: 'x', title: '', content: '<p>hallo</p>', tags: '[]', language: 'nl', created_at: '2026-01-01T00:00:00Z' };
|
|---|
| 22 | const note = AP.buildNote(BASE, site, post);
|
|---|
| 23 | assert.ok(note.contentMap, 'contentMap present');
|
|---|
| 24 | assert.deepEqual(Object.keys(note.contentMap), ['nl']);
|
|---|
| 25 | assert.equal(note.contentMap.nl, note.content, 'contentMap value mirrors content');
|
|---|
| 26 | assert.ok(note.content, 'plain content still emitted alongside');
|
|---|
| 27 | });
|
|---|
| 28 |
|
|---|
| 29 | test('no language → no contentMap', () => {
|
|---|
| 30 | const post = { id: 'l2', slug: 'y', title: '', content: '<p>hi</p>', tags: '[]', created_at: '2026-01-01T00:00:00Z' };
|
|---|
| 31 | assert.ok(!('contentMap' in AP.buildNote(BASE, site, post)));
|
|---|
| 32 | });
|
|---|
| 33 |
|
|---|
| 34 | test('an invalid language code is ignored', () => {
|
|---|
| 35 | const post = { id: 'l3', slug: 'z', title: '', content: '<p>hi</p>', tags: '[]', language: 'not-a-lang!!', created_at: '2026-01-01T00:00:00Z' };
|
|---|
| 36 | assert.ok(!('contentMap' in AP.buildNote(BASE, site, post)));
|
|---|
| 37 | });
|
|---|