-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmru.4-teams.test.ts
367 lines (312 loc) · 10.2 KB
/
mru.4-teams.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import { assert, expect } from "chai";
import {
ActionConfirmationStatus,
MicroRollup,
StackrConfig,
} from "@stackr/sdk";
import { StateMachine } from "@stackr/sdk/machine";
import { STATE_MACHINES } from "../src/stackr/machines";
import { League, LeagueState } from "../src/stackr/state";
import { transitions } from "../src/stackr/transitions";
import { signByOperator } from "../src/utils";
import { stackrConfig } from "../stackr.config";
import genesisState4 from "./fixtures/genesis-state.4-teams.json";
const testConfig = {
...stackrConfig,
isSandbox: true,
logLevel: "error",
sequencer: {
...stackrConfig.sequencer,
blockTime: 10,
},
} as StackrConfig;
describe("League with 4 teams", async () => {
const initialState = genesisState4.state;
let machine: StateMachine<LeagueState, LeagueState>;
const leagueMachine = new StateMachine({
id: STATE_MACHINES.LEAGUE,
stateClass: League,
initialState,
on: transitions,
});
// setup MicroRollup
const mru = await MicroRollup({
config: testConfig,
stateMachines: [leagueMachine],
});
await mru.init();
const _machine = mru.stateMachines.get<typeof leagueMachine>(
STATE_MACHINES.LEAGUE
);
if (!_machine) {
throw new Error("League machine not initialized yet");
}
machine = _machine;
const performAction = async (name: string, _inputs: any) => {
const inputs = {
..._inputs,
timestamp: Date.now(),
};
const domain = mru.config.domain;
const types = mru.getStfSchemaMap()[name];
const { msgSender, signature } = await signByOperator(domain, types, {
name,
inputs,
});
const actionParams = {
name,
inputs,
msgSender,
signature,
};
const ack = await mru.submitAction(actionParams);
const action = await ack.waitFor(ActionConfirmationStatus.C1);
return action;
};
it("has 4 teams", async () => {
expect(machine.state.teams.length).to.equal(4);
});
it("should be able to start a tournament", async () => {
await performAction("startTournament", {});
// should have 2 matches in the first round
expect(machine.state.meta.round).to.equal(1);
expect(machine.state.matches.length).to.equal(2);
});
it("should be able to complete round 1", async () => {
for (const match of machine.state.matches) {
await performAction("startMatch", {
matchId: match.id,
});
const teamIds = Object.keys(match.scores).map((k) => parseInt(k));
const team1Id = teamIds[0];
const teamOnePlayers = machine.state.players.filter(
(p) => p.teamId === team1Id
);
if (!teamOnePlayers.length) {
throw new Error("Players not found");
}
const team2Id = teamIds[1];
const teamTwoPlayers = machine.state.players.filter(
(p) => p.teamId === team2Id
);
if (!teamTwoPlayers.length) {
throw new Error("Players not found");
}
// first team score a goal
await performAction("logGoal", {
matchId: match.id,
playerId: teamOnePlayers[0].id,
});
// second team score a goal
await performAction("logGoal", {
matchId: match.id,
playerId: teamTwoPlayers[0].id,
});
// first team score another goal
await performAction("logGoal", {
matchId: match.id,
playerId: teamOnePlayers[1].id,
});
// second team saves a goal
await performAction("logBlock", {
matchId: match.id,
playerId: teamTwoPlayers[1].id,
});
// first team does a foul
await performAction("logFoul", {
matchId: match.id,
playerId: teamOnePlayers[1].id,
});
// end the game
await performAction("endMatch", {
matchId: match.id,
});
// check winner
const _match = machine.state.matches.find((m) => m.id === match.id);
expect(_match?.scores[team1Id]).to.equal(2);
expect(_match?.scores[team2Id]).to.equal(1);
expect(_match?.scores[team1Id]).to.greaterThan(_match?.scores[team2Id]!);
}
// should have 3 total match at round 2
expect(machine.state.meta.round).to.equal(2);
expect(machine.state.matches.length).to.equal(3);
// should have 1 new (incomplete) match at round 2
const incompleteMatches = machine.state.matches.filter((m) => !m.endTime);
expect(incompleteMatches.length).to.equal(1);
});
it("should not be able to score goal before a match starts", async () => {
const match = machine.state.matches.filter((m) => !m.endTime)[0];
const teamIds = Object.keys(match.scores).map((k) => parseInt(k));
const team1Id = teamIds[0];
const teamOnePlayers = machine.state.players.filter(
(p) => p.teamId === team1Id
);
if (!teamOnePlayers.length) {
throw new Error("Players not found");
}
const { logs, errors } = await performAction("logGoal", {
matchId: match.id,
playerId: teamOnePlayers[0].id,
});
if (!errors) {
throw new Error("Error not found");
}
// check error for "MATCH_NOT_STARTED"
assert.typeOf(errors, "array");
expect(errors.length).to.equal(1);
expect(errors[0].message).to.equal(
"Transition logGoal failed to execute: MATCH_NOT_STARTED"
);
});
it("should be able complete a round 2 (final)", async () => {
for (const match of machine.state.matches) {
if (match.endTime) {
continue;
}
const matchId = match.id;
const matchIdx = machine.state.matches.findIndex((m) => m.id === matchId);
await performAction("startMatch", {
matchId,
});
const teamIds = Object.keys(match.scores).map((k) => parseInt(k));
const [team1Id, team2Id] = teamIds;
const teamOnePlayers = machine.state.players.filter(
(p) => p.teamId === team1Id
);
if (!teamOnePlayers.length) {
throw new Error("Players not found");
}
const teamTwoPlayers = machine.state.players.filter(
(p) => p.teamId === team2Id
);
if (!teamTwoPlayers.length) {
throw new Error("Players not found");
}
// first team score a goal
await performAction("logGoal", {
matchId,
playerId: teamOnePlayers[0].id,
});
// first team score another goal
await performAction("logGoal", {
matchId,
playerId: teamOnePlayers[1].id,
});
// removing a goal
await performAction("removeGoal", {
matchId,
playerId: teamOnePlayers[1].id,
});
// try scoring a goal from a player that is not playing
const playersNotPlaying = machine.state.players.filter(
(p) => p.teamId !== team1Id && p.teamId !== team2Id
);
const { logs: logs1, errors: errors1 } = await performAction("logGoal", {
matchId: match.id,
playerId: playersNotPlaying[0].id,
});
if (!errors1) {
throw new Error("Error not found");
}
// check error for "PLAYER_NOT_FOUND"
assert.typeOf(errors1, "array");
expect(errors1.length).to.equal(1);
expect(errors1[0].message).to.equal(
"Transition logGoal failed to execute: INVALID_TEAM"
);
// remove a goal when not scored
const { logs: logs2, errors: errors2 } = await performAction(
"removeGoal",
{
matchId: match.id,
playerId: teamTwoPlayers[0].id,
}
);
if (!errors2) {
throw new Error("Error not found");
}
// check error for "PLAYER_NOT_FOUND"
assert.typeOf(errors2, "array");
expect(errors2?.length).to.equal(1);
expect(errors2[0].message).to.equal(
"Transition removeGoal failed to execute: NO_GOALS_TO_REMOVE"
);
// second team score a goal
await performAction("logGoal", {
matchId,
playerId: teamTwoPlayers[0].id,
});
// have equal scores
expect(machine.state.matches[matchIdx].scores[team1Id]).to.equal(
machine.state.matches[matchIdx].scores[team2Id]
);
// start a penalty shootout
await performAction("penaltyShootout", {
matchId,
});
expect(machine.state.matches[matchIdx].penaltyStartTime).to.not.equal(0);
// first team penalty hit
await performAction("logPenaltyHit", {
matchId,
playerId: teamOnePlayers[0].id,
});
// second team penalty hit
await performAction("logPenaltyHit", {
matchId,
playerId: teamTwoPlayers[1].id,
});
// first team penalty miss
await performAction("logPenaltyMiss", {
matchId,
playerId: teamOnePlayers[1].id,
});
// second team penalty hit
await performAction("logPenaltyHit", {
matchId,
playerId: teamTwoPlayers[2].id,
});
// end the game
await performAction("endMatch", {
matchId,
});
const incompleteMatches = machine.state.matches.filter((m) => !m.endTime);
expect(incompleteMatches.length).to.equal(0);
const lastMatch = machine.state.matches.at(-1);
if (!lastMatch) {
throw new Error("Match not found");
}
expect(lastMatch.endTime).to.not.equal(0);
}
});
it("should not be able to score goal after a match ends", async () => {
const match = machine.state.matches.at(-1);
if (!match) {
throw new Error("Match not found");
}
const teamIds = Object.keys(match.scores).map((k) => parseInt(k));
const team1Id = teamIds[0];
const teamOnePlayers = machine.state.players.filter(
(p) => p.teamId === team1Id
);
if (!teamOnePlayers.length) {
throw new Error("Players not found");
}
const { logs, errors } = await performAction("logGoal", {
matchId: match.id,
playerId: teamOnePlayers[0].id,
});
if (!errors) {
throw new Error("Error not found");
}
// check error for "TOURNAMENT_ENDED"
assert.typeOf(errors, "array");
expect(errors.length).to.equal(1);
expect(errors[0].message).to.equal(
"Transition logGoal failed to execute: TOURNAMENT_ENDED"
);
});
it("should end the tournament", async () => {
expect(machine.state.meta.endTime).to.not.equal(0);
expect(machine.state.meta.winnerTeamId).to.equal(3);
});
});