@app-connect/core 1.7.24 → 1.7.26

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.
Files changed (137) hide show
  1. package/.env.test +5 -5
  2. package/README.md +441 -441
  3. package/connector/developerPortal.js +31 -42
  4. package/connector/mock.js +84 -77
  5. package/connector/proxy/engine.js +164 -163
  6. package/connector/proxy/index.js +500 -500
  7. package/connector/registry.js +252 -252
  8. package/docs/README.md +50 -50
  9. package/docs/architecture.md +93 -93
  10. package/docs/connectors.md +116 -117
  11. package/docs/handlers.md +125 -125
  12. package/docs/libraries.md +101 -101
  13. package/docs/models.md +144 -144
  14. package/docs/routes.md +115 -115
  15. package/docs/tests.md +73 -73
  16. package/handlers/admin.js +523 -523
  17. package/handlers/appointment.js +193 -0
  18. package/handlers/auth.js +296 -296
  19. package/handlers/calldown.js +99 -99
  20. package/handlers/contact.js +280 -280
  21. package/handlers/disposition.js +82 -80
  22. package/handlers/log.js +984 -973
  23. package/handlers/managedAuth.js +446 -446
  24. package/handlers/plugin.js +208 -208
  25. package/handlers/user.js +142 -142
  26. package/index.js +3140 -2652
  27. package/jest.config.js +56 -56
  28. package/lib/analytics.js +54 -54
  29. package/lib/authSession.js +109 -109
  30. package/lib/cacheCleanup.js +21 -0
  31. package/lib/callLogComposer.js +898 -898
  32. package/lib/callLogLookup.js +34 -0
  33. package/lib/constants.js +8 -8
  34. package/lib/debugTracer.js +177 -177
  35. package/lib/encode.js +30 -30
  36. package/lib/errorHandler.js +218 -206
  37. package/lib/generalErrorMessage.js +41 -41
  38. package/lib/jwt.js +18 -18
  39. package/lib/logger.js +190 -190
  40. package/lib/migrateCallLogsSchema.js +116 -0
  41. package/lib/ringcentral.js +266 -266
  42. package/lib/s3ErrorLogReport.js +65 -65
  43. package/lib/sharedSMSComposer.js +471 -471
  44. package/lib/util.js +67 -67
  45. package/mcp/README.md +412 -395
  46. package/mcp/lib/validator.js +91 -91
  47. package/mcp/mcpHandler.js +425 -425
  48. package/mcp/tools/cancelAppointment.js +101 -0
  49. package/mcp/tools/checkAuthStatus.js +105 -105
  50. package/mcp/tools/confirmAppointment.js +101 -0
  51. package/mcp/tools/createAppointment.js +157 -0
  52. package/mcp/tools/createCallLog.js +327 -316
  53. package/mcp/tools/createContact.js +117 -117
  54. package/mcp/tools/createMessageLog.js +287 -287
  55. package/mcp/tools/doAuth.js +60 -60
  56. package/mcp/tools/findContactByName.js +93 -93
  57. package/mcp/tools/findContactByPhone.js +101 -101
  58. package/mcp/tools/getCallLog.js +111 -102
  59. package/mcp/tools/getGoogleFilePicker.js +99 -99
  60. package/mcp/tools/getHelp.js +43 -43
  61. package/mcp/tools/getPublicConnectors.js +94 -94
  62. package/mcp/tools/getSessionInfo.js +90 -90
  63. package/mcp/tools/index.js +51 -41
  64. package/mcp/tools/listAppointments.js +163 -0
  65. package/mcp/tools/logout.js +96 -96
  66. package/mcp/tools/rcGetCallLogs.js +65 -65
  67. package/mcp/tools/updateAppointment.js +154 -0
  68. package/mcp/tools/updateCallLog.js +130 -126
  69. package/mcp/ui/App/App.tsx +358 -358
  70. package/mcp/ui/App/components/AuthInfoForm.tsx +113 -113
  71. package/mcp/ui/App/components/AuthSuccess.tsx +22 -22
  72. package/mcp/ui/App/components/ConnectorList.tsx +82 -82
  73. package/mcp/ui/App/components/DebugPanel.tsx +43 -43
  74. package/mcp/ui/App/components/OAuthConnect.tsx +270 -270
  75. package/mcp/ui/App/lib/callTool.ts +130 -130
  76. package/mcp/ui/App/lib/debugLog.ts +41 -41
  77. package/mcp/ui/App/lib/developerPortal.ts +111 -111
  78. package/mcp/ui/App/main.css +5 -5
  79. package/mcp/ui/App/root.tsx +13 -13
  80. package/mcp/ui/index.html +13 -13
  81. package/mcp/ui/package-lock.json +6356 -6356
  82. package/mcp/ui/package.json +25 -25
  83. package/mcp/ui/tsconfig.json +26 -26
  84. package/mcp/ui/vite.config.ts +16 -16
  85. package/models/accountDataModel.js +33 -33
  86. package/models/adminConfigModel.js +35 -35
  87. package/models/cacheModel.js +30 -26
  88. package/models/callDownListModel.js +34 -34
  89. package/models/callLogModel.js +33 -27
  90. package/models/dynamo/connectorSchema.js +146 -146
  91. package/models/dynamo/lockSchema.js +24 -24
  92. package/models/dynamo/noteCacheSchema.js +29 -29
  93. package/models/llmSessionModel.js +17 -17
  94. package/models/messageLogModel.js +25 -25
  95. package/models/sequelize.js +16 -16
  96. package/models/userModel.js +45 -45
  97. package/package.json +72 -72
  98. package/releaseNotes.json +1093 -1073
  99. package/test/connector/proxy/engine.test.js +126 -93
  100. package/test/connector/proxy/index.test.js +279 -279
  101. package/test/connector/proxy/sample.json +161 -161
  102. package/test/connector/registry.test.js +415 -415
  103. package/test/handlers/admin.test.js +616 -616
  104. package/test/handlers/auth.test.js +1018 -1015
  105. package/test/handlers/contact.test.js +1014 -1014
  106. package/test/handlers/log.test.js +1298 -1160
  107. package/test/handlers/managedAuth.test.js +458 -458
  108. package/test/handlers/plugin.test.js +380 -380
  109. package/test/index.test.js +105 -105
  110. package/test/lib/cacheCleanup.test.js +42 -0
  111. package/test/lib/callLogComposer.test.js +1231 -1231
  112. package/test/lib/debugTracer.test.js +328 -328
  113. package/test/lib/jwt.test.js +176 -176
  114. package/test/lib/logger.test.js +206 -206
  115. package/test/lib/oauth.test.js +359 -359
  116. package/test/lib/ringcentral.test.js +467 -467
  117. package/test/lib/sharedSMSComposer.test.js +1084 -1084
  118. package/test/lib/util.test.js +329 -329
  119. package/test/mcp/tools/checkAuthStatus.test.js +83 -82
  120. package/test/mcp/tools/createCallLog.test.js +436 -436
  121. package/test/mcp/tools/createContact.test.js +58 -58
  122. package/test/mcp/tools/createMessageLog.test.js +595 -595
  123. package/test/mcp/tools/doAuth.test.js +113 -113
  124. package/test/mcp/tools/findContactByName.test.js +275 -275
  125. package/test/mcp/tools/findContactByPhone.test.js +296 -296
  126. package/test/mcp/tools/getCallLog.test.js +298 -298
  127. package/test/mcp/tools/getGoogleFilePicker.test.js +281 -281
  128. package/test/mcp/tools/getPublicConnectors.test.js +107 -107
  129. package/test/mcp/tools/getSessionInfo.test.js +127 -127
  130. package/test/mcp/tools/logout.test.js +233 -233
  131. package/test/mcp/tools/rcGetCallLogs.test.js +56 -56
  132. package/test/mcp/tools/updateCallLog.test.js +360 -360
  133. package/test/models/accountDataModel.test.js +98 -98
  134. package/test/models/dynamo/connectorSchema.test.js +189 -189
  135. package/test/models/models.test.js +568 -539
  136. package/test/routes/managedAuthRoutes.test.js +104 -129
  137. package/test/setup.js +178 -178
