const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); require('../src/common'); const { buildDuplicatePlaylistName, buildDuplicatePlaylist } = require('../src/web/routes/signage/playlists/duplicate'); const renderPlaylistFormPage = require('../src/web/routes/signage/playlists/add'); test('buildDuplicatePlaylist copies playlist fields for the create page', () => { const duplicate = buildDuplicatePlaylist({ id: 12, name: 'Lunch Loop', fade_between_slides: 1, skip_unavailable_rtmp: 0, canvas_id: 7 }, buildDuplicatePlaylistName('Lunch Loop')); assert.equal(duplicate.id, 12); assert.equal(duplicate.name, 'Copy of Lunch Loop'); assert.equal(duplicate.fade_between_slides, true); assert.equal(duplicate.skip_unavailable_rtmp, false); assert.equal(duplicate.canvas_id, 7); }); test('playlist duplicate form page reuses the copied playlist id for rows', async () => { const html = await renderPlaylistFormPage( { id: 12, name: 'Copy of Lunch Loop', fade_between_slides: true, skip_unavailable_rtmp: false, canvas_id: 7 }, 'Review the copied values and save when ready.', { permissionKeys: ['playlists.create'] }, { playlists: [], canvasSizes: [{ id: 7, name: 'HD', width: 1280, height: 720 }], slides: [ { id: 3, title: 'Welcome', template_id: 8, content_json: '{}', thumbnail_path: '', canvas_size_id: 7, canvas_width: 1280, canvas_height: 720 } ], playlistSlides: [ { id: 21, playlist_id: 12, position: 0, duration_seconds: 10, use_video_duration: 0, disable_audio: 1, slide_id: 3, title: 'Welcome', content_json: '{}', thumbnail_path: '', canvas_size_id: 7, canvas_width: 1280, canvas_height: 720, scheduleRules: [] } ] }, null ); assert.match(html, /Copy of Lunch Loop/); assert.match(html, /data-playlist-id="12"/); assert.match(html, /Welcome/); assert.match(html, /Save and Close/); assert.ok(html.indexOf('id="playlist-create-group"') < html.indexOf('id="playlist-open-slide-modal"')); assert.doesNotMatch(html, /Group<\/th>/); assert.match(html, /name="group_key\[\]"/); assert.match(html, /name="group_schedule_rules_json\[\]"/); assert.match(html, /name="group_shuffle\[\]" value="0"/); assert.match(html, /name="group_mode\[\]" value="select"/); assert.match(html, /name="group_repeat_count\[\]" value="1"/); assert.match(html, /name="duration_seconds\[\]" type="number" inputmode="decimal" min="1" step="any"/); }); test('playlist list template includes duplicate action', () => { const template = fs.readFileSync(path.join(__dirname, '..', 'src', 'web', 'views', 'signage', 'playlists', 'list.hbs'), 'utf8'); assert.match(template, /\/playlists\/\{\{id\}\}\/duplicate/); assert.match(template, />Dupe<\/a>/); }); test('playlist group colours differ, survive reorder, and clear on ungrouped slides', () => { const source = fs.readFileSync(path.join(__dirname, '..', 'src', 'web', 'public', 'js', 'playlists', 'playlist-schedule.js'), 'utf8'); const functionSource = source.match(/function updatePlaylistGroupColours\(rows, blocks\) \{[\s\S]*?\n \}/)[0]; const updateColours = require('node:vm').runInNewContext('(' + functionSource + ')'); function makeRow(isHeader) { const attributes = new Map(); return { style: { setProperty(key, value) { this[key] = value; }, removeProperty(key) { delete this[key]; } }, getAttribute(key) { return attributes.get(key) || null; }, setAttribute(key, value) { attributes.set(key, value); }, matches() { return isHeader; } }; } const firstHeader = makeRow(true); const secondHeader = makeRow(true); firstHeader.setAttribute('data-group-colour', 'primary'); secondHeader.setAttribute('data-group-colour', 'secondary'); const firstMember = makeRow(false); const secondMember = makeRow(false); const blocks = [{ nodes: [firstHeader, firstMember] }, { nodes: [secondHeader, secondMember] }]; updateColours([firstMember, secondMember], blocks); const firstColour = firstHeader.style['--playlist-group-accent']; const secondColour = secondHeader.style['--playlist-group-accent']; assert.notEqual(firstColour, secondColour); assert.equal(firstHeader.getAttribute('data-group-colour'), 'success'); assert.equal(secondHeader.getAttribute('data-group-colour'), 'warning'); const paletteHeaders = Array.from({ length: 8 }, () => makeRow(true)); updateColours([], paletteHeaders.map(header => ({ nodes: [header] }))); paletteHeaders.forEach(header => { assert.ok(['success', 'warning', 'info', 'danger'].includes(header.getAttribute('data-group-colour'))); }); assert.equal(firstMember.style['--playlist-group-accent'], firstColour); assert.equal(secondMember.style['--playlist-group-bg'], secondHeader.style['--playlist-group-bg']); updateColours([secondMember, firstMember], blocks.slice().reverse()); assert.equal(firstHeader.style['--playlist-group-accent'], firstColour); assert.equal(secondHeader.style['--playlist-group-accent'], secondColour); blocks[0].nodes = [firstHeader]; updateColours([firstMember, secondMember], blocks); assert.equal(firstMember.style['--playlist-group-accent'], undefined); assert.equal(firstMember.style['--playlist-group-bg'], undefined); }); test('schedule rules use the same native AdminLTE icons as slide regions', () => { const root = path.join(__dirname, '..', 'src', 'web'); const sources = [ fs.readFileSync(path.join(root, 'views', 'signage', 'playlists', 'schedule-rule-card.hbs'), 'utf8'), fs.readFileSync(path.join(root, 'public', 'js', 'playlists', 'playlist-schedule.js'), 'utf8'), fs.readFileSync(path.join(root, 'public', 'js', 'slides', 'slide-form-regions.js'), 'utf8') ]; for (const source of sources) { assert.match(source, /