forked from asyncapi/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-meetings.test.js
147 lines (120 loc) · 4.3 KB
/
build-meetings.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const { google } = require('googleapis');
const path = require('path');
const { readFileSync, mkdirSync, rmSync } = require('fs');
const { buildMeetings } = require('../scripts/build-meetings.ts');
const { mockEvents, expectedContent } = require('./fixtures/meetingsData');
jest.mock('googleapis', () => {
const events = {
list: jest.fn()
};
const calendar = {
events
};
const mockGoogle = {
calendar: jest.fn(() => calendar),
auth: {
GoogleAuth: jest.fn(() => ({
getClient: jest.fn()
}))
}
};
return { google: mockGoogle };
});
describe('buildMeetings', () => {
const testDir = path.join(__dirname, 'testCache');
const outputFilePath = path.join(testDir, 'meetings.json');
beforeEach(() => {
jest.clearAllMocks();
process.env.CALENDAR_SERVICE_ACCOUNT = JSON.stringify({ key: 'test_key' });
process.env.CALENDAR_ID = 'test_calendar_id';
mkdirSync(testDir, { recursive: true });
});
afterEach(() => {
rmSync(testDir, { recursive: true, force: true });
});
it('should fetch events, process them, and write to a file', async () => {
google.calendar().events.list.mockResolvedValue({ data: { items: mockEvents } });
await buildMeetings(outputFilePath);
expect(google.auth.GoogleAuth).toHaveBeenCalledWith({
scopes: ['https://www.googleapis.com/auth/calendar'],
credentials: { key: 'test_key' }
});
expect(google.calendar).toHaveBeenCalled();
expect(google.calendar().events.list).toHaveBeenCalledWith({
calendarId: 'test_calendar_id',
timeMax: expect.any(String),
timeMin: expect.any(String)
});
const fileContent = readFileSync(outputFilePath, 'utf8');
const parsedContent = JSON.parse(fileContent);
expect(parsedContent).toEqual(expectedContent);
});
it('should throw an error if the Google API call fails', async () => {
google.calendar().events.list.mockRejectedValue(new Error('Google API error'));
try {
await buildMeetings(outputFilePath);
} catch (err) {
expect(err.message).toContain('Google API error');
}
});
it('should handle undefined CALENDAR_SERVICE_ACCOUNT', async () => {
delete process.env.CALENDAR_SERVICE_ACCOUNT;
google.calendar().events.list.mockResolvedValue({ data: { items: [] } });
await buildMeetings(outputFilePath);
expect(google.auth.GoogleAuth).toHaveBeenCalledWith({
scopes: ['https://www.googleapis.com/auth/calendar'],
credentials: undefined
});
const fileContent = readFileSync(outputFilePath, 'utf8');
expect(fileContent).toBe('[]');
});
it('should throw an error if authentication fails', async () => {
google.auth.GoogleAuth.mockImplementation(() => {
throw new Error('Authentication failed');
});
try {
await buildMeetings(outputFilePath);
} catch (err) {
expect(err.message).toContain('Authentication failed');
}
});
it('should handle file write errors', async () => {
google.auth.GoogleAuth.mockImplementation(() => ({
getClient: jest.fn()
}));
google.calendar().events.list.mockResolvedValue({ data: { items: mockEvents } });
const invalidPath = '/root/invalid_dir/meetings.json';
try {
await buildMeetings(invalidPath);
} catch (err) {
expect(err.message).toMatch(/ENOENT|EACCES/);
}
});
it('should throw an error if the data structure received from Google Calendar API is invalid', async () => {
const mockCalendar = google.calendar().events.list;
mockCalendar.mockResolvedValueOnce({
data: {
items: null // or {} or any non-array value to trigger the error
}
});
await expect(buildMeetings('/path/to/write')).rejects.toThrow(
'Invalid data structure received from Google Calendar API'
);
});
it('should throw an error if start.dateTime is missing in the event', async () => {
const mockCalendar = google.calendar().events.list;
mockCalendar.mockResolvedValueOnce({
data: {
items: [
{
summary: 'Test Event',
htmlLink: 'http://example.com/event',
// start.dateTime is intentionally missing to trigger the error
start: {}
}
]
}
});
await expect(buildMeetings('/path/to/write')).rejects.toThrow('start.dateTime is missing in the event');
});
});