@@ -1,1084 +1,1084 @@
1
- const {
2
- composeSharedSMSLog,
3
- gatherParticipants,
4
- countEntities,
5
- processEntities,
6
- escapeHtml
7
- } = require('../../lib/sharedSMSComposer');
8
- const { LOG_DETAILS_FORMAT_TYPE } = require('../../lib/constants');
9
-
10
- describe('sharedSMSComposer', () => {
11
- describe('composeSharedSMSLog', () => {
12
- const baseConversation = {
13
- creationTime: '2024-01-15T10:30:00Z',
14
- messages: [
15
- { lastModifiedTime: '2024-01-15T10:30:00Z' },
16
- { lastModifiedTime: '2024-01-15T11:45:00Z' }
17
- ],
18
- entities: [
19
- {
20
- recordType: 'AliveMessage',
21
- creationTime: '2024-01-15T10:30:00Z',
22
- direction: 'Inbound',
23
- author: { name: 'John Customer' },
24
- text: 'Hello, I need help'
25
- },
26
- {
27
- recordType: 'AliveMessage',
28
- creationTime: '2024-01-15T10:35:00Z',
29
- direction: 'Outbound',
30
- author: { name: 'Agent Smith' },
31
- text: 'Hi! How can I assist you?'
32
- }
33
- ],
34
- owner: {
35
- name: 'Support Team',
36
- extensionType: 'User',
37
- extensionId: '12345'
38
- }
39
- };
40
-
41
- test('should compose SMS log with default settings (plain text)', () => {
42
- const result = composeSharedSMSLog({
43
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
44
- conversation: baseConversation,
45
- contactName: 'John Customer',
46
- timezoneOffset: '+00:00'
47
- });
48
-
49
- expect(result.subject).toBe('SMS conversation with John Customer');
50
- expect(result.body).toContain('Conversation summary');
51
- expect(result.body).toContain('John Customer (customer)');
52
- expect(result.body).toContain('BEGIN');
53
- expect(result.body).toContain('END');
54
- });
55
-
56
- test('should compose SMS log in HTML format', () => {
57
- const result = composeSharedSMSLog({
58
- logFormat: LOG_DETAILS_FORMAT_TYPE.HTML,
59
- conversation: baseConversation,
60
- contactName: 'John Customer',
61
- timezoneOffset: '+00:00'
62
- });
63
-
64
- expect(result.subject).toBe('SMS conversation with John Customer');
65
- expect(result.body).toContain('<b>Conversation summary</b>');
66
- expect(result.body).toContain('<b>Participants</b>');
67
- expect(result.body).toContain('<li>');
68
- });
69
-
70
- test('should compose SMS log in Markdown format', () => {
71
- const result = composeSharedSMSLog({
72
- logFormat: LOG_DETAILS_FORMAT_TYPE.MARKDOWN,
73
- conversation: baseConversation,
74
- contactName: 'John Customer',
75
- timezoneOffset: '+00:00'
76
- });
77
-
78
- expect(result.subject).toBe('**SMS conversation with John Customer**');
79
- expect(result.body).toContain('## Conversation summary');
80
- expect(result.body).toContain('### Participants');
81
- expect(result.body).toContain('---');
82
- });
83
-
84
- test('should handle conversation with call queue owner', () => {
85
- const conversationWithQueue = {
86
- ...baseConversation,
87
- owner: {
88
- name: 'Sales Queue',
89
- extensionType: 'Department',
90
- extensionId: '99999'
91
- }
92
- };
93
-
94
- const result = composeSharedSMSLog({
95
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
96
- conversation: conversationWithQueue,
97
- contactName: 'John Customer',
98
- timezoneOffset: '+00:00'
99
- });
100
-
101
- expect(result.body).toContain('Receiving call queue: Sales Queue');
102
- });
103
-
104
- test('should handle conversation with notes', () => {
105
- const conversationWithNotes = {
106
- ...baseConversation,
107
- entities: [
108
- ...baseConversation.entities,
109
- {
110
- recordType: 'AliveNote',
111
- creationTime: '2024-01-15T10:40:00Z',
112
- author: { name: 'Agent Smith' },
113
- text: 'Customer prefers email contact'
114
- }
115
- ]
116
- };
117
-
118
- const result = composeSharedSMSLog({
119
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
120
- conversation: conversationWithNotes,
121
- contactName: 'John Customer',
122
- timezoneOffset: '+00:00'
123
- });
124
-
125
- expect(result.body).toContain('2 messages');
126
- expect(result.body).toContain('1 note');
127
- expect(result.body).toContain('left a note');
128
- });
129
-
130
- test('should handle conversation with assignment', () => {
131
- const conversationWithAssignment = {
132
- ...baseConversation,
133
- entities: [
134
- ...baseConversation.entities,
135
- {
136
- recordType: 'ThreadAssignedHint',
137
- creationTime: '2024-01-15T10:32:00Z',
138
- assignee: { name: 'Agent Smith' }
139
- }
140
- ]
141
- };
142
-
143
- const result = composeSharedSMSLog({
144
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
145
- conversation: conversationWithAssignment,
146
- contactName: 'John Customer',
147
- timezoneOffset: '+00:00'
148
- });
149
-
150
- expect(result.body).toContain('Conversation assigned to Agent Smith');
151
- });
152
-
153
- test('should skip ThreadResolvedHint entities (not processed)', () => {
154
- const conversationWithResolved = {
155
- ...baseConversation,
156
- entities: [
157
- ...baseConversation.entities,
158
- {
159
- recordType: 'ThreadResolvedHint',
160
- creationTime: '2024-01-15T11:45:00Z',
161
- initiator: { name: 'Agent Smith' }
162
- }
163
- ]
164
- };
165
-
166
- const result = composeSharedSMSLog({
167
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
168
- conversation: conversationWithResolved,
169
- contactName: 'John Customer',
170
- timezoneOffset: '+00:00'
171
- });
172
-
173
- // ThreadResolvedHint is not processed, so it won't appear in the body
174
- expect(result.body).not.toContain('resolved the conversation');
175
- });
176
-
177
- test('should skip ThreadReopenedHint entities (not processed)', () => {
178
- const conversationWithReopened = {
179
- ...baseConversation,
180
- entities: [
181
- ...baseConversation.entities,
182
- {
183
- recordType: 'ThreadReopenedHint',
184
- creationTime: '2024-01-15T12:00:00Z',
185
- initiator: { name: 'Agent Smith' }
186
- }
187
- ]
188
- };
189
-
190
- const result = composeSharedSMSLog({
191
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
192
- conversation: conversationWithReopened,
193
- contactName: 'John Customer',
194
- timezoneOffset: '+00:00'
195
- });
196
-
197
- // ThreadReopenedHint is not processed, so it won't appear in the body
198
- expect(result.body).not.toContain('reopened the conversation');
199
- });
200
-
201
- test('should apply timezone offset', () => {
202
- const result = composeSharedSMSLog({
203
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
204
- conversation: baseConversation,
205
- contactName: 'John Customer',
206
- timezoneOffset: '+05:00'
207
- });
208
-
209
- // The time should be adjusted by +5 hours
210
- expect(result.body).toContain('Started:');
211
- });
212
-
213
- test('should handle empty entities array', () => {
214
- const emptyConversation = {
215
- creationTime: '2024-01-15T10:30:00Z',
216
- messages: [{ lastModifiedTime: '2024-01-15T11:45:00Z' }],
217
- entities: []
218
- };
219
-
220
- const result = composeSharedSMSLog({
221
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
222
- conversation: emptyConversation,
223
- contactName: 'John Customer',
224
- timezoneOffset: '+00:00'
225
- });
226
-
227
- expect(result.body).toContain('0 messages');
228
- });
229
-
230
- test('should handle missing entities', () => {
231
- const noEntitiesConversation = {
232
- creationTime: '2024-01-15T10:30:00Z',
233
- messages: [{ lastModifiedTime: '2024-01-15T11:45:00Z' }]
234
- };
235
-
236
- const result = composeSharedSMSLog({
237
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
238
- conversation: noEntitiesConversation,
239
- contactName: 'John Customer',
240
- timezoneOffset: '+00:00'
241
- });
242
-
243
- expect(result.body).toContain('0 messages');
244
- });
245
- });
246
-
247
- describe('gatherParticipants', () => {
248
- test('should gather participants from author names', () => {
249
- const entities = [
250
- { author: { name: 'Agent Smith' } },
251
- { author: { name: 'Agent Jones' } }
252
- ];
253
-
254
- const result = gatherParticipants(entities);
255
-
256
- expect(result).toContain('Agent Smith');
257
- expect(result).toContain('Agent Jones');
258
- });
259
-
260
- test('should gather participants from from names', () => {
261
- const entities = [
262
- { from: { name: 'Customer John' } }
263
- ];
264
-
265
- const result = gatherParticipants(entities);
266
-
267
- expect(result).toContain('Customer John');
268
- });
269
-
270
- test('should gather participants from initiator names', () => {
271
- const entities = [
272
- { initiator: { name: 'Manager Bob' } }
273
- ];
274
-
275
- const result = gatherParticipants(entities);
276
-
277
- expect(result).toContain('Manager Bob');
278
- });
279
-
280
- test('should gather participants from assignee names', () => {
281
- const entities = [
282
- { assignee: { name: 'Agent Smith' } }
283
- ];
284
-
285
- const result = gatherParticipants(entities);
286
-
287
- expect(result).toContain('Agent Smith');
288
- });
289
-
290
- test('should deduplicate participants', () => {
291
- const entities = [
292
- { author: { name: 'Agent Smith' } },
293
- { from: { name: 'Agent Smith' } },
294
- { initiator: { name: 'Agent Smith' } }
295
- ];
296
-
297
- const result = gatherParticipants(entities);
298
-
299
- expect(result).toHaveLength(1);
300
- expect(result).toContain('Agent Smith');
301
- });
302
-
303
- test('should handle empty entities array', () => {
304
- const result = gatherParticipants([]);
305
-
306
- expect(result).toEqual([]);
307
- });
308
-
309
- test('should handle entities without names', () => {
310
- const entities = [
311
- { author: {} },
312
- { from: null },
313
- {}
314
- ];
315
-
316
- const result = gatherParticipants(entities);
317
-
318
- expect(result).toEqual([]);
319
- });
320
- });
321
-
322
- describe('countEntities', () => {
323
- test('should count messages correctly', () => {
324
- const entities = [
325
- { recordType: 'AliveMessage' },
326
- { recordType: 'AliveMessage' },
327
- { recordType: 'AliveMessage' }
328
- ];
329
-
330
- const result = countEntities(entities);
331
-
332
- expect(result.messageCount).toBe(3);
333
- expect(result.noteCount).toBe(0);
334
- });
335
-
336
- test('should count AliveNote as note', () => {
337
- const entities = [
338
- { recordType: 'AliveNote' },
339
- { recordType: 'AliveNote' }
340
- ];
341
-
342
- const result = countEntities(entities);
343
-
344
- expect(result.messageCount).toBe(0);
345
- expect(result.noteCount).toBe(2);
346
- });
347
-
348
- test('should not count NoteHint as note (only AliveNote is counted)', () => {
349
- const entities = [
350
- { recordType: 'NoteHint' },
351
- { recordType: 'ThreadNoteAddedHint' }
352
- ];
353
-
354
- const result = countEntities(entities);
355
-
356
- expect(result.messageCount).toBe(0);
357
- expect(result.noteCount).toBe(0);
358
- });
359
-
360
- test('should not count ThreadAssignedHint as note', () => {
361
- const entities = [
362
- { recordType: 'ThreadAssignedHint' }
363
- ];
364
-
365
- const result = countEntities(entities);
366
-
367
- expect(result.noteCount).toBe(0);
368
- });
369
-
370
- test('should count both messages and notes', () => {
371
- const entities = [
372
- { recordType: 'AliveMessage' },
373
- { recordType: 'AliveMessage' },
374
- { recordType: 'AliveNote' },
375
- { recordType: 'AliveNote' }
376
- ];
377
-
378
- const result = countEntities(entities);
379
-
380
- expect(result.messageCount).toBe(2);
381
- expect(result.noteCount).toBe(2);
382
- });
383
-
384
- test('should return zero counts for empty array', () => {
385
- const result = countEntities([]);
386
-
387
- expect(result.messageCount).toBe(0);
388
- expect(result.noteCount).toBe(0);
389
- });
390
-
391
- test('should ignore other record types', () => {
392
- const entities = [
393
- { recordType: 'ThreadResolvedHint' },
394
- { recordType: 'ThreadReopenedHint' },
395
- { recordType: 'ThreadCreatedHint' },
396
- { recordType: 'NoteHint' },
397
- { recordType: 'ThreadNoteAddedHint' },
398
- { recordType: 'ThreadAssignedHint' }
399
- ];
400
-
401
- const result = countEntities(entities);
402
-
403
- expect(result.messageCount).toBe(0);
404
- expect(result.noteCount).toBe(0);
405
- });
406
- });
407
-
408
- describe('processEntities', () => {
409
- test('should process message entities (plain text)', () => {
410
- const entities = [
411
- {
412
- recordType: 'AliveMessage',
413
- creationTime: '2024-01-15T10:30:00Z',
414
- direction: 'Inbound',
415
- author: { name: 'Customer' },
416
- text: 'Hello!'
417
- }
418
- ];
419
-
420
- const result = processEntities({
421
- entities,
422
- timezoneOffset: '+00:00',
423
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
424
- contactName: 'Customer'
425
- });
426
-
427
- expect(result).toHaveLength(1);
428
- expect(result[0].type).toBe('message');
429
- expect(result[0].content).toContain('said on');
430
- expect(result[0].content).toContain('Hello!');
431
- });
432
-
433
- test('should process message entities (HTML)', () => {
434
- const entities = [
435
- {
436
- recordType: 'AliveMessage',
437
- creationTime: '2024-01-15T10:30:00Z',
438
- direction: 'Outbound',
439
- author: { name: 'Agent' },
440
- text: 'Hi there!'
441
- }
442
- ];
443
-
444
- const result = processEntities({
445
- entities,
446
- timezoneOffset: '+00:00',
447
- logFormat: LOG_DETAILS_FORMAT_TYPE.HTML,
448
- contactName: 'Customer'
449
- });
450
-
451
- expect(result).toHaveLength(1);
452
- expect(result[0].content).toContain('<p>');
453
- expect(result[0].content).toContain('<b>');
454
- });
455
-
456
- test('should process message entities (Markdown)', () => {
457
- const entities = [
458
- {
459
- recordType: 'AliveMessage',
460
- creationTime: '2024-01-15T10:30:00Z',
461
- direction: 'Outbound',
462
- author: { name: 'Agent' },
463
- text: 'Hi there!'
464
- }
465
- ];
466
-
467
- const result = processEntities({
468
- entities,
469
- timezoneOffset: '+00:00',
470
- logFormat: LOG_DETAILS_FORMAT_TYPE.MARKDOWN,
471
- contactName: 'Customer'
472
- });
473
-
474
- expect(result).toHaveLength(1);
475
- expect(result[0].content).toContain('**');
476
- });
477
-
478
- test('should process assignment entities', () => {
479
- const entities = [
480
- {
481
- recordType: 'ThreadAssignedHint',
482
- creationTime: '2024-01-15T10:30:00Z',
483
- assignee: { name: 'Agent Smith' }
484
- }
485
- ];
486
-
487
- const result = processEntities({
488
- entities,
489
- timezoneOffset: '+00:00',
490
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
491
- contactName: 'Customer'
492
- });
493
-
494
- expect(result).toHaveLength(1);
495
- expect(result[0].type).toBe('assignment');
496
- expect(result[0].content).toContain('assigned to Agent Smith');
497
- });
498
-
499
- test('should skip ThreadResolvedHint entities', () => {
500
- const entities = [
501
- {
502
- recordType: 'ThreadResolvedHint',
503
- creationTime: '2024-01-15T10:30:00Z',
504
- initiator: { name: 'Agent Smith' }
505
- }
506
- ];
507
-
508
- const result = processEntities({
509
- entities,
510
- timezoneOffset: '+00:00',
511
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
512
- contactName: 'Customer'
513
- });
514
-
515
- // ThreadResolvedHint is not processed (returns null)
516
- expect(result).toHaveLength(0);
517
- });
518
-
519
- test('should skip ThreadReopenedHint entities', () => {
520
- const entities = [
521
- {
522
- recordType: 'ThreadReopenedHint',
523
- creationTime: '2024-01-15T10:30:00Z',
524
- initiator: { name: 'Agent Smith' }
525
- }
526
- ];
527
-
528
- const result = processEntities({
529
- entities,
530
- timezoneOffset: '+00:00',
531
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
532
- contactName: 'Customer'
533
- });
534
-
535
- // ThreadReopenedHint is not processed (returns null)
536
- expect(result).toHaveLength(0);
537
- });
538
-
539
- test('should process AliveNote entities', () => {
540
- const entities = [
541
- {
542
- recordType: 'AliveNote',
543
- creationTime: '2024-01-15T10:30:00Z',
544
- author: { name: 'Agent Smith' },
545
- text: 'Important note here'
546
- }
547
- ];
548
-
549
- const result = processEntities({
550
- entities,
551
- timezoneOffset: '+00:00',
552
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
553
- contactName: 'Customer'
554
- });
555
-
556
- expect(result).toHaveLength(1);
557
- expect(result[0].type).toBe('note');
558
- expect(result[0].content).toContain('left a note');
559
- expect(result[0].content).toContain('Important note here');
560
- });
561
-
562
- test('should skip NoteHint entities (not processed)', () => {
563
- const entities = [
564
- {
565
- recordType: 'NoteHint',
566
- creationTime: '2024-01-15T10:30:00Z',
567
- author: { name: 'Agent Smith' },
568
- text: 'Important note here'
569
- }
570
- ];
571
-
572
- const result = processEntities({
573
- entities,
574
- timezoneOffset: '+00:00',
575
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
576
- contactName: 'Customer'
577
- });
578
-
579
- // NoteHint is counted but not processed into formatted entries
580
- expect(result).toHaveLength(0);
581
- });
582
-
583
- test('should skip ThreadCreatedHint entities', () => {
584
- const entities = [
585
- {
586
- recordType: 'ThreadCreatedHint',
587
- creationTime: '2024-01-15T10:30:00Z'
588
- }
589
- ];
590
-
591
- const result = processEntities({
592
- entities,
593
- timezoneOffset: '+00:00',
594
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
595
- contactName: 'Customer'
596
- });
597
-
598
- expect(result).toHaveLength(0);
599
- });
600
-
601
- test('should process multiple message entities', () => {
602
- const entities = [
603
- {
604
- recordType: 'AliveMessage',
605
- creationTime: '2024-01-15T10:00:00Z',
606
- direction: 'Outbound',
607
- author: { name: 'Agent' },
608
- text: 'First message'
609
- },
610
- {
611
- recordType: 'AliveMessage',
612
- creationTime: '2024-01-15T12:00:00Z',
613
- direction: 'Outbound',
614
- author: { name: 'Agent' },
615
- text: 'Third message'
616
- },
617
- {
618
- recordType: 'AliveMessage',
619
- creationTime: '2024-01-15T11:00:00Z',
620
- direction: 'Outbound',
621
- author: { name: 'Agent' },
622
- text: 'Second message'
623
- }
624
- ];
625
-
626
- const result = processEntities({
627
- entities,
628
- timezoneOffset: '+00:00',
629
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
630
- contactName: 'Customer'
631
- });
632
-
633
- expect(result).toHaveLength(3);
634
- // Verify all messages are processed
635
- const allContent = result.map(r => r.content).join(' ');
636
- expect(allContent).toContain('First message');
637
- expect(allContent).toContain('Second message');
638
- expect(allContent).toContain('Third message');
639
- });
640
-
641
- test('should apply timezone offset to timestamps', () => {
642
- const entities = [
643
- {
644
- recordType: 'AliveMessage',
645
- creationTime: '2024-01-15T10:30:00Z',
646
- direction: 'Outbound',
647
- author: { name: 'Agent' },
648
- text: 'Hello!'
649
- }
650
- ];
651
-
652
- const result = processEntities({
653
- entities,
654
- timezoneOffset: '+05:00',
655
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
656
- contactName: 'Customer'
657
- });
658
-
659
- // 10:30 UTC + 5 hours = 15:30 (3:30 PM)
660
- expect(result[0].content).toContain('03:30 PM');
661
- });
662
-
663
- test('should handle empty entities array', () => {
664
- const result = processEntities({
665
- entities: [],
666
- timezoneOffset: '+00:00',
667
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
668
- contactName: 'Customer'
669
- });
670
-
671
- expect(result).toEqual([]);
672
- });
673
- });
674
-
675
- describe('escapeHtml', () => {
676
- test('should escape ampersand', () => {
677
- expect(escapeHtml('Tom & Jerry')).toBe('Tom &amp; Jerry');
678
- });
679
-
680
- test('should escape less than', () => {
681
- expect(escapeHtml('a < b')).toBe('a &lt; b');
682
- });
683
-
684
- test('should escape greater than', () => {
685
- expect(escapeHtml('a > b')).toBe('a &gt; b');
686
- });
687
-
688
- test('should escape double quotes', () => {
689
- expect(escapeHtml('say "hello"')).toBe('say &quot;hello&quot;');
690
- });
691
-
692
- test('should escape single quotes', () => {
693
- expect(escapeHtml("it's fine")).toBe('it&#039;s fine');
694
- });
695
-
696
- test('should escape multiple special characters', () => {
697
- expect(escapeHtml('<script>alert("XSS")</script>'))
698
- .toBe('&lt;script&gt;alert(&quot;XSS&quot;)&lt;/script&gt;');
699
- });
700
-
701
- test('should return empty string for null input', () => {
702
- expect(escapeHtml(null)).toBe('');
703
- });
704
-
705
- test('should return empty string for undefined input', () => {
706
- expect(escapeHtml(undefined)).toBe('');
707
- });
708
-
709
- test('should return empty string for empty string input', () => {
710
- expect(escapeHtml('')).toBe('');
711
- });
712
-
713
- test('should not modify text without special characters', () => {
714
- expect(escapeHtml('Hello World')).toBe('Hello World');
715
- });
716
- });
717
-
718
- describe('Format-specific output', () => {
719
- const testConversation = {
720
- creationTime: '2024-01-15T10:30:00Z',
721
- messages: [{ lastModifiedTime: '2024-01-15T11:45:00Z' }],
722
- entities: [
723
- {
724
- recordType: 'AliveMessage',
725
- creationTime: '2024-01-15T10:30:00Z',
726
- direction: 'Inbound',
727
- author: { name: 'John Customer' },
728
- text: 'Hello!'
729
- }
730
- ],
731
- owner: {
732
- name: 'Support Team',
733
- extensionType: 'User',
734
- extensionId: '12345'
735
- }
736
- };
737
-
738
- describe('Plain Text format', () => {
739
- test('should include proper separators', () => {
740
- const result = composeSharedSMSLog({
741
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
742
- conversation: testConversation,
743
- contactName: 'John Customer',
744
- timezoneOffset: '+00:00'
745
- });
746
-
747
- expect(result.body).toContain('------------');
748
- expect(result.body).toContain('BEGIN');
749
- expect(result.body).toContain('END');
750
- });
751
-
752
- test('should use asterisks for list items', () => {
753
- const result = composeSharedSMSLog({
754
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
755
- conversation: testConversation,
756
- contactName: 'John Customer',
757
- timezoneOffset: '+00:00'
758
- });
759
-
760
- expect(result.body).toContain('* John Customer (customer)');
761
- });
762
- });
763
-
764
- describe('HTML format', () => {
765
- test('should include proper HTML tags', () => {
766
- const result = composeSharedSMSLog({
767
- logFormat: LOG_DETAILS_FORMAT_TYPE.HTML,
768
- conversation: testConversation,
769
- contactName: 'John Customer',
770
- timezoneOffset: '+00:00'
771
- });
772
-
773
- expect(result.body).toContain('<div>');
774
- expect(result.body).toContain('<ul>');
775
- expect(result.body).toContain('<li>');
776
- expect(result.body).toContain('<hr>');
777
- });
778
-
779
- test('should use bold tags for headers', () => {
780
- const result = composeSharedSMSLog({
781
- logFormat: LOG_DETAILS_FORMAT_TYPE.HTML,
782
- conversation: testConversation,
783
- contactName: 'John Customer',
784
- timezoneOffset: '+00:00'
785
- });
786
-
787
- expect(result.body).toContain('<b>Conversation summary</b>');
788
- expect(result.body).toContain('<b>Participants</b>');
789
- });
790
-
791
- test('should escape HTML in content', () => {
792
- const conversationWithSpecialChars = {
793
- ...testConversation,
794
- entities: [
795
- {
796
- recordType: 'AliveMessage',
797
- creationTime: '2024-01-15T10:30:00Z',
798
- direction: 'Inbound',
799
- author: { name: '<script>alert("XSS")</script>' },
800
- text: 'Test <b>bold</b>'
801
- }
802
- ]
803
- };
804
-
805
- const result = composeSharedSMSLog({
806
- logFormat: LOG_DETAILS_FORMAT_TYPE.HTML,
807
- conversation: conversationWithSpecialChars,
808
- contactName: 'John Customer',
809
- timezoneOffset: '+00:00'
810
- });
811
-
812
- expect(result.body).toContain('&lt;script&gt;');
813
- expect(result.body).toContain('&lt;b&gt;bold&lt;/b&gt;');
814
- });
815
- });
816
-
817
- describe('Markdown format', () => {
818
- test('should include proper Markdown headers', () => {
819
- const result = composeSharedSMSLog({
820
- logFormat: LOG_DETAILS_FORMAT_TYPE.MARKDOWN,
821
- conversation: testConversation,
822
- contactName: 'John Customer',
823
- timezoneOffset: '+00:00'
824
- });
825
-
826
- expect(result.body).toContain('## Conversation summary');
827
- expect(result.body).toContain('### Participants');
828
- });
829
-
830
- test('should use horizontal rules', () => {
831
- const result = composeSharedSMSLog({
832
- logFormat: LOG_DETAILS_FORMAT_TYPE.MARKDOWN,
833
- conversation: testConversation,
834
- contactName: 'John Customer',
835
- timezoneOffset: '+00:00'
836
- });
837
-
838
- expect(result.body).toContain('---');
839
- });
840
-
841
- test('should use asterisks for list items', () => {
842
- const result = composeSharedSMSLog({
843
- logFormat: LOG_DETAILS_FORMAT_TYPE.MARKDOWN,
844
- conversation: testConversation,
845
- contactName: 'John Customer',
846
- timezoneOffset: '+00:00'
847
- });
848
-
849
- expect(result.body).toContain('* John Customer (customer)');
850
- });
851
-
852
- test('should use bold for owner name', () => {
853
- const result = composeSharedSMSLog({
854
- logFormat: LOG_DETAILS_FORMAT_TYPE.MARKDOWN,
855
- conversation: testConversation,
856
- contactName: 'John Customer',
857
- timezoneOffset: '+00:00'
858
- });
859
-
860
- expect(result.body).toContain('**Support Team**');
861
- });
862
- });
863
- });
864
-
865
- describe('Edge Cases', () => {
866
- test('should handle conversation with no owner', () => {
867
- const conversationNoOwner = {
868
- creationTime: '2024-01-15T10:30:00Z',
869
- messages: [{ lastModifiedTime: '2024-01-15T11:45:00Z' }],
870
- entities: []
871
- };
872
-
873
- const result = composeSharedSMSLog({
874
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
875
- conversation: conversationNoOwner,
876
- contactName: 'John Customer',
877
- timezoneOffset: '+00:00'
878
- });
879
-
880
- expect(result.body).not.toContain('Owner:');
881
- expect(result.body).not.toContain('Receiving call queue:');
882
- });
883
-
884
- test('should handle message with subject instead of text', () => {
885
- const conversationWithSubject = {
886
- creationTime: '2024-01-15T10:30:00Z',
887
- messages: [{ lastModifiedTime: '2024-01-15T11:45:00Z' }],
888
- entities: [
889
- {
890
- recordType: 'AliveMessage',
891
- creationTime: '2024-01-15T10:30:00Z',
892
- direction: 'Inbound',
893
- author: { name: 'Customer' },
894
- subject: 'Subject line message'
895
- }
896
- ]
897
- };
898
-
899
- const result = composeSharedSMSLog({
900
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
901
- conversation: conversationWithSubject,
902
- contactName: 'Customer',
903
- timezoneOffset: '+00:00'
904
- });
905
-
906
- expect(result.body).toContain('Subject line message');
907
- });
908
-
909
- test('should handle note with body instead of text', () => {
910
- const conversationWithBody = {
911
- creationTime: '2024-01-15T10:30:00Z',
912
- messages: [{ lastModifiedTime: '2024-01-15T11:45:00Z' }],
913
- entities: [
914
- {
915
- recordType: 'AliveNote',
916
- creationTime: '2024-01-15T10:30:00Z',
917
- author: { name: 'Agent' },
918
- body: 'Note body content'
919
- }
920
- ]
921
- };
922
-
923
- const result = composeSharedSMSLog({
924
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
925
- conversation: conversationWithBody,
926
- contactName: 'Customer',
927
- timezoneOffset: '+00:00'
928
- });
929
-
930
- expect(result.body).toContain('Note body content');
931
- });
932
-
933
- test('should handle unknown assignee', () => {
934
- const conversationUnknownAssignee = {
935
- creationTime: '2024-01-15T10:30:00Z',
936
- messages: [{ lastModifiedTime: '2024-01-15T11:45:00Z' }],
937
- entities: [
938
- {
939
- recordType: 'ThreadAssignedHint',
940
- creationTime: '2024-01-15T10:30:00Z'
941
- }
942
- ]
943
- };
944
-
945
- const result = composeSharedSMSLog({
946
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
947
- conversation: conversationUnknownAssignee,
948
- contactName: 'Customer',
949
- timezoneOffset: '+00:00'
950
- });
951
-
952
- expect(result.body).toContain('assigned to Unknown');
953
- });
954
-
955
- test('should handle unknown note author', () => {
956
- const conversationUnknownAuthor = {
957
- creationTime: '2024-01-15T10:30:00Z',
958
- messages: [{ lastModifiedTime: '2024-01-15T11:45:00Z' }],
959
- entities: [
960
- {
961
- recordType: 'AliveNote',
962
- creationTime: '2024-01-15T10:30:00Z',
963
- text: 'Some note'
964
- }
965
- ]
966
- };
967
-
968
- const result = composeSharedSMSLog({
969
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
970
- conversation: conversationUnknownAuthor,
971
- contactName: 'Customer',
972
- timezoneOffset: '+00:00'
973
- });
974
-
975
- expect(result.body).toContain('Unknown left a note');
976
- });
977
-
978
- test('should handle missing timezone offset', () => {
979
- const conversation = {
980
- creationTime: '2024-01-15T10:30:00Z',
981
- messages: [{ lastModifiedTime: '2024-01-15T11:45:00Z' }],
982
- entities: []
983
- };
984
-
985
- const result = composeSharedSMSLog({
986
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
987
- conversation,
988
- contactName: 'Customer'
989
- });
990
-
991
- expect(result.subject).toBe('SMS conversation with Customer');
992
- expect(result.body).toContain('Conversation summary');
993
- });
994
-
995
- test('should default to plain text format', () => {
996
- const conversation = {
997
- creationTime: '2024-01-15T10:30:00Z',
998
- messages: [{ lastModifiedTime: '2024-01-15T11:45:00Z' }],
999
- entities: []
1000
- };
1001
-
1002
- const result = composeSharedSMSLog({
1003
- conversation,
1004
- contactName: 'Customer'
1005
- });
1006
-
1007
- expect(result.subject).toBe('SMS conversation with Customer');
1008
- expect(result.body).not.toContain('<b>');
1009
- expect(result.body).not.toContain('##');
1010
- });
1011
-
1012
- test('should handle owner name with queue keyword', () => {
1013
- const conversationWithQueueName = {
1014
- creationTime: '2024-01-15T10:30:00Z',
1015
- messages: [{ lastModifiedTime: '2024-01-15T11:45:00Z' }],
1016
- entities: [],
1017
- owner: {
1018
- name: 'Support Queue',
1019
- extensionType: 'User',
1020
- extensionId: '12345'
1021
- }
1022
- };
1023
-
1024
- const result = composeSharedSMSLog({
1025
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
1026
- conversation: conversationWithQueueName,
1027
- contactName: 'Customer',
1028
- timezoneOffset: '+00:00'
1029
- });
1030
-
1031
- expect(result.body).toContain('Receiving call queue: Support Queue');
1032
- });
1033
-
1034
- test('should handle message from from.name instead of author.name', () => {
1035
- const conversationWithFromName = {
1036
- creationTime: '2024-01-15T10:30:00Z',
1037
- messages: [{ lastModifiedTime: '2024-01-15T11:45:00Z' }],
1038
- entities: [
1039
- {
1040
- recordType: 'AliveMessage',
1041
- creationTime: '2024-01-15T10:30:00Z',
1042
- direction: 'Outbound',
1043
- from: { name: 'Agent via from' },
1044
- text: 'Hello!'
1045
- }
1046
- ]
1047
- };
1048
-
1049
- const result = composeSharedSMSLog({
1050
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
1051
- conversation: conversationWithFromName,
1052
- contactName: 'Customer',
1053
- timezoneOffset: '+00:00'
1054
- });
1055
-
1056
- expect(result.body).toContain('Agent via from');
1057
- });
1058
-
1059
- test('should handle note initiator as fallback for author', () => {
1060
- const conversationWithInitiator = {
1061
- creationTime: '2024-01-15T10:30:00Z',
1062
- messages: [{ lastModifiedTime: '2024-01-15T11:45:00Z' }],
1063
- entities: [
1064
- {
1065
- recordType: 'AliveNote',
1066
- creationTime: '2024-01-15T10:30:00Z',
1067
- initiator: { name: 'Note Initiator' },
1068
- text: 'A note'
1069
- }
1070
- ]
1071
- };
1072
-
1073
- const result = composeSharedSMSLog({
1074
- logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
1075
- conversation: conversationWithInitiator,
1076
- contactName: 'Customer',
1077
- timezoneOffset: '+00:00'
1078
- });
1079
-
1080
- expect(result.body).toContain('Note Initiator left a note');
1081
- });
1082
- });
1083
- });
1084
-
1
+ const {
2
+ composeSharedSMSLog,
3
+ gatherParticipants,
4
+ countEntities,
5
+ processEntities,
6
+ escapeHtml
7
+ } = require('../../lib/sharedSMSComposer');
8
+ const { LOG_DETAILS_FORMAT_TYPE } = require('../../lib/constants');
9
+
10
+ describe('sharedSMSComposer', () => {
11
+ describe('composeSharedSMSLog', () => {
12
+ const baseConversation = {
13
+ creationTime: '2024-01-15T10:30:00Z',
14
+ messages: [
15
+ { lastModifiedTime: '2024-01-15T10:30:00Z' },
16
+ { lastModifiedTime: '2024-01-15T11:45:00Z' }
17
+ ],
18
+ entities: [
19
+ {
20
+ recordType: 'AliveMessage',
21
+ creationTime: '2024-01-15T10:30:00Z',
22
+ direction: 'Inbound',
23
+ author: { name: 'John Customer' },
24
+ text: 'Hello, I need help'
25
+ },
26
+ {
27
+ recordType: 'AliveMessage',
28
+ creationTime: '2024-01-15T10:35:00Z',
29
+ direction: 'Outbound',
30
+ author: { name: 'Agent Smith' },
31
+ text: 'Hi! How can I assist you?'
32
+ }
33
+ ],
34
+ owner: {
35
+ name: 'Support Team',
36
+ extensionType: 'User',
37
+ extensionId: '12345'
38
+ }
39
+ };
40
+
41
+ test('should compose SMS log with default settings (plain text)', () => {
42
+ const result = composeSharedSMSLog({
43
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
44
+ conversation: baseConversation,
45
+ contactName: 'John Customer',
46
+ timezoneOffset: '+00:00'
47
+ });
48
+
49
+ expect(result.subject).toBe('SMS conversation with John Customer');
50
+ expect(result.body).toContain('Conversation summary');
51
+ expect(result.body).toContain('John Customer (customer)');
52
+ expect(result.body).toContain('BEGIN');
53
+ expect(result.body).toContain('END');
54
+ });
55
+
56
+ test('should compose SMS log in HTML format', () => {
57
+ const result = composeSharedSMSLog({
58
+ logFormat: LOG_DETAILS_FORMAT_TYPE.HTML,
59
+ conversation: baseConversation,
60
+ contactName: 'John Customer',
61
+ timezoneOffset: '+00:00'
62
+ });
63
+
64
+ expect(result.subject).toBe('SMS conversation with John Customer');
65
+ expect(result.body).toContain('<b>Conversation summary</b>');
66
+ expect(result.body).toContain('<b>Participants</b>');
67
+ expect(result.body).toContain('<li>');
68
+ });
69
+
70
+ test('should compose SMS log in Markdown format', () => {
71
+ const result = composeSharedSMSLog({
72
+ logFormat: LOG_DETAILS_FORMAT_TYPE.MARKDOWN,
73
+ conversation: baseConversation,
74
+ contactName: 'John Customer',
75
+ timezoneOffset: '+00:00'
76
+ });
77
+
78
+ expect(result.subject).toBe('**SMS conversation with John Customer**');
79
+ expect(result.body).toContain('## Conversation summary');
80
+ expect(result.body).toContain('### Participants');
81
+ expect(result.body).toContain('---');
82
+ });
83
+
84
+ test('should handle conversation with call queue owner', () => {
85
+ const conversationWithQueue = {
86
+ ...baseConversation,
87
+ owner: {
88
+ name: 'Sales Queue',
89
+ extensionType: 'Department',
90
+ extensionId: '99999'
91
+ }
92
+ };
93
+
94
+ const result = composeSharedSMSLog({
95
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
96
+ conversation: conversationWithQueue,
97
+ contactName: 'John Customer',
98
+ timezoneOffset: '+00:00'
99
+ });
100
+
101
+ expect(result.body).toContain('Receiving call queue: Sales Queue');
102
+ });
103
+
104
+ test('should handle conversation with notes', () => {
105
+ const conversationWithNotes = {
106
+ ...baseConversation,
107
+ entities: [
108
+ ...baseConversation.entities,
109
+ {
110
+ recordType: 'AliveNote',
111
+ creationTime: '2024-01-15T10:40:00Z',
112
+ author: { name: 'Agent Smith' },
113
+ text: 'Customer prefers email contact'
114
+ }
115
+ ]
116
+ };
117
+
118
+ const result = composeSharedSMSLog({
119
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
120
+ conversation: conversationWithNotes,
121
+ contactName: 'John Customer',
122
+ timezoneOffset: '+00:00'
123
+ });
124
+
125
+ expect(result.body).toContain('2 messages');
126
+ expect(result.body).toContain('1 note');
127
+ expect(result.body).toContain('left a note');
128
+ });
129
+
130
+ test('should handle conversation with assignment', () => {
131
+ const conversationWithAssignment = {
132
+ ...baseConversation,
133
+ entities: [
134
+ ...baseConversation.entities,
135
+ {
136
+ recordType: 'ThreadAssignedHint',
137
+ creationTime: '2024-01-15T10:32:00Z',
138
+ assignee: { name: 'Agent Smith' }
139
+ }
140
+ ]
141
+ };
142
+
143
+ const result = composeSharedSMSLog({
144
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
145
+ conversation: conversationWithAssignment,
146
+ contactName: 'John Customer',
147
+ timezoneOffset: '+00:00'
148
+ });
149
+
150
+ expect(result.body).toContain('Conversation assigned to Agent Smith');
151
+ });
152
+
153
+ test('should skip ThreadResolvedHint entities (not processed)', () => {
154
+ const conversationWithResolved = {
155
+ ...baseConversation,
156
+ entities: [
157
+ ...baseConversation.entities,
158
+ {
159
+ recordType: 'ThreadResolvedHint',
160
+ creationTime: '2024-01-15T11:45:00Z',
161
+ initiator: { name: 'Agent Smith' }
162
+ }
163
+ ]
164
+ };
165
+
166
+ const result = composeSharedSMSLog({
167
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
168
+ conversation: conversationWithResolved,
169
+ contactName: 'John Customer',
170
+ timezoneOffset: '+00:00'
171
+ });
172
+
173
+ // ThreadResolvedHint is not processed, so it won't appear in the body
174
+ expect(result.body).not.toContain('resolved the conversation');
175
+ });
176
+
177
+ test('should skip ThreadReopenedHint entities (not processed)', () => {
178
+ const conversationWithReopened = {
179
+ ...baseConversation,
180
+ entities: [
181
+ ...baseConversation.entities,
182
+ {
183
+ recordType: 'ThreadReopenedHint',
184
+ creationTime: '2024-01-15T12:00:00Z',
185
+ initiator: { name: 'Agent Smith' }
186
+ }
187
+ ]
188
+ };
189
+
190
+ const result = composeSharedSMSLog({
191
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
192
+ conversation: conversationWithReopened,
193
+ contactName: 'John Customer',
194
+ timezoneOffset: '+00:00'
195
+ });
196
+
197
+ // ThreadReopenedHint is not processed, so it won't appear in the body
198
+ expect(result.body).not.toContain('reopened the conversation');
199
+ });
200
+
201
+ test('should apply timezone offset', () => {
202
+ const result = composeSharedSMSLog({
203
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
204
+ conversation: baseConversation,
205
+ contactName: 'John Customer',
206
+ timezoneOffset: '+05:00'
207
+ });
208
+
209
+ // The time should be adjusted by +5 hours
210
+ expect(result.body).toContain('Started:');
211
+ });
212
+
213
+ test('should handle empty entities array', () => {
214
+ const emptyConversation = {
215
+ creationTime: '2024-01-15T10:30:00Z',
216
+ messages: [{ lastModifiedTime: '2024-01-15T11:45:00Z' }],
217
+ entities: []
218
+ };
219
+
220
+ const result = composeSharedSMSLog({
221
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
222
+ conversation: emptyConversation,
223
+ contactName: 'John Customer',
224
+ timezoneOffset: '+00:00'
225
+ });
226
+
227
+ expect(result.body).toContain('0 messages');
228
+ });
229
+
230
+ test('should handle missing entities', () => {
231
+ const noEntitiesConversation = {
232
+ creationTime: '2024-01-15T10:30:00Z',
233
+ messages: [{ lastModifiedTime: '2024-01-15T11:45:00Z' }]
234
+ };
235
+
236
+ const result = composeSharedSMSLog({
237
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
238
+ conversation: noEntitiesConversation,
239
+ contactName: 'John Customer',
240
+ timezoneOffset: '+00:00'
241
+ });
242
+
243
+ expect(result.body).toContain('0 messages');
244
+ });
245
+ });
246
+
247
+ describe('gatherParticipants', () => {
248
+ test('should gather participants from author names', () => {
249
+ const entities = [
250
+ { author: { name: 'Agent Smith' } },
251
+ { author: { name: 'Agent Jones' } }
252
+ ];
253
+
254
+ const result = gatherParticipants(entities);
255
+
256
+ expect(result).toContain('Agent Smith');
257
+ expect(result).toContain('Agent Jones');
258
+ });
259
+
260
+ test('should gather participants from from names', () => {
261
+ const entities = [
262
+ { from: { name: 'Customer John' } }
263
+ ];
264
+
265
+ const result = gatherParticipants(entities);
266
+
267
+ expect(result).toContain('Customer John');
268
+ });
269
+
270
+ test('should gather participants from initiator names', () => {
271
+ const entities = [
272
+ { initiator: { name: 'Manager Bob' } }
273
+ ];
274
+
275
+ const result = gatherParticipants(entities);
276
+
277
+ expect(result).toContain('Manager Bob');
278
+ });
279
+
280
+ test('should gather participants from assignee names', () => {
281
+ const entities = [
282
+ { assignee: { name: 'Agent Smith' } }
283
+ ];
284
+
285
+ const result = gatherParticipants(entities);
286
+
287
+ expect(result).toContain('Agent Smith');
288
+ });
289
+
290
+ test('should deduplicate participants', () => {
291
+ const entities = [
292
+ { author: { name: 'Agent Smith' } },
293
+ { from: { name: 'Agent Smith' } },
294
+ { initiator: { name: 'Agent Smith' } }
295
+ ];
296
+
297
+ const result = gatherParticipants(entities);
298
+
299
+ expect(result).toHaveLength(1);
300
+ expect(result).toContain('Agent Smith');
301
+ });
302
+
303
+ test('should handle empty entities array', () => {
304
+ const result = gatherParticipants([]);
305
+
306
+ expect(result).toEqual([]);
307
+ });
308
+
309
+ test('should handle entities without names', () => {
310
+ const entities = [
311
+ { author: {} },
312
+ { from: null },
313
+ {}
314
+ ];
315
+
316
+ const result = gatherParticipants(entities);
317
+
318
+ expect(result).toEqual([]);
319
+ });
320
+ });
321
+
322
+ describe('countEntities', () => {
323
+ test('should count messages correctly', () => {
324
+ const entities = [
325
+ { recordType: 'AliveMessage' },
326
+ { recordType: 'AliveMessage' },
327
+ { recordType: 'AliveMessage' }
328
+ ];
329
+
330
+ const result = countEntities(entities);
331
+
332
+ expect(result.messageCount).toBe(3);
333
+ expect(result.noteCount).toBe(0);
334
+ });
335
+
336
+ test('should count AliveNote as note', () => {
337
+ const entities = [
338
+ { recordType: 'AliveNote' },
339
+ { recordType: 'AliveNote' }
340
+ ];
341
+
342
+ const result = countEntities(entities);
343
+
344
+ expect(result.messageCount).toBe(0);
345
+ expect(result.noteCount).toBe(2);
346
+ });
347
+
348
+ test('should not count NoteHint as note (only AliveNote is counted)', () => {
349
+ const entities = [
350
+ { recordType: 'NoteHint' },
351
+ { recordType: 'ThreadNoteAddedHint' }
352
+ ];
353
+
354
+ const result = countEntities(entities);
355
+
356
+ expect(result.messageCount).toBe(0);
357
+ expect(result.noteCount).toBe(0);
358
+ });
359
+
360
+ test('should not count ThreadAssignedHint as note', () => {
361
+ const entities = [
362
+ { recordType: 'ThreadAssignedHint' }
363
+ ];
364
+
365
+ const result = countEntities(entities);
366
+
367
+ expect(result.noteCount).toBe(0);
368
+ });
369
+
370
+ test('should count both messages and notes', () => {
371
+ const entities = [
372
+ { recordType: 'AliveMessage' },
373
+ { recordType: 'AliveMessage' },
374
+ { recordType: 'AliveNote' },
375
+ { recordType: 'AliveNote' }
376
+ ];
377
+
378
+ const result = countEntities(entities);
379
+
380
+ expect(result.messageCount).toBe(2);
381
+ expect(result.noteCount).toBe(2);
382
+ });
383
+
384
+ test('should return zero counts for empty array', () => {
385
+ const result = countEntities([]);
386
+
387
+ expect(result.messageCount).toBe(0);
388
+ expect(result.noteCount).toBe(0);
389
+ });
390
+
391
+ test('should ignore other record types', () => {
392
+ const entities = [
393
+ { recordType: 'ThreadResolvedHint' },
394
+ { recordType: 'ThreadReopenedHint' },
395
+ { recordType: 'ThreadCreatedHint' },
396
+ { recordType: 'NoteHint' },
397
+ { recordType: 'ThreadNoteAddedHint' },
398
+ { recordType: 'ThreadAssignedHint' }
399
+ ];
400
+
401
+ const result = countEntities(entities);
402
+
403
+ expect(result.messageCount).toBe(0);
404
+ expect(result.noteCount).toBe(0);
405
+ });
406
+ });
407
+
408
+ describe('processEntities', () => {
409
+ test('should process message entities (plain text)', () => {
410
+ const entities = [
411
+ {
412
+ recordType: 'AliveMessage',
413
+ creationTime: '2024-01-15T10:30:00Z',
414
+ direction: 'Inbound',
415
+ author: { name: 'Customer' },
416
+ text: 'Hello!'
417
+ }
418
+ ];
419
+
420
+ const result = processEntities({
421
+ entities,
422
+ timezoneOffset: '+00:00',
423
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
424
+ contactName: 'Customer'
425
+ });
426
+
427
+ expect(result).toHaveLength(1);
428
+ expect(result[0].type).toBe('message');
429
+ expect(result[0].content).toContain('said on');
430
+ expect(result[0].content).toContain('Hello!');
431
+ });
432
+
433
+ test('should process message entities (HTML)', () => {
434
+ const entities = [
435
+ {
436
+ recordType: 'AliveMessage',
437
+ creationTime: '2024-01-15T10:30:00Z',
438
+ direction: 'Outbound',
439
+ author: { name: 'Agent' },
440
+ text: 'Hi there!'
441
+ }
442
+ ];
443
+
444
+ const result = processEntities({
445
+ entities,
446
+ timezoneOffset: '+00:00',
447
+ logFormat: LOG_DETAILS_FORMAT_TYPE.HTML,
448
+ contactName: 'Customer'
449
+ });
450
+
451
+ expect(result).toHaveLength(1);
452
+ expect(result[0].content).toContain('<p>');
453
+ expect(result[0].content).toContain('<b>');
454
+ });
455
+
456
+ test('should process message entities (Markdown)', () => {
457
+ const entities = [
458
+ {
459
+ recordType: 'AliveMessage',
460
+ creationTime: '2024-01-15T10:30:00Z',
461
+ direction: 'Outbound',
462
+ author: { name: 'Agent' },
463
+ text: 'Hi there!'
464
+ }
465
+ ];
466
+
467
+ const result = processEntities({
468
+ entities,
469
+ timezoneOffset: '+00:00',
470
+ logFormat: LOG_DETAILS_FORMAT_TYPE.MARKDOWN,
471
+ contactName: 'Customer'
472
+ });
473
+
474
+ expect(result).toHaveLength(1);
475
+ expect(result[0].content).toContain('**');
476
+ });
477
+
478
+ test('should process assignment entities', () => {
479
+ const entities = [
480
+ {
481
+ recordType: 'ThreadAssignedHint',
482
+ creationTime: '2024-01-15T10:30:00Z',
483
+ assignee: { name: 'Agent Smith' }
484
+ }
485
+ ];
486
+
487
+ const result = processEntities({
488
+ entities,
489
+ timezoneOffset: '+00:00',
490
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
491
+ contactName: 'Customer'
492
+ });
493
+
494
+ expect(result).toHaveLength(1);
495
+ expect(result[0].type).toBe('assignment');
496
+ expect(result[0].content).toContain('assigned to Agent Smith');
497
+ });
498
+
499
+ test('should skip ThreadResolvedHint entities', () => {
500
+ const entities = [
501
+ {
502
+ recordType: 'ThreadResolvedHint',
503
+ creationTime: '2024-01-15T10:30:00Z',
504
+ initiator: { name: 'Agent Smith' }
505
+ }
506
+ ];
507
+
508
+ const result = processEntities({
509
+ entities,
510
+ timezoneOffset: '+00:00',
511
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
512
+ contactName: 'Customer'
513
+ });
514
+
515
+ // ThreadResolvedHint is not processed (returns null)
516
+ expect(result).toHaveLength(0);
517
+ });
518
+
519
+ test('should skip ThreadReopenedHint entities', () => {
520
+ const entities = [
521
+ {
522
+ recordType: 'ThreadReopenedHint',
523
+ creationTime: '2024-01-15T10:30:00Z',
524
+ initiator: { name: 'Agent Smith' }
525
+ }
526
+ ];
527
+
528
+ const result = processEntities({
529
+ entities,
530
+ timezoneOffset: '+00:00',
531
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
532
+ contactName: 'Customer'
533
+ });
534
+
535
+ // ThreadReopenedHint is not processed (returns null)
536
+ expect(result).toHaveLength(0);
537
+ });
538
+
539
+ test('should process AliveNote entities', () => {
540
+ const entities = [
541
+ {
542
+ recordType: 'AliveNote',
543
+ creationTime: '2024-01-15T10:30:00Z',
544
+ author: { name: 'Agent Smith' },
545
+ text: 'Important note here'
546
+ }
547
+ ];
548
+
549
+ const result = processEntities({
550
+ entities,
551
+ timezoneOffset: '+00:00',
552
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
553
+ contactName: 'Customer'
554
+ });
555
+
556
+ expect(result).toHaveLength(1);
557
+ expect(result[0].type).toBe('note');
558
+ expect(result[0].content).toContain('left a note');
559
+ expect(result[0].content).toContain('Important note here');
560
+ });
561
+
562
+ test('should skip NoteHint entities (not processed)', () => {
563
+ const entities = [
564
+ {
565
+ recordType: 'NoteHint',
566
+ creationTime: '2024-01-15T10:30:00Z',
567
+ author: { name: 'Agent Smith' },
568
+ text: 'Important note here'
569
+ }
570
+ ];
571
+
572
+ const result = processEntities({
573
+ entities,
574
+ timezoneOffset: '+00:00',
575
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
576
+ contactName: 'Customer'
577
+ });
578
+
579
+ // NoteHint is counted but not processed into formatted entries
580
+ expect(result).toHaveLength(0);
581
+ });
582
+
583
+ test('should skip ThreadCreatedHint entities', () => {
584
+ const entities = [
585
+ {
586
+ recordType: 'ThreadCreatedHint',
587
+ creationTime: '2024-01-15T10:30:00Z'
588
+ }
589
+ ];
590
+
591
+ const result = processEntities({
592
+ entities,
593
+ timezoneOffset: '+00:00',
594
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
595
+ contactName: 'Customer'
596
+ });
597
+
598
+ expect(result).toHaveLength(0);
599
+ });
600
+
601
+ test('should process multiple message entities', () => {
602
+ const entities = [
603
+ {
604
+ recordType: 'AliveMessage',
605
+ creationTime: '2024-01-15T10:00:00Z',
606
+ direction: 'Outbound',
607
+ author: { name: 'Agent' },
608
+ text: 'First message'
609
+ },
610
+ {
611
+ recordType: 'AliveMessage',
612
+ creationTime: '2024-01-15T12:00:00Z',
613
+ direction: 'Outbound',
614
+ author: { name: 'Agent' },
615
+ text: 'Third message'
616
+ },
617
+ {
618
+ recordType: 'AliveMessage',
619
+ creationTime: '2024-01-15T11:00:00Z',
620
+ direction: 'Outbound',
621
+ author: { name: 'Agent' },
622
+ text: 'Second message'
623
+ }
624
+ ];
625
+
626
+ const result = processEntities({
627
+ entities,
628
+ timezoneOffset: '+00:00',
629
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
630
+ contactName: 'Customer'
631
+ });
632
+
633
+ expect(result).toHaveLength(3);
634
+ // Verify all messages are processed
635
+ const allContent = result.map(r => r.content).join(' ');
636
+ expect(allContent).toContain('First message');
637
+ expect(allContent).toContain('Second message');
638
+ expect(allContent).toContain('Third message');
639
+ });
640
+
641
+ test('should apply timezone offset to timestamps', () => {
642
+ const entities = [
643
+ {
644
+ recordType: 'AliveMessage',
645
+ creationTime: '2024-01-15T10:30:00Z',
646
+ direction: 'Outbound',
647
+ author: { name: 'Agent' },
648
+ text: 'Hello!'
649
+ }
650
+ ];
651
+
652
+ const result = processEntities({
653
+ entities,
654
+ timezoneOffset: '+05:00',
655
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
656
+ contactName: 'Customer'
657
+ });
658
+
659
+ // 10:30 UTC + 5 hours = 15:30 (3:30 PM)
660
+ expect(result[0].content).toContain('03:30 PM');
661
+ });
662
+
663
+ test('should handle empty entities array', () => {
664
+ const result = processEntities({
665
+ entities: [],
666
+ timezoneOffset: '+00:00',
667
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
668
+ contactName: 'Customer'
669
+ });
670
+
671
+ expect(result).toEqual([]);
672
+ });
673
+ });
674
+
675
+ describe('escapeHtml', () => {
676
+ test('should escape ampersand', () => {
677
+ expect(escapeHtml('Tom & Jerry')).toBe('Tom &amp; Jerry');
678
+ });
679
+
680
+ test('should escape less than', () => {
681
+ expect(escapeHtml('a < b')).toBe('a &lt; b');
682
+ });
683
+
684
+ test('should escape greater than', () => {
685
+ expect(escapeHtml('a > b')).toBe('a &gt; b');
686
+ });
687
+
688
+ test('should escape double quotes', () => {
689
+ expect(escapeHtml('say "hello"')).toBe('say &quot;hello&quot;');
690
+ });
691
+
692
+ test('should escape single quotes', () => {
693
+ expect(escapeHtml("it's fine")).toBe('it&#039;s fine');
694
+ });
695
+
696
+ test('should escape multiple special characters', () => {
697
+ expect(escapeHtml('<script>alert("XSS")</script>'))
698
+ .toBe('&lt;script&gt;alert(&quot;XSS&quot;)&lt;/script&gt;');
699
+ });
700
+
701
+ test('should return empty string for null input', () => {
702
+ expect(escapeHtml(null)).toBe('');
703
+ });
704
+
705
+ test('should return empty string for undefined input', () => {
706
+ expect(escapeHtml(undefined)).toBe('');
707
+ });
708
+
709
+ test('should return empty string for empty string input', () => {
710
+ expect(escapeHtml('')).toBe('');
711
+ });
712
+
713
+ test('should not modify text without special characters', () => {
714
+ expect(escapeHtml('Hello World')).toBe('Hello World');
715
+ });
716
+ });
717
+
718
+ describe('Format-specific output', () => {
719
+ const testConversation = {
720
+ creationTime: '2024-01-15T10:30:00Z',
721
+ messages: [{ lastModifiedTime: '2024-01-15T11:45:00Z' }],
722
+ entities: [
723
+ {
724
+ recordType: 'AliveMessage',
725
+ creationTime: '2024-01-15T10:30:00Z',
726
+ direction: 'Inbound',
727
+ author: { name: 'John Customer' },
728
+ text: 'Hello!'
729
+ }
730
+ ],
731
+ owner: {
732
+ name: 'Support Team',
733
+ extensionType: 'User',
734
+ extensionId: '12345'
735
+ }
736
+ };
737
+
738
+ describe('Plain Text format', () => {
739
+ test('should include proper separators', () => {
740
+ const result = composeSharedSMSLog({
741
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
742
+ conversation: testConversation,
743
+ contactName: 'John Customer',
744
+ timezoneOffset: '+00:00'
745
+ });
746
+
747
+ expect(result.body).toContain('------------');
748
+ expect(result.body).toContain('BEGIN');
749
+ expect(result.body).toContain('END');
750
+ });
751
+
752
+ test('should use asterisks for list items', () => {
753
+ const result = composeSharedSMSLog({
754
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
755
+ conversation: testConversation,
756
+ contactName: 'John Customer',
757
+ timezoneOffset: '+00:00'
758
+ });
759
+
760
+ expect(result.body).toContain('* John Customer (customer)');
761
+ });
762
+ });
763
+
764
+ describe('HTML format', () => {
765
+ test('should include proper HTML tags', () => {
766
+ const result = composeSharedSMSLog({
767
+ logFormat: LOG_DETAILS_FORMAT_TYPE.HTML,
768
+ conversation: testConversation,
769
+ contactName: 'John Customer',
770
+ timezoneOffset: '+00:00'
771
+ });
772
+
773
+ expect(result.body).toContain('<div>');
774
+ expect(result.body).toContain('<ul>');
775
+ expect(result.body).toContain('<li>');
776
+ expect(result.body).toContain('<hr>');
777
+ });
778
+
779
+ test('should use bold tags for headers', () => {
780
+ const result = composeSharedSMSLog({
781
+ logFormat: LOG_DETAILS_FORMAT_TYPE.HTML,
782
+ conversation: testConversation,
783
+ contactName: 'John Customer',
784
+ timezoneOffset: '+00:00'
785
+ });
786
+
787
+ expect(result.body).toContain('<b>Conversation summary</b>');
788
+ expect(result.body).toContain('<b>Participants</b>');
789
+ });
790
+
791
+ test('should escape HTML in content', () => {
792
+ const conversationWithSpecialChars = {
793
+ ...testConversation,
794
+ entities: [
795
+ {
796
+ recordType: 'AliveMessage',
797
+ creationTime: '2024-01-15T10:30:00Z',
798
+ direction: 'Inbound',
799
+ author: { name: '<script>alert("XSS")</script>' },
800
+ text: 'Test <b>bold</b>'
801
+ }
802
+ ]
803
+ };
804
+
805
+ const result = composeSharedSMSLog({
806
+ logFormat: LOG_DETAILS_FORMAT_TYPE.HTML,
807
+ conversation: conversationWithSpecialChars,
808
+ contactName: 'John Customer',
809
+ timezoneOffset: '+00:00'
810
+ });
811
+
812
+ expect(result.body).toContain('&lt;script&gt;');
813
+ expect(result.body).toContain('&lt;b&gt;bold&lt;/b&gt;');
814
+ });
815
+ });
816
+
817
+ describe('Markdown format', () => {
818
+ test('should include proper Markdown headers', () => {
819
+ const result = composeSharedSMSLog({
820
+ logFormat: LOG_DETAILS_FORMAT_TYPE.MARKDOWN,
821
+ conversation: testConversation,
822
+ contactName: 'John Customer',
823
+ timezoneOffset: '+00:00'
824
+ });
825
+
826
+ expect(result.body).toContain('## Conversation summary');
827
+ expect(result.body).toContain('### Participants');
828
+ });
829
+
830
+ test('should use horizontal rules', () => {
831
+ const result = composeSharedSMSLog({
832
+ logFormat: LOG_DETAILS_FORMAT_TYPE.MARKDOWN,
833
+ conversation: testConversation,
834
+ contactName: 'John Customer',
835
+ timezoneOffset: '+00:00'
836
+ });
837
+
838
+ expect(result.body).toContain('---');
839
+ });
840
+
841
+ test('should use asterisks for list items', () => {
842
+ const result = composeSharedSMSLog({
843
+ logFormat: LOG_DETAILS_FORMAT_TYPE.MARKDOWN,
844
+ conversation: testConversation,
845
+ contactName: 'John Customer',
846
+ timezoneOffset: '+00:00'
847
+ });
848
+
849
+ expect(result.body).toContain('* John Customer (customer)');
850
+ });
851
+
852
+ test('should use bold for owner name', () => {
853
+ const result = composeSharedSMSLog({
854
+ logFormat: LOG_DETAILS_FORMAT_TYPE.MARKDOWN,
855
+ conversation: testConversation,
856
+ contactName: 'John Customer',
857
+ timezoneOffset: '+00:00'
858
+ });
859
+
860
+ expect(result.body).toContain('**Support Team**');
861
+ });
862
+ });
863
+ });
864
+
865
+ describe('Edge Cases', () => {
866
+ test('should handle conversation with no owner', () => {
867
+ const conversationNoOwner = {
868
+ creationTime: '2024-01-15T10:30:00Z',
869
+ messages: [{ lastModifiedTime: '2024-01-15T11:45:00Z' }],
870
+ entities: []
871
+ };
872
+
873
+ const result = composeSharedSMSLog({
874
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
875
+ conversation: conversationNoOwner,
876
+ contactName: 'John Customer',
877
+ timezoneOffset: '+00:00'
878
+ });
879
+
880
+ expect(result.body).not.toContain('Owner:');
881
+ expect(result.body).not.toContain('Receiving call queue:');
882
+ });
883
+
884
+ test('should handle message with subject instead of text', () => {
885
+ const conversationWithSubject = {
886
+ creationTime: '2024-01-15T10:30:00Z',
887
+ messages: [{ lastModifiedTime: '2024-01-15T11:45:00Z' }],
888
+ entities: [
889
+ {
890
+ recordType: 'AliveMessage',
891
+ creationTime: '2024-01-15T10:30:00Z',
892
+ direction: 'Inbound',
893
+ author: { name: 'Customer' },
894
+ subject: 'Subject line message'
895
+ }
896
+ ]
897
+ };
898
+
899
+ const result = composeSharedSMSLog({
900
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
901
+ conversation: conversationWithSubject,
902
+ contactName: 'Customer',
903
+ timezoneOffset: '+00:00'
904
+ });
905
+
906
+ expect(result.body).toContain('Subject line message');
907
+ });
908
+
909
+ test('should handle note with body instead of text', () => {
910
+ const conversationWithBody = {
911
+ creationTime: '2024-01-15T10:30:00Z',
912
+ messages: [{ lastModifiedTime: '2024-01-15T11:45:00Z' }],
913
+ entities: [
914
+ {
915
+ recordType: 'AliveNote',
916
+ creationTime: '2024-01-15T10:30:00Z',
917
+ author: { name: 'Agent' },
918
+ body: 'Note body content'
919
+ }
920
+ ]
921
+ };
922
+
923
+ const result = composeSharedSMSLog({
924
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
925
+ conversation: conversationWithBody,
926
+ contactName: 'Customer',
927
+ timezoneOffset: '+00:00'
928
+ });
929
+
930
+ expect(result.body).toContain('Note body content');
931
+ });
932
+
933
+ test('should handle unknown assignee', () => {
934
+ const conversationUnknownAssignee = {
935
+ creationTime: '2024-01-15T10:30:00Z',
936
+ messages: [{ lastModifiedTime: '2024-01-15T11:45:00Z' }],
937
+ entities: [
938
+ {
939
+ recordType: 'ThreadAssignedHint',
940
+ creationTime: '2024-01-15T10:30:00Z'
941
+ }
942
+ ]
943
+ };
944
+
945
+ const result = composeSharedSMSLog({
946
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
947
+ conversation: conversationUnknownAssignee,
948
+ contactName: 'Customer',
949
+ timezoneOffset: '+00:00'
950
+ });
951
+
952
+ expect(result.body).toContain('assigned to Unknown');
953
+ });
954
+
955
+ test('should handle unknown note author', () => {
956
+ const conversationUnknownAuthor = {
957
+ creationTime: '2024-01-15T10:30:00Z',
958
+ messages: [{ lastModifiedTime: '2024-01-15T11:45:00Z' }],
959
+ entities: [
960
+ {
961
+ recordType: 'AliveNote',
962
+ creationTime: '2024-01-15T10:30:00Z',
963
+ text: 'Some note'
964
+ }
965
+ ]
966
+ };
967
+
968
+ const result = composeSharedSMSLog({
969
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
970
+ conversation: conversationUnknownAuthor,
971
+ contactName: 'Customer',
972
+ timezoneOffset: '+00:00'
973
+ });
974
+
975
+ expect(result.body).toContain('Unknown left a note');
976
+ });
977
+
978
+ test('should handle missing timezone offset', () => {
979
+ const conversation = {
980
+ creationTime: '2024-01-15T10:30:00Z',
981
+ messages: [{ lastModifiedTime: '2024-01-15T11:45:00Z' }],
982
+ entities: []
983
+ };
984
+
985
+ const result = composeSharedSMSLog({
986
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
987
+ conversation,
988
+ contactName: 'Customer'
989
+ });
990
+
991
+ expect(result.subject).toBe('SMS conversation with Customer');
992
+ expect(result.body).toContain('Conversation summary');
993
+ });
994
+
995
+ test('should default to plain text format', () => {
996
+ const conversation = {
997
+ creationTime: '2024-01-15T10:30:00Z',
998
+ messages: [{ lastModifiedTime: '2024-01-15T11:45:00Z' }],
999
+ entities: []
1000
+ };
1001
+
1002
+ const result = composeSharedSMSLog({
1003
+ conversation,
1004
+ contactName: 'Customer'
1005
+ });
1006
+
1007
+ expect(result.subject).toBe('SMS conversation with Customer');
1008
+ expect(result.body).not.toContain('<b>');
1009
+ expect(result.body).not.toContain('##');
1010
+ });
1011
+
1012
+ test('should handle owner name with queue keyword', () => {
1013
+ const conversationWithQueueName = {
1014
+ creationTime: '2024-01-15T10:30:00Z',
1015
+ messages: [{ lastModifiedTime: '2024-01-15T11:45:00Z' }],
1016
+ entities: [],
1017
+ owner: {
1018
+ name: 'Support Queue',
1019
+ extensionType: 'User',
1020
+ extensionId: '12345'
1021
+ }
1022
+ };
1023
+
1024
+ const result = composeSharedSMSLog({
1025
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
1026
+ conversation: conversationWithQueueName,
1027
+ contactName: 'Customer',
1028
+ timezoneOffset: '+00:00'
1029
+ });
1030
+
1031
+ expect(result.body).toContain('Receiving call queue: Support Queue');
1032
+ });
1033
+
1034
+ test('should handle message from from.name instead of author.name', () => {
1035
+ const conversationWithFromName = {
1036
+ creationTime: '2024-01-15T10:30:00Z',
1037
+ messages: [{ lastModifiedTime: '2024-01-15T11:45:00Z' }],
1038
+ entities: [
1039
+ {
1040
+ recordType: 'AliveMessage',
1041
+ creationTime: '2024-01-15T10:30:00Z',
1042
+ direction: 'Outbound',
1043
+ from: { name: 'Agent via from' },
1044
+ text: 'Hello!'
1045
+ }
1046
+ ]
1047
+ };
1048
+
1049
+ const result = composeSharedSMSLog({
1050
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
1051
+ conversation: conversationWithFromName,
1052
+ contactName: 'Customer',
1053
+ timezoneOffset: '+00:00'
1054
+ });
1055
+
1056
+ expect(result.body).toContain('Agent via from');
1057
+ });
1058
+
1059
+ test('should handle note initiator as fallback for author', () => {
1060
+ const conversationWithInitiator = {
1061
+ creationTime: '2024-01-15T10:30:00Z',
1062
+ messages: [{ lastModifiedTime: '2024-01-15T11:45:00Z' }],
1063
+ entities: [
1064
+ {
1065
+ recordType: 'AliveNote',
1066
+ creationTime: '2024-01-15T10:30:00Z',
1067
+ initiator: { name: 'Note Initiator' },
1068
+ text: 'A note'
1069
+ }
1070
+ ]
1071
+ };
1072
+
1073
+ const result = composeSharedSMSLog({
1074
+ logFormat: LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
1075
+ conversation: conversationWithInitiator,
1076
+ contactName: 'Customer',
1077
+ timezoneOffset: '+00:00'
1078
+ });
1079
+
1080
+ expect(result.body).toContain('Note Initiator left a note');
1081
+ });
1082
+ });
1083
+ });
1084
+