UNPKG

5.83 kBJavaScriptView Raw
1/* eslint-env mocha */
2
3const path = require('path')
4const expect = require('chai').expect
5const tymly = require('../lib')
6
7process.on('unhandledRejection', (reason, p) => {
8 console.log('Unhandled Rejection at: Promise', p, 'reason:', reason)
9 // application specific logging, throwing an error, or other logic here
10})
11
12describe('Registry key state resources', function () {
13 this.timeout(process.env.TIMEOUT || 5000)
14
15 let tymlyService
16 let statebox
17 let registry
18
19 describe('setup', () => {
20 it('boot Tymly', async () => {
21 const tymlyServices = await tymly.boot(
22 {
23 blueprintPaths: [
24 path.resolve(__dirname, './fixtures/blueprints/registry-blueprint')
25 ],
26 pluginPaths: [
27 require.resolve('@wmfs/tymly-test-helpers/plugins/allow-everything-rbac-plugin')
28 ],
29 config: {
30 caches: {
31 registryKeys: {}
32 }
33 }
34 }
35 )
36
37 tymlyService = tymlyServices.tymly
38 statebox = tymlyServices.statebox
39 registry = tymlyServices.registry
40 })
41 })
42
43 describe('state resources configured to a specific key', () => {
44 const regKeyName = 'tymlyTest_anotherTestKey'
45 const expectedRegValue = 15
46 const newValue = 2
47 const setTestKey = 'tymlyTest_setTestKey_1_0'
48 const getTestKey = 'tymlyTest_getTestKey_1_0'
49 const clearTestKey = 'tymlyTest_clearTestKey_1_0'
50 const getFalseTestKey = 'tymlyTest_getFalseTestKey_1_0'
51
52 it('check the value via the registry', () => {
53 expect(registry.get(regKeyName)).to.eql(expectedRegValue)
54 })
55
56 it('check the value via the getTestKey state machine', async () => {
57 const execDesc = await statebox.startExecution(
58 { },
59 getTestKey,
60 { sendResponse: 'COMPLETE' }
61 )
62 expect(execDesc.ctx.result).to.eql(expectedRegValue)
63 })
64
65 it('update using the setTestKey state machine', async () => {
66 await statebox.startExecution(
67 { value: newValue },
68 setTestKey,
69 { sendResponse: 'COMPLETE' }
70 )
71 })
72
73 it('verify the registry value has changed', () => {
74 expect(registry.get(regKeyName)).to.eql(newValue)
75 })
76
77 it('verify the change via the getRegistryKey state machine', async () => {
78 const execDesc = await statebox.startExecution(
79 { },
80 getTestKey,
81 { sendResponse: 'COMPLETE' }
82 )
83 expect(execDesc.ctx.result).to.eql(newValue)
84 })
85
86 it('clear the registry key', async () => {
87 await statebox.startExecution(
88 { },
89 clearTestKey,
90 {
91 sendResponse: 'COMPLETE'
92 }
93 )
94 })
95
96 it('verify the registry value has gone', () => {
97 expect(registry.has(regKeyName)).to.eql(false)
98 })
99
100 it('try to get registry value with a false key, expect default value', async () => {
101 const execDesc = await statebox.startExecution(
102 { defaultValue: 123 },
103 getFalseTestKey,
104 { sendResponse: 'COMPLETE' }
105 )
106 expect(execDesc.ctx.result).to.eql(123)
107 })
108 })
109
110 describe('state resources using only event input', () => {
111 const regKeyName = 'tymlyTest_testKey'
112 const expectedRegValue = 15
113 const setRegKeyStateMachine = 'tymlyTest_setAnyRegistryKey_1_0'
114 const getRegKeyStateMachine = 'tymlyTest_getAnyRegistryKey_1_0'
115 const clearRegKeyStateMachine = 'tymlyTest_clearAnyRegistryKey_1_0'
116
117 it('check the value via the registry', () => {
118 expect(registry.get(regKeyName)).to.eql(expectedRegValue)
119 })
120
121 it('check the value via the getAnyRegistryKey state machine', async () => {
122 const execDesc = await statebox.startExecution(
123 {
124 key: regKeyName
125 },
126 getRegKeyStateMachine,
127 {
128 sendResponse: 'COMPLETE'
129 }
130 )
131 expect(execDesc.ctx.registryValue).to.eql(expectedRegValue)
132 })
133
134 it('update using the setAnyRegistryKey state machine', async () => {
135 await statebox.startExecution(
136 {
137 key: regKeyName,
138 value: 2
139 },
140 setRegKeyStateMachine,
141 {
142 sendResponse: 'COMPLETE'
143 }
144 )
145 })
146
147 it('verify the registry value has changed', () => {
148 expect(registry.get(regKeyName)).to.eql(2)
149 })
150
151 it('verify the change via the getRegistryKey state machine', async () => {
152 const execDesc = await statebox.startExecution(
153 {
154 key: regKeyName
155 },
156 getRegKeyStateMachine,
157 {
158 sendResponse: 'COMPLETE'
159 }
160 )
161 expect(execDesc.ctx.registryValue).to.eql(2)
162 })
163
164 it('clear the registry key', async () => {
165 await statebox.startExecution(
166 {
167 key: regKeyName
168 },
169 clearRegKeyStateMachine,
170 {
171 sendResponse: 'COMPLETE'
172 }
173 )
174 })
175
176 it('verify the registry value has gone', () => {
177 expect(registry.has(regKeyName)).to.eql(false)
178 })
179
180 it('fetch an unknown registry key', async () => {
181 const execDesc = await statebox.startExecution(
182 {
183 key: 'i_do_not_exist'
184 },
185 getRegKeyStateMachine,
186 {
187 sendResponse: 'COMPLETE'
188 }
189 )
190 expect(execDesc.status).to.eql('FAILED')
191 })
192
193 it('fetch an unknown registry key, with default', async () => {
194 const testDefault = 'a lovely default value'
195 const execDesc = await statebox.startExecution(
196 {
197 key: 'i_do_not_exist',
198 defaultValue: testDefault
199 },
200 getRegKeyStateMachine,
201 {
202 sendResponse: 'COMPLETE'
203 }
204 )
205 expect(execDesc.ctx.registryValue).to.eql(testDefault)
206 })
207 })
208
209 describe('shutdown', () => {
210 it('shutdown Tymly', async () => {
211 await tymlyService.shutdown()
212 })
213 })
214})