1
+import { describe, it, expect } from 'vitest';
2
+import {
3
+ ConnectionExpiredEventSchema,
4
+ WebhookEventSchema,
5
+ WebhookEventTypes,
6
+ SingleConnectedAccountDetailedResponseSchema,
7
+ WebhookConnectionMetadataSchema,
8
+} from '../../src/types/webhookEvents.types';
9
+
10
+describe('WebhookEventTypes', () => {
11
+ it('should have correct event type constants', () => {
12
+ expect(WebhookEventTypes.CONNECTION_EXPIRED).toBe('composio.connected_account.expired');
13
+ });
14
+});
15
+
16
+describe('WebhookConnectionMetadataSchema', () => {
17
+ it('should validate valid metadata', () => {
18
+ const validMetadata = {
19
+ project_id: 'pr_koucdrMIwRsf',
20
+ org_id: '4a4ded8f-d3ae-4dea-a229-c30234298b05',
21
+ };
22
+
23
+ const result = WebhookConnectionMetadataSchema.safeParse(validMetadata);
24
+ expect(result.success).toBe(true);
25
+ });
26
+
27
+ it('should reject missing project_id', () => {
28
+ const invalidMetadata = {
29
+ org_id: '4a4ded8f-d3ae-4dea-a229-c30234298b05',
30
+ };
31
+
32
+ const result = WebhookConnectionMetadataSchema.safeParse(invalidMetadata);
33
+ expect(result.success).toBe(false);
34
+ });
35
+
36
+ it('should reject missing org_id', () => {
37
+ const invalidMetadata = {
38
+ project_id: 'pr_koucdrMIwRsf',
39
+ };
40
+
41
+ const result = WebhookConnectionMetadataSchema.safeParse(invalidMetadata);
42
+ expect(result.success).toBe(false);
43
+ });
44
+});
45
+
46
+describe('SingleConnectedAccountDetailedResponseSchema', () => {
47
+ const validConnectedAccountData = {
48
+ toolkit: { slug: 'gmail' },
49
+ auth_config: {
50
+ id: 'ac_izZGRCZ9qyxk',
51
+ auth_scheme: 'OAUTH2',
52
+ is_composio_managed: true,
53
+ is_disabled: false,
54
+ },
55
+ id: 'ca__IvSeEzEBjVt',
56
+ user_id: 'test-user-123',
57
+ status: 'EXPIRED',
58
+ created_at: '2026-02-02T08:35:44.272Z',
59
+ updated_at: '2026-02-02T10:14:20.949Z',
60
+ state: {
61
+ authScheme: 'OAUTH2',
62
+ val: { status: 'EXPIRED', access_token: 'ya29.test' },
63
+ },
64
+ data: { status: 'EXPIRED' },
65
+ params: { status: 'EXPIRED' },
66
+ status_reason: 'Token refresh failed',
67
+ is_disabled: false,
68
+ };
69
+
70
+ it('should validate valid connected account data', () => {
71
+ const result =
72
+ SingleConnectedAccountDetailedResponseSchema.safeParse(validConnectedAccountData);
73
+ expect(result.success).toBe(true);
74
+ });
75
+
76
+ it('should validate connected account with null status_reason', () => {
77
+ const data = {
78
+ ...validConnectedAccountData,
79
+ status_reason: null,
80
+ };
81
+
82
+ const result = SingleConnectedAccountDetailedResponseSchema.safeParse(data);
83
+ expect(result.success).toBe(true);
84
+ });
85
+
86
+ it('should validate connected account with optional deprecated field', () => {
87
+ const data = {
88
+ ...validConnectedAccountData,
89
+ deprecated: {
90
+ labels: ['label1'],
91
+ uuid: 'd9641efa-8007-4864-8a41-3164599d65ec',
92
+ },
93
+ };
94
+
95
+ const result = SingleConnectedAccountDetailedResponseSchema.safeParse(data);
96
+ expect(result.success).toBe(true);
97
+ });
98
+
99
+ it('should reject invalid auth_scheme', () => {
100
+ const data = {
101
+ ...validConnectedAccountData,
102
+ auth_config: {
103
+ ...validConnectedAccountData.auth_config,
104
+ auth_scheme: 'INVALID_SCHEME',
105
+ },
106
+ };
107
+
108
+ const result = SingleConnectedAccountDetailedResponseSchema.safeParse(data);
109
+ expect(result.success).toBe(false);
110
+ });
111
+
112
+ it('should reject invalid status', () => {
113
+ const data = {
114
+ ...validConnectedAccountData,
115
+ status: 'INVALID_STATUS',
116
+ };
117
+
118
+ const result = SingleConnectedAccountDetailedResponseSchema.safeParse(data);
119
+ expect(result.success).toBe(false);
120
+ });
121
+});
122
+
123
+describe('ConnectionExpiredEventSchema', () => {
124
+ const validPayload = {
125
+ id: 'msg_847cdfcd-d219-4f18-a6dd-91acd42ca94a',
126
+ timestamp: '2026-02-02T10:14:20.955Z',
127
+ type: 'composio.connected_account.expired',
128
+ data: {
129
+ toolkit: { slug: 'gmail' },
130
+ auth_config: {
131
+ id: 'ac_izZGRCZ9qyxk',
132
+ auth_scheme: 'OAUTH2',
133
+ is_composio_managed: true,
134
+ is_disabled: false,
135
+ },
136
+ id: 'ca__IvSeEzEBjVt',
137
+ user_id: 'test-user',
138
+ status: 'EXPIRED',
139
+ created_at: '2026-02-02T08:35:44.272Z',
140
+ updated_at: '2026-02-02T10:14:20.949Z',
141
+ state: {
142
+ authScheme: 'OAUTH2',
143
+ val: { status: 'EXPIRED' },
144
+ },
145
+ data: {},
146
+ params: {},
147
+ status_reason: null,
148
+ is_disabled: false,
149
+ },
150
+ metadata: {
151
+ project_id: 'pr_koucdrMIwRsf',
152
+ org_id: '4a4ded8f-d3ae-4dea-a229-c30234298b05',
153
+ },
154
+ };
155
+
156
+ it('validates a valid connection expired event', () => {
157
+ const result = ConnectionExpiredEventSchema.safeParse(validPayload);
158
+ expect(result.success).toBe(true);
159
+ });
160
+
161
+ it('extracts typed data from parsed result', () => {
162
+ const result = ConnectionExpiredEventSchema.safeParse(validPayload);
163
+ expect(result.success).toBe(true);
164
+ if (result.success) {
165
+ expect(result.data.type).toBe('composio.connected_account.expired');
166
+ expect(result.data.data.id).toBe('ca__IvSeEzEBjVt');
167
+ expect(result.data.data.toolkit.slug).toBe('gmail');
168
+ expect(result.data.data.status).toBe('EXPIRED');
169
+ expect(result.data.metadata.project_id).toBe('pr_koucdrMIwRsf');
170
+ }
171
+ });
172
+
173
+ it('rejects invalid event type', () => {
174
+ const payload = {
175
+ ...validPayload,
176
+ type: 'composio.trigger.message',
177
+ };
178
+ const result = ConnectionExpiredEventSchema.safeParse(payload);
179
+ expect(result.success).toBe(false);
180
+ });
181
+
182
+ it('rejects missing metadata fields', () => {
183
+ const payload = {
184
+ ...validPayload,
185
+ metadata: { project_id: 'pr_abc' }, // missing org_id
186
+ };
187
+ const result = ConnectionExpiredEventSchema.safeParse(payload);
188
+ expect(result.success).toBe(false);
189
+ });
190
+
191
+ it('rejects missing data fields', () => {
192
+ const payload = {
193
+ ...validPayload,
194
+ data: { toolkit: { slug: 'gmail' } }, // missing required fields
195
+ };
196
+ const result = ConnectionExpiredEventSchema.safeParse(payload);
197
+ expect(result.success).toBe(false);
198
+ });
199
+
200
+ it('validates payload with status_reason', () => {
201
+ const payload = {
202
+ ...validPayload,
203
+ data: {
204
+ ...validPayload.data,
205
+ status_reason: 'Token refresh failed due to revoked access',
206
+ },
207
+ };
208
+ const result = ConnectionExpiredEventSchema.safeParse(payload);
209
+ expect(result.success).toBe(true);
210
+ if (result.success) {
211
+ expect(result.data.data.status_reason).toBe('Token refresh failed due to revoked access');
212
+ }
213
+ });
214
+});
215
+
216
+describe('WebhookEventSchema', () => {
217
+ const validConnectionExpiredPayload = {
218
+ id: 'msg_847cdfcd-d219-4f18-a6dd-91acd42ca94a',
219
+ timestamp: '2026-02-02T10:14:20.955Z',
220
+ type: 'composio.connected_account.expired',
221
+ data: {
222
+ toolkit: { slug: 'gmail' },
223
+ auth_config: {
224
+ id: 'ac_izZGRCZ9qyxk',
225
+ auth_scheme: 'OAUTH2',
226
+ is_composio_managed: true,
227
+ is_disabled: false,
228
+ },
229
+ id: 'ca__IvSeEzEBjVt',
230
+ user_id: 'test-user',
231
+ status: 'EXPIRED',
232
+ created_at: '2026-02-02T08:35:44.272Z',
233
+ updated_at: '2026-02-02T10:14:20.949Z',
234
+ state: {
235
+ authScheme: 'OAUTH2',
236
+ val: { status: 'EXPIRED' },
237
+ },
238
+ data: {},
239
+ params: {},
240
+ status_reason: null,
241
+ is_disabled: false,
242
+ },
243
+ metadata: {
244
+ project_id: 'pr_koucdrMIwRsf',
245
+ org_id: '4a4ded8f-d3ae-4dea-a229-c30234298b05',
246
+ },
247
+ };
248
+
249
+ it('discriminates by event type for connection expired', () => {
250
+ const result = WebhookEventSchema.safeParse(validConnectionExpiredPayload);
251
+ expect(result.success).toBe(true);
252
+ if (result.success) {
253
+ expect(result.data.type).toBe('composio.connected_account.expired');
254
+ }
255
+ });
256
+
257
+ it('rejects unknown event types', () => {
258
+ const payload = {
259
+ ...validConnectionExpiredPayload,
260
+ type: 'composio.unknown.event',
261
+ };
262
+ const result = WebhookEventSchema.safeParse(payload);
263
+ expect(result.success).toBe(false);
264
+ });
265
+});