terminal/src/apps/menu-tablet: the venue's menu for staff (a quick price
check) and guests. Category chips that jump to their section, or one A–Z
list; compact rows, expanded rows (photo, description) or full cards; tap a
product to see it big (photo, price, description, tags); search that ignores
accents and also finds names typed in Latin letters ("souvl"); sold out
marked; the guest's language for the tablet's texts; back to the start (search
cleared, first language, top) after the set time without a touch; light or
dark; the last menu kept for offline; live on menu_tablet.changed /
device.updated. @xenia/i18n exports ./latin for plain-JS use.
tests: logic (3), e2e menu_tablet.mjs (10 checks, in run-all).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
88 lines
4.7 KiB
JavaScript
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, menu tablet', () => {
|
|
assert.deepEqual(Object.keys(APPS).sort(), ['customer_display', 'kds', 'menu_tablet', 'pos_counter', 'signage'])
|
|
assert.equal(typeof appLoader('kds'), 'function')
|
|
assert.equal(appLoader('waiter'), 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' })
|
|
})
|