File size: 5,975 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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.clock.install();
    await page.goto(pathToFileURL(entry).href);
    await page.clock.pauseAt(new Date(Date.now() + 60000));
    const state = () => page.evaluate(() => gameTest.state());
    const step = milliseconds => page.evaluate(ms => gameTest.step(ms), milliseconds);
    await page.evaluate(() => gameTest.reset());
    await step(0); // Ask the deterministic update path to repaint the empty map.
    const emptyMap = await page.screenshot({animations: 'disabled'});
    await page.evaluate(() => gameTest.spawnEnemy());
    await step(0); // A zero-time update may repaint, but may not move the enemy.
    const populatedMap = await page.screenshot({animations: 'disabled'});
    assert(!emptyMap.equals(populatedMap), 'spawned enemy exists in state but is not visible on the map');
    let current = await state();
    assert(current.path.length >= 2 && current.enemies.length === 1, 'path and spawned enemy required');
    assert(current.enemies[0].hp === 3 && current.enemies[0].seg === 0 && current.enemies[0].t === 0, 'spawn initial state wrong');
    const initial = current;
    const initialSnapshot = JSON.stringify(initial);
    await step(0);
    assert(JSON.stringify(await state()) === initialSnapshot, 'zero-duration step changed state');
    await step(15);
    assert(JSON.stringify(await state()) === initialSnapshot, 'subframe step advanced simulation early');
    await step(1);
    current = await state();
    if (current.enemies.length) {
      const moved = current.enemies[0];
      assert(moved.seg > 0 || moved.t > 0, 'accumulated 16ms did not advance enemy');
      const start = current.path[moved.seg], end = current.path[moved.seg + 1];
      assert(start && end && moved.t >= 0 && moved.t <= 1, 'enemy segment/progress invalid');
      assert(Math.hypot(moved.position.x - start.x - (end.x - start.x) * moved.t,
        moved.position.y - start.y - (end.y - start.y) * moved.t) < 0.01, 'enemy position left path');
    }
    current = await page.evaluate(() => {
      const first = gameTest.state();
      const progressed = first.enemies[0];
      if (!progressed) return first;
      const lengths = first.path.slice(1).map((end, index) => {
        const start = first.path[index];
        return Math.hypot(end.x - start.x, end.y - start.y);
      });
      const traveled = lengths.slice(0, progressed.seg).reduce((a, b) => a + b, 0)
        + lengths[progressed.seg] * progressed.t;
      const totalLength = lengths.reduce((a, b) => a + b, 0);
      const byFraction = (first.path.length - 1) / (progressed.seg + progressed.t);
      const byDistance = traveled > 0 ? totalLength / traveled : 0;
      const remaining = 2 * Math.max(byFraction, byDistance);
      // Allow much slower paths and implementations whose speed is in pixels rather
      // than normalized segment fractions. Each public call advances one tick.
      const limit = Math.min(100000, Math.max(10000, Math.ceil(remaining)));
      let snapshot = first;
      for (let ticks = 0; ticks < limit && snapshot.enemies.length; ticks += 32) {
        gameTest.step(16 * 32);
        snapshot = gameTest.state();
      }
      return snapshot;
    });
    assert(current.enemies.length === 0, 'normal-speed enemy did not reach the path end');
    assert(current.lives === initial.lives - 1 && current.kills === initial.kills,
      'an escaped enemy must cost one life without awarding a kill');
    await step(16);
    current = await state();
    assert(current.lives === initial.lives - 1 && current.kills === initial.kills,
      'an escaped enemy must not be counted again on later steps');
    await page.evaluate(() => { gameTest.reset(); gameTest.spawnEnemy({speed: 0}); });
    current = await state();
    const enemy = current.enemies[0];
    assert(enemy?.position && enemy.hp === 3, 'stationary fixture enemy missing');
    const far = [...current.path].sort((a, b) =>
      Math.hypot(b.x - enemy.position.x, b.y - enemy.position.y)
      - Math.hypot(a.x - enemy.position.x, a.y - enemy.position.y))[0];
    const distanceToFar = Math.hypot(far.x - enemy.position.x, far.y - enemy.position.y);
    assert(distanceToFar > 0, 'path needs two distinct points');
    const range = distanceToFar / 2;
    await page.evaluate(({x, y, range}) => gameTest.placeTower(x, y, {range}), {...far, range});
    const placed = (await state()).towers;
    assert(placed.length === 1 && Number.isFinite(placed[0].range) && placed[0].range >= 0,
      'placed tower must expose a finite nonnegative range');
    assert(Math.abs(placed[0].range - range) < 1e-9, 'range override must be honored');
    const hp = enemy.hp;
    await step(2048);
    current = await state();
    assert(current.enemies[0]?.position, 'enemy disappeared during range check');
    assert(JSON.stringify(current.enemies[0].position) === JSON.stringify(enemy.position), 'speed zero must hold position');
    assert(current.enemies[0].hp === hp, 'far tower attacked out-of-range enemy');
    const near = current.enemies[0].position;
    await page.evaluate(({x, y}) => gameTest.placeTower(x, y, {range: 1}), near);
    await step(2048);
    current = await state();
    assert(current.towers.length === 2, 'placed towers missing');
    assert(current.kills === 1 || (current.enemies.length === 1 && current.enemies[0].hp < hp), 'near tower did not attack');
    console.log('tower-defense fixed-step, path interpolation and range checks passed');
  } finally {
    await browser.close();
  }
})().catch(error => { console.error(error); process.exit(1); });