// 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 { frameStyle, localNow, matches, mediaUrls, nextSlide, pick, playlistNow, slideMs } from '../src/apps/signage/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 }), []) })