@jsb188/mday 1.1.10 → 1.1.11

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.
Files changed (119) hide show
  1. package/package.json +105 -54
  2. package/src/constants/agentDoc.ts +12 -0
  3. package/src/constants/agentMemory.ts +154 -0
  4. package/src/constants/aiChat.ts +5 -0
  5. package/src/constants/campaign.ts +127 -0
  6. package/src/constants/dataTable.ts +35 -9
  7. package/src/constants/dataTableFieldTypes.generated.ts +63 -0
  8. package/src/constants/docAccess.ts +32 -0
  9. package/src/constants/email.ts +20 -4
  10. package/src/constants/fieldDoc.ts +34 -0
  11. package/src/constants/graphqlReactivity.ts +113 -0
  12. package/src/constants/integration.ts +8 -9
  13. package/src/constants/log.ts +7 -410
  14. package/src/constants/logActivityGroupColors.generated.ts +19 -0
  15. package/src/constants/logTaxonomy.generated.ts +1103 -0
  16. package/src/constants/noteDoc.ts +77 -0
  17. package/src/constants/organization.ts +13 -15
  18. package/src/constants/pageRoute.ts +23 -0
  19. package/src/constants/schedule.ts +220 -0
  20. package/src/constants/scheduleRuleTemplateSeed.ts +138 -0
  21. package/src/constants/scheduleRuleTemplates.generated.ts +145 -0
  22. package/src/constants/sheet.ts +154 -6
  23. package/src/constants/sheetRegionSources.generated.ts +578 -0
  24. package/src/constants/square.ts +6 -6
  25. package/src/constants/storage.ts +56 -7
  26. package/src/constants/usage.ts +79 -0
  27. package/src/schemas/editLogEntry.ts +1010 -777
  28. package/src/schemas/fieldDocsQuery.ts +12 -0
  29. package/src/schemas/inboundContact.ts +60 -55
  30. package/src/schemas/logsQuery.ts +55 -49
  31. package/src/schemas/organization.ts +49 -46
  32. package/src/types/account.d.ts +4 -1
  33. package/src/types/agentDoc.d.ts +64 -0
  34. package/src/types/agentMemory.d.ts +74 -0
  35. package/src/types/campaign.d.ts +683 -0
  36. package/src/types/dataTable.d.ts +175 -256
  37. package/src/types/docAccess.d.ts +96 -0
  38. package/src/types/email.d.ts +83 -12
  39. package/src/types/fieldDoc.d.ts +273 -0
  40. package/src/types/inboundContact.d.ts +2 -1
  41. package/src/types/log.d.ts +209 -341
  42. package/src/types/noteDoc.d.ts +86 -0
  43. package/src/types/organization.d.ts +83 -40
  44. package/src/types/pageRoute.d.ts +48 -0
  45. package/src/types/schedule.d.ts +391 -0
  46. package/src/types/sheet.d.ts +862 -30
  47. package/src/types/storage.d.ts +206 -125
  48. package/src/types/usage.d.ts +106 -0
  49. package/src/types/workflow.d.ts +149 -18
  50. package/src/utils/agentDoc.ts +109 -0
  51. package/src/utils/aiChat.ts +26 -0
  52. package/src/utils/campaign.ts +741 -0
  53. package/src/utils/campaignFormEditor.ts +871 -0
  54. package/src/utils/dataTable.ts +425 -410
  55. package/src/utils/dataTableFieldTypeRegistry.ts +105 -0
  56. package/src/utils/dataTableRealtimeAuthority.ts +209 -0
  57. package/src/utils/dataTableRealtimeSelection.ts +134 -0
  58. package/src/utils/dataTableRowDocument.ts +508 -0
  59. package/src/utils/dataTableSource.ts +230 -0
  60. package/src/utils/docAccess.ts +383 -0
  61. package/src/utils/documentName.ts +53 -0
  62. package/src/utils/email.ts +112 -53
  63. package/src/utils/fieldDoc.ts +113 -0
  64. package/src/utils/log.ts +169 -176
  65. package/src/utils/logRegistryValue.ts +90 -0
  66. package/src/utils/logStoreRead.ts +266 -0
  67. package/src/utils/logTaxonomy.ts +76 -0
  68. package/src/utils/logTaxonomyDisplay.ts +439 -0
  69. package/src/utils/noteDoc.ts +827 -0
  70. package/src/utils/organization.ts +510 -91
  71. package/src/utils/pageRoute.ts +71 -0
  72. package/src/utils/schedule.ts +799 -0
  73. package/src/utils/scheduleEvaluation.ts +1994 -0
  74. package/src/utils/scheduleICS.ts +246 -0
  75. package/src/utils/scheduleRuleTemplates.ts +188 -0
  76. package/src/utils/sheet.ts +4842 -39
  77. package/src/utils/sheetAxisMove.ts +830 -0
  78. package/src/utils/sheetBootstrap.ts +1769 -0
  79. package/src/utils/sheetCommand.ts +423 -0
  80. package/src/utils/sheetExport.ts +238 -0
  81. package/src/utils/sheetFormulaEvaluation.ts +914 -0
  82. package/src/utils/sheetFormulaEvaluator.ts +852 -0
  83. package/src/utils/sheetJournal.ts +78 -0
  84. package/src/utils/sheetNumberFormat.ts +1211 -0
  85. package/src/utils/sheetRegion.ts +374 -0
  86. package/src/utils/sheetRegionFilter.ts +1259 -0
  87. package/src/utils/sheetRegionSourceRegistry.ts +594 -0
  88. package/src/utils/sheetSequence.ts +148 -0
  89. package/src/utils/sheetSnapshot.ts +992 -0
  90. package/src/utils/sheetStructureEdit.ts +1284 -0
  91. package/src/utils/square.ts +22 -12
  92. package/src/utils/storage.ts +152 -0
  93. package/src/utils/table-route.ts +127 -11
  94. package/src/utils/workflow.ts +191 -23
  95. package/tests/dataTableFieldTypeRegistry.test.ts +110 -0
  96. package/tests/docAccess.test.ts +525 -0
  97. package/tests/editLogEntry.test.ts +112 -0
  98. package/tests/noteDoc.test.ts +669 -0
  99. package/tests/schedule.test.ts +693 -0
  100. package/tests/scheduleEvaluation.test.ts +1970 -0
  101. package/tests/scheduleICS.test.ts +208 -0
  102. package/tests/scheduleRuleTemplates.test.ts +176 -0
  103. package/tests/scheduleSweepInvariants.test.ts +428 -0
  104. package/tests/sheetAxisMove.test.ts +343 -0
  105. package/tests/sheetFormulaEvaluator.test.ts +575 -0
  106. package/tests/sheetRegionFilter.test.ts +1135 -0
  107. package/tests/sheetSequence.test.ts +41 -0
  108. package/tests/sheetStructureEdit.test.ts +82 -0
  109. package/tsconfig.json +10 -10
  110. package/vitest.config.ts +8 -0
  111. package/src/constants/log-activityTexts.ts +0 -235
  112. package/src/constants/product.ts +0 -42
  113. package/src/constants/report.ts +0 -44
  114. package/src/schemas/productsQuery.ts +0 -53
  115. package/src/schemas/reportsQuery.ts +0 -20
  116. package/src/types/product.d.ts +0 -84
  117. package/src/types/report.d.ts +0 -290
  118. package/src/utils/product-calEvent.ts +0 -455
  119. package/src/utils/report.ts +0 -158
