Files
xenia-pos-local/manager_dashboard/tests/fees.test.mjs
T
bonaminandClaude Opus 5.5 6f42e00684 feat: delivery areas drawn on a map (plan 6R.G)
An area is a list of postcodes, a shape drawn on a map or a circle around a
point. The fee engine (both copies, byte-identical) matches a shape only by
the customer's pin, then postcodes; point-in-polygon (edges inside) and
haversine circles; shared cases in fee_cases.json.

- Alembic 0053: delivery_areas.kind / geometry, orders.online_lat / lng
- /api/connect/fees: areas with kind + geometry (validated: 3-200 corners,
  50 m - 50 km), the venue's place (PUT /venue-point, setting venue.location,
  sent to the cloud as fees.venue_point), preview by lat / lng
- manager: DeliveryMap (Leaflet + OpenStreetMap tiles): draw / move / remove
  corners, circles, the venue on the map, an overview, test an order by a point
- pulled online orders keep the pin; the site re-checks the fee with it;
  the counter links the pin to a map
- tests: backend (shapes, venue point, pinned orders), manager logic,
  e2e manager.mjs and order_app.mjs 6R.G blocks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 21:55:36 +02:00

101 lines
6.9 KiB
JavaScript

// Delivery areas and fee rules, the manager's forms (plan phase 3S, step S.3).
import assert from 'node:assert/strict'
import { test } from 'node:test'
import { describeRule, emptyRule, moveRule, parsePostcodes, previewBody, ruleBody, ruleProblems, ruleToForm } from '../src/pages/online/feesLogic.js'
test('postcodes typed freely become a tidy list', () => {
assert.deepEqual(parsePostcodes('241 00, 24131;24131\n 241 31 '), ['241 00', '24131', '241 31'])
assert.deepEqual(parsePostcodes(''), [])
})
test('a rule form round-trips and becomes the request body', () => {
const form = { ...emptyRule(), name: ' Far ', area_ids: [2], subtotal_min: '15,5', items_max: '9', days: [6, 5], time_from: '22:00', time_to: '02:00', value: '1,5', action: 'add' }
assert.deepEqual(ruleBody(form), {
name: 'Far', is_active: true, sales_points: ['online_delivery'], area_ids: [2], subtotal_min: 15.5, subtotal_max: null,
items_min: null, items_max: 9, days: [5, 6], time_from: '22:00', time_to: '02:00', action: 'add', value: 1.5, stop: false,
})
const saved = { id: 7, ...ruleBody(form) }
assert.equal(ruleToForm(saved).subtotal_min, '15.5')
assert.equal(ruleToForm(saved).subtotal_max, '')
assert.equal(ruleBody({ ...emptyRule(), name: 'Free', action: 'free', value: '' }).value, 0)
})
test('what a rule form still needs', () => {
assert.deepEqual(ruleProblems(emptyRule()), ['name', 'value'])
const ok = { ...emptyRule(), name: 'Base', value: '2' }
assert.deepEqual(ruleProblems(ok), [])
assert.deepEqual(ruleProblems({ ...ok, value: '-1' }), ['negative'])
assert.deepEqual(ruleProblems({ ...ok, action: 'add', value: '-1' }), [])
assert.deepEqual(ruleProblems({ ...ok, action: 'percent', value: '120' }), ['percent'])
assert.deepEqual(ruleProblems({ ...ok, subtotal_min: '20', subtotal_max: '10', items_min: '5', items_max: '2', time_from: '22:00' }),
['subtotalRange', 'itemsRange', 'times'])
assert.deepEqual(ruleProblems({ ...ok, sales_points: [] }), ['salesPoints'])
})
test('a rule in words', () => {
const d = describeRule({ area_ids: [2], subtotal_min: 25, subtotal_max: null, items_min: 10, items_max: null, days: [5, 6], time_from: '22:00', time_to: '02:00', action: 'free', value: 0 }, { 2: 'Kalamata' })
assert.deepEqual(d.when.map(w => w[0]), ['areas', 'subtotalFrom', 'itemsFrom', 'days', 'time'])
assert.equal(d.when[0][1].names, 'Kalamata')
assert.deepEqual(d.action, ['free', { value: 0 }])
assert.deepEqual(describeRule({ days: [0, 1, 2, 3, 4, 5, 6], action: 'set', value: 2 }).when, [])
})
test('moving rules and the test form', () => {
const rules = [{ id: 1 }, { id: 2 }, { id: 3 }]
assert.deepEqual(moveRule(rules, 2, -1), [2, 1, 3])
assert.deepEqual(moveRule(rules, 3, 1), [1, 2, 3])
assert.deepEqual(previewBody({ sales_point: 'online_delivery', where: 'postcode', area_id: '1', postcode: ' 24131 ', subtotal: '12,5', items: '3', day: 'now', time: '' }),
{ sales_point: 'online_delivery', area_id: null, postcode: '24131', lat: null, lng: null, subtotal: 12.5, items: 3, weekday: null, time: null })
assert.deepEqual(previewBody({ sales_point: 'online_delivery', where: 'area', area_id: '2', postcode: '', subtotal: '', items: '', day: '5', time: '23:30' }),
{ sales_point: 'online_delivery', area_id: 2, postcode: null, lat: null, lng: null, subtotal: 0, items: 0, weekday: 5, time: '23:30' })
})
test('online ordering settings: hours per day, any time, numbers', async () => {
const { RES_DEFAULTS, orderingBody, orderingToForm } = await import('../src/pages/online/feesLogic.js')
const saved = { services: ['delivery'], hours: { 5: [['20:00', '02:00']] }, prep_minutes: 30, slot_minutes: 15, days_ahead: 1, asap: false,
reservations: RES_DEFAULTS }
const form = orderingToForm(saved)
assert.equal(form.anyTime, false)
assert.deepEqual(form.days[5], [{ from: '20:00', to: '02:00' }])
assert.deepEqual(orderingBody(form), saved)
assert.deepEqual(orderingBody({ ...form, anyTime: true }).hours, {})
const blank = orderingToForm(null)
assert.deepEqual(orderingBody({ ...blank, prep_minutes: '' }), { services: [], hours: {}, prep_minutes: 20, slot_minutes: 15, days_ahead: 0, asap: true, reservations: RES_DEFAULTS })
form.days[0] = [{ from: '12:00', to: '12:00' }, { from: '', to: '15:00' }]
assert.equal(orderingBody(form).hours['0'], undefined)
})
test('table bookings online: settings round trip (S.6)', async () => {
const { orderingBody, orderingToForm } = await import('../src/pages/online/feesLogic.js')
const reservations = { enabled: true, with_food: true, hours: { 4: [['19:00', '23:30']] }, slot_minutes: 15, days_ahead: 14, notice_minutes: 90, party_max: 8, fire_minutes: 25 }
const form = orderingToForm({ services: [], hours: {}, reservations })
assert.equal(form.res.anyTime, false)
assert.deepEqual(orderingBody(form).reservations, reservations)
assert.deepEqual(orderingBody({ ...form, res: { ...form.res, enabled: false } }).reservations.with_food, false)
assert.deepEqual(orderingBody({ ...form, res: { ...form.res, anyTime: true, party_max: '' } }).reservations,
{ ...reservations, hours: {}, party_max: 10 })
})
test('6R.G: an area form for each kind, and what keeps it from saving', async () => {
const { areaBody, areaProblem, distanceText } = await import('../src/pages/online/feesLogic.js')
assert.deepEqual(areaBody({ name: ' Centre ', codes: '24100, 24131' }), { name: 'Centre', kind: 'postcodes', is_active: true, postcodes: ['24100', '24131'] })
const sq = [[37, 22], [37, 22.1], [37.1, 22.1]]
assert.deepEqual(areaBody({ name: 'Drawn', kind: 'polygon', corners: sq, codes: '1' }), { name: 'Drawn', kind: 'polygon', is_active: true, postcodes: [], geometry: sq })
assert.deepEqual(areaBody({ name: 'Ring', kind: 'circle', circle: { center: [37, 22], radius_m: 1500 }, active: false }).geometry, { center: [37, 22], radius_m: 1500 })
assert.equal(areaProblem({ name: ' ' }), 'name')
assert.equal(areaProblem({ name: 'x', kind: 'polygon', corners: sq.slice(0, 2) }), 'corners')
assert.equal(areaProblem({ name: 'x', kind: 'polygon', corners: sq }), null)
assert.equal(areaProblem({ name: 'x', kind: 'circle', circle: { radius_m: 500 } }), 'centre')
assert.equal(areaProblem({ name: 'x', kind: 'circle', circle: { center: [37, 22], radius_m: 20 } }), 'radius')
assert.equal(areaProblem({ name: 'x', kind: 'circle', circle: { center: [37, 22], radius_m: null } }), 'radius')
assert.equal(distanceText(800), '800 m')
assert.equal(distanceText(2549), '2.5 km')
})
test('6R.G: a test order by a point on the map', () => {
const body = previewBody({ sales_point: 'online_delivery', where: 'point', point: [37.05, 22.05], area_id: 3, postcode: '1', subtotal: '10', items: '1', day: 'now' })
assert.deepEqual([body.lat, body.lng, body.area_id, body.postcode], [37.05, 22.05, null, null])
const none = previewBody({ sales_point: 'online_delivery', where: 'area', area_id: '', postcode: '', subtotal: '10', items: '1', day: 'now' })
assert.deepEqual([none.lat, none.lng], [null, null])
})