@jsb188/mday 1.1.11 → 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.
- package/package.json +58 -23
- package/src/constants/agentMemory.ts +14 -45
- package/src/constants/agentNote.ts +212 -0
- package/src/constants/aiChat.ts +5 -0
- package/src/constants/billing.ts +126 -0
- package/src/constants/campaign.ts +156 -47
- package/src/constants/contact.ts +72 -0
- package/src/constants/csvExport.ts +133 -0
- package/src/constants/dataTable.ts +12 -38
- package/src/constants/dataTableFieldTypes.generated.ts +3 -7
- package/src/constants/docAccess.ts +3 -2
- package/src/constants/dt.ts +234 -0
- package/src/constants/email.ts +114 -12
- package/src/constants/fieldDoc.ts +74 -4
- package/src/constants/graphqlReactivity.ts +108 -25
- package/src/constants/log.ts +9 -0
- package/src/constants/logDisplay/README.md +88 -0
- package/src/constants/logDisplay/defaults.ts +39 -0
- package/src/constants/logDisplay/index.ts +27 -0
- package/src/constants/logDisplay/operations/arable.ts +16 -0
- package/src/constants/logDisplay/operations/farmersMarket.ts +10 -0
- package/src/constants/logDisplay/operations/foodManufacturing.ts +13 -0
- package/src/constants/logDisplay/shared/accounting.ts +13 -0
- package/src/constants/logDisplay/shared/interactions.ts +13 -0
- package/src/constants/logDisplay/shared/rackTemperatures.ts +42 -0
- package/src/constants/logTaxonomy.generated.ts +1834 -730
- package/src/constants/organization.ts +13 -14
- package/src/constants/qrCode.ts +30 -0
- package/src/constants/sheet.ts +65 -101
- package/src/constants/sms.ts +18 -0
- package/src/constants/storage.ts +33 -9
- package/src/constants/usage.ts +3 -0
- package/src/constants/workflowSteps.ts +60 -0
- package/src/schemas/editLogEntry.ts +106 -283
- package/src/schemas/inboundContact.ts +5 -5
- package/src/types/agentMemory.d.ts +38 -14
- package/src/types/agentNote.d.ts +443 -0
- package/src/types/billing.d.ts +537 -0
- package/src/types/campaign.d.ts +1043 -193
- package/src/types/contact.d.ts +15 -0
- package/src/types/csvExport.d.ts +213 -0
- package/src/types/dataAnalysis.d.ts +44 -0
- package/src/types/dataTable.d.ts +102 -309
- package/src/types/definitionElement.d.ts +35 -0
- package/src/types/dt.d.ts +257 -0
- package/src/types/email.d.ts +144 -55
- package/src/types/emailReplyReport.d.ts +76 -0
- package/src/types/fieldDoc.d.ts +579 -150
- package/src/types/fieldDocContract.d.ts +90 -0
- package/src/types/inboundContact.d.ts +8 -0
- package/src/types/log.d.ts +94 -3
- package/src/types/logDisplay.types.ts +85 -0
- package/src/types/organization.d.ts +8 -3
- package/src/types/qrCode.d.ts +96 -0
- package/src/types/richText.d.ts +81 -0
- package/src/types/sheet.d.ts +180 -463
- package/src/types/sms.d.ts +38 -0
- package/src/types/storage.d.ts +266 -27
- package/src/types/workflow.d.ts +196 -66
- package/src/utils/agentMemory.ts +15 -0
- package/src/utils/agentNote.ts +850 -0
- package/src/utils/aiChat.ts +31 -14
- package/src/utils/aiChatWork.ts +64 -0
- package/src/utils/billing.ts +37 -0
- package/src/utils/campaign.ts +642 -209
- package/src/utils/campaignAI.ts +72 -0
- package/src/utils/campaignAccess.ts +239 -0
- package/src/utils/campaignEmail.ts +601 -0
- package/src/utils/campaignEmailCompiler.ts +372 -0
- package/src/utils/campaignEmailImages.ts +60 -0
- package/src/utils/campaignEmailStyles.ts +544 -0
- package/src/utils/campaignLifecycle.ts +104 -0
- package/src/utils/campaignPreview.ts +285 -0
- package/src/utils/contact.ts +50 -0
- package/src/utils/csvExport.ts +19 -0
- package/src/utils/dataTable.ts +18 -556
- package/src/utils/dataTableFieldTypeRegistry.ts +1 -0
- package/src/utils/dataTableRelativeDate.ts +447 -0
- package/src/utils/definitionElement.ts +84 -0
- package/src/utils/email.ts +208 -1
- package/src/utils/emailReplyReport.test.ts +187 -0
- package/src/utils/emailReplyReport.ts +143 -0
- package/src/utils/fieldDoc.ts +81 -37
- package/src/utils/fieldDocAnswers.ts +128 -0
- package/src/utils/fieldDocDefaults.ts +36 -0
- package/src/utils/fieldDocProgram.ts +36 -0
- package/src/utils/fieldDocSummary.ts +30 -0
- package/src/utils/log.ts +232 -141
- package/src/utils/logDisplay.helpers.ts +177 -0
- package/src/utils/logDisplay.ts +87 -0
- package/src/utils/logStoreRead.ts +9 -13
- package/src/utils/logTaxonomy.helpers.ts +17 -0
- package/src/utils/logTaxonomyDisplay.ts +80 -124
- package/src/utils/noteDoc.ts +66 -284
- package/src/utils/organization.ts +117 -283
- package/src/utils/qrCode.helpers.ts +189 -0
- package/src/utils/qrCode.test.ts +519 -0
- package/src/utils/qrCode.ts +273 -0
- package/src/utils/richTextHTML.ts +385 -0
- package/src/utils/sheet.ts +123 -4834
- package/src/utils/sheetAxisMove.ts +77 -291
- package/src/utils/sheetBootstrap.ts +771 -1138
- package/src/utils/sheetCellLiteral.ts +101 -0
- package/src/utils/sheetCommand.ts +23 -88
- package/src/utils/sheetDesign.ts +705 -0
- package/src/utils/sheetExport.ts +17 -69
- package/src/utils/sheetFeatureReference.ts +183 -0
- package/src/utils/sheetFormulaArgs.ts +168 -0
- package/src/utils/sheetFormulaCompile.ts +475 -0
- package/src/utils/sheetFormulaConditionalAggregate.ts +254 -0
- package/src/utils/sheetFormulaCriteria.ts +656 -0
- package/src/utils/sheetFormulaDate.ts +444 -0
- package/src/utils/sheetFormulaEvaluation.ts +388 -403
- package/src/utils/sheetFormulaEvaluator.ts +589 -444
- package/src/utils/sheetFormulaFunctionHandlers.ts +403 -0
- package/src/utils/sheetFormulaLogical.ts +116 -0
- package/src/utils/sheetFormulaLookup.ts +488 -0
- package/src/utils/sheetFormulaMath.ts +48 -0
- package/src/utils/sheetFormulaParser.ts +729 -0
- package/src/utils/sheetFormulaRange.ts +330 -0
- package/src/utils/sheetFormulaReference.ts +703 -0
- package/src/utils/sheetFormulaReferenceAggregates.ts +313 -0
- package/src/utils/sheetFormulaReferenceDates.ts +237 -0
- package/src/utils/sheetFormulaReferenceLogical.ts +330 -0
- package/src/utils/sheetFormulaReferenceLookup.ts +341 -0
- package/src/utils/sheetFormulaReferenceMath.ts +95 -0
- package/src/utils/sheetFormulaReferenceRewrite.ts +220 -0
- package/src/utils/sheetFormulaReferenceText.ts +462 -0
- package/src/utils/sheetFormulaSyntax.ts +1027 -0
- package/src/utils/sheetFormulaText.ts +430 -0
- package/src/utils/sheetJournal.ts +140 -18
- package/src/utils/sheetNumberFormat.ts +68 -267
- package/src/utils/sheetPreview.ts +68 -0
- package/src/utils/sheetSequence.ts +13 -43
- package/src/utils/sheetSnapshot.ts +176 -429
- package/src/utils/sheetStructureEdit.ts +537 -907
- package/src/utils/sheetStructureReferences.ts +389 -0
- package/src/utils/storage.ts +49 -3
- package/src/utils/time.ts +12 -0
- package/src/utils/workflow.ts +199 -73
- package/src/utils/workflowLogFingerprint.ts +302 -0
- package/src/utils/workflowLogRevisionSnapshot.ts +424 -0
- package/src/utils/workflowProgress.ts +280 -0
- package/src/utils/workflowTriggers.ts +61 -0
- package/tests/agentNote.test.ts +395 -0
- package/tests/campaignAI.test.ts +119 -0
- package/tests/campaignAccess.test.ts +239 -0
- package/tests/campaignEmail.test.ts +441 -0
- package/tests/campaignLifecycle.test.ts +134 -0
- package/tests/campaignPublication.test.ts +309 -0
- package/tests/dataTableFieldTypeRegistry.test.ts +2 -1
- package/tests/dataTableRelativeDate.test.ts +412 -0
- package/tests/email.test.ts +39 -0
- package/tests/fieldDocProgram.test.ts +114 -0
- package/tests/logDisplay.fixtures.ts +67 -0
- package/tests/logDisplay.test.ts +227 -0
- package/tests/noteDoc.test.ts +37 -192
- package/tests/richTextHTML.test.ts +185 -0
- package/tests/sheetAxisMove.test.ts +49 -40
- package/tests/sheetBootstrap.test.ts +1967 -0
- package/tests/sheetFeatureReference.test.ts +22 -0
- package/tests/sheetFormulaCriteria.test.ts +269 -0
- package/tests/sheetFormulaEvaluator.test.ts +93 -200
- package/tests/sheetFormulaFunctions.test.ts +538 -0
- package/tests/sheetFormulaLanguage.test.ts +361 -0
- package/tests/sheetFormulaParity.fixtures.ts +872 -0
- package/tests/sheetFormulaParity.test.ts +188 -0
- package/tests/sheetFormulaRange.test.ts +167 -0
- package/tests/sheetFormulaReference.test.ts +78 -0
- package/tests/sheetFormulaReferenceRewrite.test.ts +117 -0
- package/tests/sheetFormulaTextDate.test.ts +512 -0
- package/tests/sheetSequence.test.ts +4 -23
- package/tests/sheetSnapshot.test.ts +134 -0
- package/tests/sheetStructureEdit.test.ts +492 -37
- package/tests/sheetStructureReferences.test.ts +589 -0
- package/tests/workflowLifecycleStatus.test.ts +60 -0
- package/tests/workflowLogFingerprint.test.ts +203 -0
- package/tests/workflowLogRevisionSnapshot.test.ts +96 -0
- package/tests/workflowProgress.test.ts +180 -0
- package/tests/workflowTriggers.test.ts +29 -0
- package/src/constants/agentDoc.ts +0 -12
- package/src/constants/logActivityGroupColors.generated.ts +0 -19
- package/src/constants/schedule.ts +0 -220
- package/src/constants/scheduleRuleTemplateSeed.ts +0 -138
- package/src/constants/scheduleRuleTemplates.generated.ts +0 -145
- package/src/constants/sheetRegionSources.generated.ts +0 -578
- package/src/schemas/fieldDocsQuery.ts +0 -12
- package/src/schemas/organization.ts +0 -68
- package/src/types/agentDoc.d.ts +0 -64
- package/src/types/noteDoc.d.ts +0 -86
- package/src/types/schedule.d.ts +0 -391
- package/src/utils/agentDoc.ts +0 -109
- package/src/utils/campaignFormEditor.ts +0 -871
- package/src/utils/dataTableRealtimeAuthority.ts +0 -209
- package/src/utils/dataTableRealtimeSelection.ts +0 -134
- package/src/utils/dataTableRowDocument.ts +0 -508
- package/src/utils/dataTableSource.ts +0 -230
- package/src/utils/schedule.ts +0 -799
- package/src/utils/scheduleEvaluation.ts +0 -1994
- package/src/utils/scheduleICS.ts +0 -246
- package/src/utils/scheduleRuleTemplates.ts +0 -188
- package/src/utils/sheetRegion.ts +0 -374
- package/src/utils/sheetRegionFilter.ts +0 -1259
- package/src/utils/sheetRegionSourceRegistry.ts +0 -594
- package/tests/schedule.test.ts +0 -693
- package/tests/scheduleEvaluation.test.ts +0 -1970
- package/tests/scheduleICS.test.ts +0 -208
- package/tests/scheduleRuleTemplates.test.ts +0 -176
- package/tests/scheduleSweepInvariants.test.ts +0 -428
- package/tests/sheetRegionFilter.test.ts +0 -1135
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { getSheetFormulaExpression, parseSheetFormulaCellReference } from '../src/utils/sheetFormulaSyntax.ts';
|
|
3
|
+
import {
|
|
4
|
+
buildSheetFormulaErrorResult,
|
|
5
|
+
evaluateSheetFormulaExpressionSync,
|
|
6
|
+
type SheetFormulaEvaluatorResult,
|
|
7
|
+
type SheetFormulaReferenceResolver,
|
|
8
|
+
} from '../src/utils/sheetFormulaEvaluator.ts';
|
|
9
|
+
import {
|
|
10
|
+
densifySheetFormulaRangeEntries,
|
|
11
|
+
getSheetFormulaRangeBounds,
|
|
12
|
+
getSheetFormulaReferencedCellError,
|
|
13
|
+
} from '../src/utils/sheetFormulaRange.ts';
|
|
14
|
+
import {
|
|
15
|
+
getSheetFormulaParityMismatch,
|
|
16
|
+
SHEET_FORMULA_PARITY_CASES,
|
|
17
|
+
SHEET_FORMULA_PARITY_DEFAULTS,
|
|
18
|
+
type SheetFormulaParityCase,
|
|
19
|
+
type SheetFormulaParityCell,
|
|
20
|
+
} from './sheetFormulaParity.fixtures.ts';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Types
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
type ParityCoord = {
|
|
27
|
+
columnIndex: number;
|
|
28
|
+
rowIndex: number;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
type ParityContext = {
|
|
32
|
+
at: ParityCoord;
|
|
33
|
+
cells: Array<ParityCoord & { cell: SheetFormulaParityCell }>;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/*
|
|
37
|
+
* Return the row/column of one A1 fixture coordinate.
|
|
38
|
+
*/
|
|
39
|
+
function getParityCoord(a1: string): ParityCoord {
|
|
40
|
+
const reference = parseSheetFormulaCellReference(a1);
|
|
41
|
+
if (!reference) {
|
|
42
|
+
throw new Error(`Invalid parity fixture coordinate ${a1}`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
columnIndex: reference.columnIndex,
|
|
47
|
+
rowIndex: reference.rowIndex,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/*
|
|
52
|
+
* Return the stored result a resolver reads for one fixture cell: literals
|
|
53
|
+
* are values, formula cells expose what the recalc engine stored.
|
|
54
|
+
*/
|
|
55
|
+
function getParityStoredResult(cell?: SheetFormulaParityCell): {
|
|
56
|
+
error?: { code: string; message: string };
|
|
57
|
+
value: unknown;
|
|
58
|
+
} {
|
|
59
|
+
if (cell === null || cell === undefined || typeof cell !== 'object') {
|
|
60
|
+
return { value: cell ?? null };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return 'errorCode' in cell.stored
|
|
64
|
+
? {
|
|
65
|
+
error: {
|
|
66
|
+
code: cell.stored.errorCode,
|
|
67
|
+
message: cell.stored.errorMessage || cell.stored.errorCode,
|
|
68
|
+
},
|
|
69
|
+
value: null,
|
|
70
|
+
}
|
|
71
|
+
: { value: cell.stored.value };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/* Stored-value resolver built only from the shared range/error helpers: it
|
|
75
|
+
* reads what the engine stored (like the server) and applies policy B. */
|
|
76
|
+
const PARITY_RESOLVER: SheetFormulaReferenceResolver<ParityContext> = {
|
|
77
|
+
resolveCellReference(context, node) {
|
|
78
|
+
if (node.reference.sheetName) {
|
|
79
|
+
throw new Error('Parity fixtures must stay on one tab.');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (node.reference.rowIndex === context.at.rowIndex && node.reference.columnIndex === context.at.columnIndex) {
|
|
83
|
+
return buildSheetFormulaErrorResult('CIRCULAR_REFERENCE', 'Formula cannot reference its own cell.');
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const entry = context.cells.find(
|
|
87
|
+
(cell) => cell.rowIndex === node.reference.rowIndex && cell.columnIndex === node.reference.columnIndex,
|
|
88
|
+
);
|
|
89
|
+
const stored = getParityStoredResult(entry?.cell);
|
|
90
|
+
if (stored.error) {
|
|
91
|
+
const error = getSheetFormulaReferencedCellError(stored.error);
|
|
92
|
+
return buildSheetFormulaErrorResult(error.code, error.message);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return { references: [], value: stored.value ?? null };
|
|
96
|
+
},
|
|
97
|
+
resolveRangeReference(context, node) {
|
|
98
|
+
if (node.reference.sheetName) {
|
|
99
|
+
throw new Error('Parity fixtures must stay on one tab.');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (
|
|
103
|
+
context.at.rowIndex >= node.reference.startRowIndex &&
|
|
104
|
+
context.at.rowIndex <= node.reference.endRowIndex &&
|
|
105
|
+
context.at.columnIndex >= node.reference.startColumnIndex &&
|
|
106
|
+
context.at.columnIndex <= node.reference.endColumnIndex
|
|
107
|
+
) {
|
|
108
|
+
return buildSheetFormulaErrorResult('CIRCULAR_REFERENCE', 'Formula cannot include its own cell in a referenced range.');
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const outcome = densifySheetFormulaRangeEntries(
|
|
112
|
+
context.cells.map((entry) => ({
|
|
113
|
+
...getParityStoredResult(entry.cell),
|
|
114
|
+
columnIndex: entry.columnIndex,
|
|
115
|
+
rowIndex: entry.rowIndex,
|
|
116
|
+
})),
|
|
117
|
+
getSheetFormulaRangeBounds(node.reference),
|
|
118
|
+
);
|
|
119
|
+
if (outcome.status === 'ERROR') {
|
|
120
|
+
return buildSheetFormulaErrorResult(outcome.error.code, outcome.error.message);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
references: [],
|
|
125
|
+
value: {
|
|
126
|
+
kind: 'SHEET_RANGE_VALUE',
|
|
127
|
+
values: outcome.status === 'READY' ? outcome.values : [],
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
/*
|
|
134
|
+
* Evaluate one formula at one coordinate against the fixture cells.
|
|
135
|
+
*/
|
|
136
|
+
function evaluateParityFormula(testCase: SheetFormulaParityCase, formula: string, at: string): SheetFormulaEvaluatorResult {
|
|
137
|
+
return evaluateSheetFormulaExpressionSync({
|
|
138
|
+
expression: getSheetFormulaExpression(formula),
|
|
139
|
+
now: new Date(testCase.now || SHEET_FORMULA_PARITY_DEFAULTS.now),
|
|
140
|
+
resolver: PARITY_RESOLVER,
|
|
141
|
+
resolverContext: {
|
|
142
|
+
at: getParityCoord(at),
|
|
143
|
+
cells: Object.entries(testCase.cells || {}).map(([a1, cell]) => ({
|
|
144
|
+
...getParityCoord(a1),
|
|
145
|
+
cell,
|
|
146
|
+
})),
|
|
147
|
+
},
|
|
148
|
+
timeZone: testCase.timeZone || SHEET_FORMULA_PARITY_DEFAULTS.timeZone,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
describe('formula parity fixtures (mday stored-value runner)', () => {
|
|
153
|
+
for (const testCase of SHEET_FORMULA_PARITY_CASES) {
|
|
154
|
+
it.skipIf(Boolean(testCase.skip?.mday))(testCase.name, () => {
|
|
155
|
+
const result = evaluateParityFormula(testCase, testCase.formula, testCase.at || SHEET_FORMULA_PARITY_DEFAULTS.at);
|
|
156
|
+
|
|
157
|
+
expect(
|
|
158
|
+
getSheetFormulaParityMismatch(testCase.expected, {
|
|
159
|
+
display: result.error ? String(result.value) : null,
|
|
160
|
+
errorCode: result.error?.code || null,
|
|
161
|
+
value: result.value,
|
|
162
|
+
}),
|
|
163
|
+
).toBeNull();
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
it('stores results that each non-cycle formula cell really evaluates to', () => {
|
|
168
|
+
for (const testCase of SHEET_FORMULA_PARITY_CASES) {
|
|
169
|
+
for (const [a1, cell] of Object.entries(testCase.cells || {})) {
|
|
170
|
+
if (!cell || typeof cell !== 'object') {
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if ('errorCode' in cell.stored && cell.stored.errorCode === 'CYCLE') {
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const result = evaluateParityFormula(testCase, cell.formula, a1);
|
|
179
|
+
const stored = cell.stored;
|
|
180
|
+
if ('errorCode' in stored) {
|
|
181
|
+
expect(result.error?.code, `${testCase.name} ${a1}`).toBe(stored.errorCode);
|
|
182
|
+
} else {
|
|
183
|
+
expect(result.value, `${testCase.name} ${a1}`).toBe(stored.value);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
});
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { SHEET_MAX_COLUMN_COUNT, SHEET_MAX_ROW_COUNT } from '../src/constants/sheet.ts';
|
|
3
|
+
import {
|
|
4
|
+
addSheetFormulaRangeEntry,
|
|
5
|
+
buildSheetFormulaArrayValue,
|
|
6
|
+
createSheetFormulaRangeAccumulator,
|
|
7
|
+
densifySheetFormulaRangeEntries,
|
|
8
|
+
getSheetFormulaRangeAccumulatorResult,
|
|
9
|
+
getSheetFormulaRangeBounds,
|
|
10
|
+
getSheetFormulaRangeCell,
|
|
11
|
+
getSheetFormulaRangeShape,
|
|
12
|
+
getSheetFormulaRangeVector,
|
|
13
|
+
getSheetFormulaReferencedCellError,
|
|
14
|
+
isSheetFormulaVectorRange,
|
|
15
|
+
sheetFormulaRangesHaveSameShape,
|
|
16
|
+
withSheetFormulaRangeShape,
|
|
17
|
+
} from '../src/utils/sheetFormulaRange.ts';
|
|
18
|
+
|
|
19
|
+
/*
|
|
20
|
+
* Return clamped bounds for one A1-style rectangle given by indexes.
|
|
21
|
+
*/
|
|
22
|
+
function getTestBounds(startRowIndex: number, startColumnIndex: number, endRowIndex: number, endColumnIndex: number) {
|
|
23
|
+
return getSheetFormulaRangeBounds({
|
|
24
|
+
endColumnIndex,
|
|
25
|
+
endRowIndex,
|
|
26
|
+
startColumnIndex,
|
|
27
|
+
startRowIndex,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
describe('getSheetFormulaRangeBounds', () => {
|
|
32
|
+
it('keeps in-cap rectangles and counts their shape', () => {
|
|
33
|
+
expect(getTestBounds(2, 1, 4, 3)).toEqual({
|
|
34
|
+
columnCount: 3,
|
|
35
|
+
endColumnIndex: 3,
|
|
36
|
+
endRowIndex: 4,
|
|
37
|
+
rowCount: 3,
|
|
38
|
+
startColumnIndex: 1,
|
|
39
|
+
startRowIndex: 2,
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('clamps out-of-cap corners to the sheet caps', () => {
|
|
44
|
+
const bounds = getTestBounds(1, 1, 99_999_999, 18_278);
|
|
45
|
+
|
|
46
|
+
expect(bounds.rowCount).toBe(SHEET_MAX_ROW_COUNT);
|
|
47
|
+
expect(bounds.columnCount).toBe(SHEET_MAX_COLUMN_COUNT);
|
|
48
|
+
expect(bounds.rowCount * bounds.columnCount).toBe(128_000);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('returns an empty 0 x 0 rectangle when the range starts past the caps', () => {
|
|
52
|
+
expect(getTestBounds(600, 1, 700, 1)).toMatchObject({
|
|
53
|
+
columnCount: 0,
|
|
54
|
+
rowCount: 0,
|
|
55
|
+
});
|
|
56
|
+
expect(getTestBounds(1, 300, 2, 400)).toMatchObject({
|
|
57
|
+
columnCount: 0,
|
|
58
|
+
rowCount: 0,
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe('range accumulator (referenced-error policy)', () => {
|
|
64
|
+
it('places entries row-major and fills gaps with null', () => {
|
|
65
|
+
const outcome = densifySheetFormulaRangeEntries(
|
|
66
|
+
[
|
|
67
|
+
{ columnIndex: 2, rowIndex: 2, value: 'd' },
|
|
68
|
+
{ columnIndex: 1, rowIndex: 1, value: 'a' },
|
|
69
|
+
{ columnIndex: 9, rowIndex: 9, value: 'outside' },
|
|
70
|
+
],
|
|
71
|
+
getTestBounds(1, 1, 2, 2),
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
expect(outcome).toEqual({
|
|
75
|
+
status: 'READY',
|
|
76
|
+
values: ['a', null, null, 'd'],
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('reads ordinary error cells as blank', () => {
|
|
81
|
+
const outcome = densifySheetFormulaRangeEntries(
|
|
82
|
+
[
|
|
83
|
+
{ columnIndex: 1, rowIndex: 1, value: 1 },
|
|
84
|
+
{
|
|
85
|
+
columnIndex: 1,
|
|
86
|
+
error: { code: 'DIVIDE_BY_ZERO', message: 'x' },
|
|
87
|
+
rowIndex: 2,
|
|
88
|
+
value: '#DIV/0!',
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
columnIndex: 1,
|
|
92
|
+
error: { code: 'NOT_FOUND', message: 'x' },
|
|
93
|
+
rowIndex: 3,
|
|
94
|
+
value: '#N/A',
|
|
95
|
+
},
|
|
96
|
+
],
|
|
97
|
+
getTestBounds(1, 1, 3, 1),
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
expect(outcome).toEqual({ status: 'READY', values: [1, null, null] });
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('fails the whole range with CYCLE for a circular cell, before loading', () => {
|
|
104
|
+
const accumulator = createSheetFormulaRangeAccumulator(getTestBounds(1, 1, 3, 1));
|
|
105
|
+
addSheetFormulaRangeEntry(accumulator, {
|
|
106
|
+
columnIndex: 1,
|
|
107
|
+
loading: true,
|
|
108
|
+
rowIndex: 1,
|
|
109
|
+
});
|
|
110
|
+
addSheetFormulaRangeEntry(accumulator, {
|
|
111
|
+
columnIndex: 1,
|
|
112
|
+
error: { code: 'CIRCULAR_REFERENCE', message: 'x' },
|
|
113
|
+
rowIndex: 2,
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
const outcome = getSheetFormulaRangeAccumulatorResult(accumulator);
|
|
117
|
+
expect(outcome.status).toBe('ERROR');
|
|
118
|
+
expect(outcome.status === 'ERROR' && outcome.error.code).toBe('CYCLE');
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('reports loading when any entry is still loading', () => {
|
|
122
|
+
expect(densifySheetFormulaRangeEntries([{ columnIndex: 1, loading: true, rowIndex: 1 }], getTestBounds(1, 1, 1, 1))).toEqual({
|
|
123
|
+
status: 'LOADING',
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('normalizes referenced circular errors to CYCLE and keeps others', () => {
|
|
128
|
+
expect(
|
|
129
|
+
getSheetFormulaReferencedCellError({
|
|
130
|
+
code: 'CIRCULAR_REFERENCE',
|
|
131
|
+
message: 'x',
|
|
132
|
+
}).code,
|
|
133
|
+
).toBe('CYCLE');
|
|
134
|
+
expect(getSheetFormulaReferencedCellError({ code: 'NOT_FOUND', message: 'x' })).toEqual({ code: 'NOT_FOUND', message: 'x' });
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
describe('range shape helpers', () => {
|
|
139
|
+
it('stamps and reads shapes only when the length matches', () => {
|
|
140
|
+
const shaped = withSheetFormulaRangeShape({ kind: 'SHEET_RANGE_VALUE', values: [1, 2, 3, 4, 5, 6] }, getTestBounds(1, 1, 2, 3));
|
|
141
|
+
expect(getSheetFormulaRangeShape(shaped)).toEqual({
|
|
142
|
+
columnCount: 3,
|
|
143
|
+
rowCount: 2,
|
|
144
|
+
});
|
|
145
|
+
expect(getSheetFormulaRangeCell(shaped, 1, 2)).toBe(6);
|
|
146
|
+
expect(getSheetFormulaRangeCell(shaped, 2, 0)).toBeUndefined();
|
|
147
|
+
|
|
148
|
+
const unshaped = withSheetFormulaRangeShape({ kind: 'SHEET_RANGE_VALUE', values: [1] }, getTestBounds(1, 1, 2, 3));
|
|
149
|
+
expect(getSheetFormulaRangeShape(unshaped)).toBeNull();
|
|
150
|
+
expect(getSheetFormulaRangeShape(5)).toBeNull();
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('recognizes vectors and equal shapes', () => {
|
|
154
|
+
const column = buildSheetFormulaArrayValue([1, 2, 3], 3, 1);
|
|
155
|
+
const row = buildSheetFormulaArrayValue([1, 2, 3], 1, 3);
|
|
156
|
+
const grid = buildSheetFormulaArrayValue([1, 2, 3, 4], 2, 2);
|
|
157
|
+
|
|
158
|
+
expect(isSheetFormulaVectorRange(column)).toBe(true);
|
|
159
|
+
expect(isSheetFormulaVectorRange(row)).toBe(true);
|
|
160
|
+
expect(isSheetFormulaVectorRange(grid)).toBe(false);
|
|
161
|
+
expect(getSheetFormulaRangeVector(row)).toEqual([1, 2, 3]);
|
|
162
|
+
expect(getSheetFormulaRangeVector(grid)).toBeNull();
|
|
163
|
+
expect(sheetFormulaRangesHaveSameShape([column, column])).toBe(true);
|
|
164
|
+
expect(sheetFormulaRangesHaveSameShape([column, row])).toBe(false);
|
|
165
|
+
expect(sheetFormulaRangesHaveSameShape([])).toBe(false);
|
|
166
|
+
});
|
|
167
|
+
});
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { SHEET_FORMULA_AT_PREFIX_BUILTIN_FUNCTION_NAMES } from '../src/constants/sheet.ts';
|
|
3
|
+
import { getSheetFormulaColumnLabel, getSheetFormulaExpression } from '../src/utils/sheetFormulaSyntax.ts';
|
|
4
|
+
import { SHEET_FORMULA_BUILTIN_FUNCTION_NAMES, getSheetFormulaFunctionGuide } from '../src/utils/sheetFormulaEvaluation.ts';
|
|
5
|
+
import { evaluateSheetFormulaExpressionSync, type SheetFormulaReferenceResolver } from '../src/utils/sheetFormulaEvaluator.ts';
|
|
6
|
+
import {
|
|
7
|
+
getSheetFormulaFunctionReference,
|
|
8
|
+
SHEET_FORMULA_REFERENCE,
|
|
9
|
+
type SheetFormulaReferenceScalar,
|
|
10
|
+
} from '../src/utils/sheetFormulaReference.ts';
|
|
11
|
+
|
|
12
|
+
const resolver: SheetFormulaReferenceResolver<Record<string, SheetFormulaReferenceScalar>> = {
|
|
13
|
+
/* Resolve documented literal cells without involving storage or a second evaluator. */
|
|
14
|
+
resolveCellReference(cells, node) {
|
|
15
|
+
if (node.reference.sheetName) throw new Error('Self-contained examples must not require another tab.');
|
|
16
|
+
const key = `${node.reference.columnLabel}${node.reference.rowIndex}`;
|
|
17
|
+
return { references: [], value: cells[key] ?? null };
|
|
18
|
+
},
|
|
19
|
+
/* Read the documented range in canonical row-major order. */
|
|
20
|
+
resolveRangeReference(cells, node) {
|
|
21
|
+
if (node.reference.sheetName) throw new Error('Self-contained examples must not require another tab.');
|
|
22
|
+
const values: SheetFormulaReferenceScalar[] = [];
|
|
23
|
+
for (let row = node.reference.startRowIndex; row <= node.reference.endRowIndex; row += 1) {
|
|
24
|
+
for (let column = node.reference.startColumnIndex; column <= node.reference.endColumnIndex; column += 1) {
|
|
25
|
+
values.push(cells[`${getSheetFormulaColumnLabel(column)}${row}`] ?? null);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return { references: [], value: { kind: 'SHEET_RANGE_VALUE', values } };
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
describe('shared formula reference', () => {
|
|
33
|
+
it('documents every supported function and preserves legacy guide access', () => {
|
|
34
|
+
expect(Object.keys(SHEET_FORMULA_REFERENCE).sort()).toEqual([...SHEET_FORMULA_BUILTIN_FUNCTION_NAMES].sort());
|
|
35
|
+
for (const name of SHEET_FORMULA_BUILTIN_FUNCTION_NAMES) {
|
|
36
|
+
const reference = SHEET_FORMULA_REFERENCE[name];
|
|
37
|
+
expect(reference.name).toBe(name);
|
|
38
|
+
expect(reference.signature).toMatch(new RegExp(`^${name}\\(`));
|
|
39
|
+
expect(reference.description.length).toBeGreaterThan(10);
|
|
40
|
+
expect(reference.returns.description.length).toBeGreaterThan(0);
|
|
41
|
+
expect(reference.examples.length).toBeGreaterThan(0);
|
|
42
|
+
expect(getSheetFormulaFunctionGuide(name)).toBe(reference);
|
|
43
|
+
// The '@name' spelling is kept only for the legacy builtins
|
|
44
|
+
expect(getSheetFormulaFunctionReference(`@${name.toLowerCase()}`)).toBe(
|
|
45
|
+
(SHEET_FORMULA_AT_PREFIX_BUILTIN_FUNCTION_NAMES as readonly string[]).includes(name) ? reference : null,
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
expect(getSheetFormulaFunctionReference('UNKNOWN_FN')).toBeNull();
|
|
49
|
+
expect(getSheetFormulaFunctionReference('constructor')).toBeNull();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
for (const reference of Object.values(SHEET_FORMULA_REFERENCE)) {
|
|
53
|
+
for (const example of reference.examples) {
|
|
54
|
+
it(`evaluates ${reference.name} example ${example.formula}`, () => {
|
|
55
|
+
const result = evaluateSheetFormulaExpressionSync({
|
|
56
|
+
expression: getSheetFormulaExpression(example.formula),
|
|
57
|
+
resolver,
|
|
58
|
+
resolverContext: example.cells || {},
|
|
59
|
+
now: new Date(example.now || '2026-01-02T03:04:05.000Z'),
|
|
60
|
+
timeZone: example.timeZone || 'UTC',
|
|
61
|
+
});
|
|
62
|
+
expect(result.loading).toBeFalsy();
|
|
63
|
+
if (example.expectedErrorCode) {
|
|
64
|
+
expect(result.error?.code).toBe(example.expectedErrorCode);
|
|
65
|
+
expect(result.value).toBe(example.expectedValue);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
expect(result.error).toBeFalsy();
|
|
70
|
+
if (typeof example.expectedValue === 'number') {
|
|
71
|
+
expect(result.value).toBeCloseTo(example.expectedValue, 10);
|
|
72
|
+
} else {
|
|
73
|
+
expect(result.value).toBe(example.expectedValue);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
});
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
rewriteSheetFormulaReferenceTokens,
|
|
4
|
+
type SheetFormulaDirectReferenceToken,
|
|
5
|
+
} from '../src/utils/sheetFormulaReferenceRewrite.ts';
|
|
6
|
+
|
|
7
|
+
/*
|
|
8
|
+
* Return a visitor that marks every direct token with brackets and records
|
|
9
|
+
* the visited token texts.
|
|
10
|
+
*/
|
|
11
|
+
function getMarkingVisitor(visited: string[]) {
|
|
12
|
+
return (token: SheetFormulaDirectReferenceToken) => {
|
|
13
|
+
visited.push(token.text);
|
|
14
|
+
return { flag: 'MARK', text: `[${token.text}]` };
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/*
|
|
19
|
+
* Verify the shared reference token walker: which tokens it visits, how it
|
|
20
|
+
* splices, and how overrides replace spans.
|
|
21
|
+
*/
|
|
22
|
+
describe('sheet formula reference token walker', () => {
|
|
23
|
+
it('keeps the text when the visitor answers nothing', () => {
|
|
24
|
+
const value = '=SUM(A1:B2, Other!C3) + @t(A5, "k")';
|
|
25
|
+
const result = rewriteSheetFormulaReferenceTokens(value, () => null);
|
|
26
|
+
|
|
27
|
+
expect(result).toEqual({
|
|
28
|
+
changed: false,
|
|
29
|
+
overridesRejected: false,
|
|
30
|
+
replacements: [],
|
|
31
|
+
text: value,
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('splices only token spans and keeps qualifiers and whitespace', () => {
|
|
36
|
+
const visited: string[] = [];
|
|
37
|
+
const result = rewriteSheetFormulaReferenceTokens("= a1 +'My Tab'!$B$2 *SUM( C3 : D4 )", getMarkingVisitor(visited));
|
|
38
|
+
|
|
39
|
+
expect(result.text).toBe("= [a1] +['My Tab'!$B$2] *SUM( [C3 : D4] )");
|
|
40
|
+
expect(visited).toEqual(['a1', "'My Tab'!$B$2", 'C3 : D4']);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('reports input and output offsets for every replacement', () => {
|
|
44
|
+
const result = rewriteSheetFormulaReferenceTokens('=A1+B22', getMarkingVisitor([]));
|
|
45
|
+
|
|
46
|
+
expect(result.replacements).toEqual([
|
|
47
|
+
{
|
|
48
|
+
after: '[A1]',
|
|
49
|
+
before: 'A1',
|
|
50
|
+
flag: 'MARK',
|
|
51
|
+
inputEnd: 3,
|
|
52
|
+
inputStart: 1,
|
|
53
|
+
outputEnd: 5,
|
|
54
|
+
outputStart: 1,
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
after: '[B22]',
|
|
58
|
+
before: 'B22',
|
|
59
|
+
flag: 'MARK',
|
|
60
|
+
inputEnd: 7,
|
|
61
|
+
inputStart: 4,
|
|
62
|
+
outputEnd: 11,
|
|
63
|
+
outputStart: 6,
|
|
64
|
+
},
|
|
65
|
+
]);
|
|
66
|
+
for (const replacement of result.replacements) {
|
|
67
|
+
expect(result.text.slice(replacement.outputStart, replacement.outputEnd)).toBe(replacement.after);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('skips strings and coordinate-like function names such as LOG10(', () => {
|
|
72
|
+
const visited: string[] = [];
|
|
73
|
+
const result = rewriteSheetFormulaReferenceTokens('="A1"&LOG10(A2)+ATAN2 (B3, C4)+\'x\'', getMarkingVisitor(visited));
|
|
74
|
+
|
|
75
|
+
expect(visited).toEqual(['A2', 'B3', 'C4']);
|
|
76
|
+
expect(result.text).toBe('="A1"&LOG10([A2])+ATAN2 ([B3], [C4])+\'x\'');
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('walks an invalid formula token by token', () => {
|
|
80
|
+
const result = rewriteSheetFormulaReferenceTokens('=SUM(A1,,, +* B2', getMarkingVisitor([]));
|
|
81
|
+
|
|
82
|
+
expect(result.text).toBe('=SUM([A1],,, +* [B2]');
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('emits overrides verbatim and skips the tokens they overlap', () => {
|
|
86
|
+
const visited: string[] = [];
|
|
87
|
+
const result = rewriteSheetFormulaReferenceTokens('=#REF!+A2+SUM(B1:B9)', getMarkingVisitor(visited), {
|
|
88
|
+
overrides: [
|
|
89
|
+
{ expected: '#REF!', length: 5, start: 1, text: 'A10' },
|
|
90
|
+
{ length: 5, start: 14, text: 'B1:B10' },
|
|
91
|
+
],
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
expect(visited).toEqual(['A2']);
|
|
95
|
+
expect(result.text).toBe('=A10+[A2]+SUM(B1:B10)');
|
|
96
|
+
expect(result.overridesRejected).toBe(false);
|
|
97
|
+
expect(result.replacements.map((item) => item.flag)).toEqual([null, 'MARK', null]);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it.each([
|
|
101
|
+
['a failed guard', [{ expected: 'A1', length: 5, start: 1, text: 'x' }]],
|
|
102
|
+
[
|
|
103
|
+
'overlapping spans',
|
|
104
|
+
[
|
|
105
|
+
{ length: 3, start: 1, text: 'x' },
|
|
106
|
+
{ length: 3, start: 2, text: 'y' },
|
|
107
|
+
],
|
|
108
|
+
],
|
|
109
|
+
['a span past the end', [{ length: 50, start: 1, text: 'x' }]],
|
|
110
|
+
['an empty span', [{ length: 0, start: 1, text: 'x' }]],
|
|
111
|
+
])('rejects every override for %s', (_label, overrides) => {
|
|
112
|
+
const result = rewriteSheetFormulaReferenceTokens('=#REF!+A2', getMarkingVisitor([]), { overrides });
|
|
113
|
+
|
|
114
|
+
expect(result.overridesRejected).toBe(true);
|
|
115
|
+
expect(result.text).toBe('=#REF!+[A2]');
|
|
116
|
+
});
|
|
117
|
+
});
|