- order_items.claimed_by / claimed_at (Alembic 0015). POST/DELETE
/api/kds/orders/{id}/items/{item}/claim (kds.bump): someone else's claim is refused
(kds.item_claimed), letting go works for anyone with kds.bump; live kds_item_claimed.
In progress claims for whoever did it; done/served/pending/declined clear the claim.
- Fix: order-level status and decline from a zone screen changed every zone's items;
now only the screen's zone, and a zone screen sees the order status of its own items.
- Terminal: "I'm on it" / "Let go" on the selected card, the cook's name on claimed items.
- Tests: backend +6, terminal unit +1, terminal.mjs +6 with a second screen and cook.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
201 lines
12 KiB
JavaScript
201 lines
12 KiB
JavaScript
// The kitchen board's pure logic (plan step 3.2b): layout, age, buckets, rounds, totals,
|
|
// status changes and settings.
|
|
import assert from 'node:assert/strict'
|
|
import { test } from 'node:test'
|
|
import {
|
|
ageLabel, ageOrderFor, ageState, bucketCounts, buildItemGroups, courseGroups, dedupeModifiers, formatQty, nextItemStatus,
|
|
orderStatusFromItems, readyNotice, tabCounts, visibleOrders, withBatches, withItemStatus, withOrderStatus,
|
|
} from '../src/apps/kds/logic/board.js'
|
|
import { BOARD_PAD, COL_GAP, cardChrome, distribute, layoutBoard, packBoard, sizeCard } from '../src/apps/kds/logic/layout.js'
|
|
import { DEFAULT_CONFIG, boardSettings, senderName, zoomStep } from '../src/apps/kds/logic/settings.js'
|
|
|
|
const T0 = Date.parse('2026-11-14T12:00:00Z')
|
|
const at = min => new Date(T0 + min * 60_000).toISOString().replace('.000Z', '') // backend style: UTC, no offset
|
|
const thr = { warning: 5, late: 10, flash: 15 }
|
|
const t = (key, o = {}) => `${key}${Object.keys(o).length ? JSON.stringify(o) : ''}`
|
|
|
|
// ── layout ──────────────────────────────────────────────────────────────────
|
|
|
|
test('distribute: items into columns of at most the given height', () => {
|
|
assert.deepEqual(distribute([10, 10, 10], 4, 100), [[0, 1, 2]])
|
|
assert.deepEqual(distribute([50, 50, 50], 4, 100), [[0], [1], [2]])
|
|
assert.deepEqual(distribute([40, 40, 40], 4, 90), [[0, 1], [2]])
|
|
assert.deepEqual(distribute([], 4, 90), [[]])
|
|
})
|
|
|
|
test('sizeCard: one column when it fits, wider until it fits, capped at 3', () => {
|
|
const chrome = 100
|
|
const one = sizeCard({ blocks: [30, 30], blockGap: 4, chrome, maxH: 400, base: 272 })
|
|
assert.equal(one.units, 1)
|
|
assert.equal(one.cardH, 164)
|
|
assert.equal(one.cardW, 272)
|
|
const two = sizeCard({ blocks: Array(6).fill(50), blockGap: 4, chrome, maxH: 400, base: 272 })
|
|
assert.equal(two.units, 2)
|
|
assert.ok(two.cardH <= 400)
|
|
assert.equal(two.cardW, 2 * 272 + COL_GAP)
|
|
const huge = sizeCard({ blocks: Array(40).fill(80), blockGap: 4, chrome, maxH: 400, base: 272 })
|
|
assert.equal(huge.units, 3)
|
|
assert.equal(huge.cardH, 400) // the only case that overflows
|
|
})
|
|
|
|
test('packBoard: cards stack down a column, then a new column to the right', () => {
|
|
const cards = [{ id: 'a', units: 1, cardW: 272, cardH: 200 }, { id: 'b', units: 1, cardW: 272, cardH: 150 },
|
|
{ id: 'c', units: 1, cardW: 272, cardH: 200 }, { id: 'd', units: 2, cardW: 558, cardH: 100 }]
|
|
const { positions, totalW } = packBoard(cards, 272, 400)
|
|
assert.deepEqual(positions.a, { x: 0, y: 0 })
|
|
assert.deepEqual(positions.b, { x: 0, y: 214 })
|
|
assert.deepEqual(positions.c, { x: 286, y: 0 })
|
|
assert.deepEqual(positions.d, { x: 572, y: 0 }) // two tracks wide: no room under a+b / c, so a new column
|
|
assert.equal(totalW, 572 + 558)
|
|
})
|
|
|
|
test('cardChrome scales with the text size; layoutBoard places every card', () => {
|
|
assert.equal(cardChrome(1, false, false), 46 + 28 + 18)
|
|
assert.equal(cardChrome(2, true, true), 92 + 56 + 64 + 70 + 18)
|
|
const orders = [{ id: 1, items: [{ id: 1 }, { id: 2 }], waiters: ['maria'] }, { id: 2, items: [{ id: 3 }] }]
|
|
const out = layoutBoard({ orders, measured: { 1: { blocks: [30, 40] } }, boardH: 600, colW: 272, fontScale: 1, batchMode: 'merged' })
|
|
assert.deepEqual(Object.keys(out.map), ['1', '2'])
|
|
assert.equal(out.map[1].x, BOARD_PAD)
|
|
assert.equal(out.map[1].h, cardChrome(1, true, false) + 30 + 4 + 40)
|
|
assert.deepEqual(layoutBoard({ orders, measured: {}, boardH: 0, colW: 272, fontScale: 1 }), { map: {}, totalW: BOARD_PAD * 2 })
|
|
})
|
|
|
|
// ── age ─────────────────────────────────────────────────────────────────────
|
|
|
|
test('ageState: new → warning → late → flash; done counts since done; future counts down', () => {
|
|
const now = T0
|
|
const o = (min, extra = {}) => ({ kds_status: 'pending', opened_at: at(-min), ...extra })
|
|
assert.deepEqual(ageState(o(0), now, thr), { age: 'green', flash: false, minutes: 0, kind: 'new' })
|
|
assert.equal(ageState(o(4), now, thr).age, 'green')
|
|
assert.equal(ageState(o(5), now, thr).age, 'amber')
|
|
assert.equal(ageState(o(10), now, thr).age, 'red')
|
|
assert.deepEqual(ageState(o(15), now, thr), { age: 'red', flash: true, minutes: 15, kind: 'elapsed' })
|
|
assert.deepEqual(ageState(o(-20), now, thr), { age: 'future', flash: false, minutes: 20, kind: 'until' })
|
|
assert.deepEqual(ageState(o(30, { kds_status: 'done', closed_at: at(-3) }), now, thr), { age: 'done', flash: false, minutes: 3, kind: 'ago' })
|
|
// a timestamp with Z is the same moment
|
|
assert.equal(ageState({ kds_status: 'pending', opened_at: `${at(-6)}Z` }, now, thr).age, 'amber')
|
|
})
|
|
|
|
test('ageLabel / durationLabel', () => {
|
|
assert.equal(ageLabel({ kind: 'new', minutes: 0 }, t), 'kds.age.new')
|
|
assert.equal(ageLabel({ kind: 'elapsed', minutes: 7 }, t), 'kds.age.minutes{"m":7}')
|
|
assert.equal(ageLabel({ kind: 'elapsed', minutes: 65 }, t), 'kds.age.hours{"h":1,"m":5}')
|
|
assert.equal(ageLabel({ kind: 'ago', minutes: 3 }, t), 'kds.age.ago{"time":"kds.age.minutes{\\"m\\":3}"}')
|
|
})
|
|
|
|
// ── buckets, tabs, rounds ───────────────────────────────────────────────────
|
|
|
|
const orders = [
|
|
{ id: 1, kds_status: 'pending', order_type: 'here', opened_at: at(-10), items: [] },
|
|
{ id: 2, kds_status: 'pending', order_type: 'takeaway', opened_at: at(30), items: [] }, // scheduled
|
|
{ id: 3, kds_status: 'preparing', order_type: 'here', opened_at: at(-20), items: [] },
|
|
{ id: 4, kds_status: 'done', order_type: 'delivery', opened_at: at(-40), items: [] },
|
|
]
|
|
|
|
test('buckets and tab counts', () => {
|
|
assert.deepEqual(bucketCounts(orders, T0), { future: 1, pending: 1, preparing: 1, done: 1 })
|
|
assert.deepEqual(tabCounts(orders, new Set(['pending', 'preparing']), T0), { all: 2, here: 2, takeaway: 0, delivery: 0 })
|
|
})
|
|
|
|
test('visibleOrders: filters, tab, oldest first, zone sorting', () => {
|
|
const v = visibleOrders(orders, { filters: new Set(['future', 'pending', 'preparing']), now: T0 })
|
|
assert.deepEqual(v.map(o => o.id), [3, 1, 2])
|
|
assert.deepEqual(visibleOrders(orders, { filters: new Set(['pending', 'preparing']), tab: 'here', now: T0 }).map(o => o.id), [3, 1])
|
|
const one = [{ id: 9, kds_status: 'pending', opened_at: at(-1), order_type: 'here',
|
|
items: [{ id: 1, product_name: 'Beta', quantity: 1, added_at: at(-1) }, { id: 2, product_name: 'alpha', quantity: 3, added_at: at(-1) }] }]
|
|
assert.deepEqual(visibleOrders(one, { filters: new Set(['pending']), now: T0, sortBy: 'alpha', locale: 'en' })[0].items.map(i => i.id), [2, 1])
|
|
assert.deepEqual(visibleOrders(one, { filters: new Set(['pending']), now: T0, sortBy: 'item_count' })[0].items.map(i => i.id), [2, 1])
|
|
})
|
|
|
|
test('rounds: items more than 2 minutes apart; split = one card per round', () => {
|
|
const o = { id: 7, kds_status: 'pending', order_type: 'here', opened_at: at(-20), items: [
|
|
{ id: 1, added_at: at(-20), kds_status: 'done' }, { id: 2, added_at: at(-19), kds_status: 'done' }, { id: 3, added_at: at(-5), kds_status: 'pending' }] }
|
|
const b = withBatches(o)
|
|
assert.equal(b._batches.length, 2)
|
|
assert.deepEqual(b.items.map(i => i._batchIndex), [0, 0, 1])
|
|
assert.equal(ageOrderFor(b).opened_at, at(-5)) // the clock runs from the round still being made
|
|
const split = visibleOrders([o], { filters: new Set(['pending']), now: T0, batchMode: 'split' })
|
|
assert.deepEqual(split.map(x => [x.id, x._realId, x.items.length]), [['7_b0', 7, 2], ['7_b1', 7, 1]])
|
|
})
|
|
|
|
test('courses: in the venue order, no course last', () => {
|
|
const items = [{ id: 1, course_id: 2 }, { id: 2, course_id: null }, { id: 3, course_id: 1 }]
|
|
const g = courseGroups(items, [{ id: 1, name: 'Starters' }, { id: 2, name: 'Mains' }])
|
|
assert.deepEqual(g.map(x => [x.course?.name ?? null, x.items.map(i => i.id)]), [['Starters', [3]], ['Mains', [1]], [null, [2]]])
|
|
})
|
|
|
|
test('item totals: same product + same changes count together; done items left out', () => {
|
|
const g = buildItemGroups([
|
|
{ items: [{ product_name: 'Souvlaki', quantity: 2, removed: ['onion'], kds_status: 'pending' },
|
|
{ product_name: 'Souvlaki', quantity: 1, removed: ['onion'], kds_status: 'preparing' },
|
|
{ product_name: 'Souvlaki', quantity: 1, kds_status: 'pending' }] },
|
|
{ items: [{ product_name: 'Souvlaki', quantity: 5, kds_status: 'done' }, { product_name: 'Fries', quantity: 1, kds_status: 'declined' }] },
|
|
])
|
|
assert.deepEqual(g.map(x => [x.product_name, x.removed, x.count]), [['Souvlaki', ['onion'], 3], ['Souvlaki', [], 1]])
|
|
assert.deepEqual(dedupeModifiers(['sauce', 'sauce', 'salt']), ['2x sauce', 'salt'])
|
|
assert.equal(formatQty(0.5, 'kg'), '0.5kg')
|
|
assert.equal(formatQty(2, 'piece'), '2')
|
|
})
|
|
|
|
// ── status changes ──────────────────────────────────────────────────────────
|
|
|
|
test('a tap on an item: to do → done (→ in progress first with three steps) → to do', () => {
|
|
assert.equal(nextItemStatus('pending', false), 'done')
|
|
assert.equal(nextItemStatus('pending', true), 'preparing')
|
|
assert.equal(nextItemStatus('preparing', true), 'done')
|
|
assert.equal(nextItemStatus('done', false), 'pending')
|
|
})
|
|
|
|
test('order status follows its items (as the backend does)', () => {
|
|
const it = (...s) => s.map((kds_status, id) => ({ id, kds_status }))
|
|
assert.equal(orderStatusFromItems(it('done', 'served'), 'x'), 'done')
|
|
assert.equal(orderStatusFromItems(it('pending', 'done'), 'x'), 'preparing')
|
|
assert.equal(orderStatusFromItems(it('pending'), 'x'), 'pending')
|
|
assert.equal(orderStatusFromItems([], 'x'), 'x')
|
|
const o = { id: 1, kds_status: 'pending', items: it('pending', 'served').concat({ id: 9, kds_status: 'pending', status: 'cancelled' }) }
|
|
assert.deepEqual(withOrderStatus(o, 'done').items.map(i => i.kds_status), ['done', 'served', 'pending'])
|
|
assert.equal(withItemStatus(o, 0, 'done').kds_status, 'done')
|
|
})
|
|
|
|
test('tell the waiter: the whole order, or how many items are ready', () => {
|
|
const o = { items: [{ id: 1, kds_status: 'done' }, { id: 2, kds_status: 'pending' }, { id: 3, kds_status: 'pending' }] }
|
|
assert.deepEqual(readyNotice(o, 2, { notifyItemComplete: true }), { kind: 'items', ready_count: 2, total_count: 3 })
|
|
assert.equal(readyNotice(o, 2, { notifyOrderComplete: true }), null)
|
|
const last = { items: [{ id: 1, kds_status: 'done' }, { id: 2, kds_status: 'pending' }] }
|
|
assert.deepEqual(readyNotice(last, 2, { notifyOrderComplete: true }), { kind: 'order' })
|
|
})
|
|
|
|
// ── settings ────────────────────────────────────────────────────────────────
|
|
|
|
test('device config → board settings; zoom overrides the size for the session', () => {
|
|
const s = boardSettings({ ...DEFAULT_CONFIG, font_scale: 120, age_late_minutes: 12, colour_late: '#ff0000' })
|
|
assert.equal(s.fontScale, 1.2)
|
|
assert.deepEqual(s.thresholds, { warning: 5, late: 12, flash: 15 })
|
|
assert.equal(s.colours.late, '#ff0000')
|
|
assert.equal(boardSettings(null).colW, 272)
|
|
assert.equal(boardSettings({ font_scale: 120 }, 90).fontScale, 0.9)
|
|
assert.equal(zoomStep(100, 1), 110)
|
|
assert.equal(zoomStep(60, -1), 60)
|
|
assert.equal(zoomStep(200, 1), 200)
|
|
assert.equal(senderName({ zone: { name: 'Grill', notification_name: 'Grill station' }, device: { name: 'Screen' } }), 'Grill station')
|
|
assert.equal(senderName({ zone: null, device: { name: 'Screen' } }), 'Screen')
|
|
})
|
|
|
|
test('claims: take what nobody has, let go of mine, nothing when others have it all (3.2c)', async () => {
|
|
const { claimState, claimTargets } = await import('../src/apps/kds/logic/board.js')
|
|
const items = [
|
|
{ id: 1, kds_status: 'pending', claimed_by: null },
|
|
{ id: 2, kds_status: 'preparing', claimed_by: 7 },
|
|
{ id: 3, kds_status: 'done', claimed_by: null },
|
|
{ id: 4, kds_status: 'pending', claimed_by: 9 },
|
|
]
|
|
assert.equal(claimState(items, 7), 'take')
|
|
assert.deepEqual(claimTargets(items, 7, 'take').map(i => i.id), [1])
|
|
const mine = items.map(i => (i.id === 1 ? { ...i, claimed_by: 7 } : i))
|
|
assert.equal(claimState(mine, 7), 'mine')
|
|
assert.deepEqual(claimTargets(mine, 7, 'release').map(i => i.id), [1, 2])
|
|
assert.equal(claimState(mine.map(i => ({ ...i, claimed_by: i.claimed_by && 9 })), 7), 'none')
|
|
assert.equal(claimState([{ id: 1, kds_status: 'done' }], 7), 'none')
|
|
assert.equal(claimState(items, null), 'none')
|
|
})
|