Files
xenia-pos-local/packages/signage/tests/logic.test.mjs
T
bonaminandClaude Opus 5.5 1b993e4573 feat(signage): stacks, menu and text widgets, widget order (owner review 2)
- Stacks: horizontal / vertical, 2-6 parts with % shares; each part shows
  a widget, a stack one level deep, or a whole Slides-library slide
- Menu widget (a board's list in its box), text widget, weather for
  tomorrow / the day after and in picture / degrees / both styles
- Widgets' order (arrows, drag with a drop line), drawn in list order;
  arrow keys move the picked widget from its card too
- Server: extras by widget path (QR links, menus, embedded slides),
  POST /preview/slide, resolve(keep_hidden) so hidden products stay
  greyed in the editor's list; screens told when an embedded slide changes
- Editor rows tidied: framed sliders, colours as fields, text look on one
  row, Switch boxed toggles from its whole frame

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 09:44:23 +02:00

171 lines
10 KiB
JavaScript

// The signage player's logic (plan step 5.5a): the same schedule cases as the server's
// local_backend/tests/test_signage_schedule.py, slide windows, the next slide, the frame.
import assert from 'node:assert/strict'
import { test } from 'node:test'
import { boardPages, frameHeightVmin, frameStyle, localNow, matches, mediaUrls, menuMs, menuPages, nextSlide, pageAt, pick, playlistNow, rowsPerColumn, slideMs, weatherIcon } from '../src/logic.js'
const at = (date, hhmm) => {
const d = new Date(`${date}T12:00:00Z`)
const [h, m] = hhmm.split(':').map(Number)
return { weekday: (d.getUTCDay() + 6) % 7, minutes: h * 60 + m, date }
}
const MON_10 = at('2026-11-16', '10:00')
const MON_13 = at('2026-11-16', '13:00')
const SAT_13 = at('2026-11-21', '13:00')
const SAT_23 = at('2026-11-21', '23:30')
const SUN_01 = at('2026-11-22', '01:00')
const MON_01 = at('2026-11-16', '01:00')
test('the venue clock in its own time zone', () => {
const l = localNow('Europe/Athens', new Date('2026-11-16T22:30:00Z')) // 00:30 on Tuesday in Athens
assert.deepEqual(l, { weekday: 1, minutes: 30, date: '2026-11-17' })
})
test('which windows match (the server\'s cases)', () => {
const lunch = { time_from: '12:00', time_to: '15:00' }
assert.ok(matches(lunch, MON_13) && !matches(lunch, MON_10))
const night = { days: [5], time_from: '22:00', time_to: '02:00' }
assert.ok(matches(night, SAT_23) && matches(night, SUN_01) && !matches(night, MON_01))
const xmas = { date_from: '2026-12-20', date_to: '2026-12-27' }
assert.ok(matches(xmas, at('2026-12-24', '20:00')) && !matches(xmas, at('2026-12-28', '09:00')))
assert.ok(matches({}, MON_10))
assert.ok(matches({ time_from: '18:00' }, at('2026-11-16', '23:59')))
})
test('the schedule\'s winner (the server\'s cases)', () => {
const entries = [{ playlist_id: 10, time_from: '12:00', time_to: '15:00' }, { playlist_id: 20, days: [5, 6] },
{ playlist_id: 30, days: [5, 6], time_from: '12:00', time_to: '15:00' },
{ playlist_id: 40, date_from: '2026-11-21', date_to: '2026-11-21', priority: 5 }]
assert.equal(pick(entries, 99, MON_10), 99)
assert.equal(pick(entries, 99, MON_13), 10)
assert.equal(pick(entries, 99, at('2026-11-22', '13:00')), 30)
assert.equal(pick(entries, 99, SAT_13), 40)
assert.equal(pick(entries, null, MON_10), null)
})
test('the playlist now, alone or from a schedule', () => {
assert.equal(playlistNow({ playlist: { id: 1 } }).id, 1)
const data = { schedule: { timezone: 'Europe/Athens', entries: [{ playlist_id: 2, time_from: '12:00', time_to: '15:00' }], fallback_playlist_id: 3 },
playlists: { 2: { id: 2 }, 3: { id: 3 } } }
assert.equal(playlistNow(data, new Date('2026-11-16T11:00:00Z')).id, 2) // 13:00 in Athens
assert.equal(playlistNow(data, new Date('2026-11-16T08:00:00Z')).id, 3)
assert.equal(playlistNow(null), null)
})
test('the next slide skips the ones outside their window, and wraps', () => {
const slides = [{ id: 1 }, { id: 2, time_from: '12:00', time_to: '15:00' }, { id: 3, days: [0] }]
assert.equal(nextSlide(slides, -1, MON_10), 0)
assert.equal(nextSlide(slides, 0, MON_10), 2) // 2 is a lunch slide
assert.equal(nextSlide(slides, 2, MON_10), 0)
assert.equal(nextSlide(slides, 0, MON_13), 1)
assert.equal(nextSlide([{ id: 1, days: [6] }], -1, MON_10), -1)
assert.equal(nextSlide([], -1, MON_10), -1)
})
test('how long a slide stays; the frame for a turned TV; what to keep on the device', () => {
assert.equal(slideMs({ kind: 'text', duration_s: 8 }), 8000)
assert.equal(slideMs({ kind: 'video', video_to_end: true, duration_s: 8 }), null)
assert.equal(slideMs({ kind: 'image' }), 10000)
assert.equal(frameStyle(90).width, '100vh')
assert.equal(frameStyle(0).width, '100vw')
assert.equal(frameStyle(180).transform, 'rotate(180deg)')
const data = { schedule: {}, playlists: { 1: { media: { 5: { url: '/a' } } }, 2: { media: { 6: { url: '/b' }, 5: { url: '/a' } } } } }
assert.deepEqual(mediaUrls(data).sort(), ['/a', '/b'])
assert.deepEqual(mediaUrls({ playlist: null }), [])
const vid = { playlist: { media: { 7: { url: '/v/play', poster: '/v/full' } } } } // a video keeps its still frame too (5.4e)
assert.deepEqual(mediaUrls(vid).sort(), ['/v/full', '/v/play'])
})
test('a menu board in pages: headings count, a cut group repeats its heading', () => {
const g = [{ category: { id: 1, name: 'A' }, items: [1, 2, 3, 4, 5, 6, 7].map(id => ({ id })) }, { category: { id: 2, name: 'B' }, items: [{ id: 9 }] }]
const pages = menuPages(g, 4)
assert.deepEqual(pages.map(p => p.map(x => [x.category.name, x.items.length])), [[['A', 3]], [['A', 3]], [['A', 1], ['B', 1]]])
assert.deepEqual(menuPages(g, 4, false).map(p => p.reduce((n, x) => n + x.items.length, 0)), [4, 4])
assert.deepEqual(menuPages([], 4), [])
assert.equal(menuPages([{ category: null, items: [{ id: 1 }] }], 10).length, 1)
})
test('rows that fit: taller screens and smaller text fit more, photos fewer', () => {
assert.equal(frameHeightVmin(0, 1920, 1080), 100)
assert.ok(Math.abs(frameHeightVmin(0, 1080, 1920) - 177.8) < 0.1)
assert.ok(Math.abs(frameHeightVmin(90, 1920, 1080) - 177.8) < 0.1) // a wide TV turned: a tall frame
const base = rowsPerColumn({ heightVmin: 100 })
assert.ok(rowsPerColumn({ heightVmin: 178 }) > base && rowsPerColumn({ heightVmin: 100, textSize: 70 }) > base)
assert.ok(rowsPerColumn({ heightVmin: 100, photos: true }) < base && rowsPerColumn({ heightVmin: 100, bars: 2 }) < base)
assert.ok(rowsPerColumn({ heightVmin: 10 }) >= 3)
const slide = { kind: 'menu', content: { columns: 1, page_size: 5 }, menu: { groups: [{ category: null, items: Array.from({ length: 12 }, (_, i) => ({ id: i })) }] } }
assert.equal(boardPages(slide, 0, 1920, 1080).length, 3)
})
test('a menu slide stays for all its pages; the page on screen', () => {
const slide = { kind: 'menu', duration_s: 10, content: { page_seconds: 8 } }
assert.equal(menuMs(slide, 1), 10_000)
assert.equal(menuMs(slide, 3), 24_000)
assert.equal(pageAt(3, 8, 0), 0)
assert.equal(pageAt(3, 8, 8_100), 1)
assert.equal(pageAt(3, 8, 16_500), 2)
assert.equal(pageAt(3, 8, 24_100), 0) // round again on a long slide
assert.equal(pageAt(1, 8, 99_000), 0)
assert.equal(pageAt(3, 8, -400), 0)
})
test('weather codes → icons', () => {
assert.equal(weatherIcon(0, true), 'sun')
assert.equal(weatherIcon(0, false), 'moon')
assert.equal(weatherIcon(2), 'cloud-sun')
assert.equal(weatherIcon(61), 'rain')
assert.equal(weatherIcon(73), 'snow')
assert.equal(weatherIcon(95), 'storm')
assert.equal(weatherIcon(45), 'fog')
assert.equal(weatherIcon(null), 'cloud')
})
test('smart days and scenes, the same dates as the server (owner 2026-11-21)', async () => {
const { isHoliday, matches, monthDaysOk, orthodoxEaster, pick } = await import('../src/logic.js')
assert.deepEqual([2025, 2026, 2027].map(orthodoxEaster), ['2025-04-20', '2026-04-12', '2027-05-02'])
assert.ok(isHoliday('2026-03-25') && isHoliday('2026-02-23') && isHoliday('2026-04-10') && isHoliday('2026-04-13'))
assert.ok(isHoliday('2026-06-01') && !isHoliday('2026-06-02'))
assert.ok(monthDaysOk(['first'], '2026-11-01') && !monthDaysOk(['first'], '2026-11-02'))
assert.ok(monthDaysOk(['last'], '2026-02-28') && !monthDaysOk(['last'], '2026-02-27'))
assert.ok(monthDaysOk(['first_week'], '2026-11-07') && !monthDaysOk(['first_week'], '2026-11-08'))
assert.ok(monthDaysOk(['last_week'], '2026-11-24') && !monthDaysOk(['last_week'], '2026-11-23'))
assert.ok(!monthDaysOk(['not_holiday'], '2026-12-25') && monthDaysOk(['not_holiday'], '2026-12-24'))
const sat = { weekday: 5, minutes: 13 * 60, date: '2026-11-21' }
const entries = [{ playlist_id: 40, date_from: '2026-11-21', date_to: '2026-11-21', priority: 5 }, { playlist_id: 50, scene_id: 1, priority: 9 }]
assert.equal(pick(entries, 99, sat), 40)
assert.equal(pick(entries, 99, sat, [1]), 50)
assert.ok(!matches({ scene_id: 2 }, sat, [1]))
})
test('widgets placed by hand: how they line up, and a unit that fills their box (2026-11-21)', async () => {
const { fitUnit, textWidth, widgetAlign } = await import('../src/logic.js')
assert.equal(widgetAlign({ position: 'top-right' }), 'right')
assert.equal(widgetAlign({ position: 'bottom-center' }), 'center')
assert.equal(widgetAlign({ position: 'top-bar' }), 'left')
assert.equal(widgetAlign({ position: 'top-left', align: 'center' }), 'center') // its own choice
assert.equal(widgetAlign({ position: 'custom', box: { x: 70, y: 0, w: 25, h: 10 } }), 'right') // by its box's side
assert.equal(widgetAlign({ position: 'custom', box: { x: 40, y: 0, w: 20, h: 10 } }), 'center')
assert.equal(fitUnit(5, 20)(2.5), 'calc(2.5 * min(20cqh, 5cqw))')
assert.equal(textWidth(5, 4), 12)
})
test('a screen whose default is a library slide plays it when no line matches', () => {
const one = { id: 'slide:3', slides: [{ id: 3 }] }
const data = { schedule: { entries: [{ playlist_id: 1, date_from: '2000-01-01', date_to: '2000-01-01' }], fallback_playlist_id: 'slide:3', scenes: [], timezone: 'Europe/Athens' },
playlists: { 1: { id: 1, slides: [] }, 'slide:3': one } }
assert.equal(playlistNow(data, new Date('2026-11-21T12:00:00Z')), one)
})
test('stacks: even shares and a part resized against its neighbour; hidden products never on the TV (2026-11-21)', async () => {
const { boardPages, evenShares, setShare, visibleGroups } = await import('../src/logic.js')
assert.deepEqual(evenShares(3), [34, 33, 33])
assert.deepEqual(evenShares(2), [50, 50])
assert.deepEqual(setShare([50, 50], 0, 25), [25, 75])
assert.deepEqual(setShare([25, 25, 50], 2, 70), [25, 5, 70]) // the last part takes from the one before
assert.deepEqual(setShare([50, 50], 1, 99), [5, 95]) // its neighbour keeps 5 %
const groups = [{ category: { id: 1 }, items: [{ id: 1, hidden: true }] }, { category: { id: 2 }, items: [{ id: 2 }, { id: 3, hidden: true }] }]
assert.deepEqual(visibleGroups(groups), [{ category: { id: 2 }, items: [{ id: 2 }] }])
const pages = boardPages({ content: { columns: 1 }, menu: { groups } }, 0, 1920, 1080)
assert.deepEqual(pages.flat().flatMap(g => g.items.map(i => i.id)), [2])
})