Files
xenia-pos-local/manager_dashboard/tests/platform.test.mjs
T
bonaminandClaude Opus 5.5 d5c4400fde feat(backend): KDS settings as device config, zone enforced for kitchen screens, KDS-only API (platform-v2 3.2a)
- KDS manifest: 23 typed settings (prep zone, board, waiting-time minutes and colours,
  behaviour) replacing the KDS page's localStorage settings.
- ConfigField: colour type, "after" ordering (warning < late < flash), groups for the
  manager's dialogs, ".none" text for an empty ref choice.
- routers/kds.py: a KDS device gets only its zone's orders, prints to its zone by default
  and signs waiter messages with its zone/name. New GET /api/kds/context and
  POST /api/kds/orders/{id}/retry-print so the screen never leaves its kds.* scope.
  Unused v1 /items and /items/{id}/status endpoints removed (D-21).
- PATCH /api/devices/me/config: a screen changes its own settings when the signed-in
  person holds core.devices.manage.
- Manager Devices dialogs: settings in sections, colour pickers, "All zones".
- Tests: test_kds_device.py, colour/ordered fields; terminal.mjs scope + zone checks;
  manager.mjs settings sections; stack.mjs detaches the non-LAN network before the
  restart check (one flaky 403).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 18:56:59 +02:00

147 lines
8.5 KiB
JavaScript