@@ -0,0 +1,1135 @@
1
+ import {
2
+ evaluateSheetRegionSourceFilterCondition,
3
+ evaluateSheetRegionSourceFilterGroup,
4
+ getSheetRegionFilterComparableDateFromText,
5
+ getSheetRegionFilterComparableNumberFromText,
6
+ getSheetRegionFilterComparableValueFromRecord,
7
+ getSheetRegionFilterContainsLikePattern,
8
+ getSheetRegionFilterFieldKind,
9
+ getSheetRegionFilterFieldType,
10
+ getSheetRegionFilterOperatorByKeyword,
11
+ getSheetRegionFilterOperatorKeywords,
12
+ getSheetRegionFilterTemporalSQL,
13
+ isSheetRegionFilterDateOnlyValue,
14
+ isSheetRegionFilterOperatorCompatible,
15
+ isSheetRegionFilterRecordStorageNonEmpty,
16
+ isSheetRegionFilterSQLOnlyDateLikeText,
17
+ isSheetRegionFilterTextFieldType,
18
+ normalizeSheetRegionFilterBooleanValue,
19
+ normalizeSheetRegionFilterDateTimeValue,
20
+ normalizeSheetRegionFilterDateValue,
21
+ normalizeSheetRegionFilterNumberValue,
22
+ normalizeSheetRegionFilterTextValue,
23
+ normalizeSheetRegionFilterTextValues,
24
+ SHEET_REGION_FILTER_OPERATOR_DEFS,
25
+ type SheetRegionFilterComparableValue,
26
+ type SheetRegionFilterSQLColumnExprs,
27
+ } from '@jsb188/mday/utils/sheetRegionFilter.ts';
28
+ import { SHEET_REGION_SOURCE_FILTER_OPERATOR_ENUMS } from '@jsb188/mday/constants/sheet.ts';
29
+ import { describe, expect, it } from 'vitest';
30
+
31
+ /* Literal copy of the historical operator x fieldType truth table. */
32
+ const COMPATIBILITY_MATRIX: Record<string, string[]> = {
33
+ TEXT: ['CONTAINS', 'EQUALS', 'IN', 'IS_EMPTY'],
34
+ ID: ['CONTAINS', 'EQUALS', 'IN', 'IS_EMPTY'],
35
+ ID_OR_TEXT: ['CONTAINS', 'EQUALS', 'IN', 'IS_EMPTY'],
36
+ SELECT: ['CONTAINS', 'EQUALS', 'IN', 'IS_EMPTY'],
37
+ SELECT_OR_TEXT: ['CONTAINS', 'EQUALS', 'IN', 'IS_EMPTY'],
38
+ NUMBER: ['EQUALS', 'GT', 'GTE', 'LT', 'LTE', 'IS_EMPTY'],
39
+ PRICE: ['EQUALS', 'GT', 'GTE', 'LT', 'LTE', 'IS_EMPTY'],
40
+ DATE: [
41
+ 'EQUALS',
42
+ 'BEFORE',
43
+ 'AFTER',
44
+ 'ON_OR_BEFORE',
45
+ 'ON_OR_AFTER',
46
+ 'IS_EMPTY',
47
+ ],
48
+ WEEK_OF_MON: [
49
+ 'EQUALS',
50
+ 'BEFORE',
51
+ 'AFTER',
52
+ 'ON_OR_BEFORE',
53
+ 'ON_OR_AFTER',
54
+ 'IS_EMPTY',
55
+ ],
56
+ WEEK_OF_SUN: [
57
+ 'EQUALS',
58
+ 'BEFORE',
59
+ 'AFTER',
60
+ 'ON_OR_BEFORE',
61
+ 'ON_OR_AFTER',
62
+ 'IS_EMPTY',
63
+ ],
64
+ DATETIME: [
65
+ 'EQUALS',
66
+ 'BEFORE',
67
+ 'AFTER',
68
+ 'ON_OR_BEFORE',
69
+ 'ON_OR_AFTER',
70
+ 'IS_EMPTY',
71
+ ],
72
+ BOOLEAN: ['EQUALS', 'IS_EMPTY'],
73
+ MULTI_SELECT: ['CONTAINS', 'CONTAINS_ANY', 'IS_EMPTY'],
74
+ };
75
+
76
+ const SELECT_CELL = {
77
+ fieldType: 'SELECT',
78
+ humanFieldType: null,
79
+ options: [
80
+ { value: 'RED', label: 'Red' },
81
+ { value: 'BLUE', label: 'Blue' },
82
+ ],
83
+ } as never;
84
+
85
+ const MULTI_CELL = {
86
+ fieldType: 'MULTI_SELECT',
87
+ humanFieldType: null,
88
+ options: [
89
+ { value: 'RED', label: 'Red' },
90
+ { value: 'BLUE', label: 'Blue' },
91
+ ],
92
+ } as never;
93
+
94
+ const TEXT_CELL = {
95
+ fieldType: 'TEXT',
96
+ humanFieldType: null,
97
+ options: [],
98
+ } as never;
99
+
100
+ /* Build one comparable value with a kind + partial fields. */
101
+ function comparable(
102
+ kind: SheetRegionFilterComparableValue['kind'],
103
+ fields: Partial<SheetRegionFilterComparableValue> = {},
104
+ ): SheetRegionFilterComparableValue {
105
+ return { kind, ...fields };
106
+ }
107
+
108
+ /* Run one operator tsPredicate directly. */
109
+ function runPredicate(
110
+ operator: string,
111
+ fieldValue: SheetRegionFilterComparableValue,
112
+ condition: Record<string, unknown>,
113
+ ) {
114
+ return SHEET_REGION_FILTER_OPERATOR_DEFS[
115
+ operator as keyof typeof SHEET_REGION_FILTER_OPERATOR_DEFS
116
+ ].tsPredicate(fieldValue, {
117
+ cellKey: 'col',
118
+ operator,
119
+ ...condition,
120
+ } as never);
121
+ }
122
+
123
+ describe('operator def table', () => {
124
+ it('covers every filter operator enum exactly once', () => {
125
+ expect(Object.keys(SHEET_REGION_FILTER_OPERATOR_DEFS).sort()).toEqual(
126
+ [...SHEET_REGION_SOURCE_FILTER_OPERATOR_ENUMS].sort(),
127
+ );
128
+ });
129
+
130
+ it('reproduces the historical compatibility truth table exactly', () => {
131
+ for (const [fieldType, operators] of Object.entries(COMPATIBILITY_MATRIX)) {
132
+ for (const operator of SHEET_REGION_SOURCE_FILTER_OPERATOR_ENUMS) {
133
+ expect(
134
+ isSheetRegionFilterOperatorCompatible(operator, fieldType as never),
135
+ `${operator} x ${fieldType}`,
136
+ ).toBe(operators.includes(operator));
137
+ }
138
+ }
139
+ });
140
+
141
+ it('rejects every operator except IS_EMPTY for unknown field types', () => {
142
+ for (const operator of SHEET_REGION_SOURCE_FILTER_OPERATOR_ENUMS) {
143
+ expect(isSheetRegionFilterOperatorCompatible(operator, null)).toBe(
144
+ operator === 'IS_EMPTY',
145
+ );
146
+ expect(
147
+ isSheetRegionFilterOperatorCompatible(operator, 'FORMULA' as never),
148
+ ).toBe(operator === 'IS_EMPTY');
149
+ }
150
+ });
151
+
152
+ it('pins the exact DSL keyword list (IS_EMPTY stays excluded)', () => {
153
+ expect(getSheetRegionFilterOperatorKeywords()).toEqual([
154
+ 'CONTAINS',
155
+ 'EQUALS',
156
+ 'IN',
157
+ 'GT',
158
+ 'GTE',
159
+ 'LT',
160
+ 'LTE',
161
+ 'BEFORE',
162
+ 'AFTER',
163
+ 'ON_OR_BEFORE',
164
+ 'ON_OR_AFTER',
165
+ 'CONTAINS_ANY',
166
+ ]);
167
+ expect(getSheetRegionFilterOperatorByKeyword().IS_EMPTY).toBeUndefined();
168
+ expect(getSheetRegionFilterOperatorByKeyword().CONTAINS).toBe('CONTAINS');
169
+ });
170
+
171
+ it('keeps the historical explanation and canonical query text maps', () => {
172
+ const expectations: Record<string, [string, string]> = {
173
+ AFTER: ['is after', 'AFTER'],
174
+ BEFORE: ['is before', 'BEFORE'],
175
+ CONTAINS: ['contains', 'CONTAINS'],
176
+ CONTAINS_ANY: ['contains any of', 'CONTAINS_ANY'],
177
+ EQUALS: ['equals', '='],
178
+ GT: ['is greater than', '>'],
179
+ GTE: ['is greater than or equal to', '>='],
180
+ IN: ['is one of', 'IN'],
181
+ IS_EMPTY: ['is empty', 'IS EMPTY'],
182
+ LT: ['is less than', '<'],
183
+ LTE: ['is less than or equal to', '<='],
184
+ ON_OR_AFTER: ['is on or after', '>='],
185
+ ON_OR_BEFORE: ['is on or before', '<='],
186
+ };
187
+ for (const [
188
+ operator,
189
+ [explanationText, canonicalQueryText],
190
+ ] of Object.entries(expectations)) {
191
+ const def = SHEET_REGION_FILTER_OPERATOR_DEFS[operator as never];
192
+ expect(def.explanationText).toBe(explanationText);
193
+ expect(def.canonicalQueryText).toBe(canonicalQueryText);
194
+ }
195
+ });
196
+ });
197
+
198
+ describe('field type helpers', () => {
199
+ it('uses the canonical fieldType', () => {
200
+ expect(
201
+ getSheetRegionFilterFieldType({
202
+ fieldType: 'ID_OR_TEXT',
203
+ humanFieldType: 'DATE',
204
+ } as never),
205
+ ).toBe('ID_OR_TEXT');
206
+ expect(
207
+ getSheetRegionFilterFieldType({
208
+ fieldType: 'TEXT',
209
+ humanFieldType: null,
210
+ } as never),
211
+ ).toBe('TEXT');
212
+ });
213
+
214
+ it('classifies field kinds like the historical if-chains', () => {
215
+ expect(isSheetRegionFilterTextFieldType('SELECT_OR_TEXT')).toBe(true);
216
+ expect(isSheetRegionFilterTextFieldType('MULTI_SELECT')).toBe(false);
217
+ expect(getSheetRegionFilterFieldKind('ID')).toBe('TEXT');
218
+ expect(getSheetRegionFilterFieldKind('PRICE')).toBe('NUMBER');
219
+ expect(getSheetRegionFilterFieldKind('WEEK_OF_SUN')).toBe('DATE');
220
+ expect(getSheetRegionFilterFieldKind('DATETIME')).toBe('DATETIME');
221
+ expect(getSheetRegionFilterFieldKind('BOOLEAN')).toBe('BOOLEAN');
222
+ expect(getSheetRegionFilterFieldKind('MULTI_SELECT')).toBe('MULTI_SELECT');
223
+ expect(getSheetRegionFilterFieldKind('FORMULA' as never)).toBe(null);
224
+ expect(getSheetRegionFilterFieldKind(null)).toBe(null);
225
+ });
226
+ });
227
+
228
+ describe('tsPredicate semantics', () => {
229
+ it('compares text case-insensitively with COALESCE null semantics', () => {
230
+ expect(
231
+ runPredicate('EQUALS', comparable('TEXT', { text: 'ABC' }), {
232
+ textValue: 'abc',
233
+ }),
234
+ ).toBe(true);
235
+ expect(
236
+ runPredicate('EQUALS', comparable('TEXT', { text: null }), {
237
+ textValue: '',
238
+ }),
239
+ ).toBe(true);
240
+ expect(
241
+ runPredicate('EQUALS', comparable('TEXT', { text: 'x' }), {
242
+ textValue: '',
243
+ }),
244
+ ).toBe(false);
245
+ expect(
246
+ runPredicate('EQUALS', comparable('TEXT', { text: 'ünïcode' }), {
247
+ textValue: 'ÜNÏCODE',
248
+ }),
249
+ ).toBe(true);
250
+ });
251
+
252
+ it('treats CONTAINS as literal case-insensitive substring for text', () => {
253
+ expect(
254
+ runPredicate('CONTAINS', comparable('TEXT', { text: 'Fresh Basil' }), {
255
+ textValue: 'basil',
256
+ }),
257
+ ).toBe(true);
258
+ expect(
259
+ runPredicate('CONTAINS', comparable('TEXT', { text: '50% off' }), {
260
+ textValue: '50%',
261
+ }),
262
+ ).toBe(true);
263
+ // Literal substring: metachars are not wildcards in the TS meaning (DV-6)
264
+ expect(
265
+ runPredicate('CONTAINS', comparable('TEXT', { text: '505 off' }), {
266
+ textValue: '50%',
267
+ }),
268
+ ).toBe(false);
269
+ expect(
270
+ runPredicate('CONTAINS', comparable('TEXT', { text: 'aXb' }), {
271
+ textValue: 'a_b',
272
+ }),
273
+ ).toBe(false);
274
+ expect(
275
+ runPredicate('CONTAINS', comparable('TEXT', { text: null }), {
276
+ textValue: '',
277
+ }),
278
+ ).toBe(true);
279
+ });
280
+
281
+ it('keeps MULTI_SELECT containment exact and case-sensitive (DV-3)', () => {
282
+ expect(
283
+ runPredicate(
284
+ 'CONTAINS',
285
+ comparable('MULTI_SELECT', { elements: ['RED'] }),
286
+ { textValue: 'RED' },
287
+ ),
288
+ ).toBe(true);
289
+ expect(
290
+ runPredicate(
291
+ 'CONTAINS',
292
+ comparable('MULTI_SELECT', { elements: ['RED'] }),
293
+ { textValue: 'red' },
294
+ ),
295
+ ).toBe(false);
296
+ expect(
297
+ runPredicate('CONTAINS', comparable('MULTI_SELECT', { elements: null }), {
298
+ textValue: 'RED',
299
+ }),
300
+ ).toBe(false);
301
+ expect(
302
+ runPredicate(
303
+ 'CONTAINS',
304
+ comparable('MULTI_SELECT', { elements: ['RED'] }),
305
+ {},
306
+ ),
307
+ ).toBe(false);
308
+ expect(
309
+ runPredicate(
310
+ 'CONTAINS_ANY',
311
+ comparable('MULTI_SELECT', { elements: ['RED'] }),
312
+ { textValues: ['BLUE', 'RED'] },
313
+ ),
314
+ ).toBe(true);
315
+ expect(
316
+ runPredicate(
317
+ 'CONTAINS_ANY',
318
+ comparable('MULTI_SELECT', { elements: ['RED'] }),
319
+ { textValues: ['red'] },
320
+ ),
321
+ ).toBe(false);
322
+ expect(
323
+ runPredicate(
324
+ 'CONTAINS_ANY',
325
+ comparable('MULTI_SELECT', { elements: [] }),
326
+ { textValues: [] },
327
+ ),
328
+ ).toBe(false);
329
+ });
330
+
331
+ it('matches IN against lowered value lists', () => {
332
+ expect(
333
+ runPredicate('IN', comparable('TEXT', { text: 'ABC' }), {
334
+ textValues: ['abc', 'x'],
335
+ }),
336
+ ).toBe(true);
337
+ expect(
338
+ runPredicate('IN', comparable('TEXT', { text: null }), {
339
+ textValues: [''],
340
+ }),
341
+ ).toBe(true);
342
+ expect(
343
+ runPredicate('IN', comparable('TEXT', { text: 'y' }), { textValues: [] }),
344
+ ).toBe(false);
345
+ });
346
+
347
+ it('compares numbers with SQL null semantics', () => {
348
+ expect(
349
+ runPredicate('GT', comparable('NUMBER', { number: 6 }), {
350
+ numberValue: 5,
351
+ }),
352
+ ).toBe(true);
353
+ expect(
354
+ runPredicate('GT', comparable('NUMBER', { number: null }), {
355
+ numberValue: 5,
356
+ }),
357
+ ).toBe(false);
358
+ expect(runPredicate('GT', comparable('NUMBER', { number: 6 }), {})).toBe(
359
+ false,
360
+ );
361
+ expect(
362
+ runPredicate('LTE', comparable('NUMBER', { number: -3.5 }), {
363
+ numberValue: -3.5,
364
+ }),
365
+ ).toBe(true);
366
+ expect(
367
+ runPredicate('EQUALS', comparable('NUMBER', { number: 7 }), {
368
+ numberValue: 7,
369
+ }),
370
+ ).toBe(true);
371
+ });
372
+
373
+ it('bucket-compares dates and instants per the dateValue/datetimeValue split', () => {
374
+ expect(
375
+ runPredicate('BEFORE', comparable('DATE', { date: '2026-06-30' }), {
376
+ dateValue: '2026-07-01',
377
+ }),
378
+ ).toBe(true);
379
+ expect(
380
+ runPredicate('BEFORE', comparable('DATE', { date: null }), {
381
+ dateValue: '2026-07-01',
382
+ }),
383
+ ).toBe(false);
384
+ expect(
385
+ runPredicate('ON_OR_BEFORE', comparable('DATE', { date: '2026-02-28' }), {
386
+ dateValue: '2026-02-28',
387
+ }),
388
+ ).toBe(true);
389
+ // DATETIME + dateValue → org-tz day bucket
390
+ expect(
391
+ runPredicate(
392
+ 'EQUALS',
393
+ comparable('DATETIME', { datetimeMs: 1, datetimeDate: '2026-07-01' }),
394
+ { dateValue: '2026-07-01' },
395
+ ),
396
+ ).toBe(true);
397
+ // DATETIME + datetimeValue → exact instant compare
398
+ expect(
399
+ runPredicate(
400
+ 'ON_OR_AFTER',
401
+ comparable('DATETIME', {
402
+ datetimeMs: Date.parse('2026-07-01T16:00:00.000Z'),
403
+ }),
404
+ {
405
+ datetimeValue: '2026-07-01T16:00:00.000Z',
406
+ },
407
+ ),
408
+ ).toBe(true);
409
+ expect(
410
+ runPredicate(
411
+ 'AFTER',
412
+ comparable('DATETIME', {
413
+ datetimeMs: Date.parse('2026-07-01T16:00:00.000Z'),
414
+ }),
415
+ {
416
+ datetimeValue: '2026-07-01T16:00:00.000Z',
417
+ },
418
+ ),
419
+ ).toBe(false);
420
+ expect(
421
+ runPredicate('BEFORE', comparable('DATETIME', { datetimeMs: null }), {
422
+ datetimeValue: '2026-07-01T00:00:00Z',
423
+ }),
424
+ ).toBe(false);
425
+ });
426
+
427
+ it('compares booleans with SQL null semantics', () => {
428
+ expect(
429
+ runPredicate('EQUALS', comparable('BOOLEAN', { boolean: true }), {
430
+ booleanValue: true,
431
+ }),
432
+ ).toBe(true);
433
+ expect(
434
+ runPredicate('EQUALS', comparable('BOOLEAN', { boolean: null }), {
435
+ booleanValue: false,
436
+ }),
437
+ ).toBe(false);
438
+ expect(
439
+ runPredicate('EQUALS', comparable('BOOLEAN', { boolean: false }), {}),
440
+ ).toBe(false);
441
+ });
442
+
443
+ it('detects emptiness per kind', () => {
444
+ expect(runPredicate('IS_EMPTY', comparable('TEXT', { text: '' }), {})).toBe(
445
+ true,
446
+ );
447
+ expect(
448
+ runPredicate('IS_EMPTY', comparable('TEXT', { text: 'x' }), {}),
449
+ ).toBe(false);
450
+ expect(
451
+ runPredicate('IS_EMPTY', comparable('NUMBER', { number: null }), {}),
452
+ ).toBe(true);
453
+ expect(
454
+ runPredicate('IS_EMPTY', comparable('NUMBER', { number: 0 }), {}),
455
+ ).toBe(false);
456
+ expect(
457
+ runPredicate('IS_EMPTY', comparable('DATE', { date: null }), {}),
458
+ ).toBe(true);
459
+ expect(
460
+ runPredicate(
461
+ 'IS_EMPTY',
462
+ comparable('DATETIME', { datetimeMs: null }),
463
+ {},
464
+ ),
465
+ ).toBe(true);
466
+ expect(
467
+ runPredicate('IS_EMPTY', comparable('BOOLEAN', { boolean: false }), {}),
468
+ ).toBe(false);
469
+ expect(
470
+ runPredicate(
471
+ 'IS_EMPTY',
472
+ comparable('MULTI_SELECT', { elements: [] }),
473
+ {},
474
+ ),
475
+ ).toBe(true);
476
+ expect(
477
+ runPredicate(
478
+ 'IS_EMPTY',
479
+ comparable('MULTI_SELECT', { elements: ['A'] }),
480
+ {},
481
+ ),
482
+ ).toBe(false);
483
+ });
484
+
485
+ it('prefers raw-storage emptiness over the coerced value for IS_EMPTY', () => {
486
+ // Mixed storage (NUMBER filter over TEXT column holding 'abc'): the SQL
487
+ // non-empty predicate checks COALESCE(textValue,'') <> '', so the row is
488
+ // NON-empty even though the coerced number is null
489
+ expect(
490
+ runPredicate(
491
+ 'IS_EMPTY',
492
+ comparable('NUMBER', { number: null, storageNonEmpty: true }),
493
+ {},
494
+ ),
495
+ ).toBe(false);
496
+ expect(
497
+ runPredicate(
498
+ 'IS_EMPTY',
499
+ comparable('NUMBER', { number: null, storageNonEmpty: false }),
500
+ {},
501
+ ),
502
+ ).toBe(true);
503
+ expect(
504
+ runPredicate(
505
+ 'IS_EMPTY',
506
+ comparable('DATE', { date: null, storageNonEmpty: true }),
507
+ {},
508
+ ),
509
+ ).toBe(false);
510
+ expect(
511
+ runPredicate(
512
+ 'IS_EMPTY',
513
+ getSheetRegionFilterComparableValueFromRecord(
514
+ { textValue: 'abc' },
515
+ 'NUMBER',
516
+ 'TEXT',
517
+ ),
518
+ {},
519
+ ),
520
+ ).toBe(false);
521
+ });
522
+ });
523
+
524
+ describe('three-valued evaluation', () => {
525
+ it('returns null for ID conditions that require SQL lookups', () => {
526
+ expect(
527
+ evaluateSheetRegionSourceFilterCondition(
528
+ {
529
+ cellKey: 'col',
530
+ operator: 'EQUALS',
531
+ textValue: 'x',
532
+ fieldType: 'ID',
533
+ } as never,
534
+ comparable('TEXT', { text: 'x' }),
535
+ ),
536
+ ).toBe(null);
537
+ expect(
538
+ evaluateSheetRegionSourceFilterCondition(
539
+ {
540
+ cellKey: 'col',
541
+ operator: 'IN',
542
+ textValues: ['x'],
543
+ fieldType: 'ID',
544
+ } as never,
545
+ comparable('TEXT', { text: 'x' }),
546
+ ),
547
+ ).toBe(null);
548
+ // CONTAINS on ID stays evaluable (plain text comparison in SQL)
549
+ expect(
550
+ evaluateSheetRegionSourceFilterCondition(
551
+ {
552
+ cellKey: 'col',
553
+ operator: 'CONTAINS',
554
+ textValue: 'x',
555
+ fieldType: 'ID',
556
+ } as never,
557
+ comparable('TEXT', { text: 'axb' }),
558
+ ),
559
+ ).toBe(true);
560
+ });
561
+
562
+ it('propagates null through groups with AND/OR short-circuits', () => {
563
+ const idCondition = {
564
+ cellKey: 'a',
565
+ operator: 'EQUALS',
566
+ textValue: 'x',
567
+ fieldType: 'ID',
568
+ } as never;
569
+ const trueCondition = {
570
+ cellKey: 'b',
571
+ operator: 'EQUALS',
572
+ textValue: 'y',
573
+ fieldType: 'TEXT',
574
+ } as never;
575
+ const falseCondition = {
576
+ cellKey: 'c',
577
+ operator: 'EQUALS',
578
+ textValue: 'z',
579
+ fieldType: 'TEXT',
580
+ } as never;
581
+ const getComparable = (condition: { cellKey: string }) =>
582
+ condition.cellKey === 'b'
583
+ ? comparable('TEXT', { text: 'y' })
584
+ : condition.cellKey === 'c'
585
+ ? comparable('TEXT', { text: 'nope' })
586
+ : comparable('TEXT', { text: 'x' });
587
+
588
+ // AND with a definite false decides false even when a null leaf exists
589
+ expect(
590
+ evaluateSheetRegionSourceFilterGroup(
591
+ {
592
+ combinator: 'AND',
593
+ conditions: [idCondition, falseCondition],
594
+ groups: [],
595
+ },
596
+ getComparable,
597
+ ),
598
+ ).toBe(false);
599
+ // AND with true + null stays null
600
+ expect(
601
+ evaluateSheetRegionSourceFilterGroup(
602
+ {
603
+ combinator: 'AND',
604
+ conditions: [idCondition, trueCondition],
605
+ groups: [],
606
+ },
607
+ getComparable,
608
+ ),
609
+ ).toBe(null);
610
+ // OR with a definite true decides true
611
+ expect(
612
+ evaluateSheetRegionSourceFilterGroup(
613
+ {
614
+ combinator: 'OR',
615
+ conditions: [idCondition, trueCondition],
616
+ groups: [],
617
+ },
618
+ getComparable,
619
+ ),
620
+ ).toBe(true);
621
+ // OR with false + null stays null
622
+ expect(
623
+ evaluateSheetRegionSourceFilterGroup(
624
+ {
625
+ combinator: 'OR',
626
+ conditions: [idCondition, falseCondition],
627
+ groups: [],
628
+ },
629
+ getComparable,
630
+ ),
631
+ ).toBe(null);
632
+ });
633
+
634
+ it('skips empty groups entirely (no constraint) like the SQL recursion', () => {
635
+ const getComparable = () => comparable('TEXT', { text: 'nope' });
636
+ // Empty subgroup inside an OR must NOT decide the OR
637
+ expect(
638
+ evaluateSheetRegionSourceFilterGroup(
639
+ {
640
+ combinator: 'OR',
641
+ conditions: [
642
+ {
643
+ cellKey: 'c',
644
+ operator: 'EQUALS',
645
+ textValue: 'z',
646
+ fieldType: 'TEXT',
647
+ } as never,
648
+ ],
649
+ groups: [{ combinator: 'AND', conditions: [], groups: [] }],
650
+ },
651
+ getComparable,
652
+ ),
653
+ ).toBe(false);
654
+ expect(evaluateSheetRegionSourceFilterGroup(null, getComparable)).toBe(
655
+ true,
656
+ );
657
+ expect(
658
+ evaluateSheetRegionSourceFilterGroup(
659
+ { combinator: 'AND', conditions: [], groups: [] },
660
+ getComparable,
661
+ ),
662
+ ).toBe(true);
663
+ });
664
+
665
+ it('returns null for SQL-only comparables except storage-flagged IS_EMPTY (DV-9)', () => {
666
+ const sqlOnlyComparable = comparable('DATE', {
667
+ date: null,
668
+ sqlOnly: true,
669
+ storageNonEmpty: true,
670
+ });
671
+ // Value comparisons cannot be mirrored (session-timezone text casts)
672
+ expect(
673
+ evaluateSheetRegionSourceFilterCondition(
674
+ {
675
+ cellKey: 'col',
676
+ operator: 'BEFORE',
677
+ dateValue: '2026-08-01',
678
+ fieldType: 'DATE',
679
+ } as never,
680
+ sqlOnlyComparable,
681
+ ),
682
+ ).toBe(null);
683
+ // IS_EMPTY reads raw storage emptiness, so it stays decidable
684
+ expect(
685
+ evaluateSheetRegionSourceFilterCondition(
686
+ { cellKey: 'col', operator: 'IS_EMPTY', fieldType: 'DATE' } as never,
687
+ sqlOnlyComparable,
688
+ ),
689
+ ).toBe(false);
690
+ // ... but not when the storage flag is missing
691
+ expect(
692
+ evaluateSheetRegionSourceFilterCondition(
693
+ { cellKey: 'col', operator: 'IS_EMPTY', fieldType: 'DATE' } as never,
694
+ comparable('DATE', { date: null, sqlOnly: true }),
695
+ ),
696
+ ).toBe(null);
697
+ });
698
+
699
+ it('returns null when a comparable cannot be produced', () => {
700
+ expect(
701
+ evaluateSheetRegionSourceFilterGroup(
702
+ {
703
+ combinator: 'AND',
704
+ conditions: [
705
+ {
706
+ cellKey: 'c',
707
+ operator: 'EQUALS',
708
+ textValue: 'z',
709
+ fieldType: 'TEXT',
710
+ } as never,
711
+ ],
712
+ groups: [],
713
+ },
714
+ () => null,
715
+ ),
716
+ ).toBe(null);
717
+ });
718
+ });
719
+
720
+ describe('value normalizers (server semantics; DV-4 matrix)', () => {
721
+ it('maps option labels and values to canonical option values for any cell with options', () => {
722
+ expect(normalizeSheetRegionFilterTextValue(SELECT_CELL, 'Red')).toBe('RED');
723
+ expect(normalizeSheetRegionFilterTextValue(SELECT_CELL, 'rEd')).toBe('RED');
724
+ expect(normalizeSheetRegionFilterTextValue(SELECT_CELL, 'RED')).toBe('RED');
725
+ expect(normalizeSheetRegionFilterTextValue(SELECT_CELL, 'Green')).toBe(
726
+ 'Green',
727
+ );
728
+ // MULTI_SELECT labels map too (the client-side gap DV-4 closes)
729
+ expect(normalizeSheetRegionFilterTextValue(MULTI_CELL, 'Red')).toBe('RED');
730
+ expect(normalizeSheetRegionFilterTextValue(MULTI_CELL, 'BLUE')).toBe(
731
+ 'BLUE',
732
+ );
733
+ expect(normalizeSheetRegionFilterTextValue(TEXT_CELL, ' Basil ')).toBe(
734
+ 'Basil',
735
+ );
736
+ expect(normalizeSheetRegionFilterTextValue(TEXT_CELL, '')).toBe(null);
737
+ expect(normalizeSheetRegionFilterTextValue(TEXT_CELL, null)).toBe(null);
738
+ });
739
+
740
+ it('normalizes text value lists', () => {
741
+ expect(
742
+ normalizeSheetRegionFilterTextValues(MULTI_CELL, ['Red', 'BLUE']),
743
+ ).toEqual(['RED', 'BLUE']);
744
+ expect(normalizeSheetRegionFilterTextValues(MULTI_CELL, [])).toBe(null);
745
+ expect(normalizeSheetRegionFilterTextValues(MULTI_CELL, ['Red', ''])).toBe(
746
+ null,
747
+ );
748
+ expect(normalizeSheetRegionFilterTextValues(MULTI_CELL, 'Red')).toBe(null);
749
+ });
750
+
751
+ it('normalizes number, boolean, date, and datetime values', () => {
752
+ expect(normalizeSheetRegionFilterNumberValue('007')).toBe(7);
753
+ expect(normalizeSheetRegionFilterNumberValue('1e3')).toBe(1000);
754
+ expect(normalizeSheetRegionFilterNumberValue('abc')).toBe(null);
755
+ expect(normalizeSheetRegionFilterBooleanValue(true)).toBe(true);
756
+ expect(normalizeSheetRegionFilterBooleanValue('true')).toBe(null);
757
+ expect(normalizeSheetRegionFilterDateValue('DATE', '2026-07-01')).toBe(
758
+ '2026-07-01',
759
+ );
760
+ expect(
761
+ normalizeSheetRegionFilterDateValue('WEEK_OF_MON', '2026-07-01'),
762
+ ).toBe('2026-06-29');
763
+ // Historical quirk kept for parity: unparseable date text passes through
764
+ // (both S2 and the client normalizer accept it today)
765
+ expect(normalizeSheetRegionFilterDateValue('DATE', 'nope')).toBe('nope');
766
+ expect(
767
+ normalizeSheetRegionFilterDateTimeValue(
768
+ '2026-07-01T09:00',
769
+ 'America/New_York',
770
+ ),
771
+ ).toBe('2026-07-01T13:00:00.000Z');
772
+ expect(normalizeSheetRegionFilterDateTimeValue('nope')).toBe(null);
773
+ expect(isSheetRegionFilterDateOnlyValue('2026-07-01')).toBe(true);
774
+ expect(isSheetRegionFilterDateOnlyValue('2026-07-01T00:00:00Z')).toBe(
775
+ false,
776
+ );
777
+ });
778
+ });
779
+
780
+ describe('comparable coercion (DV-8 / DV-9 regex mirroring)', () => {
781
+ it('gates numeric text with the exact SQL regex', () => {
782
+ expect(getSheetRegionFilterComparableNumberFromText('007')).toBe(7);
783
+ expect(getSheetRegionFilterComparableNumberFromText('-3.5')).toBe(-3.5);
784
+ expect(getSheetRegionFilterComparableNumberFromText('+2')).toBe(2);
785
+ // SQL regex rejects these even though Number() accepts them
786
+ expect(getSheetRegionFilterComparableNumberFromText('1e3')).toBe(null);
787
+ expect(getSheetRegionFilterComparableNumberFromText('.5')).toBe(null);
788
+ expect(getSheetRegionFilterComparableNumberFromText('1,000')).toBe(null);
789
+ expect(getSheetRegionFilterComparableNumberFromText('')).toBe(null);
790
+ expect(getSheetRegionFilterComparableNumberFromText(null)).toBe(null);
791
+ });
792
+
793
+ it('mirrors the SQL date-from-text fallback regex set', () => {
794
+ expect(
795
+ getSheetRegionFilterComparableDateFromText('2026-07-01T12:00:00Z'),
796
+ ).toBe('2026-07-01');
797
+ expect(getSheetRegionFilterComparableDateFromText('"2026-07-01"')).toBe(
798
+ '2026-07-01',
799
+ );
800
+ expect(getSheetRegionFilterComparableDateFromText('not a date')).toBe(null);
801
+ expect(getSheetRegionFilterComparableDateFromText('1/2/2026')).toBe(null);
802
+ // Month-name shapes are SQL-only: their ::timestamptz conversion anchors
803
+ // in the DB session timezone, so no in-memory day is produced (DV-9)
804
+ expect(getSheetRegionFilterComparableDateFromText('July 1, 2026')).toBe(
805
+ null,
806
+ );
807
+ expect(isSheetRegionFilterSQLOnlyDateLikeText('July 1, 2026')).toBe(true);
808
+ expect(isSheetRegionFilterSQLOnlyDateLikeText('Wed Jul 1 2026')).toBe(true);
809
+ expect(isSheetRegionFilterSQLOnlyDateLikeText('2026-07-01')).toBe(false);
810
+ expect(isSheetRegionFilterSQLOnlyDateLikeText('not a date')).toBe(false);
811
+ });
812
+
813
+ it('decides raw-storage emptiness from the STORAGE field type', () => {
814
+ expect(isSheetRegionFilterRecordStorageNonEmpty(null, 'TEXT')).toBe(false);
815
+ expect(
816
+ isSheetRegionFilterRecordStorageNonEmpty({ textValue: 'abc' }, 'TEXT'),
817
+ ).toBe(true);
818
+ expect(
819
+ isSheetRegionFilterRecordStorageNonEmpty({ textValue: '' }, 'TEXT'),
820
+ ).toBe(false);
821
+ expect(
822
+ isSheetRegionFilterRecordStorageNonEmpty({ numberValue: 0 }, 'NUMBER'),
823
+ ).toBe(true);
824
+ expect(
825
+ isSheetRegionFilterRecordStorageNonEmpty({ numberValue: null }, 'NUMBER'),
826
+ ).toBe(false);
827
+ expect(
828
+ isSheetRegionFilterRecordStorageNonEmpty(
829
+ { booleanValue: false },
830
+ 'BOOLEAN',
831
+ ),
832
+ ).toBe(true);
833
+ expect(
834
+ isSheetRegionFilterRecordStorageNonEmpty(
835
+ { dateValue: '2026-07-01' },
836
+ 'DATE',
837
+ ),
838
+ ).toBe(true);
839
+ expect(
840
+ isSheetRegionFilterRecordStorageNonEmpty(
841
+ { datetimeValue: null },
842
+ 'DATETIME',
843
+ ),
844
+ ).toBe(false);
845
+ expect(
846
+ isSheetRegionFilterRecordStorageNonEmpty({ value: [] }, 'MULTI_SELECT'),
847
+ ).toBe(false);
848
+ expect(
849
+ isSheetRegionFilterRecordStorageNonEmpty(
850
+ { value: ['A'] },
851
+ 'MULTI_SELECT',
852
+ ),
853
+ ).toBe(true);
854
+ expect(
855
+ isSheetRegionFilterRecordStorageNonEmpty({ value: 'x' }, 'MULTI_SELECT'),
856
+ ).toBe(false);
857
+ });
858
+
859
+ it('builds record comparables with storage fallbacks', () => {
860
+ expect(
861
+ getSheetRegionFilterComparableValueFromRecord(
862
+ { numberValue: 5 },
863
+ 'NUMBER',
864
+ 'NUMBER',
865
+ ),
866
+ ).toEqual({ kind: 'NUMBER', number: 5, storageNonEmpty: true });
867
+ expect(
868
+ getSheetRegionFilterComparableValueFromRecord(
869
+ { textValue: '007' },
870
+ 'NUMBER',
871
+ 'TEXT',
872
+ ),
873
+ ).toEqual({ kind: 'NUMBER', number: 7, storageNonEmpty: true });
874
+ // Unparseable text coerces to null but the RAW storage stays non-empty
875
+ expect(
876
+ getSheetRegionFilterComparableValueFromRecord(
877
+ { textValue: '1e3' },
878
+ 'NUMBER',
879
+ 'TEXT',
880
+ ),
881
+ ).toEqual({ kind: 'NUMBER', number: null, storageNonEmpty: true });
882
+ expect(
883
+ getSheetRegionFilterComparableValueFromRecord(
884
+ { dateValue: '2026-07-01' },
885
+ 'DATE',
886
+ 'DATE',
887
+ ),
888
+ ).toEqual({ kind: 'DATE', date: '2026-07-01', storageNonEmpty: true });
889
+ // DATETIME storage compared as DATE uses the org-tz day (23:30 LA = next day UTC)
890
+ expect(
891
+ getSheetRegionFilterComparableValueFromRecord(
892
+ { datetimeValue: '2026-07-02T06:30:00.000Z' },
893
+ 'DATE',
894
+ 'DATETIME',
895
+ 'America/Los_Angeles',
896
+ ),
897
+ ).toEqual({ kind: 'DATE', date: '2026-07-01', storageNonEmpty: true });
898
+ expect(
899
+ getSheetRegionFilterComparableValueFromRecord(
900
+ { value: ['A', 'B', 3] },
901
+ 'MULTI_SELECT',
902
+ 'MULTI_SELECT',
903
+ ),
904
+ ).toEqual({
905
+ kind: 'MULTI_SELECT',
906
+ elements: ['A', 'B'],
907
+ storageNonEmpty: true,
908
+ });
909
+ expect(
910
+ getSheetRegionFilterComparableValueFromRecord(
911
+ { textValue: 'x' },
912
+ 'TEXT',
913
+ 'TEXT',
914
+ ),
915
+ ).toEqual({ kind: 'TEXT', text: 'x', storageNonEmpty: true });
916
+ expect(
917
+ getSheetRegionFilterComparableValueFromRecord(null, 'TEXT', 'TEXT'),
918
+ ).toEqual({ kind: 'TEXT', text: null, storageNonEmpty: false });
919
+ });
920
+
921
+ it('marks session-timezone-dependent text timestamps as SQL-only (DV-9)', () => {
922
+ // Month-name text over a DATE filter: SQL converts via ::timestamptz in
923
+ // the DB session timezone — no in-memory day is produced
924
+ expect(
925
+ getSheetRegionFilterComparableValueFromRecord(
926
+ { textValue: 'July 1, 2026' },
927
+ 'DATE',
928
+ 'TEXT',
929
+ 'UTC',
930
+ ),
931
+ ).toEqual({
932
+ kind: 'DATE',
933
+ date: null,
934
+ sqlOnly: true,
935
+ storageNonEmpty: true,
936
+ });
937
+ // Any date-like text over a DATETIME filter is SQL-only: naive text
938
+ // anchors in the session timezone, and day buckets use LEFT(text, 10)
939
+ expect(
940
+ getSheetRegionFilterComparableValueFromRecord(
941
+ { textValue: '2026-07-01 10:00' },
942
+ 'DATETIME',
943
+ 'TEXT',
944
+ 'UTC',
945
+ ),
946
+ ).toEqual({
947
+ kind: 'DATETIME',
948
+ datetimeMs: null,
949
+ datetimeDate: null,
950
+ sqlOnly: true,
951
+ storageNonEmpty: true,
952
+ });
953
+ expect(
954
+ getSheetRegionFilterComparableValueFromRecord(
955
+ { textValue: '2026-07-01T01:00:00+02:00' },
956
+ 'DATETIME',
957
+ 'TEXT',
958
+ 'UTC',
959
+ ),
960
+ ).toEqual({
961
+ kind: 'DATETIME',
962
+ datetimeMs: null,
963
+ datetimeDate: null,
964
+ sqlOnly: true,
965
+ storageNonEmpty: true,
966
+ });
967
+ // Non-date-like text stays evaluable: both sides coerce NULL
968
+ expect(
969
+ getSheetRegionFilterComparableValueFromRecord(
970
+ { textValue: 'abc' },
971
+ 'DATETIME',
972
+ 'TEXT',
973
+ 'UTC',
974
+ ),
975
+ ).toEqual({
976
+ kind: 'DATETIME',
977
+ datetimeMs: null,
978
+ datetimeDate: null,
979
+ storageNonEmpty: true,
980
+ });
981
+ });
982
+ });
983
+
984
+ describe('sqlPredicate parameterization', () => {
985
+ const EXPRS: SheetRegionFilterSQLColumnExprs = {
986
+ textExpr: `COALESCE("a"."textValue", '')`,
987
+ numberExpr: '"a"."numberValue"',
988
+ dateExpr: '"a"."dateValue"::text',
989
+ datetimeExpr: '"a"."datetimeValue"',
990
+ booleanExpr: '"a"."booleanValue"',
991
+ jsonbExpr: '"a"."value"',
992
+ };
993
+
994
+ /* Build a recording binder capturing bound values. */
995
+ function recordingBinder() {
996
+ const bound: unknown[] = [];
997
+ const bind = (value: unknown) => {
998
+ bound.push(value);
999
+ return `$value${bound.length - 1}`;
1000
+ };
1001
+ return { bound, bind };
1002
+ }
1003
+
1004
+ it('emits only exprs, keywords, and $keys (never inline values)', () => {
1005
+ const cases: Array<[string, Record<string, unknown>, string]> = [
1006
+ ['CONTAINS', { textValue: "o'; DROP TABLE x --" }, 'TEXT'],
1007
+ ['EQUALS', { textValue: "o'; DROP" }, 'TEXT'],
1008
+ ['IN', { textValues: ["a'b", 'c'] }, 'TEXT'],
1009
+ ['GT', { numberValue: 5 }, 'NUMBER'],
1010
+ ['BEFORE', { dateValue: "2026-07-01'; --" }, 'DATE'],
1011
+ [
1012
+ 'ON_OR_AFTER',
1013
+ { datetimeValue: "2026-07-01T00:00:00Z'; --" },
1014
+ 'DATETIME',
1015
+ ],
1016
+ ['EQUALS', { booleanValue: true }, 'BOOLEAN'],
1017
+ ['CONTAINS', { textValue: "x'y" }, 'MULTI_SELECT'],
1018
+ ['CONTAINS_ANY', { textValues: ["x'y"] }, 'MULTI_SELECT'],
1019
+ ];
1020
+ for (const [operator, condition, kind] of cases) {
1021
+ const { bind } = recordingBinder();
1022
+ const sql = SHEET_REGION_FILTER_OPERATOR_DEFS[
1023
+ operator as never
1024
+ ].sqlPredicate(
1025
+ EXPRS,
1026
+ { cellKey: 'col', operator, ...condition } as never,
1027
+ kind as never,
1028
+ bind,
1029
+ );
1030
+ // No single-quoted literal other than the COALESCE '' from the expr
1031
+ // and the whitelisted ESCAPE '\' clause (DV-6)
1032
+ const sanitizedSQL = sql
1033
+ .replaceAll(`COALESCE("a"."textValue", '')`, '')
1034
+ .replaceAll(`ESCAPE '\\'`, '');
1035
+ expect(sanitizedSQL).not.toMatch(/'[^']/);
1036
+ expect(sql).not.toContain('DROP');
1037
+ }
1038
+ });
1039
+
1040
+ it('binds lowered values for text comparisons at bind time', () => {
1041
+ const { bound, bind } = recordingBinder();
1042
+ SHEET_REGION_FILTER_OPERATOR_DEFS.EQUALS.sqlPredicate(
1043
+ EXPRS,
1044
+ { cellKey: 'col', operator: 'EQUALS', textValue: 'MiXeD' } as never,
1045
+ 'TEXT',
1046
+ bind,
1047
+ );
1048
+ SHEET_REGION_FILTER_OPERATOR_DEFS.CONTAINS.sqlPredicate(
1049
+ EXPRS,
1050
+ { cellKey: 'col', operator: 'CONTAINS', textValue: '50%' } as never,
1051
+ 'TEXT',
1052
+ bind,
1053
+ );
1054
+ SHEET_REGION_FILTER_OPERATOR_DEFS.IN.sqlPredicate(
1055
+ EXPRS,
1056
+ { cellKey: 'col', operator: 'IN', textValues: ['A', 'b'] } as never,
1057
+ 'TEXT',
1058
+ bind,
1059
+ );
1060
+ expect(bound).toEqual(['mixed', '%50\\%%', ['a', 'b']]);
1061
+ });
1062
+
1063
+ it('escapes LIKE metacharacters and appends ESCAPE for literal substrings (DV-6)', () => {
1064
+ expect(getSheetRegionFilterContainsLikePattern('50%_\\')).toBe(
1065
+ '%50\\%\\_\\\\%',
1066
+ );
1067
+ expect(getSheetRegionFilterContainsLikePattern('plain')).toBe('%plain%');
1068
+
1069
+ const bound: unknown[] = [];
1070
+ const sql = SHEET_REGION_FILTER_OPERATOR_DEFS.CONTAINS.sqlPredicate(
1071
+ EXPRS,
1072
+ { cellKey: 'col', operator: 'CONTAINS', textValue: '50%' } as never,
1073
+ 'TEXT',
1074
+ (value) => {
1075
+ bound.push(value);
1076
+ return '$value0';
1077
+ },
1078
+ );
1079
+ expect(sql).toBe(
1080
+ `LOWER(COALESCE("a"."textValue", '')) LIKE $value0 ESCAPE '\\'`,
1081
+ );
1082
+ expect(bound).toEqual(['%50\\%%']);
1083
+ });
1084
+
1085
+ it('supports the LOGS cast-on-bind DATE variant in the temporal helper', () => {
1086
+ const { bound, bind } = recordingBinder();
1087
+ const sql = getSheetRegionFilterTemporalSQL(
1088
+ { dateExpr: '"fieldDocSubmission"."period"', dateBindCast: '::date' },
1089
+ {
1090
+ cellKey: 'fieldDocSubmissionPeriod',
1091
+ operator: 'BEFORE',
1092
+ dateValue: '2026-07-01',
1093
+ } as never,
1094
+ 'DATE',
1095
+ bind,
1096
+ '<',
1097
+ );
1098
+ expect(sql).toBe('"fieldDocSubmission"."period" < $value0::date');
1099
+ expect(bound).toEqual(['2026-07-01']);
1100
+ });
1101
+
1102
+ it('falls back to the datetimeValue path when no dateExpr is supplied (staged DV-1 behavior)', () => {
1103
+ const { bound, bind } = recordingBinder();
1104
+ const sql = getSheetRegionFilterTemporalSQL(
1105
+ { datetimeExpr: '"logs"."date"' },
1106
+ { cellKey: 'date', operator: 'BEFORE', dateValue: '2026-07-01' } as never,
1107
+ 'DATETIME',
1108
+ bind,
1109
+ '<',
1110
+ );
1111
+ expect(sql).toBe('"logs"."date" < $value0::timestamptz');
1112
+ expect(bound).toEqual([undefined]);
1113
+ });
1114
+
1115
+ it('throws the shared error code when a required expression is missing', () => {
1116
+ const { bind } = recordingBinder();
1117
+ expect(() =>
1118
+ getSheetRegionFilterTemporalSQL(
1119
+ {},
1120
+ { cellKey: 'x', operator: 'BEFORE', dateValue: '2026-07-01' } as never,
1121
+ 'DATE',
1122
+ bind,
1123
+ '<',
1124
+ ),
1125
+ ).toThrow('20077');
1126
+ expect(() =>
1127
+ SHEET_REGION_FILTER_OPERATOR_DEFS.CONTAINS_ANY.sqlPredicate(
1128
+ {},
1129
+ { cellKey: 'x', operator: 'CONTAINS_ANY', textValues: ['a'] } as never,
1130
+ 'MULTI_SELECT',
1131
+ bind,
1132
+ ),
1133
+ ).toThrow('20077');
1134
+ });
1135
+ });