source: Klonkt/test/post-language.test.js@ 3ccca13

main
Last change on this file since 3ccca13 was 0688b5f, checked in by roboburr <roboburr@…>, 2 months ago

feat(fediverse): post language → AS2 contentMap

A post can now carry a language; it federates as an AS2 contentMap (a BCP-47-keyed
copy of the content, alongside plain content) so Mastodon's timeline language filter
and translate button work. Editor gets a language picker (defaults to the author's
UI language).

  • src/config/database.js — posts.language column.
  • src/services/ActivityPubService.js — buildNote emits contentMap { <lang>: content } when the post has a valid BCP-47 language.
  • src/routes/posts.js — capture/validate/store language on create/save (default = the author's current language) and pass it to the federation hooks.
  • src/services/Scheduler.js — carry language when a scheduled post goes live.
  • src/views/pages/post-edit.ejs — language <select> (18 common languages).
  • src/services/i18n.js — pedit.f_language + pedit.language_hint (nl/en/de).
  • test/activitypub-as2.test.js — allow contentMap/nameMap/summaryMap; don't treat a language-map's keys as vocab terms; kitchen-sink post now sets a language.
  • test/post-language.test.js — contentMap shape, no-language, invalid-code = ignored.
  • CHANGELOG(.nl/.de).md — "Set a post's language" under Unreleased.

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

  • Property mode set to 100644
File size: 1.9 KB
Line 
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
3import { test } from 'node:test';
4import assert from 'node:assert/strict';
5
6process.env.DATABASE_PATH = ':memory:';
7process.env.PUBLIC_BASE_URL = 'https://test.example';
8
9const dbMod = await import('../src/config/database.js');
10const db = dbMod.default;
11dbMod.initializeDatabase();
12const AP = (await import('../src/services/ActivityPubService.js')).default;
13
14const BASE = 'https://test.example';
15db.prepare('INSERT INTO users (id, username, email, password_hash, role) VALUES (?,?,?,?,?)').run('u1', 'u1', 'u1@test', 'x', 'god');
16db.prepare('INSERT INTO sites (id, slug, title, owner_id, is_primary) VALUES (?,?,?,?,?)').run('s1', 'demo', 'Demo', 'u1', 1);
17const site = db.prepare('SELECT * FROM sites WHERE id = ?').get('s1');
18site.primary_slug = 'demo';
19
20test('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
29test('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
34test('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});
Note: See TracBrowser for help on using the repository browser.