@webex/contact-center 3.12.0-webex-services-ready.4 → 3.13.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.
Files changed (250) hide show
  1. package/.sdd/manifest.json +24 -13
  2. package/ai-docs/ARCHITECTURE.md +1 -1
  3. package/ai-docs/CONTRACTS.md +19 -3
  4. package/ai-docs/SECURITY.md +1 -1
  5. package/ai-docs/SERVICE_STATE.md +4 -0
  6. package/ai-docs/contact-center-spec.md +50 -6
  7. package/ai-docs/features/consult-transfer-list-policy/spec/feature-spec.md +362 -0
  8. package/ai-docs/features/contact-owner-change-refactor.md +78 -0
  9. package/dist/cc.js +757 -22
  10. package/dist/cc.js.map +1 -1
  11. package/dist/config.js +7 -0
  12. package/dist/config.js.map +1 -1
  13. package/dist/constants.js +23 -2
  14. package/dist/constants.js.map +1 -1
  15. package/dist/index.js +34 -12
  16. package/dist/index.js.map +1 -1
  17. package/dist/metrics/MetricsManager.js +16 -0
  18. package/dist/metrics/MetricsManager.js.map +1 -1
  19. package/dist/metrics/behavioral-events.js +110 -0
  20. package/dist/metrics/behavioral-events.js.map +1 -1
  21. package/dist/metrics/constants.js +33 -0
  22. package/dist/metrics/constants.js.map +1 -1
  23. package/dist/services/AddressBook.js +18 -15
  24. package/dist/services/AddressBook.js.map +1 -1
  25. package/dist/services/AnswerCallOnWebexService.js +202 -0
  26. package/dist/services/AnswerCallOnWebexService.js.map +1 -0
  27. package/dist/services/ApiAiAssistant.js +221 -11
  28. package/dist/services/ApiAiAssistant.js.map +1 -1
  29. package/dist/services/EntryPoint.js +46 -68
  30. package/dist/services/EntryPoint.js.map +1 -1
  31. package/dist/services/Queue.js +27 -22
  32. package/dist/services/Queue.js.map +1 -1
  33. package/dist/services/WebCallingService.js +10 -2
  34. package/dist/services/WebCallingService.js.map +1 -1
  35. package/dist/services/WebexCrossClientService.js +225 -0
  36. package/dist/services/WebexCrossClientService.js.map +1 -0
  37. package/dist/services/WxAppTelephonyMercurySync.js +111 -0
  38. package/dist/services/WxAppTelephonyMercurySync.js.map +1 -0
  39. package/dist/services/config/Util.js +46 -46
  40. package/dist/services/config/Util.js.map +1 -1
  41. package/dist/services/config/constants.js +34 -77
  42. package/dist/services/config/constants.js.map +1 -1
  43. package/dist/services/config/index.js +100 -226
  44. package/dist/services/config/index.js.map +1 -1
  45. package/dist/services/config/types.js +51 -18
  46. package/dist/services/config/types.js.map +1 -1
  47. package/dist/services/constants.js +11 -1
  48. package/dist/services/constants.js.map +1 -1
  49. package/dist/services/core/Err.js.map +1 -1
  50. package/dist/services/core/Utils.js +25 -1
  51. package/dist/services/core/Utils.js.map +1 -1
  52. package/dist/services/core/WebexRequest.js +6 -2
  53. package/dist/services/core/WebexRequest.js.map +1 -1
  54. package/dist/services/core/aqm-reqs.js +17 -11
  55. package/dist/services/core/aqm-reqs.js.map +1 -1
  56. package/dist/services/core/constants.js +9 -1
  57. package/dist/services/core/constants.js.map +1 -1
  58. package/dist/services/core/types.js.map +1 -1
  59. package/dist/services/core/websocket/WebSocketManager.js +17 -1
  60. package/dist/services/core/websocket/WebSocketManager.js.map +1 -1
  61. package/dist/services/task/Task.js +275 -8
  62. package/dist/services/task/Task.js.map +1 -1
  63. package/dist/services/task/TaskFactory.js +11 -6
  64. package/dist/services/task/TaskFactory.js.map +1 -1
  65. package/dist/services/task/TaskManager.js +590 -42
  66. package/dist/services/task/TaskManager.js.map +1 -1
  67. package/dist/services/task/TaskUtils.js +11 -1
  68. package/dist/services/task/TaskUtils.js.map +1 -1
  69. package/dist/services/task/WebexCallingUtils.js +70 -0
  70. package/dist/services/task/WebexCallingUtils.js.map +1 -0
  71. package/dist/services/task/constants.js +14 -2
  72. package/dist/services/task/constants.js.map +1 -1
  73. package/dist/services/task/contact.js +29 -0
  74. package/dist/services/task/contact.js.map +1 -1
  75. package/dist/services/task/digital/Digital.js +4 -3
  76. package/dist/services/task/digital/Digital.js.map +1 -1
  77. package/dist/services/task/state-machine/TaskStateMachine.js +162 -53
  78. package/dist/services/task/state-machine/TaskStateMachine.js.map +1 -1
  79. package/dist/services/task/state-machine/actions.js +6 -1
  80. package/dist/services/task/state-machine/actions.js.map +1 -1
  81. package/dist/services/task/state-machine/guards.js +7 -10
  82. package/dist/services/task/state-machine/guards.js.map +1 -1
  83. package/dist/services/task/state-machine/types.js.map +1 -1
  84. package/dist/services/task/state-machine/uiControlsComputer.js +118 -18
  85. package/dist/services/task/state-machine/uiControlsComputer.js.map +1 -1
  86. package/dist/services/task/types.js +47 -1
  87. package/dist/services/task/types.js.map +1 -1
  88. package/dist/services/task/voice/Voice.js +477 -17
  89. package/dist/services/task/voice/Voice.js.map +1 -1
  90. package/dist/services/task/voice/WebRTC.js +6 -3
  91. package/dist/services/task/voice/WebRTC.js.map +1 -1
  92. package/dist/services/task/voice/wxAppOfferObservability.js +145 -0
  93. package/dist/services/task/voice/wxAppOfferObservability.js.map +1 -0
  94. package/dist/services/task/voice/wxAppVoiceMethods.js +395 -0
  95. package/dist/services/task/voice/wxAppVoiceMethods.js.map +1 -0
  96. package/dist/services/wxAppDiagnosticLogging.js +159 -0
  97. package/dist/services/wxAppDiagnosticLogging.js.map +1 -0
  98. package/dist/services/wxAppTelephonyUtils.js +19 -0
  99. package/dist/services/wxAppTelephonyUtils.js.map +1 -0
  100. package/dist/types/cc.d.ts +102 -8
  101. package/dist/types/config.d.ts +7 -0
  102. package/dist/types/constants.d.ts +21 -0
  103. package/dist/types/index.d.ts +34 -15
  104. package/dist/types/metrics/MetricsManager.d.ts +6 -0
  105. package/dist/types/metrics/constants.d.ts +31 -0
  106. package/dist/types/services/AddressBook.d.ts +2 -1
  107. package/dist/types/services/AnswerCallOnWebexService.d.ts +38 -0
  108. package/dist/types/services/ApiAiAssistant.d.ts +48 -4
  109. package/dist/types/services/EntryPoint.d.ts +7 -5
  110. package/dist/types/services/Queue.d.ts +5 -3
  111. package/dist/types/services/WebexCrossClientService.d.ts +32 -0
  112. package/dist/types/services/WxAppTelephonyMercurySync.d.ts +28 -0
  113. package/dist/types/services/config/Util.d.ts +4 -8
  114. package/dist/types/services/config/constants.d.ts +28 -76
  115. package/dist/types/services/config/index.d.ts +16 -64
  116. package/dist/types/services/config/types.d.ts +146 -173
  117. package/dist/types/services/constants.d.ts +9 -0
  118. package/dist/types/services/core/Err.d.ts +2 -0
  119. package/dist/types/services/core/Utils.d.ts +10 -0
  120. package/dist/types/services/core/WebexRequest.d.ts +1 -0
  121. package/dist/types/services/core/constants.d.ts +7 -0
  122. package/dist/types/services/core/types.d.ts +2 -0
  123. package/dist/types/services/task/Task.d.ts +34 -5
  124. package/dist/types/services/task/TaskFactory.d.ts +4 -2
  125. package/dist/types/services/task/TaskUtils.d.ts +6 -0
  126. package/dist/types/services/task/WebexCallingUtils.d.ts +11 -0
  127. package/dist/types/services/task/constants.d.ts +12 -0
  128. package/dist/types/services/task/contact.d.ts +4 -0
  129. package/dist/types/services/task/digital/Digital.d.ts +4 -3
  130. package/dist/types/services/task/state-machine/TaskStateMachine.d.ts +304 -56
  131. package/dist/types/services/task/state-machine/guards.d.ts +6 -5
  132. package/dist/types/services/task/state-machine/types.d.ts +9 -1
  133. package/dist/types/services/task/types.d.ts +192 -5
  134. package/dist/types/services/task/voice/Voice.d.ts +68 -11
  135. package/dist/types/services/task/voice/WebRTC.d.ts +5 -4
  136. package/dist/types/services/task/voice/wxAppOfferObservability.d.ts +39 -0
  137. package/dist/types/services/task/voice/wxAppVoiceMethods.d.ts +68 -0
  138. package/dist/types/services/wxAppDiagnosticLogging.d.ts +63 -0
  139. package/dist/types/services/wxAppTelephonyUtils.d.ts +5 -0
  140. package/dist/types/types.d.ts +80 -13
  141. package/dist/types/utils/PageCache.d.ts +20 -3
  142. package/dist/types.js +54 -8
  143. package/dist/types.js.map +1 -1
  144. package/dist/utils/PageCache.js +19 -5
  145. package/dist/utils/PageCache.js.map +1 -1
  146. package/dist/webex.js +9 -1
  147. package/dist/webex.js.map +1 -1
  148. package/package.json +10 -9
  149. package/src/cc.ts +969 -38
  150. package/src/config.ts +7 -0
  151. package/src/constants.ts +24 -0
  152. package/src/index.ts +39 -10
  153. package/src/metrics/MetricsManager.ts +16 -0
  154. package/src/metrics/ai-docs/metrics-spec.md +138 -21
  155. package/src/metrics/behavioral-events.ts +112 -0
  156. package/src/metrics/constants.ts +36 -0
  157. package/src/services/AddressBook.ts +17 -6
  158. package/src/services/AnswerCallOnWebexService.ts +239 -0
  159. package/src/services/ApiAiAssistant.ts +297 -13
  160. package/src/services/EntryPoint.ts +59 -60
  161. package/src/services/Queue.ts +29 -12
  162. package/src/services/WebCallingService.ts +16 -2
  163. package/src/services/WebexCrossClientService.ts +298 -0
  164. package/src/services/WxAppTelephonyMercurySync.ts +142 -0
  165. package/src/services/agent/ai-docs/AGENTS.md +1 -1
  166. package/src/services/agent/ai-docs/agent-spec.md +1 -0
  167. package/src/services/ai-docs/AGENTS.md +10 -10
  168. package/src/services/ai-docs/services-spec.md +25 -5
  169. package/src/services/config/Util.ts +52 -82
  170. package/src/services/config/ai-docs/AGENTS.md +35 -40
  171. package/src/services/config/ai-docs/ARCHITECTURE.md +90 -103
  172. package/src/services/config/ai-docs/config-spec.md +132 -132
  173. package/src/services/config/constants.ts +39 -85
  174. package/src/services/config/index.ts +116 -277
  175. package/src/services/config/types.ts +140 -195
  176. package/src/services/constants.ts +10 -0
  177. package/src/services/core/Err.ts +1 -0
  178. package/src/services/core/Utils.ts +28 -0
  179. package/src/services/core/WebexRequest.ts +3 -1
  180. package/src/services/core/ai-docs/core-spec.md +25 -11
  181. package/src/services/core/aqm-reqs.ts +18 -12
  182. package/src/services/core/constants.ts +8 -0
  183. package/src/services/core/types.ts +33 -1
  184. package/src/services/core/websocket/WebSocketManager.ts +26 -2
  185. package/src/services/task/Task.ts +386 -12
  186. package/src/services/task/TaskFactory.ts +33 -6
  187. package/src/services/task/TaskManager.ts +857 -41
  188. package/src/services/task/TaskUtils.ts +13 -0
  189. package/src/services/task/WebexCallingUtils.ts +136 -0
  190. package/src/services/task/ai-docs/ARCHITECTURE.md +1 -0
  191. package/src/services/task/ai-docs/task-spec.md +190 -8
  192. package/src/services/task/constants.ts +13 -0
  193. package/src/services/task/contact.ts +30 -0
  194. package/src/services/task/digital/Digital.ts +11 -4
  195. package/src/services/task/state-machine/TaskStateMachine.ts +235 -81
  196. package/src/services/task/state-machine/actions.ts +5 -2
  197. package/src/services/task/state-machine/ai-docs/AGENTS.md +6 -4
  198. package/src/services/task/state-machine/ai-docs/ARCHITECTURE.md +35 -16
  199. package/src/services/task/state-machine/ai-docs/task-state-machine-spec.md +103 -33
  200. package/src/services/task/state-machine/guards.ts +7 -10
  201. package/src/services/task/state-machine/types.ts +16 -1
  202. package/src/services/task/state-machine/uiControlsComputer.ts +206 -32
  203. package/src/services/task/types.ts +269 -4
  204. package/src/services/task/voice/Voice.ts +625 -17
  205. package/src/services/task/voice/WebRTC.ts +11 -4
  206. package/src/services/task/voice/wxAppOfferObservability.ts +204 -0
  207. package/src/services/task/voice/wxAppVoiceMethods.ts +575 -0
  208. package/src/services/wxAppDiagnosticLogging.ts +286 -0
  209. package/src/services/wxAppTelephonyUtils.ts +14 -0
  210. package/src/types.ts +108 -13
  211. package/src/utils/AGENTS.md +21 -10
  212. package/src/utils/PageCache.ts +38 -5
  213. package/src/utils/ai-docs/utils-spec.md +21 -11
  214. package/src/webex.js +8 -0
  215. package/test/unit/fixtures/aiSummaryTestUtils.ts +40 -0
  216. package/test/unit/spec/cc.ts +3408 -152
  217. package/test/unit/spec/index.ts +885 -0
  218. package/test/unit/spec/metrics/MetricsManager.ts +25 -0
  219. package/test/unit/spec/metrics/behavioral-events.ts +60 -0
  220. package/test/unit/spec/services/AddressBook.ts +37 -6
  221. package/test/unit/spec/services/AnswerCallOnWebexService.ts +264 -0
  222. package/test/unit/spec/services/ApiAiAssistant.ts +269 -8
  223. package/test/unit/spec/services/EntryPoint.ts +87 -40
  224. package/test/unit/spec/services/Queue.ts +123 -12
  225. package/test/unit/spec/services/WebCallingService.ts +38 -0
  226. package/test/unit/spec/services/WebexCrossClientService.ts +509 -0
  227. package/test/unit/spec/services/WxAppTelephonyMercurySync.ts +136 -0
  228. package/test/unit/spec/services/config/Util.ts +501 -0
  229. package/test/unit/spec/services/config/index.ts +313 -543
  230. package/test/unit/spec/services/core/Utils.ts +22 -0
  231. package/test/unit/spec/services/core/WebexRequest.ts +8 -7
  232. package/test/unit/spec/services/core/aqm-reqs.ts +142 -1
  233. package/test/unit/spec/services/core/websocket/WebSocketManager.ts +64 -14
  234. package/test/unit/spec/services/task/Task.ts +2257 -11
  235. package/test/unit/spec/services/task/TaskFactory.ts +56 -2
  236. package/test/unit/spec/services/task/TaskManager.ts +5559 -1493
  237. package/test/unit/spec/services/task/TaskUtils.ts +72 -0
  238. package/test/unit/spec/services/task/WebexCallingUtils.ts +153 -0
  239. package/test/unit/spec/services/task/contact.ts +33 -0
  240. package/test/unit/spec/services/task/state-machine/TaskStateMachine.ts +1501 -92
  241. package/test/unit/spec/services/task/state-machine/guards.ts +210 -8
  242. package/test/unit/spec/services/task/state-machine/uiControlsComputer.ts +445 -7
  243. package/test/unit/spec/services/task/voice/Voice.ts +1710 -4
  244. package/test/unit/spec/services/task/voice/wxAppOfferObservability.ts +361 -0
  245. package/test/unit/spec/services/task/voice/wxAppVoiceMethods.ts +719 -0
  246. package/test/unit/spec/services/wxAppDiagnosticLogging.ts +179 -0
  247. package/test/unit/spec/wellnessSampleUtils.ts +288 -0
  248. package/typedoc.md +1 -1
  249. package/umd/contact-center.min.js +2 -2
  250. package/umd/contact-center.min.js.map +1 -1
