|
| 1 | +import { Definition, Step } from 'sequential-workflow-model'; |
| 2 | +import { createSignalActivity, signalSignalActivity } from './signal-activity'; |
| 3 | +import { createActivitySet } from '../../core'; |
| 4 | +import { createWorkflowMachineBuilder } from '../../workflow-machine-builder'; |
| 5 | +import { STATE_FINISHED_ID } from '../../types'; |
| 6 | + |
| 7 | +interface TestGlobalState { |
| 8 | + beforeCalled: boolean; |
| 9 | + afterCalled: boolean; |
| 10 | +} |
| 11 | + |
| 12 | +const activitySet = createActivitySet<TestGlobalState>([ |
| 13 | + createSignalActivity<Step, TestGlobalState>('waitForSignal', { |
| 14 | + init: () => ({}), |
| 15 | + beforeSignal: async (_, globalState) => { |
| 16 | + expect(globalState.beforeCalled).toBe(false); |
| 17 | + expect(globalState.afterCalled).toBe(false); |
| 18 | + globalState.beforeCalled = true; |
| 19 | + }, |
| 20 | + afterSignal: async (_, globalState, __, payload) => { |
| 21 | + expect(globalState.beforeCalled).toBe(true); |
| 22 | + expect(globalState.afterCalled).toBe(false); |
| 23 | + globalState.afterCalled = true; |
| 24 | + expect(payload['TEST_VALUE']).toBe(123456); |
| 25 | + expect(Object.keys(payload).length).toBe(1); |
| 26 | + } |
| 27 | + }) |
| 28 | +]); |
| 29 | + |
| 30 | +describe('SignalActivity', () => { |
| 31 | + it('stops, after signal continues', done => { |
| 32 | + const definition: Definition = { |
| 33 | + sequence: [ |
| 34 | + { |
| 35 | + id: '0x1', |
| 36 | + componentType: 'task', |
| 37 | + type: 'waitForSignal', |
| 38 | + name: 'W8', |
| 39 | + properties: {} |
| 40 | + } |
| 41 | + ], |
| 42 | + properties: {} |
| 43 | + }; |
| 44 | + |
| 45 | + const builder = createWorkflowMachineBuilder(activitySet); |
| 46 | + const machine = builder.build(definition); |
| 47 | + const interpreter = machine.create({ |
| 48 | + init: () => ({ |
| 49 | + afterCalled: false, |
| 50 | + beforeCalled: false |
| 51 | + }) |
| 52 | + }); |
| 53 | + |
| 54 | + interpreter.onChange(() => { |
| 55 | + const snapshot = interpreter.getSnapshot(); |
| 56 | + |
| 57 | + if (snapshot.tryGetStatePath()?.includes('WAIT_FOR_SIGNAL')) { |
| 58 | + expect(snapshot.globalState.beforeCalled).toBe(true); |
| 59 | + expect(snapshot.globalState.afterCalled).toBe(false); |
| 60 | + |
| 61 | + setTimeout(() => { |
| 62 | + signalSignalActivity(interpreter, { |
| 63 | + TEST_VALUE: 123456 |
| 64 | + }); |
| 65 | + }, 25); |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + interpreter.onDone(() => { |
| 70 | + const snapshot = interpreter.getSnapshot(); |
| 71 | + |
| 72 | + expect(snapshot.tryGetStatePath()).toStrictEqual([STATE_FINISHED_ID]); |
| 73 | + expect(snapshot.globalState.beforeCalled).toBe(true); |
| 74 | + expect(snapshot.globalState.afterCalled).toBe(true); |
| 75 | + |
| 76 | + done(); |
| 77 | + }); |
| 78 | + |
| 79 | + interpreter.start(); |
| 80 | + }); |
| 81 | +}); |
0 commit comments