// The products page: categories, per-category order, filters and the editor's form (plan step 6.3). import { test } from 'node:test' import assert from 'node:assert/strict' import { SCOPES, applyFilters, bulkCategories, categoryTree, costOf, countIn, groupProducts, moveId, reorderBody, scopeProducts, stepBody, treeLabel, } from '../src/pages/products/productsLogic.js' import { addCategory, buildBody, buildForm, canSave, cleanTag, cleanTexts, labelIn, favourites, moveChoice, moveFavourite, removeCategory, removeChoice, removeGroup, setSub, toggleFavourite } from '../src/pages/products/editor/formLogic.js' const cats = [ { id: 1, name: 'Coffees', sort_order: 0 }, { id: 2, name: 'Breakfast', sort_order: 1 }, { id: 3, name: 'Hot', parent_id: 1, sort_order: 0 }, ] const prod = (id, name, ids, positions = {}, o = {}) => ({ id, name, base_price: 3, is_available: true, lifecycle_status: 'active', category_ids: ids, primary_category_id: ids[0] ?? null, categories: ids.map(c => ({ category_id: c, sort_order: positions[c] ?? 0 })), ...o, }) const freddo = prod(10, 'Freddo', [1, 2], { 1: 0, 2: 5 }) const toast = prod(11, 'Τοστ', [2], { 2: 1 }) const latte = prod(12, 'Latte', [3], { 3: 0 }, { base_price: 4.5, image_url: '/x' }) const old = prod(13, 'Old', [1], {}, { lifecycle_status: 'archived' }) const fork = prod(14, 'Fork', [], {}, { is_service_item: true }) const loose = prod(15, 'Loose', []) const all = [freddo, toast, latte, old, fork, loose] test('the tree, counts and scopes follow every listing', () => { assert.deepEqual(categoryTree(cats).map(x => [x.cat.id, x.depth]), [[1, 0], [3, 1], [2, 0]]) assert.equal(treeLabel(cats[2], 1).endsWith('↳ Hot'), true) assert.equal(countIn(all, cats, 1), 2) // freddo + latte (sub), not the archived one assert.equal(countIn(all, cats, 2), 2) // freddo also counts here assert.deepEqual(scopeProducts(all, cats, 2).map(p => p.id), [10, 11]) assert.deepEqual(scopeProducts(all, cats, 1).map(p => p.id), [10, 12]) assert.deepEqual(scopeProducts(all, cats, 1, { showArchived: true }).map(p => p.id), [10, 12, 13]) assert.deepEqual(scopeProducts(all, cats, SCOPES.service).map(p => p.id), [14]) assert.deepEqual(scopeProducts(all, cats, SCOPES.none).map(p => p.id), [15]) assert.deepEqual(scopeProducts(all, cats, SCOPES.archived).map(p => p.id), [13]) assert.deepEqual(scopeProducts(all, cats, SCOPES.all).map(p => p.id), [10, 11, 12, 15]) }) test('filters: search ignores accents and finds Greek names in Latin letters', () => { const list = scopeProducts(all, cats, SCOPES.all) assert.deepEqual(applyFilters(list, { search: 'tost' }).map(p => p.id), [11]) assert.deepEqual(applyFilters(list, { search: 'ΤΟΣΤ' }).map(p => p.id), [11]) assert.deepEqual(applyFilters(list, { priceMin: 4 }).map(p => p.id), [12]) assert.deepEqual(applyFilters(list, { quality: 'no-photo' }).map(p => p.id), [10, 11, 15]) assert.deepEqual(applyFilters(list, { quality: 'no-cat' }).map(p => p.id), [15]) }) test('groups: each category its own list in its own order; a product in two shows in both', () => { const inBreakfast = groupProducts(scopeProducts(all, cats, 2), cats, 2) assert.deepEqual(inBreakfast.map(g => [g.key, g.items.map(p => p.id)]), [[2, [11, 10]]]) // toast 1, freddo 5 const coffees = groupProducts(scopeProducts(all, cats, 1), cats, 1) assert.deepEqual(coffees.map(g => [g.key, g.items.map(p => p.id)]), [[1, [10]], [3, [12]]]) assert.deepEqual(groupProducts(scopeProducts(all, cats, 1), cats, 1, 3).map(g => g.key), [3]) const everything = groupProducts(scopeProducts(all, cats, SCOPES.all), cats, SCOPES.all) assert.deepEqual(everything.map(g => [g.key, g.items.map(p => p.id)]), [[1, [10]], [3, [12]], [2, [11]], ['__none__', [15]]]) // by main category }) test('reordering sends positions for that category only', () => { const g = groupProducts(scopeProducts(all, cats, 2), cats, 2)[0] // [toast, freddo] assert.deepEqual(reorderBody(g, 10, 11), [{ id: 10, sort_order: 0, category_id: 2 }, { id: 11, sort_order: 1, category_id: 2 }]) assert.deepEqual(stepBody(g, 11, 1), [{ id: 10, sort_order: 0, category_id: 2 }, { id: 11, sort_order: 1, category_id: 2 }]) assert.equal(stepBody(g, 11, -1), null) assert.deepEqual(reorderBody({ key: '__none__', items: [loose, fork] }, 14, 15), [{ id: 14, sort_order: 0 }, { id: 15, sort_order: 1 }]) assert.deepEqual(moveId([1, 2, 3], 3, 1), [3, 1, 2]) assert.equal(moveId([1, 2], 1, 1), null) }) test('cost, margin and bulk categories', () => { assert.deepEqual(costOf({ base_price: 4, cost_simple: 1 }), { cost: 1, over: false, margin: 0.75 }) assert.equal(costOf({ base_price: 4, cost_breakdown: [{ amount: 3 }, { amount: 2 }] }).over, true) assert.equal(costOf({ base_price: 4 }), null) assert.deepEqual(bulkCategories(freddo, 3, 'move'), { category_ids: [3], primary_category_id: 3 }) assert.deepEqual(bulkCategories(freddo, 3, 'add'), { category_ids: [1, 2, 3], primary_category_id: 1 }) assert.deepEqual(bulkCategories(loose, 2, 'add'), { category_ids: [2], primary_category_id: 2 }) }) test('the editor keeps categories, the main one and allergens', () => { const f = buildForm({ ...freddo, allergens: { milk: 'contains' }, primary_category_id: 2 }) assert.deepEqual([f.category_ids, f.primary_category_id], [[1, 2], 2]) let g = removeCategory(f, 2) assert.deepEqual([g.category_ids, g.primary_category_id], [[1], 1]) // the main one went: the next takes over g = addCategory(removeCategory(g, 1), 3) assert.deepEqual([g.category_ids, g.primary_category_id], [[3], 3]) // the first one becomes main const body = buildBody({ ...f, base_price: '3,5' }) assert.deepEqual([body.category_ids, body.primary_category_id, body.base_price, body.allergens], [[1, 2], 2, 3.5, { milk: 'contains' }]) assert.equal(canSave({ ...f, name: ' ' }), false) assert.equal(canSave({ ...f, base_price: '' }), false) assert.equal(canSave({ ...f, base_price: '', is_service_item: true }), true) assert.equal(cleanTag(['vegan'], ' Spicy, '), 'spicy') assert.equal(cleanTag(['vegan'], 'VEGAN'), null) }) test('the editor: favourites across lists, choices, groups', () => { let f = buildForm({ name: 'X', base_price: 1, quick_options: [{ name: 'Bacon' }], ingredients: [{ name: 'Onion', group_id: 7 }], options: [], preference_sets: [{ name: 'Sugar', default_choice_id: 2, choices: [{ id: 1, name: 'None' }, { id: 2, name: 'Medium' }, { id: 3, name: 'Sweet' }] }], modifier_groups: [{ id: 7, modifier_type: 'ingredient', name: 'Veg' }] }) f = toggleFavourite(toggleFavourite(f, 'ingredient', 0), 'quick', 0) assert.deepEqual(favourites(f).map(x => x.item.name), ['Onion', 'Bacon']) f = moveFavourite(f, 1, -1) assert.deepEqual(favourites(f).map(x => x.item.name), ['Bacon', 'Onion']) const ps = f.preference_sets[0] assert.equal(ps.default_choice_index, 1) assert.equal(moveChoice(ps, 1, 1).default_choice_index, 2) // the default follows its choice assert.equal(removeChoice(ps, 0).default_choice_index, 0) assert.equal(removeChoice(ps, 1).default_choice_index, -1) assert.deepEqual(setSub([{ is_default: true }, { is_default: false }], 1, 'is_default', true).map(s => s.is_default), [false, true]) const body = buildBody(f) assert.equal(body.ingredients[0].group_id, 0) // the group's index in modifier_groups assert.equal(buildBody(removeGroup(f, 7)).ingredients[0].group_id, null) }) test('the editor: every text per language; "Default" is the venue language (owner 2026-11-21)', () => { const f = buildForm({ name: 'Σουβλάκι', base_price: 3, quick_options: [{ name: 'Πίτα', translations: { el: 'Πίτα' } }, { name: 'Σάλτσα' }], translations: { el: { name: 'Σουβλάκι' }, en: { name: 'Skewer', quick: { 'Σάλτσα': 'Sauce' } } } }) assert.deepEqual(f.names, { el: 'Σουβλάκι', en: 'Skewer' }) assert.equal(f.quick_options[1].tr.en, 'Sauce') // the product's old quick map // the venue speaks Greek: the shown name is the Greek one let body = buildBody(f, 'el') assert.equal(body.name, 'Σουβλάκι') assert.deepEqual(body.translations, { el: { name: 'Σουβλάκι' }, en: { name: 'Skewer' } }) assert.deepEqual(body.quick_options[1].translations, { en: 'Sauce' }) // the venue switches to English: English is shown; with no English, the only text there is assert.equal(buildBody(f, 'en').name, 'Skewer') const greekOnly = { ...f, names: { el: 'Πίτα' } } assert.equal(buildBody(greekOnly, 'en').name, 'Πίτα') assert.equal(labelIn(f.quick_options[0], 'en', 'en'), 'Πίτα') // nothing in English: what is shown // a choice typed only in English is called that const typed = { ...f, ingredients: [{ name: '', tr: { en: 'Onion' }, extra_cost: 0, is_favorite: false, favorite_sort_order: 0, is_compact: false, group_id: null }] } assert.deepEqual([buildBody(typed, 'el').ingredients[0].name, buildBody(typed, 'el').ingredients[0].translations], ['Onion', { en: 'Onion' }]) assert.equal(canSave({ ...f, names: {}, name: '' }, 'el'), false) assert.deepEqual(cleanTexts({ el: ' ', en: ' x ', fr: 'y' }), { en: 'x' }) }) test('a category dropped above or below a row lands where the line showed (2026-11-21)', async () => { const { moveIdTo } = await import('../src/pages/products/productsLogic.js') assert.deepEqual(moveIdTo([1, 2, 3, 4], 1, 3, false), [2, 1, 3, 4]) // above 3 assert.deepEqual(moveIdTo([1, 2, 3, 4], 1, 3, true), [2, 3, 1, 4]) // below 3 assert.deepEqual(moveIdTo([1, 2, 3, 4], 4, 2, false), [1, 4, 2, 3]) assert.equal(moveIdTo([1, 2, 3], 1, 2, false), null) // already there assert.equal(moveIdTo([1, 2, 3], 2, 2, true), null) }) test('"One more" travels with the product, off unless set (6F.5)', () => { assert.equal(buildForm({ name: 'Beer', base_price: 4 }).one_more_enabled, false) const f = buildForm({ name: 'Beer', base_price: 4, one_more_enabled: true }) assert.equal(f.one_more_enabled, true) assert.equal(buildBody(f).one_more_enabled, true) assert.equal(buildBody({ ...f, one_more_enabled: false }).one_more_enabled, false) })