@n24q02m/workspace 0.9.0-beta.1 → 0.9.1-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NOTICE +11 -5
- package/bin/cli.mjs +52 -52
- package/build/src/cli/tools.integration.test.js +26 -5
- package/build/src/cli/tools.integration.test.js.map +1 -1
- package/build/src/docs/docs.md +5 -1
- package/build/src/docs/overview.md +14 -0
- package/build/src/services/FormsService.d.ts +4 -4
- package/build/src/services/FormsService.js +4 -4
- package/build/src/services/FormsService.test.js +3 -3
- package/build/src/services/FormsService.test.js.map +1 -1
- package/build/src/tools/domains/error-payload.test.d.ts +2 -0
- package/build/src/tools/domains/error-payload.test.d.ts.map +1 -0
- package/build/src/tools/domains/error-payload.test.js +94 -0
- package/build/src/tools/domains/error-payload.test.js.map +1 -0
- package/build/src/tools/domains/factory.d.ts.map +1 -1
- package/build/src/tools/domains/factory.js +27 -1
- package/build/src/tools/domains/factory.js.map +1 -1
- package/build/src/tools/domains/factory.test.js +41 -0
- package/build/src/tools/domains/factory.test.js.map +1 -1
- package/build/src/vendored/services/CalendarService.d.ts.map +1 -1
- package/build/src/vendored/services/CalendarService.js +19 -15
- package/build/src/vendored/services/CalendarService.js.map +1 -1
- package/build/src/vendored/services/CalendarService.test.d.ts +2 -0
- package/build/src/vendored/services/CalendarService.test.d.ts.map +1 -0
- package/build/src/vendored/services/CalendarService.test.js +1227 -0
- package/build/src/vendored/services/CalendarService.test.js.map +1 -0
- package/build/src/vendored/services/CalendarValidation.test.d.ts +2 -0
- package/build/src/vendored/services/CalendarValidation.test.d.ts.map +1 -0
- package/build/src/vendored/services/CalendarValidation.test.js +312 -0
- package/build/src/vendored/services/CalendarValidation.test.js.map +1 -0
- package/build/src/vendored/services/ChatService.test.d.ts +2 -0
- package/build/src/vendored/services/ChatService.test.d.ts.map +1 -0
- package/build/src/vendored/services/ChatService.test.js +505 -0
- package/build/src/vendored/services/ChatService.test.js.map +1 -0
- package/build/src/vendored/services/DocsService.d.ts.map +1 -1
- package/build/src/vendored/services/DocsService.js +6 -1
- package/build/src/vendored/services/DocsService.js.map +1 -1
- package/build/src/vendored/services/DocsService.test.js +943 -5
- package/build/src/vendored/services/DocsService.test.js.map +1 -1
- package/build/src/vendored/services/DriveService.test.js +797 -9
- package/build/src/vendored/services/DriveService.test.js.map +1 -1
- package/build/src/vendored/services/SheetsService.test.js +328 -0
- package/build/src/vendored/services/SheetsService.test.js.map +1 -1
- package/build/src/vendored/utils/MimeHelper.test.js +329 -0
- package/build/src/vendored/utils/MimeHelper.test.js.map +1 -1
- package/build/src/vendored/utils/validation.d.ts.map +1 -1
- package/build/src/vendored/utils/validation.js +10 -1
- package/build/src/vendored/utils/validation.js.map +1 -1
- package/build/src/vendored/utils/validation.test.d.ts +2 -0
- package/build/src/vendored/utils/validation.test.d.ts.map +1 -0
- package/build/src/vendored/utils/validation.test.js +260 -0
- package/build/src/vendored/utils/validation.test.js.map +1 -0
- package/build/tsconfig.tsbuildinfo +1 -1
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
|
@@ -0,0 +1,1227 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
const h = vi.hoisted(() => ({
|
|
5
|
+
authClient: { tag: 'oauth-client' },
|
|
6
|
+
getAuthenticatedClient: vi.fn(),
|
|
7
|
+
calendarFactory: vi.fn(),
|
|
8
|
+
calendarApi: {
|
|
9
|
+
calendarList: { list: vi.fn() },
|
|
10
|
+
events: { insert: vi.fn(), list: vi.fn(), get: vi.fn(), delete: vi.fn(), patch: vi.fn() },
|
|
11
|
+
freebusy: { query: vi.fn() }
|
|
12
|
+
}
|
|
13
|
+
}));
|
|
14
|
+
// Mock the auth shim so no real credentials are needed.
|
|
15
|
+
vi.mock('../../auth/credential-state.js', () => ({
|
|
16
|
+
getAuth: () => ({ getAuthenticatedClient: h.getAuthenticatedClient })
|
|
17
|
+
}));
|
|
18
|
+
// Intercept every Calendar API call the service makes.
|
|
19
|
+
vi.mock('googleapis', () => ({ google: { calendar: h.calendarFactory } }));
|
|
20
|
+
// Real validation by default; individual tests make it throw a non-Zod error.
|
|
21
|
+
vi.mock('./CalendarValidation.js', async (importOriginal) => {
|
|
22
|
+
const actual = await importOriginal();
|
|
23
|
+
return {
|
|
24
|
+
...actual,
|
|
25
|
+
validateCreateEventInput: vi.fn(actual.validateCreateEventInput),
|
|
26
|
+
validateUpdateEventInput: vi.fn(actual.validateUpdateEventInput)
|
|
27
|
+
};
|
|
28
|
+
});
|
|
29
|
+
import { AuthManager } from '../auth/AuthManager.js';
|
|
30
|
+
import { CalendarService } from './CalendarService.js';
|
|
31
|
+
import { validateCreateEventInput, validateUpdateEventInput } from './CalendarValidation.js';
|
|
32
|
+
const api = h.calendarApi;
|
|
33
|
+
const newService = () => new CalendarService(new AuthManager(['scope']));
|
|
34
|
+
const body = (res) => JSON.parse(res.content[0].text);
|
|
35
|
+
const googleError = (error) => Object.assign(new Error('Request failed with status code 400'), { response: { data: { error } } });
|
|
36
|
+
// The account's primary calendar, returned by calendarList.list unless a test overrides it.
|
|
37
|
+
const PRIMARY = 'me@x.com';
|
|
38
|
+
const START = { dateTime: '2026-03-02T09:00:00Z' };
|
|
39
|
+
const END = { dateTime: '2026-03-02T10:00:00Z' };
|
|
40
|
+
beforeEach(() => {
|
|
41
|
+
h.getAuthenticatedClient.mockReset().mockResolvedValue(h.authClient);
|
|
42
|
+
h.calendarFactory.mockReset().mockReturnValue(api);
|
|
43
|
+
for (const fn of [
|
|
44
|
+
api.calendarList.list,
|
|
45
|
+
api.events.insert,
|
|
46
|
+
api.events.list,
|
|
47
|
+
api.events.get,
|
|
48
|
+
api.events.delete,
|
|
49
|
+
api.events.patch,
|
|
50
|
+
api.freebusy.query
|
|
51
|
+
]) {
|
|
52
|
+
fn.mockReset();
|
|
53
|
+
}
|
|
54
|
+
api.calendarList.list.mockResolvedValue({ data: { items: [{ id: PRIMARY, primary: true }] } });
|
|
55
|
+
vi.mocked(validateCreateEventInput).mockClear();
|
|
56
|
+
vi.mocked(validateUpdateEventInput).mockClear();
|
|
57
|
+
});
|
|
58
|
+
afterEach(() => {
|
|
59
|
+
vi.useRealTimers();
|
|
60
|
+
vi.restoreAllMocks();
|
|
61
|
+
});
|
|
62
|
+
describe('CalendarService client wiring', () => {
|
|
63
|
+
it('creates the calendar v3 client with the shared gaxios options and the authenticated OAuth client', async () => {
|
|
64
|
+
api.calendarList.list.mockResolvedValue({ data: { items: [] } });
|
|
65
|
+
await newService().listCalendars();
|
|
66
|
+
expect(h.calendarFactory).toHaveBeenCalledWith(expect.objectContaining({ version: 'v3', auth: h.authClient, timeout: 30000 }));
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
describe('CalendarService.listCalendars', () => {
|
|
70
|
+
it('returns only id and summary of each calendar', async () => {
|
|
71
|
+
api.calendarList.list.mockResolvedValue({
|
|
72
|
+
data: {
|
|
73
|
+
items: [
|
|
74
|
+
{ id: 'a@x.com', summary: 'Work', timeZone: 'UTC', primary: true },
|
|
75
|
+
{ id: 'b@x.com', summary: 'Home', accessRole: 'owner' }
|
|
76
|
+
]
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
const res = await newService().listCalendars();
|
|
80
|
+
expect(body(res)).toEqual([
|
|
81
|
+
{ id: 'a@x.com', summary: 'Work' },
|
|
82
|
+
{ id: 'b@x.com', summary: 'Home' }
|
|
83
|
+
]);
|
|
84
|
+
});
|
|
85
|
+
it('returns an empty list when the account has no calendars', async () => {
|
|
86
|
+
api.calendarList.list.mockResolvedValue({ data: {} });
|
|
87
|
+
expect(body(await newService().listCalendars())).toEqual([]);
|
|
88
|
+
});
|
|
89
|
+
it('reports the message of an Error thrown by the API', async () => {
|
|
90
|
+
api.calendarList.list.mockRejectedValue(new Error('quota exceeded'));
|
|
91
|
+
expect(body(await newService().listCalendars())).toEqual({ error: 'quota exceeded' });
|
|
92
|
+
});
|
|
93
|
+
it('stringifies a non-Error rejection', async () => {
|
|
94
|
+
api.calendarList.list.mockRejectedValue('plain failure');
|
|
95
|
+
expect(body(await newService().listCalendars())).toEqual({ error: 'plain failure' });
|
|
96
|
+
});
|
|
97
|
+
it('reports an authentication failure instead of throwing', async () => {
|
|
98
|
+
h.getAuthenticatedClient.mockRejectedValue(new Error('not authenticated'));
|
|
99
|
+
expect(body(await newService().listCalendars())).toEqual({ error: 'not authenticated' });
|
|
100
|
+
expect(api.calendarList.list).not.toHaveBeenCalled();
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
describe('CalendarService primary calendar resolution', () => {
|
|
104
|
+
it('uses the primary calendar id and looks it up only once', async () => {
|
|
105
|
+
api.calendarList.list.mockResolvedValue({
|
|
106
|
+
data: { items: [{ id: 'other@x.com' }, { id: 'me@x.com', primary: true }] }
|
|
107
|
+
});
|
|
108
|
+
api.events.get.mockResolvedValue({ data: { id: 'ev1' } });
|
|
109
|
+
const svc = newService();
|
|
110
|
+
await svc.getEvent({ eventId: 'ev1' });
|
|
111
|
+
await svc.getEvent({ eventId: 'ev2' });
|
|
112
|
+
expect(api.calendarList.list).toHaveBeenCalledTimes(1);
|
|
113
|
+
expect(api.events.get).toHaveBeenNthCalledWith(1, { calendarId: 'me@x.com', eventId: 'ev1' });
|
|
114
|
+
expect(api.events.get).toHaveBeenNthCalledWith(2, { calendarId: 'me@x.com', eventId: 'ev2' });
|
|
115
|
+
});
|
|
116
|
+
it('falls back to "primary" and retries the lookup next time when no calendar is flagged primary', async () => {
|
|
117
|
+
api.calendarList.list.mockResolvedValue({ data: { items: [{ id: 'other@x.com' }] } });
|
|
118
|
+
api.events.get.mockResolvedValue({ data: {} });
|
|
119
|
+
const svc = newService();
|
|
120
|
+
await svc.getEvent({ eventId: 'ev1' });
|
|
121
|
+
await svc.getEvent({ eventId: 'ev2' });
|
|
122
|
+
expect(api.events.get).toHaveBeenNthCalledWith(1, { calendarId: 'primary', eventId: 'ev1' });
|
|
123
|
+
expect(api.events.get).toHaveBeenNthCalledWith(2, { calendarId: 'primary', eventId: 'ev2' });
|
|
124
|
+
expect(api.calendarList.list).toHaveBeenCalledTimes(2);
|
|
125
|
+
});
|
|
126
|
+
it('falls back to "primary" when the primary calendar has no id', async () => {
|
|
127
|
+
api.calendarList.list.mockResolvedValue({ data: { items: [{ primary: true }] } });
|
|
128
|
+
api.events.get.mockResolvedValue({ data: {} });
|
|
129
|
+
await newService().getEvent({ eventId: 'ev1' });
|
|
130
|
+
expect(api.events.get).toHaveBeenCalledWith({ calendarId: 'primary', eventId: 'ev1' });
|
|
131
|
+
});
|
|
132
|
+
it('falls back to "primary" when the calendar list has no items', async () => {
|
|
133
|
+
api.calendarList.list.mockResolvedValue({ data: {} });
|
|
134
|
+
api.events.get.mockResolvedValue({ data: {} });
|
|
135
|
+
await newService().getEvent({ eventId: 'ev1' });
|
|
136
|
+
expect(api.events.get).toHaveBeenCalledWith({ calendarId: 'primary', eventId: 'ev1' });
|
|
137
|
+
});
|
|
138
|
+
it('skips the lookup when the caller names a calendar', async () => {
|
|
139
|
+
api.events.get.mockResolvedValue({ data: {} });
|
|
140
|
+
await newService().getEvent({ eventId: 'ev1', calendarId: 'team@x.com' });
|
|
141
|
+
expect(api.calendarList.list).not.toHaveBeenCalled();
|
|
142
|
+
expect(api.events.get).toHaveBeenCalledWith({ calendarId: 'team@x.com', eventId: 'ev1' });
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
describe('CalendarService.createEvent', () => {
|
|
146
|
+
it('inserts a regular event and returns the created resource', async () => {
|
|
147
|
+
api.events.insert.mockResolvedValue({ data: { id: 'new1', htmlLink: 'https://cal/new1' } });
|
|
148
|
+
const res = await newService().createEvent({
|
|
149
|
+
calendarId: 'cal-1',
|
|
150
|
+
summary: 'Standup',
|
|
151
|
+
description: 'Daily sync',
|
|
152
|
+
start: START,
|
|
153
|
+
end: END,
|
|
154
|
+
attendees: ['a@x.com', 'b@x.com']
|
|
155
|
+
});
|
|
156
|
+
expect(api.events.insert).toHaveBeenCalledWith({
|
|
157
|
+
calendarId: 'cal-1',
|
|
158
|
+
sendUpdates: 'all',
|
|
159
|
+
requestBody: {
|
|
160
|
+
summary: 'Standup',
|
|
161
|
+
description: 'Daily sync',
|
|
162
|
+
start: START,
|
|
163
|
+
end: END,
|
|
164
|
+
attendees: [{ email: 'a@x.com' }, { email: 'b@x.com' }]
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
expect(body(res)).toEqual({ id: 'new1', htmlLink: 'https://cal/new1' });
|
|
168
|
+
});
|
|
169
|
+
it('looks up the primary calendar when no calendarId is given', async () => {
|
|
170
|
+
api.calendarList.list.mockResolvedValue({ data: { items: [{ id: 'me@x.com', primary: true }] } });
|
|
171
|
+
api.events.insert.mockResolvedValue({ data: { id: 'new1' } });
|
|
172
|
+
await newService().createEvent({ summary: 'Solo', start: START, end: END });
|
|
173
|
+
expect(api.events.insert).toHaveBeenCalledWith(expect.objectContaining({ calendarId: 'me@x.com' }));
|
|
174
|
+
});
|
|
175
|
+
it('does not notify anyone by default when the event has no attendees', async () => {
|
|
176
|
+
api.events.insert.mockResolvedValue({ data: { id: 'new1' } });
|
|
177
|
+
await newService().createEvent({ summary: 'Solo', start: START, end: END });
|
|
178
|
+
await newService().createEvent({ summary: 'Solo', start: START, end: END, attendees: [] });
|
|
179
|
+
expect(api.events.insert).toHaveBeenNthCalledWith(1, expect.objectContaining({ sendUpdates: 'none' }));
|
|
180
|
+
expect(api.events.insert).toHaveBeenNthCalledWith(2, expect.objectContaining({ sendUpdates: 'none' }));
|
|
181
|
+
});
|
|
182
|
+
it('still creates the event when the client sends a null sendUpdates', async () => {
|
|
183
|
+
api.events.insert.mockResolvedValue({ data: { id: 'new1' } });
|
|
184
|
+
const res = await newService().createEvent({ summary: 'Solo', start: START, end: END, sendUpdates: null });
|
|
185
|
+
expect(body(res)).toEqual({ id: 'new1' });
|
|
186
|
+
expect(api.events.insert).toHaveBeenCalledTimes(1);
|
|
187
|
+
expect(api.events.insert).toHaveBeenCalledWith(expect.objectContaining({ sendUpdates: null }));
|
|
188
|
+
});
|
|
189
|
+
it('keeps an explicit sendUpdates value over the attendee-based default', async () => {
|
|
190
|
+
api.events.insert.mockResolvedValue({ data: { id: 'new1' } });
|
|
191
|
+
await newService().createEvent({
|
|
192
|
+
summary: 'Review',
|
|
193
|
+
start: START,
|
|
194
|
+
end: END,
|
|
195
|
+
attendees: ['a@x.com'],
|
|
196
|
+
sendUpdates: 'externalOnly'
|
|
197
|
+
});
|
|
198
|
+
expect(api.events.insert).toHaveBeenCalledWith(expect.objectContaining({ sendUpdates: 'externalOnly' }));
|
|
199
|
+
});
|
|
200
|
+
it('creates all-day events with a date instead of a dateTime', async () => {
|
|
201
|
+
api.events.insert.mockResolvedValue({ data: { id: 'allday' } });
|
|
202
|
+
await newService().createEvent({
|
|
203
|
+
summary: 'Holiday',
|
|
204
|
+
start: { date: '2026-03-02' },
|
|
205
|
+
end: { date: '2026-03-03' }
|
|
206
|
+
});
|
|
207
|
+
expect(api.events.insert.mock.calls[0][0].requestBody).toMatchObject({
|
|
208
|
+
start: { date: '2026-03-02' },
|
|
209
|
+
end: { date: '2026-03-03' }
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
it('does not tag a default event with an eventType', async () => {
|
|
213
|
+
api.events.insert.mockResolvedValue({ data: { id: 'new1' } });
|
|
214
|
+
await newService().createEvent({
|
|
215
|
+
summary: 'Plain',
|
|
216
|
+
eventType: 'default',
|
|
217
|
+
start: START,
|
|
218
|
+
end: END
|
|
219
|
+
});
|
|
220
|
+
const requestBody = api.events.insert.mock.calls[0][0].requestBody;
|
|
221
|
+
expect(requestBody).not.toHaveProperty('eventType');
|
|
222
|
+
expect(requestBody.summary).toBe('Plain');
|
|
223
|
+
});
|
|
224
|
+
describe('event types', () => {
|
|
225
|
+
it('applies focus-time defaults and a default summary', async () => {
|
|
226
|
+
api.events.insert.mockResolvedValue({ data: { id: 'ft' } });
|
|
227
|
+
await newService().createEvent({ eventType: 'focusTime', start: START, end: END });
|
|
228
|
+
expect(api.events.insert.mock.calls[0][0].requestBody).toEqual({
|
|
229
|
+
summary: 'Focus Time',
|
|
230
|
+
start: START,
|
|
231
|
+
end: END,
|
|
232
|
+
eventType: 'focusTime',
|
|
233
|
+
transparency: 'opaque',
|
|
234
|
+
focusTimeProperties: {
|
|
235
|
+
chatStatus: 'doNotDisturb',
|
|
236
|
+
autoDeclineMode: 'declineOnlyNewConflictingInvitations'
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
it('passes explicit focus-time properties and decline message through', async () => {
|
|
241
|
+
api.events.insert.mockResolvedValue({ data: { id: 'ft' } });
|
|
242
|
+
await newService().createEvent({
|
|
243
|
+
summary: 'Deep work',
|
|
244
|
+
eventType: 'focusTime',
|
|
245
|
+
start: START,
|
|
246
|
+
end: END,
|
|
247
|
+
focusTimeProperties: {
|
|
248
|
+
chatStatus: 'available',
|
|
249
|
+
autoDeclineMode: 'declineAllConflictingInvitations',
|
|
250
|
+
declineMessage: 'Heads down'
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
expect(api.events.insert.mock.calls[0][0].requestBody).toMatchObject({
|
|
254
|
+
summary: 'Deep work',
|
|
255
|
+
focusTimeProperties: {
|
|
256
|
+
chatStatus: 'available',
|
|
257
|
+
autoDeclineMode: 'declineAllConflictingInvitations',
|
|
258
|
+
declineMessage: 'Heads down'
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
it('applies out-of-office defaults and a default summary', async () => {
|
|
263
|
+
api.events.insert.mockResolvedValue({ data: { id: 'ooo' } });
|
|
264
|
+
await newService().createEvent({ eventType: 'outOfOffice', start: START, end: END });
|
|
265
|
+
expect(api.events.insert.mock.calls[0][0].requestBody).toEqual({
|
|
266
|
+
summary: 'Out of Office',
|
|
267
|
+
start: START,
|
|
268
|
+
end: END,
|
|
269
|
+
eventType: 'outOfOffice',
|
|
270
|
+
transparency: 'opaque',
|
|
271
|
+
outOfOfficeProperties: { autoDeclineMode: 'declineOnlyNewConflictingInvitations' }
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
it('passes explicit out-of-office properties and decline message through', async () => {
|
|
275
|
+
api.events.insert.mockResolvedValue({ data: { id: 'ooo' } });
|
|
276
|
+
await newService().createEvent({
|
|
277
|
+
eventType: 'outOfOffice',
|
|
278
|
+
start: START,
|
|
279
|
+
end: END,
|
|
280
|
+
outOfOfficeProperties: { autoDeclineMode: 'declineNone', declineMessage: 'On leave' }
|
|
281
|
+
});
|
|
282
|
+
expect(api.events.insert.mock.calls[0][0].requestBody.outOfOfficeProperties).toEqual({
|
|
283
|
+
autoDeclineMode: 'declineNone',
|
|
284
|
+
declineMessage: 'On leave'
|
|
285
|
+
});
|
|
286
|
+
});
|
|
287
|
+
it('builds a home-office working location as a public, transparent all-day event', async () => {
|
|
288
|
+
api.events.insert.mockResolvedValue({ data: { id: 'wl' } });
|
|
289
|
+
await newService().createEvent({
|
|
290
|
+
eventType: 'workingLocation',
|
|
291
|
+
start: { date: '2026-03-02' },
|
|
292
|
+
end: { date: '2026-03-03' },
|
|
293
|
+
workingLocationProperties: { type: 'homeOffice' }
|
|
294
|
+
});
|
|
295
|
+
expect(api.events.insert.mock.calls[0][0].requestBody).toEqual({
|
|
296
|
+
summary: 'Working Location',
|
|
297
|
+
start: { date: '2026-03-02' },
|
|
298
|
+
end: { date: '2026-03-03' },
|
|
299
|
+
eventType: 'workingLocation',
|
|
300
|
+
visibility: 'public',
|
|
301
|
+
transparency: 'transparent',
|
|
302
|
+
workingLocationProperties: { type: 'homeOffice', homeOffice: {} }
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
it('copies the office location of an office working location', async () => {
|
|
306
|
+
api.events.insert.mockResolvedValue({ data: { id: 'wl' } });
|
|
307
|
+
await newService().createEvent({
|
|
308
|
+
eventType: 'workingLocation',
|
|
309
|
+
start: { date: '2026-03-02' },
|
|
310
|
+
end: { date: '2026-03-03' },
|
|
311
|
+
workingLocationProperties: {
|
|
312
|
+
type: 'officeLocation',
|
|
313
|
+
officeLocation: { buildingId: 'HQ', label: 'Headquarters' }
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
expect(api.events.insert.mock.calls[0][0].requestBody.workingLocationProperties).toEqual({
|
|
317
|
+
type: 'officeLocation',
|
|
318
|
+
officeLocation: { buildingId: 'HQ', label: 'Headquarters' }
|
|
319
|
+
});
|
|
320
|
+
});
|
|
321
|
+
it('copies the label of a custom working location', async () => {
|
|
322
|
+
api.events.insert.mockResolvedValue({ data: { id: 'wl' } });
|
|
323
|
+
await newService().createEvent({
|
|
324
|
+
eventType: 'workingLocation',
|
|
325
|
+
start: { date: '2026-03-02' },
|
|
326
|
+
end: { date: '2026-03-03' },
|
|
327
|
+
workingLocationProperties: { type: 'customLocation', customLocation: { label: 'Cafe' } }
|
|
328
|
+
});
|
|
329
|
+
expect(api.events.insert.mock.calls[0][0].requestBody.workingLocationProperties).toEqual({
|
|
330
|
+
type: 'customLocation',
|
|
331
|
+
customLocation: { label: 'Cafe' }
|
|
332
|
+
});
|
|
333
|
+
});
|
|
334
|
+
it('sends only the type for a working location of an unknown type', async () => {
|
|
335
|
+
api.events.insert.mockResolvedValue({ data: { id: 'wl' } });
|
|
336
|
+
await newService().createEvent({
|
|
337
|
+
eventType: 'workingLocation',
|
|
338
|
+
start: { date: '2026-03-02' },
|
|
339
|
+
end: { date: '2026-03-03' },
|
|
340
|
+
workingLocationProperties: { type: 'somewhereNew' }
|
|
341
|
+
});
|
|
342
|
+
expect(api.events.insert.mock.calls[0][0].requestBody.workingLocationProperties).toEqual({
|
|
343
|
+
type: 'somewhereNew'
|
|
344
|
+
});
|
|
345
|
+
});
|
|
346
|
+
it('prefers a caller-supplied summary over the event-type default', async () => {
|
|
347
|
+
api.events.insert.mockResolvedValue({ data: { id: 'ooo' } });
|
|
348
|
+
await newService().createEvent({
|
|
349
|
+
summary: 'Vacation',
|
|
350
|
+
eventType: 'outOfOffice',
|
|
351
|
+
start: START,
|
|
352
|
+
end: END
|
|
353
|
+
});
|
|
354
|
+
expect(api.events.insert.mock.calls[0][0].requestBody.summary).toBe('Vacation');
|
|
355
|
+
});
|
|
356
|
+
});
|
|
357
|
+
describe('Google Meet and attachments', () => {
|
|
358
|
+
it('requests a Meet conference and bumps conferenceDataVersion', async () => {
|
|
359
|
+
api.events.insert.mockResolvedValue({ data: { id: 'meet' } });
|
|
360
|
+
vi.spyOn(crypto, 'randomUUID').mockReturnValue('11111111-2222-4333-8444-555555555555');
|
|
361
|
+
await newService().createEvent({
|
|
362
|
+
summary: 'Call',
|
|
363
|
+
start: START,
|
|
364
|
+
end: END,
|
|
365
|
+
addGoogleMeet: true
|
|
366
|
+
});
|
|
367
|
+
const params = api.events.insert.mock.calls[0][0];
|
|
368
|
+
expect(params.conferenceDataVersion).toBe(1);
|
|
369
|
+
expect(params.requestBody.conferenceData).toEqual({
|
|
370
|
+
createRequest: {
|
|
371
|
+
requestId: '11111111-2222-4333-8444-555555555555',
|
|
372
|
+
conferenceSolutionKey: { type: 'hangoutsMeet' }
|
|
373
|
+
}
|
|
374
|
+
});
|
|
375
|
+
});
|
|
376
|
+
it('attaches Drive files and enables supportsAttachments', async () => {
|
|
377
|
+
api.events.insert.mockResolvedValue({ data: { id: 'att' } });
|
|
378
|
+
await newService().createEvent({
|
|
379
|
+
summary: 'Review',
|
|
380
|
+
start: START,
|
|
381
|
+
end: END,
|
|
382
|
+
attachments: [
|
|
383
|
+
{ fileUrl: 'https://drive.google.com/file/d/1', title: 'Spec', mimeType: 'application/pdf' },
|
|
384
|
+
{ fileUrl: 'https://drive.google.com/file/d/2' }
|
|
385
|
+
]
|
|
386
|
+
});
|
|
387
|
+
const params = api.events.insert.mock.calls[0][0];
|
|
388
|
+
expect(params.supportsAttachments).toBe(true);
|
|
389
|
+
expect(params.requestBody.attachments).toEqual([
|
|
390
|
+
{ fileUrl: 'https://drive.google.com/file/d/1', title: 'Spec', mimeType: 'application/pdf' },
|
|
391
|
+
{ fileUrl: 'https://drive.google.com/file/d/2' }
|
|
392
|
+
]);
|
|
393
|
+
});
|
|
394
|
+
it('ignores an empty attachment list on create', async () => {
|
|
395
|
+
api.events.insert.mockResolvedValue({ data: { id: 'att' } });
|
|
396
|
+
await newService().createEvent({
|
|
397
|
+
summary: 'Review',
|
|
398
|
+
start: START,
|
|
399
|
+
end: END,
|
|
400
|
+
attachments: []
|
|
401
|
+
});
|
|
402
|
+
const params = api.events.insert.mock.calls[0][0];
|
|
403
|
+
expect(params).not.toHaveProperty('supportsAttachments');
|
|
404
|
+
expect(params.requestBody).not.toHaveProperty('attachments');
|
|
405
|
+
expect(params).not.toHaveProperty('conferenceDataVersion');
|
|
406
|
+
});
|
|
407
|
+
});
|
|
408
|
+
describe('validation errors', () => {
|
|
409
|
+
it('requires a summary for a regular event', async () => {
|
|
410
|
+
const res = await newService().createEvent({ start: START, end: END });
|
|
411
|
+
expect(body(res).details).toBe('summary: summary is required for regular events');
|
|
412
|
+
expect(api.events.insert).not.toHaveBeenCalled();
|
|
413
|
+
});
|
|
414
|
+
it('requires a summary for an explicit default event too', async () => {
|
|
415
|
+
const res = await newService().createEvent({ eventType: 'default', start: START, end: END });
|
|
416
|
+
expect(body(res).details).toBe('summary: summary is required for regular events');
|
|
417
|
+
expect(api.events.insert).not.toHaveBeenCalled();
|
|
418
|
+
});
|
|
419
|
+
it('rejects an event whose start has neither dateTime nor date without calling Google', async () => {
|
|
420
|
+
const res = await newService().createEvent({ summary: 'Bad', start: {}, end: END });
|
|
421
|
+
expect(body(res)).toEqual({
|
|
422
|
+
error: 'Invalid input format',
|
|
423
|
+
details: 'start: start must have exactly one of "dateTime" (for timed events) or "date" (for all-day events)',
|
|
424
|
+
help: 'Please use strict ISO 8601 format with seconds and timezone. Examples: 2024-01-15T10:30:00Z (UTC) or 2024-01-15T10:30:00-05:00 (EST)'
|
|
425
|
+
});
|
|
426
|
+
expect(api.events.insert).not.toHaveBeenCalled();
|
|
427
|
+
});
|
|
428
|
+
it('rejects a dateTime without a timezone', async () => {
|
|
429
|
+
const res = await newService().createEvent({
|
|
430
|
+
summary: 'Bad',
|
|
431
|
+
start: { dateTime: '2026-03-02T09:00:00' },
|
|
432
|
+
end: END
|
|
433
|
+
});
|
|
434
|
+
expect(body(res).details).toContain('Invalid ISO 8601 datetime format');
|
|
435
|
+
expect(api.events.insert).not.toHaveBeenCalled();
|
|
436
|
+
});
|
|
437
|
+
it('points at the attendee list when an attendee email is malformed', async () => {
|
|
438
|
+
const res = await newService().createEvent({
|
|
439
|
+
summary: 'Bad',
|
|
440
|
+
start: START,
|
|
441
|
+
end: END,
|
|
442
|
+
attendees: ['not-an-email']
|
|
443
|
+
});
|
|
444
|
+
expect(body(res)).toEqual({
|
|
445
|
+
error: 'Invalid input format',
|
|
446
|
+
details: '0: Invalid email format',
|
|
447
|
+
help: 'Please ensure all attendee emails are in a valid format.'
|
|
448
|
+
});
|
|
449
|
+
expect(api.events.insert).not.toHaveBeenCalled();
|
|
450
|
+
});
|
|
451
|
+
it('reports an issue without a path as the bare message', async () => {
|
|
452
|
+
const res = await newService().createEvent({
|
|
453
|
+
summary: 'Bad',
|
|
454
|
+
start: START,
|
|
455
|
+
end: END,
|
|
456
|
+
attendees: 'not-an-email'
|
|
457
|
+
});
|
|
458
|
+
expect(body(res).details).toBe('Invalid email format');
|
|
459
|
+
});
|
|
460
|
+
it('joins several validation issues with "; ", prefixing the ones that have a path', async () => {
|
|
461
|
+
vi.mocked(validateCreateEventInput).mockImplementationOnce(() => {
|
|
462
|
+
throw new z.ZodError([
|
|
463
|
+
{ code: 'custom', path: ['start', 'dateTime'], message: 'bad start' },
|
|
464
|
+
{ code: 'custom', path: [], message: 'general problem' }
|
|
465
|
+
]);
|
|
466
|
+
});
|
|
467
|
+
const res = await newService().createEvent({ summary: 'Bad', start: START, end: END });
|
|
468
|
+
expect(body(res)).toMatchObject({
|
|
469
|
+
error: 'Invalid input format',
|
|
470
|
+
details: 'start.dateTime: bad start; general problem'
|
|
471
|
+
});
|
|
472
|
+
});
|
|
473
|
+
it('reports the Error message when validation throws a plain Error', async () => {
|
|
474
|
+
vi.mocked(validateCreateEventInput).mockImplementationOnce(() => {
|
|
475
|
+
throw new Error('custom validation failure');
|
|
476
|
+
});
|
|
477
|
+
const res = await newService().createEvent({ summary: 'Bad', start: START, end: END });
|
|
478
|
+
expect(body(res).details).toBe('custom validation failure');
|
|
479
|
+
});
|
|
480
|
+
it('falls back to a generic message when validation throws something that is not an Error', async () => {
|
|
481
|
+
vi.mocked(validateCreateEventInput).mockImplementationOnce(() => {
|
|
482
|
+
throw 'nope';
|
|
483
|
+
});
|
|
484
|
+
const res = await newService().createEvent({ summary: 'Bad', start: START, end: END });
|
|
485
|
+
expect(body(res).details).toBe('Validation failed');
|
|
486
|
+
expect(api.events.insert).not.toHaveBeenCalled();
|
|
487
|
+
});
|
|
488
|
+
});
|
|
489
|
+
describe('API errors', () => {
|
|
490
|
+
const create = () => newService().createEvent({ summary: 'X', start: START, end: END });
|
|
491
|
+
it('reports a Google error with its code', async () => {
|
|
492
|
+
api.events.insert.mockRejectedValue(googleError({ message: 'Invalid value', code: 400 }));
|
|
493
|
+
expect(body(await create())).toEqual({ error: 'Invalid value (code 400)' });
|
|
494
|
+
});
|
|
495
|
+
it('reports a Google error without a code', async () => {
|
|
496
|
+
api.events.insert.mockRejectedValue(googleError({ message: 'Backend Error' }));
|
|
497
|
+
expect(body(await create())).toEqual({ error: 'Backend Error' });
|
|
498
|
+
});
|
|
499
|
+
it('uses "Unknown Error" when the Google error carries no message', async () => {
|
|
500
|
+
api.events.insert.mockRejectedValue(googleError({ code: 500 }));
|
|
501
|
+
expect(body(await create())).toEqual({ error: 'Unknown Error (code 500)' });
|
|
502
|
+
});
|
|
503
|
+
it('shows a single field error once when it repeats the top-level message', async () => {
|
|
504
|
+
api.events.insert.mockRejectedValue(googleError({
|
|
505
|
+
message: 'Bad start',
|
|
506
|
+
code: 400,
|
|
507
|
+
errors: [
|
|
508
|
+
{ domain: 'global', reason: 'invalid', message: 'Bad start', locationType: 'parameter', location: 'start' }
|
|
509
|
+
]
|
|
510
|
+
}));
|
|
511
|
+
expect(body(await create())).toEqual({ error: 'global.parameter.start invalid: Bad start (code 400)' });
|
|
512
|
+
});
|
|
513
|
+
it('shows a single field error once when the top-level message contains it', async () => {
|
|
514
|
+
api.events.insert.mockRejectedValue(googleError({
|
|
515
|
+
message: 'Invalid value: Bad start',
|
|
516
|
+
errors: [{ domain: 'global', reason: 'invalid', message: 'Bad start' }]
|
|
517
|
+
}));
|
|
518
|
+
expect(body(await create())).toEqual({ error: 'global invalid: Bad start' });
|
|
519
|
+
});
|
|
520
|
+
it('prefixes the top-level message when a single field error says something different', async () => {
|
|
521
|
+
api.events.insert.mockRejectedValue(googleError({
|
|
522
|
+
message: 'Invalid Value',
|
|
523
|
+
code: 400,
|
|
524
|
+
errors: [{ domain: 'calendar', reason: 'timeRangeEmpty', message: 'The specified time range is empty.' }]
|
|
525
|
+
}));
|
|
526
|
+
expect(body(await create())).toEqual({
|
|
527
|
+
error: 'Invalid Value (code 400): calendar timeRangeEmpty: The specified time range is empty.'
|
|
528
|
+
});
|
|
529
|
+
});
|
|
530
|
+
it('lists every field error when there are several', async () => {
|
|
531
|
+
api.events.insert.mockRejectedValue(googleError({
|
|
532
|
+
message: 'Multiple problems',
|
|
533
|
+
code: 400,
|
|
534
|
+
errors: [
|
|
535
|
+
{ domain: 'global', reason: 'required', message: 'Missing summary', location: 'summary' },
|
|
536
|
+
{ message: 'No reason given' }
|
|
537
|
+
]
|
|
538
|
+
}));
|
|
539
|
+
expect(body(await create())).toEqual({
|
|
540
|
+
error: 'Multiple problems (code 400): global.summary required: Missing summary; No reason given'
|
|
541
|
+
});
|
|
542
|
+
});
|
|
543
|
+
it('names the reason after the top-level message when the only field error has no message', async () => {
|
|
544
|
+
api.events.insert.mockRejectedValue(googleError({ message: 'Not Found', code: 404, errors: [{ reason: 'notFound' }] }));
|
|
545
|
+
expect(body(await create())).toEqual({ error: 'Not Found (code 404): notFound' });
|
|
546
|
+
});
|
|
547
|
+
it('leaves out a field error that has neither a message nor any context', async () => {
|
|
548
|
+
api.events.insert.mockRejectedValue(googleError({
|
|
549
|
+
message: 'Multiple problems',
|
|
550
|
+
code: 400,
|
|
551
|
+
errors: [{ domain: 'global', reason: 'required', message: 'Missing summary' }, {}]
|
|
552
|
+
}));
|
|
553
|
+
expect(body(await create())).toEqual({ error: 'Multiple problems (code 400): global required: Missing summary' });
|
|
554
|
+
});
|
|
555
|
+
it('shows only the top-level message when no field error carries anything', async () => {
|
|
556
|
+
api.events.insert.mockRejectedValue(googleError({ message: 'Backend Error', code: 503, errors: [{}] }));
|
|
557
|
+
expect(body(await create())).toEqual({ error: 'Backend Error (code 503)' });
|
|
558
|
+
});
|
|
559
|
+
it('shows only the top-level message when errors is not an array', async () => {
|
|
560
|
+
api.events.insert.mockRejectedValue(googleError({ message: 'Bad Request', code: 400, errors: {} }));
|
|
561
|
+
expect(body(await create())).toEqual({ error: 'Bad Request (code 400)' });
|
|
562
|
+
});
|
|
563
|
+
it('falls back to the Error message when the failure is not a Google error response', async () => {
|
|
564
|
+
api.events.insert.mockRejectedValue(new Error('socket hang up'));
|
|
565
|
+
expect(body(await create())).toEqual({ error: 'socket hang up' });
|
|
566
|
+
});
|
|
567
|
+
it('stringifies a rejection that is not an Error', async () => {
|
|
568
|
+
api.events.insert.mockRejectedValue('weird failure');
|
|
569
|
+
expect(body(await create())).toEqual({ error: 'weird failure' });
|
|
570
|
+
});
|
|
571
|
+
it('reports an authentication failure raised while building the client', async () => {
|
|
572
|
+
h.getAuthenticatedClient.mockRejectedValue(new Error('token expired'));
|
|
573
|
+
const res = await newService().createEvent({ calendarId: 'cal-1', summary: 'X', start: START, end: END });
|
|
574
|
+
expect(body(res)).toEqual({ error: 'token expired' });
|
|
575
|
+
expect(api.events.insert).not.toHaveBeenCalled();
|
|
576
|
+
});
|
|
577
|
+
});
|
|
578
|
+
});
|
|
579
|
+
describe('CalendarService.listEvents', () => {
|
|
580
|
+
const FIELDS = 'nextPageToken, items(id,summary,start,end,description,htmlLink,attendees,status,eventType,focusTimeProperties,outOfOfficeProperties,workingLocationProperties,attachments(fileId,fileUrl,title,mimeType,iconLink))';
|
|
581
|
+
const ids = (res) => body(res).events.map((e) => e.id);
|
|
582
|
+
it('requests expanded single events with the caller time window and returns them', async () => {
|
|
583
|
+
api.events.list.mockResolvedValue({
|
|
584
|
+
data: {
|
|
585
|
+
items: [
|
|
586
|
+
{ id: 'e1', summary: 'Standup' },
|
|
587
|
+
{ id: 'e2', summary: 'Review' }
|
|
588
|
+
]
|
|
589
|
+
}
|
|
590
|
+
});
|
|
591
|
+
const res = await newService().listEvents({
|
|
592
|
+
calendarId: 'cal-1',
|
|
593
|
+
timeMin: '2026-03-01T00:00:00Z',
|
|
594
|
+
timeMax: '2026-03-08T00:00:00Z'
|
|
595
|
+
});
|
|
596
|
+
expect(api.events.list).toHaveBeenCalledWith({
|
|
597
|
+
calendarId: 'cal-1',
|
|
598
|
+
timeMin: '2026-03-01T00:00:00Z',
|
|
599
|
+
timeMax: '2026-03-08T00:00:00Z',
|
|
600
|
+
singleEvents: true,
|
|
601
|
+
fields: FIELDS
|
|
602
|
+
});
|
|
603
|
+
expect(body(res)).toEqual({
|
|
604
|
+
events: [
|
|
605
|
+
{ id: 'e1', summary: 'Standup' },
|
|
606
|
+
{ id: 'e2', summary: 'Review' }
|
|
607
|
+
],
|
|
608
|
+
nextPageToken: null
|
|
609
|
+
});
|
|
610
|
+
});
|
|
611
|
+
it('defaults the window to now through thirty days from now', async () => {
|
|
612
|
+
vi.useFakeTimers({ toFake: ['Date'] });
|
|
613
|
+
vi.setSystemTime(new Date('2026-01-10T08:30:00.000Z'));
|
|
614
|
+
api.events.list.mockResolvedValue({ data: { items: [] } });
|
|
615
|
+
await newService().listEvents({ calendarId: 'cal-1' });
|
|
616
|
+
expect(api.events.list).toHaveBeenCalledWith(expect.objectContaining({
|
|
617
|
+
timeMin: '2026-01-10T08:30:00.000Z',
|
|
618
|
+
timeMax: '2026-02-09T08:30:00.000Z'
|
|
619
|
+
}));
|
|
620
|
+
});
|
|
621
|
+
it('looks up the primary calendar when no calendarId is given', async () => {
|
|
622
|
+
api.calendarList.list.mockResolvedValue({ data: { items: [{ id: 'me@x.com', primary: true }] } });
|
|
623
|
+
api.events.list.mockResolvedValue({ data: { items: [] } });
|
|
624
|
+
await newService().listEvents({ timeMin: '2026-03-01T00:00:00Z', timeMax: '2026-03-08T00:00:00Z' });
|
|
625
|
+
expect(api.events.list).toHaveBeenCalledWith(expect.objectContaining({ calendarId: 'me@x.com' }));
|
|
626
|
+
});
|
|
627
|
+
describe('pagination (fork: maxResults/pageToken in, nextPageToken out)', () => {
|
|
628
|
+
it('forwards maxResults and pageToken to the API', async () => {
|
|
629
|
+
api.events.list.mockResolvedValue({ data: { items: [] } });
|
|
630
|
+
await newService().listEvents({
|
|
631
|
+
calendarId: 'cal-1',
|
|
632
|
+
timeMin: '2026-03-01T00:00:00Z',
|
|
633
|
+
timeMax: '2026-03-08T00:00:00Z',
|
|
634
|
+
maxResults: 25,
|
|
635
|
+
pageToken: 'cursor-2'
|
|
636
|
+
});
|
|
637
|
+
expect(api.events.list).toHaveBeenCalledWith(expect.objectContaining({ maxResults: 25, pageToken: 'cursor-2' }));
|
|
638
|
+
});
|
|
639
|
+
it('forwards a maxResults of 0 rather than treating it as unset', async () => {
|
|
640
|
+
api.events.list.mockResolvedValue({ data: { items: [] } });
|
|
641
|
+
await newService().listEvents({ calendarId: 'cal-1', maxResults: 0 });
|
|
642
|
+
expect(api.events.list.mock.calls[0][0].maxResults).toBe(0);
|
|
643
|
+
});
|
|
644
|
+
it('leaves maxResults and pageToken out of the request when not given', async () => {
|
|
645
|
+
api.events.list.mockResolvedValue({ data: { items: [] } });
|
|
646
|
+
await newService().listEvents({ calendarId: 'cal-1' });
|
|
647
|
+
const params = api.events.list.mock.calls[0][0];
|
|
648
|
+
expect(params).not.toHaveProperty('maxResults');
|
|
649
|
+
expect(params).not.toHaveProperty('pageToken');
|
|
650
|
+
});
|
|
651
|
+
it('returns the next page cursor alongside the events', async () => {
|
|
652
|
+
api.events.list.mockResolvedValue({ data: { items: [{ id: 'e1', summary: 'A' }], nextPageToken: 'cursor-3' } });
|
|
653
|
+
const res = await newService().listEvents({ calendarId: 'cal-1' });
|
|
654
|
+
expect(body(res)).toEqual({ events: [{ id: 'e1', summary: 'A' }], nextPageToken: 'cursor-3' });
|
|
655
|
+
});
|
|
656
|
+
it('returns an empty page and a null cursor when the response has no items', async () => {
|
|
657
|
+
api.events.list.mockResolvedValue({ data: {} });
|
|
658
|
+
const res = await newService().listEvents({ calendarId: 'cal-1' });
|
|
659
|
+
expect(body(res)).toEqual({ events: [], nextPageToken: null });
|
|
660
|
+
});
|
|
661
|
+
});
|
|
662
|
+
describe('event type filter', () => {
|
|
663
|
+
it('passes a non-empty eventTypes list to the API', async () => {
|
|
664
|
+
api.events.list.mockResolvedValue({ data: { items: [] } });
|
|
665
|
+
await newService().listEvents({ calendarId: 'cal-1', eventTypes: ['focusTime', 'birthday'] });
|
|
666
|
+
expect(api.events.list.mock.calls[0][0].eventTypes).toEqual(['focusTime', 'birthday']);
|
|
667
|
+
});
|
|
668
|
+
it('omits eventTypes when the list is empty or missing', async () => {
|
|
669
|
+
api.events.list.mockResolvedValue({ data: { items: [] } });
|
|
670
|
+
await newService().listEvents({ calendarId: 'cal-1', eventTypes: [] });
|
|
671
|
+
await newService().listEvents({ calendarId: 'cal-1' });
|
|
672
|
+
expect(api.events.list.mock.calls[0][0]).not.toHaveProperty('eventTypes');
|
|
673
|
+
expect(api.events.list.mock.calls[1][0]).not.toHaveProperty('eventTypes');
|
|
674
|
+
});
|
|
675
|
+
});
|
|
676
|
+
describe('result filtering', () => {
|
|
677
|
+
it('drops cancelled events', async () => {
|
|
678
|
+
api.events.list.mockResolvedValue({
|
|
679
|
+
data: {
|
|
680
|
+
items: [
|
|
681
|
+
{ id: 'ok', summary: 'Kept', status: 'confirmed' },
|
|
682
|
+
{ id: 'gone', summary: 'Dropped', status: 'cancelled' }
|
|
683
|
+
]
|
|
684
|
+
}
|
|
685
|
+
});
|
|
686
|
+
expect(ids(await newService().listEvents({ calendarId: 'cal-1' }))).toEqual(['ok']);
|
|
687
|
+
});
|
|
688
|
+
it('drops untitled events unless they have a non-default event type', async () => {
|
|
689
|
+
api.events.list.mockResolvedValue({
|
|
690
|
+
data: {
|
|
691
|
+
items: [
|
|
692
|
+
{ id: 'untitled' },
|
|
693
|
+
{ id: 'untitled-default', eventType: 'default' },
|
|
694
|
+
{ id: 'ooo', eventType: 'outOfOffice' },
|
|
695
|
+
{ id: 'titled', summary: 'Named' }
|
|
696
|
+
]
|
|
697
|
+
}
|
|
698
|
+
});
|
|
699
|
+
expect(ids(await newService().listEvents({ calendarId: 'cal-1' }))).toEqual(['ooo', 'titled']);
|
|
700
|
+
});
|
|
701
|
+
it('keeps events without attendees', async () => {
|
|
702
|
+
api.events.list.mockResolvedValue({
|
|
703
|
+
data: {
|
|
704
|
+
items: [
|
|
705
|
+
{ id: 'none', summary: 'A' },
|
|
706
|
+
{ id: 'empty', summary: 'B', attendees: [] }
|
|
707
|
+
]
|
|
708
|
+
}
|
|
709
|
+
});
|
|
710
|
+
expect(ids(await newService().listEvents({ calendarId: 'cal-1' }))).toEqual(['none', 'empty']);
|
|
711
|
+
});
|
|
712
|
+
it('keeps an event where the user is the only attendee, whatever their response', async () => {
|
|
713
|
+
api.events.list.mockResolvedValue({
|
|
714
|
+
data: { items: [{ id: 'solo', summary: 'A', attendees: [{ self: true, responseStatus: 'declined' }] }] }
|
|
715
|
+
});
|
|
716
|
+
expect(ids(await newService().listEvents({ calendarId: 'cal-1' }))).toEqual(['solo']);
|
|
717
|
+
});
|
|
718
|
+
it('keeps an event the user is not an attendee of', async () => {
|
|
719
|
+
api.events.list.mockResolvedValue({
|
|
720
|
+
data: {
|
|
721
|
+
items: [
|
|
722
|
+
{
|
|
723
|
+
id: 'other',
|
|
724
|
+
summary: 'A',
|
|
725
|
+
attendees: [{ email: 'a@x.com', responseStatus: 'declined' }, { email: 'b@x.com' }]
|
|
726
|
+
}
|
|
727
|
+
]
|
|
728
|
+
}
|
|
729
|
+
});
|
|
730
|
+
expect(ids(await newService().listEvents({ calendarId: 'cal-1' }))).toEqual(['other']);
|
|
731
|
+
});
|
|
732
|
+
it('filters by the user response status, treating a missing status as needsAction', async () => {
|
|
733
|
+
const attendees = (status) => [{ email: 'o@x.com' }, { self: true, responseStatus: status }];
|
|
734
|
+
api.events.list.mockResolvedValue({
|
|
735
|
+
data: {
|
|
736
|
+
items: [
|
|
737
|
+
{ id: 'accepted', summary: 'A', attendees: attendees('accepted') },
|
|
738
|
+
{ id: 'tentative', summary: 'B', attendees: attendees('tentative') },
|
|
739
|
+
{ id: 'pending', summary: 'C', attendees: attendees('needsAction') },
|
|
740
|
+
{ id: 'unset', summary: 'D', attendees: attendees(undefined) },
|
|
741
|
+
{ id: 'declined', summary: 'E', attendees: attendees('declined') }
|
|
742
|
+
]
|
|
743
|
+
}
|
|
744
|
+
});
|
|
745
|
+
expect(ids(await newService().listEvents({ calendarId: 'cal-1' }))).toEqual([
|
|
746
|
+
'accepted',
|
|
747
|
+
'tentative',
|
|
748
|
+
'pending',
|
|
749
|
+
'unset'
|
|
750
|
+
]);
|
|
751
|
+
});
|
|
752
|
+
it('honours a custom attendeeResponseStatus list', async () => {
|
|
753
|
+
const attendees = (status) => [{ email: 'o@x.com' }, { self: true, responseStatus: status }];
|
|
754
|
+
api.events.list.mockResolvedValue({
|
|
755
|
+
data: {
|
|
756
|
+
items: [
|
|
757
|
+
{ id: 'accepted', summary: 'A', attendees: attendees('accepted') },
|
|
758
|
+
{ id: 'declined', summary: 'B', attendees: attendees('declined') }
|
|
759
|
+
]
|
|
760
|
+
}
|
|
761
|
+
});
|
|
762
|
+
const res = await newService().listEvents({ calendarId: 'cal-1', attendeeResponseStatus: ['declined'] });
|
|
763
|
+
expect(ids(res)).toEqual(['declined']);
|
|
764
|
+
});
|
|
765
|
+
});
|
|
766
|
+
it('reports an API failure as an error', async () => {
|
|
767
|
+
api.events.list.mockRejectedValue(new Error('rate limited'));
|
|
768
|
+
expect(body(await newService().listEvents({ calendarId: 'cal-1' }))).toEqual({ error: 'rate limited' });
|
|
769
|
+
});
|
|
770
|
+
it('stringifies a non-Error rejection', async () => {
|
|
771
|
+
api.events.list.mockRejectedValue('boom');
|
|
772
|
+
expect(body(await newService().listEvents({ calendarId: 'cal-1' }))).toEqual({ error: 'boom' });
|
|
773
|
+
});
|
|
774
|
+
});
|
|
775
|
+
describe('CalendarService.getEvent', () => {
|
|
776
|
+
it('fetches the event by id and returns its resource', async () => {
|
|
777
|
+
api.events.get.mockResolvedValue({ data: { id: 'ev1', summary: 'Standup' } });
|
|
778
|
+
const res = await newService().getEvent({ eventId: 'ev1', calendarId: 'cal-1' });
|
|
779
|
+
expect(api.events.get).toHaveBeenCalledWith({ calendarId: 'cal-1', eventId: 'ev1' });
|
|
780
|
+
expect(body(res)).toEqual({ id: 'ev1', summary: 'Standup' });
|
|
781
|
+
});
|
|
782
|
+
it('prefers the Google error message over the generic Error message', async () => {
|
|
783
|
+
api.events.get.mockRejectedValue(googleError({ message: 'Not Found', code: 404 }));
|
|
784
|
+
expect(body(await newService().getEvent({ eventId: 'missing', calendarId: 'cal-1' }))).toEqual({
|
|
785
|
+
error: 'Not Found'
|
|
786
|
+
});
|
|
787
|
+
});
|
|
788
|
+
it('falls back to the Error message', async () => {
|
|
789
|
+
api.events.get.mockRejectedValue(new Error('network down'));
|
|
790
|
+
expect(body(await newService().getEvent({ eventId: 'ev1', calendarId: 'cal-1' }))).toEqual({
|
|
791
|
+
error: 'network down'
|
|
792
|
+
});
|
|
793
|
+
});
|
|
794
|
+
it('stringifies a non-Error rejection', async () => {
|
|
795
|
+
api.events.get.mockRejectedValue('odd');
|
|
796
|
+
expect(body(await newService().getEvent({ eventId: 'ev1', calendarId: 'cal-1' }))).toEqual({ error: 'odd' });
|
|
797
|
+
});
|
|
798
|
+
});
|
|
799
|
+
describe('CalendarService.deleteEvent', () => {
|
|
800
|
+
it('deletes the event and confirms it by id', async () => {
|
|
801
|
+
api.events.delete.mockResolvedValue({});
|
|
802
|
+
const res = await newService().deleteEvent({ eventId: 'ev1', calendarId: 'cal-1' });
|
|
803
|
+
expect(api.events.delete).toHaveBeenCalledWith({ calendarId: 'cal-1', eventId: 'ev1' });
|
|
804
|
+
expect(body(res)).toEqual({ message: 'Successfully deleted event ev1' });
|
|
805
|
+
});
|
|
806
|
+
it('deletes from the primary calendar when no calendarId is given', async () => {
|
|
807
|
+
api.events.delete.mockResolvedValue({});
|
|
808
|
+
const res = await newService().deleteEvent({ eventId: 'ev1' });
|
|
809
|
+
expect(api.events.delete).toHaveBeenCalledWith({ calendarId: PRIMARY, eventId: 'ev1' });
|
|
810
|
+
expect(body(res)).toEqual({ message: 'Successfully deleted event ev1' });
|
|
811
|
+
});
|
|
812
|
+
it('prefers the Google error message over the generic Error message', async () => {
|
|
813
|
+
api.events.delete.mockRejectedValue(googleError({ message: 'Resource has been deleted', code: 410 }));
|
|
814
|
+
expect(body(await newService().deleteEvent({ eventId: 'ev1', calendarId: 'cal-1' }))).toEqual({
|
|
815
|
+
error: 'Resource has been deleted'
|
|
816
|
+
});
|
|
817
|
+
});
|
|
818
|
+
it('falls back to the Error message', async () => {
|
|
819
|
+
api.events.delete.mockRejectedValue(new Error('forbidden'));
|
|
820
|
+
expect(body(await newService().deleteEvent({ eventId: 'ev1', calendarId: 'cal-1' }))).toEqual({
|
|
821
|
+
error: 'forbidden'
|
|
822
|
+
});
|
|
823
|
+
});
|
|
824
|
+
it('stringifies a non-Error rejection', async () => {
|
|
825
|
+
api.events.delete.mockRejectedValue('odd');
|
|
826
|
+
expect(body(await newService().deleteEvent({ eventId: 'ev1', calendarId: 'cal-1' }))).toEqual({ error: 'odd' });
|
|
827
|
+
});
|
|
828
|
+
});
|
|
829
|
+
describe('CalendarService.updateEvent', () => {
|
|
830
|
+
it('patches only the fields the caller provided', async () => {
|
|
831
|
+
api.events.patch.mockResolvedValue({ data: { id: 'ev1', summary: 'Renamed' } });
|
|
832
|
+
const res = await newService().updateEvent({ eventId: 'ev1', calendarId: 'cal-1', summary: 'Renamed' });
|
|
833
|
+
expect(api.events.patch).toHaveBeenCalledWith({
|
|
834
|
+
calendarId: 'cal-1',
|
|
835
|
+
eventId: 'ev1',
|
|
836
|
+
requestBody: { summary: 'Renamed' }
|
|
837
|
+
});
|
|
838
|
+
expect(api.events.patch.mock.calls[0][0].requestBody).not.toHaveProperty('description');
|
|
839
|
+
expect(body(res)).toEqual({ id: 'ev1', summary: 'Renamed' });
|
|
840
|
+
});
|
|
841
|
+
it('sends description, times and attendees when provided', async () => {
|
|
842
|
+
api.events.patch.mockResolvedValue({ data: { id: 'ev1' } });
|
|
843
|
+
await newService().updateEvent({
|
|
844
|
+
eventId: 'ev1',
|
|
845
|
+
description: 'New notes',
|
|
846
|
+
start: START,
|
|
847
|
+
end: END,
|
|
848
|
+
attendees: ['a@x.com']
|
|
849
|
+
});
|
|
850
|
+
expect(api.events.patch.mock.calls[0][0].requestBody).toEqual({
|
|
851
|
+
description: 'New notes',
|
|
852
|
+
start: START,
|
|
853
|
+
end: END,
|
|
854
|
+
attendees: [{ email: 'a@x.com' }]
|
|
855
|
+
});
|
|
856
|
+
});
|
|
857
|
+
it('clears the description and the attendee list when given empty values', async () => {
|
|
858
|
+
api.events.patch.mockResolvedValue({ data: { id: 'ev1' } });
|
|
859
|
+
await newService().updateEvent({ eventId: 'ev1', summary: '', description: '', attendees: [] });
|
|
860
|
+
expect(api.events.patch.mock.calls[0][0].requestBody).toEqual({ summary: '', description: '', attendees: [] });
|
|
861
|
+
});
|
|
862
|
+
it('looks up the primary calendar when no calendarId is given', async () => {
|
|
863
|
+
api.calendarList.list.mockResolvedValue({ data: { items: [{ id: 'me@x.com', primary: true }] } });
|
|
864
|
+
api.events.patch.mockResolvedValue({ data: { id: 'ev1' } });
|
|
865
|
+
await newService().updateEvent({ eventId: 'ev1', summary: 'X' });
|
|
866
|
+
expect(api.events.patch).toHaveBeenCalledWith(expect.objectContaining({ calendarId: 'me@x.com' }));
|
|
867
|
+
});
|
|
868
|
+
it('adds a Meet conference when asked', async () => {
|
|
869
|
+
api.events.patch.mockResolvedValue({ data: { id: 'ev1' } });
|
|
870
|
+
await newService().updateEvent({ eventId: 'ev1', addGoogleMeet: true });
|
|
871
|
+
const params = api.events.patch.mock.calls[0][0];
|
|
872
|
+
expect(params.conferenceDataVersion).toBe(1);
|
|
873
|
+
expect(params.requestBody.conferenceData.createRequest.conferenceSolutionKey).toEqual({ type: 'hangoutsMeet' });
|
|
874
|
+
});
|
|
875
|
+
it('replaces attachments with the given list', async () => {
|
|
876
|
+
api.events.patch.mockResolvedValue({ data: { id: 'ev1' } });
|
|
877
|
+
await newService().updateEvent({
|
|
878
|
+
eventId: 'ev1',
|
|
879
|
+
attachments: [{ fileUrl: 'https://drive.google.com/file/d/1', title: 'Spec' }]
|
|
880
|
+
});
|
|
881
|
+
const params = api.events.patch.mock.calls[0][0];
|
|
882
|
+
expect(params.supportsAttachments).toBe(true);
|
|
883
|
+
expect(params.requestBody.attachments).toEqual([{ fileUrl: 'https://drive.google.com/file/d/1', title: 'Spec' }]);
|
|
884
|
+
});
|
|
885
|
+
it('removes every attachment when given an empty list (unlike create)', async () => {
|
|
886
|
+
api.events.patch.mockResolvedValue({ data: { id: 'ev1' } });
|
|
887
|
+
await newService().updateEvent({ eventId: 'ev1', attachments: [] });
|
|
888
|
+
const params = api.events.patch.mock.calls[0][0];
|
|
889
|
+
expect(params.supportsAttachments).toBe(true);
|
|
890
|
+
expect(params.requestBody.attachments).toEqual([]);
|
|
891
|
+
});
|
|
892
|
+
it('leaves attachments and conference data untouched when neither is given', async () => {
|
|
893
|
+
api.events.patch.mockResolvedValue({ data: { id: 'ev1' } });
|
|
894
|
+
await newService().updateEvent({ eventId: 'ev1', summary: 'X' });
|
|
895
|
+
const params = api.events.patch.mock.calls[0][0];
|
|
896
|
+
expect(params).not.toHaveProperty('supportsAttachments');
|
|
897
|
+
expect(params).not.toHaveProperty('conferenceDataVersion');
|
|
898
|
+
expect(params.requestBody).not.toHaveProperty('attachments');
|
|
899
|
+
});
|
|
900
|
+
it('rejects a start with both dateTime and date without calling Google', async () => {
|
|
901
|
+
const res = await newService().updateEvent({
|
|
902
|
+
eventId: 'ev1',
|
|
903
|
+
start: { dateTime: '2026-03-02T09:00:00Z', date: '2026-03-02' }
|
|
904
|
+
});
|
|
905
|
+
expect(body(res)).toMatchObject({ error: 'Invalid input format' });
|
|
906
|
+
expect(body(res).details).toContain('start must have exactly one of');
|
|
907
|
+
expect(api.events.patch).not.toHaveBeenCalled();
|
|
908
|
+
});
|
|
909
|
+
it('rejects a malformed attendee email with the attendee help text', async () => {
|
|
910
|
+
const res = await newService().updateEvent({ eventId: 'ev1', attendees: ['nope'] });
|
|
911
|
+
expect(body(res).help).toBe('Please ensure all attendee emails are in a valid format.');
|
|
912
|
+
expect(api.events.patch).not.toHaveBeenCalled();
|
|
913
|
+
});
|
|
914
|
+
it('falls back to a generic message when validation throws something that is not an Error', async () => {
|
|
915
|
+
vi.mocked(validateUpdateEventInput).mockImplementationOnce(() => {
|
|
916
|
+
throw 42;
|
|
917
|
+
});
|
|
918
|
+
const res = await newService().updateEvent({ eventId: 'ev1', summary: 'X' });
|
|
919
|
+
expect(body(res).details).toBe('Validation failed');
|
|
920
|
+
});
|
|
921
|
+
it('reports the Error message when validation throws a plain Error', async () => {
|
|
922
|
+
vi.mocked(validateUpdateEventInput).mockImplementationOnce(() => {
|
|
923
|
+
throw new Error('custom validation failure');
|
|
924
|
+
});
|
|
925
|
+
const res = await newService().updateEvent({ eventId: 'ev1', summary: 'X' });
|
|
926
|
+
expect(body(res).details).toBe('custom validation failure');
|
|
927
|
+
});
|
|
928
|
+
it('formats a Google API error with its field errors', async () => {
|
|
929
|
+
api.events.patch.mockRejectedValue(googleError({
|
|
930
|
+
message: 'Invalid Value',
|
|
931
|
+
code: 400,
|
|
932
|
+
errors: [{ domain: 'calendar', reason: 'timeRangeEmpty', message: 'The specified time range is empty.' }]
|
|
933
|
+
}));
|
|
934
|
+
const res = await newService().updateEvent({ eventId: 'ev1', summary: 'X' });
|
|
935
|
+
expect(body(res)).toEqual({
|
|
936
|
+
error: 'Invalid Value (code 400): calendar timeRangeEmpty: The specified time range is empty.'
|
|
937
|
+
});
|
|
938
|
+
});
|
|
939
|
+
});
|
|
940
|
+
describe('CalendarService.respondToEvent', () => {
|
|
941
|
+
const eventWithSelf = () => ({
|
|
942
|
+
data: {
|
|
943
|
+
id: 'ev1',
|
|
944
|
+
attendees: [
|
|
945
|
+
{ email: 'org@x.com', responseStatus: 'accepted' },
|
|
946
|
+
{ email: 'me@x.com', self: true, responseStatus: 'needsAction' }
|
|
947
|
+
]
|
|
948
|
+
}
|
|
949
|
+
});
|
|
950
|
+
it('updates only the user attendee entry and notifies by default', async () => {
|
|
951
|
+
api.events.get.mockResolvedValue(eventWithSelf());
|
|
952
|
+
api.events.patch.mockResolvedValue({ data: { id: 'ev1', summary: 'Planning' } });
|
|
953
|
+
const res = await newService().respondToEvent({ eventId: 'ev1', calendarId: 'cal-1', responseStatus: 'accepted' });
|
|
954
|
+
expect(api.events.get).toHaveBeenCalledWith({ calendarId: 'cal-1', eventId: 'ev1' });
|
|
955
|
+
expect(api.events.patch).toHaveBeenCalledWith({
|
|
956
|
+
calendarId: 'cal-1',
|
|
957
|
+
eventId: 'ev1',
|
|
958
|
+
sendNotifications: true,
|
|
959
|
+
requestBody: {
|
|
960
|
+
attendees: [
|
|
961
|
+
{ email: 'org@x.com', responseStatus: 'accepted' },
|
|
962
|
+
{ email: 'me@x.com', self: true, responseStatus: 'accepted' }
|
|
963
|
+
]
|
|
964
|
+
}
|
|
965
|
+
});
|
|
966
|
+
expect(body(res)).toEqual({
|
|
967
|
+
eventId: 'ev1',
|
|
968
|
+
summary: 'Planning',
|
|
969
|
+
responseStatus: 'accepted',
|
|
970
|
+
message: 'Successfully accepted the meeting invitation'
|
|
971
|
+
});
|
|
972
|
+
});
|
|
973
|
+
it('attaches the response message as a comment and says so', async () => {
|
|
974
|
+
api.events.get.mockResolvedValue(eventWithSelf());
|
|
975
|
+
api.events.patch.mockResolvedValue({ data: { id: 'ev1', summary: 'Planning' } });
|
|
976
|
+
const res = await newService().respondToEvent({
|
|
977
|
+
eventId: 'ev1',
|
|
978
|
+
responseStatus: 'declined',
|
|
979
|
+
responseMessage: 'Clash with another meeting'
|
|
980
|
+
});
|
|
981
|
+
const sent = api.events.patch.mock.calls[0][0].requestBody.attendees;
|
|
982
|
+
expect(sent[1]).toEqual({
|
|
983
|
+
email: 'me@x.com',
|
|
984
|
+
self: true,
|
|
985
|
+
responseStatus: 'declined',
|
|
986
|
+
comment: 'Clash with another meeting'
|
|
987
|
+
});
|
|
988
|
+
expect(body(res).message).toBe('Successfully declined the meeting invitation with message');
|
|
989
|
+
});
|
|
990
|
+
it('does not set a comment when no response message is given', async () => {
|
|
991
|
+
api.events.get.mockResolvedValue(eventWithSelf());
|
|
992
|
+
api.events.patch.mockResolvedValue({ data: { id: 'ev1' } });
|
|
993
|
+
await newService().respondToEvent({ eventId: 'ev1', responseStatus: 'tentative' });
|
|
994
|
+
expect(api.events.patch.mock.calls[0][0].requestBody.attendees[1]).not.toHaveProperty('comment');
|
|
995
|
+
});
|
|
996
|
+
it('can respond without notifying the organizer', async () => {
|
|
997
|
+
api.events.get.mockResolvedValue(eventWithSelf());
|
|
998
|
+
api.events.patch.mockResolvedValue({ data: { id: 'ev1' } });
|
|
999
|
+
await newService().respondToEvent({
|
|
1000
|
+
eventId: 'ev1',
|
|
1001
|
+
responseStatus: 'accepted',
|
|
1002
|
+
sendNotification: false
|
|
1003
|
+
});
|
|
1004
|
+
expect(api.events.patch).toHaveBeenCalledWith(expect.objectContaining({ sendNotifications: false }));
|
|
1005
|
+
});
|
|
1006
|
+
it('looks up the primary calendar when no calendarId is given', async () => {
|
|
1007
|
+
api.calendarList.list.mockResolvedValue({ data: { items: [{ id: 'me@x.com', primary: true }] } });
|
|
1008
|
+
api.events.get.mockResolvedValue(eventWithSelf());
|
|
1009
|
+
api.events.patch.mockResolvedValue({ data: { id: 'ev1' } });
|
|
1010
|
+
await newService().respondToEvent({ eventId: 'ev1', responseStatus: 'accepted' });
|
|
1011
|
+
expect(api.events.get).toHaveBeenCalledWith({ calendarId: 'me@x.com', eventId: 'ev1' });
|
|
1012
|
+
});
|
|
1013
|
+
it('refuses an event without attendees and does not patch', async () => {
|
|
1014
|
+
api.events.get.mockResolvedValue({ data: { id: 'ev1' } });
|
|
1015
|
+
const res = await newService().respondToEvent({ eventId: 'ev1', responseStatus: 'accepted' });
|
|
1016
|
+
expect(body(res)).toEqual({ error: 'Event has no attendees' });
|
|
1017
|
+
expect(api.events.patch).not.toHaveBeenCalled();
|
|
1018
|
+
});
|
|
1019
|
+
it('refuses an event with an empty attendee list', async () => {
|
|
1020
|
+
api.events.get.mockResolvedValue({ data: { id: 'ev1', attendees: [] } });
|
|
1021
|
+
const res = await newService().respondToEvent({ eventId: 'ev1', responseStatus: 'accepted' });
|
|
1022
|
+
expect(body(res)).toEqual({ error: 'Event has no attendees' });
|
|
1023
|
+
expect(api.events.patch).not.toHaveBeenCalled();
|
|
1024
|
+
});
|
|
1025
|
+
it('refuses an event the user is not invited to and does not patch', async () => {
|
|
1026
|
+
api.events.get.mockResolvedValue({
|
|
1027
|
+
data: { id: 'ev1', attendees: [{ email: 'org@x.com' }, { email: 'other@x.com', self: false }] }
|
|
1028
|
+
});
|
|
1029
|
+
const res = await newService().respondToEvent({ eventId: 'ev1', responseStatus: 'accepted' });
|
|
1030
|
+
expect(body(res)).toEqual({ error: 'You are not an attendee of this event' });
|
|
1031
|
+
expect(api.events.patch).not.toHaveBeenCalled();
|
|
1032
|
+
});
|
|
1033
|
+
it('reports a failure to read the event', async () => {
|
|
1034
|
+
api.events.get.mockRejectedValue(new Error('event not found'));
|
|
1035
|
+
const res = await newService().respondToEvent({ eventId: 'ev1', responseStatus: 'accepted' });
|
|
1036
|
+
expect(body(res)).toEqual({ error: 'event not found' });
|
|
1037
|
+
expect(api.events.patch).not.toHaveBeenCalled();
|
|
1038
|
+
});
|
|
1039
|
+
it('stringifies a non-Error rejection from the patch call', async () => {
|
|
1040
|
+
api.events.get.mockResolvedValue(eventWithSelf());
|
|
1041
|
+
api.events.patch.mockRejectedValue('patch failed');
|
|
1042
|
+
const res = await newService().respondToEvent({ eventId: 'ev1', responseStatus: 'accepted' });
|
|
1043
|
+
expect(body(res)).toEqual({ error: 'patch failed' });
|
|
1044
|
+
});
|
|
1045
|
+
});
|
|
1046
|
+
describe('CalendarService.findFreeTime', () => {
|
|
1047
|
+
const TIME_MIN = '2026-03-02T09:00:00Z';
|
|
1048
|
+
const TIME_MAX = '2026-03-02T17:00:00Z';
|
|
1049
|
+
const busyFor = (...busy) => ({
|
|
1050
|
+
data: { calendars: { 'a@x.com': { busy } } }
|
|
1051
|
+
});
|
|
1052
|
+
const find = (duration, timeMin = TIME_MIN, timeMax = TIME_MAX) => newService().findFreeTime({ attendees: ['a@x.com'], timeMin, timeMax, duration });
|
|
1053
|
+
it('queries free/busy for every attendee over the window', async () => {
|
|
1054
|
+
api.freebusy.query.mockResolvedValue(busyFor());
|
|
1055
|
+
await newService().findFreeTime({
|
|
1056
|
+
attendees: ['a@x.com', 'b@x.com'],
|
|
1057
|
+
timeMin: TIME_MIN,
|
|
1058
|
+
timeMax: TIME_MAX,
|
|
1059
|
+
duration: 30
|
|
1060
|
+
});
|
|
1061
|
+
expect(api.freebusy.query).toHaveBeenCalledWith({
|
|
1062
|
+
requestBody: { items: [{ id: 'a@x.com' }, { id: 'b@x.com' }], timeMin: TIME_MIN, timeMax: TIME_MAX }
|
|
1063
|
+
});
|
|
1064
|
+
});
|
|
1065
|
+
it('resolves the attendee "me" to the primary calendar id', async () => {
|
|
1066
|
+
api.calendarList.list.mockResolvedValue({ data: { items: [{ id: 'me@x.com', primary: true }] } });
|
|
1067
|
+
api.freebusy.query.mockResolvedValue(busyFor());
|
|
1068
|
+
await newService().findFreeTime({
|
|
1069
|
+
attendees: ['me', 'b@x.com'],
|
|
1070
|
+
timeMin: TIME_MIN,
|
|
1071
|
+
timeMax: TIME_MAX,
|
|
1072
|
+
duration: 30
|
|
1073
|
+
});
|
|
1074
|
+
expect(api.freebusy.query.mock.calls[0][0].requestBody.items).toEqual([{ id: 'me@x.com' }, { id: 'b@x.com' }]);
|
|
1075
|
+
});
|
|
1076
|
+
describe('validation', () => {
|
|
1077
|
+
it('rejects a timeMin that is not ISO 8601 without calling Google', async () => {
|
|
1078
|
+
const res = await find(30, 'tomorrow');
|
|
1079
|
+
expect(body(res)).toMatchObject({ error: 'Invalid input format' });
|
|
1080
|
+
expect(body(res).details).toContain('Invalid ISO 8601 datetime format');
|
|
1081
|
+
expect(api.freebusy.query).not.toHaveBeenCalled();
|
|
1082
|
+
});
|
|
1083
|
+
it('rejects a timeMax without a timezone', async () => {
|
|
1084
|
+
const res = await find(30, TIME_MIN, '2026-03-02T17:00:00');
|
|
1085
|
+
expect(body(res)).toMatchObject({ error: 'Invalid input format' });
|
|
1086
|
+
expect(api.freebusy.query).not.toHaveBeenCalled();
|
|
1087
|
+
});
|
|
1088
|
+
it('rejects a well-formed timestamp that is not a real date', async () => {
|
|
1089
|
+
const res = await find(30, '2026-13-45T09:00:00Z');
|
|
1090
|
+
expect(body(res).error).toBe('Invalid input format');
|
|
1091
|
+
expect(api.freebusy.query).not.toHaveBeenCalled();
|
|
1092
|
+
});
|
|
1093
|
+
});
|
|
1094
|
+
describe('with no busy time', () => {
|
|
1095
|
+
it('offers the start of the window when every calendar is free', async () => {
|
|
1096
|
+
api.freebusy.query.mockResolvedValue(busyFor());
|
|
1097
|
+
expect(body(await find(30))).toEqual({ start: TIME_MIN, end: '2026-03-02T09:30:00.000Z' });
|
|
1098
|
+
});
|
|
1099
|
+
it('offers the start of the window when the response has no calendars', async () => {
|
|
1100
|
+
api.freebusy.query.mockResolvedValue({ data: {} });
|
|
1101
|
+
expect(body(await find(45))).toEqual({ start: TIME_MIN, end: '2026-03-02T09:45:00.000Z' });
|
|
1102
|
+
});
|
|
1103
|
+
it('treats a calendar without a busy list as free', async () => {
|
|
1104
|
+
api.freebusy.query.mockResolvedValue({ data: { calendars: { 'a@x.com': {} } } });
|
|
1105
|
+
expect(body(await find(30))).toEqual({ start: TIME_MIN, end: '2026-03-02T09:30:00.000Z' });
|
|
1106
|
+
});
|
|
1107
|
+
it('computes the slot end from a timeMin carrying a UTC offset (Date.parse)', async () => {
|
|
1108
|
+
api.freebusy.query.mockResolvedValue(busyFor());
|
|
1109
|
+
const res = await find(90, '2026-03-02T09:00:00+07:00', '2026-03-02T17:00:00+07:00');
|
|
1110
|
+
expect(body(res)).toEqual({ start: '2026-03-02T09:00:00+07:00', end: '2026-03-02T03:30:00.000Z' });
|
|
1111
|
+
});
|
|
1112
|
+
it('treats busy entries missing a start or an end as no busy time', async () => {
|
|
1113
|
+
api.freebusy.query.mockResolvedValue(busyFor({ start: '2026-03-02T10:00:00Z' }, { end: '2026-03-02T11:00:00Z' }, {}));
|
|
1114
|
+
expect(body(await find(30))).toEqual({ start: TIME_MIN, end: '2026-03-02T09:30:00.000Z' });
|
|
1115
|
+
});
|
|
1116
|
+
});
|
|
1117
|
+
describe('with busy time', () => {
|
|
1118
|
+
it('offers the start of the window when the meeting fits before the first busy slot', async () => {
|
|
1119
|
+
api.freebusy.query.mockResolvedValue(busyFor({ start: '2026-03-02T10:00:00Z', end: '2026-03-02T11:00:00Z' }));
|
|
1120
|
+
expect(body(await find(30))).toEqual({ start: TIME_MIN, end: '2026-03-02T09:30:00.000Z' });
|
|
1121
|
+
});
|
|
1122
|
+
it('offers the start of the window when the meeting ends exactly as the first busy slot starts', async () => {
|
|
1123
|
+
api.freebusy.query.mockResolvedValue(busyFor({ start: '2026-03-02T09:30:00Z', end: '2026-03-02T11:00:00Z' }));
|
|
1124
|
+
expect(body(await find(30))).toEqual({ start: TIME_MIN, end: '2026-03-02T09:30:00.000Z' });
|
|
1125
|
+
});
|
|
1126
|
+
it('offers the first gap between busy slots that is long enough', async () => {
|
|
1127
|
+
api.freebusy.query.mockResolvedValue(busyFor({ start: '2026-03-02T09:00:00Z', end: '2026-03-02T10:00:00Z' }, { start: '2026-03-02T10:30:00Z', end: '2026-03-02T12:00:00Z' }));
|
|
1128
|
+
expect(body(await find(30))).toEqual({
|
|
1129
|
+
start: '2026-03-02T10:00:00.000Z',
|
|
1130
|
+
end: '2026-03-02T10:30:00.000Z'
|
|
1131
|
+
});
|
|
1132
|
+
});
|
|
1133
|
+
it('skips a gap that is too short and takes the next one', async () => {
|
|
1134
|
+
api.freebusy.query.mockResolvedValue(busyFor({ start: '2026-03-02T09:00:00Z', end: '2026-03-02T10:00:00Z' }, { start: '2026-03-02T10:15:00Z', end: '2026-03-02T11:00:00Z' }, { start: '2026-03-02T12:00:00Z', end: '2026-03-02T13:00:00Z' }));
|
|
1135
|
+
expect(body(await find(30))).toEqual({
|
|
1136
|
+
start: '2026-03-02T11:00:00.000Z',
|
|
1137
|
+
end: '2026-03-02T11:30:00.000Z'
|
|
1138
|
+
});
|
|
1139
|
+
});
|
|
1140
|
+
it('offers the time right after the last busy slot when no earlier gap fits', async () => {
|
|
1141
|
+
api.freebusy.query.mockResolvedValue(busyFor({ start: '2026-03-02T09:00:00Z', end: '2026-03-02T16:00:00Z' }));
|
|
1142
|
+
expect(body(await find(60))).toEqual({
|
|
1143
|
+
start: '2026-03-02T16:00:00.000Z',
|
|
1144
|
+
end: '2026-03-02T17:00:00.000Z'
|
|
1145
|
+
});
|
|
1146
|
+
});
|
|
1147
|
+
it('merges overlapping, adjacent and nested busy slots given out of order', async () => {
|
|
1148
|
+
api.freebusy.query.mockResolvedValue(busyFor({ start: '2026-03-02T11:00:00Z', end: '2026-03-02T12:00:00Z' }, { start: '2026-03-02T09:00:00Z', end: '2026-03-02T10:30:00Z' }, { start: '2026-03-02T09:30:00Z', end: '2026-03-02T10:00:00Z' }, { start: '2026-03-02T10:00:00Z', end: '2026-03-02T11:00:00Z' }));
|
|
1149
|
+
expect(body(await find(30))).toEqual({
|
|
1150
|
+
start: '2026-03-02T12:00:00.000Z',
|
|
1151
|
+
end: '2026-03-02T12:30:00.000Z'
|
|
1152
|
+
});
|
|
1153
|
+
});
|
|
1154
|
+
it('merges the busy slots of several calendars', async () => {
|
|
1155
|
+
api.freebusy.query.mockResolvedValue({
|
|
1156
|
+
data: {
|
|
1157
|
+
calendars: {
|
|
1158
|
+
'a@x.com': { busy: [{ start: '2026-03-02T09:00:00Z', end: '2026-03-02T10:00:00Z' }] },
|
|
1159
|
+
'b@x.com': { busy: [{ start: '2026-03-02T09:30:00Z', end: '2026-03-02T11:00:00Z' }] }
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
});
|
|
1163
|
+
expect(body(await find(30))).toEqual({
|
|
1164
|
+
start: '2026-03-02T11:00:00.000Z',
|
|
1165
|
+
end: '2026-03-02T11:30:00.000Z'
|
|
1166
|
+
});
|
|
1167
|
+
});
|
|
1168
|
+
it('ignores busy entries that lack a start or an end', async () => {
|
|
1169
|
+
api.freebusy.query.mockResolvedValue(busyFor({ start: '2026-03-02T09:00:00Z', end: '2026-03-02T10:00:00Z' }, { start: '2026-03-02T10:00:00Z' }));
|
|
1170
|
+
expect(body(await find(30))).toEqual({
|
|
1171
|
+
start: '2026-03-02T10:00:00.000Z',
|
|
1172
|
+
end: '2026-03-02T10:30:00.000Z'
|
|
1173
|
+
});
|
|
1174
|
+
});
|
|
1175
|
+
it('orders busy slots by instant, not by text, when they carry different UTC offsets (Date.parse)', async () => {
|
|
1176
|
+
// 09:00+07:00 is 02:00Z: earlier than 04:00Z although its text sorts after "2026-03-02T04:00:00Z".
|
|
1177
|
+
api.freebusy.query.mockResolvedValue(busyFor({ start: '2026-03-02T04:00:00Z', end: '2026-03-02T05:00:00Z' }, { start: '2026-03-02T09:00:00+07:00', end: '2026-03-02T10:00:00+07:00' }));
|
|
1178
|
+
const res = await find(30, '2026-03-02T01:45:00Z', '2026-03-02T10:00:00Z');
|
|
1179
|
+
expect(body(res)).toEqual({ start: '2026-03-02T03:00:00.000Z', end: '2026-03-02T03:30:00.000Z' });
|
|
1180
|
+
});
|
|
1181
|
+
it('reports no availability when the window is fully booked', async () => {
|
|
1182
|
+
api.freebusy.query.mockResolvedValue(busyFor({ start: '2026-03-02T08:00:00Z', end: '2026-03-02T18:00:00Z' }));
|
|
1183
|
+
expect(body(await find(30))).toEqual({ error: 'No available free time found' });
|
|
1184
|
+
});
|
|
1185
|
+
it('reports no availability when no gap and no tail is long enough', async () => {
|
|
1186
|
+
api.freebusy.query.mockResolvedValue(busyFor({ start: '2026-03-02T09:00:00Z', end: '2026-03-02T12:00:00Z' }, { start: '2026-03-02T12:30:00Z', end: '2026-03-02T17:00:00Z' }));
|
|
1187
|
+
expect(body(await find(60))).toEqual({ error: 'No available free time found' });
|
|
1188
|
+
});
|
|
1189
|
+
it('does not offer a slot that would run past the end of the window', async () => {
|
|
1190
|
+
api.freebusy.query.mockResolvedValue(busyFor({ start: '2026-03-02T09:00:00Z', end: '2026-03-02T16:30:00Z' }));
|
|
1191
|
+
expect(body(await find(60))).toEqual({ error: 'No available free time found' });
|
|
1192
|
+
});
|
|
1193
|
+
});
|
|
1194
|
+
describe('unparseable busy timestamps (Date.parse yields NaN)', () => {
|
|
1195
|
+
it('reports no availability instead of throwing on an invalid time value', async () => {
|
|
1196
|
+
api.freebusy.query.mockResolvedValue(busyFor({ start: 'not-a-date', end: 'also-not-a-date' }));
|
|
1197
|
+
expect(body(await find(30))).toEqual({ error: 'No available free time found' });
|
|
1198
|
+
});
|
|
1199
|
+
it('never offers a slot after a busy entry whose end is unparseable', async () => {
|
|
1200
|
+
// 09:00 + 120 min overruns the 09:30 start, so only the (NaN) end of the busy entry could free a slot.
|
|
1201
|
+
api.freebusy.query.mockResolvedValue(busyFor({ start: '2026-03-02T09:30:00Z', end: 'garbage' }));
|
|
1202
|
+
expect(body(await find(120))).toEqual({ error: 'No available free time found' });
|
|
1203
|
+
});
|
|
1204
|
+
});
|
|
1205
|
+
describe('API errors', () => {
|
|
1206
|
+
it('reports the Error message when the free/busy query fails', async () => {
|
|
1207
|
+
api.freebusy.query.mockRejectedValue(new Error('freebusy unavailable'));
|
|
1208
|
+
expect(body(await find(30))).toEqual({ error: 'freebusy unavailable' });
|
|
1209
|
+
});
|
|
1210
|
+
it('stringifies a non-Error rejection', async () => {
|
|
1211
|
+
api.freebusy.query.mockRejectedValue('odd');
|
|
1212
|
+
expect(body(await find(30))).toEqual({ error: 'odd' });
|
|
1213
|
+
});
|
|
1214
|
+
it('reports a failure to resolve the primary calendar for "me"', async () => {
|
|
1215
|
+
api.calendarList.list.mockRejectedValue(new Error('calendar list down'));
|
|
1216
|
+
const res = await newService().findFreeTime({
|
|
1217
|
+
attendees: ['me'],
|
|
1218
|
+
timeMin: TIME_MIN,
|
|
1219
|
+
timeMax: TIME_MAX,
|
|
1220
|
+
duration: 30
|
|
1221
|
+
});
|
|
1222
|
+
expect(body(res)).toEqual({ error: 'calendar list down' });
|
|
1223
|
+
expect(api.freebusy.query).not.toHaveBeenCalled();
|
|
1224
|
+
});
|
|
1225
|
+
});
|
|
1226
|
+
});
|
|
1227
|
+
//# sourceMappingURL=CalendarService.test.js.map
|