-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping-pong_model_test.ts
192 lines (164 loc) · 5.9 KB
/
ping-pong_model_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
import { Clarinet, Tx, type Chain, Account, types } from 'https://deno.land/x/[email protected]/index.ts'
import { assertEquals } from 'https://deno.land/[email protected]/testing/asserts.ts'
import fc from 'https://cdn.skypack.dev/[email protected]'
import PingPongModel from '../src/models.ts'
type ChainT = { chain: Chain, ctx: fc.Context }
class EndGameCommand implements fc.Command<PingPongModel, ChainT> {
constructor(caller: string, deployer: string) {
this.caller = caller
this.deployer = deployer
}
check(m: Readonly<PingPongModel>): boolean {
return true
}
run(m: Model, { chain, ctx }: ChainT): void {
m.actionCounter++
const block = chain.mineBlock([
Tx.contractCall(
'ping-pong', 'end-game', [], this.caller)
])
ctx.log(`end ${this} ${m.actionCounter}`)
const result = block.receipts[0].result
if (this.caller === this.deployer) {
result
.expectOk()
.expectAscii('Game reset!')
m.startable = true
m.optionHitter = null
m.optionReceiver = null
} else {
result
.expectErr()
.expectAscii('Not the owner')
}
}
toString() {
return `{ End: caller: ${this.caller}, deployer: ${this.deployer}`;
}
}
class NewGameCommand implements fc.Command<PingPongModel, ChainT> {
constructor(player1: string, player2: string, caller: string, deployer: string) {
this.player1 = player1
this.player2 = player2
this.caller = caller
this.deployer = deployer
}
check(m: Readonly<PingPongModel>): boolean {
//always reachable
//Usefulness varies depending on how you want to explore the model.
return true
// this may suffer if the event is not well connected in complex models. Making the event very hard to sample.
// return m.startable
}
run(m: Model, { chain, ctx }: ChainT): void {
m.actionCounter++
const p1 = types.principal(this.player1)
const p2 = types.principal(this.player2)
const block = chain.mineBlock([
Tx.contractCall(
'ping-pong', 'new-game', [p1, p2], this.caller)
])
const result = block.receipts[0].result
if (this.caller === this.deployer) {
if (!m.startable) {
result
.expectErr()
.expectAscii('Game in progress')
}
else {
if (this.player1 === this.player2) {
result
.expectErr()
.expectAscii('self-play')
} else {
result
.expectOk()
.expectAscii('New Game!')
m.startable = false
m.optionHitter = this.player1
m.optionReceiver = this.player2
}
}
} else {
result
.expectErr()
.expectAscii('Not the owner')
}
}
toString() {
return `{ New: player1: ${this.player1}, player2: ${this.player2}, caller: ${this.caller}, deployer: ${this.deployer} }`;
}
}
class PlayGameCommand implements fc.Command<PingPongModel, ChainT> {
constructor(caller: string, deployer: string) {
this.caller = caller
this.deployer = deployer
}
check(m: Readonly<PingPongModel>): boolean {
return !m.startable
}
run(m: Model, { chain, ctx }: ChainT): void {
m.actionCounter++
const block = chain.mineBlock([
Tx.contractCall(
'ping-pong', 'play', [], this.caller)
])
ctx.log(`play ${m.optionHitter} ${m.optionReceiver}`)
const result = block.receipts[0].result
if (m.optionHitter && this.caller === m.optionHitter) {
result
.expectOk()
.expectAscii('Cool!')
const tmp = m.optionHitter
m.optionHitter = m.optionReceiver
m.optionReceiver = tmp
} else {
result
.expectErr()
.expectAscii('Not your turn to move.')
}
}
toString() {
return `{ Play: caller: ${this.caller}, deployer: ${this.deployer} }`;
}
}
Clarinet.test({
name: 'Ping-Pong model: start-ping-end',
async fn(chain: Chain, accounts: Map<string, Account>) {
const deployer = accounts.get('deployer')!.address
const commands = [
fc.constantFrom(...accounts.values())
.map(account =>
new EndGameCommand(account.address, deployer)
),
fc.constantFrom(...accounts.values())
.map(account =>
new PlayGameCommand(account.address, deployer)
),
fc.record(
{
p1: fc.constantFrom(...accounts.values()).map((x) => x.address),
p2: fc.constantFrom(...accounts.values()).map((x) => x.address),
c: fc.constantFrom(...accounts.values()).map((x) => x.address),
d: fc.constantFrom(deployer)
}
).noShrink()
.map(({ p1, p2, c, d }) => new NewGameCommand(p1, p2, c, d)
),
]
const model: PingPongModel = {
actionCounter: 0,
startable: true,
optionHitter: null,
optionReceiver: null
};
fc.assert(fc.property(
fc.commands(commands, { size: '+1' }),
fc.context(),
(commands, ctx) => {
const initialState = () => ({ model: model, real: { chain: chain, ctx: ctx } });
fc.modelRun(initialState, commands);
})
, { numRuns: 100 }); // Run `numRuns` times.
}
});