File size: 4,414 Bytes
ef987bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const fs = require('fs');
const path = require('path');
const {pathToFileURL} = require('url');
const {chromium} = require('playwright');
const assert = (value, message) => { if (!value) throw Error(message); };
const entry = path.join(process.cwd(), 'index.html');

(async () => {
  const browser = await chromium.launch({headless: true, args: ['--no-sandbox']});
  try {
    const page = await browser.newPage();
    await page.goto(pathToFileURL(entry).href);
    await page.evaluate(() => { gameTest.reset(); gameTest.setRolls([1,2,3,4,6,2,6,3]); });
    const state = () => page.evaluate(() => gameTest.state());
    const roll = () => page.evaluate(() => { gameTest.roll(); return gameTest.state(); });
    let current = await state();
    assert(current.players.length === 4 && current.currentPlayer === 0, 'four-player initial turn wrong');
    assert(new Set(current.players.map(player => player.start)).size === 4, 'players need distinct starting squares');
    assert(current.players.every(player => player.pos === -1 && player.steps === 0), 'all players must begin in base');
    for (let i = 0; i < 4; i++) {
      const before = await state();
      const after = await roll();
      assert(after.lastRoll === i + 1, 'reported die differs from injected roll');
      assert(after.currentPlayer === (before.currentPlayer + 1) % 4, 'non-six did not rotate turn');
      assert(after.players.every(player => player.pos === -1 && player.steps === 0), 'non-six moved a base piece or advanced its steps');
    }
    current = await roll();
    assert(current.currentPlayer === 0 && current.players[0].pos === current.players[0].start && current.players[0].steps === 0, 'six must enter board without path progress and retain turn');
    current = await roll();
    assert(current.currentPlayer === 1 && current.players[0].pos === (current.players[0].start + 2) % current.trackLength && current.players[0].steps === 2, 'board movement must equal die and rotate');
    current = await roll();
    assert(current.currentPlayer === 1 && current.players[1].pos === current.players[1].start && current.players[1].steps === 0, 'second player six did not leave base');
    current = await roll();
    assert(current.currentPlayer === 2 && current.players[1].pos === (current.players[1].start + 3) % current.trackLength && current.players[1].steps === 3, 'second player movement/turn wrong');
    await page.evaluate(() => { gameTest.reset(); gameTest.setRolls([6]); });
    const rolls = page.getByRole('button', {name: /roll|dice|\bdie\b|掷骰|擲骰/i});
    const buttons = rolls.or(page.getByRole('button')).or(page.getByText(/roll|dice|\bdie\b|掷骰|擲骰/i));
    let rolledWithControl = false;
    for (const button of (await buttons.all()).slice(0, 20)) {
      await page.evaluate(() => { gameTest.reset(); gameTest.setRolls([6]); });
      if (!await button.isVisible() || !await button.isEnabled()) continue;
      try {
        await button.click({timeout: 1000});
      } catch {
        continue;
      }
      const clicked = await state();
      if (clicked.lastRoll === 6 && clicked.players[0].pos === clicked.players[0].start
          && clicked.players[0].steps === 0 && clicked.currentPlayer === 0) {
        rolledWithControl = true;
        break;
      }
    }
    const hasNamedRoll = (await Promise.all((await rolls.all()).map(button => button.isVisible()))).some(Boolean);
    // A drawn or icon-only control has no prescribed locator. Preserve those
    // implementations and require an actual click when a roll control is found.
    if (hasNamedRoll) assert(rolledWithControl, 'clicking the visible Roll control must update the real game');
    // With two consecutive sixes the die and turn indicator are unchanged,
    // while a real piece must move from its start to a different square.
    await page.evaluate(() => { gameTest.reset(); gameTest.setRolls([6, 6]); });
    await roll();
    const onStart = await page.screenshot({animations: 'disabled'});
    await roll();
    const sixAhead = await page.screenshot({animations: 'disabled'});
    if (6 % (await state()).trackLength !== 0) {
      assert(!onStart.equals(sixAhead), 'piece moved in state but not on the visible board');
    }
    console.log('four-player base, progress, dice and turn checks passed');
  } finally {
    await browser.close();
  }
})().catch(error => { console.error(error); process.exit(1); });