import assert from "node:assert/strict"; import { test } from "node:test"; import { autopilot } from "../src/bot.js"; import { LAMP, createGame, dangerAhead, inBeam, simulate, start, step } from "../src/game.js"; test("the same seed plays the same game", () => { const a = simulate(start(createGame(42)), 30, autopilot); const b = simulate(start(createGame(42)), 30, autopilot); assert.deepEqual( { saved: a.saved, lives: a.lives, ships: a.ships.map((s) => [s.x, s.y]) }, { saved: b.saved, lives: b.lives, ships: b.ships.map((s) => [s.x, s.y]) }, ); }); test("rocks keep apart and clear of the lighthouse", () => { for (let seed = 1; seed <= 20; seed++) { const { rocks } = createGame(seed); assert.equal(rocks.length, 7); for (const r of rocks) assert.ok(Math.hypot(r.x - LAMP.x, r.y - LAMP.y) > 150); for (const r of rocks) for (const o of rocks) if (r !== o) assert.ok(Math.hypot(r.x - o.x, r.y - o.y) > r.r + o.r + 40); } }); function lane(game) { // One rock, one ship sailing straight at it. game.rocks = [{ x: 400, y: 200, r: 20 }]; game.ships = [{ id: 1, x: 250, y: 200, heading: 0, speed: 50, lit: false }]; game.nextShip = 999; return game; } test("a ship in the dark sails onto the rock", () => { const game = lane(start(createGame(1))); game.beam = -0.2; // pointing away, at the right edge simulate(game, 4, () => ({})); assert.equal(game.ships.length, 0); assert.equal(game.lives, 2); }); test("a ship in the light steers around it", () => { const game = lane(start(createGame(1))); const ship = game.ships[0]; for (let i = 0; i < 600 && game.ships.length; i++) { const s = game.ships[0]; step(game, { aim: Math.atan2(s.y - LAMP.y, s.x - LAMP.x) }); } assert.equal(game.lives, 3); assert.ok(ship.x > 420, "the ship got past the rock"); }); test("the beam lights what it points at, and nothing behind it", () => { const game = createGame(1); game.beam = -Math.PI / 2; assert.ok(inBeam(game, LAMP.x, LAMP.y - 200)); assert.ok(!inBeam(game, LAMP.x + 300, LAMP.y - 50)); }); test("danger ahead sees a rock on the course and not one behind", () => { const game = lane(createGame(1)); assert.ok(dangerAhead(game, game.ships[0], 200)); game.ships[0].heading = Math.PI; assert.equal(dangerAhead(game, game.ships[0], 200), null); }); test("a ship that reaches the far side is saved, and three wrecks end the game", () => { const game = start(createGame(3)); game.rocks = []; game.ships = [{ id: 1, x: 950, y: 200, heading: 0, speed: 60, lit: false }]; game.nextShip = 999; simulate(game, 1); assert.equal(game.saved, 1); game.lives = 1; game.rocks = [{ x: 100, y: 100, r: 20 }]; game.ships = [{ id: 2, x: 100, y: 100, heading: 0, speed: 10, lit: false }]; step(game); assert.equal(game.state, "over"); });