UNPKG

@jss-rule-engine/workflow

Version:

141 lines (113 loc) 4.39 kB
// @ts-nocheck import test from 'ava'; import WorkflowService from '../../src/workflowService'; import { getMockActionFactory, getWorkflowService, resetTest, getDatabaseService } from '../_testHelper'; import workflowMock from '../_workflowMock'; import { WorkflowExecutionOptions, WorkflowExecutionContext } from '../../src/workflowTypes'; // Helper: Rule engine that always returns true const alwaysTrueRuleEngine = { getRuleEngineContext: () => ({}), parseAndRunRule: async () => true, }; // Helper: Rule engine that always returns false const alwaysFalseRuleEngine = { getRuleEngineContext: () => ({}), parseAndRunRule: async () => false, }; test('executeTriggers: should execute actions if trigger condition is met', async t => { await resetTest(t); const workflowService = new WorkflowService({ databaseService: getDatabaseService(t), ruleEngine: alwaysTrueRuleEngine, actionFactory: getMockActionFactory(), }); await workflowService.init(); await workflowService.load(workflowMock); const options: WorkflowExecutionOptions = { visitorId: 'visitor-1', workflowId: 'testworkflow', eventName: 'testtrigger', eventParameters: '', defaultStateId: 'teststate', }; const result = await workflowService.executeTriggers(options); t.true(result.success); t.is(result.visitorId, 'visitor-1'); t.is(result.workflowId, 'testworkflow'); t.is(result.stateId, 'teststate'); t.true(Array.isArray(result.clientCommands)); }); test('executeTriggers: should not execute actions if trigger condition is not met', async t => { await resetTest(t); const workflowService = new WorkflowService({ databaseService: getDatabaseService(t), ruleEngine: alwaysFalseRuleEngine, actionFactory: getMockActionFactory(), }); await workflowService.init(); await workflowService.load(workflowMock); const options: WorkflowExecutionOptions = { visitorId: 'visitor-2', workflowId: 'testworkflow', eventName: 'testtrigger', eventParameters: '', defaultStateId: 'teststate', }; const result = await workflowService.executeTriggers(options); t.true(result.success); t.deepEqual(result.clientCommands, []); }); test('executeActions: should execute actions and change state if condition is met', async t => { await resetTest(t); const dbService = getDatabaseService(t); const workflowService = new WorkflowService({ databaseService: dbService, ruleEngine: alwaysTrueRuleEngine, actionFactory: getMockActionFactory(), }); await workflowService.init(); await workflowService.load(workflowMock); await dbService.addVisitor('visitor-3', 'teststate', 'testworkflow'); const workflow = workflowMock; const state = workflow.states['teststate']; const context: WorkflowExecutionContext = { workflowService, workflow, visitor: { id: 'visitor-3' }, ruleEngine: alwaysTrueRuleEngine, clientCommands: [], trigger: 'testtrigger', triggerParameters: '', }; await workflowService.executeActions('visitor-3', context, state); // Should have changed state to nextStateId const newState = await dbService.getVisitorState('visitor-3', 'testworkflow'); t.is(newState, 'next-state-id'); }); test('executeActions: should not execute actions if condition is not met', async t => { await resetTest(t); const dbService = getDatabaseService(t); const workflowService = new WorkflowService({ databaseService: dbService, ruleEngine: alwaysFalseRuleEngine, actionFactory: getMockActionFactory(), }); await workflowService.init(); await workflowService.load(workflowMock); await dbService.addVisitor('visitor-4', 'teststate', 'testworkflow'); const workflow = workflowMock; const state = workflow.states['teststate']; const context: WorkflowExecutionContext = { workflowService, workflow, visitor: { id: 'visitor-4' }, ruleEngine: alwaysFalseRuleEngine, clientCommands: [], trigger: 'testtrigger', triggerParameters: '', }; await workflowService.executeActions('visitor-4', context, state); // Should not have changed state const newState = await dbService.getVisitorState('visitor-4', 'testworkflow'); t.is(newState, 'teststate'); });