-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe_test.ts
67 lines (57 loc) · 1.83 KB
/
tictactoe_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
import { behavioralThreadSystem } from "./bthreads.ts"
import { createTicTacToeGame, GameEvent } from "./tictactoe.ts"
import { assertEquals } from "https://deno.land/[email protected]/assert/mod.ts"
import { run } from "effection"
import { Sync } from "./bthreads.ts"
const TEST_OPTIONS = {
timeout: 1000,
}
Deno.test({
name: "basic tic tac toe gameplay",
...TEST_OPTIONS,
fn: async () => {
const events: GameEvent[] = []
await run(() =>
behavioralThreadSystem<GameEvent>(function* (thread, sync) {
yield* createTicTacToeGame(thread, sync)
// Simulate a game where X wins
yield* thread("TestDriver", function* (): Generator<
Sync<GameEvent>,
void,
GameEvent
> {
// X plays center
yield sync({
post: [{ type: "CLICK", position: { row: 1, col: 1 } }],
})
// O should play a corner
const move1 = yield sync({
wait: (e: GameEvent) => e.type === "MOVE" && e.move.type === "O",
})
events.push(move1)
// X plays top-middle
yield sync({
post: [{ type: "CLICK", position: { row: 0, col: 1 } }],
})
// Wait for O's move
const move2 = yield sync({
wait: (e: GameEvent) => e.type === "MOVE" && e.move.type === "O",
})
events.push(move2)
// X plays bottom-middle to win
yield sync({
post: [{ type: "CLICK", position: { row: 2, col: 1 } }],
})
// Wait for win detection
const result = yield sync({
wait: (e: GameEvent) => e.type === "X_WIN",
})
events.push(result)
})
})
)
// Verify game progression
assertEquals(events.length, 3)
assertEquals(events[2], { type: "X_WIN" })
},
})