Files
xenia-pos-local/packages/signage/tests/logic.test.mjs
T
bonaminandClaude Opus 5.5 2a38602d45 feat(manager): the venue's identity (Settings -> Identity): name, slogan, named colours, logos, guest Wi-Fi, contact details
Set once (GET/PUT /api/identity, venue.name + venue.identity); every colour field
offers the venue's colours first (VenueColoursProvider in @xenia/ui); signage screens
get the identity and the venue's time zone with their venue and are told on a change;
logos are protected in the media library; wifiQr() for the join-the-network code.
Plan 6R.14, F-111.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 13:10:58 +02:00

280 lines
17 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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)
// owner review 6: whole rounds only (10 s, 2 pages of 3 s → 2 rounds of 6 s = 12 s; 7 s of 2 × 3 s → 12 s; 12 s → 12 s)
assert.equal(menuMs({ kind: 'menu', duration_s: 10, content: { page_seconds: 3 } }, 2), 12_000)
assert.equal(menuMs({ kind: 'menu', duration_s: 7, content: { page_seconds: 3 } }, 2), 12_000)
assert.equal(menuMs({ kind: 'menu', duration_s: 12, content: { page_seconds: 3 } }, 2), 12_000)
assert.equal(menuMs({ kind: 'menu', duration_s: 2, content: { page_seconds: 5 } }, 3), 15_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])
})
test("a menu's look part by part: sizes against the board's text, colours fall back to text / accent (2026-11-21)", async () => {
const { menuLook, priceChars } = await import('../src/logic.js')
const plain = menuLook({ text_size: 100, text_colour: '#ffffff', accent_colour: '#f59e0b' })
assert.equal(plain.heading.fontSize, '1.15em')
assert.equal(plain.heading.color, '#f59e0b')
assert.equal(plain.item.color, '#ffffff')
assert.equal(plain.price.fontWeight, 700)
assert.equal(plain.lineHeight, 1.25)
assert.equal(plain.itemAlign, 'left')
assert.equal(plain.priceAlign, 'right')
const own = menuLook({ text_size: 200, heading_style: { size: 300, colour: '#ff0000', italic: true, align: 'center' }, price_style: { bold: false },
dot_style: { style: 'dashed', size: 200, colour: '#00ff00' }, line_height: 1.5, photo_shape: 'round', photo_size: 200, item_style: { size: 250 } })
assert.equal(own.heading.fontSize, '1.725em')
assert.equal(own.heading.fontStyle, 'italic')
assert.equal(own.heading.textAlign, 'center')
assert.equal(own.price.fontWeight, 400)
assert.equal(own.dots.borderBottom, '0.16em dashed #00ff00')
assert.equal(own.photo.radius, '50%')
assert.equal(own.photo.size, '4.4em')
assert.equal(own.rowScale, 1.5 * 1.2)
assert.deepEqual(menuLook({ dot_style: { style: 'none' } }).dots, { borderBottom: 'none' })
assert.equal(priceChars([{ items: [{ price: 4.5 }, { price: 12 }, { price: null }] }], p => `€${p.toFixed(2)}`), 6)
})
test('owner review 4: a part\'s widgets (one stored as `widget` too), badges that read', async () => {
const { cellWidgets, menuLook, readableOn } = await import('../src/logic.js')
assert.deepEqual(cellWidgets({ widgets: [{ kind: 'clock' }, { kind: 'date' }] }).map(w => w.kind), ['clock', 'date'])
assert.deepEqual(cellWidgets({ widget: { kind: 'qr' } }).map(w => w.kind), ['qr'])
assert.deepEqual(cellWidgets({ slide_id: 3 }), [])
assert.equal(readableOn('#111827'), '#ffffff')
assert.equal(readableOn('#1e3a8acc'), '#ffffff')
assert.equal(readableOn('#f59e0b'), '#111111')
assert.equal(readableOn('#fff'), '#111111')
assert.equal(menuLook({ accent_colour: '#123456', heading_style: { colour: '#ff0000' } }).title, '#ff0000')
})
test('owner review 5: the venue badges, several per product; a board with its own badge colours', async () => {
const { badgeColours, badgeName, DEFAULT_BADGES, itemBadges } = await import('../src/logic.js')
const mine = [{ key: 'gf', names: { en: 'Gluten free' }, colour: '#0ea5e9', text_colour: null, icon: 'wheat-off' }, DEFAULT_BADGES[1]]
assert.deepEqual(itemBadges({ badges: ['chef', 'gf', 'gone'] }, mine).map(d => d.key), ['chef', 'gf'])
assert.deepEqual(itemBadges({ badge: 'new' }, null).map(d => d.key), ['new']) // stored before: one badge; no list yet: the defaults
assert.deepEqual(badgeColours(mine[0]), { background: '#0ea5e9', color: '#111111' })
assert.deepEqual(badgeColours(mine[0], { badge_colour: '#1e3a8a' }), { background: '#1e3a8a', color: '#ffffff' })
assert.deepEqual(badgeColours(mine[0], { badge_text_colour: '#fde68a' }), { background: '#0ea5e9', color: '#fde68a' })
assert.equal(badgeName(mine[0], 'el'), 'Gluten free')
assert.equal(badgeName(DEFAULT_BADGES[1], 'el'), 'Πρόταση σεφ')
})
test('container units: written exactly as before where the browser has them (older TVs, 2026-11-21)', async () => {
const { cq, CQ_BOX, CQ_NATIVE } = await import('../src/cq.js')
const { fitUnit } = await import('../src/logic.js')
assert.equal(CQ_NATIVE, true)
assert.equal(cq(3), '3cqmin')
assert.equal(cq(4.5, 'h'), '4.5cqh')
assert.equal(cq(0.25, 'w'), '0.25cqw')
assert.deepEqual(CQ_BOX, {})
assert.equal(fitUnit(4, 20)(2), 'calc(2 * min(25cqh, 5cqw))')
})
test('container units: an older browser gets calc() of the variables the compat layer sets', async () => {
globalThis.CSS = { supports: () => false }
try {
const { cq, CQ_BOX, CQ_NATIVE } = await import('../src/cq.js?older')
assert.equal(CQ_NATIVE, false)
assert.equal(cq(3), 'calc(3 * var(--xcqmin, 1vmin))')
assert.equal(cq(4.5, 'h'), 'calc(4.5 * var(--xcqh, 1vh))')
assert.deepEqual(CQ_BOX, { 'data-cq': '' })
} finally { delete globalThis.CSS }
})
test('slide shapes: custom ratios, the canvas, the box a slide keeps on any screen (6R.13)', async () => {
const { canvasOf, fitBox, parseAspect, pictureArea, presetInsets, shapeName, shapeOf, shapeRatio } = await import('../src/logic.js')
assert.deepEqual(parseAspect('21:9'), [21, 9])
assert.equal(parseAspect('9:1'), null)
assert.equal(parseAspect('16x9'), null)
assert.equal(shapeOf({ orientation: 'portrait' }), 'portrait')
assert.equal(shapeOf({ orientation: 'landscape', aspect: '21:9' }), '21:9')
assert.equal(shapeOf({ orientation: 'landscape', aspect: 'nope' }), 'landscape')
assert.equal(shapeRatio('landscape'), 16 / 9)
assert.deepEqual(canvasOf('landscape'), { w: 1920, h: 1080 })
assert.deepEqual(canvasOf('portrait'), { w: 1080, h: 1920 })
assert.deepEqual(canvasOf('21:9'), { w: 1920, h: 823 })
// a 16:9 slide on a 21:9 monitor: full height, bands left and right
const b = fitBox(2560, 1080, 16 / 9)
assert.deepEqual([Math.round(b.left), b.top, Math.round(b.width), b.height], [320, 0, 1920, 1080])
// on 16:10: full width, bands above and below
const c = fitBox(1920, 1200, 16 / 9)
assert.deepEqual([c.left, c.top, c.width, c.height], [0, 60, 1920, 1080])
// calibration: the window minus the insets; the presets centre a shape
assert.deepEqual(pictureArea({ w: 1920, h: 1080 }, { left: 10, top: 0, right: 30, bottom: 20 }), { left: 10, top: 0, width: 1880, height: 1060 })
assert.deepEqual(presetInsets({ w: 2560, h: 1080 }, '16:9'), { left: 320, top: 0, right: 320, bottom: 0 })
assert.deepEqual(presetInsets({ w: 1920, h: 1080 }, 'full'), { left: 0, top: 0, right: 0, bottom: 0 })
assert.equal(shapeName(2560, 1080), '21:9')
assert.equal(shapeName(1920, 1200), '16:10')
assert.equal(shapeName(1000, 300), '10:3')
})
test('a Wi-Fi QR code: escaped, open networks without a password, hidden ones (6R.14)', async () => {
const { wifiQr } = await import('../src/logic.js')
assert.equal(wifiQr({ ssid: 'Xenia Guests', password: 'a;b:c', security: 'WPA' }), 'WIFI:T:WPA;S:Xenia Guests;P:a\;b\:c;;')
assert.equal(wifiQr({ ssid: 'Open', password: 'x', security: 'nopass', hidden: true }), 'WIFI:T:nopass;S:Open;H:true;;')
assert.equal(wifiQr({ password: 'x' }), '')
})