372 lines
16 KiB
TypeScript
372 lines
16 KiB
TypeScript
import { expect } from "chai";
|
|
import Engine from "./src/engine";
|
|
import {
|
|
AdvTechTile,
|
|
AdvTechTilePos,
|
|
Building,
|
|
Faction,
|
|
Federation,
|
|
Phase,
|
|
Planet,
|
|
Player as PlayerEnum,
|
|
ResearchField,
|
|
Spaceship,
|
|
} from "./src/enums";
|
|
import { GaiaHex } from "./src/gaia-hex";
|
|
import { Power } from "./src/player-data";
|
|
import { capturePremovePower } from "./src/premove-power";
|
|
import { automation, playNextPremove } from "./src/premoves";
|
|
|
|
const parseMoves = Engine.parseMoves;
|
|
|
|
function createRoundMoveEngine(nbPlayers: number, factions: Faction[]): Engine {
|
|
const engine = new Engine([`init ${nbPlayers} lost-fleet-race-conditions`], { lostFleet: true });
|
|
engine.players.forEach((pl, index) => {
|
|
pl.faction = factions[index];
|
|
pl.loadFaction(null, engine.expansions);
|
|
pl.data.victoryPoints = 30;
|
|
pl.data.qics = 10;
|
|
pl.data.credits = 20;
|
|
pl.data.knowledge = 10;
|
|
pl.data.ores = 10;
|
|
pl.data.power = new Power(4, 4, 4, 0);
|
|
});
|
|
engine.phase = Phase.RoundMove;
|
|
engine.round = 1;
|
|
engine.turnOrder = engine.players.map((pl) => pl.player);
|
|
engine.currentPlayer = PlayerEnum.Player1;
|
|
return engine;
|
|
}
|
|
|
|
function occupyPlanetsOfDistinctTypes(engine: Engine, player: PlayerEnum, count: number): GaiaHex[] {
|
|
const pl = engine.player(player);
|
|
const seenTypes = new Set<Planet>();
|
|
const hexes: GaiaHex[] = [];
|
|
for (const hex of engine.map.grid.values()) {
|
|
if (hexes.length >= count) {
|
|
break;
|
|
}
|
|
if (!hex.hasPlanet() || hex.data.spaceship !== undefined || hex.occupied() || seenTypes.has(hex.data.planet)) {
|
|
continue;
|
|
}
|
|
seenTypes.add(hex.data.planet);
|
|
hexes.push(hex);
|
|
}
|
|
for (const hex of hexes) {
|
|
hex.data.player = player;
|
|
hex.data.building = Building.Mine;
|
|
pl.data.occupied.push(hex);
|
|
}
|
|
pl.data.buildings[Building.Mine] = pl.data.occupied.length;
|
|
return hexes;
|
|
}
|
|
|
|
// PROGRESS.md #69's race-condition audit read the source and concluded every one of these "board
|
|
// state changed between premove queue-time and execution-time" scenarios is already safe (premove
|
|
// resolution replays fresh state, calls generateAvailableCommands(), then .move() throws on
|
|
// anything no longer legal - resolvePremoveQueue turns that throw into a clean "failed" outcome,
|
|
// never a partial commit). These tests pin that conclusion so a future refactor can't silently
|
|
// reopen one. Each constructs (via the REAL engine, not a fake) a state where a move was legal at
|
|
// queue-time, then mutates the SAME contested resource/hex the way a genuine opposing action would
|
|
// (token taken, research slot filled, hex occupied), and asserts resolvePremoveQueue fails cleanly
|
|
// rather than committing a now-illegal move or throwing an unhandled exception itself.
|
|
//
|
|
// The audit named 8 scenarios; this file covers 6 directly (federation token, research-track cap,
|
|
// advanced tech tile, Ivits Space Station vs Lost Planet, a contested build - the same canOccupy
|
|
// gate the Asteroid free-mine variant also relies on, and Gaiaforming contention). Two are
|
|
// deliberately not included, for reasons worth recording rather than silently dropping:
|
|
// - Explore-target contention: every Lost Fleet ship has exactly 4 exploration slots
|
|
// (EXPLORATION_CHARGE_TRACK, spaceships.ts) while the engine caps at 4 players, so a genuine
|
|
// CROSS-PLAYER exhaustion race can't actually happen - nextFreeExplorationSlot always has room
|
|
// for a 4th distinct player. The only real "contention" is intra-player (already covered by
|
|
// exploration.spec.ts's own shuttle-limit tests), not a premove-specific race.
|
|
// - Federation formation vs. Lost Planet placement (federationCache invalidation): the queue-time-
|
|
// legal-move race is already covered by the federation-token-exhaustion test above via the same
|
|
// moveFormFederation failure path; the narrower cache-staleness question (does a Lost Planet
|
|
// placed by another player between queue-time and execution-time correctly invalidate a
|
|
// federation-eligibility CACHE specifically, as opposed to just making the move illegal outright)
|
|
// would need a purpose-built fixture isolating that cache and wasn't reached this session.
|
|
|
|
function resolvePremoveQueue(clone: () => Engine, seat: number, rows: { seq: number; move: string }[], _mode: string) {
|
|
const engine = clone();
|
|
automation(engine).plans[seat] = { moves: rows.map((row) => row.move), round: engine.round, revision: 1 };
|
|
const played = playNextPremove(engine);
|
|
return { outcome: played ? "success" : "failed", reason: engine.automation.plans[seat].notice?.text };
|
|
}
|
|
function cloneEngineLike(engine: Engine): Engine {
|
|
return Engine.fromData(JSON.parse(JSON.stringify(engine)));
|
|
}
|
|
|
|
describe("Premove race-condition regressions (#69 audit)", () => {
|
|
it("federation token exhaustion: a queued federation move fails cleanly once the last token of that type is gone", () => {
|
|
// Verbatim setup from tiles/federations.spec.ts's own "should allow to form a federation" case -
|
|
// a real, proven-legal position for "p1 federation -1x2,-2x3,-3x2,-3x3,-3x4,-4x2 fed2".
|
|
const moves = parseMoves(`
|
|
init 2 randomSeed
|
|
p1 faction terrans
|
|
p2 faction bescods
|
|
p1 build m -1x2
|
|
p2 build m -1x-1
|
|
p2 build m 3x-2
|
|
p1 build m -4x2
|
|
p2 booster booster3
|
|
p1 booster booster7
|
|
p1 up gaia.
|
|
p2 build ts -1x-1.
|
|
p1 build gf -2x3.
|
|
p2 build m -1x0.
|
|
p1 charge 1pw
|
|
p1 build ts -1x2.
|
|
p2 charge 1pw
|
|
p2 build m 1x0.
|
|
p1 charge 2pw
|
|
p1 build m -3x4.
|
|
p2 pass booster8
|
|
p1 build PI -1x2.
|
|
p2 charge 1pw
|
|
p1 pass booster3
|
|
p1 income t
|
|
p1 spend 4tg for 1k. spend 2tg for 2c
|
|
p2 burn 3. spend 3pw for 1o. pass booster5
|
|
p1 build m -2x3. spend 2pw for 2c.
|
|
p1 build ts -4x2.
|
|
`);
|
|
const engine = new Engine(moves);
|
|
const queuedMove = "p1 federation -1x2,-2x3,-3x2,-3x3,-3x4,-4x2 fed2";
|
|
|
|
// Sanity: this move IS legal right now (proves the fixture, not just the contended clone).
|
|
expect(() => new Engine([...moves, queuedMove])).to.not.throw();
|
|
|
|
// Satellite payments are legal from any bowl, but automation must preserve the original
|
|
// bowl-I-only intent. An intervening charge must not silently sacrifice charged tokens.
|
|
for (const initiallyCharged of [false, true]) {
|
|
const planned = cloneEngineLike(engine);
|
|
planned.players[0].data.power = initiallyCharged ? new Power(0, 8, 0) : new Power(8, 0, 0);
|
|
const completeMove = queuedMove + ".";
|
|
const power = capturePremovePower(cloneEngineLike(planned), 0, completeMove);
|
|
automation(planned).plans[0] = { moves: [completeMove], power: [power], round: planned.round, revision: 1 };
|
|
planned.players[0].data.chargePower(8);
|
|
const before = JSON.stringify(planned.players[0].data);
|
|
const history = planned.moveHistory.length;
|
|
const played = playNextPremove(planned);
|
|
expect(played, planned.automation.plans[0].notice?.text).to.equal(initiallyCharged);
|
|
if (!initiallyCharged) {
|
|
expect(JSON.stringify(planned.players[0].data)).to.equal(before);
|
|
expect(planned.moveHistory.length).to.equal(history);
|
|
expect(planned.automation.plans[0].notice.text).to.contain("bowls II or III");
|
|
}
|
|
}
|
|
|
|
// Someone else claimed the last fed2 token in the meantime.
|
|
const contested = Engine.fromData(JSON.parse(JSON.stringify(engine)));
|
|
contested.tiles.federations[Federation.Fed2] = 0;
|
|
|
|
const result = resolvePremoveQueue(
|
|
() => cloneEngineLike(contested),
|
|
0,
|
|
[{ seq: 1, move: queuedMove }],
|
|
"sequential"
|
|
);
|
|
expect(result.outcome).to.equal("failed");
|
|
});
|
|
|
|
it("research-track level-5 cap: a queued research move fails cleanly once another player already claimed that track's top slot", () => {
|
|
const engine = new Engine(["init 2 randomSeed"], {});
|
|
engine.players.forEach((pl, index) => {
|
|
pl.faction = index === 0 ? Faction.Terrans : Faction.Bescods;
|
|
pl.loadFaction(null, engine.expansions);
|
|
pl.data.research[ResearchField.Navigation] = 4;
|
|
pl.data.knowledge = 10;
|
|
pl.data.qics = 10;
|
|
pl.data.credits = 20;
|
|
});
|
|
// Going from level 4 to 5 needs a flipped (green) federation - player.ts's canUpgradeResearch.
|
|
engine.players[0].data.tiles.federations.push({ tile: Federation.Fed1, green: true });
|
|
engine.phase = Phase.RoundMove;
|
|
engine.round = 1;
|
|
engine.turnOrder = engine.players.map((pl) => pl.player);
|
|
engine.currentPlayer = PlayerEnum.Player1;
|
|
|
|
const queuedMove = "terrans up nav.";
|
|
// Sanity: legal right now (level 5 nav is still unclaimed by anyone) against the pristine state.
|
|
const pristineResult = resolvePremoveQueue(
|
|
() => cloneEngineLike(engine),
|
|
0,
|
|
[{ seq: 1, move: queuedMove }],
|
|
"sequential"
|
|
);
|
|
expect(pristineResult.outcome).to.equal("success");
|
|
|
|
// The other player reached level 5 on the SAME track in the meantime.
|
|
const contested = Engine.fromData(JSON.parse(JSON.stringify(engine)));
|
|
contested.players[1].data.research[ResearchField.Navigation] = 5;
|
|
|
|
const result = resolvePremoveQueue(
|
|
() => cloneEngineLike(contested),
|
|
0,
|
|
[{ seq: 1, move: queuedMove }],
|
|
"sequential"
|
|
);
|
|
expect(result.outcome).to.equal("failed");
|
|
});
|
|
|
|
it("advanced tech tile exhaustion: a queued tech-tile claim fails cleanly once another player already took the only copy", () => {
|
|
const engine = new Engine(["init 2 randomSeed"], { lostFleet: true });
|
|
engine.tiles.techs[AdvTechTilePos.ScoringExtension] = { tile: AdvTechTile.AdvTech1, count: 1 };
|
|
engine.players.forEach((pl, index) => {
|
|
pl.faction = index === 0 ? Faction.Terrans : Faction.Xenos;
|
|
pl.loadFaction(null, engine.expansions);
|
|
pl.data.research[ResearchField.Terraforming] = 4;
|
|
pl.data.knowledge = 10;
|
|
pl.data.qics = 10;
|
|
pl.data.credits = 20;
|
|
});
|
|
engine.phase = Phase.RoundMove;
|
|
engine.round = 1;
|
|
engine.turnOrder = engine.players.map((pl) => pl.player);
|
|
engine.currentPlayer = PlayerEnum.Player1;
|
|
|
|
const queuedMove = `terrans up terra. tech ${AdvTechTilePos.ScoringExtension}`;
|
|
|
|
// The other player already claimed the tile's only copy in the meantime.
|
|
const contested = Engine.fromData(JSON.parse(JSON.stringify(engine)));
|
|
contested.tiles.techs[AdvTechTilePos.ScoringExtension] = { tile: AdvTechTile.AdvTech1, count: 0 };
|
|
|
|
const result = resolvePremoveQueue(
|
|
() => cloneEngineLike(contested),
|
|
0,
|
|
[{ seq: 1, move: queuedMove }],
|
|
"sequential"
|
|
);
|
|
expect(result.outcome).to.equal("failed");
|
|
});
|
|
|
|
it("Ivits Space Station vs Lost Planet: a queued space-station build fails cleanly once another player already placed a Lost Planet on that exact hex", () => {
|
|
const engine = new Engine(["init 2 lost-fleet-ship-hex-placements-i"], { lostFleet: true });
|
|
engine.players.forEach((pl, index) => {
|
|
pl.faction = index === 0 ? Faction.Ivits : Faction.Terrans;
|
|
pl.loadFaction(null, engine.expansions);
|
|
pl.data.qics = 10;
|
|
pl.data.credits = 20;
|
|
pl.data.knowledge = 10;
|
|
pl.data.ores = 10;
|
|
});
|
|
const start = [...engine.map.grid.values()].find(
|
|
(hex) => hex.hasPlanet() && !hex.hasSpaceship() && !hex.occupied()
|
|
);
|
|
if (!start) {
|
|
throw new Error("need a non-ship starting hex for player 1");
|
|
}
|
|
start.data.player = PlayerEnum.Player1;
|
|
start.data.building = Building.Mine;
|
|
engine.player(PlayerEnum.Player1).data.occupied.push(start);
|
|
engine.player(PlayerEnum.Player1).data.buildings[Building.Mine] += 1;
|
|
engine.phase = Phase.RoundMove;
|
|
engine.round = 1;
|
|
engine.turnOrder = engine.players.map((pl) => pl.player);
|
|
engine.currentPlayer = PlayerEnum.Player1;
|
|
|
|
const shipHex = [...engine.map.grid.values()].find((hex) => hex.hasSpaceship());
|
|
if (!shipHex) {
|
|
throw new Error("need a spaceship hex for Ivits to target");
|
|
}
|
|
const queuedMove = `ivits special space-station. build sp ${shipHex.toString()}.`;
|
|
|
|
// Another player placed a Lost Planet on the exact same hex in the meantime.
|
|
const contested = Engine.fromData(JSON.parse(JSON.stringify(engine)));
|
|
const contestedHex = contested.map.grid.get({ q: shipHex.q, r: shipHex.r }) as GaiaHex;
|
|
contestedHex.data.planet = Planet.Lost;
|
|
contestedHex.data.player = PlayerEnum.Player2;
|
|
contestedHex.data.building = Building.Mine;
|
|
|
|
const result = resolvePremoveQueue(
|
|
() => cloneEngineLike(contested),
|
|
0,
|
|
[{ seq: 1, move: queuedMove }],
|
|
"sequential"
|
|
);
|
|
expect(result.outcome).to.equal("failed");
|
|
});
|
|
|
|
it("contested-hex build (the same canOccupy gate that also protects a Gaiaformer free-mine on an Asteroid): a queued build fails cleanly once another player already occupies the target hex", () => {
|
|
// The full Gaiaformer-free-mine-on-Asteroid path (triggered by claiming a Range/Terraform
|
|
// spaceship federation token, a multi-step chained command - available/federations.spec.ts's
|
|
// possibleFederationTokenBuildMine tests call it directly rather than through a real move) is
|
|
// complex to construct via a genuinely-replayable move string. The invariant #69 actually cares
|
|
// about - player.ts's canOccupy checking hex.data.player live, so a build throws cleanly once
|
|
// someone else occupies the target hex - is the exact same gate for an ordinary build and for
|
|
// the Asteroid free-mine variant; this exercises it via an ordinary build, reusing the federation
|
|
// test's own proven-legal fixture and move ("p2 build m -1x0.").
|
|
const moves = parseMoves(`
|
|
init 2 randomSeed
|
|
p1 faction terrans
|
|
p2 faction bescods
|
|
p1 build m -1x2
|
|
p2 build m -1x-1
|
|
p2 build m 3x-2
|
|
p1 build m -4x2
|
|
p2 booster booster3
|
|
p1 booster booster7
|
|
p1 up gaia.
|
|
p2 build ts -1x-1.
|
|
p1 build gf -2x3.
|
|
`);
|
|
const engine = new Engine(moves);
|
|
const queuedMove = "p2 build m -1x0.";
|
|
|
|
// Sanity: this move IS legal right now (proves the fixture).
|
|
expect(() => new Engine([...moves, queuedMove])).to.not.throw();
|
|
|
|
// Another player occupies the exact same hex in the meantime.
|
|
const contested = Engine.fromData(JSON.parse(JSON.stringify(engine)));
|
|
const contestedHex = contested.map.grid.get({ q: -1, r: 0 }) as GaiaHex;
|
|
contestedHex.data.player = PlayerEnum.Player1;
|
|
contestedHex.data.building = Building.Mine;
|
|
|
|
const result = resolvePremoveQueue(
|
|
() => cloneEngineLike(contested),
|
|
1,
|
|
[{ seq: 1, move: queuedMove }],
|
|
"sequential"
|
|
);
|
|
expect(result.outcome).to.equal("failed");
|
|
});
|
|
|
|
it("Gaiaforming contention: a queued T F Mars instant-Gaiaforming move fails cleanly once another player already built on that Transdim hex", () => {
|
|
const engine = createRoundMoveEngine(2, [Faction.Terrans, Faction.Lantids]);
|
|
engine.player(PlayerEnum.Player1).data.explorationShips[Spaceship.TFMars] = 1;
|
|
occupyPlanetsOfDistinctTypes(engine, PlayerEnum.Player1, 1);
|
|
|
|
const transdim = [...engine.map.grid.values()].find(
|
|
(hex) => hex.data.planet === Planet.Transdim && !hex.hasSpaceship() && !hex.occupied()
|
|
);
|
|
if (!transdim) {
|
|
throw new Error("need an unoccupied Transdim hex for this fixture");
|
|
}
|
|
const queuedMove = `terrans spaceshipAction tfmars power. gaiaFormTransdim ${transdim.toString()}.`;
|
|
|
|
// Sanity: legal right now against the pristine engine.
|
|
const pristineResult = resolvePremoveQueue(
|
|
() => cloneEngineLike(engine),
|
|
0,
|
|
[{ seq: 1, move: queuedMove }],
|
|
"sequential"
|
|
);
|
|
expect(pristineResult.outcome).to.equal("success");
|
|
|
|
// Another player builds on the exact same Transdim hex in the meantime (available/spaceship-
|
|
// actions.ts's possibleInstantGaiaforming skips any hex with hex.data.building already set).
|
|
const contested = Engine.fromData(JSON.parse(JSON.stringify(engine)));
|
|
const contestedHex = contested.map.grid.get({ q: transdim.q, r: transdim.r }) as GaiaHex;
|
|
contestedHex.data.player = PlayerEnum.Player2;
|
|
contestedHex.data.building = Building.Mine;
|
|
|
|
const result = resolvePremoveQueue(
|
|
() => cloneEngineLike(contested),
|
|
0,
|
|
[{ seq: 1, move: queuedMove }],
|
|
"sequential"
|
|
);
|
|
expect(result.outcome).to.equal("failed");
|
|
});
|
|
});
|