@jsb188/calendar 1.1.10

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.
@@ -0,0 +1,403 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import {
3
+ getCalendarAllDayLayout,
4
+ getCalendarEventsByDateKey,
5
+ getCalendarMonthCells,
6
+ getCalendarTimedLayout,
7
+ } from '../src/libs/calendar-geometry.ts';
8
+ import { getCalendarRange, parseCalendarDateParam } from '../src/libs/calendar-range.ts';
9
+ import type { CalendarEventObj } from '../src/types/calendar.d.ts';
10
+
11
+ const LA = 'America/Los_Angeles';
12
+ const NOW = new Date('2026-07-22T19:00:00Z');
13
+
14
+ /*
15
+ * Build the week of 2026-07-19 in Los Angeles.
16
+ */
17
+
18
+ function buildWeek() {
19
+ return getCalendarRange({
20
+ view: 'WEEK',
21
+ anchorDate: parseCalendarDateParam('2026-07-22', NOW, LA),
22
+ now: NOW,
23
+ timeZone: LA,
24
+ });
25
+ }
26
+
27
+ /*
28
+ * Build one timed event. Times are ISO strings so the tests read as instants.
29
+ */
30
+
31
+ function buildEvent(id: string, startAt: string, endAt: string, overrides: Partial<CalendarEventObj> = {}): CalendarEventObj {
32
+ return {
33
+ id,
34
+ calendarId: 'cal1',
35
+ title: id,
36
+ startAt: new Date(startAt),
37
+ endAt: new Date(endAt),
38
+ allDay: false,
39
+ kind: 'SHIFT',
40
+ status: 'PUBLISHED',
41
+ assigneeAccountIds: [],
42
+ ...overrides,
43
+ };
44
+ }
45
+
46
+ describe('getCalendarTimedLayout', () => {
47
+ it('places one event at the right minutes of its day', () => {
48
+ const week = buildWeek();
49
+ // 09:00–17:00 local on Wednesday the 22nd
50
+ const layout = getCalendarTimedLayout([buildEvent('a', '2026-07-22T16:00:00Z', '2026-07-23T00:00:00Z')], week.days);
51
+
52
+ expect(layout).toHaveLength(1);
53
+ expect(layout[0].dayIndex).toBe(3);
54
+ expect(layout[0].startMinute).toBe(9 * 60);
55
+ expect(layout[0].endMinute).toBe(17 * 60);
56
+ expect(layout[0].left).toBe(0);
57
+ expect(layout[0].width).toBe(1);
58
+ });
59
+
60
+ it('splits an overnight shift into one rectangle per day', () => {
61
+ const week = buildWeek();
62
+ // 22:00 Wednesday to 06:00 Thursday, local
63
+ const layout = getCalendarTimedLayout(
64
+ [buildEvent('night', '2026-07-23T05:00:00Z', '2026-07-23T13:00:00Z')],
65
+ week.days,
66
+ ).sort((left, right) => left.dayIndex - right.dayIndex);
67
+
68
+ expect(layout).toHaveLength(2);
69
+
70
+ expect(layout[0].dayIndex).toBe(3);
71
+ expect(layout[0].startMinute).toBe(22 * 60);
72
+ expect(layout[0].endMinute).toBe(24 * 60);
73
+ expect(layout[0].continuesToNextDay).toBe(true);
74
+ expect(layout[0].continuesFromPreviousDay).toBe(false);
75
+
76
+ expect(layout[1].dayIndex).toBe(4);
77
+ expect(layout[1].startMinute).toBe(0);
78
+ expect(layout[1].endMinute).toBe(6 * 60);
79
+ expect(layout[1].continuesFromPreviousDay).toBe(true);
80
+ expect(layout[1].continuesToNextDay).toBe(false);
81
+ });
82
+
83
+ it('does not draw an event ending exactly at midnight on the next day', () => {
84
+ const week = buildWeek();
85
+ const layout = getCalendarTimedLayout(
86
+ [buildEvent('closes', '2026-07-22T22:00:00Z', '2026-07-23T07:00:00Z')],
87
+ week.days,
88
+ );
89
+
90
+ // A half-open interval ending at the boundary belongs only to the day it ran
91
+ expect(layout).toHaveLength(1);
92
+ expect(layout[0].dayIndex).toBe(3);
93
+ expect(layout[0].endMinute).toBe(24 * 60);
94
+ });
95
+
96
+ it('splits two overlapping events down the middle', () => {
97
+ const week = buildWeek();
98
+ const layout = getCalendarTimedLayout([
99
+ buildEvent('a', '2026-07-22T16:00:00Z', '2026-07-22T20:00:00Z'),
100
+ buildEvent('b', '2026-07-22T17:00:00Z', '2026-07-22T21:00:00Z'),
101
+ ], week.days);
102
+
103
+ expect(layout.map((entry) => entry.columnCount)).toEqual([2, 2]);
104
+ expect(layout.map((entry) => entry.left).sort()).toEqual([0, 0.5]);
105
+ expect(layout.every((entry) => entry.width === 0.5)).toBe(true);
106
+ });
107
+
108
+ it('gives a non-overlapping later event the full width', () => {
109
+ const week = buildWeek();
110
+ // Two at 9am, one at 2pm: the 2pm event is its own cluster
111
+ const layout = getCalendarTimedLayout([
112
+ buildEvent('a', '2026-07-22T16:00:00Z', '2026-07-22T18:00:00Z'),
113
+ buildEvent('b', '2026-07-22T16:00:00Z', '2026-07-22T18:00:00Z'),
114
+ buildEvent('c', '2026-07-22T21:00:00Z', '2026-07-22T22:00:00Z'),
115
+ ], week.days);
116
+
117
+ const afternoon = layout.find((entry) => entry.event.id === 'c');
118
+
119
+ expect(afternoon?.columnCount).toBe(1);
120
+ expect(afternoon?.width).toBe(1);
121
+ expect(layout.filter((entry) => entry.event.id !== 'c').every((entry) => entry.columnCount === 2)).toBe(true);
122
+ });
123
+
124
+ it('treats a transitively-overlapping chain as one cluster', () => {
125
+ const week = buildWeek();
126
+ // A 9-12, B 10-13, C 11-14 — A and C never touch, but every column is
127
+ // still busy when C starts, so all three must share the width or B
128
+ // would be drawn over one of them
129
+ const layout = getCalendarTimedLayout([
130
+ buildEvent('a', '2026-07-22T16:00:00Z', '2026-07-22T19:00:00Z'),
131
+ buildEvent('b', '2026-07-22T17:00:00Z', '2026-07-22T20:00:00Z'),
132
+ buildEvent('c', '2026-07-22T18:00:00Z', '2026-07-22T21:00:00Z'),
133
+ ], week.days);
134
+
135
+ expect(layout.map((entry) => entry.columnCount)).toEqual([3, 3, 3]);
136
+ expect(layout.map((entry) => entry.column).sort()).toEqual([0, 1, 2]);
137
+ });
138
+
139
+ it('packs a chain into fewer columns when an earlier one has ended', () => {
140
+ const week = buildWeek();
141
+ // A 9-11, B 10-12, C 11:30-13 — the same chain shape, but C can reuse
142
+ // the column A vacated, so two columns is the correct answer and three
143
+ // would waste half the day's width
144
+ const layout = getCalendarTimedLayout([
145
+ buildEvent('a', '2026-07-22T16:00:00Z', '2026-07-22T18:00:00Z'),
146
+ buildEvent('b', '2026-07-22T17:00:00Z', '2026-07-22T19:00:00Z'),
147
+ buildEvent('c', '2026-07-22T18:30:00Z', '2026-07-22T20:00:00Z'),
148
+ ], week.days);
149
+
150
+ const byId = Object.fromEntries(layout.map((entry) => [entry.event.id, entry]));
151
+
152
+ expect(layout.map((entry) => entry.columnCount)).toEqual([2, 2, 2]);
153
+ expect(byId.c.column).toBe(byId.a.column);
154
+ expect(byId.b.column).not.toBe(byId.a.column);
155
+ });
156
+
157
+ it('reuses a freed column for a later non-overlapping event in the same cluster', () => {
158
+ const week = buildWeek();
159
+ // A 9-10, B 9-12, C 10:30-11 — C can take A's column
160
+ const layout = getCalendarTimedLayout([
161
+ buildEvent('a', '2026-07-22T16:00:00Z', '2026-07-22T17:00:00Z'),
162
+ buildEvent('b', '2026-07-22T16:00:00Z', '2026-07-22T19:00:00Z'),
163
+ buildEvent('c', '2026-07-22T17:30:00Z', '2026-07-22T18:00:00Z'),
164
+ ], week.days);
165
+
166
+ const byId = Object.fromEntries(layout.map((entry) => [entry.event.id, entry]));
167
+
168
+ expect(byId.c.column).toBe(byId.a.column);
169
+ expect(byId.b.columnCount).toBe(2);
170
+ });
171
+
172
+ it('stacks rather than slivers past the column cap', () => {
173
+ const week = buildWeek();
174
+ const events = Array.from({ length: 8 }, (_unused, index) => (
175
+ buildEvent(`e${index}`, '2026-07-22T16:00:00Z', '2026-07-22T20:00:00Z')
176
+ ));
177
+ const layout = getCalendarTimedLayout(events, week.days);
178
+
179
+ expect(layout.every((entry) => entry.columnCount === 8)).toBe(true);
180
+ // Nothing is narrower than a readable slice, and each peeks out
181
+ expect(layout.every((entry) => entry.width >= 0.1)).toBe(true);
182
+ expect(new Set(layout.map((entry) => entry.left)).size).toBeGreaterThan(1);
183
+ });
184
+
185
+ it('keeps two near-instant events side by side rather than stacked', () => {
186
+ const week = buildWeek();
187
+ // Five minutes apart and five minutes long: they do not overlap in time,
188
+ // but both are drawn at the minimum height, so they overlap on screen
189
+ const layout = getCalendarTimedLayout([
190
+ buildEvent('a', '2026-07-22T16:00:00Z', '2026-07-22T16:05:00Z'),
191
+ buildEvent('b', '2026-07-22T16:05:00Z', '2026-07-22T16:10:00Z'),
192
+ ], week.days);
193
+
194
+ expect(layout.map((entry) => entry.columnCount)).toEqual([2, 2]);
195
+ });
196
+
197
+ it('excludes all-day entries from the time grid', () => {
198
+ const week = buildWeek();
199
+ const layout = getCalendarTimedLayout([
200
+ buildEvent('holiday', '2026-07-22T07:00:00Z', '2026-07-23T07:00:00Z', { allDay: true }),
201
+ ], week.days);
202
+
203
+ expect(layout).toHaveLength(0);
204
+ });
205
+
206
+ it('drops an event entirely outside the range', () => {
207
+ const week = buildWeek();
208
+ const layout = getCalendarTimedLayout([buildEvent('past', '2026-06-01T16:00:00Z', '2026-06-01T18:00:00Z')], week.days);
209
+
210
+ expect(layout).toHaveLength(0);
211
+ });
212
+
213
+ it('places events correctly on a DST fall-back day', () => {
214
+ const range = getCalendarRange({
215
+ view: 'DAY',
216
+ anchorDate: parseCalendarDateParam('2026-11-01', NOW, LA),
217
+ now: NOW,
218
+ timeZone: LA,
219
+ });
220
+ // 9am local on the 25-hour day is 17:00Z (PST, after the transition)
221
+ const layout = getCalendarTimedLayout([buildEvent('a', '2026-11-01T17:00:00Z', '2026-11-01T18:00:00Z')], range.days);
222
+
223
+ // Measured from the day's own start, an extra hour has already elapsed
224
+ expect(layout[0].startMinute).toBe(10 * 60);
225
+ expect(layout[0].endMinute).toBe(11 * 60);
226
+ });
227
+ });
228
+
229
+ describe('getCalendarAllDayLayout', () => {
230
+ it('spans an all-day entry across its day columns', () => {
231
+ const week = buildWeek();
232
+ const bars = getCalendarAllDayLayout([
233
+ buildEvent('conf', '2026-07-21T07:00:00Z', '2026-07-24T07:00:00Z', { allDay: true }),
234
+ ], week.days);
235
+
236
+ expect(bars).toHaveLength(1);
237
+ expect(bars[0].startDayIndex).toBe(2);
238
+ expect(bars[0].endDayIndex).toBe(4);
239
+ expect(bars[0].row).toBe(0);
240
+ });
241
+
242
+ it('packs non-overlapping bars onto the same row', () => {
243
+ const week = buildWeek();
244
+ const bars = getCalendarAllDayLayout([
245
+ buildEvent('a', '2026-07-19T07:00:00Z', '2026-07-21T07:00:00Z', { allDay: true }),
246
+ buildEvent('b', '2026-07-23T07:00:00Z', '2026-07-25T07:00:00Z', { allDay: true }),
247
+ ], week.days);
248
+
249
+ expect(bars.map((bar) => bar.row)).toEqual([0, 0]);
250
+ });
251
+
252
+ it('pushes an overlapping bar to the next row', () => {
253
+ const week = buildWeek();
254
+ const bars = getCalendarAllDayLayout([
255
+ buildEvent('a', '2026-07-19T07:00:00Z', '2026-07-23T07:00:00Z', { allDay: true }),
256
+ buildEvent('b', '2026-07-21T07:00:00Z', '2026-07-25T07:00:00Z', { allDay: true }),
257
+ ], week.days);
258
+
259
+ expect(bars.map((bar) => bar.row).sort()).toEqual([0, 1]);
260
+ });
261
+
262
+ it('promotes a multi-day timed entry into the band', () => {
263
+ const week = buildWeek();
264
+ // A three-day closure is not usefully drawn as three tall columns
265
+ const bars = getCalendarAllDayLayout([
266
+ buildEvent('closure', '2026-07-20T18:00:00Z', '2026-07-23T02:00:00Z', { kind: 'BLOCK' }),
267
+ ], week.days);
268
+
269
+ expect(bars).toHaveLength(1);
270
+ expect(bars[0].startDayIndex).toBe(1);
271
+ expect(bars[0].endDayIndex).toBe(3);
272
+ });
273
+
274
+ it('leaves a single-day timed entry in the grid', () => {
275
+ const week = buildWeek();
276
+ const bars = getCalendarAllDayLayout([buildEvent('shift', '2026-07-22T16:00:00Z', '2026-07-22T20:00:00Z')], week.days);
277
+
278
+ expect(bars).toHaveLength(0);
279
+ });
280
+
281
+ it('clips a bar that starts before or ends after the range', () => {
282
+ const week = buildWeek();
283
+ const bars = getCalendarAllDayLayout([
284
+ buildEvent('long', '2026-07-01T07:00:00Z', '2026-08-01T07:00:00Z', { allDay: true }),
285
+ ], week.days);
286
+
287
+ expect(bars[0].startDayIndex).toBe(0);
288
+ expect(bars[0].endDayIndex).toBe(6);
289
+ expect(bars[0].continuesFromPreviousDay).toBe(true);
290
+ expect(bars[0].continuesToNextDay).toBe(true);
291
+ });
292
+ });
293
+
294
+ describe('getCalendarEventsByDateKey', () => {
295
+ it('lists a multi-day entry under every day it covers', () => {
296
+ const week = buildWeek();
297
+ const byDateKey = getCalendarEventsByDateKey([
298
+ buildEvent('conf', '2026-07-21T07:00:00Z', '2026-07-24T07:00:00Z', { allDay: true }),
299
+ ], week.days);
300
+
301
+ expect(byDateKey.get('2026-07-20')?.map((event) => event.id)).toEqual([]);
302
+ expect(byDateKey.get('2026-07-21')?.map((event) => event.id)).toEqual(['conf']);
303
+ expect(byDateKey.get('2026-07-22')?.map((event) => event.id)).toEqual(['conf']);
304
+ expect(byDateKey.get('2026-07-23')?.map((event) => event.id)).toEqual(['conf']);
305
+ expect(byDateKey.get('2026-07-24')?.map((event) => event.id)).toEqual([]);
306
+ });
307
+
308
+ it('sorts all-day entries above timed ones, then chronologically', () => {
309
+ const week = buildWeek();
310
+ const byDateKey = getCalendarEventsByDateKey([
311
+ buildEvent('late', '2026-07-22T21:00:00Z', '2026-07-22T22:00:00Z'),
312
+ buildEvent('early', '2026-07-22T16:00:00Z', '2026-07-22T17:00:00Z'),
313
+ buildEvent('holiday', '2026-07-22T07:00:00Z', '2026-07-23T07:00:00Z', { allDay: true }),
314
+ ], week.days);
315
+
316
+ expect(byDateKey.get('2026-07-22')?.map((event) => event.id)).toEqual(['holiday', 'early', 'late']);
317
+ });
318
+
319
+ it('gives every day in the range a bucket, empty or not', () => {
320
+ const week = buildWeek();
321
+ const byDateKey = getCalendarEventsByDateKey([], week.days);
322
+
323
+ expect(byDateKey.size).toBe(7);
324
+ expect([...byDateKey.values()].every((events) => events.length === 0)).toBe(true);
325
+ });
326
+ });
327
+
328
+ describe('getCalendarMonthCells', () => {
329
+ it('gives every day in the grid a cell', () => {
330
+ const month = getCalendarRange({
331
+ view: 'MONTH',
332
+ anchorDate: parseCalendarDateParam('2026-07-22', NOW, LA),
333
+ now: NOW,
334
+ timeZone: LA,
335
+ });
336
+ const cells = getCalendarMonthCells([], month.days, 3);
337
+
338
+ expect(cells).toHaveLength(42);
339
+ expect(cells.every((cell) => cell.events.length === 0 && cell.overflowCount === 0)).toBe(true);
340
+ });
341
+
342
+ it('truncates a busy cell and reports the remainder', () => {
343
+ const month = getCalendarRange({
344
+ view: 'MONTH',
345
+ anchorDate: parseCalendarDateParam('2026-07-22', NOW, LA),
346
+ now: NOW,
347
+ timeZone: LA,
348
+ });
349
+ const events = Array.from({ length: 7 }, (_unused, index) =>
350
+ buildEvent(`e${index}`, `2026-07-22T${String(16 + index).padStart(2, '0')}:00:00Z`, `2026-07-22T${String(17 + index).padStart(2, '0')}:00:00Z`));
351
+
352
+ const cell = getCalendarMonthCells(events, month.days, 3).find((entry) => entry.day.dateKey === '2026-07-22');
353
+
354
+ // The count is computed, never left to a CSS overflow to hide
355
+ expect(cell?.events).toHaveLength(3);
356
+ expect(cell?.overflowCount).toBe(4);
357
+ });
358
+
359
+ it('counts a multi-day entry in every cell it covers', () => {
360
+ const month = getCalendarRange({
361
+ view: 'MONTH',
362
+ anchorDate: parseCalendarDateParam('2026-07-22', NOW, LA),
363
+ now: NOW,
364
+ timeZone: LA,
365
+ });
366
+ const cells = getCalendarMonthCells(
367
+ [buildEvent('conf', '2026-07-21T07:00:00Z', '2026-07-24T07:00:00Z', { allDay: true })],
368
+ month.days,
369
+ 3,
370
+ );
371
+
372
+ const covered = cells.filter((cell) => cell.events.length > 0).map((cell) => cell.day.dateKey);
373
+
374
+ expect(covered).toEqual(['2026-07-21', '2026-07-22', '2026-07-23']);
375
+ });
376
+ });
377
+
378
+ describe('year range', () => {
379
+ it('builds every day of the year', () => {
380
+ const year = getCalendarRange({
381
+ view: 'YEAR',
382
+ anchorDate: parseCalendarDateParam('2026-07-22', NOW, LA),
383
+ now: NOW,
384
+ timeZone: LA,
385
+ });
386
+
387
+ expect(year.days).toHaveLength(365);
388
+ expect(year.days[0].dateKey).toBe('2026-01-01');
389
+ expect(year.days[364].dateKey).toBe('2026-12-31');
390
+ });
391
+
392
+ it('handles a leap year', () => {
393
+ const year = getCalendarRange({
394
+ view: 'YEAR',
395
+ anchorDate: parseCalendarDateParam('2028-03-01', NOW, LA),
396
+ now: NOW,
397
+ timeZone: LA,
398
+ });
399
+
400
+ expect(year.days).toHaveLength(366);
401
+ expect(year.days[365].dateKey).toBe('2028-12-31');
402
+ });
403
+ });
@@ -0,0 +1,309 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import {
3
+ getCalendarCreateRange,
4
+ getCalendarMoveRange,
5
+ getCalendarPointerSlot,
6
+ getCalendarResizeRange,
7
+ } from '../src/libs/calendar-gesture.ts';
8
+ import { getCalendarRange, parseCalendarDateParam } from '../src/libs/calendar-range.ts';
9
+
10
+ const LA = 'America/Los_Angeles';
11
+ const NOW = new Date('2026-07-22T19:00:00Z');
12
+
13
+ function buildWeek() {
14
+ return getCalendarRange({
15
+ view: 'WEEK',
16
+ anchorDate: parseCalendarDateParam('2026-07-22', NOW, LA),
17
+ now: NOW,
18
+ timeZone: LA,
19
+ });
20
+ }
21
+
22
+ /*
23
+ * The DST fall-back day, which is 25 hours long.
24
+ */
25
+
26
+ function buildFallBackDay() {
27
+ return getCalendarRange({
28
+ view: 'DAY',
29
+ anchorDate: parseCalendarDateParam('2026-11-01', NOW, LA),
30
+ now: NOW,
31
+ timeZone: LA,
32
+ });
33
+ }
34
+
35
+ describe('getCalendarPointerSlot', () => {
36
+ it('maps a pointer to its day column and snapped minute', () => {
37
+ const week = buildWeek();
38
+ // Fourth of seven columns, 9.5 hours down at 48px/hour
39
+ const slot = getCalendarPointerSlot({
40
+ offsetX: 700 * (3 / 7) + 10,
41
+ offsetY: 9.5 * 48,
42
+ contentWidth: 700,
43
+ hourHeight: 48,
44
+ days: week.days,
45
+ });
46
+
47
+ expect(slot.dayIndex).toBe(3);
48
+ expect(slot.minute).toBe(9 * 60 + 30);
49
+ });
50
+
51
+ it('snaps to the nearest quarter hour', () => {
52
+ const week = buildWeek();
53
+ const slot = getCalendarPointerSlot({
54
+ offsetX: 10,
55
+ // 9h07m — closer to 9:00 than to 9:15
56
+ offsetY: (9 + 7 / 60) * 48,
57
+ contentWidth: 700,
58
+ hourHeight: 48,
59
+ days: week.days,
60
+ });
61
+
62
+ expect(slot.minute).toBe(9 * 60);
63
+ });
64
+
65
+ it('clamps a pointer dragged outside the grid', () => {
66
+ const week = buildWeek();
67
+
68
+ expect(getCalendarPointerSlot({ offsetX: -50, offsetY: -100, contentWidth: 700, hourHeight: 48, days: week.days }))
69
+ .toEqual({ dayIndex: 0, minute: 0 });
70
+ expect(getCalendarPointerSlot({ offsetX: 9999, offsetY: 99999, contentWidth: 700, hourHeight: 48, days: week.days }))
71
+ .toEqual({ dayIndex: 6, minute: 24 * 60 });
72
+ });
73
+ });
74
+
75
+ describe('getCalendarCreateRange', () => {
76
+ it('makes a default-length event from a click that never moved', () => {
77
+ const week = buildWeek();
78
+ const range = getCalendarCreateRange({
79
+ days: week.days,
80
+ anchorDayIndex: 3,
81
+ anchorMinute: 9 * 60,
82
+ currentDayIndex: 3,
83
+ currentMinute: 9 * 60,
84
+ moved: false,
85
+ });
86
+
87
+ // A click must not produce a zero-length event
88
+ expect(range.endAt.getTime() - range.startAt.getTime()).toBe(60 * 60_000);
89
+ expect(range.startAt.toISOString()).toBe('2026-07-22T16:00:00.000Z');
90
+ });
91
+
92
+ it('draws the dragged range', () => {
93
+ const week = buildWeek();
94
+ const range = getCalendarCreateRange({
95
+ days: week.days,
96
+ anchorDayIndex: 3,
97
+ anchorMinute: 9 * 60,
98
+ currentDayIndex: 3,
99
+ currentMinute: 12 * 60,
100
+ moved: true,
101
+ });
102
+
103
+ expect(range.endAt.getTime() - range.startAt.getTime()).toBe(3 * 60 * 60_000);
104
+ });
105
+
106
+ it('accepts a drag drawn upward', () => {
107
+ const week = buildWeek();
108
+ const range = getCalendarCreateRange({
109
+ days: week.days,
110
+ anchorDayIndex: 3,
111
+ anchorMinute: 12 * 60,
112
+ currentDayIndex: 3,
113
+ currentMinute: 9 * 60,
114
+ moved: true,
115
+ });
116
+
117
+ expect(range.startAt.toISOString()).toBe('2026-07-22T16:00:00.000Z');
118
+ expect(range.endAt.getTime() - range.startAt.getTime()).toBe(3 * 60 * 60_000);
119
+ });
120
+
121
+ it('gives a barely-moved drag the minimum length', () => {
122
+ const week = buildWeek();
123
+ const range = getCalendarCreateRange({
124
+ days: week.days,
125
+ anchorDayIndex: 3,
126
+ anchorMinute: 9 * 60,
127
+ currentDayIndex: 3,
128
+ currentMinute: 9 * 60,
129
+ moved: true,
130
+ });
131
+
132
+ expect(range.endAt.getTime() - range.startAt.getTime()).toBe(20 * 60_000);
133
+ });
134
+
135
+ it('stays in the column it began in', () => {
136
+ const week = buildWeek();
137
+ // A sideways wobble while drawing is not a request for a multi-day event
138
+ const range = getCalendarCreateRange({
139
+ days: week.days,
140
+ anchorDayIndex: 3,
141
+ anchorMinute: 9 * 60,
142
+ currentDayIndex: 5,
143
+ currentMinute: 12 * 60,
144
+ moved: true,
145
+ });
146
+
147
+ expect(range.dayIndex).toBe(3);
148
+ expect(range.startAt.toISOString()).toBe('2026-07-22T16:00:00.000Z');
149
+ });
150
+
151
+ it('does not run a click off the end of the day', () => {
152
+ const week = buildWeek();
153
+ const range = getCalendarCreateRange({
154
+ days: week.days,
155
+ anchorDayIndex: 3,
156
+ anchorMinute: 24 * 60,
157
+ currentDayIndex: 3,
158
+ currentMinute: 24 * 60,
159
+ moved: false,
160
+ });
161
+
162
+ expect(range.endAt.getTime()).toBeLessThanOrEqual(week.days[3].endAt.getTime());
163
+ });
164
+ });
165
+
166
+ describe('getCalendarMoveRange', () => {
167
+ it('preserves duration and honours the grab offset', () => {
168
+ const week = buildWeek();
169
+ const startAt = new Date('2026-07-22T16:00:00Z');
170
+ const endAt = new Date('2026-07-22T20:00:00Z');
171
+
172
+ // Grabbed one hour into the block, dropped with the pointer at 14:00
173
+ const range = getCalendarMoveRange({
174
+ days: week.days,
175
+ startAt,
176
+ endAt,
177
+ grabOffsetMinutes: 60,
178
+ currentDayIndex: 3,
179
+ currentMinute: 14 * 60,
180
+ });
181
+
182
+ // The block starts an hour before the pointer, not at it
183
+ expect(range.startAt.toISOString()).toBe('2026-07-22T20:00:00.000Z');
184
+ expect(range.endAt.getTime() - range.startAt.getTime()).toBe(4 * 60 * 60_000);
185
+ });
186
+
187
+ it('moves an event to another day', () => {
188
+ const week = buildWeek();
189
+ const range = getCalendarMoveRange({
190
+ days: week.days,
191
+ startAt: new Date('2026-07-22T16:00:00Z'),
192
+ endAt: new Date('2026-07-22T18:00:00Z'),
193
+ grabOffsetMinutes: 0,
194
+ currentDayIndex: 5,
195
+ currentMinute: 9 * 60,
196
+ });
197
+
198
+ expect(range.dayIndex).toBe(5);
199
+ expect(range.startAt.toISOString()).toBe('2026-07-24T16:00:00.000Z');
200
+ });
201
+
202
+ it('clamps so a drag cannot push an event past midnight', () => {
203
+ const week = buildWeek();
204
+ const range = getCalendarMoveRange({
205
+ days: week.days,
206
+ startAt: new Date('2026-07-22T16:00:00Z'),
207
+ endAt: new Date('2026-07-22T20:00:00Z'),
208
+ grabOffsetMinutes: 0,
209
+ currentDayIndex: 3,
210
+ // Dropped at 23:00 — a 4-hour block would spill into the next day
211
+ currentMinute: 23 * 60,
212
+ });
213
+
214
+ expect(range.endAt.getTime()).toBeLessThanOrEqual(week.days[3].endAt.getTime());
215
+ expect(range.endAt.getTime() - range.startAt.getTime()).toBe(4 * 60 * 60_000);
216
+ });
217
+
218
+ it('keeps a shift the same number of hours across a DST day', () => {
219
+ const day = buildFallBackDay();
220
+ const startAt = new Date('2026-11-01T15:00:00Z');
221
+ const endAt = new Date('2026-11-01T19:00:00Z');
222
+
223
+ const range = getCalendarMoveRange({
224
+ days: day.days,
225
+ startAt,
226
+ endAt,
227
+ grabOffsetMinutes: 0,
228
+ currentDayIndex: 0,
229
+ currentMinute: 10 * 60,
230
+ });
231
+
232
+ // Four hours of actual work stays four hours, even on a 25-hour day
233
+ expect(range.endAt.getTime() - range.startAt.getTime()).toBe(4 * 60 * 60_000);
234
+ });
235
+ });
236
+
237
+ describe('getCalendarResizeRange', () => {
238
+ it('moves the end edge and leaves the start alone', () => {
239
+ const week = buildWeek();
240
+ const startAt = new Date('2026-07-22T16:00:00Z');
241
+ const endAt = new Date('2026-07-22T18:00:00Z');
242
+
243
+ const range = getCalendarResizeRange({
244
+ days: week.days,
245
+ dayIndex: 3,
246
+ startAt,
247
+ endAt,
248
+ edge: 'END',
249
+ currentMinute: 15 * 60,
250
+ });
251
+
252
+ expect(range.startAt.toISOString()).toBe(startAt.toISOString());
253
+ expect(range.endAt.toISOString()).toBe('2026-07-22T22:00:00.000Z');
254
+ });
255
+
256
+ it('moves the start edge and leaves the end alone', () => {
257
+ const week = buildWeek();
258
+ const startAt = new Date('2026-07-22T16:00:00Z');
259
+ const endAt = new Date('2026-07-22T18:00:00Z');
260
+
261
+ const range = getCalendarResizeRange({
262
+ days: week.days,
263
+ dayIndex: 3,
264
+ startAt,
265
+ endAt,
266
+ edge: 'START',
267
+ currentMinute: 8 * 60,
268
+ });
269
+
270
+ expect(range.startAt.toISOString()).toBe('2026-07-22T15:00:00.000Z');
271
+ expect(range.endAt.toISOString()).toBe(endAt.toISOString());
272
+ });
273
+
274
+ it('refuses to invert the event', () => {
275
+ const week = buildWeek();
276
+ const startAt = new Date('2026-07-22T16:00:00Z');
277
+ const endAt = new Date('2026-07-22T18:00:00Z');
278
+
279
+ // Dragging the end edge above the start
280
+ const range = getCalendarResizeRange({
281
+ days: week.days,
282
+ dayIndex: 3,
283
+ startAt,
284
+ endAt,
285
+ edge: 'END',
286
+ currentMinute: 2 * 60,
287
+ });
288
+
289
+ expect(range.endAt.getTime()).toBeGreaterThan(range.startAt.getTime());
290
+ expect(range.endAt.getTime() - range.startAt.getTime()).toBe(20 * 60_000);
291
+ });
292
+
293
+ it('refuses to collapse the event from the start edge', () => {
294
+ const week = buildWeek();
295
+ const startAt = new Date('2026-07-22T16:00:00Z');
296
+ const endAt = new Date('2026-07-22T18:00:00Z');
297
+
298
+ const range = getCalendarResizeRange({
299
+ days: week.days,
300
+ dayIndex: 3,
301
+ startAt,
302
+ endAt,
303
+ edge: 'START',
304
+ currentMinute: 20 * 60,
305
+ });
306
+
307
+ expect(range.endAt.getTime() - range.startAt.getTime()).toBe(20 * 60_000);
308
+ });
309
+ });