// Pure logic of the platform pages (plan steps 2.6c, 2.6d, 2.6e).
import assert from 'node:assert/strict'
import { test } from 'node:test'
import { configGroups, configInitial, configValue, deviceStatus, deviceUsage, hasRoom, linkTargets, normalizeCode, refOptions } from '../src/pages/platform/devicesLogic.js'
import { moduleState, stoppedBy } from '../src/pages/platform/modulesLogic.js'
import {
approvalState, approvalTarget, canEndSession, canGive, diff, differsFromRole, editorGroups, permLabelKey, resetToRole,
} from '../src/pages/staff/permissionsLogic.js'
const mod = (key, extra = {}) => ({ key, depends: [], can_disable: true, entitled: true, enabled: true, effective: true, limits: [], device_app: null, ...extra })
test('module state', () => {
assert.equal(moduleState(mod('core', { can_disable: false })), 'always')
assert.equal(moduleState(mod('kds', { entitled: false, effective: false })), 'not_licensed')
assert.equal(moduleState(mod('kds')), 'on')
assert.equal(moduleState(mod('kds', { effective: false })), 'waiting')
assert.equal(moduleState(mod('kds', { enabled: false, effective: false })), 'off')
})
test('turning a module off stops what depends on it, directly or not (only those that are on)', () => {
const modules = [
mod('catalog'), mod('ordering', { depends: ['catalog'] }), mod('kds', { depends: ['ordering'] }),
mod('pos_counter', { depends: ['ordering'] }), mod('customer_display', { depends: ['pos_counter'] }),
mod('pricing', { depends: ['ordering'], effective: false }), mod('notes'),
]
assert.deepEqual(stoppedBy('ordering', modules).map(m => m.key).sort(), ['customer_display', 'kds', 'pos_counter'])
assert.deepEqual(stoppedBy('notes', modules), [])
})
test('device status', () => {
const now = Date.parse('2026-11-13T12:00:00Z')
const d = { claimed: true, revoked_at: null, last_seen_at: '2026-11-13T11:58:30Z' }
assert.equal(deviceStatus(d, { now }), 'online')
assert.equal(deviceStatus({ ...d, last_seen_at: '2026-11-13T11:50:00Z' }, { now }), 'offline')
assert.equal(deviceStatus({ ...d, last_seen_at: null }, { now }), 'offline')
assert.equal(deviceStatus({ ...d, claimed: false }, { now }), 'waiting')
assert.equal(deviceStatus(d, { now, appOn: false }), 'app_off')
assert.equal(deviceStatus({ ...d, revoked_at: '2026-11-13T11:00:00Z' }, { now }), 'removed')
})
test('pairing codes as typed', () => {
assert.equal(normalizeCode(' ab-c d2e '), 'ABCD2E')
assert.equal(normalizeCode('k7m9pq2x'), 'K7M9PQ')
assert.equal(normalizeCode(null), '')
})
test('device usage against the license limit', () => {
const modules = [
mod('kds', { device_app: {}, limits: [{ key: 'devices', value: 2 }] }),
mod('signage', { device_app: {}, limits: [{ key: 'devices', value: null }] }),
mod('notes'),
]
const devices = [
{ app_key: 'kds', revoked_at: null }, { app_key: 'kds', revoked_at: '2026-01-01T00:00:00Z' },
{ app_key: 'kds', revoked_at: null }, { app_key: 'signage', revoked_at: null },
]
const usage = deviceUsage(devices, modules)
assert.deepEqual(usage, { kds: { used: 2, max: 2 }, signage: { used: 1, max: null } })
assert.equal(hasRoom(usage, 'kds'), false)
assert.equal(hasRoom(usage, 'kds', { moving: true }), true)
assert.equal(hasRoom(usage, 'signage'), true)
})
test('link targets: not itself, not removed', () => {
const devices = [{ id: 1, revoked_at: null }, { id: 2, revoked_at: 'x' }, { id: 3, revoked_at: null }]
assert.deepEqual(linkTargets(devices, 1).map(d => d.id), [3])
})
// ── 2.6e ────────────────────────────────────────────────────────────────────
test('permission label keys follow the dots of the key under the module', () => {
assert.equal(permLabelKey('ordering.orders.cancel'), 'modules:ordering.permissions.orders.cancel')
assert.equal(permLabelKey('kds.access'), 'modules:kds.permissions.access')
})
test('editor groups: licensed modules only (core, on, off), keys no role has only for a superadmin', () => {
const catalog = [
{ key: 'core', permissions: [{ key: 'core.staff.manage', roles: ['owner'] }, { key: 'core.system.admin', roles: [] }] },
{ key: 'kds', permissions: [{ key: 'kds.access', roles: ['chef'] }] },
{ key: 'tabs', permissions: [{ key: 'tabs.manage', roles: ['owner'] }] },
]
catalog.unshift({ key: 'chat', permissions: [{ key: 'chat.use', roles: ['waiter'] }] })
const modules = [mod('chat', { effective: false }), mod('core'), mod('kds'), mod('tabs', { entitled: false, effective: false })]
const g = editorGroups(catalog, modules)
assert.deepEqual(g.map(x => [x.key, x.on, x.permissions.map(p => p.key)]), [['core', true, ['core.staff.manage']], ['kds', true, ['kds.access']], ['chat', false, ['chat.use']]])
assert.deepEqual(editorGroups(catalog, modules, { actorIsSuperadmin: true })[0].permissions.map(p => p.key), ['core.staff.manage', 'core.system.admin'])
})
test('giving: only what you hold or what they already had; reset skips the rest', () => {
const original = new Set(['kds.access'])
const mine = new Set(['kds.bump'])
assert.equal(canGive('kds.bump', original, mine), true)
assert.equal(canGive('kds.access', original, mine), true) // not new
assert.equal(canGive('reports.view', original, mine), false)
assert.equal(canGive('reports.view', original, null), true) // superadmin
const r = resetToRole(['kds.access', 'kds.bump', 'reports.view'], original, mine)
assert.deepEqual([...r.keys].sort(), ['kds.access', 'kds.bump'])
assert.deepEqual(r.skipped, ['reports.view'])
assert.deepEqual(diff(new Set(['kds.bump']), original), { added: ['kds.bump'], removed: ['kds.access'] })
assert.equal(differsFromRole(['a', 'b'], ['b', 'a']), false)
assert.equal(differsFromRole(['a'], ['a', 'b']), true)
})
test('approvals and sessions', () => {
const now = Date.parse('2026-11-13T12:00:00Z')
assert.equal(approvalState({ used_at: '2026-11-13T11:59:00Z', expires_at: '2026-11-13T11:59:30Z' }, { now }), 'used')
assert.equal(approvalState({ used_at: null, expires_at: '2026-11-13T11:59:30Z' }, { now }), 'expired')
assert.equal(approvalState({ used_at: null, expires_at: '2026-11-13T12:00:30Z' }, { now }), 'waiting')
assert.deepEqual(approvalTarget('order:12'), { order: 12 })
assert.deepEqual(approvalTarget('DELETE /api/x/3'), { text: 'DELETE /api/x/3' })
assert.equal(approvalTarget(null), null)
const s = { client: 'waiter', current: false, ended_at: null }
assert.equal(canEndSession(s), true)
assert.equal(canEndSession({ ...s, current: true }), false)
assert.equal(canEndSession({ ...s, ended_at: 'x' }), false)
assert.equal(canEndSession({ ...s, client: 'support' }), false)
assert.equal(canEndSession({ ...s, client: 'support' }, { actorIsSuperadmin: true }), true)
})
test('device settings: starting values, typed values, ref options (3.1a)', () => {
const fields = [
{ key: 'auto_sign_out_minutes', type: 'int', default: 0, choices: [0, 5] },
{ key: 'zone', type: 'ref', default: null, choices_from: 'prep_zones' },
{ key: 'pos', type: 'ref', default: null, choices_from: 'devices:pos_counter' },
{ key: 'sound', type: 'bool', default: true },
]
assert.deepEqual(configInitial(fields), { auto_sign_out_minutes: 0, zone: null, pos: null, sound: true })
assert.deepEqual(configInitial(fields, { auto_sign_out_minutes: 5, sound: false }), { auto_sign_out_minutes: 5, zone: null, pos: null, sound: false })
assert.equal(configValue(fields[0], '5'), 5)
assert.equal(configValue(fields[1], ''), null)
assert.equal(configValue(fields[1], '3'), 3)
assert.equal(configValue(fields[3], false), false)
assert.deepEqual(refOptions(fields[1], { zones: [{ id: 2, name: 'Grill' }] }), [{ value: '2', label: 'Grill' }])
const devices = [{ id: 1, app_key: 'pos_counter', name: 'Till' }, { id: 2, app_key: 'kds', name: 'KDS' },
{ id: 3, app_key: 'pos_counter', name: 'Old', revoked_at: 'x' }]
assert.deepEqual(refOptions(fields[2], { devices }), [{ value: '1', label: 'Till' }])
assert.deepEqual(refOptions(fields[2], { devices, selfId: 1 }), [])
})
test('config fields in sections, in the order their groups first appear (3.2a)', () => {
const f = (key, group) => ({ key, type: 'bool', default: false, group })
const groups = configGroups([f('zone', 'zone'), f('w', 'board'), f('c', 'age'), f('s', 'board'), f('auto', undefined)])
assert.deepEqual(groups.map(g => [g.group, g.fields.map(x => x.key)]),
[['zone', ['zone']], ['board', ['w', 's']], ['age', ['c']], [null, ['auto']]])
assert.equal(configValue({ type: 'color' }, '#aabbcc'), '#aabbcc')
})