This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsm.test.ts
248 lines (209 loc) · 7.6 KB
/
fsm.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**
* Copyright (C) 2019, Dmitriy Pleshevskiy <[email protected]>
*
* it-fsm is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* it-fsm is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with it-fsm. If not, see <https://www.gnu.org/licenses/>.
*/
import {
assertEquals,
assertRejects,
assertThrows,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import * as fsm from "./fsm.ts";
enum ProjectStatus {
Pending = "pending",
Active = "active",
Completed = "completed",
Archived = "archive",
}
Deno.test("should add states separately in builder", function () {
const smb = new fsm.StateMachineBuilder()
.withState(ProjectStatus.Pending)
.withState(ProjectStatus.Active)
.withState(ProjectStatus.Completed)
.withState(ProjectStatus.Archived);
const states = smb[fsm._states];
assertEquals(states.size, 4);
assertEquals(Array.from(states.keys()), [
ProjectStatus.Pending,
ProjectStatus.Active,
ProjectStatus.Completed,
ProjectStatus.Archived,
]);
});
Deno.test("should bulk add states in builder", function () {
const stateNames = Object.values(ProjectStatus);
const smb = new fsm.StateMachineBuilder()
.withStates(stateNames);
const states = smb[fsm._states];
assertEquals(states.size, 4);
assertEquals(Array.from(states.keys()), stateNames);
});
Deno.test("should build without transitions", function () {
const sm = new fsm.StateMachineBuilder()
.withState(ProjectStatus.Pending)
.build(ProjectStatus.Pending);
assertEquals(sm.allowedTransitionStates(), []);
});
Deno.test("should build base state machine", function () {
const sm = new fsm.StateMachineBuilder()
.withStates(Object.values(ProjectStatus))
.withTransitions([
[ProjectStatus.Pending, [ProjectStatus.Active, ProjectStatus.Archived]],
])
.build(ProjectStatus.Pending);
const [, active, , archived] = sm[fsm._states];
assertEquals(sm.allowedTransitionStates(), [active, archived]);
});
Deno.test("should change state", async function () {
const sm = new fsm.StateMachineBuilder()
.withStates(Object.values(ProjectStatus))
.withTransitions([
[ProjectStatus.Pending, [ProjectStatus.Active, ProjectStatus.Archived]],
[ProjectStatus.Active, [ProjectStatus.Completed]],
])
.build(ProjectStatus.Pending);
const [, active, completed, archived] = sm[fsm._states];
assertEquals(sm.allowedTransitionStates(), [active, archived]);
await sm.tryChangeState(ProjectStatus.Active, null);
assertEquals(sm.allowedTransitionStates(), [completed]);
await sm.tryChangeState(ProjectStatus.Completed, null);
assertEquals(sm.allowedTransitionStates(), []);
});
Deno.test("should trigger state actions", async function () {
const triggeredTimes = {
beforeExit: 0,
onEntry: 0,
};
const sm = new fsm.StateMachineBuilder()
.withStates(
Object.values(ProjectStatus),
{
onEntry() {
triggeredTimes.onEntry += 1;
},
beforeExit() {
triggeredTimes.beforeExit += 1;
return true;
},
},
)
.withTransitions([
[ProjectStatus.Pending, [ProjectStatus.Active, ProjectStatus.Archived]],
[ProjectStatus.Active, [ProjectStatus.Completed]],
])
.build(ProjectStatus.Pending);
const [, active, completed, archived] = sm[fsm._states];
assertEquals(triggeredTimes, { beforeExit: 0, onEntry: 0 });
assertEquals(sm.allowedTransitionStates(), [active, archived]);
assertEquals(
sm.allowedTransitionStateNames(),
[ProjectStatus.Active, ProjectStatus.Archived],
);
await sm.tryChangeState(ProjectStatus.Active, null);
assertEquals(triggeredTimes, { beforeExit: 1, onEntry: 1 });
assertEquals(sm.allowedTransitionStates(), [completed]);
assertEquals(sm.allowedTransitionStateNames(), [ProjectStatus.Completed]);
await sm.tryChangeState(ProjectStatus.Completed, null);
assertEquals(triggeredTimes, { beforeExit: 2, onEntry: 2 });
assertEquals(sm.allowedTransitionStates(), []);
assertEquals(sm.allowedTransitionStateNames(), []);
});
Deno.test("should stringify state", function () {
const pending = new fsm.State(ProjectStatus.Pending);
const active = new fsm.State(ProjectStatus.Active);
assertEquals(pending.toString(), ProjectStatus.Pending);
assertEquals(
[pending, active].join(),
`${ProjectStatus.Pending},${ProjectStatus.Active}`,
);
assertEquals(JSON.stringify({ pending }), '{"pending":"pending"}');
});
Deno.test("should throw type error if state doesn't exist", () => {
assertThrows(
() => new fsm.StateMachineBuilder().build(ProjectStatus.Pending),
TypeError,
"an instance of State class is expected",
);
});
Deno.test("should throw error if transition to the state doesn't exist", () => {
const sm = new fsm.StateMachineBuilder()
.withStates(Object.values(ProjectStatus))
.build(ProjectStatus.Pending);
assertRejects(
() => sm.tryChangeState(ProjectStatus.Active, null),
fsm.FsmError,
`cannot change state from "${ProjectStatus.Pending}" to "${ProjectStatus.Active}"`,
);
});
Deno.test("should return null if transition to the state doesn't exist", async () => {
const sm = new fsm.StateMachineBuilder()
.withStates(Object.values(ProjectStatus))
.build(ProjectStatus.Pending);
assertEquals(
await sm.maybeChangeState(ProjectStatus.Active, null),
null,
);
});
Deno.test("should throw error if beforeExit action returns false", () => {
const sm = new fsm.StateMachineBuilder()
.withStates(
Object.values(ProjectStatus),
{ beforeExit: () => false },
)
.withTransitions([
[ProjectStatus.Pending, [ProjectStatus.Active]],
])
.build(ProjectStatus.Pending);
assertRejects(
() => sm.tryChangeState(ProjectStatus.Active, null),
fsm.FsmError,
`cannot change state from "${ProjectStatus.Pending}" to "${ProjectStatus.Active}"`,
);
});
Deno.test("should reuse one builder for many entities", () => {
const statuses = Object.values(ProjectStatus);
const transitions: Array<[string, Array<string>]> = [
[ProjectStatus.Pending, [ProjectStatus.Active, ProjectStatus.Archived]],
[ProjectStatus.Active, [ProjectStatus.Completed]],
];
const smb = new fsm.StateMachineBuilder()
.withStates(statuses)
.withTransitions(transitions);
function expectedAllowedStates(status: ProjectStatus) {
return transitions.find(([s]) => s === status)?.[1] || [];
}
for (const status of statuses) {
const sm = smb.build(status);
assertEquals(
sm.allowedTransitionStates().map(String),
expectedAllowedStates(status),
);
}
});
Deno.test("should trigger builded transition actions", async () => {
const states = ["locked", "unlocked"] as const;
const [locked, unlocked] = states;
const sm = new fsm.StateMachineBuilder()
.withStates([locked, unlocked])
.withTransitions([
[locked, { coin: unlocked }],
[unlocked, { push: locked }],
])
.build(locked);
const [lockedState, unlockedState] = sm[fsm._states];
assertEquals(await sm.trigger("coin", {}), unlockedState);
assertEquals(await sm.trigger("coin", {}), unlockedState);
assertEquals(await sm.trigger("push", {}), lockedState);
assertEquals(await sm.trigger("push", {}), lockedState);
});