@jsb188/mday 1.1.8 → 1.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/package.json +105 -35
  2. package/src/constants/agentDoc.ts +12 -0
  3. package/src/constants/agentMemory.ts +154 -0
  4. package/src/constants/aiChat.ts +5 -0
  5. package/src/constants/campaign.ts +127 -0
  6. package/src/constants/dataTable.ts +48 -0
  7. package/src/constants/dataTableFieldTypes.generated.ts +63 -0
  8. package/src/constants/docAccess.ts +32 -0
  9. package/src/constants/email.ts +44 -0
  10. package/src/constants/fieldDoc.ts +34 -0
  11. package/src/constants/graphqlReactivity.ts +113 -0
  12. package/src/constants/inboundContact.ts +8 -0
  13. package/src/constants/integration.ts +13 -0
  14. package/src/constants/log.ts +7 -366
  15. package/src/constants/logActivityGroupColors.generated.ts +19 -0
  16. package/src/constants/logTaxonomy.generated.ts +1103 -0
  17. package/src/constants/noteDoc.ts +77 -0
  18. package/src/constants/organization.ts +18 -11
  19. package/src/constants/pageRoute.ts +23 -0
  20. package/src/constants/schedule.ts +220 -0
  21. package/src/constants/scheduleRuleTemplateSeed.ts +138 -0
  22. package/src/constants/scheduleRuleTemplates.generated.ts +145 -0
  23. package/src/constants/sheet.ts +168 -0
  24. package/src/constants/sheetRegionSources.generated.ts +578 -0
  25. package/src/constants/square.ts +16 -0
  26. package/src/constants/storage.ts +61 -0
  27. package/src/constants/usage.ts +79 -0
  28. package/src/schemas/editLogEntry.ts +1017 -738
  29. package/src/schemas/fieldDocsQuery.ts +12 -0
  30. package/src/schemas/inboundContact.ts +77 -0
  31. package/src/schemas/logsQuery.ts +57 -316
  32. package/src/schemas/organization.ts +53 -213
  33. package/src/types/account.d.ts +81 -0
  34. package/src/types/agentDoc.d.ts +64 -0
  35. package/src/types/agentMemory.d.ts +74 -0
  36. package/src/types/campaign.d.ts +683 -0
  37. package/src/types/dataTable.d.ts +366 -0
  38. package/src/types/docAccess.d.ts +96 -0
  39. package/src/types/email.d.ts +142 -0
  40. package/src/types/fieldDoc.d.ts +273 -0
  41. package/src/types/inboundContact.d.ts +57 -0
  42. package/src/types/integration.d.ts +42 -0
  43. package/src/types/log.d.ts +169 -190
  44. package/src/types/noteDoc.d.ts +86 -0
  45. package/src/types/organization.d.ts +175 -76
  46. package/src/types/pageRoute.d.ts +48 -0
  47. package/src/types/schedule.d.ts +391 -0
  48. package/src/types/sheet.d.ts +1130 -0
  49. package/src/types/storage.d.ts +242 -0
  50. package/src/types/usage.d.ts +106 -0
  51. package/src/types/workflow.d.ts +305 -0
  52. package/src/utils/agentDoc.ts +109 -0
  53. package/src/utils/aiChat.ts +26 -0
  54. package/src/utils/campaign.ts +741 -0
  55. package/src/utils/campaignFormEditor.ts +871 -0
  56. package/src/utils/dataTable.ts +849 -0
  57. package/src/utils/dataTableFieldTypeRegistry.ts +105 -0
  58. package/src/utils/dataTableRealtimeAuthority.ts +209 -0
  59. package/src/utils/dataTableRealtimeSelection.ts +134 -0
  60. package/src/utils/dataTableRowDocument.ts +508 -0
  61. package/src/utils/dataTableSource.ts +230 -0
  62. package/src/utils/docAccess.ts +383 -0
  63. package/src/utils/documentName.ts +53 -0
  64. package/src/utils/email.ts +172 -0
  65. package/src/utils/fieldDoc.ts +113 -0
  66. package/src/utils/log.ts +173 -778
  67. package/src/utils/logRegistryValue.ts +90 -0
  68. package/src/utils/logStoreRead.ts +266 -0
  69. package/src/utils/logTaxonomy.ts +76 -0
  70. package/src/utils/logTaxonomyDisplay.ts +439 -0
  71. package/src/utils/noteDoc.ts +827 -0
  72. package/src/utils/organization.ts +683 -81
  73. package/src/utils/pageRoute.ts +71 -0
  74. package/src/utils/schedule.ts +799 -0
  75. package/src/utils/scheduleEvaluation.ts +1994 -0
  76. package/src/utils/scheduleICS.ts +246 -0
  77. package/src/utils/scheduleRuleTemplates.ts +188 -0
  78. package/src/utils/sheet.ts +4973 -0
  79. package/src/utils/sheetAxisMove.ts +830 -0
  80. package/src/utils/sheetBootstrap.ts +1769 -0
  81. package/src/utils/sheetCommand.ts +423 -0
  82. package/src/utils/sheetExport.ts +238 -0
  83. package/src/utils/sheetFormulaEvaluation.ts +914 -0
  84. package/src/utils/sheetFormulaEvaluator.ts +852 -0
  85. package/src/utils/sheetJournal.ts +78 -0
  86. package/src/utils/sheetNumberFormat.ts +1211 -0
  87. package/src/utils/sheetRegion.ts +374 -0
  88. package/src/utils/sheetRegionFilter.ts +1259 -0
  89. package/src/utils/sheetRegionSourceRegistry.ts +594 -0
  90. package/src/utils/sheetSequence.ts +148 -0
  91. package/src/utils/sheetSnapshot.ts +992 -0
  92. package/src/utils/sheetStructureEdit.ts +1284 -0
  93. package/src/utils/square.ts +32 -0
  94. package/src/utils/storage.ts +152 -0
  95. package/src/utils/table-route.ts +262 -0
  96. package/src/utils/workflow.ts +380 -0
  97. package/tests/dataTableFieldTypeRegistry.test.ts +110 -0
  98. package/tests/docAccess.test.ts +525 -0
  99. package/tests/editLogEntry.test.ts +112 -0
  100. package/tests/noteDoc.test.ts +669 -0
  101. package/tests/schedule.test.ts +693 -0
  102. package/tests/scheduleEvaluation.test.ts +1970 -0
  103. package/tests/scheduleICS.test.ts +208 -0
  104. package/tests/scheduleRuleTemplates.test.ts +176 -0
  105. package/tests/scheduleSweepInvariants.test.ts +428 -0
  106. package/tests/sheetAxisMove.test.ts +343 -0
  107. package/tests/sheetFormulaEvaluator.test.ts +575 -0
  108. package/tests/sheetRegionFilter.test.ts +1135 -0
  109. package/tests/sheetSequence.test.ts +41 -0
  110. package/tests/sheetStructureEdit.test.ts +82 -0
  111. package/tsconfig.json +12 -0
  112. package/vitest.config.ts +8 -0
  113. package/src/constants/log-activityTexts.ts +0 -209
  114. package/src/constants/product.ts +0 -66
  115. package/src/constants/report.ts +0 -25
  116. package/src/schemas/childOrganizationsQuery.ts +0 -44
  117. package/src/schemas/productsQuery.ts +0 -145
  118. package/src/schemas/reportsQuery.ts +0 -107
  119. package/src/types/action.d.ts +0 -42
  120. package/src/types/product.d.ts +0 -173
  121. package/src/types/report.d.ts +0 -203
  122. package/src/utils/product-calEvent.ts +0 -508
  123. package/src/utils/route-helpers.ts +0 -583
