@xenterprises/fastify-xtwilio 1.0.0

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,511 @@
1
+ // test/xTwilio.test.js
2
+ import { test } from "node:test";
3
+ import assert from "node:assert";
4
+ import Fastify from "fastify";
5
+ import xTwilio from "../src/xTwilio.js";
6
+
7
+ // Helper function to create a Fastify instance with xTwilio
8
+ async function createInstance(options) {
9
+ const fastify = Fastify({ logger: false });
10
+ await fastify.register(xTwilio, options);
11
+ return fastify;
12
+ }
13
+
14
+ // ============================================================================
15
+ // PLUGIN REGISTRATION TESTS
16
+ // ============================================================================
17
+
18
+ test("Plugin registers successfully with Twilio config", async () => {
19
+ const fastify = await createInstance({
20
+ twilio: {
21
+ accountSid: "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
22
+ authToken: "mock_auth_token",
23
+ phoneNumber: "+12025550123",
24
+ messagingServiceSid: "MGxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
25
+ },
26
+ sendgrid: { active: false },
27
+ });
28
+ assert.ok(fastify.sms);
29
+ assert.ok(fastify.conversations);
30
+ assert.ok(fastify.rcs);
31
+ await fastify.close();
32
+ });
33
+
34
+ test("Plugin registers successfully with SendGrid config", async () => {
35
+ const fastify = await createInstance({
36
+ twilio: { active: false },
37
+ sendgrid: {
38
+ apiKey: "SG.xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
39
+ fromEmail: "test@example.com",
40
+ },
41
+ });
42
+ assert.ok(fastify.email);
43
+ await fastify.close();
44
+ });
45
+
46
+ test("Plugin registers with all services disabled", async () => {
47
+ const fastify = await createInstance({
48
+ twilio: { active: false },
49
+ sendgrid: { active: false },
50
+ });
51
+ assert.ok(fastify);
52
+ await fastify.close();
53
+ });
54
+
55
+ test("Plugin registers with messaging service only", async () => {
56
+ const fastify = await createInstance({
57
+ twilio: {
58
+ accountSid: "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
59
+ authToken: "mock_auth_token",
60
+ messagingServiceSid: "MGxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
61
+ },
62
+ sendgrid: { active: false },
63
+ });
64
+ assert.ok(fastify.sms);
65
+ assert.ok(fastify.rcs);
66
+ await fastify.close();
67
+ });
68
+
69
+ test("Plugin registers with phone number only", async () => {
70
+ const fastify = await createInstance({
71
+ twilio: {
72
+ accountSid: "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
73
+ authToken: "mock_auth_token",
74
+ phoneNumber: "+12025550123",
75
+ },
76
+ sendgrid: { active: false },
77
+ });
78
+ assert.ok(fastify.sms);
79
+ await fastify.close();
80
+ });
81
+
82
+ // ============================================================================
83
+ // SMS SERVICE TESTS
84
+ // ============================================================================
85
+
86
+ test("SMS service has send method", async () => {
87
+ const fastify = await createInstance({
88
+ twilio: {
89
+ accountSid: "AC123",
90
+ authToken: "token",
91
+ phoneNumber: "+1234567890",
92
+ },
93
+ sendgrid: { active: false },
94
+ });
95
+ assert.equal(typeof fastify.sms.send, "function");
96
+ await fastify.close();
97
+ });
98
+
99
+ test("SMS service has sendMMS method", async () => {
100
+ const fastify = await createInstance({
101
+ twilio: {
102
+ accountSid: "AC123",
103
+ authToken: "token",
104
+ phoneNumber: "+1234567890",
105
+ },
106
+ sendgrid: { active: false },
107
+ });
108
+ assert.equal(typeof fastify.sms.sendMMS, "function");
109
+ await fastify.close();
110
+ });
111
+
112
+ test("SMS service has schedule method", async () => {
113
+ const fastify = await createInstance({
114
+ twilio: {
115
+ accountSid: "AC123",
116
+ authToken: "token",
117
+ phoneNumber: "+1234567890",
118
+ },
119
+ sendgrid: { active: false },
120
+ });
121
+ assert.equal(typeof fastify.sms.schedule, "function");
122
+ await fastify.close();
123
+ });
124
+
125
+ test("SMS service has validatePhoneNumber method", async () => {
126
+ const fastify = await createInstance({
127
+ twilio: {
128
+ accountSid: "AC123",
129
+ authToken: "token",
130
+ phoneNumber: "+1234567890",
131
+ },
132
+ sendgrid: { active: false },
133
+ });
134
+ assert.equal(typeof fastify.sms.validatePhoneNumber, "function");
135
+ await fastify.close();
136
+ });
137
+
138
+ test("SMS service has sendBulk method", async () => {
139
+ const fastify = await createInstance({
140
+ twilio: {
141
+ accountSid: "AC123",
142
+ authToken: "token",
143
+ phoneNumber: "+1234567890",
144
+ },
145
+ sendgrid: { active: false },
146
+ });
147
+ assert.equal(typeof fastify.sms.sendBulk, "function");
148
+ await fastify.close();
149
+ });
150
+
151
+ test("SMS service has list method", async () => {
152
+ const fastify = await createInstance({
153
+ twilio: {
154
+ accountSid: "AC123",
155
+ authToken: "token",
156
+ phoneNumber: "+1234567890",
157
+ },
158
+ sendgrid: { active: false },
159
+ });
160
+ assert.equal(typeof fastify.sms.list, "function");
161
+ await fastify.close();
162
+ });
163
+
164
+ test("SMS service has get method", async () => {
165
+ const fastify = await createInstance({
166
+ twilio: {
167
+ accountSid: "AC123",
168
+ authToken: "token",
169
+ phoneNumber: "+1234567890",
170
+ },
171
+ sendgrid: { active: false },
172
+ });
173
+ assert.equal(typeof fastify.sms.get, "function");
174
+ await fastify.close();
175
+ });
176
+
177
+ test("SMS service has getStatus method", async () => {
178
+ const fastify = await createInstance({
179
+ twilio: {
180
+ accountSid: "AC123",
181
+ authToken: "token",
182
+ phoneNumber: "+1234567890",
183
+ },
184
+ sendgrid: { active: false },
185
+ });
186
+ assert.equal(typeof fastify.sms.getStatus, "function");
187
+ await fastify.close();
188
+ });
189
+
190
+ // ============================================================================
191
+ // CONVERSATIONS SERVICE TESTS
192
+ // ============================================================================
193
+
194
+ test("Conversations service has create method", async () => {
195
+ const fastify = await createInstance({
196
+ twilio: {
197
+ accountSid: "AC123",
198
+ authToken: "token",
199
+ phoneNumber: "+1234567890",
200
+ },
201
+ sendgrid: { active: false },
202
+ });
203
+ assert.equal(typeof fastify.conversations.create, "function");
204
+ await fastify.close();
205
+ });
206
+
207
+ test("Conversations service has sendMessage method", async () => {
208
+ const fastify = await createInstance({
209
+ twilio: {
210
+ accountSid: "AC123",
211
+ authToken: "token",
212
+ phoneNumber: "+1234567890",
213
+ },
214
+ sendgrid: { active: false },
215
+ });
216
+ assert.equal(typeof fastify.conversations.sendMessage, "function");
217
+ await fastify.close();
218
+ });
219
+
220
+ test("Conversations service has addParticipant method", async () => {
221
+ const fastify = await createInstance({
222
+ twilio: {
223
+ accountSid: "AC123",
224
+ authToken: "token",
225
+ phoneNumber: "+1234567890",
226
+ },
227
+ sendgrid: { active: false },
228
+ });
229
+ assert.equal(typeof fastify.conversations.addParticipant, "function");
230
+ await fastify.close();
231
+ });
232
+
233
+ test("Conversations service has listParticipants method", async () => {
234
+ const fastify = await createInstance({
235
+ twilio: {
236
+ accountSid: "AC123",
237
+ authToken: "token",
238
+ phoneNumber: "+1234567890",
239
+ },
240
+ sendgrid: { active: false },
241
+ });
242
+ assert.equal(typeof fastify.conversations.listParticipants, "function");
243
+ await fastify.close();
244
+ });
245
+
246
+ test("Conversations service has getMessages method", async () => {
247
+ const fastify = await createInstance({
248
+ twilio: {
249
+ accountSid: "AC123",
250
+ authToken: "token",
251
+ phoneNumber: "+1234567890",
252
+ },
253
+ sendgrid: { active: false },
254
+ });
255
+ assert.equal(typeof fastify.conversations.getMessages, "function");
256
+ await fastify.close();
257
+ });
258
+
259
+ // ============================================================================
260
+ // RCS SERVICE TESTS
261
+ // ============================================================================
262
+
263
+ test("RCS service has send method", async () => {
264
+ const fastify = await createInstance({
265
+ twilio: {
266
+ accountSid: "AC123",
267
+ authToken: "token",
268
+ messagingServiceSid: "MG123",
269
+ },
270
+ sendgrid: { active: false },
271
+ });
272
+ assert.equal(typeof fastify.rcs.send, "function");
273
+ await fastify.close();
274
+ });
275
+
276
+ test("RCS service has sendRichCard method", async () => {
277
+ const fastify = await createInstance({
278
+ twilio: {
279
+ accountSid: "AC123",
280
+ authToken: "token",
281
+ messagingServiceSid: "MG123",
282
+ },
283
+ sendgrid: { active: false },
284
+ });
285
+ assert.equal(typeof fastify.rcs.sendRichCard, "function");
286
+ await fastify.close();
287
+ });
288
+
289
+ test("RCS service has sendCarousel method", async () => {
290
+ const fastify = await createInstance({
291
+ twilio: {
292
+ accountSid: "AC123",
293
+ authToken: "token",
294
+ messagingServiceSid: "MG123",
295
+ },
296
+ sendgrid: { active: false },
297
+ });
298
+ assert.equal(typeof fastify.rcs.sendCarousel, "function");
299
+ await fastify.close();
300
+ });
301
+
302
+ test("RCS service has sendQuickReplies method", async () => {
303
+ const fastify = await createInstance({
304
+ twilio: {
305
+ accountSid: "AC123",
306
+ authToken: "token",
307
+ messagingServiceSid: "MG123",
308
+ },
309
+ sendgrid: { active: false },
310
+ });
311
+ assert.equal(typeof fastify.rcs.sendQuickReplies, "function");
312
+ await fastify.close();
313
+ });
314
+
315
+ test("RCS service has sendTemplate method", async () => {
316
+ const fastify = await createInstance({
317
+ twilio: {
318
+ accountSid: "AC123",
319
+ authToken: "token",
320
+ messagingServiceSid: "MG123",
321
+ },
322
+ sendgrid: { active: false },
323
+ });
324
+ assert.equal(typeof fastify.rcs.sendTemplate, "function");
325
+ await fastify.close();
326
+ });
327
+
328
+ test("RCS service has getStatus method", async () => {
329
+ const fastify = await createInstance({
330
+ twilio: {
331
+ accountSid: "AC123",
332
+ authToken: "token",
333
+ messagingServiceSid: "MG123",
334
+ },
335
+ sendgrid: { active: false },
336
+ });
337
+ assert.equal(typeof fastify.rcs.getStatus, "function");
338
+ await fastify.close();
339
+ });
340
+
341
+ // ============================================================================
342
+ // EMAIL SERVICE TESTS
343
+ // ============================================================================
344
+
345
+ test("Email service has send method", async () => {
346
+ const fastify = await createInstance({
347
+ twilio: { active: false },
348
+ sendgrid: {
349
+ apiKey: "SG.xxxx",
350
+ fromEmail: "test@example.com",
351
+ },
352
+ });
353
+ assert.equal(typeof fastify.email.send, "function");
354
+ await fastify.close();
355
+ });
356
+
357
+ test("Email service has sendTemplate method", async () => {
358
+ const fastify = await createInstance({
359
+ twilio: { active: false },
360
+ sendgrid: {
361
+ apiKey: "SG.xxxx",
362
+ fromEmail: "test@example.com",
363
+ },
364
+ });
365
+ assert.equal(typeof fastify.email.sendTemplate, "function");
366
+ await fastify.close();
367
+ });
368
+
369
+ test("Email service has sendWithAttachments method", async () => {
370
+ const fastify = await createInstance({
371
+ twilio: { active: false },
372
+ sendgrid: {
373
+ apiKey: "SG.xxxx",
374
+ fromEmail: "test@example.com",
375
+ },
376
+ });
377
+ assert.equal(typeof fastify.email.sendWithAttachments, "function");
378
+ await fastify.close();
379
+ });
380
+
381
+ test("Email service has validate method", async () => {
382
+ const fastify = await createInstance({
383
+ twilio: { active: false },
384
+ sendgrid: {
385
+ apiKey: "SG.xxxx",
386
+ fromEmail: "test@example.com",
387
+ },
388
+ });
389
+ assert.equal(typeof fastify.email.validate, "function");
390
+ await fastify.close();
391
+ });
392
+
393
+ test("Email service has sendBulk method", async () => {
394
+ const fastify = await createInstance({
395
+ twilio: { active: false },
396
+ sendgrid: {
397
+ apiKey: "SG.xxxx",
398
+ fromEmail: "test@example.com",
399
+ },
400
+ });
401
+ assert.equal(typeof fastify.email.sendBulk, "function");
402
+ await fastify.close();
403
+ });
404
+
405
+ test("Email service has addContact method", async () => {
406
+ const fastify = await createInstance({
407
+ twilio: { active: false },
408
+ sendgrid: {
409
+ apiKey: "SG.xxxx",
410
+ fromEmail: "test@example.com",
411
+ },
412
+ });
413
+ assert.equal(typeof fastify.email.addContact, "function");
414
+ await fastify.close();
415
+ });
416
+
417
+ test("Email service has searchContact method", async () => {
418
+ const fastify = await createInstance({
419
+ twilio: { active: false },
420
+ sendgrid: {
421
+ apiKey: "SG.xxxx",
422
+ fromEmail: "test@example.com",
423
+ },
424
+ });
425
+ assert.equal(typeof fastify.email.searchContact, "function");
426
+ await fastify.close();
427
+ });
428
+
429
+ test("Email service has createList method", async () => {
430
+ const fastify = await createInstance({
431
+ twilio: { active: false },
432
+ sendgrid: {
433
+ apiKey: "SG.xxxx",
434
+ fromEmail: "test@example.com",
435
+ },
436
+ });
437
+ assert.equal(typeof fastify.email.createList, "function");
438
+ await fastify.close();
439
+ });
440
+
441
+ // ============================================================================
442
+ // MULTIPLE REGISTRATIONS TEST
443
+ // ============================================================================
444
+
445
+ test("Plugin registers successfully with full config", async () => {
446
+ const fastify = await createInstance({
447
+ twilio: {
448
+ accountSid: "AC123",
449
+ authToken: "token",
450
+ phoneNumber: "+1234567890",
451
+ messagingServiceSid: "MG123",
452
+ },
453
+ sendgrid: {
454
+ apiKey: "SG.xxxx",
455
+ fromEmail: "test@example.com",
456
+ },
457
+ });
458
+ assert.ok(fastify.sms);
459
+ assert.ok(fastify.rcs);
460
+ assert.ok(fastify.conversations);
461
+ assert.ok(fastify.email);
462
+ await fastify.close();
463
+ });
464
+
465
+ // ============================================================================
466
+ // CONFIGURATION TESTS
467
+ // ============================================================================
468
+
469
+ test("Plugin accepts Twilio with phone number", async () => {
470
+ const fastify = await createInstance({
471
+ twilio: {
472
+ accountSid: "AC123",
473
+ authToken: "token",
474
+ phoneNumber: "+1234567890",
475
+ },
476
+ sendgrid: { active: false },
477
+ });
478
+ assert.ok(fastify.sms);
479
+ await fastify.close();
480
+ });
481
+
482
+ test("Plugin accepts Twilio with messaging service", async () => {
483
+ const fastify = await createInstance({
484
+ twilio: {
485
+ accountSid: "AC123",
486
+ authToken: "token",
487
+ messagingServiceSid: "MG123",
488
+ },
489
+ sendgrid: { active: false },
490
+ });
491
+ assert.ok(fastify.rcs);
492
+ await fastify.close();
493
+ });
494
+
495
+ test("Plugin accepts full config with both services", async () => {
496
+ const fastify = await createInstance({
497
+ twilio: {
498
+ accountSid: "AC123",
499
+ authToken: "token",
500
+ phoneNumber: "+1234567890",
501
+ messagingServiceSid: "MG123",
502
+ },
503
+ sendgrid: {
504
+ apiKey: "SG.xxxx",
505
+ fromEmail: "test@example.com",
506
+ },
507
+ });
508
+ assert.ok(fastify.sms);
509
+ assert.ok(fastify.email);
510
+ await fastify.close();
511
+ });