UNPKG

5.08 kBJavaScriptView Raw
1/* eslint-env mocha */
2
3const tymly = require('./../lib')
4const path = require('path')
5const expect = require('chai').expect
6const STATE_MACHINE_NAME = 'tymlyTest_aDayInTheLife'
7
8describe('Statebox Service', function () {
9 let tymlyService
10 let statebox
11 this.timeout(process.env.TIMEOUT || 5000)
12 let rupert
13
14 describe('start up', () => {
15 it('create some basic tymly services to run a simple cat blueprint', function (done) {
16 tymly.boot(
17 {
18 blueprintPaths: [
19 path.resolve(__dirname, './fixtures/blueprints/cats-blueprint'),
20 path.resolve(__dirname, './fixtures/blueprints/it-lives-blueprint')
21 ],
22
23 pluginPaths: [
24 path.resolve(__dirname, './fixtures/plugins/cats-plugin'),
25 path.resolve(__dirname, './fixtures/plugins/it-lives-plugin'),
26 path.resolve(__dirname, '../node_modules/@wmfs/tymly-test-helpers/plugins/allow-everything-rbac-plugin')
27 ]
28 },
29 function (err, tymlyServices) {
30 expect(err).to.eql(null)
31 tymlyService = tymlyServices.tymly
32 statebox = tymlyServices.statebox
33 done()
34 }
35 )
36 })
37 })
38
39 describe('find things', () => {
40 it('find cat state machine', function () {
41 const stateMachine = statebox.findStateMachineByName(STATE_MACHINE_NAME)
42 expect(stateMachine.name).to.eql(STATE_MACHINE_NAME)
43 })
44
45 it('fail finding dog state machine', function () {
46 const stateMachine = statebox.findStateMachineByName('DOGS!')
47 expect(stateMachine).to.be.an('undefined')
48 })
49 })
50
51 describe('run state machine', () => {
52 it('execute cat state machine', function (done) {
53 statebox.startExecution(
54 {
55 petName: 'Rupert',
56 gender: 'male',
57 hoursSinceLastMotion: 11,
58 hoursSinceLastMeal: 5,
59 petDiary: []
60 }, // input
61 STATE_MACHINE_NAME, // state machine name
62 {}, // options
63 function (err, result) {
64 expect(err).to.eql(null)
65 rupert = result.executionName
66 done()
67 }
68 )
69 })
70
71 it('successfully complete Rupert\'s day', function (done) {
72 statebox.waitUntilStoppedRunning(
73 rupert,
74 function (err, executionDescription) {
75 expect(err).to.eql(null)
76 expect(executionDescription.status).to.eql('SUCCEEDED')
77 expect(executionDescription.stateMachineName).to.eql('tymlyTest_aDayInTheLife')
78 expect(executionDescription.currentStateName).to.eql('Sleeping')
79 expect(executionDescription.ctx.hoursSinceLastMeal).to.eql(0)
80 expect(executionDescription.ctx.hoursSinceLastMotion).to.eql(0)
81 expect(executionDescription.ctx.gender).to.eql('male')
82 expect(executionDescription.ctx.petDiary).to.be.an('array')
83 expect(executionDescription.ctx.petDiary[0]).to.equal('Look out, Rupert is waking up!')
84 expect(executionDescription.ctx.petDiary[2]).to.equal('Rupert is walking... where\'s he off to?')
85 expect(executionDescription.ctx.petDiary[6]).to.equal('Shh, Rupert is eating...')
86 done()
87 }
88 )
89 })
90 })
91
92 describe('can\'t run things which don\'t exist', () => {
93 it('fail to execute dog state machine', function (done) {
94 statebox.startExecution(
95 {
96 dogName: 'Scooby',
97 gender: 'male',
98 hoursSinceLastMotion: 1,
99 hoursSinceLastMeal: 0,
100 petDiary: []
101 }, // input
102 'DOG_MACHINE', // state machine name
103 {}, // options
104 function (err, result) {
105 expect(err.message).to.eql('Unknown stateMachine with name \'DOG_MACHINE\'')
106 done()
107 }
108 )
109 })
110 })
111
112 describe('fail, come back to life, succeed', () => {
113 let executionName
114
115 it('start', async () => {
116 const executionDescription = await statebox.startExecution(
117 {}, // input
118 'tymlyTest_helloFailButLiveAgain', // state machine name
119 {} // options
120 )
121
122 executionName = executionDescription.executionName
123 })
124
125 it('oh dear', async () => {
126 const executionDescription = await statebox.waitUntilStoppedRunning(executionName)
127
128 expect(executionDescription.status).to.eql('FAILED')
129 expect(executionDescription.stateMachineName).to.eql('tymlyTest_helloFailButLiveAgain')
130 expect(executionDescription.currentStateName).to.eql('Stuttery')
131 })
132
133 it('raise from the grave', async () => {
134 await statebox.sendTaskRevivification(
135 executionName,
136 {}
137 )
138 })
139
140 it('it lives', async () => {
141 const executionDescription = await statebox.waitUntilStoppedRunning(executionName)
142
143 expect(executionDescription.status).to.eql('SUCCEEDED')
144 expect(executionDescription.stateMachineName).to.eql('tymlyTest_helloFailButLiveAgain')
145 expect(executionDescription.currentStateName).to.eql('IT-LIVES')
146 })
147 })
148
149 describe('clean up', () => {
150 it('shutdown Tymly', async () => {
151 await tymlyService.shutdown()
152 })
153 })
154})