Files
xenia-pos-local/terminal/tests/terminal.test.mjs
T
bonaminandClaude Opus 5.5 e612e2436c feat(terminal): the signage player: playlists on a TV, offline, transitions, rotation (platform-v2 step 5.5a)
- terminal/src/apps/signage: plays the screen's playlist (or the one its
  schedule picks now, switching by itself) full screen: slides in order with
  their time, a video to its end (a 15 min safety limit), only in their days /
  hours / dates; transitions fade / slide / zoom / none (CSS, reduced motion
  respected); the frame turned 90/180/270 for a TV mounted sideways. Picture,
  video and text slides drawn; a first menu board (5.5b lays it out fully).
- Offline: the last GET /api/signage/screen kept in localStorage, every
  picture and video (up to 120 MB) as an in-memory copy; reads again on
  signage.changed / device.updated / reconnect / every 5 min.
- logic.js: the server's schedule pick ported (same test cases), slide windows,
  next slide, frame; tests/signage.test.mjs (6).
- Signage screens get no offline banner over the slides, a small mark instead
  (registry QUIET_OFFLINE).
- e2e signage.mjs (in run-all): a paired TV plays picture → text → video →
  menu in order, the text's slide transition, rotation, keeps playing with the
  server stopped (mark, no banner), catches up when it is back. terminal.mjs
  passed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 19:40:35 +02:00

88 lines
4.7 KiB
JavaScript

// Pure parts of the terminal shell (plan steps 3.1b, 3.1c).
import assert from 'node:assert/strict'
import { test } from 'node:test'
import { APPS, appLoader } from '../src/apps/registry.js'
import { DEVICE_KEY, SESSION_KEY, createDeviceStore } from '../src/device/storage.js'
import { deviceGone, formatCode, idleExpired, liveAction, pollOutcome, refusedAction, screenFor } from '../src/device/flow.js'
test('the apps a screen can run: KDS, counter POS, customer display, signage (the menu tablet in 5.7)', () => {
assert.deepEqual(Object.keys(APPS).sort(), ['customer_display', 'kds', 'pos_counter', 'signage'])
assert.equal(typeof appLoader('kds'), 'function')
assert.equal(appLoader('menu_tablet'), null)
assert.equal(appLoader('toString'), null) // never a prototype member
})
function memoryStorage() {
const m = new Map()
return { getItem: k => (m.has(k) ? m.get(k) : null), setItem: (k, v) => m.set(k, String(v)), removeItem: k => m.delete(k), m }
}
test('device store keeps the device and session tokens; forget() clears both', () => {
const s = memoryStorage()
const d = createDeviceStore(s)
assert.equal(d.deviceToken(), null)
d.setDeviceToken('dev-1')
d.setSessionToken('jwt-1')
assert.equal(s.m.get(DEVICE_KEY), 'dev-1')
assert.equal(d.sessionToken(), 'jwt-1')
d.setSessionToken(null)
assert.equal(s.m.has(SESSION_KEY), false)
d.setSessionToken('jwt-2')
d.forget()
assert.equal(d.deviceToken(), null)
assert.equal(d.sessionToken(), null)
})
test('device store survives a storage that throws (private mode)', () => {
const broken = { getItem() { throw new Error('denied') }, setItem() { throw new Error('denied') }, removeItem() { throw new Error('denied') } }
const d = createDeviceStore(broken)
d.setDeviceToken('x')
assert.equal(d.deviceToken(), null)
assert.equal(createDeviceStore(null).sessionToken(), null)
})
// ── what a screen shows (3.1c) ──────────────────────────────────────────────
test('screenFor: pairing → loading/blocked → app off → sign in → app', () => {
const me = (extra = {}) => ({ app_key: 'kds', app_status: 'ok', login_mode: 'pin', ...extra })
assert.equal(screenFor({ deviceToken: null }), 'pairing')
assert.equal(screenFor({ deviceToken: 'd' }), 'loading')
assert.equal(screenFor({ deviceToken: 'd', meError: { offline: true } }), 'loading') // keeps trying
assert.equal(screenFor({ deviceToken: 'd', meError: { code: 'license.limit_reached' } }), 'blocked')
assert.equal(screenFor({ deviceToken: 'd', me: me({ app_status: 'module.disabled' }), session: 'x' }), 'app_off')
assert.equal(screenFor({ deviceToken: 'd', me: me() }), 'sign_in')
assert.equal(screenFor({ deviceToken: 'd', me: me(), session: 'jwt' }), 'app')
assert.equal(screenFor({ deviceToken: 'd', me: me({ login_mode: 'none' }) }), 'app') // customer display
})
test('device gone, live events and refusals', () => {
assert.ok(deviceGone('device.revoked') && deviceGone('device.not_paired') && !deviceGone('auth.session_ended'))
assert.equal(liveAction('device.revoked'), 'forget')
assert.equal(liveAction('session.ended', { reason: 'logout' }), 'sign_out')
assert.equal(liveAction('device.updated', { device_id: 1 }), 'reload')
assert.equal(liveAction('device.updated', { device_id: 1, app_key: 'pos_counter' }), 'reload_and_sign_out')
assert.equal(liveAction('order_updated', {}), null)
assert.equal(refusedAction('device.revoked'), 'forget')
assert.equal(refusedAction('device.required'), 'sign_out')
assert.equal(refusedAction('auth.session_ended'), 'sign_out')
assert.equal(refusedAction('license.limit_reached'), null)
})
test('idle sign-out after N minutes; 0 = never', () => {
const t0 = 1_000_000
assert.equal(idleExpired(t0, 5, t0 + 4 * 60_000), false)
assert.equal(idleExpired(t0, 5, t0 + 5 * 60_000), true)
assert.equal(idleExpired(t0, 0, t0 + 999 * 60_000), false)
})
test('pairing: code shown in two groups; poll outcomes', () => {
assert.equal(formatCode('K7M2QX'), 'K7M 2QX')
const now = Date.parse('2026-11-14T12:00:00Z')
const later = '2026-11-14T12:05:00Z'
assert.deepEqual(pollOutcome({ result: { status: 'pending' }, expiresAt: later, now }), { next: 'wait' })
assert.deepEqual(pollOutcome({ result: { status: 'paired', device_token: 'tok' }, now }), { next: 'paired', token: 'tok' })
assert.deepEqual(pollOutcome({ result: { status: 'pending' }, expiresAt: '2026-11-14T11:59:00Z', now }), { next: 'new_code' })
assert.deepEqual(pollOutcome({ error: { code: 'device.pair_code_invalid' }, now }), { next: 'new_code' })
assert.deepEqual(pollOutcome({ error: { code: 'network.offline', offline: true }, now }), { next: 'retry' })
})