// The staff page and the weekly schedule as plain data (plan step 6.11). import { test } from 'node:test' import assert from 'node:assert/strict' import { customKeys, filterStaff, groupCounts, groupOf, staffBody, staffForm, staffProblems, validPin, zonesBody, zonesOf, zonesSummary, } from '../src/pages/staff/staffLogic.js' import { addDays, clashWith, gridOf, hourMarks, hoursOf, hoursValid, inOpening, inSpans, loadView, overlaps, placeOnDay, saveView, shiftState, weekDays, weekStartOf, weekTotals, } from '../src/pages/schedule/scheduleLogic.js' const staff = [ { id: 1, full_name: 'Μαρία Κωνσταντίνου', nickname: 'Μαρία', username: 'maria', role: 'waiter', is_active: true, mobile_phone: '6901' }, { id: 2, full_name: 'Nikos P', username: 'nikos', role: 'chef', is_active: false }, { id: 3, full_name: 'Anna', username: 'anna', role: 'owner', is_active: true }, ] test('groups, filters (role, status, search without accents), active first', () => { assert.equal(groupOf('barman'), 'kitchen') assert.equal(groupOf('custom'), 'other') assert.deepEqual(groupCounts(staff), { management: 1, kitchen: 1, service: 1, other: 0 }) assert.deepEqual(filterStaff(staff).map(m => m.id).at(-1), 2) // blocked last assert.deepEqual(filterStaff(staff, { q: 'μαρια' }).map(m => m.id), [1]) assert.deepEqual(filterStaff(staff, { q: '6901' }).map(m => m.id), [1]) assert.deepEqual(filterStaff(staff, { status: 'blocked' }).map(m => m.id), [2]) assert.deepEqual(filterStaff(staff, { role: 'owner' }).map(m => m.id), [3]) }) test('the form: what is wrong for a new person and an edit, and the bodies', () => { const f = { ...staffForm(null), full_name: ' Eleni ', nickname: 'Eleni', username: 'el eni', pin: '12', password: 'abc' } assert.deepEqual(staffProblems(f, true), ['username', 'pin', 'password']) assert.deepEqual(staffProblems({ ...f, username: 'eleni', pin: '1234', password: '' }, true), []) assert.deepEqual(staffProblems({ ...f, username: 'eleni', hourly_rate: 'x' }, false), ['hourly_rate']) assert.equal(validPin('123456'), true) assert.equal(validPin('12345a'), false) const body = staffBody({ ...f, username: 'eleni', pin: '1234', password: 'secret1', hourly_rate: '8,5' }, true) assert.equal(body.full_name, 'Eleni') assert.equal(body.hourly_rate, 8.5) assert.equal(body.password, 'secret1') assert.equal(body.language, null) const edit = staffBody(staffForm({ ...staff[0], hourly_rate: 9, language: 'el' }), false) assert.equal('pin' in edit, false) assert.equal(edit.language, 'el') assert.equal(staffBody(staffForm(staff[0]), false).language, '') // '' = as the venue }) test('zones and the "custom keys" mark', () => { const groups = [{ id: 1, name: 'Terrace' }, { id: 2, name: 'Inside' }] assert.deepEqual(zonesOf({ zone_assignments: [{ group_id: null }] }), { all: true, ids: [] }) assert.equal(zonesSummary({ zone_assignments: [] }, groups), 'none') assert.equal(zonesSummary({ zone_assignments: [{ group_id: 2 }, { group_id: 1 }] }, groups), 'Inside, Terrace') assert.deepEqual(zonesBody({ all: true, ids: [1] }), { all_zones: true, group_ids: [] }) assert.equal(customKeys({ role: 'waiter', permissions: ['a', 'b'] }, { waiter: ['b', 'a'] }), false) assert.equal(customKeys({ role: 'waiter', permissions: ['a'] }, { waiter: ['a', 'b'] }), true) }) test('weeks: Monday or Sunday first, crossing months', () => { assert.equal(weekStartOf('2026-11-19', true), '2026-11-16') // a Thursday assert.equal(weekStartOf('2026-11-22', true), '2026-11-16') // Sunday belongs to the week before assert.equal(weekStartOf('2026-11-22', false), '2026-11-22') assert.deepEqual(weekDays('2026-11-30').slice(0, 3), ['2026-11-30', '2026-12-01', '2026-12-02']) assert.equal(addDays('2026-03-28', 2), '2026-03-30') // across the clock change }) test('shifts: hours past midnight, clashes, the grid, states, totals, the timeline', () => { assert.equal(hoursOf('18:00', '02:00'), 8) assert.equal(overlaps('22:00', '02:00', '01:00', '03:00'), false) // same day: 01:00 is the morning assert.equal(overlaps('10:00', '14:00', '13:00', '19:00'), true) const scheduled = [ { id: 1, user_id: 7, scheduled_date: '2026-11-19', start_time: '18:00', end_time: '23:00', duration_hours: 5, estimated_pay: 40 }, { id: 2, user_id: 7, scheduled_date: '2026-11-19', start_time: '09:00', end_time: '13:00', duration_hours: 4, estimated_pay: 32, actual_shifts: [{ is_active: true }] }, ] assert.equal(clashWith(scheduled, { userId: 7, date: '2026-11-19', start: '12:00', end: '15:00' })?.id, 2) assert.equal(clashWith(scheduled, { userId: 7, date: '2026-11-19', start: '12:00', end: '15:00', skipId: 2 }), null) assert.deepEqual(gridOf(scheduled)[7]['2026-11-19'].map(s => s.id), [2, 1]) assert.equal(shiftState(scheduled[0]), 'planned') assert.equal(shiftState(scheduled[1]), 'working') assert.deepEqual(weekTotals(scheduled), { people: { 7: { hours: 9, pay: 72 } }, hours: 9, pay: 72 }) assert.deepEqual(placeOnDay({ start_time: '02:00', end_time: '04:30' }, 6), { top: 20, height: 2.5 }) assert.equal(hourMarks(22)[2], 0) assert.equal(inOpening(23, '18:00', '02:00'), true) assert.equal(inOpening(3, '18:00', '02:00'), false) // the venue's hours, several spans a day (6R.15) assert.equal(inSpans(13, [{ from: '12:00', to: '15:00' }, { from: '18:00', to: '23:00' }]), true) assert.equal(inSpans(16, [{ from: '12:00', to: '15:00' }, { from: '18:00', to: '23:00' }]), false) assert.equal(inSpans(10, undefined), false) assert.equal(hoursValid([[{ from: '12:00', to: '15:00' }], []]), true) assert.equal(hoursValid([[{ from: '12:00', to: '' }]]), false) }) test('the view settings stay in this browser and survive a broken store', () => { const store = new Map() const fake = { getItem: k => store.get(k) ?? null, setItem: (k, v) => store.set(k, v) } saveView({ view: 'hours', mondayFirst: false }, fake) assert.equal(loadView(fake).view, 'hours') assert.equal(loadView(fake).firstHour, 6) assert.equal(loadView({ getItem: () => '{bad' }).view, 'people') })