├── .gitignore ├── .npmignore ├── .prettierrc ├── test ├── fixtures │ ├── definitions │ │ ├── startat-field-does-not-exist.json │ │ ├── fail.json │ │ ├── succeed.json │ │ ├── unknown-type.json │ │ ├── pass-without-result-and-resultpath.json │ │ ├── startat-does-not-exist.json │ │ ├── state-without-next-and-end.json │ │ ├── pass-inputpath-is-undefined.json │ │ ├── state-with-end-is-true.json │ │ ├── pass-input-to-resultpath.json │ │ ├── state-with-next-property.json │ │ ├── pass-result-to-resultpath.json │ │ ├── task-inputpath-is-null.json │ │ ├── task-inputpath-to-outputpath.json │ │ ├── task-complex-inputpath-and-outputpath.json │ │ ├── task-without-input.json │ │ ├── add.json │ │ ├── task-unknown-resource.json │ │ ├── task-addasync.json │ │ ├── task-input-to-resource.json │ │ ├── task-inputpath.json │ │ ├── task-complex-inputpath.json │ │ ├── task-resultpath-is-undefined.json │ │ ├── copy-object.json │ │ ├── choice-more-than-one-choice.json │ │ ├── two-states.json │ │ ├── loop.json │ │ ├── parameter-property.json │ │ ├── many-states.json │ │ ├── states-with-common-prefix.json │ │ └── execution-path-multiple.json │ └── resources.ts ├── RunStateResult.ts ├── FakeStateMachine-runPartial.ts ├── jsonpath.ts ├── readme.example.ts ├── FakeStateMachine-runCondition.ts ├── FakeStateMachine-run.ts ├── FakeStateMachine-runState.ts └── helper │ └── choiceHelper.ts ├── src ├── index.ts ├── RunStateResult.ts ├── FakeStateMachine.ts └── helper │ └── choiceHelper.ts ├── tsconfig.json ├── .github └── workflows │ ├── nodejs.yml │ └── publish.yml ├── package.json ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | dist/ 3 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | src/ 2 | test/ 3 | .eslintrc.js 4 | .travis.yml 5 | tsconfig.json 6 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "trailingComma": "es5", 3 | "singleQuote": true 4 | } 5 | -------------------------------------------------------------------------------- /test/fixtures/definitions/startat-field-does-not-exist.json: -------------------------------------------------------------------------------- 1 | { 2 | "States": { 3 | "Done": { 4 | "Type": "Succeed" 5 | } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /test/fixtures/definitions/fail.json: -------------------------------------------------------------------------------- 1 | { 2 | "StartAt": "Start", 3 | "States": { 4 | "Target": { 5 | "Type": "Fail" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /test/fixtures/definitions/succeed.json: -------------------------------------------------------------------------------- 1 | { 2 | "StartAt": "Start", 3 | "States": { 4 | "Target": { 5 | "Type": "Succeed" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /test/fixtures/definitions/unknown-type.json: -------------------------------------------------------------------------------- 1 | { 2 | "StartAt": "Done", 3 | "States": { 4 | "Done": { 5 | "Type": "UnknownType" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /test/fixtures/resources.ts: -------------------------------------------------------------------------------- 1 | export const addNumbers = (numbers: { val1: number; val2: number }) => 2 | numbers.val1 + numbers.val2; 3 | export const increment = (i: number) => i + 1; 4 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import { FakeStateMachine } from './FakeStateMachine'; 2 | import { RunStateResult } from './RunStateResult'; 3 | 4 | export = { 5 | FakeStateMachine, 6 | RunStateResult, 7 | }; 8 | -------------------------------------------------------------------------------- /test/fixtures/definitions/pass-without-result-and-resultpath.json: -------------------------------------------------------------------------------- 1 | { 2 | "StartAt": "Start", 3 | "States": { 4 | "Target": { 5 | "Type": "Pass", 6 | "Next": "NextState" 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /test/fixtures/definitions/startat-does-not-exist.json: -------------------------------------------------------------------------------- 1 | { 2 | "StartAt": "Start", 3 | "States": { 4 | "Target2": { 5 | "Input": "a", 6 | "ResultPath": "$.a2", 7 | "Type": "Pass" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /test/fixtures/definitions/state-without-next-and-end.json: -------------------------------------------------------------------------------- 1 | { 2 | "StartAt": "Start", 3 | "States": { 4 | "Target": { 5 | "Input": "a", 6 | "ResultPath": "$.a2", 7 | "Type": "Pass" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /test/fixtures/definitions/pass-inputpath-is-undefined.json: -------------------------------------------------------------------------------- 1 | { 2 | "StartAt": "Start", 3 | "States": { 4 | "Target": { 5 | "ResultPath": "$.a2", 6 | "Type": "Pass", 7 | "Next": "NextState" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /test/fixtures/definitions/state-with-end-is-true.json: -------------------------------------------------------------------------------- 1 | { 2 | "StartAt": "Start", 3 | "States": { 4 | "Target": { 5 | "Input": "a", 6 | "ResultPath": "$.a2", 7 | "Type": "Pass", 8 | "End": true 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /test/fixtures/definitions/pass-input-to-resultpath.json: -------------------------------------------------------------------------------- 1 | { 2 | "StartAt": "Start", 3 | "States": { 4 | "Target": { 5 | "Input": "a", 6 | "ResultPath": "$.a2", 7 | "Type": "Pass", 8 | "Next": "NextState" 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /test/fixtures/definitions/state-with-next-property.json: -------------------------------------------------------------------------------- 1 | { 2 | "StartAt": "Start", 3 | "States": { 4 | "Target": { 5 | "Input": "a", 6 | "ResultPath": "$.a2", 7 | "Type": "Pass", 8 | "Next": "NextState" 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /test/fixtures/definitions/pass-result-to-resultpath.json: -------------------------------------------------------------------------------- 1 | { 2 | "StartAt": "Start", 3 | "States": { 4 | "Target": { 5 | "Result": "a", 6 | "ResultPath": "$.a2", 7 | "Type": "Pass", 8 | "Next": "NextState" 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /test/fixtures/definitions/task-inputpath-is-null.json: -------------------------------------------------------------------------------- 1 | { 2 | "StartAt": "Start", 3 | "States": { 4 | "Target": { 5 | "InputPath": null, 6 | "ResultPath": "$.a2", 7 | "Type": "Pass", 8 | "Next": "NextState" 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /test/fixtures/definitions/task-inputpath-to-outputpath.json: -------------------------------------------------------------------------------- 1 | { 2 | "StartAt": "Start", 3 | "States": { 4 | "Target": { 5 | "InputPath": "$.a1", 6 | "ResultPath": "$.a2", 7 | "Type": "Pass", 8 | "Next": "NextState" 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /test/fixtures/definitions/task-complex-inputpath-and-outputpath.json: -------------------------------------------------------------------------------- 1 | { 2 | "StartAt": "Start", 3 | "States": { 4 | "Target": { 5 | "InputPath": "$.a.b2.c1", 6 | "ResultPath": "$.a.b3.c2", 7 | "Type": "Pass", 8 | "Next": "NextState" 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /test/fixtures/definitions/task-without-input.json: -------------------------------------------------------------------------------- 1 | { 2 | "StartAt": "Start", 3 | "States": { 4 | "Target": { 5 | "Resource": "arn:aws:lambda:us-east-1:123456789012:function:Add", 6 | "ResultPath": "$.sum", 7 | "Type": "Task", 8 | "Next": "NextState" 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /test/fixtures/definitions/add.json: -------------------------------------------------------------------------------- 1 | { 2 | "StartAt": "Add", 3 | "States": { 4 | "Add": { 5 | "Type": "Task", 6 | "Resource": "arn:aws:lambda:us-east-1:123456789012:function:Add", 7 | "InputPath": "$.numbers", 8 | "ResultPath": "$.sum", 9 | "End": true 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /test/fixtures/definitions/task-unknown-resource.json: -------------------------------------------------------------------------------- 1 | { 2 | "StartAt": "Start", 3 | "States": { 4 | "Target": { 5 | "Resource": "arn:aws:lambda:us-east-1:123456789012:function:Unknown", 6 | "ResultPath": "$.result", 7 | "Type": "Task", 8 | "Next": "NextState" 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /test/fixtures/definitions/task-addasync.json: -------------------------------------------------------------------------------- 1 | { 2 | "StartAt": "Add", 3 | "States": { 4 | "Add": { 5 | "Type": "Task", 6 | "InputPath": "$.numbers", 7 | "Resource": "arn:aws:lambda:us-east-1:123456789012:function:AddAsync", 8 | "ResultPath": "$.sum", 9 | "End": true 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /test/fixtures/definitions/task-input-to-resource.json: -------------------------------------------------------------------------------- 1 | { 2 | "StartAt": "Start", 3 | "States": { 4 | "Target": { 5 | "Resource": "arn:aws:lambda:us-east-1:123456789012:function:Double", 6 | "Input": 3, 7 | "ResultPath": "$.result", 8 | "Type": "Task", 9 | "Next": "NextState" 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /test/fixtures/definitions/task-inputpath.json: -------------------------------------------------------------------------------- 1 | { 2 | "StartAt": "Start", 3 | "States": { 4 | "Target": { 5 | "InputPath": "$.numbers", 6 | "Resource": "arn:aws:lambda:us-east-1:123456789012:function:Add", 7 | "ResultPath": "$.sum", 8 | "Type": "Task", 9 | "Next": "NextState" 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /test/fixtures/definitions/task-complex-inputpath.json: -------------------------------------------------------------------------------- 1 | { 2 | "StartAt": "Start", 3 | "States": { 4 | "Target": { 5 | "InputPath": "$.a.b3.c2", 6 | "Resource": "arn:aws:lambda:us-east-1:123456789012:function:Add", 7 | "ResultPath": "$.sum", 8 | "Type": "Task", 9 | "Next": "NextState" 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /test/fixtures/definitions/task-resultpath-is-undefined.json: -------------------------------------------------------------------------------- 1 | { 2 | "StartAt": "Start", 3 | "States": { 4 | "Target": { 5 | "Resource": "arn:aws:lambda:us-east-1:123456789012:function:Identity", 6 | "Parameters": { 7 | "a": 1, 8 | "b": 2 9 | }, 10 | "Type": "Task", 11 | "Next": "NextState" 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /test/fixtures/definitions/copy-object.json: -------------------------------------------------------------------------------- 1 | { 2 | "StartAt": "Start", 3 | "States": { 4 | "Start": { 5 | "Type": "Pass", 6 | "InputPath": "$.a1", 7 | "ResultPath": "$.a2", 8 | "Next": "Increment" 9 | }, 10 | "Increment": { 11 | "Type": "Pass", 12 | "Input": 2, 13 | "ResultPath": "$.a2.b", 14 | "Next": "Done" 15 | }, 16 | "Done": { 17 | "Type": "Succeed" 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "alwaysStrict": true, 5 | "declaration": true, 6 | "noImplicitAny": true, 7 | "removeComments": true, 8 | "preserveConstEnums": true, 9 | "outDir": "./dist/", 10 | "sourceMap": true, 11 | "target": "es6" 12 | }, 13 | "include": [ 14 | "src/**/*" 15 | ], 16 | "exclude": [ 17 | "node_modules", 18 | "test/**/*" 19 | ] 20 | } 21 | -------------------------------------------------------------------------------- /test/fixtures/definitions/choice-more-than-one-choice.json: -------------------------------------------------------------------------------- 1 | { 2 | "States": { 3 | "Choices": { 4 | "Type": "Choice", 5 | "Choices": [ 6 | { 7 | "Variable": "$.condition1", 8 | "BooleanEquals": true, 9 | "Next": "NextState1" 10 | }, 11 | { 12 | "Variable": "$.condition2", 13 | "BooleanEquals": true, 14 | "Next": "NextState2" 15 | } 16 | ], 17 | "Default": "DefaultState" 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /test/fixtures/definitions/two-states.json: -------------------------------------------------------------------------------- 1 | { 2 | "StartAt": "Add1", 3 | "States": { 4 | "Add1": { 5 | "Type": "Task", 6 | "Resource": "arn:aws:lambda:us-east-1:123456789012:function:Add", 7 | "InputPath": "$.numbers", 8 | "ResultPath": "$.sum1", 9 | "Next": "Add2" 10 | }, 11 | "Add2": { 12 | "Type": "Task", 13 | "Resource": "arn:aws:lambda:us-east-1:123456789012:function:Add", 14 | "InputPath": "$.numbers", 15 | "ResultPath": "$.sum2", 16 | "End": true 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /test/RunStateResult.ts: -------------------------------------------------------------------------------- 1 | import { RunStateResult } from '../src/RunStateResult'; 2 | 3 | describe('RunStateResult', () => { 4 | test('should be able to compare', () => { 5 | const r1 = new RunStateResult({}, 'Task', 'NextState', false); 6 | const r2 = new RunStateResult({}, 'Task', 'NextState', false); 7 | expect(r1).toEqual(r2); 8 | }); 9 | 10 | describe('when the nextState is null and isTerminalState = false', () => { 11 | test('should throw error', () => { 12 | expect(() => new RunStateResult({}, 'Task', null, false)).toThrowError( 13 | Error 14 | ); 15 | }); 16 | }); 17 | }); 18 | -------------------------------------------------------------------------------- /.github/workflows/nodejs.yml: -------------------------------------------------------------------------------- 1 | name: test 2 | 3 | on: 4 | - push 5 | - pull_request 6 | 7 | jobs: 8 | build: 9 | 10 | runs-on: ubuntu-22.04 11 | 12 | strategy: 13 | matrix: 14 | node-version: 15 | - 20.x 16 | 17 | steps: 18 | - uses: actions/checkout@v4.2.2 19 | - name: Setup Node.js 20 | uses: actions/setup-node@v4.1.0 21 | with: 22 | node-version: ${{ matrix.node-version }} 23 | - name: npm install, build, and test 24 | run: | 25 | npm install 26 | npm run build --if-present 27 | npm test 28 | env: 29 | CI: true 30 | -------------------------------------------------------------------------------- /test/fixtures/definitions/loop.json: -------------------------------------------------------------------------------- 1 | { 2 | "StartAt": "IncrementOrEnd", 3 | "States": { 4 | "IncrementOrEnd": { 5 | "Type": "Choice", 6 | "Choices": [ 7 | { 8 | "Variable": "$.i", 9 | "NumericEquals": 3, 10 | "Next": "Done" 11 | } 12 | ], 13 | "Default": "Increment" 14 | }, 15 | "Increment": { 16 | "Type": "Task", 17 | "Resource": "arn:aws:lambda:us-east-1:123456789012:function:Increment", 18 | "InputPath": "$.i", 19 | "ResultPath": "$.i", 20 | "Next": "IncrementOrEnd" 21 | }, 22 | "Done": { 23 | "Type": "Succeed" 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /test/fixtures/definitions/parameter-property.json: -------------------------------------------------------------------------------- 1 | { 2 | "StartAt": "Start", 3 | "States": { 4 | "Target": { 5 | "Resource": "arn:aws:lambda:us-east-1:123456789012:function:saveInput", 6 | "Parameters": { 7 | "input": { 8 | "val1": 3, 9 | "val2.$": "$.b.c.val2" 10 | }, 11 | "arrayInput": [ 12 | { 13 | "val1": 3, 14 | "val2.$": "$.b.c.val2" 15 | }, 16 | { 17 | "val1.$": "$.b.c.val2", 18 | "val2": 3 19 | } 20 | ] 21 | }, 22 | "ResultPath": "$.result", 23 | "Type": "Task", 24 | "Next": "NextState" 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /test/fixtures/definitions/many-states.json: -------------------------------------------------------------------------------- 1 | { 2 | "StartAt": "Pass0", 3 | "States": { 4 | "Pass0": { 5 | "Input": "a", 6 | "ResultPath": "$.p0", 7 | "Type": "Pass", 8 | "Next": "Pass1" 9 | }, 10 | "Pass1": { 11 | "Input": "b", 12 | "ResultPath": "$.p1", 13 | "Type": "Pass", 14 | "Next": "Pass2" 15 | }, 16 | "Pass2": { 17 | "Input": "c", 18 | "ResultPath": "$.p2", 19 | "Type": "Pass", 20 | "Next": "Pass3" 21 | }, 22 | "Pass3": { 23 | "Input": "d", 24 | "ResultPath": "$.p3", 25 | "Type": "Pass", 26 | "Next": "Done" 27 | }, 28 | "Done": { 29 | "Type": "Succeed" 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | name: publish 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | 8 | jobs: 9 | npm-publish: 10 | name: npm-publish 11 | runs-on: ubuntu-22.04 12 | steps: 13 | - name: Checkout repository 14 | uses: actions/checkout@v4.2.2 15 | - name: Setup Node.js 16 | uses: actions/setup-node@v4.1.0 17 | with: 18 | node-version: 20.x 19 | - name: Build 20 | run: | 21 | npm install 22 | npm run build 23 | - name: Publish if version has been updated 24 | uses: pascalgn/npm-publish-action@1.3.9 25 | with: 26 | commit_pattern: "^Release (\\S+)" 27 | env: 28 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 29 | NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }} 30 | -------------------------------------------------------------------------------- /src/RunStateResult.ts: -------------------------------------------------------------------------------- 1 | export class RunStateResult { 2 | data: object; 3 | stateType: string; 4 | nextStateName: string; 5 | isTerminalState: boolean; 6 | 7 | constructor( 8 | data: object, 9 | stateType: string, 10 | nextStateName: string, 11 | isTerminalState: boolean 12 | ) { 13 | RunStateResult.validateArguments(nextStateName, isTerminalState); 14 | 15 | this.data = data; 16 | this.stateType = stateType; 17 | this.nextStateName = nextStateName; 18 | this.isTerminalState = isTerminalState; 19 | } 20 | 21 | static validateArguments(nextStateName: string, isTerminalState: boolean) { 22 | if (!nextStateName && !isTerminalState) { 23 | throw new Error( 24 | 'nextState must be non-null when the state is non-terminal state' 25 | ); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /test/fixtures/definitions/states-with-common-prefix.json: -------------------------------------------------------------------------------- 1 | { 2 | "States": { 3 | "main.initialize": { 4 | "Type": "Pass", 5 | "Result": 0, 6 | "ResultPath": "$.i", 7 | "Next": "main.increment" 8 | }, 9 | "main.increment": { 10 | "Type": "Task", 11 | "InputPath": "$.i", 12 | "Resource": "arn:aws:lambda:us-east-1:123456789012:function:Increment", 13 | "ResultPath": "$.i", 14 | "Next": "main.check" 15 | }, 16 | "main.check": { 17 | "Type": "Choice", 18 | "Choices": [ 19 | { 20 | "Variable": "$.i", 21 | "NumericEquals": 3, 22 | "Next": "hoge" 23 | } 24 | ], 25 | "Default": "main.increment" 26 | }, 27 | "hoge": { 28 | "Type": "Pass", 29 | "Input": -1, 30 | "ResultPath": "$.i" 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /test/FakeStateMachine-runPartial.ts: -------------------------------------------------------------------------------- 1 | import { FakeStateMachine } from '../src/FakeStateMachine'; 2 | import { RunStateResult } from '../src/RunStateResult'; 3 | 4 | describe('FakeStateMachine#runPartial()', () => { 5 | const definition = require('./fixtures/definitions/many-states.json'); 6 | const fakeStateMachine = new FakeStateMachine(definition, {}); 7 | test('should execute states between the start state and the end state', async () => { 8 | expect( 9 | await fakeStateMachine.runPartial( 10 | { title: 'run-partial' }, 11 | 'Pass1', 12 | 'Pass2' 13 | ) 14 | ).toEqual( 15 | new RunStateResult( 16 | { 17 | title: 'run-partial', 18 | p1: 'b', 19 | p2: 'c', 20 | }, 21 | 'Pass', 22 | 'Pass3', 23 | false 24 | ) 25 | ); 26 | }); 27 | }); 28 | -------------------------------------------------------------------------------- /test/fixtures/definitions/execution-path-multiple.json: -------------------------------------------------------------------------------- 1 | { 2 | "Comment": "A very complicated workflow", 3 | "StartAt": "Step1", 4 | "States": { 5 | "Step1": { 6 | "Type": "Pass", 7 | "Next": "Step2", 8 | "Result": { 9 | "Step1": "hello" 10 | } 11 | }, 12 | "Step2": { 13 | "Type": "Pass", 14 | "Next": "Choice3" 15 | }, 16 | "Choice3": { 17 | "Type": "Choice", 18 | "Choices": [ 19 | { 20 | "Variable": "$.Step1", 21 | "StringEquals": "world", 22 | "Next": "NonExecutingRule" 23 | }, 24 | { 25 | "Variable": "$.Step1", 26 | "StringEquals": "hello", 27 | "Next": "Rule2" 28 | } 29 | ], 30 | "Default": "NonExecutingRule" 31 | }, 32 | "NonExecutingRule": { 33 | "Type": "Pass", 34 | "End": true 35 | }, 36 | "Rule2": { 37 | "Type": "Pass", 38 | "Next": "Success" 39 | }, 40 | "Success": { 41 | "Type": "Succeed" 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /test/jsonpath.ts: -------------------------------------------------------------------------------- 1 | import * as jsonpath from 'jsonpath'; 2 | 3 | describe('jsonpath', () => { 4 | describe('#value()', () => { 5 | test('should return the path', () => { 6 | const data = { 7 | foo: 123, 8 | bar: ['a', 'b', 'c'], 9 | car: { 10 | cdr: true, 11 | }, 12 | }; 13 | expect(jsonpath.value(data, '$.foo')).toEqual(123); 14 | expect(jsonpath.value(data, '$.bar')).toEqual(['a', 'b', 'c']); 15 | expect(jsonpath.value(data, '$.car.cdr')).toEqual(true); 16 | }); 17 | test('should assign the value to the specified path)', () => { 18 | const data = { 19 | car: { 20 | cdr: true, 21 | }, 22 | }; 23 | const result = jsonpath.value(data, '$.car.foo', 123); 24 | expect(result).toBe(123); 25 | expect(data).toEqual({ 26 | car: { 27 | cdr: true, 28 | foo: 123, 29 | }, 30 | }); 31 | }); 32 | test.skip('should query using range $.a[0,1]', () => { 33 | const data = { 34 | a: [1, 2, 3, 4], 35 | }; 36 | const result = jsonpath.value(data, '$.a[0,1]'); 37 | expect(result).toBe([1, 2]); 38 | }); 39 | }); 40 | }); 41 | -------------------------------------------------------------------------------- /test/readme.example.ts: -------------------------------------------------------------------------------- 1 | import { FakeStateMachine } from '../src/FakeStateMachine'; 2 | 3 | describe('FakeStateMachine.run', () => { 4 | const definition = { 5 | Comment: 'https://states-language.net/spec.html#data', 6 | StartAt: 'AddNumbers', 7 | States: { 8 | AddNumbers: { 9 | Type: 'Task', 10 | Resource: 'arn:aws:lambda:us-east-1:123456789012:function:Add', 11 | InputPath: '$.numbers', 12 | ResultPath: '$.sum', 13 | End: true, 14 | }, 15 | }, 16 | }; 17 | const fakeResources = { 18 | 'arn:aws:lambda:us-east-1:123456789012:function:Add': (numbers: { 19 | val1: number; 20 | val2: number; 21 | }) => numbers.val1 + numbers.val2, 22 | }; 23 | const fakeStateMachine = new FakeStateMachine(definition, fakeResources); 24 | 25 | test('should execute the state machine with fakeResource', async () => { 26 | const runStateResult = await fakeStateMachine.run({ 27 | title: 'Numbers to add', 28 | numbers: { val1: 3, val2: 4 }, 29 | }); 30 | 31 | expect(runStateResult.data).toEqual({ 32 | title: 'Numbers to add', 33 | numbers: { val1: 3, val2: 4 }, 34 | sum: 7, 35 | }); 36 | }); 37 | }); 38 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fake-step-functions", 3 | "version": "0.7.1", 4 | "author": "Takamasa Oshikiri ", 5 | "repository": { 6 | "type": "git", 7 | "url": "https://github.com/oshikiri/fake-step-functions" 8 | }, 9 | "keywords": [ 10 | "aws", 11 | "stepfunctions", 12 | "step functions", 13 | "amazon states language", 14 | "test", 15 | "testing" 16 | ], 17 | "license": "Apache-2.0", 18 | "scripts": { 19 | "test": "jest", 20 | "prebuild": "prettier --check {src,test}/**/*.ts test/**/*.json", 21 | "build": "tsc", 22 | "format": "prettier --write {src,test}/**/*.ts test/**/*.json" 23 | }, 24 | "main": "./dist/index.js", 25 | "dependencies": { 26 | "jsonpath": "^1.1.1" 27 | }, 28 | "devDependencies": { 29 | "@types/jest": "^27.0.2", 30 | "@types/jsonpath": "^0.2.4", 31 | "@types/node": "^22.8", 32 | "jest": "^27.2.5", 33 | "prettier": "^3.3", 34 | "ts-jest": "^27.0.7", 35 | "ts-node": "^10.9.2", 36 | "typescript": "^4.9.5" 37 | }, 38 | "jest": { 39 | "testMatch": [ 40 | "/test/*.ts", 41 | "/test/helper/*.ts" 42 | ], 43 | "transform": { 44 | "^.+\\.tsx?$": "ts-jest" 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /test/FakeStateMachine-runCondition.ts: -------------------------------------------------------------------------------- 1 | import { FakeStateMachine } from '../src/FakeStateMachine'; 2 | import { RunStateResult } from '../src/RunStateResult'; 3 | import { increment } from './fixtures/resources'; 4 | 5 | describe('FakeStateMachine#runCondition()', () => { 6 | const definition = require('./fixtures/definitions/states-with-common-prefix.json'); 7 | const fakeStateMachine = new FakeStateMachine(definition, { 8 | 'arn:aws:lambda:us-east-1:123456789012:function:Increment': increment, 9 | }); 10 | describe('with start and end', () => { 11 | test('should run while the condition fulfills', async () => { 12 | expect( 13 | await fakeStateMachine.runCondition( 14 | {}, 15 | { start: 'main.initialize', end: 'main.check' } 16 | ) 17 | ).toEqual( 18 | new RunStateResult({ i: 1 }, 'Choice', 'main.increment', false) 19 | ); 20 | }); 21 | }); 22 | describe('with start and regex', () => { 23 | test('should run while the condition fulfills', async () => { 24 | expect( 25 | await fakeStateMachine.runCondition( 26 | {}, 27 | { start: 'main.initialize', regex: /main\.\w+/ } 28 | ) 29 | ).toEqual(new RunStateResult({ i: 3 }, 'Choice', 'hoge', false)); 30 | }); 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | fake-step-functions 2 | ===== 3 | 4 | A lightweight unit testing toolkit for Amazon States Language. 5 | 6 | [![GitHub Workflows Status](https://github.com/oshikiri/fake-step-functions/workflows/test/badge.svg)](https://github.com/oshikiri/fake-step-functions/actions) 7 | [![npm version](https://badge.fury.io/js/fake-step-functions.svg)](https://badge.fury.io/js/fake-step-functions) 8 | 9 | ```js 10 | const { FakeStateMachine } = require('fake-step-functions'); 11 | 12 | describe('FakeStateMachine.run', () => { 13 | const definition = { 14 | Comment: 'https://states-language.net/spec.html#data', 15 | StartAt: 'AddNumbers', 16 | States: { 17 | AddNumbers: { 18 | Type: 'Task', 19 | Resource: 'arn:aws:lambda:us-east-1:123456789012:function:Add', 20 | InputPath: '$.numbers', 21 | ResultPath: '$.sum', 22 | End: true, 23 | }, 24 | }, 25 | }; 26 | const fakeResources = { 27 | 'arn:aws:lambda:us-east-1:123456789012:function:Add': numbers => numbers.val1 + numbers.val2, 28 | }; 29 | const fakeStateMachine = new FakeStateMachine(definition, fakeResources); 30 | 31 | test('should execute the state machine with fakeResource', async () => { 32 | const runStateResult = await fakeStateMachine.run({ 33 | title: 'Numbers to add', 34 | numbers: { val1: 3, val2: 4 }, 35 | }); 36 | 37 | expect(runStateResult.data).toEqual({ 38 | title: 'Numbers to add', 39 | numbers: { val1: 3, val2: 4 }, 40 | sum: 7, 41 | }); 42 | }); 43 | }); 44 | ``` 45 | 46 | 47 | ## Release 48 | 49 | In order to release new version to npm, create a commit using `npm version patch -m "Release %s"` and push it. 50 | See the npm-publish-action step in `.github/workflows/publish.yml`. 51 | 52 | 53 | ## References 54 | 55 | ### Specifications 56 | 57 | - Amazon States Language specification 58 | - Amazon States Language - AWS Step Functions 59 | 60 | 61 | ### Similar projects 62 | 63 | At 2022, **AWS Step Functions Local** is a primary choice to test ASL locally. 64 | See ["Testing Step Functions State Machines Locally"](https://docs.aws.amazon.com/step-functions/latest/dg/sfn-local.html). 65 | 66 | - [airware/stepfunctions\-local](https://github.com/airware/stepfunctions-local) 67 | - [wmfs/statebox](https://github.com/wmfs/statebox) 68 | - [mikeparisstuff/stateslang\-js](https://github.com/mikeparisstuff/stateslang-js) 69 | - [coinbase/step](https://github.com/coinbase/step) (Golang) 70 | - validators 71 | - [airware/asl\-validator](https://github.com/airware/asl-validator) 72 | - [awslabs/statelint](https://github.com/awslabs/statelint) (Ruby) 73 | -------------------------------------------------------------------------------- /test/FakeStateMachine-run.ts: -------------------------------------------------------------------------------- 1 | import { FakeStateMachine } from '../src/FakeStateMachine'; 2 | import { RunStateResult } from '../src/RunStateResult'; 3 | import { addNumbers, increment } from './fixtures/resources'; 4 | 5 | describe('FakeStateMachine#run()', () => { 6 | describe('when StartAt field does not exist', () => { 7 | test('should throw an Error', () => { 8 | const definition = require('./fixtures/definitions/startat-field-does-not-exist.json'); 9 | const fakeStateMachine = new FakeStateMachine(definition, {}); 10 | return expect(fakeStateMachine.run({})).rejects.toThrow( 11 | 'StartAt does not exist' 12 | ); 13 | }); 14 | }); 15 | 16 | test('should pass the input to fakeResource and fill the result to ResultPath', async () => { 17 | const definition = require('./fixtures/definitions/add.json'); 18 | const fakeResources = { 19 | 'arn:aws:lambda:us-east-1:123456789012:function:Add': addNumbers, 20 | }; 21 | const fakeStateMachine = new FakeStateMachine(definition, fakeResources); 22 | 23 | expect( 24 | await fakeStateMachine.run({ 25 | title: 'Numbers to add', 26 | numbers: { val1: 3, val2: 4 }, 27 | }) 28 | ).toEqual( 29 | new RunStateResult( 30 | { 31 | title: 'Numbers to add', 32 | numbers: { val1: 3, val2: 4 }, 33 | sum: 7, 34 | }, 35 | 'Task', 36 | null, 37 | true 38 | ) 39 | ); 40 | }); 41 | 42 | describe('when there is invalid Type String', () => { 43 | test('should throw an Error', () => { 44 | const definition = require('./fixtures/definitions/unknown-type.json'); 45 | const fakeStateMachine = new FakeStateMachine(definition, {}); 46 | 47 | return expect(fakeStateMachine.run({})).rejects.toThrow( 48 | 'Invalid Type: UnknownType' 49 | ); 50 | }); 51 | }); 52 | 53 | describe('when the state machine has two states', () => { 54 | test('should return the result successfully', async () => { 55 | const definition = require('./fixtures/definitions/two-states.json'); 56 | const fakeResources = { 57 | 'arn:aws:lambda:us-east-1:123456789012:function:Add': addNumbers, 58 | }; 59 | const fakeStateMachine = new FakeStateMachine(definition, fakeResources); 60 | 61 | expect( 62 | await fakeStateMachine.run({ 63 | title: 'Numbers to add', 64 | numbers: { val1: 3, val2: 4 }, 65 | }) 66 | ).toEqual( 67 | new RunStateResult( 68 | { 69 | title: 'Numbers to add', 70 | numbers: { val1: 3, val2: 4 }, 71 | sum1: 7, 72 | sum2: 7, 73 | }, 74 | 'Task', 75 | null, 76 | true 77 | ) 78 | ); 79 | }); 80 | }); 81 | 82 | describe('when state machine contains a loop with break', () => { 83 | test('should return the result successfully', async () => { 84 | const definition = require('./fixtures/definitions/loop.json'); 85 | const fakeResources = { 86 | 'arn:aws:lambda:us-east-1:123456789012:function:Increment': increment, 87 | }; 88 | const fakeStateMachine = new FakeStateMachine(definition, fakeResources); 89 | expect( 90 | await fakeStateMachine.run({ 91 | i: 0, 92 | }) 93 | ).toEqual( 94 | new RunStateResult( 95 | { 96 | i: 3, 97 | }, 98 | 'Succeed', 99 | null, 100 | true 101 | ) 102 | ); 103 | }); 104 | }); 105 | 106 | describe('when the state updates a copied field', () => { 107 | test('should not affect the original field', async () => { 108 | const definition = require('./fixtures/definitions/copy-object.json'); 109 | const fakeStateMachine = new FakeStateMachine(definition, {}); 110 | expect( 111 | await fakeStateMachine.run({ 112 | a1: { b: 1 }, 113 | }) 114 | ).toEqual( 115 | new RunStateResult( 116 | { 117 | a1: { b: 1 }, 118 | a2: { b: 2 }, 119 | }, 120 | 'Succeed', 121 | null, 122 | true 123 | ) 124 | ); 125 | }); 126 | }); 127 | 128 | describe('should have execution path', () => { 129 | const definition = require('./fixtures/definitions/execution-path-multiple.json'); 130 | 131 | test('execution path should contain executed states in the order of execution', async () => { 132 | const fakeStateMachine = new FakeStateMachine(definition, {}); 133 | 134 | await fakeStateMachine.run({}); 135 | 136 | expect(fakeStateMachine.executionPath).toEqual([ 137 | 'Step1', 138 | 'Step2', 139 | 'Choice3', 140 | 'Rule2', 141 | 'Success', 142 | ]); 143 | }); 144 | }); 145 | }); 146 | -------------------------------------------------------------------------------- /src/FakeStateMachine.ts: -------------------------------------------------------------------------------- 1 | import * as jsonpath from 'jsonpath'; 2 | import { RunStateResult } from './RunStateResult'; 3 | import * as choiceHelper from './helper/choiceHelper'; 4 | 5 | export interface Definition { 6 | StartAt?: string; 7 | States: { [key: string]: State }; 8 | } 9 | 10 | export interface State { 11 | Type: string; 12 | Parameters?: Object; 13 | Input?: Object; 14 | InputPath?: string | null; 15 | Result?: Object; 16 | ResultPath?: string | null; 17 | Resource?: string; 18 | Choices?: choiceHelper.TopLevelChoiceRule[]; 19 | Default?: string; 20 | Next?: string; 21 | End?: boolean; 22 | } 23 | 24 | export type Resource = { [key: string]: (arg: any) => any }; 25 | 26 | const clone = (obj: object) => JSON.parse(JSON.stringify(obj)); // TODO 27 | const isObject = (x: object) => typeof x === 'object' && x !== null; 28 | 29 | export class FakeStateMachine { 30 | definition: Definition; 31 | fakeResources: Resource; 32 | executionPath: Array; 33 | 34 | constructor(definition: Definition, fakeResources: Resource) { 35 | this.definition = definition; 36 | this.fakeResources = fakeResources; 37 | this.executionPath = new Array(); 38 | } 39 | 40 | async run(input: object): Promise { 41 | const startAt = this.definition.StartAt; 42 | if (startAt === undefined) { 43 | throw new Error('StartAt does not exist'); 44 | } 45 | return this.runPartial(input, startAt, null); 46 | } 47 | 48 | async runPartial( 49 | data: object, 50 | current: string, 51 | end: string 52 | ): Promise { 53 | const result = await this.runState(data, current); 54 | if (result.isTerminalState || current === end) return result; 55 | return this.runPartial(result.data, result.nextStateName, end); 56 | } 57 | 58 | // experimental 59 | async runCondition(data: object, _condition: any): Promise { 60 | const condition = _condition; 61 | const result = await this.runState(data, condition.start); 62 | if ( 63 | result.isTerminalState || 64 | condition.start === condition.end || 65 | !result.nextStateName.match(condition.regex) 66 | ) 67 | return result; 68 | condition.start = result.nextStateName; 69 | return this.runCondition(result.data, condition); 70 | } 71 | 72 | async runState(_data: object, stateName: string): Promise { 73 | const data = clone(_data); 74 | const state = this.definition.States[stateName.toString()]; 75 | if (state === undefined) { 76 | throw new Error(`the state ${stateName} does not exists`); 77 | } 78 | const stateType = state.Type; 79 | let nextState = state.Next || null; 80 | 81 | this.executionPath.push(stateName); 82 | 83 | switch (stateType) { 84 | case 'Task': { 85 | if (!(state.Resource in this.fakeResources)) { 86 | throw new Error(`Unknown resource: ${state.Resource}`); 87 | } 88 | const resource = this.fakeResources[state.Resource]; 89 | const newValue = await FakeStateMachine.runStateTask( 90 | state, 91 | data, 92 | resource 93 | ); 94 | if (state.ResultPath === undefined) { 95 | Object.assign(data, newValue); 96 | } else { 97 | jsonpath.value(data, state.ResultPath, newValue); 98 | } 99 | break; 100 | } 101 | case 'Pass': { 102 | const newValue = FakeStateMachine.runStatePass(state, data); 103 | 104 | if (state.ResultPath === undefined) { 105 | Object.assign(data, newValue); 106 | } else { 107 | jsonpath.value(data, state.ResultPath, newValue); 108 | } 109 | break; 110 | } 111 | case 'Succeed': 112 | case 'Fail': 113 | break; 114 | case 'Choice': { 115 | nextState = FakeStateMachine.runStateChoice(state, data); 116 | break; 117 | } 118 | case 'Wait': 119 | case 'Parallel': 120 | break; 121 | default: 122 | throw new Error(`Invalid Type: ${stateType}`); 123 | } 124 | const isTermialState = 125 | state.End === true || stateType === 'Succeed' || stateType === 'Fail'; 126 | 127 | return new RunStateResult(data, stateType, nextState, isTermialState); 128 | } 129 | 130 | static async runStateTask( 131 | state: State, 132 | data: object, 133 | resource: (arg: any) => any 134 | ): Promise { 135 | const dataInputPath: any = FakeStateMachine.inputData(state, data); 136 | const result = await resource(dataInputPath); 137 | if (result === undefined) return undefined; 138 | return clone(result); 139 | } 140 | 141 | static runStatePass(state: State, data: object): object { 142 | const dataInputPath = FakeStateMachine.inputData(state, data); 143 | return clone(state.Input || dataInputPath); // TODO: priority? 144 | } 145 | 146 | static runStateChoice(state: State, data: any): string { 147 | const matched = state.Choices.find((choice) => { 148 | return choiceHelper.isRightChoice(choice, data); 149 | }); 150 | 151 | if (matched !== undefined) return matched.Next; 152 | return state.Default; 153 | } 154 | 155 | static inputData(state: State, data: object): object { 156 | if (state.Type === 'Pass') { 157 | if (state.Result !== undefined) return state.Result; // TODO: priority? 158 | } else if (state.Type === 'Task') { 159 | if (state.Input !== undefined) return state.Input; 160 | } 161 | 162 | if (state.Parameters !== undefined) { 163 | const rawParameters = state.Parameters; 164 | return FakeStateMachine.resolveParameters(rawParameters, data); 165 | } 166 | 167 | switch (state.InputPath) { 168 | case undefined: { 169 | return clone(data); 170 | } 171 | case null: { 172 | return {}; 173 | } 174 | default: { 175 | return jsonpath.value(data, state.InputPath); 176 | } 177 | } 178 | } 179 | 180 | static resolveParameters(rawParameters: any, data: object): object { 181 | const resolvedParameters: any = Array.isArray(rawParameters) ? [] : {}; 182 | 183 | for (let key of Object.keys(rawParameters)) { 184 | const rawValue = rawParameters[key]; 185 | if (key.endsWith('.$')) { 186 | key = key.slice(0, -2); 187 | resolvedParameters[key] = jsonpath.value(data, rawValue); 188 | } else if (isObject(rawValue) || Array.isArray(rawValue)) { 189 | resolvedParameters[key] = this.resolveParameters(rawValue, data); 190 | } else { 191 | resolvedParameters[key] = rawValue; 192 | } 193 | } 194 | return resolvedParameters; 195 | } 196 | } 197 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2018 oshikiri 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /src/helper/choiceHelper.ts: -------------------------------------------------------------------------------- 1 | import * as jsonpath from 'jsonpath'; 2 | 3 | export interface TopLevelChoiceRule extends ChoiceRule { 4 | Next: string; 5 | } 6 | 7 | interface ChoiceRule extends Partial { 8 | And?: ChoiceRule[]; 9 | Or?: ChoiceRule[]; 10 | Not?: ChoiceRule; 11 | } 12 | 13 | interface BasicChoiceRule { 14 | Variable: string; 15 | BooleanEquals?: boolean; 16 | BooleanEqualsPath?: string; 17 | NumericEquals?: number; 18 | NumericLessThan?: number; 19 | NumericGreaterThan?: number; 20 | NumericGreaterThanEquals?: number; 21 | NumericLessThanEquals?: number; 22 | NumericEqualsPath?: string; 23 | NumericLessThanPath?: string; 24 | NumericGreaterThanPath?: string; 25 | NumericLessThanEqualsPath?: string; 26 | NumericGreaterThanEqualsPath?: string; 27 | StringMatches?: string; 28 | StringEquals?: string; 29 | StringLessThan?: string; 30 | StringGreaterThan?: string; 31 | StringLessThanEquals?: string; 32 | StringGreaterThanEquals?: string; 33 | StringEqualsPath?: string; 34 | StringLessThanPath?: string; 35 | StringGreaterThanPath?: string; 36 | StringLessThanEqualsPath?: string; 37 | StringGreaterThanEqualsPath?: string; 38 | TimestampEquals?: string; 39 | TimestampLessThan?: string; 40 | TimestampGreaterThan?: string; 41 | TimestampLessThanEquals?: string; 42 | TimestampGreaterThanEquals?: string; 43 | TimestampEqualsPath?: string; 44 | TimestampLessThanPath?: string; 45 | TimestampGreaterThanPath?: string; 46 | TimestampLessThanEqualsPath?: string; 47 | TimestampGreaterThanEqualsPath?: string; 48 | IsPresent?: boolean; 49 | IsNull?: boolean; 50 | IsBoolean?: boolean; 51 | IsNumeric?: boolean; 52 | IsString?: boolean; 53 | IsTimestamp?: boolean; 54 | } 55 | 56 | export const isRightChoice = (choice: ChoiceRule, data: any): boolean => { 57 | if (choice.Not) { 58 | return !isRightChoice(choice.Not, data); 59 | } 60 | 61 | if (choice.Or) { 62 | return choice.Or.some((subChoice: any) => isRightChoice(subChoice, data)); 63 | } 64 | 65 | if (choice.And) { 66 | return choice.And.every((subChoice: any) => isRightChoice(subChoice, data)); 67 | } 68 | 69 | return compareChoice(choice as BasicChoiceRule, data); 70 | }; 71 | 72 | const compareChoice = (choice: BasicChoiceRule, data: any): boolean => { 73 | const input = jsonpath.value(data, choice.Variable); 74 | 75 | if (choice.BooleanEquals !== undefined) { 76 | return ( 77 | typeof input === 'boolean' && 78 | typeof choice.BooleanEquals === 'boolean' && 79 | input === choice.BooleanEquals 80 | ); 81 | } 82 | 83 | if (choice.BooleanEqualsPath !== undefined) { 84 | const compareTo = jsonpath.value(data, choice.BooleanEqualsPath); 85 | 86 | return ( 87 | typeof input === 'boolean' && 88 | typeof compareTo === 'boolean' && 89 | input === compareTo 90 | ); 91 | } 92 | 93 | if (choice.NumericEquals !== undefined) { 94 | return ( 95 | typeof input === 'number' && 96 | typeof choice.NumericEquals === 'number' && 97 | input === choice.NumericEquals 98 | ); 99 | } 100 | 101 | if (choice.NumericLessThan !== undefined) { 102 | return ( 103 | typeof input === 'number' && 104 | typeof choice.NumericLessThan === 'number' && 105 | input < choice.NumericLessThan 106 | ); 107 | } 108 | 109 | if (choice.NumericGreaterThan !== undefined) { 110 | return ( 111 | typeof input === 'number' && 112 | typeof choice.NumericGreaterThan === 'number' && 113 | input > choice.NumericGreaterThan 114 | ); 115 | } 116 | 117 | if (choice.NumericLessThanEquals !== undefined) { 118 | return ( 119 | typeof input === 'number' && 120 | typeof choice.NumericLessThanEquals === 'number' && 121 | input <= choice.NumericLessThanEquals 122 | ); 123 | } 124 | 125 | if (choice.NumericGreaterThanEquals !== undefined) { 126 | return ( 127 | typeof input === 'number' && 128 | typeof choice.NumericGreaterThanEquals === 'number' && 129 | input >= choice.NumericGreaterThanEquals 130 | ); 131 | } 132 | 133 | if (choice.NumericEqualsPath !== undefined) { 134 | const compareTo = jsonpath.value(data, choice.NumericEqualsPath); 135 | return ( 136 | typeof input === 'number' && 137 | typeof compareTo === 'number' && 138 | input === compareTo 139 | ); 140 | } 141 | 142 | if (choice.NumericLessThanPath !== undefined) { 143 | const compareTo = jsonpath.value(data, choice.NumericLessThanPath); 144 | return ( 145 | typeof input === 'number' && 146 | typeof compareTo === 'number' && 147 | input < compareTo 148 | ); 149 | } 150 | 151 | if (choice.NumericGreaterThanPath !== undefined) { 152 | const compareTo = jsonpath.value(data, choice.NumericGreaterThanPath); 153 | return ( 154 | typeof input === 'number' && 155 | typeof compareTo === 'number' && 156 | input > compareTo 157 | ); 158 | } 159 | 160 | if (choice.NumericLessThanEqualsPath !== undefined) { 161 | const compareTo = jsonpath.value(data, choice.NumericLessThanEqualsPath); 162 | return ( 163 | typeof input === 'number' && 164 | typeof compareTo === 'number' && 165 | input <= compareTo 166 | ); 167 | } 168 | 169 | if (choice.NumericGreaterThanEqualsPath !== undefined) { 170 | const compareTo = jsonpath.value(data, choice.NumericGreaterThanEqualsPath); 171 | return ( 172 | typeof input === 'number' && 173 | typeof compareTo === 'number' && 174 | input >= compareTo 175 | ); 176 | } 177 | 178 | if (choice.StringMatches !== undefined) { 179 | return ( 180 | typeof input === 'string' && 181 | typeof choice.StringMatches === 'string' && 182 | stringMatches(input, choice.StringMatches) 183 | ); 184 | } 185 | 186 | if (choice.StringEquals !== undefined) { 187 | return ( 188 | typeof input === 'string' && 189 | typeof choice.StringEquals === 'string' && 190 | input === choice.StringEquals 191 | ); 192 | } 193 | 194 | if (choice.StringLessThan !== undefined) { 195 | return ( 196 | typeof input === 'string' && 197 | typeof choice.StringLessThan === 'string' && 198 | input < choice.StringLessThan 199 | ); 200 | } 201 | 202 | if (choice.StringGreaterThan !== undefined) { 203 | return ( 204 | typeof input === 'string' && 205 | typeof choice.StringGreaterThan === 'string' && 206 | input > choice.StringGreaterThan 207 | ); 208 | } 209 | 210 | if (choice.StringLessThanEquals !== undefined) { 211 | return ( 212 | typeof input === 'string' && 213 | typeof choice.StringLessThanEquals === 'string' && 214 | input <= choice.StringLessThanEquals 215 | ); 216 | } 217 | 218 | if (choice.StringGreaterThanEquals !== undefined) { 219 | return ( 220 | typeof input === 'string' && 221 | typeof choice.StringGreaterThanEquals === 'string' && 222 | input >= choice.StringGreaterThanEquals 223 | ); 224 | } 225 | 226 | if (choice.StringEqualsPath !== undefined) { 227 | const compareTo = jsonpath.value(data, choice.StringEqualsPath); 228 | return ( 229 | typeof input === 'string' && 230 | typeof compareTo === 'string' && 231 | input === compareTo 232 | ); 233 | } 234 | 235 | if (choice.StringLessThanPath !== undefined) { 236 | const compareTo = jsonpath.value(data, choice.StringLessThanPath); 237 | return ( 238 | typeof input === 'string' && 239 | typeof compareTo === 'string' && 240 | input < compareTo 241 | ); 242 | } 243 | 244 | if (choice.StringGreaterThanPath !== undefined) { 245 | const compareTo = jsonpath.value(data, choice.StringGreaterThanPath); 246 | return ( 247 | typeof input === 'string' && 248 | typeof compareTo === 'string' && 249 | input > compareTo 250 | ); 251 | } 252 | 253 | if (choice.StringLessThanEqualsPath !== undefined) { 254 | const compareTo = jsonpath.value(data, choice.StringLessThanEqualsPath); 255 | return ( 256 | typeof input === 'string' && 257 | typeof compareTo === 'string' && 258 | input <= compareTo 259 | ); 260 | } 261 | 262 | if (choice.StringGreaterThanEqualsPath !== undefined) { 263 | const compareTo = jsonpath.value(data, choice.StringGreaterThanEqualsPath); 264 | return ( 265 | typeof input === 'string' && 266 | typeof compareTo === 'string' && 267 | input >= compareTo 268 | ); 269 | } 270 | 271 | if (choice.TimestampEquals !== undefined) { 272 | return ( 273 | isTimestamp(input) && 274 | isTimestamp(choice.TimestampEquals) && 275 | new Date(input).getTime() === new Date(choice.TimestampEquals).getTime() 276 | ); 277 | } 278 | 279 | if (choice.TimestampLessThan !== undefined) { 280 | return ( 281 | isTimestamp(input) && 282 | isTimestamp(choice.TimestampLessThan) && 283 | new Date(input).getTime() < new Date(choice.TimestampLessThan).getTime() 284 | ); 285 | } 286 | 287 | if (choice.TimestampGreaterThan !== undefined) { 288 | return ( 289 | isTimestamp(input) && 290 | isTimestamp(choice.TimestampGreaterThan) && 291 | new Date(input).getTime() > 292 | new Date(choice.TimestampGreaterThan).getTime() 293 | ); 294 | } 295 | 296 | if (choice.TimestampLessThanEquals !== undefined) { 297 | return ( 298 | isTimestamp(input) && 299 | isTimestamp(choice.TimestampLessThanEquals) && 300 | new Date(input).getTime() <= 301 | new Date(choice.TimestampLessThanEquals).getTime() 302 | ); 303 | } 304 | 305 | if (choice.TimestampGreaterThanEquals !== undefined) { 306 | return ( 307 | isTimestamp(input) && 308 | isTimestamp(choice.TimestampGreaterThanEquals) && 309 | new Date(input).getTime() >= 310 | new Date(choice.TimestampGreaterThanEquals).getTime() 311 | ); 312 | } 313 | 314 | if (choice.TimestampEqualsPath !== undefined) { 315 | const compareTo = jsonpath.value(data, choice.TimestampEqualsPath); 316 | return ( 317 | isTimestamp(input) && 318 | isTimestamp(compareTo) && 319 | new Date(input).getTime() === new Date(compareTo).getTime() 320 | ); 321 | } 322 | 323 | if (choice.TimestampLessThanPath !== undefined) { 324 | const compareTo = jsonpath.value(data, choice.TimestampLessThanPath); 325 | return ( 326 | isTimestamp(input) && 327 | isTimestamp(compareTo) && 328 | new Date(input).getTime() < new Date(compareTo).getTime() 329 | ); 330 | } 331 | 332 | if (choice.TimestampGreaterThanPath !== undefined) { 333 | const compareTo = jsonpath.value(data, choice.TimestampGreaterThanPath); 334 | return ( 335 | isTimestamp(input) && 336 | isTimestamp(compareTo) && 337 | new Date(input).getTime() > new Date(compareTo).getTime() 338 | ); 339 | } 340 | 341 | if (choice.TimestampLessThanEqualsPath !== undefined) { 342 | const compareTo = jsonpath.value(data, choice.TimestampLessThanEqualsPath); 343 | return ( 344 | isTimestamp(input) && 345 | isTimestamp(compareTo) && 346 | new Date(input).getTime() <= new Date(compareTo).getTime() 347 | ); 348 | } 349 | 350 | if (choice.TimestampGreaterThanEqualsPath !== undefined) { 351 | const compareTo = jsonpath.value( 352 | data, 353 | choice.TimestampGreaterThanEqualsPath 354 | ); 355 | return ( 356 | isTimestamp(input) && 357 | isTimestamp(compareTo) && 358 | new Date(input).getTime() >= new Date(compareTo).getTime() 359 | ); 360 | } 361 | 362 | return ( 363 | choice.IsPresent === (input !== undefined) || 364 | choice.IsNull === (input === null) || 365 | choice.IsBoolean === (typeof input === 'boolean') || 366 | choice.IsNumeric === (typeof input === 'number') || 367 | choice.IsString === (typeof input === 'string') || 368 | choice.IsTimestamp === isTimestamp(input) 369 | ); 370 | }; 371 | 372 | const stringMatches = (value: string, rule: string): boolean => { 373 | const escapeRegex = (str: string) => 374 | str.replace(/[-/^$*+?.()|[]{}]/g, '\\$&'); 375 | const replaceAsterisk = (str: string) => 376 | str 377 | .split(/(? { 387 | if (typeof value !== 'string') { 388 | return false; 389 | } 390 | 391 | const date = new Date(value); 392 | 393 | return !isNaN(date.getTime()) && date.toISOString() === value; 394 | }; 395 | -------------------------------------------------------------------------------- /test/FakeStateMachine-runState.ts: -------------------------------------------------------------------------------- 1 | import { FakeStateMachine } from '../src/FakeStateMachine'; 2 | import { RunStateResult } from '../src/RunStateResult'; 3 | import { addNumbers } from './fixtures/resources'; 4 | 5 | describe('FakeStateMachine#runState()', () => { 6 | describe('when the specified stateName does not exists', () => { 7 | test('should throw an Error', () => { 8 | const definition = require('./fixtures/definitions/startat-does-not-exist.json'); 9 | const fakeStateMachine = new FakeStateMachine(definition, {}); 10 | return expect( 11 | fakeStateMachine.runState( 12 | { 13 | a1: 123, 14 | }, 15 | 'Target' 16 | ) 17 | ).rejects.toThrow('the state Target does not exists'); 18 | }); 19 | }); 20 | describe('when the state has `"End": true`', () => { 21 | test('should be marked as a terminal state', async () => { 22 | const definition = require('./fixtures/definitions/state-with-end-is-true.json'); 23 | const fakeStateMachine = new FakeStateMachine(definition, {}); 24 | return expect( 25 | ( 26 | await fakeStateMachine.runState( 27 | { 28 | a1: 123, 29 | }, 30 | 'Target' 31 | ) 32 | ).isTerminalState 33 | ).toBe(true); 34 | }); 35 | }); 36 | 37 | describe('when the state contains "Next" field', () => { 38 | test('should return the state with results and "Next" destination', async () => { 39 | const definition = require('./fixtures/definitions/state-with-next-property.json'); 40 | const fakeStateMachine = new FakeStateMachine(definition, {}); 41 | expect( 42 | ( 43 | await fakeStateMachine.runState( 44 | { 45 | a1: 123, 46 | }, 47 | 'Target' 48 | ) 49 | ).nextStateName 50 | ).toBe('NextState'); 51 | }); 52 | }); 53 | 54 | describe('when the state does not contain "Next" field and does not have `"End": true`', () => { 55 | test('should throw an error', () => { 56 | const definition = require('./fixtures/definitions/state-without-next-and-end.json'); 57 | const fakeStateMachine = new FakeStateMachine(definition, {}); 58 | return expect( 59 | fakeStateMachine.runState( 60 | { 61 | a1: 123, 62 | }, 63 | 'Target' 64 | ) 65 | ).rejects.toThrow( 66 | 'nextState must be non-null when the state is non-terminal state' 67 | ); 68 | }); 69 | }); 70 | 71 | describe('when the state has `"Type": "Succeed"`', () => { 72 | test('should not change the state and return it', async () => { 73 | const definition = require('./fixtures/definitions/succeed.json'); 74 | const fakeStateMachine = new FakeStateMachine(definition, {}); 75 | 76 | expect(await fakeStateMachine.runState({ sum: 7 }, 'Target')).toEqual( 77 | new RunStateResult({ sum: 7 }, 'Succeed', null, true) 78 | ); 79 | }); 80 | }); 81 | describe('when the state has `"Type": "Fail"`', () => { 82 | test('should not change the state and return it', async () => { 83 | const definition = require('./fixtures/definitions/fail.json'); 84 | const fakeStateMachine = new FakeStateMachine(definition, {}); 85 | 86 | expect(await fakeStateMachine.runState({ sum: 7 }, 'Target')).toEqual( 87 | new RunStateResult({ sum: 7 }, 'Fail', null, true) 88 | ); 89 | }); 90 | }); 91 | describe('when the state has `"Type": "Choice"`', () => { 92 | describe('when Choices contains only one element', () => { 93 | let fakeStateMachine: FakeStateMachine; 94 | 95 | beforeEach(() => { 96 | const choice = { 97 | Variable: '$.condition', 98 | BooleanEquals: true, 99 | Next: 'NextState', 100 | }; 101 | 102 | const definition = { 103 | States: { 104 | Choices: { 105 | Type: 'Choice', 106 | Choices: [choice], 107 | Default: 'DefaultState', 108 | }, 109 | }, 110 | }; 111 | 112 | fakeStateMachine = new FakeStateMachine(definition, {}); 113 | }); 114 | 115 | test('should select next state if condition is met', async () => { 116 | expect( 117 | await fakeStateMachine.runState( 118 | { 119 | condition: true, 120 | }, 121 | 'Choices' 122 | ) 123 | ).toEqual( 124 | new RunStateResult( 125 | { 126 | condition: true, 127 | }, 128 | 'Choice', 129 | 'NextState', 130 | false 131 | ) 132 | ); 133 | }); 134 | 135 | test("should select default state if condition isn't met", async () => { 136 | expect( 137 | await fakeStateMachine.runState( 138 | { 139 | condition: false, 140 | }, 141 | 'Choices' 142 | ) 143 | ).toEqual( 144 | new RunStateResult( 145 | { 146 | condition: false, 147 | }, 148 | 'Choice', 149 | 'DefaultState', 150 | false 151 | ) 152 | ); 153 | }); 154 | }); 155 | 156 | describe('when Choices contains more than one element', () => { 157 | test('should select the expected state as a next state', async () => { 158 | const definition = require('./fixtures/definitions/choice-more-than-one-choice.json'); 159 | const fakeStateMachine = new FakeStateMachine(definition, {}); 160 | expect( 161 | await fakeStateMachine.runState( 162 | { 163 | condition1: false, 164 | condition2: true, 165 | }, 166 | 'Choices' 167 | ) 168 | ).toEqual( 169 | new RunStateResult( 170 | { 171 | condition1: false, 172 | condition2: true, 173 | }, 174 | 'Choice', 175 | 'NextState2', 176 | false 177 | ) 178 | ); 179 | }); 180 | }); 181 | }); 182 | 183 | describe('when the state has `"Type": "Pass"`', () => { 184 | describe('when nor Result nor ResultPath are provided', () => { 185 | test('should copy its input through to its output', async () => { 186 | // https://github.com/oshikiri/fake-step-functions/pull/25 187 | const definition = require('./fixtures/definitions/pass-without-result-and-resultpath.json'); 188 | const fakeStateMachine = new FakeStateMachine(definition, {}); 189 | expect( 190 | await fakeStateMachine.runState( 191 | { 192 | a1: 123, 193 | }, 194 | 'Target' 195 | ) 196 | ).toEqual( 197 | new RunStateResult( 198 | { 199 | a1: 123, 200 | }, 201 | 'Pass', 202 | 'NextState', 203 | false 204 | ) 205 | ); 206 | }); 207 | }); 208 | describe('when there is an Input field', () => { 209 | test('should fill outputPath using Input field', async () => { 210 | const definition = require('./fixtures/definitions/pass-input-to-resultpath.json'); 211 | const fakeStateMachine = new FakeStateMachine(definition, {}); 212 | expect( 213 | await fakeStateMachine.runState( 214 | { 215 | a1: 123, 216 | }, 217 | 'Target' 218 | ) 219 | ).toEqual( 220 | new RunStateResult( 221 | { 222 | a1: 123, 223 | a2: 'a', 224 | }, 225 | 'Pass', 226 | 'NextState', 227 | false 228 | ) 229 | ); 230 | }); 231 | }); 232 | describe('when the state has an InputPath field', () => { 233 | describe('when the InputPath is undefined', () => { 234 | test('should fill outputPath using the whole data (i.e. $)', async () => { 235 | const definition = require('./fixtures/definitions/pass-inputpath-is-undefined.json'); 236 | const fakeStateMachine = new FakeStateMachine(definition, {}); 237 | expect( 238 | await fakeStateMachine.runState( 239 | { 240 | a1: 123, 241 | }, 242 | 'Target' 243 | ) 244 | ).toEqual( 245 | new RunStateResult( 246 | { 247 | a1: 123, 248 | a2: { a1: 123 }, 249 | }, 250 | 'Pass', 251 | 'NextState', 252 | false 253 | ) 254 | ); 255 | }); 256 | }); 257 | describe('when the state contains Result', () => { 258 | test('should fill the content of Result to ResultPath', async () => { 259 | const definition = require('./fixtures/definitions/pass-result-to-resultpath.json'); 260 | const fakeStateMachine = new FakeStateMachine(definition, {}); 261 | expect( 262 | await fakeStateMachine.runState( 263 | { 264 | a1: 123, 265 | }, 266 | 'Target' 267 | ) 268 | ).toEqual( 269 | new RunStateResult( 270 | { 271 | a1: 123, 272 | a2: 'a', 273 | }, 274 | 'Pass', 275 | 'NextState', 276 | false 277 | ) 278 | ); 279 | }); 280 | }); 281 | describe('when the InputPath is null', () => { 282 | test('should fill outputPath using {}', async () => { 283 | const definition: any = require('./fixtures/definitions/task-inputpath-is-null.json'); 284 | const fakeStateMachine = new FakeStateMachine(definition, {}); 285 | expect( 286 | await fakeStateMachine.runState( 287 | { 288 | a1: 123, 289 | }, 290 | 'Target' 291 | ) 292 | ).toEqual( 293 | new RunStateResult( 294 | { 295 | a1: 123, 296 | a2: {}, 297 | }, 298 | 'Pass', 299 | 'NextState', 300 | false 301 | ) 302 | ); 303 | }); 304 | }); 305 | describe('when the InputPath is non-null', () => { 306 | test('should fill outputPath using InputPath field', async () => { 307 | const definition = require('./fixtures/definitions/task-inputpath-to-outputpath.json'); 308 | const fakeStateMachine = new FakeStateMachine(definition, {}); 309 | expect( 310 | await fakeStateMachine.runState( 311 | { 312 | a1: 123, 313 | }, 314 | 'Target' 315 | ) 316 | ).toEqual( 317 | new RunStateResult( 318 | { 319 | a1: 123, 320 | a2: 123, 321 | }, 322 | 'Pass', 323 | 'NextState', 324 | false 325 | ) 326 | ); 327 | }); 328 | }); 329 | }); 330 | describe('when the InputPath points a path like $.a.b3.c2', () => { 331 | test('should parse the InputPath correctly', async () => { 332 | const definition = require('./fixtures/definitions/task-complex-inputpath-and-outputpath.json'); 333 | const fakeStateMachine = new FakeStateMachine(definition, {}); 334 | expect( 335 | await fakeStateMachine.runState( 336 | { 337 | a: { 338 | b1: 'a-b1', 339 | b2: { c1: 'a-b2-c1' }, 340 | b3: { c1: 'a-b3-c1' }, 341 | }, 342 | }, 343 | 'Target' 344 | ) 345 | ).toEqual( 346 | new RunStateResult( 347 | { 348 | a: { 349 | b1: 'a-b1', 350 | b2: { c1: 'a-b2-c1' }, 351 | b3: { c1: 'a-b3-c1', c2: 'a-b2-c1' }, 352 | }, 353 | }, 354 | 'Pass', 355 | 'NextState', 356 | false 357 | ) 358 | ); 359 | }); 360 | }); 361 | }); 362 | 363 | describe('when the state has `"Type": "Task"`', () => { 364 | const fakeResources = { 365 | 'arn:aws:lambda:us-east-1:123456789012:function:Add': addNumbers, 366 | 'arn:aws:lambda:us-east-1:123456789012:function:AddAsync': 367 | async (numbers: { val1: number; val2: number }) => 368 | numbers.val1 + numbers.val2, 369 | 'arn:aws:lambda:us-east-1:123456789012:function:Double': (n: number) => 370 | 2 * n, 371 | 'arn:aws:lambda:us-east-1:123456789012:function:Identity': (x: any) => x, 372 | }; 373 | describe('when there is an InputPath field', () => { 374 | test('should pass the specified subset to the Resource', async () => { 375 | const definition = require('./fixtures/definitions/task-inputpath.json'); 376 | const fakeStateMachine = new FakeStateMachine( 377 | definition, 378 | fakeResources 379 | ); 380 | expect( 381 | await fakeStateMachine.runState( 382 | { 383 | numbers: { val1: 3, val2: 4 }, 384 | }, 385 | 'Target' 386 | ) 387 | ).toEqual( 388 | new RunStateResult( 389 | { 390 | numbers: { val1: 3, val2: 4 }, 391 | sum: 7, 392 | }, 393 | 'Task', 394 | 'NextState', 395 | false 396 | ) 397 | ); 398 | }); 399 | }); 400 | describe('when the fakeResource is an async function', () => { 401 | test('should pass the specified subset to the Resource', async () => { 402 | const definition = require('./fixtures/definitions/task-addasync.json'); 403 | const fakeStateMachine = new FakeStateMachine( 404 | definition, 405 | fakeResources 406 | ); 407 | expect( 408 | await fakeStateMachine.runState( 409 | { 410 | numbers: { val1: 3, val2: 4 }, 411 | }, 412 | 'Add' 413 | ) 414 | ).toEqual( 415 | new RunStateResult( 416 | { 417 | numbers: { val1: 3, val2: 4 }, 418 | sum: 7, 419 | }, 420 | 'Task', 421 | null, 422 | true 423 | ) 424 | ); 425 | }); 426 | }); 427 | describe('when the InputPath points a path like $.a.b3.c2', () => { 428 | test('should pass the specified subset to the Resource', async () => { 429 | const definition = require('./fixtures/definitions/task-complex-inputpath.json'); 430 | const fakeStateMachine = new FakeStateMachine( 431 | definition, 432 | fakeResources 433 | ); 434 | expect( 435 | await fakeStateMachine.runState( 436 | { 437 | a: { 438 | b3: { 439 | c2: { val1: 3, val2: 4 }, 440 | }, 441 | }, 442 | }, 443 | 'Target' 444 | ) 445 | ).toEqual( 446 | new RunStateResult( 447 | { 448 | a: { 449 | b3: { 450 | c2: { val1: 3, val2: 4 }, 451 | }, 452 | }, 453 | sum: 7, 454 | }, 455 | 'Task', 456 | 'NextState', 457 | false 458 | ) 459 | ); 460 | }); 461 | }); 462 | describe('when the Task state is called without InputPath', () => { 463 | test('should pass $ to the Resource', async () => { 464 | const definition = require('./fixtures/definitions/task-without-input.json'); 465 | const fakeStateMachine = new FakeStateMachine( 466 | definition, 467 | fakeResources 468 | ); 469 | expect( 470 | await fakeStateMachine.runState( 471 | { 472 | val1: 3, 473 | val2: 4, 474 | }, 475 | 'Target' 476 | ) 477 | ).toEqual( 478 | new RunStateResult( 479 | { 480 | val1: 3, 481 | val2: 4, 482 | sum: 7, 483 | }, 484 | 'Task', 485 | 'NextState', 486 | false 487 | ) 488 | ); 489 | }); 490 | }); 491 | describe('when the Task state is called with Input', () => { 492 | test('should pass the Input to the Resource', async () => { 493 | const definition = require('./fixtures/definitions/task-input-to-resource.json'); 494 | const fakeStateMachine = new FakeStateMachine( 495 | definition, 496 | fakeResources 497 | ); 498 | expect(await fakeStateMachine.runState({}, 'Target')).toEqual( 499 | new RunStateResult( 500 | { 501 | result: 6, 502 | }, 503 | 'Task', 504 | 'NextState', 505 | false 506 | ) 507 | ); 508 | }); 509 | }); 510 | describe('when the Task state does not contain ResultPath', () => { 511 | test('should use the default value ResultPath=`$`', async () => { 512 | const definition = require('./fixtures/definitions/task-resultpath-is-undefined.json'); 513 | const fakeStateMachine = new FakeStateMachine( 514 | definition, 515 | fakeResources 516 | ); 517 | expect(await fakeStateMachine.runState({}, 'Target')).toEqual( 518 | new RunStateResult( 519 | { 520 | a: 1, 521 | b: 2, 522 | }, 523 | 'Task', 524 | 'NextState', 525 | false 526 | ) 527 | ); 528 | }); 529 | }); 530 | describe('when the Task state contains an unknown fake resource', () => { 531 | test('should raise an error', async () => { 532 | const definition = require('./fixtures/definitions/task-unknown-resource.json'); 533 | const fakeStateMachine = new FakeStateMachine( 534 | definition, 535 | fakeResources 536 | ); 537 | return expect(fakeStateMachine.runState({}, 'Target')).rejects.toThrow( 538 | 'Unknown resource: arn:aws:lambda:us-east-1:123456789012:function:Unknown' 539 | ); 540 | }); 541 | }); 542 | describe('when the Task state contains a Parameters property', () => { 543 | test('should pass the specified parameters', async () => { 544 | const definition = require('./fixtures/definitions/parameter-property.json'); 545 | let input: any; 546 | const fakeStateMachine = new FakeStateMachine(definition, { 547 | 'arn:aws:lambda:us-east-1:123456789012:function:saveInput': ( 548 | event: any 549 | ) => { 550 | input = event; 551 | return event.input.val1 + event.input.val2; 552 | }, 553 | }); 554 | const actual = await fakeStateMachine.runState( 555 | { a: 2, b: { c: { val2: 4 } } }, 556 | 'Target' 557 | ); 558 | expect(input).toEqual({ 559 | input: { val1: 3, val2: 4 }, 560 | arrayInput: [ 561 | { val1: 3, val2: 4 }, 562 | { val1: 4, val2: 3 }, 563 | ], 564 | }); 565 | expect(actual.data).toEqual({ 566 | a: 2, 567 | b: { 568 | c: { val2: 4 }, 569 | }, 570 | result: 7, 571 | }); 572 | }); 573 | }); 574 | }); 575 | describe('when the state has `"Type": "Wait"`', () => { 576 | test.todo('pending'); 577 | }); 578 | describe('when the state has `"Type": "Parallel"`', () => { 579 | test.todo('pending'); 580 | }); 581 | }); 582 | -------------------------------------------------------------------------------- /test/helper/choiceHelper.ts: -------------------------------------------------------------------------------- 1 | import * as choiceHelper from '../../src/helper/choiceHelper'; 2 | 3 | interface ConditionTestCase { 4 | conditionType: string; 5 | condition: string | number | boolean | null; 6 | compareTo?: string | number | boolean | null; 7 | testCases: Array<[string | number | boolean | null, boolean]>; 8 | } 9 | 10 | describe('choiceHelper#isRightChoice()', () => { 11 | describe('when simple choice rule applied', () => { 12 | const conditions: ConditionTestCase[] = [ 13 | { 14 | conditionType: 'BooleanEquals', 15 | condition: true, 16 | testCases: [ 17 | [null, false], 18 | ['abc', false], 19 | ['5', false], 20 | [5, false], 21 | [false, false], 22 | [true, true], 23 | ], 24 | }, 25 | { 26 | conditionType: 'BooleanEquals', 27 | condition: false, 28 | testCases: [ 29 | [null, false], 30 | ['abc', false], 31 | ['5', false], 32 | [5, false], 33 | [false, true], 34 | [true, false], 35 | ], 36 | }, 37 | { 38 | conditionType: 'BooleanEquals', 39 | condition: null, 40 | testCases: [ 41 | [false, false], 42 | [true, false], 43 | ], 44 | }, 45 | { 46 | conditionType: 'BooleanEquals', 47 | condition: 5, 48 | testCases: [ 49 | [false, false], 50 | [true, false], 51 | ], 52 | }, 53 | { 54 | conditionType: 'BooleanEquals', 55 | condition: 'abc', 56 | testCases: [ 57 | [false, false], 58 | [true, false], 59 | ], 60 | }, 61 | { 62 | conditionType: 'BooleanEqualsPath', 63 | condition: '$.compareTo', 64 | compareTo: true, 65 | testCases: [ 66 | [null, false], 67 | ['abc', false], 68 | ['5', false], 69 | [5, false], 70 | [false, false], 71 | [true, true], 72 | ], 73 | }, 74 | { 75 | conditionType: 'BooleanEqualsPath', 76 | condition: '$.compareTo', 77 | compareTo: null, 78 | testCases: [ 79 | [false, false], 80 | [true, false], 81 | ], 82 | }, 83 | { 84 | conditionType: 'BooleanEqualsPath', 85 | condition: '$.compareTo', 86 | compareTo: 'abc', 87 | testCases: [ 88 | [false, false], 89 | [true, false], 90 | ], 91 | }, 92 | { 93 | conditionType: 'BooleanEqualsPath', 94 | condition: '$.compareTo', 95 | compareTo: 5, 96 | testCases: [ 97 | [false, false], 98 | [true, false], 99 | ], 100 | }, 101 | { 102 | conditionType: 'BooleanEqualsPath', 103 | condition: '$.compareTo', 104 | compareTo: false, 105 | testCases: [ 106 | [false, true], 107 | [true, false], 108 | ], 109 | }, 110 | { 111 | conditionType: 'NumericEquals', 112 | condition: 5, 113 | testCases: [ 114 | [null, false], 115 | ['abc', false], 116 | ['5', false], 117 | [5.0, true], 118 | [5.1, false], 119 | ], 120 | }, 121 | { 122 | conditionType: 'NumericLessThan', 123 | condition: 5, 124 | testCases: [ 125 | [null, false], 126 | ['abc', false], 127 | ['5', false], 128 | [4.9, true], 129 | [5.0, false], 130 | [5.1, false], 131 | ], 132 | }, 133 | { 134 | conditionType: 'NumericLessThanEquals', 135 | condition: 5, 136 | testCases: [ 137 | [null, false], 138 | ['abc', false], 139 | ['5', false], 140 | [4.9, true], 141 | [5.0, true], 142 | [5.1, false], 143 | ], 144 | }, 145 | { 146 | conditionType: 'NumericGreaterThan', 147 | condition: 5, 148 | testCases: [ 149 | [null, false], 150 | ['abc', false], 151 | ['5', false], 152 | [4.9, false], 153 | [5.0, false], 154 | [5.1, true], 155 | ], 156 | }, 157 | { 158 | conditionType: 'NumericGreaterThanEquals', 159 | condition: 5, 160 | testCases: [ 161 | [null, false], 162 | ['abc', false], 163 | ['5', false], 164 | [4.9, false], 165 | [5.0, true], 166 | [5.1, true], 167 | ], 168 | }, 169 | { 170 | conditionType: 'NumericEqualsPath', 171 | condition: '$.compareTo', 172 | compareTo: 5, 173 | testCases: [ 174 | [null, false], 175 | ['abc', false], 176 | ['5', false], 177 | [5.1, false], 178 | [5.0, true], 179 | [4.9, false], 180 | ], 181 | }, 182 | { 183 | conditionType: 'NumericLessThanPath', 184 | condition: '$.compareTo', 185 | compareTo: 5, 186 | testCases: [ 187 | [null, false], 188 | ['abc', false], 189 | ['5', false], 190 | [5.1, false], 191 | [5.0, false], 192 | [4.9, true], 193 | ], 194 | }, 195 | { 196 | conditionType: 'NumericGreaterThanPath', 197 | condition: '$.compareTo', 198 | compareTo: 5, 199 | testCases: [ 200 | [null, false], 201 | ['abc', false], 202 | ['5', false], 203 | [5.1, true], 204 | [5.0, false], 205 | [4.9, false], 206 | ], 207 | }, 208 | { 209 | conditionType: 'NumericLessThanEqualsPath', 210 | condition: '$.compareTo', 211 | compareTo: 5, 212 | testCases: [ 213 | [null, false], 214 | ['abc', false], 215 | ['5', false], 216 | [5.1, false], 217 | [5.0, true], 218 | [4.9, true], 219 | ], 220 | }, 221 | { 222 | conditionType: 'NumericGreaterThanEqualsPath', 223 | condition: '$.compareTo', 224 | compareTo: 5, 225 | testCases: [ 226 | [null, false], 227 | ['abc', false], 228 | ['5', false], 229 | [5.1, true], 230 | [5.0, true], 231 | [4.9, false], 232 | ], 233 | }, 234 | { 235 | conditionType: 'StringEquals', 236 | condition: 'abc', 237 | testCases: [ 238 | [null, false], 239 | [5, false], 240 | ['ab', false], 241 | ['abc', true], 242 | ], 243 | }, 244 | { 245 | conditionType: 'StringMatches', 246 | condition: 'a*c', 247 | testCases: [ 248 | [null, false], 249 | [5, false], 250 | ['ab', false], 251 | ['bc', false], 252 | ['abc', true], 253 | ['ac', true], 254 | ['abdefc', true], 255 | ], 256 | }, 257 | { 258 | conditionType: 'StringMatches', 259 | // Escaping backslash in string, so it's actually one backslash 260 | condition: 'a\\*c', 261 | testCases: [ 262 | ['ab', false], 263 | ['bc', false], 264 | ['a*c', true], 265 | ['a\\*c', false], 266 | ['ac', false], 267 | ], 268 | }, 269 | { 270 | conditionType: 'StringMatches', 271 | // Escaping backslashes in string, so it's actually 2 backslashes 272 | condition: 'a\\\\*c', 273 | testCases: [ 274 | ['ab', false], 275 | ['bc', false], 276 | ['a*c', false], 277 | ['a\\*c', true], 278 | ['a\\c', true], 279 | ['a\\defc', true], 280 | ['ac', false], 281 | ], 282 | }, 283 | { 284 | conditionType: 'StringLessThan', 285 | condition: 'abc', 286 | testCases: [ 287 | [null, false], 288 | [5, false], 289 | ['ab', true], 290 | ['aba', true], 291 | ['abd', false], 292 | ['abc', false], 293 | ], 294 | }, 295 | { 296 | conditionType: 'StringLessThanEquals', 297 | condition: 'abc', 298 | testCases: [ 299 | [null, false], 300 | [5, false], 301 | ['aba', true], 302 | ['abd', false], 303 | ['abc', true], 304 | ], 305 | }, 306 | { 307 | conditionType: 'StringGreaterThan', 308 | condition: 'abc', 309 | testCases: [ 310 | [null, false], 311 | [5, false], 312 | ['aba', false], 313 | ['abd', true], 314 | ['az', true], 315 | ['abc', false], 316 | ], 317 | }, 318 | { 319 | conditionType: 'StringGreaterThanEquals', 320 | condition: 'abc', 321 | testCases: [ 322 | [null, false], 323 | [5, false], 324 | ['aba', false], 325 | ['abd', true], 326 | ['az', true], 327 | ['abc', true], 328 | ], 329 | }, 330 | { 331 | conditionType: 'StringEqualsPath', 332 | condition: '$.compareTo', 333 | compareTo: 'abc', 334 | testCases: [ 335 | [null, false], 336 | [5, false], 337 | ['aba', false], 338 | ['abd', false], 339 | ['abc', true], 340 | ], 341 | }, 342 | { 343 | conditionType: 'StringLessThanPath', 344 | condition: '$.compareTo', 345 | compareTo: 'abc', 346 | testCases: [ 347 | [null, false], 348 | [5, false], 349 | ['aba', true], 350 | ['abd', false], 351 | ['abc', false], 352 | ], 353 | }, 354 | { 355 | conditionType: 'StringGreaterThanPath', 356 | condition: '$.compareTo', 357 | compareTo: 'abc', 358 | testCases: [ 359 | [null, false], 360 | [5, false], 361 | ['aba', false], 362 | ['abd', true], 363 | ['az', true], 364 | ['abc', false], 365 | ], 366 | }, 367 | { 368 | conditionType: 'StringLessThanEqualsPath', 369 | condition: '$.compareTo', 370 | compareTo: 'abc', 371 | testCases: [ 372 | [null, false], 373 | [5, false], 374 | ['aba', true], 375 | ['abd', false], 376 | ['abc', true], 377 | ], 378 | }, 379 | { 380 | conditionType: 'StringGreaterThanEqualsPath', 381 | condition: '$.compareTo', 382 | compareTo: 'abc', 383 | testCases: [ 384 | [null, false], 385 | [5, false], 386 | ['aba', false], 387 | ['abd', true], 388 | ['az', true], 389 | ['abc', true], 390 | ], 391 | }, 392 | { 393 | conditionType: 'TimestampEquals', 394 | condition: '2006-01-02T15:04:05.000Z', 395 | testCases: [ 396 | [null, false], 397 | [true, false], 398 | [5, false], 399 | ['ab', false], 400 | ['2006-01-02', false], 401 | ['2006-01-02T11:45:05.000Z', false], 402 | ['2006-01-02T15:04:05.000Z', true], 403 | ['2006-02-01T15:04:05.000Z', false], 404 | ], 405 | }, 406 | { 407 | conditionType: 'TimestampLessThan', 408 | condition: '2006-01-02T15:04:05.000Z', 409 | testCases: [ 410 | [null, false], 411 | [true, false], 412 | [5, false], 413 | ['ab', false], 414 | ['2006-01-02', false], 415 | ['2006-01-02T11:45:05.000Z', true], 416 | ['2006-01-02T15:04:05.000Z', false], 417 | ['2006-02-01T15:04:05.000Z', false], 418 | ], 419 | }, 420 | { 421 | conditionType: 'TimestampLessThanEquals', 422 | condition: '2006-01-02T15:04:05.000Z', 423 | testCases: [ 424 | [null, false], 425 | [true, false], 426 | [5, false], 427 | ['ab', false], 428 | ['2006-01-02', false], 429 | ['2006-01-02T11:45:05.000Z', true], 430 | ['2006-01-02T15:04:05.000Z', true], 431 | ['2006-02-01T15:04:05.000Z', false], 432 | ], 433 | }, 434 | { 435 | conditionType: 'TimestampGreaterThan', 436 | condition: '2006-01-02T15:04:05.000Z', 437 | testCases: [ 438 | [null, false], 439 | [true, false], 440 | [5, false], 441 | ['ab', false], 442 | ['2006-01-02', false], 443 | ['2006-01-02T11:45:05.000Z', false], 444 | ['2006-01-02T15:04:05.000Z', false], 445 | ['2006-02-01T15:04:05.000Z', true], 446 | ], 447 | }, 448 | { 449 | conditionType: 'TimestampGreaterThanEquals', 450 | condition: '2006-01-02T15:04:05.000Z', 451 | testCases: [ 452 | [null, false], 453 | [true, false], 454 | [5, false], 455 | ['ab', false], 456 | ['2006-01-02', false], 457 | ['2006-01-02T11:45:05.000Z', false], 458 | ['2006-01-02T15:04:05.000Z', true], 459 | ['2006-02-01T15:04:05.000Z', true], 460 | ], 461 | }, 462 | { 463 | conditionType: 'TimestampEqualsPath', 464 | condition: '$.compareTo', 465 | compareTo: '2006-01-02T15:04:05.000Z', 466 | testCases: [ 467 | [null, false], 468 | [true, false], 469 | [5, false], 470 | ['ab', false], 471 | ['2006-01-02', false], 472 | ['2006-01-02T11:45:05.000Z', false], 473 | ['2006-01-02T15:04:05.000Z', true], 474 | ['2006-02-01T15:04:05.000Z', false], 475 | ], 476 | }, 477 | { 478 | conditionType: 'TimestampLessThanPath', 479 | condition: '$.compareTo', 480 | compareTo: '2006-01-02T15:04:05.000Z', 481 | testCases: [ 482 | [null, false], 483 | [true, false], 484 | [5, false], 485 | ['ab', false], 486 | ['2006-01-02', false], 487 | ['2006-01-02T11:45:05.000Z', true], 488 | ['2006-01-02T15:04:05.000Z', false], 489 | ['2006-02-01T15:04:05.000Z', false], 490 | ], 491 | }, 492 | { 493 | conditionType: 'TimestampGreaterThanPath', 494 | condition: '$.compareTo', 495 | compareTo: '2006-01-02T15:04:05.000Z', 496 | testCases: [ 497 | [null, false], 498 | [true, false], 499 | [5, false], 500 | ['ab', false], 501 | ['2006-01-02', false], 502 | ['2006-01-02T11:45:05.000Z', false], 503 | ['2006-01-02T15:04:05.000Z', false], 504 | ['2006-02-01T15:04:05.000Z', true], 505 | ], 506 | }, 507 | { 508 | conditionType: 'TimestampLessThanEqualsPath', 509 | condition: '$.compareTo', 510 | compareTo: '2006-01-02T15:04:05.000Z', 511 | testCases: [ 512 | [null, false], 513 | [true, false], 514 | [5, false], 515 | ['ab', false], 516 | ['2006-01-02', false], 517 | ['2006-01-02T11:45:05.000Z', true], 518 | ['2006-01-02T15:04:05.000Z', true], 519 | ['2006-02-01T15:04:05.000Z', false], 520 | ], 521 | }, 522 | { 523 | conditionType: 'TimestampGreaterThanEqualsPath', 524 | condition: '$.compareTo', 525 | compareTo: '2006-01-02T15:04:05.000Z', 526 | testCases: [ 527 | [null, false], 528 | [true, false], 529 | [5, false], 530 | ['ab', false], 531 | ['2006-01-02', false], 532 | ['2006-01-02T11:45:05.000Z', false], 533 | ['2006-01-02T15:04:05.000Z', true], 534 | ['2006-02-01T15:04:05.000Z', true], 535 | ], 536 | }, 537 | { 538 | conditionType: 'IsPresent', 539 | condition: true, 540 | testCases: [ 541 | [undefined, false], 542 | [null, true], 543 | [0, true], 544 | ['abc', true], 545 | ], 546 | }, 547 | { 548 | conditionType: 'IsPresent', 549 | condition: false, 550 | testCases: [ 551 | [undefined, true], 552 | [null, false], 553 | [0, false], 554 | ['abc', false], 555 | ], 556 | }, 557 | { 558 | conditionType: 'IsNull', 559 | condition: true, 560 | testCases: [ 561 | [5, false], 562 | [null, true], 563 | ], 564 | }, 565 | { 566 | conditionType: 'IsNull', 567 | condition: false, 568 | testCases: [ 569 | [5, true], 570 | [null, false], 571 | ], 572 | }, 573 | { 574 | conditionType: 'IsBoolean', 575 | condition: true, 576 | testCases: [ 577 | [null, false], 578 | [false, true], 579 | ], 580 | }, 581 | { 582 | conditionType: 'IsBoolean', 583 | condition: false, 584 | testCases: [ 585 | [null, true], 586 | [false, false], 587 | ], 588 | }, 589 | { 590 | conditionType: 'IsNumeric', 591 | condition: true, 592 | testCases: [ 593 | ['abc', false], 594 | [5, true], 595 | [5.1, true], 596 | ], 597 | }, 598 | { 599 | conditionType: 'IsNumeric', 600 | condition: false, 601 | testCases: [ 602 | ['abc', true], 603 | [5, false], 604 | [5.1, false], 605 | ], 606 | }, 607 | { 608 | conditionType: 'IsString', 609 | condition: true, 610 | testCases: [ 611 | [5.1, false], 612 | ['abc', true], 613 | ], 614 | }, 615 | { 616 | conditionType: 'IsString', 617 | condition: false, 618 | testCases: [ 619 | [5.1, true], 620 | ['abc', false], 621 | ], 622 | }, 623 | { 624 | conditionType: 'IsTimestamp', 625 | condition: true, 626 | testCases: [ 627 | ['abc', false], 628 | ['2006-01-02T15:04:05.000Z', true], 629 | ], 630 | }, 631 | { 632 | conditionType: 'IsTimestamp', 633 | condition: false, 634 | testCases: [ 635 | ['abc', true], 636 | ['2006-01-02T15:04:05.000Z', false], 637 | ], 638 | }, 639 | ]; 640 | 641 | conditions.forEach(({ conditionType, condition, compareTo, testCases }) => { 642 | describe(`with ${conditionType}`, () => { 643 | testCases.forEach((testCaseRow) => { 644 | const [inputValue, expectedResult] = testCaseRow; 645 | const compareToSubstitution = 646 | compareTo !== undefined ? `(${compareTo})` : ''; 647 | 648 | describe(`when condition=${condition}${compareToSubstitution} and input=${inputValue}`, () => { 649 | const choice = { 650 | Variable: '$.condition', 651 | [conditionType]: condition, 652 | Next: 'NextState', 653 | }; 654 | 655 | test(`the result should be ${expectedResult}`, () => { 656 | expect( 657 | choiceHelper.isRightChoice(choice, { 658 | condition: inputValue, 659 | compareTo, 660 | }) 661 | ).toEqual(expectedResult); 662 | }); 663 | }); 664 | }); 665 | }); 666 | }); 667 | }); 668 | 669 | describe('when complex choice rule applied', () => { 670 | describe('with And', () => { 671 | const choice = { 672 | And: [ 673 | { 674 | Variable: '$.condition', 675 | NumericLessThan: 5, 676 | }, 677 | { 678 | Variable: '$.condition', 679 | NumericGreaterThan: 4, 680 | }, 681 | ], 682 | Next: 'NextState', 683 | }; 684 | 685 | test(`the result should be true if all conditions met`, async () => { 686 | expect(choiceHelper.isRightChoice(choice, { condition: 4.5 })).toEqual( 687 | true 688 | ); 689 | }); 690 | 691 | test(`the result should be false if only some conditions met`, async () => { 692 | expect(choiceHelper.isRightChoice(choice, { condition: 5.5 })).toEqual( 693 | false 694 | ); 695 | }); 696 | 697 | test(`the result should be false if none conditions met`, async () => { 698 | expect(choiceHelper.isRightChoice(choice, { condition: 0 })).toEqual( 699 | false 700 | ); 701 | }); 702 | }); 703 | 704 | describe('with Or', () => { 705 | const choice = { 706 | Or: [ 707 | { 708 | Variable: '$.condition', 709 | NumericLessThan: 6, 710 | }, 711 | { 712 | Variable: '$.condition', 713 | NumericLessThan: 5, 714 | }, 715 | ], 716 | Next: 'NextState', 717 | }; 718 | 719 | test(`the result should be true if all conditions met`, async () => { 720 | expect(choiceHelper.isRightChoice(choice, { condition: 4.5 })).toEqual( 721 | true 722 | ); 723 | }); 724 | 725 | test(`the result should be true if only some conditions met`, async () => { 726 | expect(choiceHelper.isRightChoice(choice, { condition: 5.5 })).toEqual( 727 | true 728 | ); 729 | }); 730 | 731 | test(`the result should be false if none conditions met`, async () => { 732 | expect(choiceHelper.isRightChoice(choice, { condition: 7 })).toEqual( 733 | false 734 | ); 735 | }); 736 | }); 737 | 738 | describe('with Not', () => { 739 | const choice = { 740 | Not: { 741 | Variable: '$.condition', 742 | NumericLessThan: 5, 743 | }, 744 | Next: 'NextState', 745 | }; 746 | 747 | test(`the result should be true if internal condition isn\'t met`, async () => { 748 | expect(choiceHelper.isRightChoice(choice, { condition: 5.5 })).toEqual( 749 | true 750 | ); 751 | }); 752 | 753 | test(`the result should be false if internal condition is met`, async () => { 754 | expect(choiceHelper.isRightChoice(choice, { condition: 4.5 })).toEqual( 755 | false 756 | ); 757 | }); 758 | }); 759 | }); 760 | }); 761 | --------------------------------------------------------------------------------