'use strict'; const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const { makeGame, Planet, Rng, GameCore } = require('./helpers.js'); const Offline = require('../src/offline.js'); describe('Offline Strijd progression (t1119)', () => { it('one opponent for one round, then N opponents for N-1 rounds', () => { assert.equal(Offline.roundsAtOpponentCount(1), 1); assert.equal(Offline.roundsAtOpponentCount(2), 1); assert.equal(Offline.roundsAtOpponentCount(3), 2); assert.equal(Offline.roundsAtOpponentCount(6), 5); const expect = { 1: { opponents: 1 }, 2: { opponents: 2 }, 3: { opponents: 3 }, 4: { opponents: 3 }, 5: { opponents: 4 }, 6: { opponents: 4 }, 7: { opponents: 4 }, 8: { opponents: 5 } }; for (const [round, exp] of Object.entries(expect)) { const s = Offline.scheduleForRound(Number(round)); assert.equal(s.opponents, exp.opponents, 'round ' + round + ' opponents=' + s.opponents); assert.equal(s.round, Number(round)); } }); it('skill starts at 5% and rises 1% per round, capped at 100%', () => { assert.equal(Offline.skillForRound(1), 0.05); assert.equal(Offline.skillForRound(2), 0.06); assert.equal(Offline.skillForRound(96), 1); assert.equal(Offline.skillForRound(97), 1); assert.equal(Offline.skillForRound(200), 1); assert.equal(Offline.scheduleForRound(1).skill, 0.05); assert.equal(Offline.scheduleForRound(10).skill, 0.14); }); }); describe('Bot move finder and roll (t1119)', () => { it('finds the shortest vertical move that creates a match', () => { const g = makeGame({ noSpawn: true }); // Column 0: [0, 0, 1, 0] — moving the 1 up (or the top 0 down) makes [0,0,0,1]. g.place(0, 0, 0); g.place(0, 1, 0); g.place(0, 2, 1); g.place(0, 3, 0); const move = Offline.findBotMove(g); assert.ok(move, 'expected a move'); assert.equal(move.col, 0); assert.equal(move.dist, 1); assert.ok( (move.fromY === 2 && move.toY === 3) || (move.fromY === 3 && move.toY === 2), 'from=' + move.fromY + ' to=' + move.toY ); assert.ok(Offline.executeBotMove(g, move)); const launched = g.resolveMatches(); assert.ok(launched.length >= 1, 'the move must create a match'); }); it('returns null when no vertical move creates a match', () => { const g = makeGame({ noSpawn: true }); g.place(0, 0, 0); g.place(0, 1, 1); g.place(0, 2, 2); g.place(1, 0, 3); g.place(1, 1, 4); assert.equal(Offline.findBotMove(g), null); }); it('a roll at skill 0 never moves, skill 1 always tries', () => { const setup = () => { const g = makeGame({ noSpawn: true }); g.place(0, 0, 0); g.place(0, 1, 0); g.place(0, 2, 1); g.place(0, 3, 0); return g; }; const g0 = setup(); const r0 = Offline.tryBotRoll(g0, 0, () => 0); assert.equal(r0.success, false); assert.equal(r0.moved, false); assert.equal(g0.blocks[2].y, 2, 'nothing moved at skill 0 even with roll 0'); const g1 = setup(); const r1 = Offline.tryBotRoll(g1, 1, () => 0.999); assert.equal(r1.success, true); assert.equal(r1.moved, true); assert.ok(r1.move); // roll == skill is a failure (success only when roll < skill) const g2 = setup(); const r2 = Offline.tryBotRoll(g2, 0.05, () => 0.05); assert.equal(r2.success, false); assert.equal(r2.moved, false); }); }); describe('Offline battle flow (t1119)', () => { it('starts a round with the scheduled number of bots, each a random planet', () => { const rng = Rng.create(42); const battle = Offline.createBattle({ playerPlanet: Planet.defaultPlanet(), rng: rng, round: 5 }); assert.equal(battle.schedule.opponents, 4); assert.equal(battle.bots.length, 4); assert.equal(battle.schedule.skill, Offline.skillForRound(5)); for (const bot of battle.bots) { assert.ok(bot.planet && bot.planet.id); assert.ok(bot.core instanceof GameCore); assert.equal(bot.eliminated, false); } const ids = new Set(battle.bots.map((b) => b.planet.id)); // With 12 planets and 4 bots, seed 42 is very likely to vary; just // assert they are real catalogue planets. const catalogue = new Set(Planet.allPlanets().map((p) => p.id)); for (const id of ids) { assert.ok(catalogue.has(id), 'unknown planet ' + id); } }); it('a forced miss never delivers garbage; a forced hit does', () => { const player = makeGame({ noSpawn: true }); // Forced miss: rng always returns 0 for pick? miss if rng() < missChance. // Use a planet with 60% miss (power 0) and rng that always returns 0 // → always miss? 0 < 0.60 is true, so miss. Wait, I used rng() < missChance // in battle... _dispatchShots: miss = targets.length===0 || this.rng() < missChance // Yes 0 < 0.60 → miss. const easy = Planet.createPlanet({ jumpsToClear: 1, fallTime: 5, riseTime: 3, id: 'easy' }); let n = 0; const missRng = () => { n++; return 0; }; // always miss (0 < 0.60) const battle = Offline.createBattle({ playerPlanet: easy, rng: missRng, round: 1 }); // Give the player an outgoing shot without actually flying. player.outgoingShots.push({ id: 99, color: 1, col: 0, y: 13 }); battle.tick(0.01, player); assert.ok(battle.flightResults.length >= 1); assert.equal(battle.flightResults[0].miss, true); assert.equal(battle.pending.length, 0); // Forced hit: rng always 0.99, missChance 0.60, 0.99 < 0.60 is false. const hitRng = () => 0.99; const battle2 = Offline.createBattle({ playerPlanet: easy, rng: hitRng, round: 1 }); const player2 = makeGame({ noSpawn: true }); player2.outgoingShots.push({ id: 7, color: 2, col: 1, y: 13 }); const botBlocksBefore = battle2.bots[0].core.blocks.length; battle2.tick(0.01, player2); assert.equal(battle2.flightResults[0].miss, false); assert.equal(battle2.pending.length, 1); // Deliver after the flight duration. battle2.tick(Offline.FLIGHT_DURATION + 0.05, player2); assert.equal(battle2.pending.length, 0); assert.ok(battle2.bots[0].core.blocks.length > botBlocksBefore, 'garbage was ingested by the bot'); }); it('winning a round means every bot is defeated, then advanceRound grows the field', () => { const rng = Rng.create(7); const battle = Offline.createBattle({ playerPlanet: Planet.defaultPlanet(), rng, round: 1 }); assert.equal(battle.bots.length, 1); const player = makeGame({ noSpawn: true }); battle.bots[0].core.state = 'gameover'; battle.tick(0.05, player); assert.equal(battle.state, 'round_won'); assert.equal(battle.aliveBots().length, 0); battle.advanceRound(); assert.equal(battle.round, 2); assert.equal(battle.state, 'playing'); assert.equal(battle.bots.length, 2); assert.equal(battle.schedule.skill, 0.06); }); it('the player losing sets state to lost at the current round', () => { const battle = Offline.createBattle({ playerPlanet: Planet.defaultPlanet(), rng: Rng.create(1), round: 3 }); const player = makeGame({ noSpawn: true }); player.state = 'gameover'; battle.tick(0.05, player); assert.equal(battle.state, 'lost'); assert.equal(battle.round, 3); }); });