@managesome/eposmatic-toolkit 0.2.719 → 0.2.721

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -222,193 +222,3 @@ describe('applyChooseExpensiveSegmentPrice (standalone regression)', () => {
222
222
  expect(segB.options.find((o) => o._id === 'torent2').price).toBe(600);
223
223
  });
224
224
  });
225
- describe('applyNestedChooseExpensiveSegmentPrice — selected-halves max', () => {
226
- /** Nested half-half hub: one flagged HALF_HALF option holding the segments. */
227
- function makeHub(segmentGroups) {
228
- return makeEngineItem({
229
- modifiers: [
230
- makeEngineModifier({
231
- _id: 'first-pizza',
232
- name: 'First Pizza',
233
- depth: 0,
234
- options: [
235
- makeEngineOption({
236
- _id: 'halfhalf-1',
237
- ref: 'halfhalf-1',
238
- ref_type: constants_1.MODIFIER_OPTION_REF_TYPE_ENUM.HALF_HALF,
239
- choose_expensive_segment_price: true,
240
- default: true,
241
- price: 0,
242
- modifiers: segmentGroups,
243
- }),
244
- ],
245
- }),
246
- ],
247
- });
248
- }
249
- /** Marks exactly one option per segment as selected. */
250
- function withSelections(item, selections) {
251
- return {
252
- ...item,
253
- modifiers: item.modifiers.map((modifier) => ({
254
- ...modifier,
255
- options: modifier.options.map((option) => option.ref_type === constants_1.MODIFIER_OPTION_REF_TYPE_ENUM.HALF_HALF
256
- ? {
257
- ...option,
258
- modifiers: option.modifiers.map((segment) => ({
259
- ...segment,
260
- options: segment.options.map((o) => ({
261
- ...o,
262
- default: o._id === selections[segment._id],
263
- })),
264
- })),
265
- }
266
- : option),
267
- })),
268
- };
269
- }
270
- function halfHalfOption(item) {
271
- return item.modifiers[0].options[0];
272
- }
273
- /** Marks the given option of a segment as a free slot (`is_free`). */
274
- function markFree(item, segmentId, optionId) {
275
- return {
276
- ...item,
277
- modifiers: item.modifiers.map((modifier) => ({
278
- ...modifier,
279
- options: modifier.options.map((option) => option.ref_type === constants_1.MODIFIER_OPTION_REF_TYPE_ENUM.HALF_HALF
280
- ? {
281
- ...option,
282
- modifiers: option.modifiers.map((segment) => segment._id === segmentId
283
- ? {
284
- ...segment,
285
- options: segment.options.map((o) => (o._id === optionId ? { ...o, is_free: true } : o)),
286
- }
287
- : segment),
288
- }
289
- : option),
290
- })),
291
- };
292
- }
293
- function segmentPrice(item, segmentId, optionId) {
294
- const option = halfHalfOption(item).modifiers.find((m) => m._id === segmentId);
295
- return option.options.find((o) => o._id === optionId).price;
296
- }
297
- it('a cheap selected pair is NOT raised (max of the SELECTED halves only)', () => {
298
- // seg-1: milan(500) selected, torent(600) not; seg-2: milan2(500) selected,
299
- // torent2(600) not.
300
- const item = withSelections(makeHub(makeSegmentGroups()), {
301
- 'seg-1': 'milan',
302
- 'seg-2': 'milan2',
303
- });
304
- const result = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(item);
305
- expect(segmentPrice(result, 'seg-1', 'milan')).toBe(500);
306
- expect(segmentPrice(result, 'seg-2', 'milan2')).toBe(500);
307
- // Unselected expensive options keep their own prices.
308
- expect(segmentPrice(result, 'seg-1', 'torent')).toBe(600);
309
- });
310
- it('LOWERS with basePrices when switching from an expensive to a cheap pair', () => {
311
- const basePrices = { milan: 500, torent: 600, milan2: 500, torent2: 600 };
312
- // Selected: milan (500) + torent2 (600) -> milan is raised to 600.
313
- const normalized = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(withSelections(makeHub(makeSegmentGroups()), { 'seg-1': 'milan', 'seg-2': 'torent2' }));
314
- expect(segmentPrice(normalized, 'seg-1', 'milan')).toBe(600);
315
- // User switches seg-2 to milan2 (cheap pair: 500+500). Re-applying with
316
- // pristine basePrices brings the previously raised milan back to 500.
317
- const switched = withSelections(normalized, { 'seg-1': 'milan', 'seg-2': 'milan2' });
318
- const lowered = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(switched, { basePrices });
319
- expect(segmentPrice(lowered, 'seg-1', 'milan')).toBe(500);
320
- expect(segmentPrice(lowered, 'seg-2', 'milan2')).toBe(500);
321
- });
322
- it('changing picks: cheap pair lowers the raised half, swap keeps the max', () => {
323
- const basePrices = { milan: 500, torent: 600, milan2: 500, torent2: 600 };
324
- const hub = makeHub(makeSegmentGroups());
325
- // milan (500) + torent2 (600) -> milan raised to 600.
326
- const premium = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(withSelections(hub, { 'seg-1': 'milan', 'seg-2': 'torent2' }));
327
- expect(segmentPrice(premium, 'seg-1', 'milan')).toBe(600);
328
- // -> cheap pair milan + milan2: the previously raised milan returns to 500.
329
- const cheap = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(withSelections(premium, { 'seg-1': 'milan', 'seg-2': 'milan2' }), { basePrices });
330
- expect(segmentPrice(cheap, 'seg-1', 'milan')).toBe(500);
331
- expect(segmentPrice(cheap, 'seg-2', 'milan2')).toBe(500);
332
- // De-selected torent2 keeps its own 600.
333
- expect(segmentPrice(cheap, 'seg-2', 'torent2')).toBe(600);
334
- // -> swap to torent + milan2: the newly expensive half is raised and the
335
- // de-selected milan is re-based (no leftover 600).
336
- const swapped = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(withSelections(premium, { 'seg-1': 'torent', 'seg-2': 'milan2' }), { basePrices });
337
- expect(segmentPrice(swapped, 'seg-1', 'torent')).toBe(600);
338
- expect(segmentPrice(swapped, 'seg-2', 'milan2')).toBe(600);
339
- expect(segmentPrice(swapped, 'seg-1', 'milan')).toBe(500);
340
- });
341
- it('full change matrix: every switch recomputes from base prices (pristine vs raised source)', () => {
342
- const basePrices = { milan: 500, torent: 600, milan2: 500, torent2: 600 };
343
- const combos = [
344
- { 'seg-1': 'milan', 'seg-2': 'milan2' },
345
- { 'seg-1': 'milan', 'seg-2': 'torent2' },
346
- { 'seg-1': 'torent', 'seg-2': 'milan2' },
347
- { 'seg-1': 'torent', 'seg-2': 'torent2' },
348
- ];
349
- const hub = makeHub(makeSegmentGroups());
350
- for (const from of combos) {
351
- const fromDisplay = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(withSelections(hub, from));
352
- for (const to of combos) {
353
- const expected = Math.max(basePrices[to['seg-1']], basePrices[to['seg-2']]);
354
- for (const source of [hub, fromDisplay]) {
355
- const result = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(withSelections(source, to), {
356
- basePrices,
357
- });
358
- expect(segmentPrice(result, 'seg-1', to['seg-1'])).toBe(expected);
359
- expect(segmentPrice(result, 'seg-2', to['seg-2'])).toBe(expected);
360
- // Every half de-selected by the switch is back to its own price.
361
- for (const segmentId of ['seg-1', 'seg-2']) {
362
- const fromOption = from[segmentId];
363
- if ([to['seg-1'], to['seg-2']].includes(fromOption))
364
- continue;
365
- expect(segmentPrice(result, segmentId, fromOption)).toBe(basePrices[fromOption]);
366
- }
367
- }
368
- }
369
- }
370
- });
371
- it('is idempotent when re-applied with basePrices', () => {
372
- const basePrices = { milan: 500, torent: 600, milan2: 500, torent2: 600 };
373
- const once = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(withSelections(makeHub(makeSegmentGroups()), { 'seg-1': 'milan', 'seg-2': 'torent2' }), { basePrices });
374
- const twice = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(once, { basePrices });
375
- expect(twice).toEqual(once);
376
- });
377
- it('resetOrderEngineItemPrices restores nested segment prices (edit flow)', () => {
378
- const basePrices = { milan: 500, torent: 600, milan2: 500, torent2: 600 };
379
- const raised = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(withSelections(makeHub(makeSegmentGroups()), { 'seg-1': 'milan', 'seg-2': 'torent2' }));
380
- expect(segmentPrice(raised, 'seg-1', 'milan')).toBe(600);
381
- // Reopening an edit: the persisted raise is re-based from the pristine map.
382
- const rebased = (0, order_engine_1.resetOrderEngineItemPrices)(raised, basePrices);
383
- expect(segmentPrice(rebased, 'seg-1', 'milan')).toBe(500);
384
- expect(segmentPrice(rebased, 'seg-1', 'torent')).toBe(600);
385
- expect(segmentPrice(rebased, 'seg-2', 'torent2')).toBe(600);
386
- // Changing the pick afterwards lowers correctly (no accumulation).
387
- const cheap = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(withSelections(rebased, { 'seg-1': 'milan', 'seg-2': 'milan2' }), { basePrices });
388
- expect(segmentPrice(cheap, 'seg-1', 'milan')).toBe(500);
389
- expect(segmentPrice(cheap, 'seg-2', 'milan2')).toBe(500);
390
- });
391
- it('single selected segment: nothing to raise', () => {
392
- const item = withSelections(makeHub(makeSegmentGroups()), { 'seg-1': 'milan' });
393
- const result = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(item);
394
- expect(segmentPrice(result, 'seg-1', 'milan')).toBe(500);
395
- // No selection in segment 2 — its options keep their own prices.
396
- expect(segmentPrice(result, 'seg-2', 'milan2')).toBe(500);
397
- expect(segmentPrice(result, 'seg-2', 'torent2')).toBe(600);
398
- });
399
- it('no selection at all: nothing is raised or invented', () => {
400
- const item = withSelections(makeHub(makeSegmentGroups()), {});
401
- const result = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(item);
402
- expect(segmentPrice(result, 'seg-1', 'milan')).toBe(500);
403
- expect(segmentPrice(result, 'seg-1', 'torent')).toBe(600);
404
- expect(segmentPrice(result, 'seg-2', 'torent2')).toBe(600);
405
- });
406
- it('a free selected half neither drives the max nor gets raised', () => {
407
- // The expensive side (torent2, 600) sits in a free slot: the paid half
408
- // (milan, 500) must NOT be raised to it.
409
- const item = markFree(withSelections(makeHub(makeSegmentGroups()), { 'seg-1': 'milan', 'seg-2': 'torent2' }), 'seg-2', 'torent2');
410
- const result = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(item);
411
- expect(segmentPrice(result, 'seg-1', 'milan')).toBe(500);
412
- expect(segmentPrice(result, 'seg-2', 'torent2')).toBe(600);
413
- });
414
- });