@jsb188/mday 1.1.2

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 (202) hide show
  1. package/package.json +142 -0
  2. package/src/constants/agentMemory.ts +166 -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 +22 -0
  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 +146 -0
  14. package/src/constants/fieldDoc.ts +104 -0
  15. package/src/constants/graphqlReactivity.ts +196 -0
  16. package/src/constants/inboundContact.ts +8 -0
  17. package/src/constants/integration.ts +13 -0
  18. package/src/constants/log.ts +21 -0
  19. package/src/constants/logDisplay/README.md +88 -0
  20. package/src/constants/logDisplay/defaults.ts +39 -0
  21. package/src/constants/logDisplay/index.ts +27 -0
  22. package/src/constants/logDisplay/operations/arable.ts +16 -0
  23. package/src/constants/logDisplay/operations/farmersMarket.ts +10 -0
  24. package/src/constants/logDisplay/operations/foodManufacturing.ts +13 -0
  25. package/src/constants/logDisplay/shared/accounting.ts +13 -0
  26. package/src/constants/logDisplay/shared/interactions.ts +13 -0
  27. package/src/constants/logDisplay/shared/rackTemperatures.ts +42 -0
  28. package/src/constants/logTaxonomy.generated.ts +2199 -0
  29. package/src/constants/noteDoc.ts +77 -0
  30. package/src/constants/organization.ts +60 -0
  31. package/src/constants/pageRoute.ts +23 -0
  32. package/src/constants/qrCode.ts +30 -0
  33. package/src/constants/sheet.ts +132 -0
  34. package/src/constants/sms.ts +18 -0
  35. package/src/constants/square.ts +16 -0
  36. package/src/constants/storage.ts +85 -0
  37. package/src/constants/usage.ts +82 -0
  38. package/src/constants/workflowSteps.ts +60 -0
  39. package/src/schemas/editLogEntry.ts +879 -0
  40. package/src/schemas/inboundContact.ts +77 -0
  41. package/src/schemas/logsQuery.ts +93 -0
  42. package/src/types/account.d.ts +81 -0
  43. package/src/types/agentMemory.d.ts +100 -0
  44. package/src/types/agentNote.d.ts +441 -0
  45. package/src/types/billing.d.ts +537 -0
  46. package/src/types/campaign.d.ts +1533 -0
  47. package/src/types/contact.d.ts +15 -0
  48. package/src/types/csvExport.d.ts +213 -0
  49. package/src/types/dataAnalysis.d.ts +44 -0
  50. package/src/types/dataTable.d.ts +159 -0
  51. package/src/types/definitionElement.d.ts +35 -0
  52. package/src/types/docAccess.d.ts +96 -0
  53. package/src/types/dt.d.ts +257 -0
  54. package/src/types/email.d.ts +231 -0
  55. package/src/types/emailReplyReport.d.ts +76 -0
  56. package/src/types/fieldDoc.d.ts +702 -0
  57. package/src/types/fieldDocContract.d.ts +90 -0
  58. package/src/types/inboundContact.d.ts +65 -0
  59. package/src/types/integration.d.ts +42 -0
  60. package/src/types/log.d.ts +338 -0
  61. package/src/types/logDisplay.types.ts +85 -0
  62. package/src/types/organization.d.ts +350 -0
  63. package/src/types/pageRoute.d.ts +48 -0
  64. package/src/types/qrCode.d.ts +96 -0
  65. package/src/types/richText.d.ts +81 -0
  66. package/src/types/sheet.d.ts +847 -0
  67. package/src/types/sms.d.ts +38 -0
  68. package/src/types/storage.d.ts +481 -0
  69. package/src/types/usage.d.ts +106 -0
  70. package/src/types/workflow.d.ts +435 -0
  71. package/src/utils/agentMemory.ts +15 -0
  72. package/src/utils/agentNote.ts +850 -0
  73. package/src/utils/aiChat.ts +43 -0
  74. package/src/utils/aiChatWork.ts +64 -0
  75. package/src/utils/billing.ts +37 -0
  76. package/src/utils/campaign.ts +1173 -0
  77. package/src/utils/campaignAI.ts +72 -0
  78. package/src/utils/campaignAccess.ts +239 -0
  79. package/src/utils/campaignEmail.ts +601 -0
  80. package/src/utils/campaignEmailCompiler.ts +372 -0
  81. package/src/utils/campaignEmailImages.ts +60 -0
  82. package/src/utils/campaignEmailStyles.ts +544 -0
  83. package/src/utils/campaignLifecycle.ts +104 -0
  84. package/src/utils/campaignPreview.ts +285 -0
  85. package/src/utils/contact.ts +50 -0
  86. package/src/utils/csvExport.ts +19 -0
  87. package/src/utils/dataTable.ts +311 -0
  88. package/src/utils/dataTableFieldTypeRegistry.ts +106 -0
  89. package/src/utils/dataTableRelativeDate.ts +447 -0
  90. package/src/utils/definitionElement.ts +84 -0
  91. package/src/utils/docAccess.ts +383 -0
  92. package/src/utils/documentName.ts +53 -0
  93. package/src/utils/email.ts +379 -0
  94. package/src/utils/emailReplyReport.test.ts +187 -0
  95. package/src/utils/emailReplyReport.ts +143 -0
  96. package/src/utils/fieldDoc.ts +157 -0
  97. package/src/utils/fieldDocAnswers.ts +128 -0
  98. package/src/utils/fieldDocDefaults.ts +36 -0
  99. package/src/utils/fieldDocProgram.ts +36 -0
  100. package/src/utils/fieldDocSummary.ts +30 -0
  101. package/src/utils/log.ts +357 -0
  102. package/src/utils/logDisplay.helpers.ts +177 -0
  103. package/src/utils/logDisplay.ts +87 -0
  104. package/src/utils/logRegistryValue.ts +90 -0
  105. package/src/utils/logStoreRead.ts +262 -0
  106. package/src/utils/logTaxonomy.helpers.ts +17 -0
  107. package/src/utils/logTaxonomy.ts +76 -0
  108. package/src/utils/logTaxonomyDisplay.ts +395 -0
  109. package/src/utils/noteDoc.ts +609 -0
  110. package/src/utils/organization.ts +669 -0
  111. package/src/utils/pageRoute.ts +71 -0
  112. package/src/utils/qrCode.helpers.ts +189 -0
  113. package/src/utils/qrCode.test.ts +519 -0
  114. package/src/utils/qrCode.ts +273 -0
  115. package/src/utils/richTextHTML.ts +385 -0
  116. package/src/utils/sheet.ts +262 -0
  117. package/src/utils/sheetAxisMove.ts +616 -0
  118. package/src/utils/sheetBootstrap.ts +1402 -0
  119. package/src/utils/sheetCellLiteral.ts +101 -0
  120. package/src/utils/sheetCommand.ts +358 -0
  121. package/src/utils/sheetDesign.ts +705 -0
  122. package/src/utils/sheetExport.ts +186 -0
  123. package/src/utils/sheetFeatureReference.ts +183 -0
  124. package/src/utils/sheetFormulaArgs.ts +168 -0
  125. package/src/utils/sheetFormulaCompile.ts +475 -0
  126. package/src/utils/sheetFormulaConditionalAggregate.ts +254 -0
  127. package/src/utils/sheetFormulaCriteria.ts +656 -0
  128. package/src/utils/sheetFormulaDate.ts +444 -0
  129. package/src/utils/sheetFormulaEvaluation.ts +899 -0
  130. package/src/utils/sheetFormulaEvaluator.ts +997 -0
  131. package/src/utils/sheetFormulaFunctionHandlers.ts +403 -0
  132. package/src/utils/sheetFormulaLogical.ts +116 -0
  133. package/src/utils/sheetFormulaLookup.ts +488 -0
  134. package/src/utils/sheetFormulaMath.ts +48 -0
  135. package/src/utils/sheetFormulaParser.ts +729 -0
  136. package/src/utils/sheetFormulaRange.ts +330 -0
  137. package/src/utils/sheetFormulaReference.ts +703 -0
  138. package/src/utils/sheetFormulaReferenceAggregates.ts +313 -0
  139. package/src/utils/sheetFormulaReferenceDates.ts +237 -0
  140. package/src/utils/sheetFormulaReferenceLogical.ts +330 -0
  141. package/src/utils/sheetFormulaReferenceLookup.ts +341 -0
  142. package/src/utils/sheetFormulaReferenceMath.ts +95 -0
  143. package/src/utils/sheetFormulaReferenceRewrite.ts +220 -0
  144. package/src/utils/sheetFormulaReferenceText.ts +462 -0
  145. package/src/utils/sheetFormulaSyntax.ts +1027 -0
  146. package/src/utils/sheetFormulaText.ts +430 -0
  147. package/src/utils/sheetJournal.ts +200 -0
  148. package/src/utils/sheetNumberFormat.ts +1012 -0
  149. package/src/utils/sheetPreview.ts +68 -0
  150. package/src/utils/sheetSequence.ts +118 -0
  151. package/src/utils/sheetSnapshot.ts +739 -0
  152. package/src/utils/sheetStructureEdit.ts +914 -0
  153. package/src/utils/sheetStructureReferences.ts +389 -0
  154. package/src/utils/square.ts +32 -0
  155. package/src/utils/storage.ts +198 -0
  156. package/src/utils/table-route.ts +262 -0
  157. package/src/utils/time.ts +12 -0
  158. package/src/utils/workflow.ts +506 -0
  159. package/src/utils/workflowLogFingerprint.ts +302 -0
  160. package/src/utils/workflowLogRevisionSnapshot.ts +424 -0
  161. package/src/utils/workflowProgress.ts +280 -0
  162. package/src/utils/workflowTriggers.ts +61 -0
  163. package/tests/agentNote.test.ts +395 -0
  164. package/tests/campaignAI.test.ts +119 -0
  165. package/tests/campaignAccess.test.ts +239 -0
  166. package/tests/campaignEmail.test.ts +441 -0
  167. package/tests/campaignLifecycle.test.ts +134 -0
  168. package/tests/campaignPublication.test.ts +309 -0
  169. package/tests/dataTableFieldTypeRegistry.test.ts +111 -0
  170. package/tests/dataTableRelativeDate.test.ts +412 -0
  171. package/tests/docAccess.test.ts +525 -0
  172. package/tests/editLogEntry.test.ts +112 -0
  173. package/tests/email.test.ts +39 -0
  174. package/tests/fieldDocProgram.test.ts +114 -0
  175. package/tests/logDisplay.fixtures.ts +67 -0
  176. package/tests/logDisplay.test.ts +227 -0
  177. package/tests/noteDoc.test.ts +514 -0
  178. package/tests/richTextHTML.test.ts +185 -0
  179. package/tests/sheetAxisMove.test.ts +352 -0
  180. package/tests/sheetBootstrap.test.ts +1967 -0
  181. package/tests/sheetFeatureReference.test.ts +22 -0
  182. package/tests/sheetFormulaCriteria.test.ts +269 -0
  183. package/tests/sheetFormulaEvaluator.test.ts +468 -0
  184. package/tests/sheetFormulaFunctions.test.ts +538 -0
  185. package/tests/sheetFormulaLanguage.test.ts +361 -0
  186. package/tests/sheetFormulaParity.fixtures.ts +872 -0
  187. package/tests/sheetFormulaParity.test.ts +188 -0
  188. package/tests/sheetFormulaRange.test.ts +167 -0
  189. package/tests/sheetFormulaReference.test.ts +78 -0
  190. package/tests/sheetFormulaReferenceRewrite.test.ts +117 -0
  191. package/tests/sheetFormulaTextDate.test.ts +512 -0
  192. package/tests/sheetSequence.test.ts +22 -0
  193. package/tests/sheetSnapshot.test.ts +134 -0
  194. package/tests/sheetStructureEdit.test.ts +537 -0
  195. package/tests/sheetStructureReferences.test.ts +589 -0
  196. package/tests/workflowLifecycleStatus.test.ts +60 -0
  197. package/tests/workflowLogFingerprint.test.ts +203 -0
  198. package/tests/workflowLogRevisionSnapshot.test.ts +96 -0
  199. package/tests/workflowProgress.test.ts +180 -0
  200. package/tests/workflowTriggers.test.ts +29 -0
  201. package/tsconfig.json +12 -0
  202. package/vitest.config.ts +8 -0