@@ -5,6 +5,7 @@ import {
5
5
  TaskState,
6
6
  } from '../../../../../../src/services/task/state-machine';
7
7
  import {createTaskData} from '../taskTestUtils';
8
+ import {guards} from '../../../../../../src/services/task/state-machine/guards';
8
9
 
9
10
  const createConfig = () => ({
10
11
  channelType: 'voice' as const,
@@ -14,6 +15,18 @@ const createConfig = () => ({
14
15
  isRecordingEnabled: true,
15
16
  });
16
17
 
18
+ /** Mirrors Task.emitTaskWrapup: publishWrapup spy only runs when context would emit task:wrapup. */
19
+ function createPublishAwareEmitTaskWrapupMock() {
20
+ const publishWrapup = jest.fn();
21
+ const emitTaskWrapup = ({context}: {context: {taskData?: {wrapUpRequired?: boolean}}}) => {
22
+ if (context.taskData?.wrapUpRequired) {
23
+ publishWrapup();
24
+ }
25
+ };
26
+
27
+ return {emitTaskWrapup, publishWrapup};
28
+ }
29
+
17
30
  describe('Task state machine', () => {
18
31
  const startMachine = () => {
19
32
  const actor = createActor(createTaskStateMachine(createConfig()));
@@ -106,6 +119,234 @@ describe('Task state machine', () => {
106
119
  });
107
120
  });
108
121
 
122
+ describe('partial lifecycle payloads', () => {
123
+ const createActiveMainCallTaskData = (isHold = false) =>
124
+ createTaskData({
125
+ agentId: 'agent-1',
126
+ interactionId: 'interaction-1',
127
+ mediaResourceId: 'main-media',
128
+ interaction: {
129
+ state: 'connected',
130
+ participants: {
131
+ 'agent-1': {id: 'agent-1', pType: 'Agent', hasLeft: false},
132
+ 'customer-1': {id: 'customer-1', pType: 'Customer', hasLeft: false},
133
+ },
134
+ media: {
135
+ 'main-media': {
136
+ mediaResourceId: 'main-media',
137
+ mType: 'mainCall',
138
+ participants: ['agent-1', 'customer-1'],
139
+ isHold,
140
+ },
141
+ },
142
+ } as any,
143
+ });
144
+
145
+ const createPartialMainCallTaskData = (isHold = false, omitSelf = false) =>
146
+ createTaskData({
147
+ agentId: 'agent-1',
148
+ interactionId: 'interaction-1',
149
+ mediaResourceId: 'main-media',
150
+ interaction: {
151
+ state: 'connected',
152
+ participants: omitSelf
153
+ ? {'customer-1': {id: 'customer-1', pType: 'Customer', hasLeft: false}}
154
+ : {
155
+ 'agent-1': {id: 'agent-1', pType: 'Agent', hasLeft: false},
156
+ 'customer-1': {id: 'customer-1', pType: 'Customer', hasLeft: false},
157
+ },
158
+ media: {
159
+ 'main-media': {
160
+ mediaResourceId: 'main-media',
161
+ mType: 'mainCall',
162
+ participants: ['customer-1'],
163
+ isHold,
164
+ },
165
+ },
166
+ } as any,
167
+ });
168
+
169
+ const startMachineWithEndSpy = () => {
170
+ const emitTaskEnd = jest.fn();
171
+ const service = createActor(
172
+ createTaskStateMachine({...createConfig(), agentId: 'agent-1'}, {actions: {emitTaskEnd}})
173
+ );
174
+ service.start();
175
+
176
+ return {service, emitTaskEnd};
177
+ };
178
+
179
+ const enterTransientHoldState = (
180
+ service: ReturnType<typeof startMachine>,
181
+ state: TaskState.HOLD_INITIATING | TaskState.RESUME_INITIATING,
182
+ taskData: ReturnType<typeof createActiveMainCallTaskData>
183
+ ) => {
184
+ service.send({type: TaskEvent.TASK_INCOMING, taskData});
185
+ service.send({type: TaskEvent.ASSIGN, taskData});
186
+ service.send({type: TaskEvent.HOLD_INITIATED, mediaResourceId: 'main-media'});
187
+
188
+ if (state === TaskState.RESUME_INITIATING) {
189
+ service.send({type: TaskEvent.HOLD_SUCCESS, mediaResourceId: 'main-media'});
190
+ service.send({type: TaskEvent.UNHOLD_INITIATED, mediaResourceId: 'main-media'});
191
+ }
192
+
193
+ expect(service.getSnapshot().value).toBe(state);
194
+ };
195
+
196
+ it('keeps CONNECTED when another participant leave event has a partial mainCall', () => {
197
+ const {service, emitTaskEnd} = startMachineWithEndSpy();
198
+ const taskData = createActiveMainCallTaskData();
199
+
200
+ service.send({type: TaskEvent.TASK_INCOMING, taskData});
201
+ service.send({type: TaskEvent.ASSIGN, taskData});
202
+ expect(service.getSnapshot().value).toBe(TaskState.CONNECTED);
203
+
204
+ service.send({
205
+ type: TaskEvent.PARTICIPANT_LEAVE,
206
+ participantId: 'agent-2',
207
+ taskData: createPartialMainCallTaskData(false, true),
208
+ });
209
+
210
+ expect(service.getSnapshot().value).toBe(TaskState.CONNECTED);
211
+ expect(emitTaskEnd).not.toHaveBeenCalled();
212
+ });
213
+
214
+ it('ignores PARTICIPANT_LEAVE in CONNECTED even when it identifies the current agent', () => {
215
+ const {service, emitTaskEnd} = startMachineWithEndSpy();
216
+ const taskData = createActiveMainCallTaskData();
217
+
218
+ service.send({type: TaskEvent.TASK_INCOMING, taskData});
219
+ service.send({type: TaskEvent.ASSIGN, taskData});
220
+ expect(service.getSnapshot().value).toBe(TaskState.CONNECTED);
221
+
222
+ service.send({
223
+ type: TaskEvent.PARTICIPANT_LEAVE,
224
+ participantId: 'agent-1',
225
+ taskData: createPartialMainCallTaskData(false, true),
226
+ });
227
+
228
+ expect(service.getSnapshot().value).toBe(TaskState.CONNECTED);
229
+ expect(emitTaskEnd).not.toHaveBeenCalled();
230
+ });
231
+
232
+ it('enters WRAPPING_UP on TASK_WRAPUP while HOLD_INITIATING and emits wrap-up once', () => {
233
+ const emitTaskWrapup = jest.fn();
234
+ const service = createActor(
235
+ createTaskStateMachine({...createConfig(), agentId: 'agent-1'}, {actions: {emitTaskWrapup}})
236
+ );
237
+ service.start();
238
+ const taskData = createActiveMainCallTaskData();
239
+
240
+ enterTransientHoldState(service, TaskState.HOLD_INITIATING, taskData);
241
+ service.send({
242
+ type: TaskEvent.TASK_WRAPUP,
243
+ taskData: {...taskData, wrapUpRequired: true},
244
+ });
245
+
246
+ expect(service.getSnapshot().value).toBe(TaskState.WRAPPING_UP);
247
+ expect(emitTaskWrapup).toHaveBeenCalledTimes(1);
248
+ });
249
+
250
+ it('ignores PARTICIPANT_LEAVE in HOLD_INITIATING when a remote Drop identifies the current agent', () => {
251
+ const {service, emitTaskEnd} = startMachineWithEndSpy();
252
+ const taskData = createActiveMainCallTaskData();
253
+
254
+ enterTransientHoldState(service, TaskState.HOLD_INITIATING, taskData);
255
+ service.send({
256
+ type: TaskEvent.PARTICIPANT_LEAVE,
257
+ participantId: 'agent-1',
258
+ taskData: createPartialMainCallTaskData(false, true),
259
+ });
260
+
261
+ expect(service.getSnapshot().value).toBe(TaskState.HOLD_INITIATING);
262
+ expect(emitTaskEnd).not.toHaveBeenCalled();
263
+ });
264
+
265
+ it('terminates RESUME_INITIATING when a remote Drop identifies the current agent', () => {
266
+ const {service, emitTaskEnd} = startMachineWithEndSpy();
267
+ const taskData = createActiveMainCallTaskData(true);
268
+
269
+ enterTransientHoldState(service, TaskState.RESUME_INITIATING, taskData);
270
+ service.send({
271
+ type: TaskEvent.PARTICIPANT_LEAVE,
272
+ participantId: 'agent-1',
273
+ taskData: createPartialMainCallTaskData(true, true),
274
+ });
275
+
276
+ expect(service.getSnapshot().value).toBe(TaskState.TERMINATED);
277
+ expect(emitTaskEnd).toHaveBeenCalledTimes(1);
278
+ });
279
+
280
+ it('keeps HOLD_INITIATING when ParticipantLeftConference identifies another participant', () => {
281
+ const {service, emitTaskEnd} = startMachineWithEndSpy();
282
+ const taskData = createActiveMainCallTaskData();
283
+
284
+ enterTransientHoldState(service, TaskState.HOLD_INITIATING, taskData);
285
+ service.send({
286
+ type: TaskEvent.PARTICIPANT_LEAVE,
287
+ participantId: 'agent-2',
288
+ taskData: createPartialMainCallTaskData(false, true),
289
+ });
290
+
291
+ expect(service.getSnapshot().value).toBe(TaskState.HOLD_INITIATING);
292
+ expect(emitTaskEnd).not.toHaveBeenCalled();
293
+ });
294
+
295
+ it('terminates RESUME_INITIATING when another participant leave omits self from the roster', () => {
296
+ const {service, emitTaskEnd} = startMachineWithEndSpy();
297
+ const taskData = createActiveMainCallTaskData(true);
298
+
299
+ enterTransientHoldState(service, TaskState.RESUME_INITIATING, taskData);
300
+ service.send({
301
+ type: TaskEvent.PARTICIPANT_LEAVE,
302
+ participantId: 'agent-2',
303
+ taskData: createPartialMainCallTaskData(true, true),
304
+ });
305
+
306
+ expect(service.getSnapshot().value).toBe(TaskState.TERMINATED);
307
+ expect(emitTaskEnd).toHaveBeenCalledTimes(1);
308
+ });
309
+
310
+ it('terminates HELD when another participant leave event omits self from its roster', () => {
311
+ const {service, emitTaskEnd} = startMachineWithEndSpy();
312
+ const taskData = createActiveMainCallTaskData(true);
313
+
314
+ service.send({type: TaskEvent.TASK_INCOMING, taskData});
315
+ service.send({type: TaskEvent.ASSIGN, taskData});
316
+ service.send({type: TaskEvent.HOLD_INITIATED, mediaResourceId: 'main-media'});
317
+ service.send({type: TaskEvent.HOLD_SUCCESS, mediaResourceId: 'main-media'});
318
+ expect(service.getSnapshot().value).toBe(TaskState.HELD);
319
+
320
+ service.send({
321
+ type: TaskEvent.PARTICIPANT_LEAVE,
322
+ participantId: 'agent-2',
323
+ taskData: createPartialMainCallTaskData(true, true),
324
+ });
325
+
326
+ expect(service.getSnapshot().value).toBe(TaskState.TERMINATED);
327
+ expect(emitTaskEnd).toHaveBeenCalledTimes(1);
328
+ });
329
+
330
+ it('keeps HELD when CONSULT_END has a partial mainCall', () => {
331
+ const {service, emitTaskEnd} = startMachineWithEndSpy();
332
+ const taskData = createActiveMainCallTaskData(true);
333
+
334
+ service.send({type: TaskEvent.TASK_INCOMING, taskData});
335
+ service.send({type: TaskEvent.ASSIGN, taskData});
336
+ service.send({type: TaskEvent.HOLD_INITIATED, mediaResourceId: 'main-media'});
337
+ service.send({type: TaskEvent.HOLD_SUCCESS, mediaResourceId: 'main-media'});
338
+ expect(service.getSnapshot().value).toBe(TaskState.HELD);
339
+
340
+ service.send({
341
+ type: TaskEvent.CONSULT_END,
342
+ taskData: createPartialMainCallTaskData(true),
343
+ });
344
+
345
+ expect(service.getSnapshot().value).toBe(TaskState.HELD);
346
+ expect(emitTaskEnd).not.toHaveBeenCalled();
347
+ });
348
+ });
349
+
109
350
  describe('recording pause/resume events', () => {
110
351
  it('toggles recordingPaused flag based on events', () => {
111
352
  const service = startMachine();
@@ -180,6 +421,32 @@ describe('Task state machine', () => {
180
421
  service.send({type: TaskEvent.WRAPUP_COMPLETE});
181
422
  expect(service.getSnapshot().value).toBe(TaskState.COMPLETED);
182
423
  });
424
+
425
+ it('emits task wrappedup before cleanup on COMPLETED entry', () => {
426
+ const actionOrder: string[] = [];
427
+ const actor = createActor(
428
+ createTaskStateMachine(createConfig(), {
429
+ actions: {
430
+ emitTaskWrappedup: () => {
431
+ actionOrder.push('emitTaskWrappedup');
432
+ },
433
+ cleanupResources: () => {
434
+ actionOrder.push('cleanupResources');
435
+ },
436
+ },
437
+ })
438
+ );
439
+ actor.start();
440
+ const taskData = createTaskData();
441
+
442
+ actor.send({type: TaskEvent.TASK_INCOMING, taskData});
443
+ actor.send({type: TaskEvent.ASSIGN, taskData});
444
+ actor.send({type: TaskEvent.TASK_WRAPUP});
445
+ actor.send({type: TaskEvent.WRAPUP_COMPLETE});
446
+
447
+ expect(actor.getSnapshot().value).toBe(TaskState.COMPLETED);
448
+ expect(actionOrder).toEqual(['emitTaskWrappedup', 'cleanupResources']);
449
+ });
183
450
  });
184
451
 
185
452
  describe('consult and conference flows', () => {
@@ -272,6 +539,39 @@ describe('Task state machine', () => {
272
539
  } as any,
273
540
  });
274
541
 
542
+ it('hydrates a recovered active conference from IDLE to CONFERENCING', () => {
543
+ const service = startMachine();
544
+ const taskData = createTaskData({
545
+ agentId: 'agent-1',
546
+ interactionId: 'interaction-1',
547
+ mediaResourceId: 'interaction-1',
548
+ interaction: {
549
+ state: 'conference',
550
+ interactionId: 'interaction-1',
551
+ mainInteractionId: 'interaction-1',
552
+ owner: 'agent-1',
553
+ participants: {
554
+ 'agent-1': {id: 'agent-1', pType: 'Agent', hasLeft: false},
555
+ 'agent-2': {id: 'agent-2', pType: 'Agent', hasLeft: false},
556
+ 'customer-1': {id: 'customer-1', pType: 'Customer', hasLeft: false},
557
+ },
558
+ media: {
559
+ 'interaction-1': {
560
+ mediaResourceId: 'interaction-1',
561
+ mType: 'mainCall',
562
+ participants: ['agent-1', 'agent-2', 'customer-1'],
563
+ isHold: false,
564
+ },
565
+ },
566
+ } as any,
567
+ });
568
+
569
+ service.send({type: TaskEvent.HYDRATE, taskData});
570
+
571
+ expect(service.getSnapshot().value).toBe(TaskState.CONFERENCING);
572
+ expect(service.getSnapshot().context.taskData).toEqual(taskData);
573
+ });
574
+
275
575
  it('boots from IDLE to CONSULTING on CONSULTING_ACTIVE for split-leg ordering', () => {
276
576
  const service = startMachine();
277
577
  const taskData = createTaskData({
@@ -343,53 +643,53 @@ describe('Task state machine', () => {
343
643
  expect(snapshot.context.consultFromConference).toBe(true);
344
644
  });
345
645
 
346
- it('hydrates consulted agent to CONSULTING when self consultState is consulting and main leg is held', () => {
347
- const service = startMachine();
348
- const taskData = createTaskData({
349
- agentId: 'agent-2',
350
- isConsulted: true,
351
- interaction: {
352
- state: 'conference',
353
- mainInteractionId: 'interaction-1',
354
- interactionId: 'interaction-1',
355
- participants: {
356
- 'agent-1': {
357
- id: 'agent-1',
358
- pType: 'Agent',
359
- hasLeft: false,
360
- consultState: 'consulting',
361
- isConsulted: false,
362
- },
363
- 'agent-2': {
364
- id: 'agent-2',
365
- pType: 'Agent',
366
- hasLeft: false,
367
- consultState: 'consulting',
368
- isConsulted: true,
369
- },
370
- 'customer-1': {id: 'customer-1', pType: 'Customer', hasLeft: false},
371
- },
372
- media: {
373
- 'interaction-1': {
374
- mediaResourceId: 'interaction-1',
375
- mType: 'mainCall',
376
- participants: ['agent-1', 'customer-1'],
377
- isHold: true,
378
- },
379
- 'consult-media-1': {
380
- mediaResourceId: 'consult-media-1',
381
- mType: 'consult',
382
- participants: ['agent-1', 'agent-2'],
383
- isHold: false,
646
+ it('hydrates consulted agent to CONSULTING when self consultState is consulting and main leg is held', () => {
647
+ const service = startMachine();
648
+ const taskData = createTaskData({
649
+ agentId: 'agent-2',
650
+ isConsulted: true,
651
+ interaction: {
652
+ state: 'conference',
653
+ mainInteractionId: 'interaction-1',
654
+ interactionId: 'interaction-1',
655
+ participants: {
656
+ 'agent-1': {
657
+ id: 'agent-1',
658
+ pType: 'Agent',
659
+ hasLeft: false,
660
+ consultState: 'consulting',
661
+ isConsulted: false,
662
+ },
663
+ 'agent-2': {
664
+ id: 'agent-2',
665
+ pType: 'Agent',
666
+ hasLeft: false,
667
+ consultState: 'consulting',
668
+ isConsulted: true,
669
+ },
670
+ 'customer-1': {id: 'customer-1', pType: 'Customer', hasLeft: false},
384
671
  },
385
- },
386
- } as any,
387
- });
672
+ media: {
673
+ 'interaction-1': {
674
+ mediaResourceId: 'interaction-1',
675
+ mType: 'mainCall',
676
+ participants: ['agent-1', 'customer-1'],
677
+ isHold: true,
678
+ },
679
+ 'consult-media-1': {
680
+ mediaResourceId: 'consult-media-1',
681
+ mType: 'consult',
682
+ participants: ['agent-1', 'agent-2'],
683
+ isHold: false,
684
+ },
685
+ },
686
+ } as any,
687
+ });
388
688
 
389
- service.send({type: TaskEvent.HYDRATE, taskData});
689
+ service.send({type: TaskEvent.HYDRATE, taskData});
390
690
 
391
- expect(service.getSnapshot().value).toBe(TaskState.CONSULTING);
392
- });
691
+ expect(service.getSnapshot().value).toBe(TaskState.CONSULTING);
692
+ });
393
693
 
394
694
  it('hydrates to CONSULTING when consult is pending (self consultState is consultInitiated)', () => {
395
695
  const service = startMachine();
@@ -1587,13 +1887,25 @@ describe('Task state machine', () => {
1587
1887
  expect(service.getSnapshot().value).toBe(TaskState.WRAPPING_UP);
1588
1888
  });
1589
1889
 
1590
- it('terminates when EXIT_CONFERENCE_SUCCESS is received in conferencing and wrapup is not required', () => {
1591
- const service = startMachine();
1592
- const taskData = createTaskData({
1593
- isConsulted: true,
1594
- wrapUpRequired: false,
1890
+ const primeConferencing = (service: ReturnType<typeof startMachine>, taskData: ReturnType<typeof createTaskData>) => {
1891
+ service.send({type: TaskEvent.TASK_INCOMING, taskData});
1892
+ service.send({type: TaskEvent.ASSIGN, taskData});
1893
+ service.send({
1894
+ type: TaskEvent.CONSULT,
1895
+ destination: 'agent-42',
1896
+ destinationType: 'agent',
1897
+ });
1898
+ service.send({type: TaskEvent.CONSULT_SUCCESS, taskData});
1899
+ service.send({type: TaskEvent.MERGE_TO_CONFERENCE});
1900
+ service.send({type: TaskEvent.CONFERENCE_START, taskData});
1901
+ expect(service.getSnapshot().value).toBe(TaskState.CONFERENCING);
1902
+ };
1903
+
1904
+ const conferenceInProgressTaskData = (overrides: Parameters<typeof createTaskData>[0] = {}) =>
1905
+ createTaskData({
1906
+ wrapUpRequired: true,
1595
1907
  interaction: {
1596
- owner: 'other-agent',
1908
+ owner: 'agent-1',
1597
1909
  state: 'conference',
1598
1910
  mainInteractionId: 'interaction-1',
1599
1911
  interactionId: 'interaction-1',
@@ -1604,15 +1916,198 @@ describe('Task state machine', () => {
1604
1916
  type: 'Agent',
1605
1917
  hasJoined: true,
1606
1918
  hasLeft: false,
1607
- isInPredial: false,
1608
- isWrapUp: false,
1609
1919
  },
1610
- c1: {
1611
- id: 'c1',
1612
- pType: 'Customer',
1613
- type: 'Customer',
1614
- hasJoined: true,
1615
- hasLeft: false,
1920
+ 'agent-2': {
1921
+ id: 'agent-2',
1922
+ pType: 'Agent',
1923
+ type: 'Agent',
1924
+ hasJoined: true,
1925
+ hasLeft: false,
1926
+ },
1927
+ },
1928
+ media: {
1929
+ 'interaction-1': {
1930
+ mediaResourceId: 'interaction-1',
1931
+ mediaType: 'telephony',
1932
+ participants: ['agent-1', 'agent-2'],
1933
+ mType: 'mainCall',
1934
+ isHold: false,
1935
+ },
1936
+ },
1937
+ } as any,
1938
+ ...overrides,
1939
+ });
1940
+
1941
+ const primeConnectedWithConferenceTaskData = (
1942
+ service: ReturnType<typeof startMachine>,
1943
+ taskData: ReturnType<typeof createTaskData>
1944
+ ) => {
1945
+ service.send({type: TaskEvent.TASK_INCOMING, taskData});
1946
+ service.send({type: TaskEvent.ASSIGN, taskData});
1947
+ expect(service.getSnapshot().value).toBe(TaskState.CONNECTED);
1948
+ };
1949
+
1950
+ it('enters WRAPPING_UP on EXIT_CONFERENCE_SUCCESS while CONNECTED with conference in task data', () => {
1951
+ const service = startMachine();
1952
+ const taskData = conferenceInProgressTaskData();
1953
+
1954
+ primeConnectedWithConferenceTaskData(service, taskData);
1955
+ service.send({type: TaskEvent.EXIT_CONFERENCE_SUCCESS, taskData});
1956
+
1957
+ expect(service.getSnapshot().value).toBe(TaskState.WRAPPING_UP);
1958
+ expect(service.getSnapshot().context.taskData?.wrapUpRequired).toBe(true);
1959
+ });
1960
+
1961
+ it('emits wrap-up once on EXIT_CONFERENCE_SUCCESS while CONNECTED', () => {
1962
+ const emitTaskWrapup = jest.fn();
1963
+ const service = createActor(
1964
+ createTaskStateMachine(createConfig(), {actions: {emitTaskWrapup}})
1965
+ );
1966
+ service.start();
1967
+ const taskData = conferenceInProgressTaskData();
1968
+
1969
+ primeConnectedWithConferenceTaskData(service, taskData);
1970
+ service.send({type: TaskEvent.EXIT_CONFERENCE_SUCCESS, taskData});
1971
+
1972
+ expect(service.getSnapshot().value).toBe(TaskState.WRAPPING_UP);
1973
+ expect(emitTaskWrapup).toHaveBeenCalledTimes(1);
1974
+ });
1975
+
1976
+ it('terminates on EXIT_CONFERENCE_SUCCESS while CONNECTED when wrap-up is not required', () => {
1977
+ const service = startMachine();
1978
+ const taskData = conferenceInProgressTaskData({
1979
+ wrapUpRequired: false,
1980
+ interaction: {
1981
+ owner: 'other-agent',
1982
+ state: 'conference',
1983
+ mainInteractionId: 'interaction-1',
1984
+ interactionId: 'interaction-1',
1985
+ participants: {
1986
+ 'agent-1': {
1987
+ id: 'agent-1',
1988
+ pType: 'Agent',
1989
+ type: 'Agent',
1990
+ hasJoined: true,
1991
+ hasLeft: false,
1992
+ isWrapUp: false,
1993
+ },
1994
+ 'agent-2': {
1995
+ id: 'agent-2',
1996
+ pType: 'Agent',
1997
+ type: 'Agent',
1998
+ hasJoined: true,
1999
+ hasLeft: false,
2000
+ },
2001
+ },
2002
+ media: {
2003
+ 'interaction-1': {
2004
+ mediaResourceId: 'interaction-1',
2005
+ mediaType: 'telephony',
2006
+ participants: ['agent-1', 'agent-2'],
2007
+ mType: 'mainCall',
2008
+ isHold: false,
2009
+ },
2010
+ },
2011
+ } as any,
2012
+ });
2013
+
2014
+ primeConnectedWithConferenceTaskData(service, taskData);
2015
+ service.send({type: TaskEvent.EXIT_CONFERENCE_SUCCESS, taskData});
2016
+
2017
+ expect(service.getSnapshot().value).toBe(TaskState.TERMINATED);
2018
+ });
2019
+
2020
+ it('does not re-emit wrap-up on late TASK_WRAPUP when wrap-up was already published', () => {
2021
+ const {emitTaskWrapup, publishWrapup} = createPublishAwareEmitTaskWrapupMock();
2022
+ const service = createActor(
2023
+ createTaskStateMachine(createConfig(), {actions: {emitTaskWrapup}})
2024
+ );
2025
+ service.start();
2026
+ const taskData = conferenceInProgressTaskData();
2027
+
2028
+ primeConferencing(service, taskData);
2029
+ service.send({
2030
+ type: TaskEvent.EXIT_CONFERENCE_SUCCESS,
2031
+ taskData: {...taskData, wrapUpRequired: true},
2032
+ });
2033
+ expect(service.getSnapshot().value).toBe(TaskState.WRAPPING_UP);
2034
+ expect(service.getSnapshot().context.taskData?.wrapUpRequired).toBe(true);
2035
+
2036
+ const emitCountAfterExit = publishWrapup.mock.calls.length;
2037
+ service.send({
2038
+ type: TaskEvent.TASK_WRAPUP,
2039
+ taskData: {...taskData, wrapUpRequired: true},
2040
+ });
2041
+
2042
+ expect(publishWrapup.mock.calls.length).toBe(emitCountAfterExit);
2043
+ });
2044
+
2045
+ it('emits wrap-up on late TASK_WRAPUP when wrap-up was not yet published', () => {
2046
+ const {emitTaskWrapup, publishWrapup} = createPublishAwareEmitTaskWrapupMock();
2047
+ const service = createActor(
2048
+ createTaskStateMachine(createConfig(), {actions: {emitTaskWrapup}})
2049
+ );
2050
+ service.start();
2051
+ const taskData = createTaskData({
2052
+ wrapUpRequired: false,
2053
+ interaction: {
2054
+ owner: 'agent-1',
2055
+ state: 'connected',
2056
+ mainInteractionId: 'interaction-1',
2057
+ interactionId: 'interaction-1',
2058
+ participants: {
2059
+ 'agent-1': {
2060
+ id: 'agent-1',
2061
+ pType: 'Agent',
2062
+ type: 'Agent',
2063
+ hasJoined: true,
2064
+ hasLeft: false,
2065
+ },
2066
+ },
2067
+ } as any,
2068
+ });
2069
+
2070
+ service.send({type: TaskEvent.TASK_INCOMING, taskData});
2071
+ service.send({type: TaskEvent.ASSIGN, taskData});
2072
+ service.send({type: TaskEvent.TASK_WRAPUP, taskData});
2073
+ expect(service.getSnapshot().value).toBe(TaskState.WRAPPING_UP);
2074
+ expect(service.getSnapshot().context.taskData?.wrapUpRequired).not.toBe(true);
2075
+ expect(publishWrapup).not.toHaveBeenCalled();
2076
+
2077
+ service.send({
2078
+ type: TaskEvent.TASK_WRAPUP,
2079
+ taskData: {...taskData, wrapUpRequired: true},
2080
+ });
2081
+
2082
+ expect(publishWrapup).toHaveBeenCalledTimes(1);
2083
+ });
2084
+
2085
+ it('terminates when EXIT_CONFERENCE_SUCCESS is received in conferencing and wrapup is not required', () => {
2086
+ const service = startMachine();
2087
+ const taskData = createTaskData({
2088
+ isConsulted: true,
2089
+ wrapUpRequired: false,
2090
+ interaction: {
2091
+ owner: 'other-agent',
2092
+ state: 'conference',
2093
+ mainInteractionId: 'interaction-1',
2094
+ interactionId: 'interaction-1',
2095
+ participants: {
2096
+ 'agent-1': {
2097
+ id: 'agent-1',
2098
+ pType: 'Agent',
2099
+ type: 'Agent',
2100
+ hasJoined: true,
2101
+ hasLeft: false,
2102
+ isInPredial: false,
2103
+ isWrapUp: false,
2104
+ },
2105
+ c1: {
2106
+ id: 'c1',
2107
+ pType: 'Customer',
2108
+ type: 'Customer',
2109
+ hasJoined: true,
2110
+ hasLeft: false,
1616
2111
  isInPredial: false,
1617
2112
  },
1618
2113
  },
@@ -1646,6 +2141,254 @@ describe('Task state machine', () => {
1646
2141
  expect(service.getSnapshot().value).toBe(TaskState.TERMINATED);
1647
2142
  });
1648
2143
 
2144
+ it('enters WRAPPING_UP on CONFERENCE_END when wrapUpRequired is stamped', () => {
2145
+ const service = startMachine();
2146
+ const taskData = createTaskData({
2147
+ wrapUpRequired: true,
2148
+ interaction: {
2149
+ owner: 'agent-1',
2150
+ state: 'conference',
2151
+ mainInteractionId: 'interaction-1',
2152
+ interactionId: 'interaction-1',
2153
+ participants: {
2154
+ 'agent-1': {
2155
+ id: 'agent-1',
2156
+ pType: 'Agent',
2157
+ type: 'Agent',
2158
+ isWrapUp: true,
2159
+ hasJoined: true,
2160
+ hasLeft: true,
2161
+ },
2162
+ },
2163
+ } as any,
2164
+ });
2165
+
2166
+ primeConferencing(service, taskData);
2167
+ service.send({type: TaskEvent.CONFERENCE_END, taskData});
2168
+ expect(service.getSnapshot().value).toBe(TaskState.WRAPPING_UP);
2169
+ });
2170
+
2171
+ it('emits wrap-up once on CONFERENCE_END when wrap-up is required', () => {
2172
+ const {emitTaskWrapup, publishWrapup} = createPublishAwareEmitTaskWrapupMock();
2173
+ const service = createActor(
2174
+ createTaskStateMachine(createConfig(), {actions: {emitTaskWrapup}})
2175
+ );
2176
+ service.start();
2177
+ const taskData = createTaskData({
2178
+ wrapUpRequired: true,
2179
+ interaction: {
2180
+ owner: 'agent-1',
2181
+ state: 'conference',
2182
+ mainInteractionId: 'interaction-1',
2183
+ interactionId: 'interaction-1',
2184
+ participants: {
2185
+ 'agent-1': {
2186
+ id: 'agent-1',
2187
+ pType: 'Agent',
2188
+ type: 'Agent',
2189
+ isWrapUp: true,
2190
+ hasJoined: true,
2191
+ hasLeft: true,
2192
+ },
2193
+ },
2194
+ } as any,
2195
+ });
2196
+
2197
+ primeConferencing(service, taskData);
2198
+ service.send({type: TaskEvent.CONFERENCE_END, taskData});
2199
+ expect(service.getSnapshot().value).toBe(TaskState.WRAPPING_UP);
2200
+ expect(publishWrapup).toHaveBeenCalledTimes(1);
2201
+ });
2202
+
2203
+ it('emits wrap-up once on TASK_WRAPUP from CONSULTING when wrap-up is required', () => {
2204
+ const {emitTaskWrapup, publishWrapup} = createPublishAwareEmitTaskWrapupMock();
2205
+ const service = createActor(
2206
+ createTaskStateMachine(createConfig(), {actions: {emitTaskWrapup}})
2207
+ );
2208
+ service.start();
2209
+ const taskData = createTaskData({
2210
+ interaction: {owner: 'agent-1', state: 'connected'} as any,
2211
+ });
2212
+
2213
+ service.send({type: TaskEvent.TASK_INCOMING, taskData});
2214
+ service.send({type: TaskEvent.ASSIGN, taskData});
2215
+ service.send({type: TaskEvent.CONSULT, destination: 'agent-2', destinationType: 'agent'});
2216
+ service.send({type: TaskEvent.CONSULT_SUCCESS, taskData});
2217
+ expect(service.getSnapshot().value).toBe(TaskState.CONSULTING);
2218
+
2219
+ service.send({
2220
+ type: TaskEvent.TASK_WRAPUP,
2221
+ taskData: {...taskData, wrapUpRequired: true},
2222
+ });
2223
+ expect(service.getSnapshot().value).toBe(TaskState.WRAPPING_UP);
2224
+ expect(publishWrapup).toHaveBeenCalledTimes(1);
2225
+ });
2226
+
2227
+ it('emits wrap-up once on TASK_WRAPUP from CONFERENCING when wrap-up is required', () => {
2228
+ const {emitTaskWrapup, publishWrapup} = createPublishAwareEmitTaskWrapupMock();
2229
+ const service = createActor(
2230
+ createTaskStateMachine(createConfig(), {actions: {emitTaskWrapup}})
2231
+ );
2232
+ service.start();
2233
+ const taskData = createTaskData({
2234
+ interaction: {
2235
+ owner: 'agent-1',
2236
+ state: 'conference',
2237
+ mainInteractionId: 'interaction-1',
2238
+ interactionId: 'interaction-1',
2239
+ } as any,
2240
+ });
2241
+
2242
+ primeConferencing(service, taskData);
2243
+ service.send({
2244
+ type: TaskEvent.TASK_WRAPUP,
2245
+ taskData: {...taskData, wrapUpRequired: true},
2246
+ });
2247
+ expect(service.getSnapshot().value).toBe(TaskState.WRAPPING_UP);
2248
+ expect(publishWrapup).toHaveBeenCalledTimes(1);
2249
+ });
2250
+
2251
+ it('enters WRAPPING_UP on PARTICIPANT_LEAVE when this agent left and wrap-up is required', () => {
2252
+ const service = startMachine();
2253
+ const conferenceData = createTaskData({
2254
+ interaction: {
2255
+ owner: 'agent-1',
2256
+ state: 'conference',
2257
+ mainInteractionId: 'interaction-1',
2258
+ interactionId: 'interaction-1',
2259
+ participants: {
2260
+ 'agent-1': {id: 'agent-1', pType: 'Agent', type: 'Agent', hasJoined: true, hasLeft: false},
2261
+ 'agent-2': {id: 'agent-2', pType: 'Agent', type: 'Agent', hasJoined: true, hasLeft: false},
2262
+ },
2263
+ } as any,
2264
+ });
2265
+ const leaveData = createTaskData({
2266
+ wrapUpRequired: true,
2267
+ participantId: 'agent-1',
2268
+ interaction: {
2269
+ owner: 'agent-1',
2270
+ state: 'conference',
2271
+ mainInteractionId: 'interaction-1',
2272
+ interactionId: 'interaction-1',
2273
+ participants: {
2274
+ 'agent-2': {id: 'agent-2', pType: 'Agent', type: 'Agent', hasJoined: true, hasLeft: false},
2275
+ },
2276
+ } as any,
2277
+ });
2278
+
2279
+ primeConferencing(service, conferenceData);
2280
+ service.send({
2281
+ type: TaskEvent.PARTICIPANT_LEAVE,
2282
+ participantId: 'agent-1',
2283
+ taskData: leaveData,
2284
+ });
2285
+ expect(service.getSnapshot().value).toBe(TaskState.WRAPPING_UP);
2286
+ });
2287
+
2288
+ it('emits wrap-up once on PARTICIPANT_LEAVE when wrap-up is required', () => {
2289
+ const emitTaskWrapup = jest.fn();
2290
+ const service = createActor(
2291
+ createTaskStateMachine(createConfig(), {actions: {emitTaskWrapup}})
2292
+ );
2293
+ service.start();
2294
+ const conferenceData = createTaskData({
2295
+ interaction: {
2296
+ owner: 'agent-1',
2297
+ state: 'conference',
2298
+ mainInteractionId: 'interaction-1',
2299
+ interactionId: 'interaction-1',
2300
+ participants: {
2301
+ 'agent-1': {id: 'agent-1', pType: 'Agent', type: 'Agent', hasJoined: true, hasLeft: false},
2302
+ 'agent-2': {id: 'agent-2', pType: 'Agent', type: 'Agent', hasJoined: true, hasLeft: false},
2303
+ },
2304
+ } as any,
2305
+ });
2306
+ const leaveData = createTaskData({
2307
+ wrapUpRequired: true,
2308
+ participantId: 'agent-1',
2309
+ interaction: {
2310
+ owner: 'agent-1',
2311
+ state: 'conference',
2312
+ mainInteractionId: 'interaction-1',
2313
+ interactionId: 'interaction-1',
2314
+ participants: {
2315
+ 'agent-2': {id: 'agent-2', pType: 'Agent', type: 'Agent', hasJoined: true, hasLeft: false},
2316
+ },
2317
+ } as any,
2318
+ });
2319
+
2320
+ primeConferencing(service, conferenceData);
2321
+ service.send({
2322
+ type: TaskEvent.PARTICIPANT_LEAVE,
2323
+ participantId: 'agent-1',
2324
+ taskData: leaveData,
2325
+ });
2326
+
2327
+ expect(service.getSnapshot().value).toBe(TaskState.WRAPPING_UP);
2328
+ expect(emitTaskWrapup).toHaveBeenCalledTimes(1);
2329
+ });
2330
+
2331
+ it('does not wrap remaining agent on PARTICIPANT_LEAVE', () => {
2332
+ const service = startMachine();
2333
+ const conferenceData = createTaskData({
2334
+ interaction: {
2335
+ owner: 'agent-1',
2336
+ state: 'conference',
2337
+ mainInteractionId: 'interaction-1',
2338
+ interactionId: 'interaction-1',
2339
+ participants: {
2340
+ 'agent-1': {id: 'agent-1', pType: 'Agent', type: 'Agent', hasJoined: true, hasLeft: false},
2341
+ 'agent-2': {id: 'agent-2', pType: 'Agent', type: 'Agent', hasJoined: true, hasLeft: false},
2342
+ c1: {id: 'c1', pType: 'Customer', type: 'Customer', hasJoined: true, hasLeft: false},
2343
+ },
2344
+ media: {
2345
+ 'interaction-1': {
2346
+ mediaResourceId: 'interaction-1',
2347
+ mediaType: 'telephony',
2348
+ mediaMgr: 'mm',
2349
+ participants: ['agent-1', 'agent-2', 'c1'],
2350
+ mType: 'mainCall',
2351
+ isHold: false,
2352
+ holdTimestamp: null,
2353
+ },
2354
+ },
2355
+ } as any,
2356
+ });
2357
+ const otherLeft = createTaskData({
2358
+ wrapUpRequired: false,
2359
+ participantId: 'agent-2',
2360
+ interaction: {
2361
+ owner: 'agent-1',
2362
+ state: 'conference',
2363
+ mainInteractionId: 'interaction-1',
2364
+ interactionId: 'interaction-1',
2365
+ participants: {
2366
+ 'agent-1': {id: 'agent-1', pType: 'Agent', type: 'Agent', hasJoined: true, hasLeft: false},
2367
+ c1: {id: 'c1', pType: 'Customer', type: 'Customer', hasJoined: true, hasLeft: false},
2368
+ },
2369
+ media: {
2370
+ 'interaction-1': {
2371
+ mediaResourceId: 'interaction-1',
2372
+ mediaType: 'telephony',
2373
+ mediaMgr: 'mm',
2374
+ participants: ['agent-1', 'c1'],
2375
+ mType: 'mainCall',
2376
+ isHold: false,
2377
+ holdTimestamp: null,
2378
+ },
2379
+ },
2380
+ } as any,
2381
+ });
2382
+
2383
+ primeConferencing(service, conferenceData);
2384
+ service.send({
2385
+ type: TaskEvent.PARTICIPANT_LEAVE,
2386
+ participantId: 'agent-2',
2387
+ taskData: otherLeft,
2388
+ });
2389
+ expect(service.getSnapshot().value).not.toBe(TaskState.WRAPPING_UP);
2390
+ });
2391
+
1649
2392
  it('downgrades to HELD on HOLD_SUCCESS when no other agents remain on task', () => {
1650
2393
  const service = startMachine();
1651
2394
  const conferenceTaskData = createSingleAgentConferenceTaskData('conference');
@@ -1684,6 +2427,47 @@ describe('Task state machine', () => {
1684
2427
  expect(service.getSnapshot().value).toBe(TaskState.CONNECTED);
1685
2428
  });
1686
2429
 
2430
+ it('downgrades to CONNECTED on PARTICIPANT_LEAVE when the current agent remains', () => {
2431
+ const service = startMachine();
2432
+ const conferenceTaskData = createSingleAgentConferenceTaskData('conference');
2433
+ const connectedTaskData = createSingleAgentConferenceTaskData('connected');
2434
+
2435
+ service.send({type: TaskEvent.TASK_INCOMING, taskData: conferenceTaskData});
2436
+ service.send({type: TaskEvent.ASSIGN, taskData: conferenceTaskData});
2437
+ service.send({type: TaskEvent.CONFERENCE_START, taskData: conferenceTaskData});
2438
+ expect(service.getSnapshot().value).toBe(TaskState.CONFERENCING);
2439
+
2440
+ service.send({
2441
+ type: TaskEvent.PARTICIPANT_LEAVE,
2442
+ participantId: 'agent-2',
2443
+ taskData: connectedTaskData,
2444
+ });
2445
+
2446
+ expect(service.getSnapshot().value).toBe(TaskState.CONNECTED);
2447
+ });
2448
+
2449
+ it('terminates instead of downgrading when PARTICIPANT_LEAVE identifies the current agent', () => {
2450
+ const service = startMachine();
2451
+ const conferenceTaskData = createSingleAgentConferenceTaskData('conference');
2452
+ const connectedTaskData = {
2453
+ ...createSingleAgentConferenceTaskData('connected'),
2454
+ wrapUpRequired: false,
2455
+ };
2456
+
2457
+ service.send({type: TaskEvent.TASK_INCOMING, taskData: conferenceTaskData});
2458
+ service.send({type: TaskEvent.ASSIGN, taskData: conferenceTaskData});
2459
+ service.send({type: TaskEvent.CONFERENCE_START, taskData: conferenceTaskData});
2460
+ expect(service.getSnapshot().value).toBe(TaskState.CONFERENCING);
2461
+
2462
+ service.send({
2463
+ type: TaskEvent.PARTICIPANT_LEAVE,
2464
+ participantId: 'agent-1',
2465
+ taskData: connectedTaskData,
2466
+ });
2467
+
2468
+ expect(service.getSnapshot().value).toBe(TaskState.TERMINATED);
2469
+ });
2470
+
1687
2471
  it('returns to CONNECTED when CTQ cancel arrives before queue connects', () => {
1688
2472
  const service = startMachine();
1689
2473
  const taskData = createTaskData();
@@ -1798,13 +2582,21 @@ describe('Task state machine', () => {
1798
2582
  owner: 'agent-1',
1799
2583
  } as any,
1800
2584
  });
1801
- service.send({type: TaskEvent.CONSULT_END, taskData: terminatedTaskData});
1802
- expect(service.getSnapshot().value).toBe(TaskState.WRAPPING_UP);
1803
- });
1804
-
1805
- it('transitions to CONNECTED on CONSULT_END without isTerminated during CONF_INITIATING', () => {
1806
- const service = startMachine();
1807
- const taskData = createTaskData({consultingAgentId: 'agent-1'});
2585
+ service.send({type: TaskEvent.CONSULT_END, taskData: terminatedTaskData});
2586
+ expect(service.getSnapshot().value).toBe(TaskState.WRAPPING_UP);
2587
+ });
2588
+
2589
+ it('transitions to CONNECTED on CONSULT_END without isTerminated during CONF_INITIATING', () => {
2590
+ const service = startMachine();
2591
+ const taskData = createTaskData({
2592
+ consultingAgentId: 'agent-1',
2593
+ interaction: {
2594
+ participants: {
2595
+ 'agent-1': {id: 'agent-1', pType: 'Agent', hasLeft: false},
2596
+ 'customer-1': {id: 'customer-1', pType: 'Customer', hasLeft: false},
2597
+ },
2598
+ } as any,
2599
+ });
1808
2600
 
1809
2601
  service.send({type: TaskEvent.TASK_INCOMING, taskData});
1810
2602
  service.send({type: TaskEvent.ASSIGN, taskData});
@@ -1820,10 +2612,8 @@ describe('Task state machine', () => {
1820
2612
  service.send({type: TaskEvent.CONSULT_END, taskData});
1821
2613
  expect(service.getSnapshot().value).toBe(TaskState.CONNECTED);
1822
2614
  });
1823
-
1824
2615
  });
1825
2616
 
1826
-
1827
2617
  describe('CONFERENCING state CONSULT_FAILED ANOTHER_CONSULT_IN_PROGRESS (CAI-8329)', () => {
1828
2618
  it('keeps hideBlindTransfer flag and hides transfer after secondary consult rejected from Agent Desktop', () => {
1829
2619
  const service = startMachine();
@@ -1848,7 +2638,12 @@ describe('Task state machine', () => {
1848
2638
  consultState: 'conferencing',
1849
2639
  },
1850
2640
  '+13159998059': {id: '+13159998059', pType: 'EP-DN', hasLeft: false},
1851
- '+14696762938': {id: '+14696762938', pType: 'Customer', hasJoined: true, hasLeft: false},
2641
+ '+14696762938': {
2642
+ id: '+14696762938',
2643
+ pType: 'Customer',
2644
+ hasJoined: true,
2645
+ hasLeft: false,
2646
+ },
1852
2647
  },
1853
2648
  media: {
1854
2649
  'interaction-1': {
@@ -1902,7 +2697,12 @@ describe('Task state machine', () => {
1902
2697
  consultState: 'conferencing',
1903
2698
  },
1904
2699
  '+13159998059': {id: '+13159998059', pType: 'EP-DN', hasLeft: false},
1905
- '+14696762938': {id: '+14696762938', pType: 'Customer', hasJoined: true, hasLeft: false},
2700
+ '+14696762938': {
2701
+ id: '+14696762938',
2702
+ pType: 'Customer',
2703
+ hasJoined: true,
2704
+ hasLeft: false,
2705
+ },
1906
2706
  },
1907
2707
  media: {
1908
2708
  'interaction-1': {
@@ -1920,12 +2720,19 @@ describe('Task state machine', () => {
1920
2720
  },
1921
2721
  } as any,
1922
2722
  });
1923
- service.send({type: TaskEvent.CONSULT_FAILED, taskData: consultFailedTaskData, reason: 'ANOTHER_CONSULT_IN_PROGRESS'});
2723
+ service.send({
2724
+ type: TaskEvent.CONSULT_FAILED,
2725
+ taskData: consultFailedTaskData,
2726
+ reason: 'ANOTHER_CONSULT_IN_PROGRESS',
2727
+ });
1924
2728
 
1925
2729
  const snapshot = service.getSnapshot();
1926
2730
  expect(snapshot.value).toBe(TaskState.CONFERENCING);
1927
2731
  expect(snapshot.context.hideBlindTransferForEpDnPendingMerge).toBe(true);
1928
- expect(snapshot.context.uiControls.main.transfer).toEqual({isVisible: false, isEnabled: false});
2732
+ expect(snapshot.context.uiControls.main.transfer).toEqual({
2733
+ isVisible: false,
2734
+ isEnabled: false,
2735
+ });
1929
2736
  expect(snapshot.context.uiControls.main.consult).toEqual({isVisible: true, isEnabled: false});
1930
2737
  });
1931
2738
  });
@@ -2126,6 +2933,72 @@ describe('Task state machine', () => {
2126
2933
  service.send({type: TaskEvent.CONSULT_FAILED, taskData});
2127
2934
  expect(service.getSnapshot().value).toBe(TaskState.TERMINATED);
2128
2935
  });
2936
+
2937
+ it('emits only consult-end when an unaccepted consulted offer is ended', () => {
2938
+ const emitTaskConsultEnd = jest.fn();
2939
+ const emitTaskEnd = jest.fn();
2940
+ const service = createActor(
2941
+ createTaskStateMachine(createConfig(), {
2942
+ actions: {emitTaskConsultEnd, emitTaskEnd},
2943
+ })
2944
+ );
2945
+ service.start();
2946
+ const taskData = createTaskData({isConsulted: true, consultingAgentId: 'agent-1'});
2947
+
2948
+ service.send({type: TaskEvent.TASK_INCOMING, taskData});
2949
+ service.send({type: TaskEvent.CONSULT_END, taskData});
2950
+
2951
+ expect(service.getSnapshot().value).toBe(TaskState.TERMINATED);
2952
+ expect(emitTaskConsultEnd).toHaveBeenCalledTimes(1);
2953
+ expect(emitTaskEnd).not.toHaveBeenCalled();
2954
+ });
2955
+
2956
+ it('transitions to WRAPPING_UP on TASK_WRAPUP when wrapup is required', () => {
2957
+ const service = startMachine();
2958
+ const offerTaskData = createTaskData({
2959
+ interaction: {
2960
+ outboundType: 'OUTDIAL',
2961
+ } as any,
2962
+ });
2963
+
2964
+ service.send({type: TaskEvent.TASK_INCOMING, taskData: offerTaskData});
2965
+ expect(service.getSnapshot().value).toBe(TaskState.OFFERED);
2966
+
2967
+ const wrapupTaskData = createTaskData({
2968
+ agentId: 'agent-1',
2969
+ agentsPendingWrapUp: ['agent-1'],
2970
+ wrapUpRequired: true,
2971
+ interaction: {
2972
+ outboundType: 'OUTDIAL',
2973
+ state: 'wrapUp',
2974
+ isTerminated: true,
2975
+ } as any,
2976
+ });
2977
+ service.send({type: TaskEvent.TASK_WRAPUP, taskData: wrapupTaskData});
2978
+ expect(service.getSnapshot().value).toBe(TaskState.WRAPPING_UP);
2979
+ });
2980
+
2981
+ it('emits wrap-up once on TASK_WRAPUP from OFFERED when wrapup is required', () => {
2982
+ const {emitTaskWrapup, publishWrapup} = createPublishAwareEmitTaskWrapupMock();
2983
+ const service = createActor(
2984
+ createTaskStateMachine(createConfig(), {actions: {emitTaskWrapup}})
2985
+ );
2986
+ service.start();
2987
+ const offerTaskData = createTaskData({
2988
+ interaction: {outboundType: 'OUTDIAL'} as any,
2989
+ });
2990
+ service.send({type: TaskEvent.TASK_INCOMING, taskData: offerTaskData});
2991
+ expect(service.getSnapshot().value).toBe(TaskState.OFFERED);
2992
+
2993
+ const wrapupTaskData = createTaskData({
2994
+ agentId: 'agent-1',
2995
+ wrapUpRequired: true,
2996
+ interaction: {outboundType: 'OUTDIAL', state: 'wrapUp', isTerminated: true} as any,
2997
+ });
2998
+ service.send({type: TaskEvent.TASK_WRAPUP, taskData: wrapupTaskData});
2999
+ expect(service.getSnapshot().value).toBe(TaskState.WRAPPING_UP);
3000
+ expect(publishWrapup).toHaveBeenCalledTimes(1);
3001
+ });
2129
3002
  });
2130
3003
 
2131
3004
  describe('OUTBOUND_FAILED handling', () => {
@@ -2152,29 +3025,247 @@ describe('Task state machine', () => {
2152
3025
  } as any,
2153
3026
  });
2154
3027
 
2155
- service.send({type: TaskEvent.TASK_INCOMING, taskData: offerTaskData});
2156
- expect(service.getSnapshot().value).toBe(TaskState.OFFERED);
3028
+ service.send({type: TaskEvent.TASK_INCOMING, taskData: offerTaskData});
3029
+ expect(service.getSnapshot().value).toBe(TaskState.OFFERED);
3030
+
3031
+ const failedTaskData = createTaskData({
3032
+ interaction: {
3033
+ outboundType: 'OUTDIAL',
3034
+ isTerminated: true,
3035
+ } as any,
3036
+ });
3037
+ service.send({
3038
+ type: TaskEvent.OUTBOUND_FAILED,
3039
+ taskData: failedTaskData,
3040
+ reason: 'CUSTOMER_BUSY',
3041
+ });
3042
+ expect(service.getSnapshot().value).toBe(TaskState.TERMINATED);
3043
+ });
3044
+
3045
+ it('transitions from OFFERED to WRAPPING_UP on OUTBOUND_FAILED when wrapup is required', () => {
3046
+ const service = startMachine();
3047
+ const offerTaskData = createTaskData({
3048
+ interaction: {
3049
+ outboundType: 'OUTDIAL',
3050
+ } as any,
3051
+ });
3052
+
3053
+ service.send({type: TaskEvent.TASK_INCOMING, taskData: offerTaskData});
3054
+ expect(service.getSnapshot().value).toBe(TaskState.OFFERED);
3055
+
3056
+ const failedTaskData = createTaskData({
3057
+ agentId: 'agent-1',
3058
+ agentsPendingWrapUp: ['agent-1'],
3059
+ interaction: {
3060
+ outboundType: 'OUTDIAL',
3061
+ isTerminated: true,
3062
+ } as any,
3063
+ });
3064
+ service.send({
3065
+ type: TaskEvent.OUTBOUND_FAILED,
3066
+ taskData: failedTaskData,
3067
+ reason: 'CUSTOMER_BUSY',
3068
+ });
3069
+ expect(service.getSnapshot().value).toBe(TaskState.WRAPPING_UP);
3070
+ });
3071
+
3072
+ it('transitions from CONNECTED to WRAPPING_UP on OUTBOUND_FAILED when wrapup is required', () => {
3073
+ const service = startMachine();
3074
+ const offerTaskData = createTaskData({
3075
+ interaction: {
3076
+ outboundType: 'OUTDIAL',
3077
+ } as any,
3078
+ });
3079
+
3080
+ service.send({type: TaskEvent.TASK_INCOMING, taskData: offerTaskData});
3081
+ service.send({type: TaskEvent.ASSIGN, taskData: offerTaskData});
3082
+ expect(service.getSnapshot().value).toBe(TaskState.CONNECTED);
3083
+
3084
+ const failedTaskData = createTaskData({
3085
+ agentId: 'agent-1',
3086
+ agentsPendingWrapUp: ['agent-1'],
3087
+ interaction: {
3088
+ outboundType: 'OUTDIAL',
3089
+ isTerminated: true,
3090
+ } as any,
3091
+ });
3092
+ service.send({type: TaskEvent.OUTBOUND_FAILED, taskData: failedTaskData, reason: 'AGENT_ENDS'});
3093
+ expect(service.getSnapshot().value).toBe(TaskState.WRAPPING_UP);
3094
+ });
3095
+
3096
+ it('transitions from HELD to WRAPPING_UP on OUTBOUND_FAILED when wrapup is required', () => {
3097
+ const service = startMachine();
3098
+ const taskData = createTaskData({
3099
+ interaction: {
3100
+ outboundType: 'OUTDIAL',
3101
+ } as any,
3102
+ });
3103
+
3104
+ service.send({type: TaskEvent.TASK_INCOMING, taskData});
3105
+ service.send({type: TaskEvent.ASSIGN, taskData});
3106
+ service.send({type: TaskEvent.HOLD_INITIATED, mediaResourceId: taskData.mediaResourceId});
3107
+ service.send({type: TaskEvent.HOLD_SUCCESS, mediaResourceId: taskData.mediaResourceId});
3108
+ expect(service.getSnapshot().value).toBe(TaskState.HELD);
3109
+
3110
+ const failedTaskData = createTaskData({
3111
+ agentId: 'agent-1',
3112
+ agentsPendingWrapUp: ['agent-1'],
3113
+ interaction: {
3114
+ outboundType: 'OUTDIAL',
3115
+ isTerminated: true,
3116
+ } as any,
3117
+ });
3118
+ service.send({type: TaskEvent.OUTBOUND_FAILED, taskData: failedTaskData, reason: 'AGENT_ENDS'});
3119
+ expect(service.getSnapshot().value).toBe(TaskState.WRAPPING_UP);
3120
+ });
3121
+
3122
+ it('transitions from CONSULTING to WRAPPING_UP on OUTBOUND_FAILED when wrapup is required', () => {
3123
+ const service = startMachine();
3124
+ const taskData = createTaskData({
3125
+ interaction: {
3126
+ outboundType: 'OUTDIAL',
3127
+ } as any,
3128
+ });
3129
+
3130
+ service.send({type: TaskEvent.TASK_INCOMING, taskData});
3131
+ service.send({type: TaskEvent.ASSIGN, taskData});
3132
+ service.send({type: TaskEvent.CONSULT, destination: 'agent-42', destinationType: 'agent'});
3133
+ service.send({type: TaskEvent.CONSULT_SUCCESS, taskData});
3134
+ expect(service.getSnapshot().value).toBe(TaskState.CONSULTING);
3135
+
3136
+ const failedTaskData = createTaskData({
3137
+ agentId: 'agent-1',
3138
+ agentsPendingWrapUp: ['agent-1'],
3139
+ interaction: {
3140
+ outboundType: 'OUTDIAL',
3141
+ isTerminated: true,
3142
+ } as any,
3143
+ });
3144
+ service.send({type: TaskEvent.OUTBOUND_FAILED, taskData: failedTaskData, reason: 'AGENT_ENDS'});
3145
+ expect(service.getSnapshot().value).toBe(TaskState.WRAPPING_UP);
3146
+ });
3147
+
3148
+ it('transitions from CONFERENCING to WRAPPING_UP on OUTBOUND_FAILED when wrapup is required', () => {
3149
+ const service = startMachine();
3150
+ const taskData = createTaskData({
3151
+ consultingAgentId: 'agent-1',
3152
+ interaction: {
3153
+ outboundType: 'OUTDIAL',
3154
+ } as any,
3155
+ });
3156
+
3157
+ service.send({type: TaskEvent.TASK_INCOMING, taskData});
3158
+ service.send({type: TaskEvent.ASSIGN, taskData});
3159
+ service.send({type: TaskEvent.CONSULT, destination: 'agent-42', destinationType: 'agent'});
3160
+ service.send({type: TaskEvent.CONSULT_SUCCESS, taskData});
3161
+ service.send({type: TaskEvent.MERGE_TO_CONFERENCE});
3162
+ service.send({type: TaskEvent.CONFERENCE_START});
3163
+ expect(service.getSnapshot().value).toBe(TaskState.CONFERENCING);
3164
+
3165
+ const failedTaskData = createTaskData({
3166
+ agentId: 'agent-1',
3167
+ agentsPendingWrapUp: ['agent-1'],
3168
+ interaction: {
3169
+ outboundType: 'OUTDIAL',
3170
+ isTerminated: true,
3171
+ } as any,
3172
+ });
3173
+ service.send({type: TaskEvent.OUTBOUND_FAILED, taskData: failedTaskData, reason: 'AGENT_ENDS'});
3174
+ expect(service.getSnapshot().value).toBe(TaskState.WRAPPING_UP);
3175
+ });
3176
+
3177
+ it('transitions from HOLD_INITIATING to WRAPPING_UP on OUTBOUND_FAILED when wrapup is required', () => {
3178
+ const service = startMachine();
3179
+ const taskData = createTaskData({
3180
+ interaction: {
3181
+ outboundType: 'OUTDIAL',
3182
+ } as any,
3183
+ });
3184
+
3185
+ service.send({type: TaskEvent.TASK_INCOMING, taskData});
3186
+ service.send({type: TaskEvent.ASSIGN, taskData});
3187
+ service.send({type: TaskEvent.HOLD_INITIATED, mediaResourceId: taskData.mediaResourceId});
3188
+ expect(service.getSnapshot().value).toBe(TaskState.HOLD_INITIATING);
3189
+
3190
+ const failedTaskData = createTaskData({
3191
+ agentId: 'agent-1',
3192
+ agentsPendingWrapUp: ['agent-1'],
3193
+ interaction: {
3194
+ outboundType: 'OUTDIAL',
3195
+ isTerminated: true,
3196
+ } as any,
3197
+ });
3198
+ service.send({type: TaskEvent.OUTBOUND_FAILED, taskData: failedTaskData, reason: 'AGENT_ENDS'});
3199
+ expect(service.getSnapshot().value).toBe(TaskState.WRAPPING_UP);
3200
+ });
3201
+
3202
+ it('transitions from RESUME_INITIATING to WRAPPING_UP on OUTBOUND_FAILED when wrapup is required', () => {
3203
+ const service = startMachine();
3204
+ const taskData = createTaskData({
3205
+ interaction: {
3206
+ outboundType: 'OUTDIAL',
3207
+ } as any,
3208
+ });
3209
+
3210
+ service.send({type: TaskEvent.TASK_INCOMING, taskData});
3211
+ service.send({type: TaskEvent.ASSIGN, taskData});
3212
+ service.send({type: TaskEvent.HOLD_INITIATED, mediaResourceId: taskData.mediaResourceId});
3213
+ service.send({type: TaskEvent.HOLD_SUCCESS, mediaResourceId: taskData.mediaResourceId});
3214
+ service.send({type: TaskEvent.UNHOLD_INITIATED, mediaResourceId: taskData.mediaResourceId});
3215
+ expect(service.getSnapshot().value).toBe(TaskState.RESUME_INITIATING);
3216
+
3217
+ const failedTaskData = createTaskData({
3218
+ agentId: 'agent-1',
3219
+ agentsPendingWrapUp: ['agent-1'],
3220
+ interaction: {
3221
+ outboundType: 'OUTDIAL',
3222
+ isTerminated: true,
3223
+ } as any,
3224
+ });
3225
+ service.send({type: TaskEvent.OUTBOUND_FAILED, taskData: failedTaskData, reason: 'AGENT_ENDS'});
3226
+ expect(service.getSnapshot().value).toBe(TaskState.WRAPPING_UP);
3227
+ });
3228
+
3229
+ it('transitions from CONSULT_INITIATING to WRAPPING_UP on OUTBOUND_FAILED when wrapup is required', () => {
3230
+ const service = startMachine();
3231
+ const taskData = createTaskData({
3232
+ interaction: {
3233
+ outboundType: 'OUTDIAL',
3234
+ } as any,
3235
+ });
3236
+
3237
+ service.send({type: TaskEvent.TASK_INCOMING, taskData});
3238
+ service.send({type: TaskEvent.ASSIGN, taskData});
3239
+ service.send({type: TaskEvent.CONSULT, destination: 'agent-42', destinationType: 'agent'});
3240
+ expect(service.getSnapshot().value).toBe(TaskState.CONSULT_INITIATING);
2157
3241
 
2158
3242
  const failedTaskData = createTaskData({
3243
+ agentId: 'agent-1',
3244
+ agentsPendingWrapUp: ['agent-1'],
2159
3245
  interaction: {
2160
3246
  outboundType: 'OUTDIAL',
2161
3247
  isTerminated: true,
2162
3248
  } as any,
2163
3249
  });
2164
- service.send({type: TaskEvent.OUTBOUND_FAILED, taskData: failedTaskData, reason: 'CUSTOMER_BUSY'});
2165
- expect(service.getSnapshot().value).toBe(TaskState.TERMINATED);
3250
+ service.send({type: TaskEvent.OUTBOUND_FAILED, taskData: failedTaskData, reason: 'AGENT_ENDS'});
3251
+ expect(service.getSnapshot().value).toBe(TaskState.WRAPPING_UP);
2166
3252
  });
2167
3253
 
2168
- it('transitions from OFFERED to WRAPPING_UP on OUTBOUND_FAILED when wrapup is required', () => {
3254
+ it('transitions from CONF_INITIATING to WRAPPING_UP on OUTBOUND_FAILED when wrapup is required', () => {
2169
3255
  const service = startMachine();
2170
- const offerTaskData = createTaskData({
3256
+ const taskData = createTaskData({
3257
+ consultingAgentId: 'agent-1',
2171
3258
  interaction: {
2172
3259
  outboundType: 'OUTDIAL',
2173
3260
  } as any,
2174
3261
  });
2175
3262
 
2176
- service.send({type: TaskEvent.TASK_INCOMING, taskData: offerTaskData});
2177
- expect(service.getSnapshot().value).toBe(TaskState.OFFERED);
3263
+ service.send({type: TaskEvent.TASK_INCOMING, taskData});
3264
+ service.send({type: TaskEvent.ASSIGN, taskData});
3265
+ service.send({type: TaskEvent.CONSULT, destination: 'agent-42', destinationType: 'agent'});
3266
+ service.send({type: TaskEvent.CONSULT_SUCCESS, taskData});
3267
+ service.send({type: TaskEvent.MERGE_TO_CONFERENCE});
3268
+ expect(service.getSnapshot().value).toBe(TaskState.CONF_INITIATING);
2178
3269
 
2179
3270
  const failedTaskData = createTaskData({
2180
3271
  agentId: 'agent-1',
@@ -2184,16 +3275,14 @@ describe('Task state machine', () => {
2184
3275
  isTerminated: true,
2185
3276
  } as any,
2186
3277
  });
2187
- service.send({type: TaskEvent.OUTBOUND_FAILED, taskData: failedTaskData, reason: 'CUSTOMER_BUSY'});
3278
+ service.send({type: TaskEvent.OUTBOUND_FAILED, taskData: failedTaskData, reason: 'AGENT_ENDS'});
2188
3279
  expect(service.getSnapshot().value).toBe(TaskState.WRAPPING_UP);
2189
3280
  });
2190
3281
  });
2191
3282
 
2192
3283
  describe('CONSULTING state TRANSFER_CONFERENCE_SUCCESS desktop-initiated (CAI-8329)', () => {
2193
3284
  const startAgent2Machine = () => {
2194
- const actor = createActor(
2195
- createTaskStateMachine({...createConfig(), agentId: 'agent-2'})
2196
- );
3285
+ const actor = createActor(createTaskStateMachine({...createConfig(), agentId: 'agent-2'}));
2197
3286
  actor.start();
2198
3287
 
2199
3288
  return actor;
@@ -2292,7 +3381,54 @@ describe('Task state machine', () => {
2292
3381
  } as any,
2293
3382
  });
2294
3383
 
2295
- const setupSecondaryAgentConsultingFromConference = (service: ReturnType<typeof startAgent2Machine>) => {
3384
+ const createSecondaryAgentRemovedTaskData = () =>
3385
+ createTaskData({
3386
+ agentId: 'agent-2',
3387
+ isConsulted: true,
3388
+ interactionId: 'interaction-1',
3389
+ mediaResourceId: 'interaction-1',
3390
+ consultMediaResourceId: 'consult-media-1',
3391
+ interaction: {
3392
+ state: 'conference',
3393
+ owner: 'agent-1',
3394
+ mainInteractionId: 'interaction-1',
3395
+ interactionId: 'interaction-1',
3396
+ participants: {
3397
+ 'agent-1': {
3398
+ id: 'agent-1',
3399
+ pType: 'Agent',
3400
+ hasLeft: false,
3401
+ consultState: 'conferencing',
3402
+ },
3403
+ 'agent-3': {
3404
+ id: 'agent-3',
3405
+ pType: 'Agent',
3406
+ hasLeft: false,
3407
+ isConsulted: true,
3408
+ consultState: 'consulting',
3409
+ },
3410
+ 'customer-1': {id: 'customer-1', pType: 'Customer', hasLeft: false},
3411
+ },
3412
+ media: {
3413
+ 'arbitrary-main-media-key': {
3414
+ mediaResourceId: 'interaction-1',
3415
+ mType: 'mainCall',
3416
+ participants: ['agent-1', 'customer-1'],
3417
+ isHold: false,
3418
+ },
3419
+ 'consult-media-1': {
3420
+ mediaResourceId: 'consult-media-1',
3421
+ mType: 'consult',
3422
+ participants: ['agent-2', 'agent-3'],
3423
+ isHold: false,
3424
+ },
3425
+ },
3426
+ } as any,
3427
+ });
3428
+
3429
+ const setupSecondaryAgentConsultingFromConference = (
3430
+ service: ReturnType<typeof startAgent2Machine>
3431
+ ) => {
2296
3432
  const conferenceTaskData = createSecondaryAgentConferenceBaseTaskData();
2297
3433
  const consultingTaskData = createSecondaryAgentConsultingTaskData();
2298
3434
 
@@ -2339,7 +3475,10 @@ describe('Task state machine', () => {
2339
3475
  setupSecondaryAgentDesktopConsultingFromConference(service);
2340
3476
 
2341
3477
  const transferSuccessTaskData = createSecondaryAgentConsultingTaskData();
2342
- service.send({type: TaskEvent.TRANSFER_CONFERENCE_SUCCESS, taskData: transferSuccessTaskData});
3478
+ service.send({
3479
+ type: TaskEvent.TRANSFER_CONFERENCE_SUCCESS,
3480
+ taskData: transferSuccessTaskData,
3481
+ });
2343
3482
 
2344
3483
  expect(service.getSnapshot().value).toBe(TaskState.TERMINATED);
2345
3484
  });
@@ -2349,7 +3488,10 @@ describe('Task state machine', () => {
2349
3488
  setupSecondaryAgentConsultingFromConference(service);
2350
3489
 
2351
3490
  const transferSuccessTaskData = createSecondaryAgentConsultingTaskData();
2352
- service.send({type: TaskEvent.TRANSFER_CONFERENCE_SUCCESS, taskData: transferSuccessTaskData});
3491
+ service.send({
3492
+ type: TaskEvent.TRANSFER_CONFERENCE_SUCCESS,
3493
+ taskData: transferSuccessTaskData,
3494
+ });
2353
3495
 
2354
3496
  const snapshot = service.getSnapshot();
2355
3497
  expect(snapshot.value).toBe(TaskState.TERMINATED);
@@ -2359,9 +3501,7 @@ describe('Task state machine', () => {
2359
3501
 
2360
3502
  it('moves consultee to CONFERENCING when primary agent conference-transfers', () => {
2361
3503
  const startAgent3Machine = () => {
2362
- const actor = createActor(
2363
- createTaskStateMachine({...createConfig(), agentId: 'agent-3'})
2364
- );
3504
+ const actor = createActor(createTaskStateMachine({...createConfig(), agentId: 'agent-3'}));
2365
3505
  actor.start();
2366
3506
 
2367
3507
  return actor;
@@ -2524,7 +3664,10 @@ describe('Task state machine', () => {
2524
3664
  expect(service.getSnapshot().value).toBe(TaskState.CONSULTING);
2525
3665
  expect(service.getSnapshot().context.consultInitiator).toBe(false);
2526
3666
 
2527
- service.send({type: TaskEvent.TRANSFER_CONFERENCE_SUCCESS, taskData: transferSuccessTaskData});
3667
+ service.send({
3668
+ type: TaskEvent.TRANSFER_CONFERENCE_SUCCESS,
3669
+ taskData: transferSuccessTaskData,
3670
+ });
2528
3671
  expect(service.getSnapshot().value).toBe(TaskState.CONFERENCING);
2529
3672
  });
2530
3673
 
@@ -2582,7 +3725,10 @@ describe('Task state machine', () => {
2582
3725
  expect(service.getSnapshot().value).toBe(TaskState.CONFERENCING);
2583
3726
  expect(service.getSnapshot().context.consultInitiator).toBe(false);
2584
3727
 
2585
- service.send({type: TaskEvent.TRANSFER_CONFERENCE_SUCCESS, taskData: transferSuccessTaskData});
3728
+ service.send({
3729
+ type: TaskEvent.TRANSFER_CONFERENCE_SUCCESS,
3730
+ taskData: transferSuccessTaskData,
3731
+ });
2586
3732
  expect(service.getSnapshot().value).toBe(TaskState.CONFERENCING);
2587
3733
  });
2588
3734
 
@@ -2592,11 +3738,87 @@ describe('Task state machine', () => {
2592
3738
 
2593
3739
  const transferSuccessTaskData = createSecondaryAgentConsultingTaskData();
2594
3740
  service.send({type: TaskEvent.TRANSFER_CONFERENCE});
2595
- service.send({type: TaskEvent.TRANSFER_CONFERENCE_SUCCESS, taskData: transferSuccessTaskData});
3741
+ service.send({
3742
+ type: TaskEvent.TRANSFER_CONFERENCE_SUCCESS,
3743
+ taskData: transferSuccessTaskData,
3744
+ });
2596
3745
 
2597
3746
  expect(service.getSnapshot().value).toBe(TaskState.TERMINATED);
2598
3747
  });
2599
3748
 
3749
+ it('emits wrap-up once on TRANSFER_CONFERENCE_SUCCESS from CONSULTING when wrap-up is required', () => {
3750
+ const {emitTaskWrapup, publishWrapup} = createPublishAwareEmitTaskWrapupMock();
3751
+ const service = createActor(
3752
+ createTaskStateMachine({...createConfig(), agentId: 'agent-1'}, {actions: {emitTaskWrapup}})
3753
+ );
3754
+ service.start();
3755
+ const taskData = createTaskData({
3756
+ agentId: 'agent-1',
3757
+ consultingAgentId: 'agent-1',
3758
+ interaction: {
3759
+ owner: 'agent-1',
3760
+ state: 'conference',
3761
+ mainInteractionId: 'interaction-1',
3762
+ interactionId: 'interaction-1',
3763
+ participants: {
3764
+ 'agent-1': {id: 'agent-1', pType: 'Agent', type: 'Agent', hasJoined: true, hasLeft: false},
3765
+ 'agent-2': {id: 'agent-2', pType: 'Agent', type: 'Agent', hasJoined: true, hasLeft: false},
3766
+ },
3767
+ } as any,
3768
+ });
3769
+
3770
+ service.send({type: TaskEvent.TASK_INCOMING, taskData});
3771
+ service.send({type: TaskEvent.ASSIGN, taskData});
3772
+ service.send({type: TaskEvent.CONSULT, destination: 'agent-2', destinationType: 'agent'});
3773
+ service.send({type: TaskEvent.CONSULT_SUCCESS, taskData});
3774
+ expect(service.getSnapshot().value).toBe(TaskState.CONSULTING);
3775
+
3776
+ service.send({type: TaskEvent.TRANSFER_CONFERENCE});
3777
+ service.send({
3778
+ type: TaskEvent.TRANSFER_CONFERENCE_SUCCESS,
3779
+ taskData: {...taskData, wrapUpRequired: true},
3780
+ });
3781
+
3782
+ expect(service.getSnapshot().value).toBe(TaskState.WRAPPING_UP);
3783
+ expect(publishWrapup).toHaveBeenCalledTimes(1);
3784
+ });
3785
+
3786
+ it('emits wrap-up once on TRANSFER_CONFERENCE_SUCCESS from CONFERENCING when wrap-up is required', () => {
3787
+ const {emitTaskWrapup, publishWrapup} = createPublishAwareEmitTaskWrapupMock();
3788
+ const service = createActor(
3789
+ createTaskStateMachine({...createConfig(), agentId: 'agent-1'}, {actions: {emitTaskWrapup}})
3790
+ );
3791
+ service.start();
3792
+ const taskData = createTaskData({
3793
+ agentId: 'agent-1',
3794
+ interaction: {
3795
+ owner: 'agent-1',
3796
+ state: 'conference',
3797
+ mainInteractionId: 'interaction-1',
3798
+ interactionId: 'interaction-1',
3799
+ participants: {
3800
+ 'agent-1': {id: 'agent-1', pType: 'Agent', type: 'Agent', hasJoined: true, hasLeft: false},
3801
+ 'agent-2': {id: 'agent-2', pType: 'Agent', type: 'Agent', hasJoined: true, hasLeft: false},
3802
+ },
3803
+ } as any,
3804
+ });
3805
+
3806
+ service.send({type: TaskEvent.TASK_INCOMING, taskData});
3807
+ service.send({type: TaskEvent.ASSIGN, taskData});
3808
+ service.send({type: TaskEvent.MERGE_TO_CONFERENCE});
3809
+ service.send({type: TaskEvent.CONFERENCE_START, taskData});
3810
+ expect(service.getSnapshot().value).toBe(TaskState.CONFERENCING);
3811
+
3812
+ service.send({type: TaskEvent.TRANSFER_CONFERENCE});
3813
+ service.send({
3814
+ type: TaskEvent.TRANSFER_CONFERENCE_SUCCESS,
3815
+ taskData: {...taskData, wrapUpRequired: true},
3816
+ });
3817
+
3818
+ expect(service.getSnapshot().value).toBe(TaskState.WRAPPING_UP);
3819
+ expect(publishWrapup).toHaveBeenCalledTimes(1);
3820
+ });
3821
+
2600
3822
  it('terminates secondary agent on PARTICIPANT_LEAVE while in CONSULTING during conference', () => {
2601
3823
  const service = startAgent2Machine();
2602
3824
  setupSecondaryAgentDesktopConsultingFromConference(service);
@@ -2647,5 +3869,192 @@ describe('Task state machine', () => {
2647
3869
 
2648
3870
  expect(service.getSnapshot().value).toBe(TaskState.TERMINATED);
2649
3871
  });
3872
+
3873
+ it('terminates secondary agent on PARTICIPANT_LEAVE while consult is initiating', () => {
3874
+ const service = startAgent2Machine();
3875
+ const conferenceTaskData = createSecondaryAgentConferenceBaseTaskData();
3876
+
3877
+ service.send({type: TaskEvent.TASK_INCOMING, taskData: conferenceTaskData});
3878
+ service.send({type: TaskEvent.ASSIGN, taskData: conferenceTaskData});
3879
+ service.send({type: TaskEvent.CONFERENCE_START, taskData: conferenceTaskData});
3880
+ service.send({type: TaskEvent.CONSULT, destination: 'agent-3', destinationType: 'agent'});
3881
+ expect(service.getSnapshot().value).toBe(TaskState.CONSULT_INITIATING);
3882
+ expect(service.getSnapshot().context.taskData.interaction).toBe(
3883
+ conferenceTaskData.interaction
3884
+ );
3885
+ expect(service.getSnapshot().context.taskData.destAgentId).toBe('agent-3');
3886
+
3887
+ const participantLeaveEvent = {
3888
+ type: TaskEvent.PARTICIPANT_LEAVE,
3889
+ taskData: createSecondaryAgentRemovedTaskData(),
3890
+ } as const;
3891
+ expect(
3892
+ guards.didCurrentAgentLeaveMainInteraction({
3893
+ context: service.getSnapshot().context,
3894
+ event: participantLeaveEvent,
3895
+ })
3896
+ ).toBe(true);
3897
+
3898
+ service.send(participantLeaveEvent);
3899
+
3900
+ expect(service.getSnapshot().value).toBe(TaskState.TERMINATED);
3901
+ });
3902
+
3903
+ it('terminates secondary agent on CONSULT_END while consult is initiating', () => {
3904
+ const service = startAgent2Machine();
3905
+ const conferenceTaskData = createSecondaryAgentConferenceBaseTaskData();
3906
+
3907
+ service.send({type: TaskEvent.TASK_INCOMING, taskData: conferenceTaskData});
3908
+ service.send({type: TaskEvent.ASSIGN, taskData: conferenceTaskData});
3909
+ service.send({type: TaskEvent.CONFERENCE_START, taskData: conferenceTaskData});
3910
+ service.send({type: TaskEvent.CONSULT, destination: 'agent-3', destinationType: 'agent'});
3911
+ expect(service.getSnapshot().value).toBe(TaskState.CONSULT_INITIATING);
3912
+
3913
+ const consultEndEvent = {
3914
+ type: TaskEvent.CONSULT_END,
3915
+ taskData: createSecondaryAgentRemovedTaskData(),
3916
+ } as const;
3917
+ expect(
3918
+ guards.didCurrentAgentLeaveMainInteraction({
3919
+ context: service.getSnapshot().context,
3920
+ event: consultEndEvent,
3921
+ })
3922
+ ).toBe(true);
3923
+
3924
+ service.send(consultEndEvent);
3925
+
3926
+ expect(service.getSnapshot().value).toBe(TaskState.TERMINATED);
3927
+ });
3928
+
3929
+ it('terminates a consulting secondary initiator when CONSULT_END removes it from mainCall', () => {
3930
+ const service = startAgent2Machine();
3931
+ setupSecondaryAgentConsultingFromConference(service);
3932
+
3933
+ service.send({
3934
+ type: TaskEvent.CONSULT_END,
3935
+ taskData: createSecondaryAgentRemovedTaskData(),
3936
+ });
3937
+
3938
+ expect(service.getSnapshot().value).toBe(TaskState.TERMINATED);
3939
+ expect(service.getSnapshot().context.consultInitiator).toBe(false);
3940
+ expect(service.getSnapshot().context.consultDestinationAgentJoined).toBe(false);
3941
+ });
3942
+
3943
+ it('does not infer departure from a partial CONSULT_END payload', () => {
3944
+ const service = startAgent2Machine();
3945
+ setupSecondaryAgentConsultingFromConference(service);
3946
+ const partialTaskData = {
3947
+ ...createTaskData({agentId: 'agent-2', interactionId: 'interaction-1'}),
3948
+ interaction: {
3949
+ state: 'conference',
3950
+ interactionId: 'interaction-1',
3951
+ mainInteractionId: 'interaction-1',
3952
+ },
3953
+ } as any;
3954
+
3955
+ const consultEndEvent = {type: TaskEvent.CONSULT_END, taskData: partialTaskData} as const;
3956
+ expect(service.getSnapshot().context.consultInitiator).toBe(true);
3957
+ expect(
3958
+ guards.didCurrentAgentLeaveMainInteraction({
3959
+ context: service.getSnapshot().context,
3960
+ event: consultEndEvent,
3961
+ })
3962
+ ).toBe(false);
3963
+
3964
+ service.send(consultEndEvent);
3965
+
3966
+ expect(service.getSnapshot().value).toBe(TaskState.CONNECTED);
3967
+ });
3968
+
3969
+ it('emits consult-end and task-end when AgentConsultEnded terminates the consulted agent', () => {
3970
+ const emitTaskConsultEnd = jest.fn();
3971
+ const emitTaskEnd = jest.fn();
3972
+ const cleanupResources = jest.fn();
3973
+ const service = createActor(
3974
+ createTaskStateMachine(
3975
+ {...createConfig(), agentId: 'agent-3'},
3976
+ {
3977
+ actions: {emitTaskConsultEnd, emitTaskEnd, cleanupResources},
3978
+ }
3979
+ )
3980
+ );
3981
+ service.start();
3982
+
3983
+ const consultingTaskData = createTaskData({
3984
+ agentId: 'agent-3',
3985
+ isConsulted: true,
3986
+ interactionId: 'consult-child',
3987
+ interaction: {
3988
+ state: 'consulting',
3989
+ owner: 'agent-1',
3990
+ interactionId: 'consult-child',
3991
+ mainInteractionId: 'interaction-1',
3992
+ participants: {
3993
+ 'agent-1': {id: 'agent-1', pType: 'Agent', hasLeft: false},
3994
+ 'agent-2': {
3995
+ id: 'agent-2',
3996
+ pType: 'Agent',
3997
+ hasLeft: false,
3998
+ consultState: 'consulting',
3999
+ },
4000
+ 'agent-3': {
4001
+ id: 'agent-3',
4002
+ pType: 'Agent',
4003
+ hasLeft: false,
4004
+ isConsulted: true,
4005
+ consultState: 'consulting',
4006
+ },
4007
+ 'customer-1': {id: 'customer-1', pType: 'Customer', hasLeft: false},
4008
+ },
4009
+ media: {
4010
+ 'interaction-1': {
4011
+ mediaResourceId: 'interaction-1',
4012
+ mType: 'mainCall',
4013
+ participants: ['agent-1', 'agent-2', 'customer-1'],
4014
+ isHold: true,
4015
+ },
4016
+ 'consult-child': {
4017
+ mediaResourceId: 'consult-child',
4018
+ mType: 'consult',
4019
+ participants: ['agent-2', 'agent-3'],
4020
+ isHold: false,
4021
+ },
4022
+ },
4023
+ } as any,
4024
+ });
4025
+ service.send({type: TaskEvent.HYDRATE, taskData: consultingTaskData});
4026
+ expect(service.getSnapshot().value).toBe(TaskState.CONSULTING);
4027
+
4028
+ const consultEndedTaskData = createTaskData({
4029
+ ...consultingTaskData,
4030
+ type: 'AgentConsultEnded' as any,
4031
+ interaction: {
4032
+ ...consultingTaskData.interaction,
4033
+ state: 'conference',
4034
+ participants: {
4035
+ 'agent-1': {id: 'agent-1', pType: 'Agent', hasLeft: false},
4036
+ 'agent-2': {id: 'agent-2', pType: 'Agent', hasLeft: false},
4037
+ 'customer-1': {id: 'customer-1', pType: 'Customer', hasLeft: false},
4038
+ },
4039
+ media: {
4040
+ 'interaction-1': {
4041
+ mediaResourceId: 'interaction-1',
4042
+ mType: 'mainCall',
4043
+ participants: ['agent-1', 'agent-2', 'customer-1'],
4044
+ isHold: false,
4045
+ },
4046
+ },
4047
+ } as any,
4048
+ });
4049
+
4050
+ service.send({type: TaskEvent.CONSULT_END, taskData: consultEndedTaskData});
4051
+
4052
+ expect(service.getSnapshot().value).toBe(TaskState.TERMINATED);
4053
+ expect(emitTaskConsultEnd).toHaveBeenCalledTimes(1);
4054
+ expect(emitTaskEnd).toHaveBeenCalledTimes(1);
4055
+ expect(cleanupResources).toHaveBeenCalledTimes(1);
4056
+ expect(service.getSnapshot().context.consultDestinationAgentJoined).toBe(false);
4057
+ expect(service.getSnapshot().context.consultInitiator).toBe(false);
4058
+ });
2650
4059
  });
2651
4060
  });