@@ -0,0 +1,693 @@
1
+ import type { ScheduleExpandedInstance } from '@jsb188/mday/types/schedule.d.ts';
2
+ import {
3
+ expandScheduleSeriesInstances,
4
+ getScheduleOccurrenceEndAt,
5
+ getScheduleRecurrenceRRule,
6
+ validateScheduleRecurrence,
7
+ validateScheduleSlotTemplate,
8
+ } from '@jsb188/mday/utils/schedule.ts';
9
+ import { describe, expect, it } from 'vitest';
10
+
11
+ /*
12
+ * Recurrence expansion is the one piece of the scheduling feature with no
13
+ * database constraint behind it: a rule that drifts by an hour twice a year,
14
+ * or that silently drops the 31st of a short month, produces a schedule that
15
+ * looks right and is wrong. These cases pin the behavior that matters.
16
+ */
17
+
18
+ const LA = 'America/Los_Angeles';
19
+
20
+ /*
21
+ * Render expanded instances as local wall-clock strings in one zone, which is
22
+ * the representation a person reading a calendar actually sees.
23
+ */
24
+ function localStarts(
25
+ instances: ScheduleExpandedInstance[],
26
+ zone = LA,
27
+ ): string[] {
28
+ return instances.map((instance) =>
29
+ instance.startAt.toLocaleString('sv-SE', { timeZone: zone }),
30
+ );
31
+ }
32
+
33
+ /*
34
+ * Absolute length of each instance, in hours.
35
+ */
36
+ function durationHours(instances: ScheduleExpandedInstance[]): number[] {
37
+ return instances.map(
38
+ (instance) =>
39
+ (instance.endAt.getTime() - instance.startAt.getTime()) / 3_600_000,
40
+ );
41
+ }
42
+
43
+ describe('validateScheduleRecurrence', () => {
44
+ it('rejects rules the expander could not honor', () => {
45
+ expect(validateScheduleRecurrence({ freq: 'HOURLY' }).error).toBeTruthy();
46
+ expect(
47
+ validateScheduleRecurrence({ freq: 'DAILY', interval: 0 }).error,
48
+ ).toBeTruthy();
49
+ expect(
50
+ validateScheduleRecurrence({ freq: 'DAILY', interval: 53 }).error,
51
+ ).toBeTruthy();
52
+ expect(
53
+ validateScheduleRecurrence({ freq: 'WEEKLY', byWeekday: [7] }).error,
54
+ ).toBeTruthy();
55
+ expect(
56
+ validateScheduleRecurrence({ freq: 'MONTHLY', byMonthDay: [0] }).error,
57
+ ).toBeTruthy();
58
+ });
59
+
60
+ it('rejects narrowing that does not apply to the frequency', () => {
61
+ // Silently keeping these would make two different-looking rules behave
62
+ // identically, which is worse than refusing them
63
+ expect(
64
+ validateScheduleRecurrence({ freq: 'DAILY', byWeekday: [1] }).error,
65
+ ).toBeTruthy();
66
+ expect(
67
+ validateScheduleRecurrence({ freq: 'WEEKLY', byMonth: [1] }).error,
68
+ ).toBeTruthy();
69
+ expect(
70
+ validateScheduleRecurrence({ freq: 'WEEKLY', byMonthDay: [1] }).error,
71
+ ).toBeTruthy();
72
+ });
73
+
74
+ it('normalizes weekdays and defaults the interval', () => {
75
+ expect(
76
+ validateScheduleRecurrence({ freq: 'WEEKLY', byWeekday: [5, 1, 3, 1] })
77
+ .recurrence,
78
+ ).toEqual({
79
+ freq: 'WEEKLY',
80
+ interval: 1,
81
+ byWeekday: [1, 3, 5],
82
+ });
83
+ });
84
+
85
+ it('accepts the last-day-of-month sentinel', () => {
86
+ expect(
87
+ validateScheduleRecurrence({ freq: 'MONTHLY', byMonthDay: [-1] })
88
+ .recurrence?.byMonthDay,
89
+ ).toEqual([-1]);
90
+ });
91
+ });
92
+
93
+ describe('validateScheduleSlotTemplate', () => {
94
+ it('defaults the required count and assigns ordinals', () => {
95
+ expect(
96
+ validateScheduleSlotTemplate([{ key: 'lead' }]).slotTemplate,
97
+ ).toEqual([
98
+ {
99
+ key: 'lead',
100
+ label: null,
101
+ requiredCount: 1,
102
+ predicate: null,
103
+ ordinal: 0,
104
+ },
105
+ ]);
106
+ });
107
+
108
+ it('rejects duplicate keys', () => {
109
+ expect(
110
+ validateScheduleSlotTemplate([{ key: 'lead' }, { key: 'lead' }]).error,
111
+ ).toBeTruthy();
112
+ });
113
+
114
+ it('rejects a negative required count', () => {
115
+ expect(
116
+ validateScheduleSlotTemplate([{ key: 'lead', requiredCount: -1 }]).error,
117
+ ).toBeTruthy();
118
+ });
119
+ });
120
+
121
+ describe('expandScheduleSeriesInstances — daylight saving', () => {
122
+ // 2026-03-02 is a Monday; the US springs forward on 2026-03-08
123
+ const weekdayShift = {
124
+ recurrence: { freq: 'WEEKLY' as const, interval: 1, byWeekday: [1, 3, 5] },
125
+ timeZone: LA,
126
+ startsAt: new Date('2026-03-02T15:00:00Z'), // 07:00 local, PST
127
+ durationMinutes: 240,
128
+ };
129
+
130
+ it('keeps the local start time fixed across the transition', () => {
131
+ const instances = expandScheduleSeriesInstances(weekdayShift, {
132
+ to: new Date('2026-03-17T00:00:00Z'),
133
+ });
134
+
135
+ expect(localStarts(instances)).toEqual([
136
+ '2026-03-02 07:00:00',
137
+ '2026-03-04 07:00:00',
138
+ '2026-03-06 07:00:00',
139
+ '2026-03-09 07:00:00',
140
+ '2026-03-11 07:00:00',
141
+ '2026-03-13 07:00:00',
142
+ '2026-03-16 07:00:00',
143
+ ]);
144
+ });
145
+
146
+ it('moves the absolute instant by an hour instead of the local time', () => {
147
+ const instances = expandScheduleSeriesInstances(weekdayShift, {
148
+ to: new Date('2026-03-17T00:00:00Z'),
149
+ });
150
+
151
+ expect(instances[0].startAt.toISOString()).toBe('2026-03-02T15:00:00.000Z');
152
+ expect(instances[3].startAt.toISOString()).toBe('2026-03-09T14:00:00.000Z');
153
+ });
154
+
155
+ it('keeps a timed shift the same number of real hours', () => {
156
+ const instances = expandScheduleSeriesInstances(weekdayShift, {
157
+ to: new Date('2026-03-17T00:00:00Z'),
158
+ });
159
+
160
+ expect(durationHours(instances)).toEqual([4, 4, 4, 4, 4, 4, 4]);
161
+ });
162
+
163
+ it('produces exactly one instance on a day whose local start time does not exist', () => {
164
+ // 02:30 local falls inside the skipped hour on 2026-03-08
165
+ const instances = expandScheduleSeriesInstances(
166
+ {
167
+ recurrence: { freq: 'DAILY', interval: 1 },
168
+ timeZone: LA,
169
+ startsAt: new Date('2026-03-06T10:30:00Z'),
170
+ durationMinutes: 60,
171
+ },
172
+ { to: new Date('2026-03-11T00:00:00Z') },
173
+ );
174
+
175
+ expect(localStarts(instances)).toEqual([
176
+ '2026-03-06 02:30:00',
177
+ '2026-03-07 02:30:00',
178
+ // Moved past the gap rather than dropped, as calendar clients do
179
+ '2026-03-08 03:30:00',
180
+ '2026-03-09 02:30:00',
181
+ '2026-03-10 02:30:00',
182
+ ]);
183
+ });
184
+
185
+ it('measures an all-day entry in local days, so the fall-back day is 25 hours', () => {
186
+ // The US falls back on 2026-11-01
187
+ const instances = expandScheduleSeriesInstances(
188
+ {
189
+ recurrence: { freq: 'DAILY', interval: 1 },
190
+ timeZone: LA,
191
+ startsAt: new Date('2026-10-31T07:00:00Z'), // local midnight
192
+ durationMinutes: 1440,
193
+ allDay: true,
194
+ },
195
+ { to: new Date('2026-11-04T00:00:00Z') },
196
+ );
197
+
198
+ expect(localStarts(instances)).toEqual([
199
+ '2026-10-31 00:00:00',
200
+ '2026-11-01 00:00:00',
201
+ '2026-11-02 00:00:00',
202
+ '2026-11-03 00:00:00',
203
+ ]);
204
+ // Absolute-minute math would have ended the middle day an hour early
205
+ expect(durationHours(instances)).toEqual([24, 25, 24, 24]);
206
+ });
207
+ });
208
+
209
+ describe('expandScheduleSeriesInstances — rules', () => {
210
+ it('honors a multi-week interval', () => {
211
+ const instances = expandScheduleSeriesInstances(
212
+ {
213
+ recurrence: { freq: 'WEEKLY', interval: 2, byWeekday: [2] },
214
+ timeZone: LA,
215
+ startsAt: new Date('2026-03-03T18:00:00Z'), // Tuesday 10:00 local
216
+ durationMinutes: 60,
217
+ },
218
+ { to: new Date('2026-04-15T00:00:00Z') },
219
+ );
220
+
221
+ expect(localStarts(instances)).toEqual([
222
+ '2026-03-03 10:00:00',
223
+ '2026-03-17 10:00:00',
224
+ '2026-03-31 10:00:00',
225
+ '2026-04-14 10:00:00',
226
+ ]);
227
+ });
228
+
229
+ it('resolves the last day of each month', () => {
230
+ const instances = expandScheduleSeriesInstances(
231
+ {
232
+ recurrence: { freq: 'MONTHLY', interval: 1, byMonthDay: [-1] },
233
+ timeZone: LA,
234
+ startsAt: new Date('2026-01-31T17:00:00Z'),
235
+ durationMinutes: 60,
236
+ },
237
+ { to: new Date('2026-05-01T00:00:00Z') },
238
+ );
239
+
240
+ expect(localStarts(instances)).toEqual([
241
+ '2026-01-31 09:00:00',
242
+ '2026-02-28 09:00:00',
243
+ '2026-03-31 09:00:00',
244
+ '2026-04-30 09:00:00',
245
+ ]);
246
+ });
247
+
248
+ it('skips months too short for the requested day instead of rolling forward', () => {
249
+ const instances = expandScheduleSeriesInstances(
250
+ {
251
+ recurrence: { freq: 'MONTHLY', interval: 1, byMonthDay: [31] },
252
+ timeZone: LA,
253
+ startsAt: new Date('2026-01-31T17:00:00Z'),
254
+ durationMinutes: 60,
255
+ },
256
+ { to: new Date('2026-06-01T00:00:00Z') },
257
+ );
258
+
259
+ // Rolling into March 1st / May 1st would silently reschedule the entry
260
+ expect(localStarts(instances)).toEqual([
261
+ '2026-01-31 09:00:00',
262
+ '2026-03-31 09:00:00',
263
+ '2026-05-31 09:00:00',
264
+ ]);
265
+ });
266
+
267
+ it('expands a yearly rule', () => {
268
+ const instances = expandScheduleSeriesInstances(
269
+ {
270
+ recurrence: {
271
+ freq: 'YEARLY',
272
+ interval: 1,
273
+ byMonth: [7],
274
+ byMonthDay: [4],
275
+ },
276
+ timeZone: LA,
277
+ startsAt: new Date('2026-07-04T17:00:00Z'),
278
+ durationMinutes: 120,
279
+ },
280
+ { to: new Date('2029-01-01T00:00:00Z') },
281
+ );
282
+
283
+ expect(localStarts(instances)).toEqual([
284
+ '2026-07-04 10:00:00',
285
+ '2027-07-04 10:00:00',
286
+ '2028-07-04 10:00:00',
287
+ ]);
288
+ });
289
+
290
+ it('never emits a date earlier in the anchor week than the series start', () => {
291
+ const instances = expandScheduleSeriesInstances(
292
+ {
293
+ recurrence: {
294
+ freq: 'WEEKLY',
295
+ interval: 1,
296
+ byWeekday: [0, 1, 2, 3, 4, 5, 6],
297
+ },
298
+ timeZone: LA,
299
+ startsAt: new Date('2026-03-05T15:00:00Z'), // a Thursday
300
+ durationMinutes: 60,
301
+ },
302
+ { limit: 3 },
303
+ );
304
+
305
+ expect(localStarts(instances)).toEqual([
306
+ '2026-03-05 07:00:00',
307
+ '2026-03-06 07:00:00',
308
+ '2026-03-07 07:00:00',
309
+ ]);
310
+ });
311
+ });
312
+
313
+ describe('expandScheduleSeriesInstances — bounds', () => {
314
+ const daily = {
315
+ recurrence: { freq: 'DAILY' as const, interval: 1 },
316
+ timeZone: LA,
317
+ startsAt: new Date('2026-03-06T15:00:00Z'),
318
+ durationMinutes: 60,
319
+ };
320
+
321
+ it('counts from the series start, not from the requested window', () => {
322
+ // Without this, paging forward through a bounded series would keep
323
+ // handing out instances long after its count ran out
324
+ const instances = expandScheduleSeriesInstances(
325
+ {
326
+ recurrence: {
327
+ freq: 'WEEKLY',
328
+ interval: 1,
329
+ byWeekday: [1, 3, 5],
330
+ count: 4,
331
+ },
332
+ timeZone: LA,
333
+ startsAt: new Date('2026-03-02T15:00:00Z'),
334
+ durationMinutes: 240,
335
+ },
336
+ {
337
+ from: new Date('2026-03-09T00:00:00Z'),
338
+ to: new Date('2027-01-01T00:00:00Z'),
339
+ },
340
+ );
341
+
342
+ expect(localStarts(instances)).toEqual(['2026-03-09 07:00:00']);
343
+ });
344
+
345
+ it('windowing returns exactly the tail of the unwindowed stream', () => {
346
+ const series = {
347
+ recurrence: {
348
+ freq: 'WEEKLY' as const,
349
+ interval: 1,
350
+ byWeekday: [1, 3, 5],
351
+ },
352
+ timeZone: LA,
353
+ startsAt: new Date('2026-03-02T15:00:00Z'),
354
+ durationMinutes: 240,
355
+ };
356
+ const to = new Date('2026-03-17T00:00:00Z');
357
+ const full = expandScheduleSeriesInstances(series, { to });
358
+ const windowed = expandScheduleSeriesInstances(series, {
359
+ from: new Date('2026-03-09T00:00:00Z'),
360
+ to,
361
+ });
362
+
363
+ expect(localStarts(windowed)).toEqual(localStarts(full).slice(3));
364
+ });
365
+
366
+ it('treats "until" as inclusive of an instance starting exactly on it', () => {
367
+ const instances = expandScheduleSeriesInstances(
368
+ {
369
+ ...daily,
370
+ recurrence: {
371
+ freq: 'DAILY',
372
+ interval: 1,
373
+ until: '2026-03-08T15:00:00.000Z',
374
+ },
375
+ },
376
+ {},
377
+ );
378
+
379
+ expect(instances).toHaveLength(3);
380
+ });
381
+
382
+ it('stops at the series end date', () => {
383
+ const instances = expandScheduleSeriesInstances(
384
+ {
385
+ ...daily,
386
+ endsAt: new Date('2026-03-08T15:00:00.000Z'),
387
+ },
388
+ {},
389
+ );
390
+
391
+ expect(instances).toHaveLength(3);
392
+ });
393
+
394
+ it('respects an explicit limit', () => {
395
+ expect(expandScheduleSeriesInstances(daily, { limit: 2 })).toHaveLength(2);
396
+ });
397
+
398
+ // Anchored in July so no daylight-saving transition falls inside the
399
+ // window: these cases are about the from/to/limit bounds, and the DST
400
+ // behavior they would otherwise trip over is pinned in its own block above
401
+ const summerDaily = {
402
+ recurrence: { freq: 'DAILY' as const, interval: 1 },
403
+ timeZone: LA,
404
+ startsAt: new Date('2026-07-06T16:00:00Z'),
405
+ durationMinutes: 60,
406
+ };
407
+
408
+ it('honors from, to and limit together — the shape materialization uses', () => {
409
+ // Every production call passes all three (scheduleOps-materialize.ts
410
+ // expands from the watermark to the horizon under a per-pass cap), and
411
+ // no other case here exercises the combination
412
+ const instances = expandScheduleSeriesInstances(summerDaily, {
413
+ from: new Date('2026-07-09T16:00:00.000Z'),
414
+ to: new Date('2026-07-20T00:00:00Z'),
415
+ limit: 3,
416
+ });
417
+
418
+ expect(localStarts(instances)).toEqual([
419
+ '2026-07-09 09:00:00',
420
+ '2026-07-10 09:00:00',
421
+ '2026-07-11 09:00:00',
422
+ ]);
423
+ });
424
+
425
+ it('treats "from" as inclusive of an instance starting exactly on it', () => {
426
+ // The materializer resumes at watermark + 1ms, so an off-by-one here
427
+ // either re-emits an instance (harmless, the conflict fence absorbs it)
428
+ // or skips one permanently, because the watermark only moves forward
429
+ const onTheInstant = expandScheduleSeriesInstances(summerDaily, {
430
+ from: new Date('2026-07-07T16:00:00.000Z'),
431
+ limit: 1,
432
+ });
433
+ const oneMsLater = expandScheduleSeriesInstances(summerDaily, {
434
+ from: new Date('2026-07-07T16:00:00.001Z'),
435
+ limit: 1,
436
+ });
437
+
438
+ expect(onTheInstant[0].startAt.toISOString()).toBe(
439
+ '2026-07-07T16:00:00.000Z',
440
+ );
441
+ expect(oneMsLater[0].startAt.toISOString()).toBe(
442
+ '2026-07-08T16:00:00.000Z',
443
+ );
444
+ });
445
+
446
+ it('carries the recurrence key as the exact instance instant', () => {
447
+ // recurrenceKey is the (seriesId, recurrenceKey) ON CONFLICT identity,
448
+ // so a millisecond of drift inserts a duplicate occurrence rather than
449
+ // colliding with the row it is meant to be
450
+ const instances = expandScheduleSeriesInstances(summerDaily, { limit: 2 });
451
+
452
+ for (const instance of instances) {
453
+ expect(instance.recurrenceKey.getTime()).toBe(instance.startAt.getTime());
454
+ }
455
+ });
456
+
457
+ it('terminates on an open-ended rule with no window', () => {
458
+ // The iteration cap is what stops a rule with no count, until, endsAt,
459
+ // or "to" from running forever
460
+ const instances = expandScheduleSeriesInstances(daily, {});
461
+
462
+ expect(instances.length).toBeGreaterThan(0);
463
+ expect(instances.length).toBeLessThanOrEqual(20_000);
464
+ });
465
+
466
+ it('falls back to the default zone rather than throwing on an unknown one', () => {
467
+ expect(
468
+ expandScheduleSeriesInstances(
469
+ { ...daily, timeZone: 'Mars/Olympus_Mons' },
470
+ { limit: 1 },
471
+ ),
472
+ ).toHaveLength(1);
473
+ });
474
+
475
+ it('returns nothing for an unreadable start date', () => {
476
+ expect(
477
+ expandScheduleSeriesInstances(
478
+ { ...daily, startsAt: 'not a date' },
479
+ { limit: 1 },
480
+ ),
481
+ ).toEqual([]);
482
+ });
483
+ });
484
+
485
+ describe('expandScheduleSeriesInstances — unordered rule input', () => {
486
+ /*
487
+ * A rule reaching the expander has not necessarily been through
488
+ * validateScheduleRecurrence: the documented "preview an unsaved series"
489
+ * path hands it a rule straight from a form. Every generator stops at the
490
+ * first instance past its bound with a break, so an out-of-order list makes
491
+ * the break fire early and silently drops the instances behind it.
492
+ */
493
+
494
+ const weekly = (byWeekday: number[]) =>
495
+ expandScheduleSeriesInstances(
496
+ {
497
+ recurrence: { freq: 'WEEKLY' as const, interval: 1, byWeekday },
498
+ timeZone: LA,
499
+ startsAt: new Date('2026-07-06T16:00:00Z'),
500
+ durationMinutes: 60,
501
+ },
502
+ { to: new Date('2026-07-22T12:00:00Z') },
503
+ );
504
+
505
+ it('drops no instance when the weekday list is unordered', () => {
506
+ // [5,1] used to emit Friday first, break on it, and never produce the
507
+ // Monday behind it — Monday 2026-07-20 simply vanished from the schedule
508
+ expect(localStarts(weekly([5, 1]))).toEqual(localStarts(weekly([1, 5])));
509
+ });
510
+
511
+ it('emits chronologically when the weekday list is unordered', () => {
512
+ const starts = localStarts(weekly([5, 1]));
513
+
514
+ expect(starts).toEqual([...starts].sort());
515
+ });
516
+
517
+ it('drops no instance when the month list is unordered', () => {
518
+ const yearly = (byMonth: number[]) =>
519
+ expandScheduleSeriesInstances(
520
+ {
521
+ recurrence: {
522
+ freq: 'YEARLY' as const,
523
+ interval: 1,
524
+ byMonth,
525
+ byMonthDay: [10],
526
+ },
527
+ timeZone: LA,
528
+ startsAt: new Date('2026-01-10T16:00:00Z'),
529
+ durationMinutes: 60,
530
+ },
531
+ { to: new Date('2026-08-01T00:00:00Z') },
532
+ );
533
+
534
+ expect(localStarts(yearly([9, 3, 6]))).toEqual(
535
+ localStarts(yearly([3, 6, 9])),
536
+ );
537
+ });
538
+ });
539
+
540
+ describe('expandScheduleSeriesInstances — rules that can never fire', () => {
541
+ // A generator that yields nothing must still terminate. The consumer-side
542
+ // iteration cap only counts values it received, so an unsatisfiable rule
543
+ // used to spin inside the generator forever — synchronously, taking the
544
+ // whole event loop with it.
545
+
546
+ it('returns from a monthly rule whose visited months never contain the day', () => {
547
+ // Every 12 months from February: the 31st is never reached
548
+ const instances = expandScheduleSeriesInstances(
549
+ {
550
+ recurrence: { freq: 'MONTHLY', interval: 12, byMonthDay: [31] },
551
+ timeZone: LA,
552
+ startsAt: new Date('2026-02-10T17:00:00Z'),
553
+ durationMinutes: 60,
554
+ },
555
+ { limit: 5 },
556
+ );
557
+
558
+ expect(instances).toEqual([]);
559
+ });
560
+
561
+ it('returns from a yearly rule naming a day its month never reaches', () => {
562
+ const instances = expandScheduleSeriesInstances(
563
+ {
564
+ recurrence: {
565
+ freq: 'YEARLY',
566
+ interval: 1,
567
+ byMonth: [2],
568
+ byMonthDay: [30],
569
+ },
570
+ timeZone: LA,
571
+ startsAt: new Date('2026-01-01T08:00:00Z'),
572
+ durationMinutes: 60,
573
+ },
574
+ { limit: 5 },
575
+ );
576
+
577
+ expect(instances).toEqual([]);
578
+ });
579
+
580
+ it('refuses such a yearly rule at validation, before it can be stored', () => {
581
+ expect(
582
+ validateScheduleRecurrence({
583
+ freq: 'YEARLY',
584
+ byMonth: [2],
585
+ byMonthDay: [30],
586
+ }).error,
587
+ ).toBeTruthy();
588
+ expect(
589
+ validateScheduleRecurrence({
590
+ freq: 'YEARLY',
591
+ byMonth: [4, 6],
592
+ byMonthDay: [31],
593
+ }).error,
594
+ ).toBeTruthy();
595
+ });
596
+
597
+ it('still accepts February 29th, which a leap year does reach', () => {
598
+ expect(
599
+ validateScheduleRecurrence({
600
+ freq: 'YEARLY',
601
+ byMonth: [2],
602
+ byMonthDay: [29],
603
+ }).error,
604
+ ).toBeUndefined();
605
+
606
+ const instances = expandScheduleSeriesInstances(
607
+ {
608
+ recurrence: {
609
+ freq: 'YEARLY',
610
+ interval: 1,
611
+ byMonth: [2],
612
+ byMonthDay: [29],
613
+ },
614
+ timeZone: LA,
615
+ startsAt: new Date('2027-01-01T08:00:00Z'),
616
+ durationMinutes: 60,
617
+ },
618
+ { limit: 2 },
619
+ );
620
+
621
+ expect(localStarts(instances).map((s) => s.slice(0, 10))).toEqual([
622
+ '2028-02-29',
623
+ '2032-02-29',
624
+ ]);
625
+ });
626
+
627
+ it('emits one instance when -1 and the concrete last day are both listed', () => {
628
+ // Both resolve to the 31st in a 31-day month; emitting twice would also
629
+ // double-count against the rule's own "count" bound
630
+ const instances = expandScheduleSeriesInstances(
631
+ {
632
+ recurrence: { freq: 'MONTHLY', interval: 1, byMonthDay: [31, -1] },
633
+ timeZone: LA,
634
+ startsAt: new Date('2026-01-31T17:00:00Z'),
635
+ durationMinutes: 60,
636
+ },
637
+ { to: new Date('2026-04-01T00:00:00Z') },
638
+ );
639
+
640
+ expect(localStarts(instances)).toEqual([
641
+ '2026-01-31 09:00:00',
642
+ // February resolves -1 to the 28th; the 31st is skipped
643
+ '2026-02-28 09:00:00',
644
+ '2026-03-31 09:00:00',
645
+ ]);
646
+ });
647
+ });
648
+
649
+ describe('validateScheduleSlotTemplate — column bounds', () => {
650
+ it('rejects a key longer than the schedule_slots column', () => {
651
+ expect(
652
+ validateScheduleSlotTemplate([{ key: 'k'.repeat(81) }]).error,
653
+ ).toBeTruthy();
654
+ expect(
655
+ validateScheduleSlotTemplate([{ key: 'k'.repeat(80) }]).error,
656
+ ).toBeUndefined();
657
+ });
658
+
659
+ it('rejects an over-length label', () => {
660
+ expect(
661
+ validateScheduleSlotTemplate([{ key: 'lead', label: 'l'.repeat(151) }])
662
+ .error,
663
+ ).toBeTruthy();
664
+ expect(
665
+ validateScheduleSlotTemplate([{ key: 'lead', label: 'l'.repeat(150) }])
666
+ .error,
667
+ ).toBeUndefined();
668
+ });
669
+ });
670
+
671
+ describe('derived helpers', () => {
672
+ it('ends a timed occurrence after its absolute duration', () => {
673
+ expect(
674
+ getScheduleOccurrenceEndAt(
675
+ new Date('2026-03-02T15:00:00Z'),
676
+ 240,
677
+ LA,
678
+ false,
679
+ ).toISOString(),
680
+ ).toBe('2026-03-02T19:00:00.000Z');
681
+ });
682
+
683
+ it('projects a rule to an RRULE line for calendar export', () => {
684
+ expect(
685
+ getScheduleRecurrenceRRule({
686
+ freq: 'WEEKLY',
687
+ interval: 2,
688
+ byWeekday: [1, 3, 5],
689
+ count: 10,
690
+ }),
691
+ ).toBe('FREQ=WEEKLY;INTERVAL=2;BYDAY=MO,WE,FR;COUNT=10');
692
+ });
693
+ });