File size: 4,102 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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 OUTPUT = '/logs/verifier/visual';

function winningMoves(start, direction, secondPlayer) {
  // Last stone joins two shorter runs, exercising both sides of the new stone.
  const line = [0, 4, 1, 3, 2].map(i => [start[0] + direction[0] * i, start[1] + direction[1] * i]);
  const distractors = [[14, 0], [14, 2], [14, 4], [14, 6], [14, 8]];
  return line.flatMap((point, i) => secondPlayer ? [distractors[i], point]
    : i === line.length - 1 ? [point] : [point, distractors[i]]);
}

(async () => {
  const entry = path.join(process.cwd(), 'index.html');
  assert(fs.existsSync(entry), 'missing public index.html entry');
  const browser = await chromium.launch({headless: true, args: ['--no-sandbox']});
  try {
    fs.mkdirSync(OUTPUT, {recursive: true});
    const page = await browser.newPage({viewport: {width: 1100, height: 900}});
    await page.route(/^https?:/, route => route.abort());
    await page.goto(pathToFileURL(entry).href);
    const state = () => page.evaluate(() => window.gameTest.state());
    const reset = () => page.evaluate(() => window.gameTest.reset());
    const move = (r, c) => page.evaluate(([row, col]) => window.gameTest.move(row, col), [r, c]);
    const capture = name => page.screenshot({path: path.join(OUTPUT, `${name}.png`), animations: 'disabled'});
    await reset();
    const firstPlayer = (await state()).current;
    await capture('01-empty');
    await move(2, 3); await move(11, 12);
    await capture('02-two-stones');
    await move(4, 5); await move(9, 10);
    await capture('03-four-stones');
    const directions = [
      ['horizontal', [0, 0], [0, 1]],
      ['vertical', [0, 14], [1, 0]],
      ['diagonal', [0, 0], [1, 1]],
      ['anti-diagonal', [0, 14], [1, -1]],
      ['interior-horizontal', [7, 5], [0, 1]],
      ['interior-vertical', [5, 7], [1, 0]],
    ];
    const winners = [null, null];
    for (const [label, start, direction] of directions) {
      for (const secondPlayer of [false, true]) {
        const moves = winningMoves(start, direction, secondPlayer);
        await reset();
        for (let i = 0; i < moves.length; i++) {
          await move(...moves[i]);
          const snapshot = await state();
          if (i < moves.length - 1) assert(!snapshot.winner && !snapshot.easterEgg,
            `${label}/${secondPlayer}: premature victory`);
        }
        const finished = await state();
        assert(finished.winner && finished.easterEgg, `${label}/${secondPlayer}: missed five-in-a-row`);
        const player = Number(secondPlayer);
        if (winners[player] !== null) assert(winners[player] === finished.winner, 'winner labels must be consistent');
        winners[player] = finished.winner;
        if (label === 'horizontal' && !secondPlayer) await capture('04-first-player-win');
        if (label === 'anti-diagonal' && secondPlayer) await capture('05-second-player-win');
      }
    }
    assert(winners[0] !== winners[1], 'players reported the same winner');
    for (const points of [
      [[3, 0], [3, 2], [3, 4], [3, 6], [3, 8]],
      [[4, 0], [4, 1], [4, 3], [4, 4], [4, 5]],
    ]) {
      await reset();
      for (let i = 0; i < points.length; i++) {
        await move(...points[i]);
        const snapshot = await state();
        assert(!snapshot.winner && !snapshot.easterEgg, 'disconnected stones declared a win');
        if (i < points.length - 1) await move(12, i * 2);
      }
    }
    await reset();
    const cleared = await state();
    assert(cleared.current === firstPlayer && !cleared.winner && !cleared.easterEgg
      && cleared.board.every(row => row.every(cell => cell === null)), 'reset must restore initial state');
    await capture('06-reset');
    console.log('All win directions, both players, joined runs, gaps and reset passed');
  } finally {
    await browser.close();
  }
})().catch(error => { console.error(error); process.exit(1); });