@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,232 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import {
3
+ getCalendarDateKey,
4
+ getCalendarRange,
5
+ getCalendarRangeTitle,
6
+ getCalendarStepAnchor,
7
+ getDayLengthMinutes,
8
+ getInstantFromDayMinutes,
9
+ getMinutesFromDayStart,
10
+ parseCalendarDateParam,
11
+ snapMinutes,
12
+ } from '../src/libs/calendar-range.ts';
13
+
14
+ const LA = 'America/Los_Angeles';
15
+ // A Wednesday
16
+ const NOW = new Date('2026-07-22T19:00:00Z');
17
+
18
+ describe('parseCalendarDateParam', () => {
19
+ it('parses a date param to local midnight in the calendar zone', () => {
20
+ const parsed = parseCalendarDateParam('2026-07-20', NOW, LA);
21
+
22
+ // 2026-07-20T00:00 in Los Angeles is 07:00Z during daylight time
23
+ expect(parsed.toISOString()).toBe('2026-07-20T07:00:00.000Z');
24
+ });
25
+
26
+ it('falls back to today rather than to the epoch', () => {
27
+ const parsed = parseCalendarDateParam('not-a-date', NOW, LA);
28
+
29
+ expect(getCalendarDateKey(parsed, LA)).toBe('2026-07-22');
30
+ });
31
+
32
+ it('resolves "today" in the calendar zone, not in UTC', () => {
33
+ // 2026-07-23T02:00Z is still the 22nd in Los Angeles
34
+ const lateNight = new Date('2026-07-23T02:00:00Z');
35
+
36
+ expect(getCalendarDateKey(parseCalendarDateParam(null, lateNight, LA), LA)).toBe('2026-07-22');
37
+ expect(getCalendarDateKey(parseCalendarDateParam(null, lateNight, 'UTC'), 'UTC')).toBe('2026-07-23');
38
+ });
39
+ });
40
+
41
+ describe('getCalendarRange', () => {
42
+ it('builds a Sunday-start week covering the anchor', () => {
43
+ const range = getCalendarRange({ view: 'WEEK', anchorDate: parseCalendarDateParam('2026-07-22', NOW, LA), now: NOW, timeZone: LA });
44
+
45
+ expect(range.days).toHaveLength(7);
46
+ expect(range.days[0].dateKey).toBe('2026-07-19');
47
+ expect(range.days[6].dateKey).toBe('2026-07-25');
48
+ expect(range.from.toISOString()).toBe('2026-07-19T07:00:00.000Z');
49
+ expect(range.to.toISOString()).toBe('2026-07-26T07:00:00.000Z');
50
+ });
51
+
52
+ it('honours a Monday week start', () => {
53
+ const range = getCalendarRange({
54
+ view: 'WEEK',
55
+ anchorDate: parseCalendarDateParam('2026-07-22', NOW, LA),
56
+ now: NOW,
57
+ timeZone: LA,
58
+ weekStartsOn: 1,
59
+ });
60
+
61
+ expect(range.days[0].dateKey).toBe('2026-07-20');
62
+ expect(range.days[6].dateKey).toBe('2026-07-26');
63
+ });
64
+
65
+ it('runs DAY and FOUR_DAYS forward from the anchor', () => {
66
+ const anchor = parseCalendarDateParam('2026-07-22', NOW, LA);
67
+
68
+ expect(getCalendarRange({ view: 'DAY', anchorDate: anchor, now: NOW, timeZone: LA }).days.map((d) => d.dateKey))
69
+ .toEqual(['2026-07-22']);
70
+ expect(getCalendarRange({ view: 'FOUR_DAYS', anchorDate: anchor, now: NOW, timeZone: LA }).days.map((d) => d.dateKey))
71
+ .toEqual(['2026-07-22', '2026-07-23', '2026-07-24', '2026-07-25']);
72
+ });
73
+
74
+ it('always builds a six-week month grid, including neighbouring days', () => {
75
+ const range = getCalendarRange({ view: 'MONTH', anchorDate: parseCalendarDateParam('2026-07-22', NOW, LA), now: NOW, timeZone: LA });
76
+
77
+ expect(range.days).toHaveLength(42);
78
+ // July 2026 starts on a Wednesday, so the grid opens on Sunday June 28
79
+ expect(range.days[0].dateKey).toBe('2026-06-28');
80
+ expect(range.days[0].isInPrimaryRange).toBe(false);
81
+ expect(range.days.find((day) => day.dateKey === '2026-07-01')?.isInPrimaryRange).toBe(true);
82
+ expect(range.days[41].isInPrimaryRange).toBe(false);
83
+ });
84
+
85
+ it('keeps the month grid six weeks tall even when five would fit', () => {
86
+ // February 2027 starts on a Monday and has 28 days: exactly four weeks
87
+ // from Monday, which a naive grid would render as five rows and then
88
+ // four, making every event jump between months
89
+ const range = getCalendarRange({
90
+ view: 'MONTH',
91
+ anchorDate: parseCalendarDateParam('2027-02-15', NOW, LA),
92
+ now: NOW,
93
+ timeZone: LA,
94
+ weekStartsOn: 1,
95
+ });
96
+
97
+ expect(range.days).toHaveLength(42);
98
+ expect(range.days[0].dateKey).toBe('2027-02-01');
99
+ });
100
+
101
+ it('marks today and weekends in the calendar zone', () => {
102
+ const range = getCalendarRange({ view: 'WEEK', anchorDate: parseCalendarDateParam('2026-07-22', NOW, LA), now: NOW, timeZone: LA });
103
+
104
+ expect(range.days.filter((day) => day.isToday).map((day) => day.dateKey)).toEqual(['2026-07-22']);
105
+ expect(range.days.filter((day) => day.isWeekend).map((day) => day.dateKey)).toEqual(['2026-07-19', '2026-07-25']);
106
+ });
107
+
108
+ it('falls back to a valid zone rather than throwing', () => {
109
+ const range = getCalendarRange({ view: 'DAY', anchorDate: NOW, now: NOW, timeZone: 'Not/AZone' });
110
+
111
+ expect(range.days).toHaveLength(1);
112
+ expect(range.timeZone).toBeTruthy();
113
+ });
114
+ });
115
+
116
+ describe('DST handling', () => {
117
+ it('measures a fall-back day as 25 hours', () => {
118
+ // 2026-11-01 is the US fall-back day
119
+ const range = getCalendarRange({
120
+ view: 'DAY',
121
+ anchorDate: parseCalendarDateParam('2026-11-01', NOW, LA),
122
+ now: NOW,
123
+ timeZone: LA,
124
+ });
125
+
126
+ expect(getDayLengthMinutes(range.days[0])).toBe(25 * 60);
127
+ });
128
+
129
+ it('measures a spring-forward day as 23 hours', () => {
130
+ // 2026-03-08 is the US spring-forward day
131
+ const range = getCalendarRange({
132
+ view: 'DAY',
133
+ anchorDate: parseCalendarDateParam('2026-03-08', NOW, LA),
134
+ now: NOW,
135
+ timeZone: LA,
136
+ });
137
+
138
+ expect(getDayLengthMinutes(range.days[0])).toBe(23 * 60);
139
+ });
140
+
141
+ it('keeps a week containing a transition seven days long', () => {
142
+ const range = getCalendarRange({
143
+ view: 'WEEK',
144
+ anchorDate: parseCalendarDateParam('2026-11-01', NOW, LA),
145
+ now: NOW,
146
+ timeZone: LA,
147
+ });
148
+
149
+ expect(range.days).toHaveLength(7);
150
+ // 25 hours in the first day, 24 in the rest
151
+ expect(range.to.getTime() - range.from.getTime()).toBe((7 * 24 + 1) * 3_600_000);
152
+ });
153
+ });
154
+
155
+ describe('getCalendarStepAnchor', () => {
156
+ it('steps a month view by calendar months, clamping short months', () => {
157
+ const anchor = parseCalendarDateParam('2026-01-31', NOW, LA);
158
+ const next = getCalendarStepAnchor({ view: 'MONTH', anchorDate: anchor, direction: 1, timeZone: LA });
159
+
160
+ // Stepping from January 31 must land in February, not skip to March
161
+ expect(getCalendarDateKey(next, LA)).toBe('2026-02-28');
162
+ });
163
+
164
+ it('steps week, day and four-day views by their own spans', () => {
165
+ const anchor = parseCalendarDateParam('2026-07-22', NOW, LA);
166
+
167
+ expect(getCalendarDateKey(getCalendarStepAnchor({ view: 'WEEK', anchorDate: anchor, direction: 1, timeZone: LA }), LA))
168
+ .toBe('2026-07-29');
169
+ expect(getCalendarDateKey(getCalendarStepAnchor({ view: 'DAY', anchorDate: anchor, direction: -1, timeZone: LA }), LA))
170
+ .toBe('2026-07-21');
171
+ expect(getCalendarDateKey(getCalendarStepAnchor({ view: 'FOUR_DAYS', anchorDate: anchor, direction: 1, timeZone: LA }), LA))
172
+ .toBe('2026-07-26');
173
+ });
174
+
175
+ it('steps a week across a DST boundary without drifting an hour', () => {
176
+ const anchor = parseCalendarDateParam('2026-10-28', NOW, LA);
177
+ const next = getCalendarStepAnchor({ view: 'WEEK', anchorDate: anchor, direction: 1, timeZone: LA });
178
+
179
+ // A naive +7*24h would land at 23:00 on the 3rd, which reads as the wrong day
180
+ expect(getCalendarDateKey(next, LA)).toBe('2026-11-04');
181
+ expect(next.toISOString()).toBe('2026-11-04T08:00:00.000Z');
182
+ });
183
+ });
184
+
185
+ describe('minute helpers', () => {
186
+ it('round-trips minutes through a day start', () => {
187
+ const range = getCalendarRange({ view: 'DAY', anchorDate: parseCalendarDateParam('2026-07-22', NOW, LA), now: NOW, timeZone: LA });
188
+ const dayStart = range.days[0].startAt;
189
+ const instant = getInstantFromDayMinutes(dayStart, 9 * 60 + 30);
190
+
191
+ expect(getMinutesFromDayStart(instant, dayStart)).toBe(9 * 60 + 30);
192
+ expect(instant.toISOString()).toBe('2026-07-22T16:30:00.000Z');
193
+ });
194
+
195
+ it('snaps to the nearest increment and clamps into the day', () => {
196
+ expect(snapMinutes(97, 15, 1440)).toBe(90);
197
+ expect(snapMinutes(103, 15, 1440)).toBe(105);
198
+ expect(snapMinutes(-20, 15, 1440)).toBe(0);
199
+ expect(snapMinutes(2000, 15, 1440)).toBe(1440);
200
+ });
201
+ });
202
+
203
+ describe('getCalendarRangeTitle', () => {
204
+ const anchorFor = (dateKey: string) => parseCalendarDateParam(dateKey, NOW, LA);
205
+
206
+ it('titles a week inside one month', () => {
207
+ const range = getCalendarRange({ view: 'WEEK', anchorDate: anchorFor('2026-07-22'), now: NOW, timeZone: LA });
208
+
209
+ expect(getCalendarRangeTitle(range)).toBe('Jul 19 – 25, 2026');
210
+ });
211
+
212
+ it('titles a week spanning two months', () => {
213
+ const range = getCalendarRange({ view: 'WEEK', anchorDate: anchorFor('2026-07-30'), now: NOW, timeZone: LA });
214
+
215
+ expect(getCalendarRangeTitle(range)).toBe('Jul 26 – Aug 1, 2026');
216
+ });
217
+
218
+ it('titles a week spanning two years', () => {
219
+ const range = getCalendarRange({ view: 'WEEK', anchorDate: anchorFor('2026-12-31'), now: NOW, timeZone: LA });
220
+
221
+ expect(getCalendarRangeTitle(range)).toBe('Dec 27, 2026 – Jan 2, 2027');
222
+ });
223
+
224
+ it('titles month, year and day views', () => {
225
+ expect(getCalendarRangeTitle(getCalendarRange({ view: 'MONTH', anchorDate: anchorFor('2026-07-22'), now: NOW, timeZone: LA })))
226
+ .toBe('July 2026');
227
+ expect(getCalendarRangeTitle(getCalendarRange({ view: 'YEAR', anchorDate: anchorFor('2026-07-22'), now: NOW, timeZone: LA })))
228
+ .toBe('2026');
229
+ expect(getCalendarRangeTitle(getCalendarRange({ view: 'DAY', anchorDate: anchorFor('2026-07-22'), now: NOW, timeZone: LA })))
230
+ .toBe('Wednesday, July 22, 2026');
231
+ });
232
+ });
@@ -0,0 +1,124 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { getCalendarDateKey } from '../src/libs/calendar-range.ts';
3
+ import {
4
+ getCalendarViewSlug,
5
+ parseCalendarViewSlug,
6
+ readCalendarUrlState,
7
+ writeCalendarUrlState,
8
+ } from '../src/libs/calendar-url.ts';
9
+
10
+ const LA = 'America/Los_Angeles';
11
+ // A Wednesday
12
+ const NOW = new Date('2026-07-22T19:00:00Z');
13
+
14
+ /*
15
+ * Render the query string a write produces, for readable assertions.
16
+ */
17
+
18
+ function write(current: string, next: Parameters<typeof writeCalendarUrlState>[1]) {
19
+ return writeCalendarUrlState(new URLSearchParams(current), next, NOW, LA).toString();
20
+ }
21
+
22
+ describe('view slugs', () => {
23
+ it('spells views the way a person would read them', () => {
24
+ expect(getCalendarViewSlug('FOUR_DAYS')).toBe('4day');
25
+ expect(getCalendarViewSlug('WEEK')).toBe('week');
26
+ expect(getCalendarViewSlug('AGENDA')).toBe('agenda');
27
+ });
28
+
29
+ it('round-trips every slug', () => {
30
+ for (const view of ['DAY', 'FOUR_DAYS', 'WEEK', 'MONTH', 'YEAR', 'AGENDA'] as const) {
31
+ expect(parseCalendarViewSlug(getCalendarViewSlug(view))).toBe(view);
32
+ }
33
+ });
34
+
35
+ it('still accepts the enum spelling from an older link', () => {
36
+ expect(parseCalendarViewSlug('FOUR_DAYS')).toBe('FOUR_DAYS');
37
+ expect(parseCalendarViewSlug('month')).toBe('MONTH');
38
+ });
39
+
40
+ it('falls back to the week rather than throwing', () => {
41
+ expect(parseCalendarViewSlug('nonsense')).toBe('WEEK');
42
+ expect(parseCalendarViewSlug(null)).toBe('WEEK');
43
+ });
44
+ });
45
+
46
+ describe('readCalendarUrlState', () => {
47
+ it('reads a bare URL as this week, everything', () => {
48
+ const state = readCalendarUrlState(new URLSearchParams(''), NOW, LA);
49
+
50
+ expect(state.view).toBe('WEEK');
51
+ expect(getCalendarDateKey(state.anchorDate, LA)).toBe('2026-07-22');
52
+ expect(state.mineOnly).toBe(false);
53
+ expect(state.hiddenCalendarIds).toEqual([]);
54
+ });
55
+
56
+ it('reads a full URL', () => {
57
+ const state = readCalendarUrlState(new URLSearchParams('v=month&d=2026-03-01&me=1&hide=a,b'), NOW, LA);
58
+
59
+ expect(state.view).toBe('MONTH');
60
+ expect(getCalendarDateKey(state.anchorDate, LA)).toBe('2026-03-01');
61
+ expect(state.mineOnly).toBe(true);
62
+ expect(state.hiddenCalendarIds).toEqual(['a', 'b']);
63
+ });
64
+
65
+ it('ignores blank entries in the calendar list', () => {
66
+ const state = readCalendarUrlState(new URLSearchParams('hide=a,,b,'), NOW, LA);
67
+
68
+ expect(state.hiddenCalendarIds).toEqual(['a', 'b']);
69
+ });
70
+ });
71
+
72
+ describe('writeCalendarUrlState', () => {
73
+ it('writes nothing at all for the defaults', () => {
74
+ // The whole point: an untouched calendar has a clean URL
75
+ expect(write('', { view: 'WEEK', anchorDate: NOW, mineOnly: false, hiddenCalendarIds: [] })).toBe('');
76
+ });
77
+
78
+ it('writes only what differs from the default', () => {
79
+ expect(write('', { view: 'MONTH' })).toBe('v=month');
80
+ expect(write('', { mineOnly: true })).toBe('me=1');
81
+ expect(write('', { hiddenCalendarIds: ['a', 'b'] })).toBe('hide=a%2Cb');
82
+ });
83
+
84
+ it('omits the date when it is today', () => {
85
+ expect(write('', { anchorDate: new Date('2026-07-22T23:00:00Z') })).toBe('');
86
+ });
87
+
88
+ it('writes the date when it is not today', () => {
89
+ expect(write('', { anchorDate: new Date('2026-08-01T12:00:00Z') })).toBe('d=2026-08-01');
90
+ });
91
+
92
+ it('resolves "today" in the calendar zone, not in UTC', () => {
93
+ // 2026-07-23T02:00Z is still the 22nd in Los Angeles, so it is today
94
+ // and should produce no parameter at all
95
+ expect(write('', { anchorDate: new Date('2026-07-23T02:00:00Z') })).toBe('');
96
+ });
97
+
98
+ it('clears a parameter when it returns to the default', () => {
99
+ expect(write('v=month', { view: 'WEEK' })).toBe('');
100
+ expect(write('me=1', { mineOnly: false })).toBe('');
101
+ expect(write('hide=a', { hiddenCalendarIds: [] })).toBe('');
102
+ expect(write('d=2026-08-01', { anchorDate: NOW })).toBe('');
103
+ });
104
+
105
+ it('leaves untouched keys alone', () => {
106
+ // The calendar does not own the whole query string
107
+ expect(write('v=month', { mineOnly: true })).toBe('v=month&me=1');
108
+ });
109
+
110
+ it('preserves an unrelated parameter somebody else put there', () => {
111
+ const result = write('ref=email&v=month', { view: 'WEEK' });
112
+
113
+ expect(result).toBe('ref=email');
114
+ });
115
+
116
+ it('changes nothing for keys it was not asked about', () => {
117
+ expect(write('v=month&me=1&d=2026-08-01', {})).toBe('v=month&me=1&d=2026-08-01');
118
+ });
119
+
120
+ it('produces a readable full URL', () => {
121
+ expect(write('', { view: 'MONTH', anchorDate: new Date('2026-08-01T12:00:00Z'), mineOnly: true }))
122
+ .toBe('v=month&d=2026-08-01&me=1');
123
+ });
124
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "module": "ESNext",
5
+ "moduleResolution": "Bundler",
6
+ "allowImportingTsExtensions": true,
7
+ "jsx": "react-jsx",
8
+ "noEmit": true,
9
+ "strict": true,
10
+ "esModuleInterop": true
11
+ },
12
+ "include": ["src/**/*"]
13
+ }
@@ -0,0 +1,11 @@
1
+ import { defineConfig } from 'vitest/config';
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ environment: 'node',
6
+ // Rendering tests live in apps/marketday/tests instead: vitest's jsdom
7
+ // environment cannot resolve modules from a directory whose name
8
+ // contains a colon, and this package is "@jsb188:calendar"
9
+ include: ['tests/**/*.test.ts?(x)'],
10
+ },
11
+ });