@managesome/eposmatic-toolkit 0.4.32 → 0.4.35
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.
- package/esm/constants/MATERIALIZED_REPORTS.js +22 -4
- package/esm/constants/enums/offers.js +0 -6
- package/esm/helpers/materialized-reports/index.js +1 -0
- package/esm/helpers/materialized-reports/materialized-index-bootstrap.js +4 -0
- package/esm/helpers/materialized-reports/materialized-prep-time-classification-helpers.js +12 -8
- package/esm/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +141 -10
- package/esm/helpers/materialized-reports/materialized-prep-time-compile-helpers.js +7 -3
- package/esm/helpers/materialized-reports/materialized-prep-time-read-helpers.js +638 -0
- package/esm/helpers/types/materialized-reports-apis.js +10 -0
- package/esm/schemas/models/index.js +0 -1
- package/esm/schemas/models/materialized_tenant_prep_time_entries.js +2 -2
- package/esm/schemas/types/index.js +0 -1
- package/lib/constants/MATERIALIZED_REPORTS.d.ts +17 -4
- package/lib/constants/MATERIALIZED_REPORTS.js +24 -6
- package/lib/constants/enums/offers.d.ts +0 -5
- package/lib/constants/enums/offers.js +1 -7
- package/lib/helpers/materialized-reports/index.d.ts +1 -0
- package/lib/helpers/materialized-reports/index.js +1 -0
- package/lib/helpers/materialized-reports/materialized-index-bootstrap.js +4 -0
- package/lib/helpers/materialized-reports/materialized-prep-time-classification-helpers.js +12 -8
- package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.d.ts +22 -0
- package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +142 -9
- package/lib/helpers/materialized-reports/materialized-prep-time-compile-helpers.js +6 -2
- package/lib/helpers/materialized-reports/materialized-prep-time-read-helpers.d.ts +198 -0
- package/lib/helpers/materialized-reports/materialized-prep-time-read-helpers.js +666 -0
- package/lib/helpers/types/materialized-reports-apis.d.ts +145 -1
- package/lib/helpers/types/materialized-reports-apis.js +11 -0
- package/lib/schemas/models/index.d.ts +0 -1
- package/lib/schemas/models/index.js +0 -1
- package/lib/schemas/models/materialized_tenant_prep_time_entries.d.ts +6 -6
- package/lib/schemas/models/materialized_tenant_prep_time_entries.js +1 -1
- package/lib/schemas/types/index.d.ts +0 -1
- package/lib/schemas/types/index.js +0 -1
- package/lib/schemas/types/materialized_tenant_prep_time_reports.d.ts +7 -5
- package/lib/tests/materialized-reports/materialized-prep-time-classification.test.js +28 -5
- package/lib/tests/materialized-reports/materialized-prep-time-close-day.test.js +33 -1
- package/lib/tests/materialized-reports/materialized-prep-time-compile.test.js +10 -1
- package/lib/tests/materialized-reports/materialized-prep-time-constants.test.js +14 -1
- package/lib/tests/materialized-reports/materialized-prep-time-duration-consistency.test.js +135 -0
- package/lib/tests/materialized-reports/materialized-prep-time-plan.test.js +106 -0
- package/lib/tests/materialized-reports/materialized-prep-time-read-helpers.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-prep-time-read-helpers.test.js +620 -0
- package/lib/tests/offers-matrix.test.d.ts +1 -0
- package/lib/tests/offers-matrix.test.js +401 -0
- package/package.json +1 -1
- package/esm/schemas/models/rewards_usage.js +0 -61
- package/lib/schemas/models/rewards_usage.d.ts +0 -53
- package/lib/schemas/models/rewards_usage.js +0 -64
- package/lib/schemas/types/rewards_usage.d.ts +0 -13
- package/lib/schemas/types/rewards_usage.js +0 -2
- /package/{esm/schemas/types/rewards_usage.js → lib/tests/materialized-reports/materialized-prep-time-duration-consistency.test.d.ts} +0 -0
|
@@ -0,0 +1,620 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const materialized_prep_time_read_helpers_1 = require("../../helpers/materialized-reports/materialized-prep-time-read-helpers");
|
|
4
|
+
const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
|
|
5
|
+
const materialized_reports_apis_1 = require("../../helpers/types/materialized-reports-apis");
|
|
6
|
+
/**
|
|
7
|
+
* PREP TIME item-level read helpers.
|
|
8
|
+
*
|
|
9
|
+
* These are the load-bearing rules of the phase:
|
|
10
|
+
* - filters apply BEFORE grouping, so coverage follows the filters;
|
|
11
|
+
* - UNMEASURED rows are counted in coverage and NEVER in avg / median / p90 /
|
|
12
|
+
* on-time;
|
|
13
|
+
* - out-of-range durations are excluded from the duration statistics only;
|
|
14
|
+
* - percentiles are exact (computed, not histogram-derived);
|
|
15
|
+
* - item sorting is total (item_id tie-break) so cursor paging cannot duplicate
|
|
16
|
+
* or skip a row.
|
|
17
|
+
*/
|
|
18
|
+
const row = (over = {}) => ({
|
|
19
|
+
order_id: 'o1',
|
|
20
|
+
item_id: 'i1',
|
|
21
|
+
item_name: 'Burger',
|
|
22
|
+
category_name: 'Mains',
|
|
23
|
+
anchor: 'MEASURED',
|
|
24
|
+
duration_minutes: 9,
|
|
25
|
+
delta_minutes: null,
|
|
26
|
+
order_status: 'COMPLETED',
|
|
27
|
+
platform: 'POS_WALK-IN',
|
|
28
|
+
created_at: '2026-09-10T10:00:00.000Z',
|
|
29
|
+
source_local_date: '2026-09-10',
|
|
30
|
+
...over,
|
|
31
|
+
});
|
|
32
|
+
describe('prep time read filters', () => {
|
|
33
|
+
it('parses an absent filters object into the normalized "all" shape', () => {
|
|
34
|
+
expect((0, materialized_prep_time_read_helpers_1.parseMaterializedPrepTimeReadFilters)(undefined)).toEqual({
|
|
35
|
+
ok: true,
|
|
36
|
+
filters: {
|
|
37
|
+
platforms: null,
|
|
38
|
+
order_statuses: null,
|
|
39
|
+
category_names: null,
|
|
40
|
+
search: null,
|
|
41
|
+
late_only: false,
|
|
42
|
+
anchor: null,
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
expect((0, materialized_prep_time_read_helpers_1.parseMaterializedPrepTimeReadFilters)(null)).toEqual((0, materialized_prep_time_read_helpers_1.parseMaterializedPrepTimeReadFilters)(undefined));
|
|
46
|
+
});
|
|
47
|
+
it('rejects wrong types and values a 400 must catch', () => {
|
|
48
|
+
expect((0, materialized_prep_time_read_helpers_1.parseMaterializedPrepTimeReadFilters)([]).ok).toBe(false);
|
|
49
|
+
expect((0, materialized_prep_time_read_helpers_1.parseMaterializedPrepTimeReadFilters)({ platforms: 'POS_WALK-IN' }).ok).toBe(false);
|
|
50
|
+
expect((0, materialized_prep_time_read_helpers_1.parseMaterializedPrepTimeReadFilters)({ platforms: ['NOT_A_PLATFORM'] }).ok).toBe(false);
|
|
51
|
+
expect((0, materialized_prep_time_read_helpers_1.parseMaterializedPrepTimeReadFilters)({ order_statuses: ['NOT_A_STATUS'] }).ok).toBe(false);
|
|
52
|
+
expect((0, materialized_prep_time_read_helpers_1.parseMaterializedPrepTimeReadFilters)({ anchor: 'OWN' }).ok).toBe(false);
|
|
53
|
+
expect((0, materialized_prep_time_read_helpers_1.parseMaterializedPrepTimeReadFilters)({ late_only: 'yes' }).ok).toBe(false);
|
|
54
|
+
expect((0, materialized_prep_time_read_helpers_1.parseMaterializedPrepTimeReadFilters)({ search: 3 }).ok).toBe(false);
|
|
55
|
+
expect((0, materialized_prep_time_read_helpers_1.parseMaterializedPrepTimeReadFilters)({ category_names: [''] }).ok).toBe(false);
|
|
56
|
+
});
|
|
57
|
+
it('normalizes enum arrays: trimmed, deduped, sorted', () => {
|
|
58
|
+
const parsed = (0, materialized_prep_time_read_helpers_1.parseMaterializedPrepTimeReadFilters)({
|
|
59
|
+
platforms: [' WEB_DELIVERY ', 'POS_WALK-IN', 'WEB_DELIVERY'],
|
|
60
|
+
order_statuses: ['COMPLETED', 'CANCELLED'],
|
|
61
|
+
});
|
|
62
|
+
expect(parsed.ok).toBe(true);
|
|
63
|
+
if (!parsed.ok)
|
|
64
|
+
return;
|
|
65
|
+
expect(parsed.filters.platforms).toEqual(['POS_WALK-IN', 'WEB_DELIVERY']);
|
|
66
|
+
expect(parsed.filters.order_statuses).toEqual(['CANCELLED', 'COMPLETED']);
|
|
67
|
+
});
|
|
68
|
+
it('trims search, treats empty as absent, and ignores category_names when asked', () => {
|
|
69
|
+
const parsed = (0, materialized_prep_time_read_helpers_1.parseMaterializedPrepTimeReadFilters)({ search: ' burger ', category_names: ['Not A Category'] }, { ignoreCategoryNames: true });
|
|
70
|
+
expect(parsed.ok).toBe(true);
|
|
71
|
+
if (!parsed.ok)
|
|
72
|
+
return;
|
|
73
|
+
expect(parsed.filters.search).toBe('burger');
|
|
74
|
+
expect(parsed.filters.category_names).toBeNull();
|
|
75
|
+
const empty = (0, materialized_prep_time_read_helpers_1.parseMaterializedPrepTimeReadFilters)({ search: ' ' });
|
|
76
|
+
expect(empty.ok).toBe(true);
|
|
77
|
+
if (empty.ok)
|
|
78
|
+
expect(empty.filters.search).toBeNull();
|
|
79
|
+
});
|
|
80
|
+
it('builds a signature that is independent of array order', () => {
|
|
81
|
+
const a = (0, materialized_prep_time_read_helpers_1.buildMaterializedPrepTimeFilterSignature)({
|
|
82
|
+
platforms: ['POS_WALK-IN', 'WEB_DELIVERY'],
|
|
83
|
+
order_statuses: ['COMPLETED'],
|
|
84
|
+
search: 'burger',
|
|
85
|
+
late_only: true,
|
|
86
|
+
anchor: 'MEASURED',
|
|
87
|
+
});
|
|
88
|
+
const b = (0, materialized_prep_time_read_helpers_1.buildMaterializedPrepTimeFilterSignature)({
|
|
89
|
+
platforms: ['WEB_DELIVERY', 'POS_WALK-IN'],
|
|
90
|
+
order_statuses: ['COMPLETED'],
|
|
91
|
+
search: 'burger',
|
|
92
|
+
late_only: true,
|
|
93
|
+
anchor: 'MEASURED',
|
|
94
|
+
});
|
|
95
|
+
expect(a).toBe(b);
|
|
96
|
+
expect(a).not.toBe((0, materialized_prep_time_read_helpers_1.buildMaterializedPrepTimeFilterSignature)({
|
|
97
|
+
platforms: ['WEB_DELIVERY', 'POS_WALK-IN'],
|
|
98
|
+
order_statuses: ['COMPLETED'],
|
|
99
|
+
search: 'burger',
|
|
100
|
+
late_only: true,
|
|
101
|
+
anchor: 'UNMEASURED',
|
|
102
|
+
}));
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
describe('prep time read filters - row semantics', () => {
|
|
106
|
+
const rows = [
|
|
107
|
+
row({ item_name: 'Chicken Burger', category_name: 'Mains' }),
|
|
108
|
+
row({
|
|
109
|
+
order_id: 'o2',
|
|
110
|
+
item_id: 'i2',
|
|
111
|
+
item_name: 'Cola',
|
|
112
|
+
category_name: 'Drinks',
|
|
113
|
+
platform: 'WEB_DELIVERY',
|
|
114
|
+
order_status: 'CANCELLED',
|
|
115
|
+
anchor: 'UNMEASURED',
|
|
116
|
+
duration_minutes: null,
|
|
117
|
+
delta_minutes: null,
|
|
118
|
+
}),
|
|
119
|
+
row({
|
|
120
|
+
order_id: 'o3',
|
|
121
|
+
item_id: 'i1',
|
|
122
|
+
item_name: 'Chicken Burger',
|
|
123
|
+
anchor: 'MEASURED',
|
|
124
|
+
duration_minutes: 20,
|
|
125
|
+
delta_minutes: 4,
|
|
126
|
+
}),
|
|
127
|
+
];
|
|
128
|
+
it('splits measured / unmeasured rows', () => {
|
|
129
|
+
expect((0, materialized_prep_time_read_helpers_1.filterMaterializedPrepTimeRowsByAnchor)(rows, 'MEASURED')).toHaveLength(2);
|
|
130
|
+
expect((0, materialized_prep_time_read_helpers_1.filterMaterializedPrepTimeRowsByAnchor)(rows, 'UNMEASURED')).toHaveLength(1);
|
|
131
|
+
expect((0, materialized_prep_time_read_helpers_1.filterMaterializedPrepTimeRowsByAnchor)(rows, null)).toHaveLength(3);
|
|
132
|
+
});
|
|
133
|
+
it('matches a case-insensitive item OR category substring', () => {
|
|
134
|
+
const byItem = (0, materialized_prep_time_read_helpers_1.applyMaterializedPrepTimeReadFilters)(rows, { search: 'BURGER' });
|
|
135
|
+
expect(byItem).toHaveLength(2);
|
|
136
|
+
const byCategory = (0, materialized_prep_time_read_helpers_1.applyMaterializedPrepTimeReadFilters)(rows, { search: 'drink' });
|
|
137
|
+
expect(byCategory).toHaveLength(1);
|
|
138
|
+
const none = (0, materialized_prep_time_read_helpers_1.applyMaterializedPrepTimeReadFilters)(rows, { search: 'pizza' });
|
|
139
|
+
expect(none).toHaveLength(0);
|
|
140
|
+
});
|
|
141
|
+
it('applies exact category, platform and status filters', () => {
|
|
142
|
+
expect((0, materialized_prep_time_read_helpers_1.applyMaterializedPrepTimeReadFilters)(rows, { category_names: ['Mains'] })).toHaveLength(2);
|
|
143
|
+
expect((0, materialized_prep_time_read_helpers_1.applyMaterializedPrepTimeReadFilters)(rows, { category_names: ['main'] })).toHaveLength(0);
|
|
144
|
+
expect((0, materialized_prep_time_read_helpers_1.applyMaterializedPrepTimeReadFilters)(rows, { platforms: ['WEB_DELIVERY'] })).toHaveLength(1);
|
|
145
|
+
expect((0, materialized_prep_time_read_helpers_1.applyMaterializedPrepTimeReadFilters)(rows, { order_statuses: ['CANCELLED'] })).toHaveLength(1);
|
|
146
|
+
});
|
|
147
|
+
it('late_only keeps measured graded late rows only (never fallback rows)', () => {
|
|
148
|
+
const late = (0, materialized_prep_time_read_helpers_1.applyMaterializedPrepTimeReadFilters)(rows, { late_only: true });
|
|
149
|
+
expect(late).toHaveLength(1);
|
|
150
|
+
expect(late[0].item_id).toBe('i1');
|
|
151
|
+
expect(late[0].delta_minutes).toBe(4);
|
|
152
|
+
expect((0, materialized_prep_time_read_helpers_1.isMaterializedPrepTimeLateRow)(row({ delta_minutes: 0 }))).toBe(false);
|
|
153
|
+
expect((0, materialized_prep_time_read_helpers_1.isMaterializedPrepTimeLateRow)(row({ delta_minutes: -2 }))).toBe(false);
|
|
154
|
+
expect((0, materialized_prep_time_read_helpers_1.isMaterializedPrepTimeLateRow)(row({ anchor: 'UNMEASURED', delta_minutes: 9 }))).toBe(false);
|
|
155
|
+
});
|
|
156
|
+
it('applies the anchor filter as a row filter', () => {
|
|
157
|
+
expect((0, materialized_prep_time_read_helpers_1.applyMaterializedPrepTimeReadFilters)(rows, { anchor: 'UNMEASURED' })).toHaveLength(1);
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
describe('prep time coverage', () => {
|
|
161
|
+
it('is measured / served, one decimal', () => {
|
|
162
|
+
const rows = [
|
|
163
|
+
row({ order_id: 'o1' }),
|
|
164
|
+
row({ order_id: 'o2' }),
|
|
165
|
+
row({ order_id: 'o3' }),
|
|
166
|
+
row({ order_id: 'o4', anchor: 'UNMEASURED', duration_minutes: null }),
|
|
167
|
+
];
|
|
168
|
+
expect((0, materialized_prep_time_read_helpers_1.buildMaterializedPrepTimeCoverage)(rows)).toEqual({
|
|
169
|
+
served_count: 4,
|
|
170
|
+
measured_count: 3,
|
|
171
|
+
unmeasured_count: 1,
|
|
172
|
+
coverage_percent: 75,
|
|
173
|
+
});
|
|
174
|
+
const twoOfThree = [
|
|
175
|
+
row({ order_id: 'o1' }),
|
|
176
|
+
row({ order_id: 'o2' }),
|
|
177
|
+
row({ order_id: 'o3', anchor: 'UNMEASURED', duration_minutes: null }),
|
|
178
|
+
];
|
|
179
|
+
expect((0, materialized_prep_time_read_helpers_1.buildMaterializedPrepTimeCoverage)(twoOfThree).coverage_percent).toBe(66.7);
|
|
180
|
+
});
|
|
181
|
+
it('is a valid zero block for an empty population', () => {
|
|
182
|
+
expect((0, materialized_prep_time_read_helpers_1.buildMaterializedPrepTimeCoverage)([])).toEqual({
|
|
183
|
+
served_count: 0,
|
|
184
|
+
measured_count: 0,
|
|
185
|
+
unmeasured_count: 0,
|
|
186
|
+
coverage_percent: 0,
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
describe('prep time duration statistics', () => {
|
|
191
|
+
it('computes exact interpolated percentiles', () => {
|
|
192
|
+
expect((0, materialized_prep_time_read_helpers_1.getMaterializedPrepTimePercentile)([], 0.5)).toBeNull();
|
|
193
|
+
expect((0, materialized_prep_time_read_helpers_1.getMaterializedPrepTimePercentile)([5], 0.5)).toBe(5);
|
|
194
|
+
expect((0, materialized_prep_time_read_helpers_1.getMaterializedPrepTimeMedian)([4, 1, 3, 2])).toBe(2.5);
|
|
195
|
+
expect((0, materialized_prep_time_read_helpers_1.getMaterializedPrepTimeP90)([1, 2, 3, 4])).toBe(3.7);
|
|
196
|
+
expect((0, materialized_prep_time_read_helpers_1.getMaterializedPrepTimePercentile)([1, 2, 3, 4], 1)).toBe(4);
|
|
197
|
+
expect((0, materialized_prep_time_read_helpers_1.getMaterializedPrepTimePercentile)([1, 2, 3, 4], 0)).toBe(1);
|
|
198
|
+
});
|
|
199
|
+
it('accepts only in-range, non-negative durations', () => {
|
|
200
|
+
expect((0, materialized_prep_time_read_helpers_1.isMaterializedPrepTimeDurationCountable)(0)).toBe(true);
|
|
201
|
+
expect((0, materialized_prep_time_read_helpers_1.isMaterializedPrepTimeDurationCountable)(MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_MAX_MINUTES)).toBe(true);
|
|
202
|
+
expect((0, materialized_prep_time_read_helpers_1.isMaterializedPrepTimeDurationCountable)(MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_MAX_MINUTES + 1)).toBe(false);
|
|
203
|
+
expect((0, materialized_prep_time_read_helpers_1.isMaterializedPrepTimeDurationCountable)(-1)).toBe(false);
|
|
204
|
+
expect((0, materialized_prep_time_read_helpers_1.isMaterializedPrepTimeDurationCountable)(null)).toBe(false);
|
|
205
|
+
expect((0, materialized_prep_time_read_helpers_1.isMaterializedPrepTimeDurationCountable)(Number.NaN)).toBe(false);
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
describe('prep time item grouping', () => {
|
|
209
|
+
it('groups by item_id and never inflates counts', () => {
|
|
210
|
+
const rows = [
|
|
211
|
+
row({ order_id: 'o1', item_id: 'i1', item_name: 'Burger' }),
|
|
212
|
+
row({ order_id: 'o2', item_id: 'i1', item_name: 'Burger', duration_minutes: 11 }),
|
|
213
|
+
row({ order_id: 'o2', item_id: 'i1', item_name: 'Burger', duration_minutes: 13 }),
|
|
214
|
+
row({
|
|
215
|
+
order_id: 'o3',
|
|
216
|
+
item_id: 'i2',
|
|
217
|
+
item_name: 'Cola',
|
|
218
|
+
anchor: 'UNMEASURED',
|
|
219
|
+
duration_minutes: null,
|
|
220
|
+
}),
|
|
221
|
+
];
|
|
222
|
+
const items = (0, materialized_prep_time_read_helpers_1.groupMaterializedPrepTimeItemRows)(rows);
|
|
223
|
+
expect(items.map((item) => item.item_id)).toEqual(['i1', 'i2']);
|
|
224
|
+
const burger = items[0];
|
|
225
|
+
expect(burger.orders_count).toBe(2);
|
|
226
|
+
expect(burger.served_count).toBe(3);
|
|
227
|
+
expect(burger.measured_count).toBe(3);
|
|
228
|
+
expect(burger.unmeasured_count).toBe(0);
|
|
229
|
+
expect(burger.avg_minutes).toBe(11);
|
|
230
|
+
expect(burger.median_minutes).toBe(11);
|
|
231
|
+
expect(burger.last_served_at).toBe('2026-09-10T10:00:00.000Z');
|
|
232
|
+
const cola = items[1];
|
|
233
|
+
expect(cola.served_count).toBe(1);
|
|
234
|
+
expect(cola.measured_count).toBe(0);
|
|
235
|
+
expect(cola.avg_minutes).toBeNull();
|
|
236
|
+
expect(cola.median_minutes).toBeNull();
|
|
237
|
+
expect(cola.p90_minutes).toBeNull();
|
|
238
|
+
expect(cola.on_time_percent).toBeNull();
|
|
239
|
+
expect(cola.last_served_at).toBe('2026-09-10T10:00:00.000Z');
|
|
240
|
+
});
|
|
241
|
+
it('groups by item_id even when two item ids share a name', () => {
|
|
242
|
+
const items = (0, materialized_prep_time_read_helpers_1.groupMaterializedPrepTimeItemRows)([
|
|
243
|
+
row({ item_id: 'i1', item_name: 'Same Name' }),
|
|
244
|
+
row({ item_id: 'i2', order_id: 'o2', item_name: 'Same Name' }),
|
|
245
|
+
]);
|
|
246
|
+
expect(items).toHaveLength(2);
|
|
247
|
+
});
|
|
248
|
+
it('keeps the newest non-empty name / category (deterministic)', () => {
|
|
249
|
+
const items = (0, materialized_prep_time_read_helpers_1.groupMaterializedPrepTimeItemRows)([
|
|
250
|
+
row({ created_at: '2026-09-10T08:00:00.000Z', item_name: 'Old', category_name: 'Old Cat' }),
|
|
251
|
+
row({ created_at: '2026-09-10T12:00:00.000Z', item_name: 'New', category_name: 'New Cat' }),
|
|
252
|
+
row({ created_at: '2026-09-10T13:00:00.000Z', item_name: null, category_name: null }),
|
|
253
|
+
]);
|
|
254
|
+
expect(items[0].item_name).toBe('New');
|
|
255
|
+
expect(items[0].category_name).toBe('New Cat');
|
|
256
|
+
});
|
|
257
|
+
it('excludes unmeasured rows from avg / median / p90 / on-time but counts coverage', () => {
|
|
258
|
+
const items = (0, materialized_prep_time_read_helpers_1.groupMaterializedPrepTimeItemRows)([
|
|
259
|
+
row({ delta_minutes: -1, duration_minutes: 9 }),
|
|
260
|
+
row({
|
|
261
|
+
order_id: 'o2',
|
|
262
|
+
anchor: 'UNMEASURED',
|
|
263
|
+
duration_minutes: 0,
|
|
264
|
+
delta_minutes: 500,
|
|
265
|
+
}),
|
|
266
|
+
]);
|
|
267
|
+
expect(items[0]).toMatchObject({
|
|
268
|
+
served_count: 2,
|
|
269
|
+
measured_count: 1,
|
|
270
|
+
unmeasured_count: 1,
|
|
271
|
+
avg_minutes: 9,
|
|
272
|
+
median_minutes: 9,
|
|
273
|
+
p90_minutes: 9,
|
|
274
|
+
on_time_count: 1,
|
|
275
|
+
late_count: 0,
|
|
276
|
+
on_time_percent: 100,
|
|
277
|
+
});
|
|
278
|
+
});
|
|
279
|
+
it('grades on time only when a target existed (delta present)', () => {
|
|
280
|
+
const items = (0, materialized_prep_time_read_helpers_1.groupMaterializedPrepTimeItemRows)([
|
|
281
|
+
row({ delta_minutes: null }),
|
|
282
|
+
row({ order_id: 'o2', delta_minutes: 0 }),
|
|
283
|
+
row({ order_id: 'o3', delta_minutes: 3 }),
|
|
284
|
+
]);
|
|
285
|
+
expect(items[0].on_time_count).toBe(1);
|
|
286
|
+
expect(items[0].late_count).toBe(1);
|
|
287
|
+
expect(items[0].on_time_percent).toBe(50);
|
|
288
|
+
});
|
|
289
|
+
it('excludes out-of-range durations from stats but keeps measured coverage', () => {
|
|
290
|
+
const items = (0, materialized_prep_time_read_helpers_1.groupMaterializedPrepTimeItemRows)([
|
|
291
|
+
row({ duration_minutes: 10 }),
|
|
292
|
+
row({ order_id: 'o2', duration_minutes: 30 }),
|
|
293
|
+
row({ order_id: 'o3', duration_minutes: MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_MAX_MINUTES + 1 }),
|
|
294
|
+
]);
|
|
295
|
+
expect(items[0]).toMatchObject({
|
|
296
|
+
measured_count: 3,
|
|
297
|
+
avg_minutes: 20,
|
|
298
|
+
median_minutes: 20,
|
|
299
|
+
p90_minutes: 28,
|
|
300
|
+
});
|
|
301
|
+
});
|
|
302
|
+
it('returns null (never 0) when every duration is out of range', () => {
|
|
303
|
+
const items = (0, materialized_prep_time_read_helpers_1.groupMaterializedPrepTimeItemRows)([
|
|
304
|
+
row({ duration_minutes: MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_MAX_MINUTES + 1 }),
|
|
305
|
+
]);
|
|
306
|
+
expect(items[0].measured_count).toBe(1);
|
|
307
|
+
expect(items[0].avg_minutes).toBeNull();
|
|
308
|
+
expect(items[0].median_minutes).toBeNull();
|
|
309
|
+
expect(items[0].p90_minutes).toBeNull();
|
|
310
|
+
});
|
|
311
|
+
it('filters before grouping, so coverage follows the filters', () => {
|
|
312
|
+
const page = (0, materialized_prep_time_read_helpers_1.buildMaterializedPrepTimeItemsPage)({
|
|
313
|
+
rows: [
|
|
314
|
+
row({ item_name: 'Burger' }),
|
|
315
|
+
row({ order_id: 'o2', item_id: 'i2', item_name: 'Cola' }),
|
|
316
|
+
row({
|
|
317
|
+
order_id: 'o3',
|
|
318
|
+
item_id: 'i3',
|
|
319
|
+
item_name: 'Fries',
|
|
320
|
+
anchor: 'UNMEASURED',
|
|
321
|
+
duration_minutes: null,
|
|
322
|
+
}),
|
|
323
|
+
],
|
|
324
|
+
filters: { search: 'cola' },
|
|
325
|
+
});
|
|
326
|
+
expect(page.rows).toHaveLength(1);
|
|
327
|
+
expect(page.coverage).toEqual({
|
|
328
|
+
served_count: 1,
|
|
329
|
+
measured_count: 1,
|
|
330
|
+
unmeasured_count: 0,
|
|
331
|
+
coverage_percent: 100,
|
|
332
|
+
});
|
|
333
|
+
});
|
|
334
|
+
it('keeps coverage on BOTH anchor populations while the ROWS follow the anchor view', () => {
|
|
335
|
+
// The item table is a Measured/Unmeasured view; the coverage banner must
|
|
336
|
+
// still state how much of the whole served population is observed.
|
|
337
|
+
const page = (0, materialized_prep_time_read_helpers_1.buildMaterializedPrepTimeItemsPage)({
|
|
338
|
+
rows: [
|
|
339
|
+
row({ item_id: 'i1', duration_minutes: 9 }),
|
|
340
|
+
row({
|
|
341
|
+
item_id: 'i2',
|
|
342
|
+
order_id: 'o2',
|
|
343
|
+
anchor: 'UNMEASURED',
|
|
344
|
+
duration_minutes: null,
|
|
345
|
+
}),
|
|
346
|
+
],
|
|
347
|
+
filters: { anchor: 'MEASURED' },
|
|
348
|
+
});
|
|
349
|
+
expect(page.rows.map((item) => item.item_id)).toEqual(['i1']);
|
|
350
|
+
expect(page.coverage).toEqual({
|
|
351
|
+
served_count: 2,
|
|
352
|
+
measured_count: 1,
|
|
353
|
+
unmeasured_count: 1,
|
|
354
|
+
coverage_percent: 50,
|
|
355
|
+
});
|
|
356
|
+
});
|
|
357
|
+
it('is a valid empty page', () => {
|
|
358
|
+
expect((0, materialized_prep_time_read_helpers_1.buildMaterializedPrepTimeItemsPage)({ rows: [] })).toEqual({
|
|
359
|
+
rows: [],
|
|
360
|
+
next_cursor: null,
|
|
361
|
+
coverage: {
|
|
362
|
+
served_count: 0,
|
|
363
|
+
measured_count: 0,
|
|
364
|
+
unmeasured_count: 0,
|
|
365
|
+
coverage_percent: 0,
|
|
366
|
+
},
|
|
367
|
+
});
|
|
368
|
+
});
|
|
369
|
+
});
|
|
370
|
+
describe('prep time item sorting + cursor paging', () => {
|
|
371
|
+
const items = () => [
|
|
372
|
+
{
|
|
373
|
+
item_id: 'i-a',
|
|
374
|
+
item_name: 'Zeta',
|
|
375
|
+
category_name: 'Mains',
|
|
376
|
+
orders_count: 2,
|
|
377
|
+
served_count: 3,
|
|
378
|
+
measured_count: 2,
|
|
379
|
+
unmeasured_count: 1,
|
|
380
|
+
avg_minutes: 12,
|
|
381
|
+
median_minutes: 12,
|
|
382
|
+
p90_minutes: 20,
|
|
383
|
+
on_time_count: 2,
|
|
384
|
+
late_count: 1,
|
|
385
|
+
on_time_percent: 66.7,
|
|
386
|
+
last_served_at: '2026-09-10T10:00:00.000Z',
|
|
387
|
+
},
|
|
388
|
+
{
|
|
389
|
+
item_id: 'i-b',
|
|
390
|
+
item_name: 'alpha',
|
|
391
|
+
category_name: null,
|
|
392
|
+
orders_count: 1,
|
|
393
|
+
served_count: 1,
|
|
394
|
+
measured_count: 0,
|
|
395
|
+
unmeasured_count: 1,
|
|
396
|
+
avg_minutes: null,
|
|
397
|
+
median_minutes: null,
|
|
398
|
+
p90_minutes: null,
|
|
399
|
+
on_time_count: 0,
|
|
400
|
+
late_count: 0,
|
|
401
|
+
on_time_percent: null,
|
|
402
|
+
last_served_at: null,
|
|
403
|
+
},
|
|
404
|
+
{
|
|
405
|
+
item_id: 'i-c',
|
|
406
|
+
item_name: 'Beta',
|
|
407
|
+
category_name: 'Mains',
|
|
408
|
+
orders_count: 1,
|
|
409
|
+
served_count: 4,
|
|
410
|
+
measured_count: 4,
|
|
411
|
+
unmeasured_count: 0,
|
|
412
|
+
avg_minutes: 8,
|
|
413
|
+
median_minutes: 8,
|
|
414
|
+
p90_minutes: 10,
|
|
415
|
+
on_time_count: 3,
|
|
416
|
+
late_count: 1,
|
|
417
|
+
on_time_percent: 75,
|
|
418
|
+
last_served_at: '2026-09-10T11:00:00.000Z',
|
|
419
|
+
},
|
|
420
|
+
];
|
|
421
|
+
it('sorts every mode with item_id as the final tie-break', () => {
|
|
422
|
+
const expected = {
|
|
423
|
+
late_desc: ['i-a', 'i-c', 'i-b'],
|
|
424
|
+
avg_desc: ['i-a', 'i-c', 'i-b'],
|
|
425
|
+
avg_asc: ['i-c', 'i-a', 'i-b'],
|
|
426
|
+
p90_desc: ['i-a', 'i-c', 'i-b'],
|
|
427
|
+
on_time_asc: ['i-a', 'i-c', 'i-b'],
|
|
428
|
+
name_asc: ['i-b', 'i-c', 'i-a'],
|
|
429
|
+
};
|
|
430
|
+
for (const sort of materialized_reports_apis_1.ALL_MATERIALIZED_PREP_TIME_ITEM_SORTS) {
|
|
431
|
+
expect((0, materialized_prep_time_read_helpers_1.sortMaterializedPrepTimeItemRows)(items(), sort).map((item) => item.item_id)).toEqual(expected[sort]);
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
it('breaks exact sort-value ties by item_id ascending', () => {
|
|
435
|
+
const tied = [
|
|
436
|
+
items()[0],
|
|
437
|
+
{ ...items()[0], item_id: 'i-0' },
|
|
438
|
+
{ ...items()[0], item_id: 'i-z' },
|
|
439
|
+
];
|
|
440
|
+
expect((0, materialized_prep_time_read_helpers_1.sortMaterializedPrepTimeItemRows)(tied, 'late_desc').map((item) => item.item_id)).toEqual(['i-0', 'i-a', 'i-z']);
|
|
441
|
+
});
|
|
442
|
+
it('never mutates the input rows when sorting', () => {
|
|
443
|
+
const input = items();
|
|
444
|
+
const before = input.map((item) => item.item_id);
|
|
445
|
+
(0, materialized_prep_time_read_helpers_1.sortMaterializedPrepTimeItemRows)(input, 'name_asc');
|
|
446
|
+
expect(input.map((item) => item.item_id)).toEqual(before);
|
|
447
|
+
});
|
|
448
|
+
it('pages every sort without duplicating or skipping a row', () => {
|
|
449
|
+
for (const sort of materialized_reports_apis_1.ALL_MATERIALIZED_PREP_TIME_ITEM_SORTS) {
|
|
450
|
+
const sorted = (0, materialized_prep_time_read_helpers_1.sortMaterializedPrepTimeItemRows)(items(), sort);
|
|
451
|
+
const seen = [];
|
|
452
|
+
let cursor = null;
|
|
453
|
+
for (let page = 0; page < 10; page += 1) {
|
|
454
|
+
const result = (0, materialized_prep_time_read_helpers_1.getMaterializedPrepTimeItemPage)(sorted, sort, cursor, 1);
|
|
455
|
+
seen.push(...result.rows.map((item) => item.item_id));
|
|
456
|
+
if (result.next_cursor == null)
|
|
457
|
+
break;
|
|
458
|
+
cursor = result.next_cursor;
|
|
459
|
+
}
|
|
460
|
+
expect(seen).toEqual(sorted.map((item) => item.item_id));
|
|
461
|
+
expect(new Set(seen).size).toBe(seen.length);
|
|
462
|
+
}
|
|
463
|
+
});
|
|
464
|
+
it('exposes the same sort value the cursor stores', () => {
|
|
465
|
+
const first = items()[0];
|
|
466
|
+
expect((0, materialized_prep_time_read_helpers_1.getMaterializedPrepTimeItemSortValue)(first, 'late_desc')).toBe(1);
|
|
467
|
+
expect((0, materialized_prep_time_read_helpers_1.getMaterializedPrepTimeItemSortValue)(first, 'avg_asc')).toBe(12);
|
|
468
|
+
expect((0, materialized_prep_time_read_helpers_1.getMaterializedPrepTimeItemSortValue)(first, 'name_asc')).toBe('Zeta');
|
|
469
|
+
expect((0, materialized_prep_time_read_helpers_1.compareMaterializedPrepTimeItemRows)(first, first, 'avg_desc')).toBe(0);
|
|
470
|
+
});
|
|
471
|
+
it('returns a null next_cursor on the final page', () => {
|
|
472
|
+
const sorted = (0, materialized_prep_time_read_helpers_1.sortMaterializedPrepTimeItemRows)(items(), 'late_desc');
|
|
473
|
+
const page = (0, materialized_prep_time_read_helpers_1.getMaterializedPrepTimeItemPage)(sorted, 'late_desc', null, 50);
|
|
474
|
+
expect(page.next_cursor).toBeNull();
|
|
475
|
+
expect(page.rows).toHaveLength(3);
|
|
476
|
+
});
|
|
477
|
+
});
|
|
478
|
+
describe('prep time categories', () => {
|
|
479
|
+
const rows = [
|
|
480
|
+
row({ item_id: 'i1', category_name: 'Mains', duration_minutes: 10 }),
|
|
481
|
+
row({ order_id: 'o2', item_id: 'i1', category_name: 'Mains', duration_minutes: 20 }),
|
|
482
|
+
row({ order_id: 'o3', item_id: 'i2', category_name: 'Drinks', duration_minutes: 4 }),
|
|
483
|
+
row({
|
|
484
|
+
order_id: 'o4',
|
|
485
|
+
item_id: 'i3',
|
|
486
|
+
category_name: 'Drinks',
|
|
487
|
+
anchor: 'UNMEASURED',
|
|
488
|
+
duration_minutes: null,
|
|
489
|
+
}),
|
|
490
|
+
row({ order_id: 'o5', item_id: 'i4', category_name: null }),
|
|
491
|
+
];
|
|
492
|
+
it('groups by exact category, excludes unattributed rows, and sorts by measured desc then name', () => {
|
|
493
|
+
const result = (0, materialized_prep_time_read_helpers_1.buildMaterializedPrepTimeCategoriesResult)({ rows });
|
|
494
|
+
expect(result.rows.map((category) => category.category_name)).toEqual(['Mains', 'Drinks']);
|
|
495
|
+
expect(result.rows[0]).toMatchObject({
|
|
496
|
+
served_count: 2,
|
|
497
|
+
measured_count: 2,
|
|
498
|
+
unmeasured_count: 0,
|
|
499
|
+
avg_minutes: 15,
|
|
500
|
+
median_minutes: 15,
|
|
501
|
+
p90_minutes: 19,
|
|
502
|
+
});
|
|
503
|
+
expect(result.rows[1]).toMatchObject({
|
|
504
|
+
served_count: 2,
|
|
505
|
+
measured_count: 1,
|
|
506
|
+
unmeasured_count: 1,
|
|
507
|
+
avg_minutes: 4,
|
|
508
|
+
median_minutes: 4,
|
|
509
|
+
p90_minutes: 4,
|
|
510
|
+
});
|
|
511
|
+
// The null-category row is not attributable and is excluded from this read.
|
|
512
|
+
expect(result.coverage).toEqual({
|
|
513
|
+
served_count: 4,
|
|
514
|
+
measured_count: 3,
|
|
515
|
+
unmeasured_count: 1,
|
|
516
|
+
coverage_percent: 75,
|
|
517
|
+
});
|
|
518
|
+
});
|
|
519
|
+
it('applies filters before grouping (category_names can be ignored by the parser)', () => {
|
|
520
|
+
const result = (0, materialized_prep_time_read_helpers_1.buildMaterializedPrepTimeCategoriesResult)({
|
|
521
|
+
rows,
|
|
522
|
+
filters: { search: 'drink' },
|
|
523
|
+
});
|
|
524
|
+
expect(result.rows).toHaveLength(1);
|
|
525
|
+
expect(result.rows[0].category_name).toBe('Drinks');
|
|
526
|
+
expect(result.coverage.served_count).toBe(2);
|
|
527
|
+
});
|
|
528
|
+
it('returns null stats for a measured-empty category', () => {
|
|
529
|
+
const result = (0, materialized_prep_time_read_helpers_1.buildMaterializedPrepTimeCategoriesResult)({
|
|
530
|
+
rows: [
|
|
531
|
+
row({ anchor: 'UNMEASURED', duration_minutes: null, category_name: 'Cold' }),
|
|
532
|
+
],
|
|
533
|
+
});
|
|
534
|
+
expect(result.rows[0].avg_minutes).toBeNull();
|
|
535
|
+
expect(result.rows[0].median_minutes).toBeNull();
|
|
536
|
+
expect(result.rows[0].p90_minutes).toBeNull();
|
|
537
|
+
expect(result.rows[0].on_time_percent).toBeNull();
|
|
538
|
+
});
|
|
539
|
+
it('is a valid empty result', () => {
|
|
540
|
+
expect((0, materialized_prep_time_read_helpers_1.groupMaterializedPrepTimeCategoryRows)([])).toEqual([]);
|
|
541
|
+
expect((0, materialized_prep_time_read_helpers_1.buildMaterializedPrepTimeCategoriesResult)({ rows: [] }).coverage.served_count).toBe(0);
|
|
542
|
+
});
|
|
543
|
+
});
|
|
544
|
+
describe('prep time series', () => {
|
|
545
|
+
const rows = [
|
|
546
|
+
row({ item_id: 'i1', source_local_date: '2026-09-11', duration_minutes: 10 }),
|
|
547
|
+
row({
|
|
548
|
+
item_id: 'i2',
|
|
549
|
+
order_id: 'o2',
|
|
550
|
+
source_local_date: '2026-09-11',
|
|
551
|
+
duration_minutes: 20,
|
|
552
|
+
}),
|
|
553
|
+
row({
|
|
554
|
+
item_id: 'i1',
|
|
555
|
+
order_id: 'o3',
|
|
556
|
+
source_local_date: '2026-09-10',
|
|
557
|
+
duration_minutes: 6,
|
|
558
|
+
}),
|
|
559
|
+
row({
|
|
560
|
+
item_id: 'i3',
|
|
561
|
+
order_id: 'o4',
|
|
562
|
+
source_local_date: '2026-09-10',
|
|
563
|
+
anchor: 'UNMEASURED',
|
|
564
|
+
duration_minutes: null,
|
|
565
|
+
// created_at is a different UTC day; the SOURCE local date must win.
|
|
566
|
+
created_at: '2026-09-11T01:00:00.000Z',
|
|
567
|
+
}),
|
|
568
|
+
];
|
|
569
|
+
it('groups by source local date ascending and never emits empty dates', () => {
|
|
570
|
+
const result = (0, materialized_prep_time_read_helpers_1.buildMaterializedPrepTimeSeriesResult)({ rows });
|
|
571
|
+
expect(result.points.map((point) => point.local_date)).toEqual([
|
|
572
|
+
'2026-09-10',
|
|
573
|
+
'2026-09-11',
|
|
574
|
+
]);
|
|
575
|
+
expect(result.points[0]).toMatchObject({
|
|
576
|
+
served_count: 2,
|
|
577
|
+
measured_count: 1,
|
|
578
|
+
unmeasured_count: 1,
|
|
579
|
+
avg_minutes: 6,
|
|
580
|
+
median_minutes: 6,
|
|
581
|
+
p90_minutes: 6,
|
|
582
|
+
});
|
|
583
|
+
expect(result.points[1]).toMatchObject({
|
|
584
|
+
served_count: 2,
|
|
585
|
+
measured_count: 2,
|
|
586
|
+
unmeasured_count: 0,
|
|
587
|
+
avg_minutes: 15,
|
|
588
|
+
median_minutes: 15,
|
|
589
|
+
p90_minutes: 19,
|
|
590
|
+
});
|
|
591
|
+
expect(result.coverage).toEqual({
|
|
592
|
+
served_count: 4,
|
|
593
|
+
measured_count: 3,
|
|
594
|
+
unmeasured_count: 1,
|
|
595
|
+
coverage_percent: 75,
|
|
596
|
+
});
|
|
597
|
+
expect(result.target_minutes).toBeNull();
|
|
598
|
+
});
|
|
599
|
+
it('applies filters before grouping', () => {
|
|
600
|
+
const result = (0, materialized_prep_time_read_helpers_1.buildMaterializedPrepTimeSeriesResult)({
|
|
601
|
+
rows,
|
|
602
|
+
filters: { platforms: ['POS_WALK-IN'] },
|
|
603
|
+
});
|
|
604
|
+
expect(result.points).toHaveLength(2);
|
|
605
|
+
const filtered = (0, materialized_prep_time_read_helpers_1.buildMaterializedPrepTimeSeriesResult)({
|
|
606
|
+
rows,
|
|
607
|
+
filters: { search: 'nothing-matches' },
|
|
608
|
+
});
|
|
609
|
+
expect(filtered.points).toEqual([]);
|
|
610
|
+
expect(filtered.coverage.served_count).toBe(0);
|
|
611
|
+
});
|
|
612
|
+
it('skips rows without a real source date', () => {
|
|
613
|
+
const result = (0, materialized_prep_time_read_helpers_1.groupMaterializedPrepTimeSeriesPoints)([
|
|
614
|
+
row({ source_local_date: '' }),
|
|
615
|
+
row({ order_id: 'o2', source_local_date: 'not-a-date' }),
|
|
616
|
+
row({ order_id: 'o3', source_local_date: '2026-09-10' }),
|
|
617
|
+
]);
|
|
618
|
+
expect(result.map((point) => point.local_date)).toEqual(['2026-09-10']);
|
|
619
|
+
});
|
|
620
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|