Files
pulse-signage/test/player-render-helpers.test.js
T

81 lines
2.6 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
require('../src/common');
const {
mediaKind,
normalizeSlide,
renderEditorJsContent,
sanitizeRichText
} = require('../src/player/render-helpers');
test('mediaKind classifies player media by extension', () => {
assert.equal(mediaKind('poster.PNG'), 'image');
assert.equal(mediaKind('intro.mp4'), 'video');
assert.equal(mediaKind('manual.pdf'), 'pdf');
assert.equal(mediaKind('notes.txt'), 'file');
});
test('sanitizeRichText strips unsafe content but preserves allowed markup', () => {
const html = '<div class="wrap"><a href="https://example.com" target="_blank">Link</a><script>alert(1)</script><span style="color:red">Text</span><img src="x" onerror="alert(1)"></div>';
assert.equal(
sanitizeRichText(html),
'<div class="wrap"><a href="https://example.com" target="_blank" rel="noreferrer noopener">Link</a><span style="color:red">Text</span></div>'
);
});
test('normalizeSlide normalizes nested content without mutating the source', () => {
const slide = {
id: 12,
content: {
hero: {
value: '{"headline":"Hello"}',
font_family: ' Open Sans! ',
font_size: '42',
font_color: 'not-a-color',
type: 'text'
},
footer: 'plain text'
}
};
const normalized = normalizeSlide(slide);
assert.notEqual(normalized.content, slide.content);
assert.deepEqual(normalized.content.hero, {
value: { headline: 'Hello' },
font_family: ' Open Sans! ',
font_size: '42',
font_color: 'not-a-color',
type: 'text'
});
assert.deepEqual(normalized.content.footer, {
type: 'text',
value: 'plain text'
});
assert.deepEqual(slide.content.hero, {
value: '{"headline":"Hello"}',
font_family: ' Open Sans! ',
font_size: '42',
font_color: 'not-a-color',
type: 'text'
});
});
test('renderEditorJsContent sanitizes editor blocks and wraps legacy text', () => {
const editorJson = {
blocks: [
{ type: 'header', data: { level: 2, text: '<strong>Title</strong><script>bad()</script>' } },
{ type: 'paragraph', data: { text: '<a href="javascript:alert(1)">bad</a><em>ok</em>' } },
{ type: 'list', data: { style: 'ordered', items: ['One', { text: '<span>Two</span>' }] } }
]
};
assert.equal(
renderEditorJsContent(editorJson),
'<h2><strong>Title</strong></h2><p><a>bad</a><em>ok</em></p><ol style="list-style-type:decimal;padding-left:1.4em;"><li>One</li><li><span>Two</span></li></ol>'
);
assert.equal(renderEditorJsContent('plain text'), '<p>plain text</p>');
});