|
| 1 | +/* eslint-disable @typescript-eslint/no-unsafe-call */ |
| 2 | +/* eslint-disable @typescript-eslint/no-unsafe-member-access */ |
| 3 | +/* eslint-disable @typescript-eslint/no-unsafe-argument */ |
| 4 | +/* eslint-disable @typescript-eslint/no-unsafe-assignment */ |
| 5 | +/* eslint-disable @typescript-eslint/restrict-template-expressions */ |
| 6 | + |
| 7 | +import { BaseFakeRepository } from '../../../../test-utils/src/base-fake-repository'; |
| 8 | + |
| 9 | +import { WorkflowControllerInternal } from './workflow.controller.internal'; |
| 10 | +import { WorkflowService } from './workflow.service'; |
| 11 | +import { WorkflowDefinitionModel } from './workflow-definition.model'; |
| 12 | +import { WorkflowEventEmitterService } from './workflow-event-emitter.service'; |
| 13 | + |
| 14 | +class FakeWorkflowRuntimeDataRepo extends BaseFakeRepository { |
| 15 | + constructor() { |
| 16 | + super(Object); |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +class FakeWorkflowDefinitionRepo extends BaseFakeRepository { |
| 21 | + constructor() { |
| 22 | + super(WorkflowDefinitionModel); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +function buildWorkflowDeifintion(sequenceNum) { |
| 27 | + return { |
| 28 | + id: sequenceNum.toString(), |
| 29 | + name: `name ${sequenceNum}`, |
| 30 | + version: sequenceNum, |
| 31 | + definition: { |
| 32 | + initial: 'initial', |
| 33 | + states: { |
| 34 | + initial: { |
| 35 | + on: { |
| 36 | + COMPLETE: 'completed', |
| 37 | + }, |
| 38 | + }, |
| 39 | + completed: { |
| 40 | + type: 'final', |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + definitionType: `definitionType ${sequenceNum}`, |
| 45 | + createdAt: new Date(), |
| 46 | + updatedAt: new Date(), |
| 47 | + }; |
| 48 | +} |
| 49 | + |
| 50 | +describe('WorkflowControllerInternal', () => { |
| 51 | + let controller; |
| 52 | + let workflowRuntimeDataRepo; |
| 53 | + let eventEmitterSpy; |
| 54 | + const numbUserInfo = Symbol(); |
| 55 | + |
| 56 | + beforeEach(() => { |
| 57 | + const workflowDefinitionRepo = new FakeWorkflowDefinitionRepo(); |
| 58 | + workflowRuntimeDataRepo = new FakeWorkflowRuntimeDataRepo(); |
| 59 | + |
| 60 | + eventEmitterSpy = { |
| 61 | + emitted: [], |
| 62 | + |
| 63 | + emit(status, data) { |
| 64 | + this.emitted.push({ status, data }); |
| 65 | + }, |
| 66 | + }; |
| 67 | + const service = new WorkflowService( |
| 68 | + workflowDefinitionRepo as any, |
| 69 | + workflowRuntimeDataRepo, |
| 70 | + {} as any, |
| 71 | + eventEmitterSpy, |
| 72 | + ); |
| 73 | + |
| 74 | + controller = new WorkflowControllerInternal(service, {} as any); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('.listWorkflowDefinitions', () => { |
| 78 | + it('returns workflows by query', async () => { |
| 79 | + await controller.createWorkflowDefinition(numbUserInfo, buildWorkflowDeifintion(2)); |
| 80 | + await controller.createWorkflowDefinition(numbUserInfo, buildWorkflowDeifintion(3)); |
| 81 | + await controller.createWorkflowDefinition(numbUserInfo, buildWorkflowDeifintion(4)); |
| 82 | + |
| 83 | + const definitions = await controller.listWorkflowDefinitions(numbUserInfo, { |
| 84 | + query: { |
| 85 | + where: { |
| 86 | + name: 'name 3', |
| 87 | + }, |
| 88 | + }, |
| 89 | + }); |
| 90 | + |
| 91 | + expect(definitions).toHaveLength(1); |
| 92 | + expect(definitions[0]).toMatchObject({ id: '3', name: 'name 3' }); |
| 93 | + expect(definitions[0]).not.toHaveProperty('updatedAt'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('filters out certain fields', async () => { |
| 97 | + await controller.createWorkflowDefinition(numbUserInfo, buildWorkflowDeifintion(3)); |
| 98 | + |
| 99 | + const definitions = await controller.listWorkflowDefinitions(numbUserInfo, { |
| 100 | + query: { |
| 101 | + where: { |
| 102 | + name: 'name 3', |
| 103 | + }, |
| 104 | + }, |
| 105 | + }); |
| 106 | + |
| 107 | + expect(definitions[0]).not.toHaveProperty('updatedAt'); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('.event', () => { |
| 112 | + describe('reaching to a state of type "final"', () => { |
| 113 | + it('updates runtime data status to "completed"', async () => { |
| 114 | + const initialRuntimeData = { |
| 115 | + id: '2', |
| 116 | + workflowDefinitionId: '2', |
| 117 | + context: { |
| 118 | + numb: 'context', |
| 119 | + }, |
| 120 | + }; |
| 121 | + await workflowRuntimeDataRepo.create({ |
| 122 | + data: initialRuntimeData, |
| 123 | + }); |
| 124 | + |
| 125 | + await controller.createWorkflowDefinition(numbUserInfo, buildWorkflowDeifintion(2)); |
| 126 | + await controller.event({ id: '2' }, { name: 'COMPLETE' }); |
| 127 | + |
| 128 | + const runtimeData = await workflowRuntimeDataRepo.findById('2'); |
| 129 | + |
| 130 | + expect(runtimeData.state).toEqual('completed'); |
| 131 | + expect(runtimeData.status).toEqual('completed'); |
| 132 | + }); |
| 133 | + |
| 134 | + it('emits an event', async () => { |
| 135 | + const initialRuntimeData = { |
| 136 | + id: '2', |
| 137 | + workflowDefinitionId: '2', |
| 138 | + context: { |
| 139 | + numb: 'context', |
| 140 | + }, |
| 141 | + }; |
| 142 | + await workflowRuntimeDataRepo.create({ |
| 143 | + data: initialRuntimeData, |
| 144 | + }); |
| 145 | + |
| 146 | + await controller.createWorkflowDefinition(numbUserInfo, buildWorkflowDeifintion(2)); |
| 147 | + await controller.event({ id: '2' }, { name: 'COMPLETE' }); |
| 148 | + |
| 149 | + expect(eventEmitterSpy.emitted).toEqual([ |
| 150 | + { |
| 151 | + status: 'workflow.completed', |
| 152 | + data: { |
| 153 | + runtimeData: initialRuntimeData, |
| 154 | + state: 'completed', |
| 155 | + context: { |
| 156 | + numb: 'context', |
| 157 | + }, |
| 158 | + }, |
| 159 | + }, |
| 160 | + ]); |
| 161 | + }); |
| 162 | + }); |
| 163 | + }); |
| 164 | +}); |
0 commit comments