// The signage editor's helpers (plan step 5.6a). import { test } from 'node:test' import assert from 'node:assert/strict' import { fitsOrientation, mediaEntry, misfits, moved, slideBody, slideFromTemplate, TEMPLATES, totalSeconds, windowParts } from '../src/pages/signage/signageLogic.js' test('templates fill their texts in the manager\'s language and copy their content', () => { const tpl = TEMPLATES.find(x => x.key === 'happyHour') const a = slideFromTemplate(tpl, k => `T(${k})`) assert.equal(a.kind, 'text') assert.equal(a.content.title, 'T(signage.templates.happyHour.title)') assert.equal(a.time_from, '17:00') a.content.background.colour = '#000000' assert.notEqual(tpl.content.background.colour, '#000000') // a copy, never the template itself assert.ok(TEMPLATES.every(x => ['image', 'video', 'text', 'menu'].includes(x.kind))) }) test('does a picture fit the playlist\'s shape', () => { assert.equal(fitsOrientation({ width: 1920, height: 1080 }, 'landscape'), true) assert.equal(fitsOrientation({ width: 1080, height: 1920 }, 'landscape'), false) assert.equal(fitsOrientation({ width: 1080, height: 1920 }, 'portrait'), true) assert.equal(fitsOrientation({ width: 800, height: 800 }, 'portrait'), true) assert.equal(fitsOrientation({}, 'portrait'), null) const media = { 1: { width: 1080, height: 1920 }, 2: { width: 1920, height: 1080 } } const slides = [{ id: 10, content: { media_id: 1 } }, { id: 11, content: { media_id: 2 } }, { id: 12, content: { background: { kind: 'media', media_id: 1 } } }, { id: 13, content: { background: { kind: 'colour', media_id: 1 } } }] assert.deepEqual(misfits(slides, media, 'landscape'), { 10: [1], 12: [1] }) }) test('the timeline: when a slide shows, the round\'s time, moving', () => { assert.deepEqual(windowParts({}), []) assert.deepEqual(windowParts({ days: [0, 1, 2, 3, 4, 5, 6] }), []) assert.deepEqual(windowParts({ days: [5, 6], time_from: '12:00' }).map(p => p.key), ['days', 'hours']) assert.equal(windowParts({ date_to: '2026-12-31' })[0].params.from, '…') const media = { 7: { duration_ms: 12_400 } } assert.equal(totalSeconds([{ enabled: true, duration_s: 8 }, { enabled: false, duration_s: 50 }, { enabled: true, kind: 'video', video_to_end: true, content: { media_id: 7 } }], media), 20) assert.deepEqual(moved([1, 2, 3, 4], 3, 0), [4, 1, 2, 3]) assert.deepEqual(moved([1, 2, 3], 0, 9), [1, 2, 3]) }) test('what is sent back, and library items as the slides need them', () => { const b = slideBody({ id: 1, kind: 'text', name: '', duration_s: 9, enabled: 1, days: undefined, content: { title: 'x' }, menu: {} }) assert.deepEqual(b, { name: null, duration_s: 9, transition: null, video_to_end: false, enabled: true, days: [], time_from: null, time_to: null, date_from: null, date_to: null, content: { title: 'x' } }) assert.deepEqual(mediaEntry({ id: 3, kind: 'video', name: 'v', width: 1, height: 2, duration_ms: 5, urls: { original: '/o' } }).url, '/o') assert.equal(mediaEntry({ id: 3, kind: 'image', urls: { full: '/f', original: '/o', thumb: '/t' } }).url, '/f') }) test('the board\'s own order from what it shows; moving categories and products; toggling ids', async () => { const { boardOrder, moveGroup, moveItem, toggled } = await import('../src/pages/signage/signageLogic.js') const groups = [{ category: { id: 1 }, items: [{ id: 10 }, { id: 11 }] }, { category: { id: 2 }, items: [{ id: 20 }] }] assert.deepEqual(boardOrder(groups), { category_order: [1, 2], product_order: [10, 11, 20] }) assert.deepEqual(boardOrder(moveGroup(groups, 1, -1)), { category_order: [2, 1], product_order: [20, 10, 11] }) assert.deepEqual(boardOrder(moveItem(groups, 0, 1, -1)).product_order, [11, 10, 20]) assert.deepEqual(boardOrder([{ category: null, items: [{ id: 5 }] }]), { category_order: [], product_order: [5] }) assert.deepEqual(toggled([3, 1], 2), [1, 2, 3]) assert.deepEqual(toggled([1, 2], 2), [1]) assert.deepEqual(toggled(undefined, 4), [4]) }) test('widgets moved and sized on the preview: boxes kept on the slide, snapping, custom and back (2026-11-21)', async () => { const { boxFromRect, moveBox, placeWidget, presetWidget, resizeBox, snapBox } = await import('../src/pages/signage/signageLogic.js') const frame = { left: 100, top: 50, width: 800, height: 450 } assert.deepEqual(boxFromRect({ left: 500, top: 95, width: 160, height: 45 }, frame), { x: 50, y: 10, w: 20, h: 10 }) const b = { x: 50, y: 10, w: 20, h: 10 } assert.deepEqual(moveBox(b, 40, -20), { x: 80, y: 0, w: 20, h: 10 }) // stopped at the edges assert.deepEqual(resizeBox(b, 'se', 5, 5), { x: 50, y: 10, w: 25, h: 15 }) assert.deepEqual(resizeBox(b, 'nw', 30, 30), { x: 67, y: 17, w: 3, h: 3 }) // never smaller than 3 % assert.deepEqual(resizeBox(b, 'e', 90, 0), { x: 50, y: 10, w: 50, h: 10 }) const s = snapBox({ x: 40.5, y: 0.8, w: 20, h: 10 }) // its middle near 50, its top near 0 assert.deepEqual(s.box, { x: 40, y: 0, w: 20, h: 10 }) assert.deepEqual(s.guides, ['x:50', 'y:0']) assert.deepEqual(snapBox({ x: 10, y: 10, w: 89.5, h: 10 }, 'e').box, { x: 10, y: 10, w: 90, h: 10 }) // a handle moves only its edge const w = placeWidget({ kind: 'clock', position: 'top-right', size: 'large' }, b) assert.deepEqual(w, { kind: 'clock', position: 'custom', size: 'custom', box: b }) assert.deepEqual(presetWidget(w, 'bottom-left'), { kind: 'clock', position: 'bottom-left', size: 'medium' }) assert.deepEqual(presetWidget({ kind: 'clock', position: 'top-right', size: 'large' }, 'top-left').size, 'large') }) test('a schedule line runs on weekdays or on smart days, not both (2026-11-21)', async () => { const { setDayShortcut, shortcutOf, toggleMonthDay, toggleWeekday } = await import('../src/pages/signage/signageLogic.js') const line = { days: [0, 1], month_days: [] } assert.deepEqual(toggleMonthDay(line, 'holiday'), { month_days: ['holiday'], days: [] }) assert.deepEqual(toggleWeekday({ days: [], month_days: ['first'] }, 3), { days: [3], month_days: [] }) assert.deepEqual(setDayShortcut('weekend'), { days: [5, 6], month_days: [] }) assert.equal(shortcutOf({ days: [], month_days: [] }), 'everyday') assert.equal(shortcutOf({ days: [], month_days: ['last'] }), null) // a smart day: "every day" isn't lit assert.equal(shortcutOf({ days: [0, 1, 2, 3, 4], month_days: [] }), 'weekdays') }) test('the Slides library: what is saved, and a copy for a playlist', async () => { const { savedBody, slideFromSaved } = await import('../src/pages/signage/signageLogic.js') const saved = { id: 4, name: ' Logo ', description: ' ', orientation: 'portrait', kind: 'blank', duration_s: 12, enabled: true, days: [], content: { widgets: [{ kind: 'name' }] } } const body = savedBody(saved) assert.equal(body.name, 'Logo') assert.equal(body.description, null) assert.equal(body.orientation, 'portrait') const copy = slideFromSaved(saved) assert.equal(copy.kind, 'blank') assert.equal(copy.duration_s, 12) copy.content.widgets[0].kind = 'clock' assert.equal(saved.content.widgets[0].kind, 'name') // a copy, never the kept slide }) test('a colour with its opacity: #rrggbbaa, typed by hand (owner review 4)', async () => { const { COLOUR_PRESETS, joinAlpha, splitAlpha, typedColour } = await import('../src/pages/signage/signageLogic.js') assert.equal(COLOUR_PRESETS.length, 12) assert.deepEqual(splitAlpha('#FF0000'), { rgb: '#ff0000', alpha: 100 }) assert.deepEqual(splitAlpha('#ff000080'), { rgb: '#ff0000', alpha: 50 }) assert.equal(joinAlpha('#ff0000', 100), '#ff0000') assert.equal(joinAlpha('#ff0000', 50), '#ff000080') assert.equal(joinAlpha('#ff0000', 0), '#ff000000') assert.equal(typedColour('abc'), '#aabbcc') assert.equal(typedColour(' #11223344 '), '#11223344') assert.equal(typedColour('#12345'), null) })