@@ -0,0 +1,203 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import {
3
+ canonicalizeWorkflowFingerprintValue,
4
+ getWorkflowLogFingerprintProjections,
5
+ serializeWorkflowFingerprintProjection,
6
+ } from '../src/utils/workflowLogFingerprint.ts';
7
+
8
+ const BASE_LOG = {
9
+ id: 19006n,
10
+ organizationId: 28,
11
+ childOrgId: '47',
12
+ siteId: null,
13
+ date: new Date('2026-08-11T16:00:00.000-04:00'),
14
+ summary: 'Market credits',
15
+ details: {
16
+ activity: 'MARKET_CREDIT_RECEIPT',
17
+ logType: 'MARKET_RECEIPT',
18
+ notes: 'August redemption',
19
+ metadata: {
20
+ flagColor: 'green',
21
+ item: 'Market credits',
22
+ quantity: 3,
23
+ unit: 'bundles',
24
+ price: 12.5,
25
+ otherParty: 'Bruno Arable',
26
+ referenceNumber: 'BM-77',
27
+ tax: 1.25,
28
+ voided: false,
29
+ values: [
30
+ { label: 'Market Coin', quantity: 2, unit: 'coins', value: 20 },
31
+ { label: 'Voucher', quantity: 1, value: 5 },
32
+ ],
33
+ },
34
+ },
35
+ };
36
+
37
+ /*
38
+ * Build canonical projections with stable presentation dependencies for tests.
39
+ */
40
+ function getBaseProjections(flagColor?: string) {
41
+ return getWorkflowLogFingerprintProjections(BASE_LOG, {
42
+ ...(flagColor === undefined ? {} : { flagColor }),
43
+ presentation: {
44
+ payee: { email: 'ar@bruno.example', name: 'Bruno Arable' },
45
+ issuer: { name: 'Marketday' },
46
+ timeZone: 'America/Toronto',
47
+ },
48
+ attachmentManifest: [{ name: 'terms.pdf', contentHash: 'sha256:abc' }],
49
+ });
50
+ }
51
+
52
+ describe('workflow log fingerprint projections', () => {
53
+ it('normalizes recursive values with stable keys, IDs, dates, and explicit nulls', () => {
54
+ const left = {
55
+ z: undefined,
56
+ id: 22n,
57
+ when: new Date('2026-08-11T20:00:00.000Z'),
58
+ nested: { b: -0, a: null },
59
+ rows: [{ y: 2, x: 1 }, undefined],
60
+ };
61
+ const right = {
62
+ rows: [{ x: 1, y: 2 }, null],
63
+ nested: { a: null, b: 0 },
64
+ when: '2026-08-11T20:00:00.000Z',
65
+ id: '22',
66
+ z: null,
67
+ };
68
+
69
+ expect(canonicalizeWorkflowFingerprintValue(left)).toEqual({
70
+ id: '22',
71
+ nested: { a: null, b: 0 },
72
+ rows: [{ x: 1, y: 2 }, null],
73
+ when: '2026-08-11T20:00:00.000Z',
74
+ z: null,
75
+ });
76
+ expect(serializeWorkflowFingerprintProjection(left)).toBe(
77
+ serializeWorkflowFingerprintProjection(right),
78
+ );
79
+ });
80
+
81
+ it('keeps flag color out of receipt content but includes it in workflow state', () => {
82
+ const green = getBaseProjections('green');
83
+ const red = getBaseProjections('RED');
84
+
85
+ expect(serializeWorkflowFingerprintProjection(green.receiptContent)).toBe(
86
+ serializeWorkflowFingerprintProjection(red.receiptContent),
87
+ );
88
+ expect(
89
+ serializeWorkflowFingerprintProjection(green.workflowState),
90
+ ).not.toBe(serializeWorkflowFingerprintProjection(red.workflowState));
91
+ expect(green.workflowState.flagColor).toBe('GREEN');
92
+ });
93
+
94
+ it('ignores volatile log fields that are outside delivered receipt semantics', () => {
95
+ const baseline = getBaseProjections();
96
+ const volatileEdit = getWorkflowLogFingerprintProjections(
97
+ {
98
+ ...BASE_LOG,
99
+ createdAt: new Date('2020-01-01T00:00:00.000Z'),
100
+ updatedAt: new Date('2030-01-01T00:00:00.000Z'),
101
+ source: 'WEB',
102
+ confidence: 0.2,
103
+ originalMessage: 'raw source',
104
+ details: {
105
+ ...BASE_LOG.details,
106
+ translation: 'translated',
107
+ },
108
+ },
109
+ {
110
+ presentation: {
111
+ payee: { name: 'Bruno Arable', email: 'ar@bruno.example' },
112
+ issuer: { name: 'Marketday' },
113
+ timeZone: 'America/Toronto',
114
+ },
115
+ attachmentManifest: [{ contentHash: 'sha256:abc', name: 'terms.pdf' }],
116
+ },
117
+ );
118
+
119
+ expect(
120
+ serializeWorkflowFingerprintProjection(volatileEdit.receiptContent),
121
+ ).toBe(serializeWorkflowFingerprintProjection(baseline.receiptContent));
122
+ });
123
+
124
+ it('changes canonical receipt content for rendered edits and line-item order', () => {
125
+ const baseline = getBaseProjections();
126
+ const edited = getWorkflowLogFingerprintProjections(
127
+ {
128
+ ...BASE_LOG,
129
+ details: {
130
+ ...BASE_LOG.details,
131
+ metadata: {
132
+ ...BASE_LOG.details.metadata,
133
+ values: [...BASE_LOG.details.metadata.values].reverse(),
134
+ },
135
+ },
136
+ },
137
+ {
138
+ presentation: {
139
+ payee: { email: 'ar@bruno.example', name: 'Bruno Arable' },
140
+ issuer: { name: 'Marketday' },
141
+ timeZone: 'America/Toronto',
142
+ },
143
+ attachmentManifest: [{ name: 'terms.pdf', contentHash: 'sha256:abc' }],
144
+ },
145
+ );
146
+
147
+ expect(
148
+ serializeWorkflowFingerprintProjection(edited.receiptContent),
149
+ ).not.toBe(serializeWorkflowFingerprintProjection(baseline.receiptContent));
150
+ expect(baseline.receiptContent).toMatchObject({
151
+ logId: '19006',
152
+ organizationId: '28',
153
+ vendorId: '47',
154
+ date: '2026-08-11T20:00:00.000Z',
155
+ quantity: 3,
156
+ unit: 'bundles',
157
+ price: 12.5,
158
+ voided: false,
159
+ });
160
+ });
161
+
162
+ it.each([
163
+ ['quantity', 4],
164
+ ['unit', 'cases'],
165
+ ['price', 14.75],
166
+ ] as const)(
167
+ 'changes receipt content when scalar transaction %s changes',
168
+ (field, value) => {
169
+ const baseline = getBaseProjections();
170
+ const edited = getWorkflowLogFingerprintProjections(
171
+ {
172
+ ...BASE_LOG,
173
+ details: {
174
+ ...BASE_LOG.details,
175
+ metadata: {
176
+ ...BASE_LOG.details.metadata,
177
+ [field]: value,
178
+ },
179
+ },
180
+ },
181
+ {
182
+ presentation: {
183
+ payee: { email: 'ar@bruno.example', name: 'Bruno Arable' },
184
+ issuer: { name: 'Marketday' },
185
+ timeZone: 'America/Toronto',
186
+ },
187
+ attachmentManifest: [
188
+ {
189
+ name: 'terms.pdf',
190
+ contentHash: 'sha256:abc',
191
+ },
192
+ ],
193
+ },
194
+ );
195
+
196
+ expect(
197
+ serializeWorkflowFingerprintProjection(edited.receiptContent),
198
+ ).not.toBe(
199
+ serializeWorkflowFingerprintProjection(baseline.receiptContent),
200
+ );
201
+ },
202
+ );
203
+ });
@@ -0,0 +1,96 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import type { LogEntryData } from '../src/types/log.d.ts';
3
+ import {
4
+ makeWorkflowLogRevisionSnapshot,
5
+ parseWorkflowLogRevisionSnapshot,
6
+ } from '../src/utils/workflowLogRevisionSnapshot.ts';
7
+
8
+ /*
9
+ * Build one hydrated receipt log at a requested flag color.
10
+ */
11
+ function makeReceiptLog(flagColor: string): LogEntryData {
12
+ return {
13
+ __table: 'logs',
14
+ id: 19006n,
15
+ organizationId: 28n,
16
+ accountId: 7n,
17
+ childOrgId: 47n,
18
+ inboundContactId: null,
19
+ emailId: null,
20
+ fieldDocId: null,
21
+ fieldDocSubmissionId: null,
22
+ siteId: null,
23
+ summary: 'Market credits',
24
+ source: 'WEB',
25
+ confidence: null,
26
+ originalMessage: null,
27
+ date: new Date('2026-08-11T20:00:00.000Z'),
28
+ createdAt: new Date('2026-08-11T20:00:00.000Z'),
29
+ updatedAt: new Date('2026-08-11T20:01:00.000Z'),
30
+ childOrg: {
31
+ id: 47n,
32
+ name: 'Bruno Market',
33
+ },
34
+ location: null,
35
+ account: {
36
+ id: 7n,
37
+ settings: {},
38
+ profile: {
39
+ firstName: 'Bruno',
40
+ lastName: 'Owner',
41
+ },
42
+ },
43
+ details: {
44
+ __table: 'log_entry_details',
45
+ id: 19006n,
46
+ activity: 'MARKET_CREDIT_RECEIPT',
47
+ logType: 'MARKET_RECEIPT',
48
+ notes: 'August redemption',
49
+ translation: null,
50
+ fields: null,
51
+ metadata: {
52
+ flagColor,
53
+ referenceNumber: 'BM-77',
54
+ otherParty: 'Bruno Market',
55
+ values: [{ label: 'Market Coin', quantity: 2, value: 20 }],
56
+ },
57
+ },
58
+ };
59
+ }
60
+
61
+ describe('workflow log revision snapshots', () => {
62
+ it('preserves every rapid flag transition as an independent JSON-safe revision', () => {
63
+ const greenBefore = makeWorkflowLogRevisionSnapshot(
64
+ makeReceiptLog('green'),
65
+ );
66
+ const red = makeWorkflowLogRevisionSnapshot(makeReceiptLog('red'));
67
+ const greenAfter = makeWorkflowLogRevisionSnapshot(makeReceiptLog('green'));
68
+
69
+ expect(() => JSON.stringify([greenBefore, red, greenAfter])).not.toThrow();
70
+ expect([
71
+ parseWorkflowLogRevisionSnapshot(greenBefore).projections.workflowState
72
+ .flagColor,
73
+ parseWorkflowLogRevisionSnapshot(red).projections.workflowState.flagColor,
74
+ parseWorkflowLogRevisionSnapshot(greenAfter).projections.workflowState
75
+ .flagColor,
76
+ ]).toEqual(['GREEN', 'RED', 'GREEN']);
77
+ });
78
+
79
+ it('fails closed when stored projections no longer match the captured log', () => {
80
+ const snapshot = makeWorkflowLogRevisionSnapshot(makeReceiptLog('green'));
81
+ const malformed = {
82
+ ...snapshot,
83
+ projections: {
84
+ ...snapshot.projections,
85
+ workflowState: {
86
+ ...snapshot.projections.workflowState,
87
+ flagColor: 'RED',
88
+ },
89
+ },
90
+ };
91
+
92
+ expect(() => parseWorkflowLogRevisionSnapshot(malformed)).toThrow(
93
+ 'projections do not match',
94
+ );
95
+ });
96
+ });
@@ -0,0 +1,180 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import {
3
+ compactWorkflowProgressValues,
4
+ replaceWorkflowProgressAsyncOutcomeSection,
5
+ serializeWorkflowAsyncOutcomeSection,
6
+ updateWorkflowProgressAsyncOutcome,
7
+ WORKFLOW_PROGRESS_ASYNC_OUTCOMES_END,
8
+ WORKFLOW_PROGRESS_ASYNC_OUTCOMES_START,
9
+ WORKFLOW_SENT_EMAIL_STATUS_ENUMS,
10
+ WORKFLOW_SENT_FOLLOW_UP_STATUS_ENUMS,
11
+ WORKFLOW_SENT_REMINDER_STATUS_ENUMS,
12
+ WORKFLOW_SENT_SMS_STATUS_ENUMS,
13
+ WORKFLOW_SENT_WAIT_STATUS_ENUMS,
14
+ } from '../src/utils/workflowProgress.ts';
15
+
16
+ describe('workflow progress async outcomes', () => {
17
+ it('exposes normalized statuses independent of source-specific lifecycle names', () => {
18
+ expect(WORKFLOW_SENT_EMAIL_STATUS_ENUMS).toEqual([
19
+ 'QUEUED',
20
+ 'SENT',
21
+ 'DELIVERED',
22
+ 'FAILED',
23
+ 'DELIVERY_UNKNOWN',
24
+ 'BOUNCED',
25
+ 'BLOCKED',
26
+ ]);
27
+ expect(WORKFLOW_SENT_SMS_STATUS_ENUMS).toEqual(
28
+ WORKFLOW_SENT_EMAIL_STATUS_ENUMS,
29
+ );
30
+ expect(WORKFLOW_SENT_REMINDER_STATUS_ENUMS).toEqual([
31
+ 'SCHEDULED',
32
+ 'IN_PROGRESS',
33
+ 'COMPLETED',
34
+ 'FAILED',
35
+ 'CANCELED',
36
+ ]);
37
+ expect(WORKFLOW_SENT_FOLLOW_UP_STATUS_ENUMS).toEqual([
38
+ 'SCHEDULED',
39
+ 'FIRED',
40
+ 'REPLACED',
41
+ 'CANCELED',
42
+ ]);
43
+ expect(WORKFLOW_SENT_WAIT_STATUS_ENUMS).toEqual([
44
+ 'WAITING',
45
+ 'REPLIED',
46
+ 'EXPIRED',
47
+ 'CANCELED',
48
+ ]);
49
+ });
50
+
51
+ it('serializes one deterministic line per source kind and ID', () => {
52
+ const section = serializeWorkflowAsyncOutcomeSection({
53
+ sentEmails: {
54
+ 10: { status: 'BOUNCED', recipient: 'sarah@example.com' },
55
+ 2: {
56
+ status: 'DELIVERED',
57
+ recipient: 'john@example.com',
58
+ occurredAt: new Date('2026-07-01T12:00:00.000Z'),
59
+ },
60
+ },
61
+ sentWaits: {
62
+ 7: { status: 'WAITING', detail: 'Waiting for a reply' },
63
+ },
64
+ });
65
+
66
+ expect(section.split('\n')).toEqual([
67
+ WORKFLOW_PROGRESS_ASYNC_OUTCOMES_START,
68
+ 'sentEmails:2\t{"occurredAt":"2026-07-01T12:00:00.000Z","recipient":"john@example.com","status":"DELIVERED"}',
69
+ 'sentEmails:10\t{"recipient":"sarah@example.com","status":"BOUNCED"}',
70
+ 'sentWaits:7\t{"detail":"Waiting for a reply","status":"WAITING"}',
71
+ WORKFLOW_PROGRESS_ASYNC_OUTCOMES_END,
72
+ ]);
73
+ });
74
+
75
+ it('replaces server outcomes without changing AI-authored narrative', () => {
76
+ const narrative = 'Sent the receipt to John.\nSarah still needs delivery.';
77
+ const first = replaceWorkflowProgressAsyncOutcomeSection(narrative, {
78
+ sentEmails: { 2: { status: 'SENT', recipient: 'john@example.com' } },
79
+ });
80
+ const next = replaceWorkflowProgressAsyncOutcomeSection(first, {
81
+ sentEmails: { 2: { status: 'DELIVERED', recipient: 'john@example.com' } },
82
+ });
83
+
84
+ expect(next.startsWith(`${narrative}\n\n`)).toBe(true);
85
+ expect(
86
+ next.match(new RegExp(WORKFLOW_PROGRESS_ASYNC_OUTCOMES_START, 'g')),
87
+ ).toHaveLength(1);
88
+ expect(next).toContain(
89
+ 'sentEmails:2\t{"recipient":"john@example.com","status":"DELIVERED"}',
90
+ );
91
+ expect(next).not.toContain('"status":"SENT"');
92
+ });
93
+
94
+ it('escapes sentinel-like provider text so every outcome remains one safe line', () => {
95
+ const section = serializeWorkflowAsyncOutcomeSection({
96
+ sentSMS: {
97
+ 5: {
98
+ status: 'FAILED',
99
+ detail: `${WORKFLOW_PROGRESS_ASYNC_OUTCOMES_END}\nprovider failure`,
100
+ },
101
+ },
102
+ });
103
+
104
+ expect(section.split('\n')).toHaveLength(3);
105
+ expect(section).toContain(
106
+ '\\u003c\\u003c\\u003cEND_SERVER_WORKFLOW_ASYNC_OUTCOMES_V1\\u003e\\u003e\\u003e',
107
+ );
108
+ expect(
109
+ section.match(new RegExp(WORKFLOW_PROGRESS_ASYNC_OUTCOMES_END, 'g')),
110
+ ).toHaveLength(1);
111
+ });
112
+
113
+ it('upserts one outcome while preserving unrelated values and map entries', () => {
114
+ const values = updateWorkflowProgressAsyncOutcome(
115
+ {
116
+ progressReport: 'AI narrative',
117
+ customValue: 'keep me',
118
+ sentEmails: {
119
+ 4: { status: 'DELIVERED', recipient: 'john@example.com' },
120
+ },
121
+ },
122
+ 'sentEmails',
123
+ 9n,
124
+ {
125
+ status: 'BOUNCED',
126
+ recipient: 'sarah@example.com',
127
+ occurredAt: new Date('2026-07-02T13:00:00.000Z'),
128
+ },
129
+ );
130
+
131
+ expect(values.customValue).toBe('keep me');
132
+ expect(values.sentEmails).toEqual({
133
+ 4: { status: 'DELIVERED', recipient: 'john@example.com' },
134
+ 9: {
135
+ status: 'BOUNCED',
136
+ recipient: 'sarah@example.com',
137
+ occurredAt: '2026-07-02T13:00:00.000Z',
138
+ },
139
+ });
140
+ expect(values.sentSMS).toBeUndefined();
141
+ expect(values.progressReport).toContain('AI narrative');
142
+ expect(values.progressReport).toContain('sentEmails:4');
143
+ expect(values.progressReport).toContain('sentEmails:9');
144
+ });
145
+
146
+ it('treats missing outcome maps as empty without persisting unrelated empty maps', () => {
147
+ const values = updateWorkflowProgressAsyncOutcome({}, 'sentEmails', 1, {
148
+ status: 'QUEUED',
149
+ recipient: 'john@example.com',
150
+ });
151
+
152
+ expect(values.sentEmails?.['1']).toEqual({
153
+ status: 'QUEUED',
154
+ recipient: 'john@example.com',
155
+ });
156
+ expect(values.sentSMS).toBeUndefined();
157
+ expect(values.sentReminders).toBeUndefined();
158
+ expect(values.sentFollowUps).toBeUndefined();
159
+ expect(values.sentWaits).toBeUndefined();
160
+ });
161
+
162
+ it('compacts legacy empty maps without removing populated or unrelated values', () => {
163
+ expect(
164
+ compactWorkflowProgressValues({
165
+ progressReport: 'Keep this',
166
+ customValue: true,
167
+ sentEmails: {},
168
+ sentSMS: {
169
+ 2: { status: 'DELIVERED', recipient: '+14165550100' },
170
+ },
171
+ }),
172
+ ).toEqual({
173
+ progressReport: 'Keep this',
174
+ customValue: true,
175
+ sentSMS: {
176
+ 2: { status: 'DELIVERED', recipient: '+14165550100' },
177
+ },
178
+ });
179
+ });
180
+ });
@@ -0,0 +1,29 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { hasWorkflowActiveTarget } from '../src/utils/workflow.ts';
3
+ import { canLogTriggerWorkflows, isWorkflowTriggerKeyAllowed } from '../src/utils/workflowTriggers.ts';
4
+
5
+ describe('app interaction logs never trigger log workflows', () => {
6
+ it('excludes the APP_INTERACTIONS group and its system-only activities, and nothing else', () => {
7
+ expect(canLogTriggerWorkflows('APP_INTERACTIONS', 'CAMPAIGN_FORM_SUBMISSION')).toBe(false);
8
+ expect(canLogTriggerWorkflows('APP_INTERACTIONS')).toBe(false);
9
+ expect(canLogTriggerWorkflows(null, 'CAMPAIGN_FORM_SUBMISSION')).toBe(false);
10
+ expect(canLogTriggerWorkflows('SEED', 'SEED_PURCHASE_INFO')).toBe(true);
11
+ expect(canLogTriggerWorkflows('FIELD')).toBe(true);
12
+ });
13
+
14
+ it('never allows an interaction group or activity as a trigger key', () => {
15
+ expect(isWorkflowTriggerKeyAllowed('APP_INTERACTIONS')).toBe(false);
16
+ expect(isWorkflowTriggerKeyAllowed('activity:CAMPAIGN_FORM_SUBMISSION')).toBe(false);
17
+ expect(isWorkflowTriggerKeyAllowed('SEED')).toBe(true);
18
+ expect(isWorkflowTriggerKeyAllowed('activity:SEED_PURCHASE_INFO')).toBe(true);
19
+ });
20
+
21
+ it('does not count interaction-only subscriptions as an active trigger target', () => {
22
+ expect(hasWorkflowActiveTarget({ logTypes: ['APP_INTERACTIONS'], fieldDocId: null, nextAt: null })).toBe(false);
23
+ expect(hasWorkflowActiveTarget({ logTypes: ['APP_INTERACTIONS', 'SEED'], fieldDocId: null, nextAt: null })).toBe(true);
24
+ expect(hasWorkflowActiveTarget({ logTypes: null, fieldDocId: '7', nextAt: null })).toBe(true);
25
+ // A web form tracker's managed workflow has no log trigger; its campaign bindings start it
26
+ expect(hasWorkflowActiveTarget({ logTypes: [], fieldDocId: null, nextAt: null, agentNoteId: '41' })).toBe(true);
27
+ expect(hasWorkflowActiveTarget({ logTypes: [], fieldDocId: null, nextAt: null, agentNoteId: null })).toBe(false);
28
+ });
29
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "module": "ESNext",
5
+ "moduleResolution": "Bundler",
6
+ "allowImportingTsExtensions": true,
7
+ "noEmit": true,
8
+ "strict": true,
9
+ "esModuleInterop": true
10
+ },
11
+ "include": ["src/**/*"]
12
+ }
@@ -0,0 +1,8 @@
1
+ import { defineConfig } from 'vitest/config';
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ environment: 'node',
6
+ include: ['tests/**/*.test.ts?(x)'],
7
+ },
8
+ });