Files
xenia-pos-local/terminal/tests/terminal.test.mjs
T
bonaminandClaude Opus 5.5 c24cb72640 feat(terminal): pairing, sign-in per mode, device app router and live states (platform-v2 3.1c)
- Pairing screen: requests a code, shows it big, polls with the secret, new code on
  expiry, keeps the device token; "removed from the venue" notice after a revoke.
- Sign-in per device mode: PIN (name + PinPad), tap your name, password (name +
  password), none (straight into the app). Names = holders of <app>.access, refreshed
  every 30 s and on reconnect. Slim bar with screen, user and Sign out; idle sign-out
  after the device's auto_sign_out_minutes; hourly token refresh (same session).
- DeviceProvider: GET /api/devices/me (retried while unreachable), session, the live
  connection with credentials in the first frame (device.updated -> reload, app moved ->
  sign out, device.revoked -> forget, session.ended -> sign out), offline banner;
  useLiveEvent for apps. Screen choice is pure (device/flow.js, unit-tested).
- Blocked screen: app turned off / not licensed / device refused, recovers by itself.
- @xenia/access AccessProvider: autoRetryMs (retry bootstrap while unreachable).
- Tests: terminal unit tests (screen choice, live actions, idle, pairing polls), new
  tests/e2e/terminal.mjs (18 checks), manager.mjs owner flow now pairs and signs in on
  the real terminal screen; run-all runs terminal.mjs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 17:29:18 +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 + menu tablet in phase 5)', () => {
assert.deepEqual(Object.keys(APPS).sort(), ['customer_display', 'kds', 'pos_counter'])
assert.equal(typeof appLoader('kds'), 'function')
assert.equal(appLoader('signage'), 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' })
})