@jsb188/mday 1.1.10 → 1.1.12

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 (210) hide show
  1. package/package.json +140 -54
  2. package/src/constants/agentMemory.ts +123 -0
  3. package/src/constants/agentNote.ts +212 -0
  4. package/src/constants/aiChat.ts +10 -0
  5. package/src/constants/billing.ts +126 -0
  6. package/src/constants/campaign.ts +236 -0
  7. package/src/constants/contact.ts +72 -0
  8. package/src/constants/csvExport.ts +133 -0
  9. package/src/constants/dataTable.ts +21 -21
  10. package/src/constants/dataTableFieldTypes.generated.ts +59 -0
  11. package/src/constants/docAccess.ts +33 -0
  12. package/src/constants/dt.ts +234 -0
  13. package/src/constants/email.ts +125 -7
  14. package/src/constants/fieldDoc.ts +104 -0
  15. package/src/constants/graphqlReactivity.ts +196 -0
  16. package/src/constants/integration.ts +8 -9
  17. package/src/constants/log.ts +14 -408
  18. package/src/constants/logDisplay/README.md +88 -0
  19. package/src/constants/logDisplay/defaults.ts +39 -0
  20. package/src/constants/logDisplay/index.ts +27 -0
  21. package/src/constants/logDisplay/operations/arable.ts +16 -0
  22. package/src/constants/logDisplay/operations/farmersMarket.ts +10 -0
  23. package/src/constants/logDisplay/operations/foodManufacturing.ts +13 -0
  24. package/src/constants/logDisplay/shared/accounting.ts +13 -0
  25. package/src/constants/logDisplay/shared/interactions.ts +13 -0
  26. package/src/constants/logDisplay/shared/rackTemperatures.ts +42 -0
  27. package/src/constants/logTaxonomy.generated.ts +2207 -0
  28. package/src/constants/noteDoc.ts +77 -0
  29. package/src/constants/organization.ts +18 -21
  30. package/src/constants/pageRoute.ts +23 -0
  31. package/src/constants/qrCode.ts +30 -0
  32. package/src/constants/sheet.ts +126 -14
  33. package/src/constants/sms.ts +18 -0
  34. package/src/constants/square.ts +6 -6
  35. package/src/constants/storage.ts +80 -7
  36. package/src/constants/usage.ts +82 -0
  37. package/src/constants/workflowSteps.ts +60 -0
  38. package/src/schemas/editLogEntry.ts +836 -780
  39. package/src/schemas/inboundContact.ts +60 -55
  40. package/src/schemas/logsQuery.ts +55 -49
  41. package/src/types/account.d.ts +4 -1
  42. package/src/types/agentMemory.d.ts +98 -0
  43. package/src/types/agentNote.d.ts +443 -0
  44. package/src/types/billing.d.ts +537 -0
  45. package/src/types/campaign.d.ts +1533 -0
  46. package/src/types/contact.d.ts +15 -0
  47. package/src/types/csvExport.d.ts +213 -0
  48. package/src/types/dataAnalysis.d.ts +44 -0
  49. package/src/types/dataTable.d.ts +115 -403
  50. package/src/types/definitionElement.d.ts +35 -0
  51. package/src/types/docAccess.d.ts +96 -0
  52. package/src/types/dt.d.ts +257 -0
  53. package/src/types/email.d.ts +184 -24
  54. package/src/types/emailReplyReport.d.ts +76 -0
  55. package/src/types/fieldDoc.d.ts +702 -0
  56. package/src/types/fieldDocContract.d.ts +90 -0
  57. package/src/types/inboundContact.d.ts +10 -1
  58. package/src/types/log.d.ts +254 -295
  59. package/src/types/logDisplay.types.ts +85 -0
  60. package/src/types/organization.d.ts +91 -43
  61. package/src/types/pageRoute.d.ts +48 -0
  62. package/src/types/qrCode.d.ts +96 -0
  63. package/src/types/richText.d.ts +81 -0
  64. package/src/types/sheet.d.ts +678 -129
  65. package/src/types/sms.d.ts +38 -0
  66. package/src/types/storage.d.ts +449 -129
  67. package/src/types/usage.d.ts +106 -0
  68. package/src/types/workflow.d.ts +315 -54
  69. package/src/utils/agentMemory.ts +15 -0
  70. package/src/utils/agentNote.ts +850 -0
  71. package/src/utils/aiChat.ts +43 -0
  72. package/src/utils/aiChatWork.ts +64 -0
  73. package/src/utils/billing.ts +37 -0
  74. package/src/utils/campaign.ts +1174 -0
  75. package/src/utils/campaignAI.ts +72 -0
  76. package/src/utils/campaignAccess.ts +239 -0
  77. package/src/utils/campaignEmail.ts +601 -0
  78. package/src/utils/campaignEmailCompiler.ts +372 -0
  79. package/src/utils/campaignEmailImages.ts +60 -0
  80. package/src/utils/campaignEmailStyles.ts +544 -0
  81. package/src/utils/campaignLifecycle.ts +104 -0
  82. package/src/utils/campaignPreview.ts +285 -0
  83. package/src/utils/contact.ts +50 -0
  84. package/src/utils/csvExport.ts +19 -0
  85. package/src/utils/dataTable.ts +95 -618
  86. package/src/utils/dataTableFieldTypeRegistry.ts +106 -0
  87. package/src/utils/dataTableRelativeDate.ts +447 -0
  88. package/src/utils/definitionElement.ts +84 -0
  89. package/src/utils/docAccess.ts +383 -0
  90. package/src/utils/documentName.ts +53 -0
  91. package/src/utils/email.ts +317 -51
  92. package/src/utils/emailReplyReport.test.ts +187 -0
  93. package/src/utils/emailReplyReport.ts +143 -0
  94. package/src/utils/fieldDoc.ts +157 -0
  95. package/src/utils/fieldDocAnswers.ts +128 -0
  96. package/src/utils/fieldDocDefaults.ts +36 -0
  97. package/src/utils/fieldDocProgram.ts +36 -0
  98. package/src/utils/fieldDocSummary.ts +30 -0
  99. package/src/utils/log.ts +276 -192
  100. package/src/utils/logDisplay.helpers.ts +177 -0
  101. package/src/utils/logDisplay.ts +87 -0
  102. package/src/utils/logRegistryValue.ts +90 -0
  103. package/src/utils/logStoreRead.ts +262 -0
  104. package/src/utils/logTaxonomy.helpers.ts +17 -0
  105. package/src/utils/logTaxonomy.ts +76 -0
  106. package/src/utils/logTaxonomyDisplay.ts +395 -0
  107. package/src/utils/noteDoc.ts +609 -0
  108. package/src/utils/organization.ts +337 -84
  109. package/src/utils/pageRoute.ts +71 -0
  110. package/src/utils/qrCode.helpers.ts +189 -0
  111. package/src/utils/qrCode.test.ts +519 -0
  112. package/src/utils/qrCode.ts +273 -0
  113. package/src/utils/richTextHTML.ts +385 -0
  114. package/src/utils/sheet.ts +199 -107
  115. package/src/utils/sheetAxisMove.ts +616 -0
  116. package/src/utils/sheetBootstrap.ts +1402 -0
  117. package/src/utils/sheetCellLiteral.ts +101 -0
  118. package/src/utils/sheetCommand.ts +358 -0
  119. package/src/utils/sheetDesign.ts +705 -0
  120. package/src/utils/sheetExport.ts +186 -0
  121. package/src/utils/sheetFeatureReference.ts +183 -0
  122. package/src/utils/sheetFormulaArgs.ts +168 -0
  123. package/src/utils/sheetFormulaCompile.ts +475 -0
  124. package/src/utils/sheetFormulaConditionalAggregate.ts +254 -0
  125. package/src/utils/sheetFormulaCriteria.ts +656 -0
  126. package/src/utils/sheetFormulaDate.ts +444 -0
  127. package/src/utils/sheetFormulaEvaluation.ts +899 -0
  128. package/src/utils/sheetFormulaEvaluator.ts +997 -0
  129. package/src/utils/sheetFormulaFunctionHandlers.ts +403 -0
  130. package/src/utils/sheetFormulaLogical.ts +116 -0
  131. package/src/utils/sheetFormulaLookup.ts +488 -0
  132. package/src/utils/sheetFormulaMath.ts +48 -0
  133. package/src/utils/sheetFormulaParser.ts +729 -0
  134. package/src/utils/sheetFormulaRange.ts +330 -0
  135. package/src/utils/sheetFormulaReference.ts +703 -0
  136. package/src/utils/sheetFormulaReferenceAggregates.ts +313 -0
  137. package/src/utils/sheetFormulaReferenceDates.ts +237 -0
  138. package/src/utils/sheetFormulaReferenceLogical.ts +330 -0
  139. package/src/utils/sheetFormulaReferenceLookup.ts +341 -0
  140. package/src/utils/sheetFormulaReferenceMath.ts +95 -0
  141. package/src/utils/sheetFormulaReferenceRewrite.ts +220 -0
  142. package/src/utils/sheetFormulaReferenceText.ts +462 -0
  143. package/src/utils/sheetFormulaSyntax.ts +1027 -0
  144. package/src/utils/sheetFormulaText.ts +430 -0
  145. package/src/utils/sheetJournal.ts +200 -0
  146. package/src/utils/sheetNumberFormat.ts +1012 -0
  147. package/src/utils/sheetPreview.ts +68 -0
  148. package/src/utils/sheetSequence.ts +118 -0
  149. package/src/utils/sheetSnapshot.ts +739 -0
  150. package/src/utils/sheetStructureEdit.ts +914 -0
  151. package/src/utils/sheetStructureReferences.ts +389 -0
  152. package/src/utils/square.ts +22 -12
  153. package/src/utils/storage.ts +198 -0
  154. package/src/utils/table-route.ts +127 -11
  155. package/src/utils/time.ts +12 -0
  156. package/src/utils/workflow.ts +334 -40
  157. package/src/utils/workflowLogFingerprint.ts +302 -0
  158. package/src/utils/workflowLogRevisionSnapshot.ts +424 -0
  159. package/src/utils/workflowProgress.ts +280 -0
  160. package/src/utils/workflowTriggers.ts +61 -0
  161. package/tests/agentNote.test.ts +395 -0
  162. package/tests/campaignAI.test.ts +119 -0
  163. package/tests/campaignAccess.test.ts +239 -0
  164. package/tests/campaignEmail.test.ts +441 -0
  165. package/tests/campaignLifecycle.test.ts +134 -0
  166. package/tests/campaignPublication.test.ts +309 -0
  167. package/tests/dataTableFieldTypeRegistry.test.ts +111 -0
  168. package/tests/dataTableRelativeDate.test.ts +412 -0
  169. package/tests/docAccess.test.ts +525 -0
  170. package/tests/editLogEntry.test.ts +112 -0
  171. package/tests/email.test.ts +39 -0
  172. package/tests/fieldDocProgram.test.ts +114 -0
  173. package/tests/logDisplay.fixtures.ts +67 -0
  174. package/tests/logDisplay.test.ts +227 -0
  175. package/tests/noteDoc.test.ts +514 -0
  176. package/tests/richTextHTML.test.ts +185 -0
  177. package/tests/sheetAxisMove.test.ts +352 -0
  178. package/tests/sheetBootstrap.test.ts +1967 -0
  179. package/tests/sheetFeatureReference.test.ts +22 -0
  180. package/tests/sheetFormulaCriteria.test.ts +269 -0
  181. package/tests/sheetFormulaEvaluator.test.ts +468 -0
  182. package/tests/sheetFormulaFunctions.test.ts +538 -0
  183. package/tests/sheetFormulaLanguage.test.ts +361 -0
  184. package/tests/sheetFormulaParity.fixtures.ts +872 -0
  185. package/tests/sheetFormulaParity.test.ts +188 -0
  186. package/tests/sheetFormulaRange.test.ts +167 -0
  187. package/tests/sheetFormulaReference.test.ts +78 -0
  188. package/tests/sheetFormulaReferenceRewrite.test.ts +117 -0
  189. package/tests/sheetFormulaTextDate.test.ts +512 -0
  190. package/tests/sheetSequence.test.ts +22 -0
  191. package/tests/sheetSnapshot.test.ts +134 -0
  192. package/tests/sheetStructureEdit.test.ts +537 -0
  193. package/tests/sheetStructureReferences.test.ts +589 -0
  194. package/tests/workflowLifecycleStatus.test.ts +60 -0
  195. package/tests/workflowLogFingerprint.test.ts +203 -0
  196. package/tests/workflowLogRevisionSnapshot.test.ts +96 -0
  197. package/tests/workflowProgress.test.ts +180 -0
  198. package/tests/workflowTriggers.test.ts +29 -0
  199. package/tsconfig.json +10 -10
  200. package/vitest.config.ts +8 -0
  201. package/src/constants/log-activityTexts.ts +0 -235
  202. package/src/constants/product.ts +0 -42
  203. package/src/constants/report.ts +0 -44
  204. package/src/schemas/organization.ts +0 -65
  205. package/src/schemas/productsQuery.ts +0 -53
  206. package/src/schemas/reportsQuery.ts +0 -20
  207. package/src/types/product.d.ts +0 -84
  208. package/src/types/report.d.ts +0 -290
  209. package/src/utils/product-calEvent.ts +0 -455
  210. package/src/utils/report.ts +0 -158
