// Pure logic of the platform pages (plan steps 2.6c, 2.6d). import assert from 'node:assert/strict' import { test } from 'node:test' import { deviceStatus, deviceUsage, hasRoom, linkTargets, normalizeCode } from '../src/pages/platform/devicesLogic.js' import { moduleState, stoppedBy } from '../src/pages/platform/modulesLogic.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]) })