fuzzyworld

explodorama / files / test / routePreview.test.ts

routePreview.test.ts

Blame: the commit that last changed each line · file · history

92cc4ee3 2026-11-02 Pavel Safronov1import { describe, expect, it } from 'vitest'
2import { autoRoute, playerPath } from '../src/sim/path'
3import { newRun } from '../src/sim/state'
4import { farTileAt, farTiles } from '../src/ui/padTiles'
5import { routePreview } from '../src/ui/routePreview'
6import { predictDanger } from '../src/sim/danger'
7import { safeStep } from '../src/sim/path'
8import type { GameState } from '../src/sim/types'
9import { board, v } from './helpers'
10
11const room = () =>
12 board(`
13 #########
14 #P......#
15 #.......#
16 #########`)
17const safeOf = (s: GameState) => {
18 const d = predictDanger(s)
19 return (p: { x: number; y: number }) => safeStep(s, d, p)
20}
21
22describe('routePreview', () => {
23 it('shows the same landing tiles as the real route, and changes nothing', () => {
24 const s = room()
25 s.powers.run = true
26 const before = JSON.stringify(s)
27 const view = routePreview(s, v(7, 1), safeOf(s))!
28 expect(view.way).toEqual(autoRoute(s, v(7, 1), safeOf(s)))
29 expect(view.blocked).toBe(false)
30 // Run: it lands on every other tile, not on each of the 6 along the way.
31 expect(view.way.length).toBeLessThan(playerPath(s, v(7, 1))!.length)
32 expect(JSON.stringify(s)).toBe(before)
33 })
34
35 it('without Run, lands on every tile of the walk', () => {
36 const s = room()
37 expect(routePreview(s, v(4, 1), safeOf(s))!.way).toEqual(playerPath(s, v(4, 1)))
38 })
39
40 it('flags an unsafe first step and shows the plain walk', () => {
41 const s = room()
42 const view = routePreview(s, v(4, 1), (p) => !(p.x === 2 && p.y === 1))!
43 expect(view.blocked).toBe(true)
44 expect(view.way).toEqual(playerPath(s, v(4, 1)))
45 })
46
47 it('is null where there is no way', () => {
48 const s = board(`
49 #######
50 #P.#.##
51 #######`)
52 expect(routePreview(s, v(4, 1), safeOf(s))).toBeNull()
53 })
54
55 for (const seed of [1, 2, 3, 4]) {
56 it(`seed ${seed}: every faded tile has a preview that matches the real route`, () => {
57 const s = newRun(seed, 1 + seed)
58 const safe = safeOf(s)
59 for (const f of farTiles(s, safe)) {
60 expect(farTileAt(s, safe, f.tile)).toEqual(f)
61 const view = routePreview(s, f.tile, safe)!
62 expect(view.way).toEqual(autoRoute(s, f.tile, safe))
63 expect(view.blocked).toBe(false)
64 }
65 })
66 }
67})