1+ import  {  describe ,  it ,  expect ,  vi ,  beforeEach ,  afterEach  }  from  'vitest' ; 
2+ import  {  
3+   createCaseInstanceWithMethods , 
4+   CaseInstancesServiceModel 
5+ }  from  '../../../../src/models/maestro/case-instances.models' ; 
6+ import  {  
7+   MAESTRO_TEST_CONSTANTS , 
8+   TEST_CONSTANTS , 
9+   createMockOperationResponse , 
10+   createMockCaseInstance , 
11+   createMockCaseInstanceExecutionHistory , 
12+   createMockCaseStage 
13+ }  from  '../../../utils/mocks' ; 
14+ import  type  {  
15+   CaseInstanceOperationOptions , 
16+ }  from  '../../../../src/models/maestro/case-instances.types' ; 
17+ 
18+ // ===== TEST SUITE ===== 
19+ describe ( 'Case Instance Models' ,  ( )  =>  { 
20+   let  mockService : CaseInstancesServiceModel ; 
21+ 
22+   beforeEach ( ( )  =>  { 
23+     // Create a mock service 
24+     mockService  =  { 
25+       getAll : vi . fn ( ) , 
26+       getById : vi . fn ( ) , 
27+       close : vi . fn ( ) , 
28+       pause : vi . fn ( ) , 
29+       resume : vi . fn ( ) , 
30+       getExecutionHistory : vi . fn ( ) , 
31+       getStages : vi . fn ( ) , 
32+       getActionTasks : vi . fn ( ) 
33+     }  as  any ; 
34+   } ) ; 
35+ 
36+   afterEach ( ( )  =>  { 
37+     vi . clearAllMocks ( ) ; 
38+   } ) ; 
39+ 
40+   describe ( 'bound methods on case instance' ,  ( )  =>  { 
41+     describe ( 'caseInstance.close()' ,  ( )  =>  { 
42+       it ( 'should call caseInstance.close with bound instanceId and folderKey' ,  async  ( )  =>  { 
43+         const  mockInstanceData  =  createMockCaseInstance ( ) ; 
44+         const  instance  =  createCaseInstanceWithMethods ( mockInstanceData ,  mockService ) ; 
45+         
46+         const  mockResponse  =  createMockOperationResponse ( { 
47+           instanceId : MAESTRO_TEST_CONSTANTS . CASE_INSTANCE_ID , 
48+           status : TEST_CONSTANTS . CANCELLED 
49+         } ) ; 
50+         mockService . close  =  vi . fn ( ) . mockResolvedValue ( mockResponse ) ; 
51+ 
52+         await  instance . close ( ) ; 
53+ 
54+         expect ( mockService . close ) . toHaveBeenCalledWith ( 
55+           MAESTRO_TEST_CONSTANTS . CASE_INSTANCE_ID , 
56+           MAESTRO_TEST_CONSTANTS . FOLDER_KEY , 
57+           undefined 
58+         ) ; 
59+       } ) ; 
60+ 
61+       it ( 'should call caseInstance.close with bound parameters and options' ,  async  ( )  =>  { 
62+         const  mockInstanceData  =  createMockCaseInstance ( ) ; 
63+         const  instance  =  createCaseInstanceWithMethods ( mockInstanceData ,  mockService ) ; 
64+         
65+         const  mockResponse  =  createMockOperationResponse ( { 
66+           instanceId : MAESTRO_TEST_CONSTANTS . CASE_INSTANCE_ID , 
67+           status : TEST_CONSTANTS . CANCELLED 
68+         } ) ; 
69+         const  options : CaseInstanceOperationOptions  =  {  comment : MAESTRO_TEST_CONSTANTS . TEST_COMMENT  } ; 
70+         mockService . close  =  vi . fn ( ) . mockResolvedValue ( mockResponse ) ; 
71+ 
72+         await  instance . close ( options ) ; 
73+ 
74+         expect ( mockService . close ) . toHaveBeenCalledWith ( 
75+           MAESTRO_TEST_CONSTANTS . CASE_INSTANCE_ID , 
76+           MAESTRO_TEST_CONSTANTS . FOLDER_KEY , 
77+           options 
78+         ) ; 
79+       } ) ; 
80+ 
81+       it ( 'should throw error if instanceId is undefined' ,  async  ( )  =>  { 
82+         const  mockInstanceData  =  createMockCaseInstance ( ) ; 
83+         const  invalidInstanceData  =  {  ...mockInstanceData ,  instanceId : undefined  as  any  } ; 
84+         const  invalidInstance  =  createCaseInstanceWithMethods ( invalidInstanceData ,  mockService ) ; 
85+         
86+         await  expect ( invalidInstance . close ( ) ) . rejects . toThrow ( 'Case instance ID is undefined' ) ; 
87+       } ) ; 
88+ 
89+       it ( 'should throw error if folderKey is undefined' ,  async  ( )  =>  { 
90+         const  mockInstanceData  =  createMockCaseInstance ( ) ; 
91+         const  invalidInstanceData  =  {  ...mockInstanceData ,  folderKey : undefined  as  any  } ; 
92+         const  invalidInstance  =  createCaseInstanceWithMethods ( invalidInstanceData ,  mockService ) ; 
93+         
94+         await  expect ( invalidInstance . close ( ) ) . rejects . toThrow ( 'Case instance folder key is undefined' ) ; 
95+       } ) ; 
96+     } ) ; 
97+ 
98+     describe ( 'caseInstance.pause()' ,  ( )  =>  { 
99+       it ( 'should call caseInstance.pause with bound instanceId and folderKey' ,  async  ( )  =>  { 
100+         const  mockInstanceData  =  createMockCaseInstance ( ) ; 
101+         const  instance  =  createCaseInstanceWithMethods ( mockInstanceData ,  mockService ) ; 
102+         
103+         const  mockResponse  =  createMockOperationResponse ( { 
104+           instanceId : MAESTRO_TEST_CONSTANTS . CASE_INSTANCE_ID , 
105+           status : MAESTRO_TEST_CONSTANTS . TASK_STATUS_PAUSED 
106+         } ) ; 
107+         mockService . pause  =  vi . fn ( ) . mockResolvedValue ( mockResponse ) ; 
108+         
109+         await  instance . pause ( ) ; 
110+         
111+         expect ( mockService . pause ) . toHaveBeenCalledWith ( 
112+           MAESTRO_TEST_CONSTANTS . CASE_INSTANCE_ID , 
113+           MAESTRO_TEST_CONSTANTS . FOLDER_KEY , 
114+           undefined 
115+         ) ; 
116+       } ) ; 
117+ 
118+       it ( 'should call caseInstance.pause with bound parameters and options' ,  async  ( )  =>  { 
119+         const  mockInstanceData  =  createMockCaseInstance ( ) ; 
120+         const  instance  =  createCaseInstanceWithMethods ( mockInstanceData ,  mockService ) ; 
121+         
122+         const  mockResponse  =  createMockOperationResponse ( { 
123+           instanceId : MAESTRO_TEST_CONSTANTS . CASE_INSTANCE_ID , 
124+           status : MAESTRO_TEST_CONSTANTS . TASK_STATUS_PAUSED 
125+         } ) ; 
126+         const  options : CaseInstanceOperationOptions  =  {  comment : MAESTRO_TEST_CONSTANTS . TEST_COMMENT  } ; 
127+         mockService . pause  =  vi . fn ( ) . mockResolvedValue ( mockResponse ) ; 
128+         
129+         await  instance . pause ( options ) ; 
130+ 
131+         expect ( mockService . pause ) . toHaveBeenCalledWith ( 
132+           MAESTRO_TEST_CONSTANTS . CASE_INSTANCE_ID , 
133+           MAESTRO_TEST_CONSTANTS . FOLDER_KEY , 
134+           options 
135+         ) ; 
136+       } ) ; 
137+ 
138+       it ( 'should throw error if instanceId is undefined' ,  async  ( )  =>  { 
139+         const  mockInstanceData  =  createMockCaseInstance ( ) ; 
140+         const  invalidInstanceData  =  {  ...mockInstanceData ,  instanceId : undefined  as  any  } ; 
141+         const  invalidInstance  =  createCaseInstanceWithMethods ( invalidInstanceData ,  mockService ) ; 
142+         
143+         await  expect ( invalidInstance . pause ( ) ) . rejects . toThrow ( 'Case instance ID is undefined' ) ; 
144+       } ) ; 
145+ 
146+       it ( 'should throw error if folderKey is undefined' ,  async  ( )  =>  { 
147+         const  mockInstanceData  =  createMockCaseInstance ( ) ; 
148+         const  invalidInstanceData  =  {  ...mockInstanceData ,  folderKey : undefined  as  any  } ; 
149+         const  invalidInstance  =  createCaseInstanceWithMethods ( invalidInstanceData ,  mockService ) ; 
150+         
151+         await  expect ( invalidInstance . pause ( ) ) . rejects . toThrow ( 'Case instance folder key is undefined' ) ; 
152+       } ) ; 
153+     } ) ; 
154+ 
155+     describe ( 'caseInstance.resume()' ,  ( )  =>  { 
156+       it ( 'should call caseInstance.resume with bound instanceId and folderKey' ,  async  ( )  =>  { 
157+         const  mockInstanceData  =  createMockCaseInstance ( ) ; 
158+         const  instance  =  createCaseInstanceWithMethods ( mockInstanceData ,  mockService ) ; 
159+         
160+         const  mockResponse  =  createMockOperationResponse ( { 
161+           instanceId : MAESTRO_TEST_CONSTANTS . CASE_INSTANCE_ID , 
162+           status : TEST_CONSTANTS . RUNNING 
163+         } ) ; 
164+         mockService . resume  =  vi . fn ( ) . mockResolvedValue ( mockResponse ) ; 
165+ 
166+         await  instance . resume ( ) ; 
167+ 
168+         expect ( mockService . resume ) . toHaveBeenCalledWith ( 
169+           MAESTRO_TEST_CONSTANTS . CASE_INSTANCE_ID , 
170+           MAESTRO_TEST_CONSTANTS . FOLDER_KEY , 
171+           undefined 
172+         ) ; 
173+       } ) ; 
174+ 
175+       it ( 'should call caseInstance.resume with bound parameters and options' ,  async  ( )  =>  { 
176+         const  mockInstanceData  =  createMockCaseInstance ( ) ; 
177+         const  instance  =  createCaseInstanceWithMethods ( mockInstanceData ,  mockService ) ; 
178+         
179+         const  mockResponse  =  createMockOperationResponse ( { 
180+           instanceId : MAESTRO_TEST_CONSTANTS . CASE_INSTANCE_ID , 
181+           status : TEST_CONSTANTS . RUNNING 
182+         } ) ; 
183+         const  options : CaseInstanceOperationOptions  =  {  comment : MAESTRO_TEST_CONSTANTS . TEST_COMMENT  } ; 
184+         mockService . resume  =  vi . fn ( ) . mockResolvedValue ( mockResponse ) ; 
185+ 
186+         await  instance . resume ( options ) ; 
187+         
188+         expect ( mockService . resume ) . toHaveBeenCalledWith ( 
189+           MAESTRO_TEST_CONSTANTS . CASE_INSTANCE_ID , 
190+           MAESTRO_TEST_CONSTANTS . FOLDER_KEY , 
191+           options 
192+         ) ; 
193+       } ) ; 
194+ 
195+       it ( 'should throw error if instanceId is undefined' ,  async  ( )  =>  { 
196+         const  mockInstanceData  =  createMockCaseInstance ( ) ; 
197+         const  invalidInstanceData  =  {  ...mockInstanceData ,  instanceId : undefined  as  any  } ; 
198+         const  invalidInstance  =  createCaseInstanceWithMethods ( invalidInstanceData ,  mockService ) ; 
199+ 
200+         await  expect ( invalidInstance . resume ( ) ) . rejects . toThrow ( 'Case instance ID is undefined' ) ; 
201+       } ) ; 
202+ 
203+       it ( 'should throw error if folderKey is undefined' ,  async  ( )  =>  { 
204+         const  mockInstanceData  =  createMockCaseInstance ( ) ; 
205+         const  invalidInstanceData  =  {  ...mockInstanceData ,  folderKey : undefined  as  any  } ; 
206+         const  invalidInstance  =  createCaseInstanceWithMethods ( invalidInstanceData ,  mockService ) ; 
207+ 
208+         await  expect ( invalidInstance . resume ( ) ) . rejects . toThrow ( 'Case instance folder key is undefined' ) ; 
209+       } ) ; 
210+     } ) ; 
211+ 
212+     describe ( 'caseInstance.getExecutionHistory()' ,  ( )  =>  { 
213+       it ( 'should call caseInstance.getExecutionHistory with bound instanceId and folderKey' ,  async  ( )  =>  { 
214+         const  mockInstanceData  =  createMockCaseInstance ( ) ; 
215+         const  instance  =  createCaseInstanceWithMethods ( mockInstanceData ,  mockService ) ; 
216+         
217+         const  mockHistory  =  createMockCaseInstanceExecutionHistory ( ) ; 
218+         mockService . getExecutionHistory  =  vi . fn ( ) . mockResolvedValue ( mockHistory ) ; 
219+         
220+         const  result  =  await  instance . getExecutionHistory ( ) ; 
221+ 
222+         expect ( mockService . getExecutionHistory ) . toHaveBeenCalledWith ( 
223+           MAESTRO_TEST_CONSTANTS . CASE_INSTANCE_ID , 
224+           MAESTRO_TEST_CONSTANTS . FOLDER_KEY 
225+         ) ; 
226+         expect ( result ) . toEqual ( mockHistory ) ; 
227+       } ) ; 
228+ 
229+       it ( 'should throw error if instanceId is undefined' ,  async  ( )  =>  { 
230+         const  mockInstanceData  =  createMockCaseInstance ( ) ; 
231+         const  invalidInstanceData  =  {  ...mockInstanceData ,  instanceId : undefined  as  any  } ; 
232+         const  invalidInstance  =  createCaseInstanceWithMethods ( invalidInstanceData ,  mockService ) ; 
233+         
234+         await  expect ( invalidInstance . getExecutionHistory ( ) ) . rejects . toThrow ( 'Case instance ID is undefined' ) ; 
235+       } ) ; 
236+ 
237+       it ( 'should throw error if folderKey is undefined' ,  async  ( )  =>  { 
238+         const  mockInstanceData  =  createMockCaseInstance ( ) ; 
239+         const  invalidInstanceData  =  {  ...mockInstanceData ,  folderKey : undefined  as  any  } ; 
240+         const  invalidInstance  =  createCaseInstanceWithMethods ( invalidInstanceData ,  mockService ) ; 
241+ 
242+         await  expect ( invalidInstance . getExecutionHistory ( ) ) . rejects . toThrow ( 'Case instance folder key is undefined' ) ; 
243+       } ) ; 
244+     } ) ; 
245+ 
246+     describe ( 'caseInstance.getStages()' ,  ( )  =>  { 
247+       it ( 'should call caseInstance.getStages with bound instanceId and folderKey' ,  async  ( )  =>  { 
248+         const  mockInstanceData  =  createMockCaseInstance ( ) ; 
249+         const  instance  =  createCaseInstanceWithMethods ( mockInstanceData ,  mockService ) ; 
250+         
251+         const  mockStages  =  [ createMockCaseStage ( ) ] ; 
252+         mockService . getStages  =  vi . fn ( ) . mockResolvedValue ( mockStages ) ; 
253+         
254+         const  result  =  await  instance . getStages ( ) ; 
255+ 
256+         expect ( mockService . getStages ) . toHaveBeenCalledWith ( 
257+           MAESTRO_TEST_CONSTANTS . CASE_INSTANCE_ID , 
258+           MAESTRO_TEST_CONSTANTS . FOLDER_KEY 
259+         ) ; 
260+         expect ( result ) . toEqual ( mockStages ) ; 
261+       } ) ; 
262+ 
263+       it ( 'should throw error if instanceId is undefined' ,  async  ( )  =>  { 
264+         const  mockInstanceData  =  createMockCaseInstance ( ) ; 
265+         const  invalidInstanceData  =  {  ...mockInstanceData ,  instanceId : undefined  as  any  } ; 
266+         const  invalidInstance  =  createCaseInstanceWithMethods ( invalidInstanceData ,  mockService ) ; 
267+ 
268+         await  expect ( invalidInstance . getStages ( ) ) . rejects . toThrow ( 'Case instance ID is undefined' ) ; 
269+       } ) ; 
270+ 
271+       it ( 'should throw error if folderKey is undefined' ,  async  ( )  =>  { 
272+         const  mockInstanceData  =  createMockCaseInstance ( ) ; 
273+         const  invalidInstanceData  =  {  ...mockInstanceData ,  folderKey : undefined  as  any  } ; 
274+         const  invalidInstance  =  createCaseInstanceWithMethods ( invalidInstanceData ,  mockService ) ; 
275+ 
276+         await  expect ( invalidInstance . getStages ( ) ) . rejects . toThrow ( 'Case instance folder key is undefined' ) ; 
277+       } ) ; 
278+     } ) ; 
279+ 
280+     describe ( 'caseInstance.getActionTasks()' ,  ( )  =>  { 
281+       it ( 'should call caseInstance.getActionTasks with bound instanceId' ,  async  ( )  =>  { 
282+         const  mockInstanceData  =  createMockCaseInstance ( ) ; 
283+         const  instance  =  createCaseInstanceWithMethods ( mockInstanceData ,  mockService ) ; 
284+         
285+         const  mockTasks  =  { 
286+           items : [ ] , 
287+           totalCount : 0 
288+         } ; 
289+         mockService . getActionTasks  =  vi . fn ( ) . mockResolvedValue ( mockTasks ) ; 
290+         
291+         const  result  =  await  instance . getActionTasks ( ) ; 
292+ 
293+         expect ( mockService . getActionTasks ) . toHaveBeenCalledWith ( 
294+           MAESTRO_TEST_CONSTANTS . CASE_INSTANCE_ID , 
295+           undefined 
296+         ) ; 
297+         expect ( result ) . toEqual ( mockTasks ) ; 
298+       } ) ; 
299+ 
300+       it ( 'should call caseInstance.getActionTasks with bound parameters and options' ,  async  ( )  =>  { 
301+         const  mockInstanceData  =  createMockCaseInstance ( ) ; 
302+         const  instance  =  createCaseInstanceWithMethods ( mockInstanceData ,  mockService ) ; 
303+         
304+         const  mockTasks  =  { 
305+           items : [ ] , 
306+           totalCount : 0 
307+         } ; 
308+         const  options  =  {  pageSize : 10  } ; 
309+         mockService . getActionTasks  =  vi . fn ( ) . mockResolvedValue ( mockTasks ) ; 
310+         
311+         const  result  =  await  instance . getActionTasks ( options ) ; 
312+ 
313+         expect ( mockService . getActionTasks ) . toHaveBeenCalledWith ( 
314+           MAESTRO_TEST_CONSTANTS . CASE_INSTANCE_ID , 
315+           options 
316+         ) ; 
317+         expect ( result ) . toEqual ( mockTasks ) ; 
318+       } ) ; 
319+ 
320+       it ( 'should throw error if instanceId is undefined' ,  async  ( )  =>  { 
321+         const  mockInstanceData  =  createMockCaseInstance ( ) ; 
322+         const  invalidInstanceData  =  {  ...mockInstanceData ,  instanceId : undefined  as  any  } ; 
323+         const  invalidInstance  =  createCaseInstanceWithMethods ( invalidInstanceData ,  mockService ) ; 
324+         
325+         await  expect ( invalidInstance . getActionTasks ( ) ) . rejects . toThrow ( 'Case instance ID is undefined' ) ; 
326+       } ) ; 
327+     } ) ; 
328+   } ) ; 
329+ 
330+   describe ( 'createCaseInstanceWithMethods' ,  ( )  =>  { 
331+     it ( 'should create instance with all bound methods' ,  ( )  =>  { 
332+       const  mockInstanceData  =  createMockCaseInstance ( ) ; 
333+       
334+       const  instance  =  createCaseInstanceWithMethods ( mockInstanceData ,  mockService ) ; 
335+       
336+       expect ( instance ) . toHaveProperty ( 'instanceId' ,  MAESTRO_TEST_CONSTANTS . CASE_INSTANCE_ID ) ; 
337+       expect ( instance ) . toHaveProperty ( 'folderKey' ,  MAESTRO_TEST_CONSTANTS . FOLDER_KEY ) ; 
338+       expect ( typeof  instance . close ) . toBe ( 'function' ) ; 
339+       expect ( typeof  instance . pause ) . toBe ( 'function' ) ; 
340+       expect ( typeof  instance . resume ) . toBe ( 'function' ) ; 
341+       expect ( typeof  instance . getExecutionHistory ) . toBe ( 'function' ) ; 
342+       expect ( typeof  instance . getStages ) . toBe ( 'function' ) ; 
343+       expect ( typeof  instance . getActionTasks ) . toBe ( 'function' ) ; 
344+     } ) ; 
345+ 
346+     it ( 'should preserve all original instance data' ,  ( )  =>  { 
347+       const  mockInstanceData  =  createMockCaseInstance ( ) ; 
348+       
349+       const  instance  =  createCaseInstanceWithMethods ( mockInstanceData ,  mockService ) ; 
350+ 
351+       expect ( instance . instanceId ) . toBe ( mockInstanceData . instanceId ) ; 
352+       expect ( instance . packageId ) . toBe ( mockInstanceData . packageId ) ; 
353+       expect ( instance . caseType ) . toBe ( mockInstanceData . caseType ) ; 
354+       expect ( instance . caseTitle ) . toBe ( mockInstanceData . caseTitle ) ; 
355+     } ) ; 
356+ 
357+   } ) ; 
358+ } ) ; 
0 commit comments