// Customers, throwaways and notes as plain data (plan step 6.13b). import { test } from 'node:test' import assert from 'node:assert/strict' import { customerBody, customerForm, customerLabel, customerProblems, perVisit } from '../src/pages/customers/customersLogic.js' import { wasteProducts, wasteQuantity, wasteTotals } from '../src/pages/inventory/wasteLogic.js' import { splitNotes, splitTodos } from '../src/pages/notes/notesLogic.js' test('a customer: a name needed, a wrong e-mail caught, empty fields cleared', () => { const f = customerForm(null) assert.deepEqual(customerProblems(f), ['name']) assert.deepEqual(customerProblems({ ...f, name: 'Γιώργος', email: 'x@' }), ['email']) assert.deepEqual(customerBody({ ...f, name: ' Γιώργος ', nickname: ' ', phone: '697' }), { name: 'Γιώργος', nickname: null, phone: '697', email: null, notes: null }) assert.equal(customerLabel({ name: 'Kostas', nickname: 'Big K' }), 'Kostas «Big K»') assert.equal(perVisit({ visit_count: 3, total_spent: 50 }), 16.67) assert.equal(perVisit({ visit_count: 0, total_spent: 0 }), null) }) test('throwaways: quantities with a comma, totals, the products offered', () => { assert.equal(wasteQuantity('1,5'), 1.5) assert.equal(wasteQuantity('0'), null) assert.equal(wasteQuantity('x'), null) assert.deepEqual(wasteTotals([{ total_cost: 2.5 }, { total_cost: null }, { total_cost: 1.25 }]), { count: 3, cost: 3.75, uncosted: 1 }) const products = [{ name: 'Σουβλάκι' }, { name: 'Απλή πίτα' }, { name: 'Off', is_available: false }, { name: 'Delivery', system_key: 'delivery_fee' }] assert.deepEqual(wasteProducts(products, 'el').map(p => p.name), ['Απλή πίτα', 'Σουβλάκι']) }) test('notes: pinned first; to-dos: urgent first, done apart', () => { const { pinned, rest } = splitNotes([ { id: 1, is_pinned: false, updated_at: '2026-11-19T10:00:00Z' }, { id: 2, is_pinned: true, updated_at: '2026-11-18T10:00:00Z' }, { id: 3, is_pinned: false, updated_at: '2026-11-19T11:00:00Z' }]) assert.deepEqual([pinned.map(n => n.id), rest.map(n => n.id)], [[2], [3, 1]]) const { open, done } = splitTodos([ { id: 1, is_done: false, priority: 'normal', created_at: '2026-11-19T12:00:00Z' }, { id: 2, is_done: false, priority: 'high', created_at: '2026-11-18T12:00:00Z' }, { id: 3, is_done: true, priority: 'high', done_at: '2026-11-19T09:00:00Z' }]) assert.deepEqual([open.map(t => t.id), done.map(t => t.id)], [[2, 1], [3]]) })