@@ -7,17 +7,28 @@ import { ScenarioService } from '@/scenario/scenario.service';
77import { instantiate , InstantiateEvent , step } from '@letsflow/core/process' ;
88import { normalize } from '@letsflow/core/scenario' ;
99import { uuid } from '@letsflow/core' ;
10- import { from as bsonUUID } from 'uuid-mongodb' ;
10+ import { from as bsonUUID , MUUID } from 'uuid-mongodb' ;
1111import { NotifyService } from '@/notify/notify.service' ;
1212import { ConfigModule } from '@/common/config/config.module' ;
1313import { ScenarioDbService } from '@/scenario/scenario-db/scenario-db.service' ;
1414import { EventEmitter2 } from '@nestjs/event-emitter' ;
1515
16+ function normalizeBsonUUID ( process : { _id : MUUID ; scenario : MUUID ; [ _ : string ] : any } ) {
17+ const { _id : id , scenario, ...rest } = process ;
18+
19+ return {
20+ _id : id . toString ( ) ,
21+ scenario : scenario . toString ( ) ,
22+ ...rest ,
23+ } ;
24+ }
25+
1626describe ( 'ProcessService' , ( ) => {
1727 let module : TestingModule ;
1828 let service : ProcessService ;
19- let collection : Collection ;
20- let scenarios : ScenarioService ;
29+ let collection : jest . Mocked < Collection > ;
30+ let scenarios : jest . Mocked < ScenarioService > ;
31+ let eventEmitter : jest . Mocked < EventEmitter2 > ;
2132
2233 beforeEach ( async ( ) => {
2334 module = await Test . createTestingModule ( {
@@ -31,8 +42,9 @@ describe('ProcessService', () => {
3142 ] ,
3243 } ) . compile ( ) ;
3344
34- service = module . get < ProcessService > ( ProcessService ) ;
35- scenarios = module . get < ScenarioService > ( ScenarioService ) ;
45+ service = module . get ( ProcessService ) ;
46+ scenarios = module . get ( ScenarioService ) ;
47+ eventEmitter = module . get ( EventEmitter2 ) ;
3648
3749 collection = {
3850 findOne : jest . fn ( ) ,
@@ -173,13 +185,11 @@ describe('ProcessService', () => {
173185 const process = step ( instantiate ( scenario ) , 'start' ) ;
174186
175187 it ( 'should save a process' , async ( ) => {
176- const replaceOne = jest . spyOn ( collection , 'replaceOne' ) . mockResolvedValue ( { } as any ) ;
177-
178188 await service . save ( process ) ;
179189
180- expect ( replaceOne ) . toHaveBeenCalled ( ) ;
181- expect ( replaceOne . mock . calls [ 0 ] [ 0 ] . _id . toString ( ) ) . toEqual ( process . id ) ;
182- const stored = replaceOne . mock . calls [ 0 ] [ 1 ] ;
190+ expect ( collection . replaceOne ) . toHaveBeenCalled ( ) ;
191+ expect ( collection . replaceOne . mock . calls [ 0 ] [ 0 ] . _id . toString ( ) ) . toEqual ( process . id ) ;
192+ const stored = collection . replaceOne . mock . calls [ 0 ] [ 1 ] ;
183193
184194 expect ( stored . _id . toString ( ) ) . toEqual ( process . id ) ;
185195 expect ( stored . scenario . toString ( ) ) . toEqual ( process . scenario . id ) ;
@@ -209,8 +219,6 @@ describe('ProcessService', () => {
209219
210220 it ( 'should instantiate a process' , async ( ) => {
211221 jest . spyOn ( scenarios , 'get' ) . mockResolvedValue ( { id : scenarioId , ...scenario , _disabled : false } ) ;
212- const replaceOne = jest . spyOn ( collection , 'replaceOne' ) . mockResolvedValue ( { } as any ) ;
213-
214222 const process = await service . instantiate ( scenario ) ;
215223
216224 expect ( process . id ) . toMatch (
@@ -251,24 +259,23 @@ describe('ProcessService', () => {
251259 describe ( 'has' , ( ) => {
252260 it ( 'should return true is the process exists' , async ( ) => {
253261 const processId = 'b2d39a1f-88bb-450e-95c5-feeffe95abe6' ;
254- const countDocuments = jest . spyOn ( collection , ' countDocuments' ) . mockResolvedValue ( 1 ) ;
262+ collection . countDocuments . mockResolvedValue ( 1 ) ;
255263
256264 const exists = await service . has ( processId ) ;
257265 expect ( exists ) . toBe ( true ) ;
258266
259- expect ( countDocuments ) . toHaveBeenCalled ( ) ;
260- expect ( countDocuments . mock . calls [ 0 ] [ 0 ] . _id . toString ( ) ) . toEqual ( processId ) ;
267+ expect ( collection . countDocuments ) . toHaveBeenCalled ( ) ;
268+ expect ( collection . countDocuments . mock . calls [ 0 ] [ 0 ] . _id . toString ( ) ) . toEqual ( processId ) ;
261269 } ) ;
262270
263271 it ( "should return false is the process doesn't exist" , async ( ) => {
264272 const processId = 'b2d39a1f-88bb-450e-95c5-feeffe95abe6' ;
265- const countDocuments = jest . spyOn ( collection , 'countDocuments' ) . mockResolvedValue ( 0 ) ;
266273
267274 const exists = await service . has ( processId ) ;
268275 expect ( exists ) . toBe ( false ) ;
269276
270- expect ( countDocuments ) . toHaveBeenCalled ( ) ;
271- expect ( countDocuments . mock . calls [ 0 ] [ 0 ] . _id . toString ( ) ) . toEqual ( processId ) ;
277+ expect ( collection . countDocuments ) . toHaveBeenCalled ( ) ;
278+ expect ( collection . countDocuments . mock . calls [ 0 ] [ 0 ] . _id . toString ( ) ) . toEqual ( processId ) ;
272279 } ) ;
273280 } ) ;
274281
@@ -334,7 +341,7 @@ describe('ProcessService', () => {
334341 const processDocument = { _id : bsonUUID ( processId ) , scenario : bsonUUID ( scenarioId ) , ...processData } ;
335342
336343 jest . spyOn ( scenarios , 'get' ) . mockResolvedValue ( { ...scenario , _disabled : false } ) ;
337- const findOne = jest . spyOn ( collection , ' findOne' ) . mockResolvedValue ( processDocument ) ;
344+ collection . findOne . mockResolvedValue ( processDocument ) ;
338345
339346 const process = await service . get ( processId ) ;
340347
@@ -365,17 +372,73 @@ describe('ProcessService', () => {
365372
366373 expect ( scenarios . get ) . toHaveBeenCalledWith ( scenarioId ) ;
367374 expect ( collection . findOne ) . toHaveBeenCalled ( ) ;
368- expect ( findOne . mock . calls [ 0 ] [ 0 ] . _id . toString ( ) ) . toEqual ( processId ) ;
375+ expect ( collection . findOne . mock . calls [ 0 ] [ 0 ] . _id . toString ( ) ) . toEqual ( processId ) ;
369376 } ) ;
370377
371378 it ( "should throw an error if the process doesn't exist" , async ( ) => {
372379 const processId = 'b2d39a1f-88bb-450e-95c5-feeffe95abe6' ;
373- const findOne = jest . spyOn ( collection , 'findOne' ) . mockResolvedValue ( null ) ;
374380
375381 await expect ( service . get ( processId ) ) . rejects . toThrow ( 'Process not found' ) ;
376382
377- expect ( findOne ) . toHaveBeenCalled ( ) ;
378- expect ( findOne . mock . calls [ 0 ] [ 0 ] . _id . toString ( ) ) . toEqual ( processId ) ;
383+ expect ( collection . findOne ) . toHaveBeenCalled ( ) ;
384+ expect ( collection . findOne . mock . calls [ 0 ] [ 0 ] . _id . toString ( ) ) . toEqual ( processId ) ;
385+ } ) ;
386+ } ) ;
387+
388+ describe ( 'save' , ( ) => {
389+ const scenario = normalize ( {
390+ title : 'simple workflow' ,
391+ states : {
392+ initial : {
393+ on : 'complete' ,
394+ goto : '(done)' ,
395+ } ,
396+ } ,
397+ } ) ;
398+ const scenarioId = uuid ( scenario ) ;
399+
400+ const process = instantiate ( scenario ) ;
401+ const { id : processId , scenario : _ , actors, ...rest } = process ;
402+
403+ const doc = {
404+ _id : bsonUUID ( processId ) ,
405+ scenario : bsonUUID ( scenarioId ) ,
406+ actors : Object . entries ( actors ) . map ( ( [ key , actor ] ) => ( { _key : key , ...actor } ) ) ,
407+ ...rest ,
408+ } ;
409+
410+ it ( 'should save a process' , async ( ) => {
411+ await service . save ( process ) ;
412+
413+ expect ( collection . replaceOne ) . toHaveBeenCalled ( ) ;
414+ expect ( collection . replaceOne . mock . calls [ 0 ] [ 0 ] . _id . toString ( ) ) . toEqual ( processId ) ;
415+ expect ( normalizeBsonUUID ( collection . replaceOne . mock . calls [ 0 ] [ 1 ] as any ) ) . toEqual ( normalizeBsonUUID ( doc ) ) ;
416+ } ) ;
417+ } ) ;
418+
419+ describe ( 'step' , ( ) => {
420+ const scenario = normalize ( {
421+ title : 'simple workflow' ,
422+ states : {
423+ initial : {
424+ on : 'complete' ,
425+ goto : '(done)' ,
426+ } ,
427+ done : { } ,
428+ } ,
429+ } ) ;
430+
431+ it ( 'should step through a process' , async ( ) => {
432+ const save = jest . spyOn ( service , 'save' ) ;
433+ const process = instantiate ( scenario ) ;
434+
435+ const updatedProcess = await service . step ( process , 'complete' , { key : 'actor' } ) ;
436+
437+ expect ( updatedProcess . id ) . toEqual ( process . id ) ;
438+ expect ( updatedProcess . current . key ) . toEqual ( '(done)' ) ;
439+
440+ expect ( save ) . toHaveBeenCalledWith ( updatedProcess ) ;
441+ expect ( eventEmitter . emit ) . toBeCalledWith ( 'process.stepped' , updatedProcess ) ;
379442 } ) ;
380443 } ) ;
381444} ) ;
0 commit comments