MC/website/tests/news-format.test.mjs
2026-11-27 23:43:05 +00:00

20 lines
3.2 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';
import {renderNewsBody,validateNewsContent,newsBBCode,newsPlainText} from '../web/news-format.mjs';
import {forumNewsRevision} from '../node/forum-news.mjs';
import {createApplication} from '../node/server.mjs';import {LocalD1} from './d1-adapter.mjs';
const article='# A dispatch\n\nOur **House** uses *care* and `code`. Read [the report](https://www.dunerealms.com/news).\n\n1. First item\n2. Second item\n\n- One\n- Two\n\n![House seal](https://www.dunerealms.com/assets/seals/house-seal.webp)';
test('one article retains formatting, link labels, ordered lists and image alt text across website, forum and plain delivery',()=>{
assert.equal(validateNewsContent(article),article);const html=renderNewsBody(article),forum=newsBBCode(article),plain=newsPlainText(article);
for(const expected of ['<h3>A dispatch</h3>','<strong>House</strong>','<em>care</em>','<code>code</code>','<ol><li>First item</li>','<ul><li>One</li>','alt="House seal"','rel="noopener noreferrer"'])assert.ok(html.includes(expected),expected);
for(const expected of ['[b]House[/b]','[i]care[/i]','[list=1]','[url=https://www.dunerealms.com/news]the report[/url]','[img]https://www.dunerealms.com/assets/seals/house-seal.webp[/img]'])assert.ok(forum.includes(expected),expected);
assert.match(plain,/Our House uses care and code\. Read the report/);assert.ok(!plain.includes('**'));assert.ok(plain.includes('House seal'));
const post=forumNewsRevision({id:'id',status:'published',published_at:1,title:'Title',body:article,category:'news',author_name:'Author'},2);assert.equal(post.format_version,3);assert.equal(post.body_bbcode,forum);assert.equal(post.author_name,'Author');
});
test('unsafe schemes, credentials, HTML and remote images fail validation; renderers cannot turn legacy input into active code',()=>{
for(const body of ['<script>alert(1)</script>','[click](javascript:alert(1))','[click](data:text/html,hello)','[click](https://user:pass@example.com)','![track](https://example.com/image.png)','![](https://www.dunerealms.com/assets/image.png)'])assert.throws(()=>validateNewsContent(body),body);
const html=renderNewsBody('<img src=x onerror=alert(1)>\n\n[click](javascript:alert(1))\n\n![track](https://evil.example/x.png)');assert.ok(!html.includes('<img'));assert.ok(!html.includes('href="javascript:'));assert.match(html,/&lt;img/);
const bb=newsBBCode('[img]https://evil.example/x.png[/img]\n\n<script>bad</script>');assert.ok(!bb.includes('[img]'));assert.ok(!bb.includes('<script>'));
});
test('public video artwork and existing plain articles retain supported rendering',()=>{assert.equal(renderNewsBody('A plain article.'),'<p>A plain article.</p>');assert.match(renderNewsBody('[Epic soundtrack](https://www.dunerealms.com/assets/video/epic.mp4)'),/<video controls autoplay muted loop/);});
test('the public formatter route serves the authored module rather than looking in the generated public folder',async()=>{const db=new LocalD1();try{const response=await createApplication(db)(new Request('https://www.dunerealms.com/news-format.mjs'));assert.equal(response.status,200);assert.match(response.headers.get('Content-Type'),/javascript/);assert.match(await response.text(),/export function renderNewsBody/);}finally{db.close();}});