@@ -0,0 +1,538 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { collectSheetFormulaStaticReferences } from '../src/utils/sheetFormulaCompile.ts';
3
+ import { parseSheetFormulaExpression } from '../src/utils/sheetFormulaParser.ts';
4
+ import { getSheetFormulaColumnLabel, type SheetFormulaASTNode } from '../src/utils/sheetFormulaSyntax.ts';
5
+ import {
6
+ evaluateSheetFormulaExpressionAsync,
7
+ evaluateSheetFormulaExpressionSync,
8
+ type SheetFormulaEvaluatorResult,
9
+ type SheetFormulaReferenceResolver,
10
+ } from '../src/utils/sheetFormulaEvaluator.ts';
11
+
12
+ /**
13
+ * Types
14
+ */
15
+
16
+ type TestCells = Record<string, unknown>;
17
+
18
+ /* Per-test context: literal cells keyed by A1, plus the A1 keys read so far
19
+ * (to prove lazy branches never evaluate). */
20
+ type TestContext = {
21
+ cells: TestCells;
22
+ reads: string[];
23
+ };
24
+
25
+ const TEST_NOW = new Date('2026-01-02T03:04:05.000Z');
26
+
27
+ /*
28
+ * Return the A1 key of one parsed cell coordinate.
29
+ */
30
+ function getTestA1(rowIndex: number, columnIndex: number) {
31
+ return `${getSheetFormulaColumnLabel(columnIndex)}${rowIndex}`;
32
+ }
33
+
34
+ /* Dense in-memory resolver over A1-keyed literal cells. A cell holding an
35
+ * Error object resolves as that formula error (a referenced error cell). */
36
+ const TEST_RESOLVER: SheetFormulaReferenceResolver<TestContext> = {
37
+ resolveCellReference(context, node) {
38
+ const key = getTestA1(node.reference.rowIndex, node.reference.columnIndex);
39
+ context.reads.push(key);
40
+ const value = context.cells[key];
41
+ return value instanceof Error
42
+ ? {
43
+ error: { code: value.message, message: value.message },
44
+ references: [],
45
+ value: '#ERROR!',
46
+ }
47
+ : { references: [], value: value ?? null };
48
+ },
49
+ resolveRangeReference(context, node) {
50
+ const values: unknown[] = [];
51
+ for (let row = node.reference.startRowIndex; row <= Math.min(node.reference.endRowIndex, 500); row++) {
52
+ for (let column = node.reference.startColumnIndex; column <= Math.min(node.reference.endColumnIndex, 256); column++) {
53
+ const value = context.cells[getTestA1(row, column)];
54
+ // Error cells inside ranges read as blank (policy B)
55
+ values.push(value instanceof Error ? null : (value ?? null));
56
+ }
57
+ }
58
+ return { references: [], value: { kind: 'SHEET_RANGE_VALUE', values } };
59
+ },
60
+ };
61
+
62
+ /*
63
+ * Evaluate one formula against literal cells.
64
+ */
65
+ function evaluate(formula: string, cells: TestCells = {}, context: TestContext = { cells, reads: [] }): SheetFormulaEvaluatorResult {
66
+ return evaluateSheetFormulaExpressionSync({
67
+ expression: formula.replace(/^=/, ''),
68
+ now: TEST_NOW,
69
+ resolver: TEST_RESOLVER,
70
+ resolverContext: context,
71
+ timeZone: 'UTC',
72
+ });
73
+ }
74
+
75
+ /*
76
+ * Assert one formula evaluates to a value without an error.
77
+ */
78
+ function expectValue(formula: string, cells: TestCells, expected: unknown) {
79
+ const result = evaluate(formula, cells);
80
+ expect(result.error, `${formula}: ${result.error?.message}`).toBeFalsy();
81
+ expect(result.value, formula).toEqual(expected);
82
+ }
83
+
84
+ /*
85
+ * Assert one formula evaluates to an error code and its display token.
86
+ */
87
+ function expectError(formula: string, cells: TestCells, code: string, display?: string) {
88
+ const result = evaluate(formula, cells);
89
+ expect(result.error?.code, formula).toBe(code);
90
+ if (display) {
91
+ expect(result.value, formula).toBe(display);
92
+ }
93
+ }
94
+
95
+ /*
96
+ * Return A1-keyed cells for one column of values starting at a row.
97
+ */
98
+ function column(label: string, values: unknown[], startRow = 1): TestCells {
99
+ return Object.fromEntries(values.map((value, index) => [`${label}${startRow + index}`, value]));
100
+ }
101
+
102
+ describe('AND / OR / XOR / NOT', () => {
103
+ it('combines scalars and ranges', () => {
104
+ expectValue('=AND(TRUE, 1, "true")', {}, true);
105
+ expectValue('=AND(TRUE, 0)', {}, false);
106
+ expectValue('=OR(FALSE, 0, A1)', { A1: 2 }, true);
107
+ expectValue('=XOR(TRUE, FALSE)', {}, true);
108
+ expectValue('=XOR(TRUE, TRUE)', {}, false);
109
+ expectValue('=AND(A1:A4)', column('A', [true, 'text', null, 5]), true);
110
+ expectValue('=OR(A1:A3)', column('A', ['yes', null, 0]), false);
111
+ });
112
+
113
+ it('skips blank scalars but needs at least one logical value', () => {
114
+ expectValue('=AND(A1, TRUE)', {}, true);
115
+ expectError('=AND(A1)', {}, 'INVALID_VALUE', '#VALUE!');
116
+ expectError('=OR(A1:A3)', column('A', ['x', null, 'y']), 'INVALID_VALUE');
117
+ });
118
+
119
+ it('rejects scalar text that is not TRUE/FALSE', () => {
120
+ expectError('=OR("maybe", TRUE)', {}, 'INVALID_VALUE', '#VALUE!');
121
+ expectError('=NOT("maybe")', {}, 'INVALID_VALUE');
122
+ });
123
+
124
+ it('evaluates every argument (errors propagate, no short circuit)', () => {
125
+ expectError('=AND(FALSE, 1/0)', {}, 'DIVIDE_BY_ZERO', '#DIV/0!');
126
+ expectError('=OR(TRUE, 1/0)', {}, 'DIVIDE_BY_ZERO');
127
+ });
128
+
129
+ it('checks arity', () => {
130
+ expectError('=AND()', {}, 'INVALID_FORMULA');
131
+ expectError('=NOT(TRUE, FALSE)', {}, 'INVALID_FORMULA');
132
+ expectError('=NOT(A1:A2)', {}, 'INVALID_VALUE');
133
+ });
134
+
135
+ it('reverses one value; NOT of blank is TRUE', () => {
136
+ expectValue('=NOT(0)', {}, true);
137
+ expectValue('=NOT(A1)', {}, true);
138
+ expectValue('=NOT("TRUE")', {}, false);
139
+ });
140
+ });
141
+
142
+ describe('IFS / SWITCH', () => {
143
+ it('returns the first TRUE branch and never evaluates later branches', () => {
144
+ const context: TestContext = { cells: { A1: 5 }, reads: [] };
145
+ const result = evaluate('=IFS(A1 > 10, B1, A1 > 1, "mid", A1 > 0, C1, TRUE, 1/0)', {}, context);
146
+ expect(result.value).toBe('mid');
147
+ expect(context.reads).toEqual(['A1', 'A1']);
148
+ });
149
+
150
+ it('reports misses, bad conditions and arity', () => {
151
+ expectError('=IFS(FALSE, 1)', {}, 'NOT_FOUND', '#N/A');
152
+ expectError('=IFS("maybe", 1)', {}, 'INVALID_VALUE');
153
+ expectError('=IFS(A1:A2, 1)', {}, 'INVALID_VALUE');
154
+ expectError('=IFS(TRUE)', {}, 'INVALID_FORMULA');
155
+ expectError('=IFS(TRUE, 1, FALSE)', {}, 'INVALID_FORMULA');
156
+ expectError('=IFS(1/0, 1, TRUE, 2)', {}, 'DIVIDE_BY_ZERO');
157
+ expectValue('=IFERROR(IFS(FALSE, 1), "none")', {}, 'none');
158
+ expectValue('=IFNA(IFS(FALSE, 1), "none")', {}, 'none');
159
+ });
160
+
161
+ it('matches SWITCH cases case-insensitively and lazily', () => {
162
+ const context: TestContext = { cells: { A1: 'B' }, reads: [] };
163
+ const result = evaluate('=SWITCH(A1, "a", C1, "b", "bee", D1, E1, 1/0)', {}, context);
164
+ expect(result.value).toBe('bee');
165
+ expect(context.reads).toEqual(['A1']);
166
+ expectValue('=SWITCH(2, "2", "two", "other")', {}, 'two');
167
+ expectValue('=SWITCH(A1, "", "empty", "other")', {}, 'empty');
168
+ expectValue('=SWITCH(7, 1, "one", "other")', {}, 'other');
169
+ });
170
+
171
+ it('reports SWITCH misses and errors', () => {
172
+ expectError('=SWITCH(7, 1, "one", 2, "two")', {}, 'NOT_FOUND', '#N/A');
173
+ expectError('=SWITCH(1/0, 1, "one")', {}, 'DIVIDE_BY_ZERO');
174
+ expectError('=SWITCH(1, 1/0, "one")', {}, 'DIVIDE_BY_ZERO');
175
+ expectError('=SWITCH(A1:A2, 1, "one")', {}, 'INVALID_VALUE');
176
+ expectError('=SWITCH(1, 1)', {}, 'INVALID_FORMULA');
177
+ });
178
+ });
179
+
180
+ describe('conditional aggregates', () => {
181
+ const sales = {
182
+ ...column('A', ['East', 'west', 'east', null, 'EAST']),
183
+ ...column('B', [10, 20, 'x', 40, true]),
184
+ ...column('C', ['2026-01-01', '2026-01-05', '2026-01-09', null, 'soon']),
185
+ };
186
+
187
+ it('counts, sums and averages with one or many criteria', () => {
188
+ expectValue('=COUNTIF(A1:A5, "east")', sales, 3);
189
+ expectValue('=COUNTIF(A1:A5, "<>east")', sales, 2);
190
+ expectValue('=COUNTIF(B1:B5, ">15")', sales, 2);
191
+ expectValue('=SUMIF(A1:A5, "east", B1:B5)', sales, 11);
192
+ expectValue('=SUMIF(B1:B5, ">15")', sales, 60);
193
+ expectValue('=AVERAGEIF(A1:A5, "east", B1:B5)', sales, 5.5);
194
+ expectValue('=COUNTIFS(A1:A5, "east", C1:C5, ">=2026-01-02")', sales, 1);
195
+ expectValue('=SUMIFS(B1:B5, A1:A5, "e*", C1:C5, "<2026-01-05")', sales, 10);
196
+ expectValue('=AVERAGEIFS(B1:B5, A1:A5, "*", B1:B5, "<>20")', sales, 5.5);
197
+ expectValue('=COUNTIFS(A1:A5, "", B1:B5, 40)', sales, 1);
198
+ });
199
+
200
+ it('builds criteria with & from cells and TODAY()', () => {
201
+ expectValue('=COUNTIF(B1:B5, ">="&D1)', { ...sales, D1: 20 }, 2);
202
+ expectValue('=COUNTIF(C1:C5, "<"&TODAY())', sales, 1);
203
+ });
204
+
205
+ it('rejects mismatched shapes (D12) and range criteria', () => {
206
+ expectError('=SUMIF(A1:A5, "east", B1:B4)', sales, 'INVALID_VALUE', '#VALUE!');
207
+ expectError('=COUNTIFS(A1:A5, "east", B1:C5, 1)', sales, 'INVALID_VALUE');
208
+ expectError('=SUMIFS(B1:B3, A1:A5, "east")', sales, 'INVALID_VALUE');
209
+ expectError('=COUNTIF(A1:A5, B1:B2)', sales, 'INVALID_VALUE');
210
+ expectError('=COUNTIF(A1, "east")', sales, 'INVALID_VALUE');
211
+ expectError('=SUMIF(A1:A5, "east", B1)', sales, 'INVALID_VALUE');
212
+ });
213
+
214
+ it('checks arity and pairs', () => {
215
+ expectError('=COUNTIF(A1:A5)', sales, 'INVALID_FORMULA');
216
+ expectError('=COUNTIFS(A1:A5, "x", B1:B5)', sales, 'INVALID_FORMULA');
217
+ expectError('=SUMIFS(B1:B5, A1:A5)', sales, 'INVALID_FORMULA');
218
+ expectError('=AVERAGEIFS(B1:B5, A1:A5, "x", C1:C5)', sales, 'INVALID_FORMULA');
219
+ });
220
+
221
+ it('divides by zero when an average has no numeric matches', () => {
222
+ expectError('=AVERAGEIF(A1:A5, "nobody", B1:B5)', sales, 'DIVIDE_BY_ZERO', '#DIV/0!');
223
+ expectError('=AVERAGEIFS(B1:B5, A1:A5, "west", B1:B5, ">100")', sales, 'DIVIDE_BY_ZERO');
224
+ });
225
+
226
+ it('reads error cells inside ranges as blank and propagates argument errors', () => {
227
+ const cells = { ...column('A', [1, new Error('DIVIDE_BY_ZERO'), 3]) };
228
+ expectValue('=SUMIF(A1:A3, "<>0")', cells, 4);
229
+ expectValue('=COUNTBLANK(A1:A3)', cells, 1);
230
+ expectValue('=COUNTIF(A1:A3, "")', cells, 1);
231
+ expectError('=COUNTIF(A1:A3, 1/0)', cells, 'DIVIDE_BY_ZERO');
232
+ expectError('=COUNTIF(A1:A3, A2)', cells, 'DIVIDE_BY_ZERO');
233
+ });
234
+
235
+ it('rejects wildcard criteria over 255 characters', () => {
236
+ expectError(`=COUNTIF(A1:A3, "${'a'.repeat(260)}*")`, {}, 'INVALID_VALUE');
237
+ });
238
+
239
+ it('counts blanks', () => {
240
+ expectValue('=COUNTBLANK(A1:B2)', { A1: 0, B2: '' }, 3);
241
+ expectError('=COUNTBLANK(1)', {}, 'INVALID_VALUE');
242
+ expectError('=COUNTBLANK(A1:A2, B1:B2)', {}, 'INVALID_FORMULA');
243
+ });
244
+
245
+ it('multiplies and sums same-shape ranges', () => {
246
+ const cells = {
247
+ ...column('A', [1, 2, 3]),
248
+ ...column('B', [4, '5', 'x']),
249
+ };
250
+ expectValue('=SUMPRODUCT(A1:A3, B1:B3)', cells, 14);
251
+ expectValue('=SUMPRODUCT(A1:A3)', cells, 6);
252
+ expectValue('=SUMPRODUCT(4, 5)', {}, 20);
253
+ expectError('=SUMPRODUCT(A1:A3, B1:B2)', cells, 'INVALID_VALUE');
254
+ expectError('=SUMPRODUCT(A1:A3, 2)', cells, 'INVALID_VALUE');
255
+ });
256
+
257
+ it('scans a whole-sheet range quickly', () => {
258
+ const started = performance.now();
259
+ expectValue('=COUNTIF(A1:IV500, "*x*")', { B7: 'axe' }, 1);
260
+ expect(performance.now() - started).toBeLessThan(1_000);
261
+ });
262
+ });
263
+
264
+ describe('VLOOKUP / HLOOKUP', () => {
265
+ const prices = {
266
+ ...column('A', ['apple', 'Banana', 'cherry', 10, null, 'date']),
267
+ ...column('B', [1.5, 2, null, 'ten', 'blank key', 4]),
268
+ };
269
+
270
+ it('finds exact matches case-insensitively with wildcards', () => {
271
+ expectValue('=VLOOKUP("banana", A1:B6, 2, FALSE)', prices, 2);
272
+ expectValue('=VLOOKUP("ch*", A1:B6, 2, FALSE)', prices, null);
273
+ expectValue('=VLOOKUP("d?te", A1:B6, 2, FALSE)', prices, 4);
274
+ expectValue('=VLOOKUP("10", A1:B6, 2, FALSE)', prices, 'ten');
275
+ expectValue('=VLOOKUP("apple", A1:B6, 1, FALSE)', prices, 'apple');
276
+ expectError('=VLOOKUP("kiwi", A1:B6, 2, FALSE)', prices, 'NOT_FOUND', '#N/A');
277
+ expectError('=VLOOKUP(A10, A1:B6, 2, FALSE)', prices, 'NOT_FOUND');
278
+ });
279
+
280
+ it('defaults to the approximate match on sorted keys', () => {
281
+ const tiers = {
282
+ ...column('A', [0, 100, 500, 1000]),
283
+ ...column('B', ['bronze', 'silver', 'gold', 'platinum']),
284
+ };
285
+ expectValue('=VLOOKUP(250, A1:B4, 2)', tiers, 'silver');
286
+ expectValue('=VLOOKUP(500, A1:B4, 2, TRUE)', tiers, 'gold');
287
+ expectValue('=VLOOKUP(5000, A1:B4, 2)', tiers, 'platinum');
288
+ expectError('=VLOOKUP(-1, A1:B4, 2)', tiers, 'NOT_FOUND');
289
+ // Unsorted: the last candidate before the first larger key
290
+ expectValue('=VLOOKUP(7, A1:B4, 2)', { ...column('A', [1, 5, 9, 3]), ...column('B', ['a', 'b', 'c', 'd']) }, 'b');
291
+ // Other types are skipped, so text keys do not end a numeric scan
292
+ expectValue('=VLOOKUP(7, A1:B3, 2)', { ...column('A', [1, 'x', 5]), ...column('B', ['a', 'b', 'c']) }, 'c');
293
+ });
294
+
295
+ it('validates the index', () => {
296
+ expectError('=VLOOKUP("apple", A1:B6, 3, FALSE)', prices, 'INVALID_REFERENCE', '#REF!');
297
+ expectError('=VLOOKUP("apple", A1:B6, 0, FALSE)', prices, 'INVALID_VALUE');
298
+ expectError('=VLOOKUP("apple", A1:B6, "x", FALSE)', prices, 'INVALID_VALUE');
299
+ expectError('=VLOOKUP("apple", A1:B6, 2, "maybe")', prices, 'INVALID_VALUE');
300
+ expectError('=VLOOKUP(A1:A2, A1:B6, 2, FALSE)', prices, 'INVALID_VALUE');
301
+ expectError('=VLOOKUP("apple", 5, 2, FALSE)', prices, 'INVALID_VALUE');
302
+ expectError('=VLOOKUP("apple", A1:B6)', prices, 'INVALID_FORMULA');
303
+ expectValue('=VLOOKUP("apple", A1:B6, 2.9, FALSE)', prices, 1.5);
304
+ });
305
+
306
+ it('looks across the first row with HLOOKUP', () => {
307
+ const quarters = {
308
+ A1: 'Q1',
309
+ B1: 'Q2',
310
+ C1: 'Q3',
311
+ A2: 10,
312
+ B2: 20,
313
+ C2: 30,
314
+ };
315
+ expectValue('=HLOOKUP("q3", A1:C2, 2, FALSE)', quarters, 30);
316
+ expectValue('=HLOOKUP("Q2", A1:C2, 1, FALSE)', quarters, 'Q2');
317
+ expectError('=HLOOKUP("Q2", A1:C2, 3, FALSE)', quarters, 'INVALID_REFERENCE');
318
+ expectValue('=HLOOKUP(25, A2:C2, 1)', quarters, 20);
319
+ });
320
+ });
321
+
322
+ describe('INDEX / MATCH', () => {
323
+ const grid = {
324
+ A1: 1,
325
+ B1: 2,
326
+ C1: 3,
327
+ A2: 4,
328
+ B2: 5,
329
+ C2: null,
330
+ };
331
+
332
+ it('reads positions of 1-D and 2-D ranges', () => {
333
+ expectValue('=INDEX(A1:C2, 2, 2)', grid, 5);
334
+ expectValue('=INDEX(A1:C2, 2, 3)', grid, null);
335
+ expectValue('=INDEX(A1:C1, 2)', grid, 2);
336
+ expectValue('=INDEX(A1:A2, 2)', grid, 4);
337
+ expectValue('=INDEX(A1:A2, 2, 1)', grid, 4);
338
+ expectValue('=INDEX(A1:A1, 1)', grid, 1);
339
+ });
340
+
341
+ it('reports out-of-range, zero and missing column positions', () => {
342
+ expectError('=INDEX(A1:C2, 3, 1)', grid, 'INVALID_REFERENCE', '#REF!');
343
+ expectError('=INDEX(A1:C2, 1, 4)', grid, 'INVALID_REFERENCE');
344
+ expectError('=INDEX(A1:C2, 0, 1)', grid, 'INVALID_VALUE');
345
+ expectError('=INDEX(A1:C2, -1, 1)', grid, 'INVALID_VALUE');
346
+ expectError('=INDEX(A1:C2, 1)', grid, 'INVALID_VALUE');
347
+ expectError('=INDEX(5, 1)', grid, 'INVALID_VALUE');
348
+ expectError('=INDEX(A1:C2, "x", 1)', grid, 'INVALID_VALUE');
349
+ });
350
+
351
+ it('finds 1-based positions with each match type', () => {
352
+ const names = column('A', ['apple', 'Banana', 'cherry']);
353
+ expectValue('=MATCH("banana", A1:A3, 0)', names, 2);
354
+ expectValue('=MATCH("*rry", A1:A3, 0)', names, 3);
355
+ expectValue('=MATCH(25, A1:A3)', column('A', [10, 20, 30]), 2);
356
+ expectValue('=MATCH(30, A1:A3, 1)', column('A', [10, 20, 30]), 3);
357
+ expectValue('=MATCH(15, A1:A3, -1)', column('A', [30, 20, 10]), 2);
358
+ expectValue('=MATCH(20, A1:C1, 0)', { A1: 10, B1: 20, C1: 30 }, 2);
359
+ expectError('=MATCH(5, A1:A3)', column('A', [10, 20, 30]), 'NOT_FOUND');
360
+ expectError('=MATCH("kiwi", A1:A3, 0)', names, 'NOT_FOUND', '#N/A');
361
+ expectError('=MATCH(1, A1:C2, 0)', grid, 'NOT_FOUND');
362
+ expectError('=MATCH(1, A1:A3, "x")', names, 'INVALID_VALUE');
363
+ });
364
+
365
+ it('combines INDEX and MATCH for a left lookup', () => {
366
+ expectValue('=INDEX(A1:A3, MATCH("y", B1:B3, 0))', { ...column('A', [1, 2, 3]), ...column('B', ['x', 'Y', 'z']) }, 2);
367
+ });
368
+ });
369
+
370
+ describe('XLOOKUP', () => {
371
+ const cells = {
372
+ ...column('A', ['a', 'B', 'c', 'b']),
373
+ ...column('B', [1, 2, 3, 4]),
374
+ };
375
+
376
+ it('finds exact matches first to last or last to first', () => {
377
+ expectValue('=XLOOKUP("b", A1:A4, B1:B4)', cells, 2);
378
+ expectValue('=XLOOKUP("b", A1:A4, B1:B4, "none", 0, -1)', cells, 4);
379
+ expectValue('=XLOOKUP("b", A1:A4, B1:B4, "none", 0, 2)', cells, 2);
380
+ expectValue('=XLOOKUP("b", A1:A4, B1:B4, "none", 0, -2)', cells, 4);
381
+ expectValue('=XLOOKUP("b*", A1:A4, B1:B4, "none")', cells, 'none');
382
+ expectValue('=XLOOKUP("b*", A1:A4, B1:B4, "none", 2)', cells, 2);
383
+ });
384
+
385
+ it('finds the next smaller or larger value without sorted data', () => {
386
+ const scores = {
387
+ ...column('A', [30, 10, 20, 20]),
388
+ ...column('B', ['c', 'a', 'b1', 'b2']),
389
+ };
390
+ expectValue('=XLOOKUP(25, A1:A4, B1:B4, "none", -1)', scores, 'b1');
391
+ expectValue('=XLOOKUP(25, A1:A4, B1:B4, "none", -1, -1)', scores, 'b2');
392
+ expectValue('=XLOOKUP(15, A1:A4, B1:B4, "none", 1)', scores, 'b1');
393
+ expectValue('=XLOOKUP(20, A1:A4, B1:B4, "none", 1)', scores, 'b1');
394
+ expectValue('=XLOOKUP(5, A1:A4, B1:B4, "none", -1)', scores, 'none');
395
+ expectValue('=XLOOKUP(99, A1:A4, B1:B4, "none", 1)', scores, 'none');
396
+ });
397
+
398
+ it('evaluates if_not_found only on a miss', () => {
399
+ const context: TestContext = { cells, reads: [] };
400
+ expect(evaluate('=XLOOKUP("a", A1:A4, B1:B4, D1)', {}, context).value).toBe(1);
401
+ expect(context.reads).toEqual([]);
402
+ expectValue('=XLOOKUP("a", A1:A4, B1:B4, 1/0)', cells, 1);
403
+ expectError('=XLOOKUP("z", A1:A4, B1:B4, 1/0)', cells, 'DIVIDE_BY_ZERO');
404
+ expectError('=XLOOKUP("z", A1:A4, B1:B4)', cells, 'NOT_FOUND', '#N/A');
405
+ expectValue('=XLOOKUP("z", A1:A4, B1:B4, D1)', { ...cells, D1: 'fallback' }, 'fallback');
406
+ });
407
+
408
+ it('validates ranges and modes', () => {
409
+ expectError('=XLOOKUP("a", A1:A4, B1:B3)', cells, 'INVALID_VALUE');
410
+ expectError('=XLOOKUP("a", A1:A4, B1:C4)', cells, 'INVALID_VALUE');
411
+ expectError('=XLOOKUP("a", A1:B4, A1:B4)', cells, 'INVALID_VALUE');
412
+ expectError('=XLOOKUP("a", A1:A4, B1:B4, "x", 3)', cells, 'INVALID_VALUE');
413
+ expectError('=XLOOKUP("a", A1:A4, B1:B4, "x", 0, 0)', cells, 'INVALID_VALUE');
414
+ expectError('=XLOOKUP("a", A1:A4)', cells, 'INVALID_FORMULA');
415
+ expectError('=XLOOKUP(1/0, A1:A4, B1:B4, "x")', cells, 'DIVIDE_BY_ZERO');
416
+ });
417
+ });
418
+
419
+ describe('UNIQUE', () => {
420
+ it('returns an intermediate array for range-reading functions', () => {
421
+ const cells = column('A', ['a', 'A', 'b', null, 'c', 'b']);
422
+ expectValue('=COUNTA(UNIQUE(A1:A6))', cells, 3);
423
+ expectValue('=INDEX(UNIQUE(A1:A6), 3)', cells, 'c');
424
+ expectValue('=COUNTA(UNIQUE(A1:A6, FALSE, TRUE))', cells, 1);
425
+ expectValue('=SUM(UNIQUE(A1:A4))', column('A', [1, '1', 2, 2]), 3);
426
+ expectValue('=INCLUDES("b", UNIQUE(A1:A6))', cells, true);
427
+ expectValue('=COUNTIF(UNIQUE(A1:A6), "b")', cells, 1);
428
+ expectValue('=COUNTA(UNIQUE(A1:A2))', {}, 0);
429
+ });
430
+
431
+ it('deduplicates rows and columns of 2-D ranges', () => {
432
+ const rows = {
433
+ A1: 'x',
434
+ B1: 1,
435
+ A2: 'X',
436
+ B2: '1',
437
+ A3: 'x',
438
+ B3: 2,
439
+ A4: null,
440
+ B4: null,
441
+ };
442
+ expectValue('=COUNTA(UNIQUE(A1:B4))', rows, 4);
443
+ expectValue('=INDEX(UNIQUE(A1:B4), 2, 2)', rows, 2);
444
+ const columns = { A1: 1, B1: 1, C1: 2, A2: 'a', B2: 'A', C2: 'b' };
445
+ expectValue('=COUNTA(UNIQUE(A1:C2, TRUE))', columns, 4);
446
+ expectValue('=INDEX(UNIQUE(A1:C2, TRUE), 2, 2)', columns, 'b');
447
+ });
448
+
449
+ it('cannot be the whole formula result', () => {
450
+ const result = evaluate('=UNIQUE(A1:A3)', column('A', [1, 2, 3]));
451
+ expect(result.error?.code).toBe('INVALID_FORMULA');
452
+ expect(result.error?.message).toContain('UNIQUE returns several values');
453
+ expectError('=UNIQUE(A1:A3)+1', {}, 'INVALID_FORMULA');
454
+ expectError('=UNIQUE(5)', {}, 'INVALID_VALUE');
455
+ expectError('=UNIQUE(A1:A3, "maybe")', {}, 'INVALID_VALUE');
456
+ });
457
+
458
+ it('stays linear on a whole-sheet range', () => {
459
+ const cells: TestCells = {};
460
+ for (let row = 1; row <= 500; row++) {
461
+ cells[`A${row}`] = `v${row % 250}`;
462
+ }
463
+ const started = performance.now();
464
+ expectValue('=COUNTA(UNIQUE(A1:IV500))', cells, 250);
465
+ expect(performance.now() - started).toBeLessThan(1_000);
466
+ });
467
+ });
468
+
469
+ describe('new builtin names', () => {
470
+ it('reject unsupported @name spellings', () => {
471
+ expectError('=@vlookup("a", "b")', {}, 'UNKNOWN_FUNCTION');
472
+ expectError('=@index("a", "b")', {}, 'UNKNOWN_FUNCTION');
473
+ expectError('=@and("a", "b")', {}, 'UNKNOWN_FUNCTION');
474
+ });
475
+
476
+ it('register every range argument as a dependency, including lazy branches', () => {
477
+ const node = parseSheetFormulaExpression(
478
+ 'SUMIFS(C2:C10, A2:A10, D1, sales!B2:B, ">"&E1) + XLOOKUP(F1, G1:G5, H1:H5, I1) + IFS(J1, K1:K2, TRUE, 0) + SWITCH(L1, M1, N1)',
479
+ ) as SheetFormulaASTNode;
480
+ const references = collectSheetFormulaStaticReferences(node);
481
+ expect(
482
+ references.ranges.map(
483
+ (range) =>
484
+ `${range.sheetName || ''}!${range.startRowIndex}:${range.startColumnIndex}-${range.endRowIndex}:${range.endColumnIndex}`,
485
+ ),
486
+ ).toEqual(['!2:3-10:3', '!2:1-10:1', 'sales!2:2-500:2', '!1:7-5:7', '!1:8-5:8', '!1:11-2:11']);
487
+ expect(references.cells.map((cell) => getTestA1(cell.rowIndex, cell.columnIndex))).toEqual([
488
+ 'D1',
489
+ 'E1',
490
+ 'F1',
491
+ 'I1',
492
+ 'J1',
493
+ 'L1',
494
+ 'M1',
495
+ 'N1',
496
+ ]);
497
+ expect(references.volatile).toBe(false);
498
+ expect(
499
+ collectSheetFormulaStaticReferences(parseSheetFormulaExpression('COUNTIF(A1:A5, "<"&TODAY())') as SheetFormulaASTNode).volatile,
500
+ ).toBe(true);
501
+ });
502
+
503
+ it('evaluate the lazy functions through an async resolver', async () => {
504
+ const asyncResolver: SheetFormulaReferenceResolver<TestContext> = {
505
+ ...TEST_RESOLVER,
506
+ resolveCellReference: (context, node, runtime) => Promise.resolve(TEST_RESOLVER.resolveCellReference(context, node, runtime)),
507
+ resolveRangeReference: (context, node, runtime) => Promise.resolve(TEST_RESOLVER.resolveRangeReference(context, node, runtime)),
508
+ };
509
+ const cells = {
510
+ ...column('A', ['x', 'y']),
511
+ ...column('B', [1, 2]),
512
+ C1: 'y',
513
+ };
514
+ for (const [formula, expected] of [
515
+ ['=XLOOKUP(C1, A1:A2, B1:B2, 1/0)', 2],
516
+ ['=IFS(C1 = "x", 1/0, C1 = "y", B2)', 2],
517
+ ['=SWITCH(C1, "x", 1/0, "y", B1)', 1],
518
+ ['=SUMIFS(B1:B2, A1:A2, C1)', 2],
519
+ ] as const) {
520
+ const result = await evaluateSheetFormulaExpressionAsync({
521
+ expression: formula.slice(1),
522
+ now: TEST_NOW,
523
+ resolver: asyncResolver,
524
+ resolverContext: { cells, reads: [] },
525
+ timeZone: 'UTC',
526
+ });
527
+ expect(result.error, formula).toBeFalsy();
528
+ expect(result.value, formula).toBe(expected);
529
+ }
530
+ });
531
+
532
+ it('parse every new function name as a builtin call', () => {
533
+ for (const formula of ['AND(1)', 'COUNTIFS(A1:A2, 1)', 'XLOOKUP(1, A1:A2, B1:B2)']) {
534
+ const node = parseSheetFormulaExpression(formula);
535
+ expect(node?.kind).toBe('FUNCTION_CALL');
536
+ }
537
+ });
538
+ });