-
Notifications
You must be signed in to change notification settings - Fork 0
/
frontend.spec.js
419 lines (309 loc) · 15.5 KB
/
frontend.spec.js
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
const { expect } = require("chai")
const puppeteer = require("puppeteer")
const dappeteer = require("dappeteer")
const fs = require('fs')
const path = require('path')
const Web3 = require("web3")
const HDWalletProvider = require("truffle-hdwallet-provider")
var browser, metamask, web3
var player2
var DipDappDoe
const DAPP_URL = "http://localhost:1234"
const DEFAULT_METAMASK_OPTIONS = { gasLimit: 6654755 }
describe("DipDappDoe frontend", async function () {
// INIT
before(async function () {
this.timeout(1000 * 35)
// Browser init
browser = await dappeteer.launch(puppeteer)
metamask = await dappeteer.getMetamask(browser)
// import MetaMask account, same as ganache
const mnemonic = fs.readFileSync(path.resolve(__dirname, "..", "dev", "mnemonic.txt")).toString()
await metamask.importAccount(mnemonic)
await metamask.switchNetwork('localhost 8545') // ganache
// Local init
let provider = new HDWalletProvider(mnemonic, "http://localhost:8545", 0)
player1 = provider.addresses[0]
provider = new HDWalletProvider(mnemonic, "http://localhost:8545", 1)
player2 = provider.addresses[0]
web3 = new Web3(provider)
const testEnv = fs.readFileSync(path.resolve(__dirname, "..", ".env.test.local")).toString()
const address = testEnv.match(/CONTRACT_ADDRESS=([^\n]+)/)[1]
const dipDappDoeAbi = fs.readFileSync(path.resolve(__dirname, "..", "src", "contracts", "dip-dapp-doe.json")).toString()
DipDappDoe = new web3.eth.Contract(JSON.parse(dipDappDoeAbi), address)
})
// CLEAN UP
after(() => {
if (web3 && web3.currentProvider) {
if (web3.currentProvider.disconnect) {
web3.currentProvider.disconnect()
}
else if (web3.currentProvider.connection) {
web3.currentProvider.connection.close()
}
}
if (browser) {
return browser.close()
}
})
// TEST CASES
it("should create and play a game ending in draw", async function () {
this.timeout(1000 * 60)
// open localhost
const page = await browser.newPage()
await page.goto(DAPP_URL)
// The game list should be empty
await page.waitForSelector('#main #list')
let handle = await page.$$('#list > *')
expect(handle.length).to.equal(1)
handle = await page.$('#list')
expect(await handle.$eval('p', node => node.innerText)).to.equal("There are no open games at the moment. You can create one!")
// CREATE A GAME
await page.type('input[name="nick"]', "Jack")
await page.type('input[name="number"]', "234")
await page.type('input[name="salt"]', "My salt here")
await page.type('input[name="value"]', "1")
await page.click('#start')
await delay(200)
await metamask.confirmTransaction(DEFAULT_METAMASK_OPTIONS)
// wait for tx
await page.bringToFront()
await page.waitFor(
() => document.querySelector('#start') == null,
{ timeout: 30 * 1000 }
)
await page.waitForSelector('#game')
await delay(1000)
// NOTIFICATION
await page.waitForSelector('.ant-notification-notice-with-icon')
handle = await page.$('.ant-notification-notice-with-icon')
expect(await handle.$eval('.ant-notification-notice-description', node => node.innerText)).to.equal("Your game has been created. Waiting for another user to accept it.")
// ACCEPT THE GAME (player 2)
let hash = await page.evaluate(() => {
return document.location.hash
})
expect(hash).to.match(/^#\/games\/[0-9]+$/)
let gameIdx = hash.match(/#\/games\/([0-9]+)/)
gameIdx = gameIdx[1]
expect(gameIdx).to.equal("0")
let tx = await DipDappDoe.methods.acceptGame(Number(gameIdx), 78, "James").send({ from: player2, value: web3.utils.toWei("1", "ether") })
expect(tx.events.GameAccepted.returnValues.gameIdx).to.equal(gameIdx)
// NOTIFICATION
await delay(2000)
await page.waitForSelector('.ant-notification-notice-with-icon')
handle = await page.$$('.ant-notification-notice-with-icon')
let value = await handle[1].$eval(".ant-notification-notice-description", node => node.innerText)
expect(value).to.equal("James has accepted the game!")
// SHOULD BE CONFIRMING THE GAME
await delay(1500)
await page.waitForSelector('#game .loading-spinner')
value = await page.$eval("#game .loading-spinner", node => node.innerText)
expect(value).to.equal("Waiting ")
await delay(200)
await metamask.confirmTransaction(DEFAULT_METAMASK_OPTIONS)
// wait for tx
await page.bringToFront()
await delay(1000)
// SHOULD BE CONFIRMED
await page.waitForSelector('.ant-notification-notice-description')
value = await page.$eval(".ant-notification-notice-description", node => node.innerText)
expect(value).to.equal("The game is on. Good luck!")
await page.waitForSelector('#status')
value = await page.$eval("#status", node => node.innerText)
expect(value).to.equal("It's your turn")
value = await page.$eval("#timer", node => node.innerText)
expect(value).to.match(/Remaining time: [0-9]+ minutes before James can claim the game/)
value = await page.$eval("#bet", node => node.innerText)
expect(value).to.equal("Game bet: 1 Ξ")
// CHECK STATUS
expect(await page.$eval("#cell-0", node => node.className)).to.equal("cell")
expect(await page.$eval("#cell-1", node => node.className)).to.equal("cell")
expect(await page.$eval("#cell-2", node => node.className)).to.equal("cell")
expect(await page.$eval("#cell-3", node => node.className)).to.equal("cell")
expect(await page.$eval("#cell-4", node => node.className)).to.equal("cell")
expect(await page.$eval("#cell-5", node => node.className)).to.equal("cell")
expect(await page.$eval("#cell-6", node => node.className)).to.equal("cell")
expect(await page.$eval("#cell-7", node => node.className)).to.equal("cell")
expect(await page.$eval("#cell-8", node => node.className)).to.equal("cell")
// MARKING CELLS
await markBrowserPosition(0, "James", page, metamask)
await markPosition(gameIdx, 2, "James", page)
await markBrowserPosition(4, "James", page, metamask)
await markPosition(gameIdx, 8, "James", page)
await markBrowserPosition(5, "James", page, metamask)
await markPosition(gameIdx, 3, "James", page)
await markBrowserPosition(6, "James", page, metamask)
// THE OPPONENT MARKS HIS LAST POSITION
tx = await DipDappDoe.methods.markPosition(Number(gameIdx), 1).send({ from: player2 })
expect(tx.events.PositionMarked.returnValues.gameIdx).to.equal(gameIdx)
await delay(200)
await page.waitForSelector('#game .loading-spinner')
value = await page.$eval("#game .loading-spinner", node => node.innerText)
expect(value).to.equal("Waiting ")
// CONFIRM THE AUTO ENDING MOVEMENT
await metamask.confirmTransaction(DEFAULT_METAMASK_OPTIONS)
await page.bringToFront()
await page.waitForSelector('#status')
value = await page.$eval("#status", node => node.innerText)
expect(value).to.equal("The game ended in draw")
await page.waitForSelector('#withdraw')
value = await page.$eval("#withdraw", node => node.innerText)
expect(value).to.equal("Withdraw 1 Ξ")
// WITHDRAW
await page.click("#withdraw")
await delay(200)
await metamask.confirmTransaction(DEFAULT_METAMASK_OPTIONS)
await page.bringToFront()
await delay(3000)
await page.waitForSelector('.ant-notification-notice-description')
value = await page.$eval(".ant-notification-notice-description", node => node.innerText)
expect(value).to.equal("The money has been withdrawn")
await delay(200)
await page.waitFor(
() => document.querySelector('#withdraw') == null,
{ timeout: 1000 * 30}
)
// GO BACK
await page.click("#back")
await delay(200)
hash = await page.evaluate(() => {
return document.location.hash
})
expect(hash).to.equal("#/")
await page.close()
})
it("should play a game created and won by the opponent", async function () {
this.timeout(1000 * 60)
// open localhost
const page = await browser.newPage()
await page.goto(DAPP_URL)
// The game list should be empty
await page.waitForSelector('#main #list')
let handle = await page.$$('#list > *')
expect(handle.length).to.equal(1)
handle = await page.$$('.open-game-row')
expect(handle.length).to.equal(0)
handle = await page.$('#list')
expect(await handle.$eval('p', node => node.innerText)).to.equal("There are no open games at the moment. You can create one!")
// CREATE A GAME
let hash = await DipDappDoe.methods.saltedHash(100, "player 2 salt").call()
let tx = await DipDappDoe.methods.createGame(hash, "Jim").send({ from: player2, value: web3.utils.toWei("0.1", "ether") })
expect(tx.events.GameCreated.returnValues.gameIdx).to.be.ok
const gameIdx = tx.events.GameCreated.returnValues.gameIdx
await page.waitForSelector('#main .open-game-row')
let value = await page.$eval('.open-game-row .open-game-row-text', node => node.innerText)
expect(value).to.equal("Jim (0.1 Ξ)")
await page.click('#main .open-game-row-accept')
// ACCEPT THE GAME
await page.waitForSelector(".ant-form-item-children input#nick")
await page.type(".ant-form-item-children input#nick", "John")
await page.type(".ant-form-item-children input#number", "10")
await page.click(".ant-modal-footer .ant-btn.ant-btn-primary")
await delay(500)
await metamask.confirmTransaction(DEFAULT_METAMASK_OPTIONS)
// wait for tx
await page.bringToFront()
// await page.waitFor(
// () => document.querySelector('#start') == null
// )
await page.waitForSelector('#game')
await delay(1000)
// NOTIFICATION
await page.waitForSelector('.ant-notification-notice-with-icon')
handle = await page.$('.ant-notification-notice-with-icon')
expect(await handle.$eval('.ant-notification-notice-description', node => node.innerText)).to.equal("You have accepted the game. Waiting for creator to confirm.")
// CONFIRM THE GAME
tx = await DipDappDoe.methods.confirmGame(gameIdx, 100, "player 2 salt").send({ from: player2 })
expect(tx.events.GameStarted.returnValues.gameIdx).to.be.ok
// SHOULD BE CONFIRMED
await delay(4000)
await page.waitForSelector('.ant-notification-notice-description')
value = await page.$eval(".ant-notification-notice-description", node => node.innerText)
expect(value).to.equal("Jim has confirmed the game!")
await page.waitForSelector('#status')
value = await page.$eval("#status", node => node.innerText)
expect(value).to.equal(`Waiting for Jim`)
value = await page.$eval("#timer", node => node.innerText)
expect(value).to.match(/Remaining time: [0-9]+ minutes before You can claim the game/)
value = await page.$eval("#bet", node => node.innerText)
expect(value).to.match(/Game bet: [0-9\.]+ Ξ/)
// EMPTY BOARD
expect(await page.$eval("#cell-0", node => node.className)).to.equal("cell")
expect(await page.$eval("#cell-1", node => node.className)).to.equal("cell")
expect(await page.$eval("#cell-2", node => node.className)).to.equal("cell")
expect(await page.$eval("#cell-3", node => node.className)).to.equal("cell")
expect(await page.$eval("#cell-4", node => node.className)).to.equal("cell")
expect(await page.$eval("#cell-5", node => node.className)).to.equal("cell")
expect(await page.$eval("#cell-6", node => node.className)).to.equal("cell")
expect(await page.$eval("#cell-7", node => node.className)).to.equal("cell")
expect(await page.$eval("#cell-8", node => node.className)).to.equal("cell")
// MARKING CELLS
await markPosition(gameIdx, 2, "Jim", page, "x")
await markBrowserPosition(0, "Jim", page, metamask, "o")
await markPosition(gameIdx, 4, "Jim", page, "x")
await markBrowserPosition(3, "Jim", page, metamask, "o")
// THE OPPONENT MARKS AND WINS
tx = await DipDappDoe.methods.markPosition(Number(gameIdx), 6).send({ from: player2 })
expect(tx.events.PositionMarked.returnValues.gameIdx).to.equal(gameIdx)
await delay(200)
await page.waitForSelector('#status')
value = await page.$eval("#status", node => node.innerText)
expect(value).to.equal("Jim is the winner of this game")
await page.waitFor(
() => document.querySelector(`#cell-6.cell.cell-x`) != null,
{ timeout: 10 * 1000 },
)
// no money on the game, no withdrawal available
await page.waitFor(
() => document.querySelector('#withdraw') == null,
{ timeout: 1000 * 30}
)
// GO BACK
await page.click("#back")
await delay(200)
hash = await page.evaluate(() => {
return document.location.hash
})
expect(hash).to.equal("#/")
await page.close()
})
})
// HELPERS
const delay = async interval => new Promise(resolve => setTimeout(resolve, interval))
async function markBrowserPosition(cell, opponent, page, metamask, symbol = "x") {
await page.click(`#cell-${cell}`)
await delay(100)
await metamask.confirmTransaction(DEFAULT_METAMASK_OPTIONS)
await page.bringToFront()
await page.waitFor(
(cell, symbol) => document.querySelector(`#cell-${cell}.cell.cell-${symbol}`) != null,
{ timeout: 10 * 1000 },
cell, symbol
)
await delay(100)
await page.waitForSelector('#status')
let value = await page.$eval("#status", node => node.innerText)
expect(value).to.equal(`Waiting for ${opponent}`)
value = await page.$eval("#timer", node => node.innerText)
expect(value).to.match(/Remaining time: [0-9]+ minutes before You can claim the game/)
value = await page.$eval("#bet", node => node.innerText)
expect(value).to.match(/Game bet: [0-9\.]+ Ξ/)
}
async function markPosition(gameIdx, cell, oponnent, page, symbol = "o") {
let tx = await DipDappDoe.methods.markPosition(Number(gameIdx), cell).send({ from: player2 })
expect(tx.events.PositionMarked.returnValues.gameIdx).to.equal(gameIdx)
await delay(200)
await page.waitForSelector('#status')
let value = await page.$eval("#status", node => node.innerText)
expect(value).to.equal("It's your turn")
value = await page.$eval("#timer", node => node.innerText)
expect(value).to.match(new RegExp(`Remaining time: [0-9]+ minutes before ${oponnent} can claim the game`))
value = await page.$eval("#bet", node => node.innerText)
expect(value).to.match(/Game bet: [0-9\.]+ Ξ/)
await page.waitFor(
(cell, symbol) => document.querySelector(`#cell-${cell}.cell.cell-${symbol}`) != null,
{ timeout: 10 * 1000 },
cell, symbol
)
await delay(100)
}