621 lines
29 KiB
TypeScript
621 lines
29 KiB
TypeScript
import { expect } from "chai";
|
|
import Engine from "./src/engine";
|
|
import { Operator, Phase, Resource } from "./src/enums";
|
|
import Event from "./src/events";
|
|
import { Power } from "./src/player-data";
|
|
import { capturePremovePower } from "./src/premove-power";
|
|
import { automation, playNextPremove, PremoveCommand, setPremoves } from "./src/premoves";
|
|
import {
|
|
automove,
|
|
canMoveOutOfTurn,
|
|
isLiveUpdate,
|
|
logSlice,
|
|
move,
|
|
replay,
|
|
setPlayerSettings,
|
|
stripSecret,
|
|
timeIncrements,
|
|
toSave,
|
|
} from "./wrapper";
|
|
|
|
const setup = Engine.parseMoves(`
|
|
init 2 randomSeed
|
|
p1 faction terrans
|
|
p2 faction nevlas
|
|
terrans build m -1x2
|
|
nevlas build m -1x0
|
|
nevlas build m 0x-4
|
|
terrans build m -4x-1
|
|
nevlas booster booster7
|
|
terrans booster booster3
|
|
`);
|
|
let request = 0;
|
|
function game() {
|
|
return new Engine(setup);
|
|
}
|
|
function command(engine: Engine, seat: number, moves: string[]): PremoveCommand {
|
|
return {
|
|
type: "premoves",
|
|
requestId: `test-${++request}`,
|
|
moves,
|
|
revision: engine.automation?.plans[seat]?.revision ?? 0,
|
|
round: engine.round,
|
|
turn: engine.automation?.turns[seat] ?? 0,
|
|
};
|
|
}
|
|
function queue(engine: Engine, seat: number, moves: string[]) {
|
|
return move(engine, command(engine, seat, moves), seat);
|
|
}
|
|
function json(engine: Engine): Engine {
|
|
return JSON.parse(JSON.stringify(engine));
|
|
}
|
|
|
|
describe("BGS premoves", () => {
|
|
it("keeps played, blocked and unplayed steps privately across refreshes and new queues", () => {
|
|
let engine = game();
|
|
engine.players[1].data.knowledge = 8;
|
|
const moves = ["nevlas up nav.", "nevlas up gaia.", "nevlas up eco.", "nevlas build ts -1x0."];
|
|
engine = move(engine, { ...command(engine, 1, moves), label: "Plan B" }, 1);
|
|
engine = move(json(engine), "terrans build ts -1x2.", 0);
|
|
engine = move(json(engine), "terrans pass booster8", 0);
|
|
const plan = engine.automation.plans[1];
|
|
expect(plan.moves).to.deep.equal([]);
|
|
expect(plan.runs[0].label).to.equal("Plan B");
|
|
expect(plan.runs[0].steps.map((step) => step.move)).to.deep.equal(moves);
|
|
expect(plan.runs[0].steps.map((step) => step.status)).to.deep.equal(["played", "played", "blocked", "unplayed"]);
|
|
expect(plan.runs[0].status).to.equal("stopped");
|
|
expect(plan.runs[0].reason).to.be.a("string").and.not.equal("");
|
|
for (const step of plan.runs[0].steps.filter((step) => step.status === "played")) {
|
|
expect(engine.moveHistory[step.moveIndex]).to.contain(step.move.split(".")[0]);
|
|
}
|
|
const snapshot = JSON.parse(JSON.stringify(plan.runs));
|
|
engine = Engine.fromData(json(engine));
|
|
automove(engine);
|
|
expect(engine.automation.plans[1].runs).to.deep.equal(snapshot);
|
|
expect(stripSecret(engine, 0).automation.plans).to.deep.equal({});
|
|
expect(stripSecret(engine).automation.plans).to.deep.equal({});
|
|
expect(stripSecret(engine, 1).automation.plans[1].runs).to.deep.equal(snapshot);
|
|
expect(logSlice(engine).state.automation.plans).to.deep.equal({});
|
|
engine = queue(engine, 1, ["nevlas pass booster3"]);
|
|
expect(engine.automation.plans[1].runs[1]).to.deep.equal(snapshot[0]);
|
|
expect(engine.automation.plans[1].runs[0].status).to.equal("completed");
|
|
});
|
|
|
|
it("marks taken-back premoves without deleting their remaining plan", async () => {
|
|
let engine = queue(game(), 1, ["nevlas up nav.", "nevlas up gaia."]);
|
|
engine = move(json(engine), "terrans build ts -1x2.", 0);
|
|
expect(engine.automation.plans[1].runs[0].steps[0].status).to.equal("played");
|
|
engine = await replay(engine, { to: setup.length });
|
|
expect(engine.automation.plans[1].moves).to.deep.equal([]);
|
|
expect(engine.automation.plans[1].runs[0].steps.map((step) => step.status)).to.deep.equal(["undone", "unplayed"]);
|
|
expect(engine.automation.plans[1].runs[0].reason).to.equal("The game was taken back.");
|
|
});
|
|
|
|
it("records cancellation and replacement without deleting the pending steps", () => {
|
|
let engine = queue(game(), 1, ["nevlas up nav.", "nevlas up gaia."]);
|
|
engine = queue(engine, 1, ["nevlas up eco."]);
|
|
expect(engine.automation.plans[1].runs[1].status).to.equal("replaced");
|
|
expect(engine.automation.plans[1].runs[1].steps.map((step) => step.status)).to.deep.equal(["unplayed", "unplayed"]);
|
|
engine = queue(engine, 1, []);
|
|
expect(engine.automation.plans[1].runs[0].status).to.equal("cancelled");
|
|
expect(engine.automation.plans[1].runs[0].steps[0].move).to.equal("nevlas up eco.");
|
|
expect(engine.automation.plans[1].moves).to.deep.equal([]);
|
|
});
|
|
|
|
it("adopts legacy queues and records manual interruptions and players leaving", () => {
|
|
let engine = queue(game(), 1, ["nevlas up nav."]);
|
|
delete engine.automation.plans[1].runs;
|
|
engine = move(json(engine), "terrans build ts -1x2.", 0);
|
|
expect(engine.automation.plans[1].runs[0].steps[0].status).to.equal("played");
|
|
expect(engine.automation.plans[1].runs[0].status).to.equal("completed");
|
|
|
|
engine = game();
|
|
automation(engine).plans[0] = { moves: ["terrans up nav."], revision: 1, round: 1 };
|
|
engine = move(engine, "terrans build ts -1x2.", 0);
|
|
expect(engine.automation.plans[0].runs[0].status).to.equal("stopped");
|
|
expect(engine.automation.plans[0].runs[0].reason).to.contain("manually");
|
|
|
|
engine = queue(game(), 1, ["nevlas up nav."]);
|
|
engine.players[1].dropped = true;
|
|
automove(engine);
|
|
expect(engine.automation.plans[1].runs[0].reason).to.equal("You left the game.");
|
|
expect(engine.automation.plans[1].runs[0].steps[0].status).to.equal("unplayed");
|
|
});
|
|
|
|
it("queues a second starting mine off turn without simulating other players in the real game", () => {
|
|
let engine = new Engine(setup.slice(0, 4));
|
|
const history = [...engine.moveHistory];
|
|
const queued = command(engine, 0, ["terrans build m -4x-1"]);
|
|
queued.timings = [{ round: 0, phase: Phase.SetupBuilding }];
|
|
expect(canMoveOutOfTurn(json(engine), queued, 0)).to.equal(true);
|
|
engine = move(json(engine), queued, 0);
|
|
expect(isLiveUpdate(engine)).to.equal(true);
|
|
expect(engine.moveHistory).to.deep.equal(history);
|
|
expect(timeIncrements(engine)).to.deep.equal([0, 0]);
|
|
expect(stripSecret(engine, 1).automation.plans).to.deep.equal({});
|
|
engine = move(json(engine), "nevlas build m -1x0", 1);
|
|
expect(engine.automation.plans[0].moves).to.have.length(1);
|
|
engine = move(json(engine), "nevlas build m 0x-4", 1);
|
|
expect(engine.moveHistory.slice(-1)).to.deep.equal(["terrans build m -4x-1"]);
|
|
expect(engine.automation.plans[0].moves).to.deep.equal([]);
|
|
expect(engine.phase).to.equal(Phase.SetupBooster);
|
|
expect(timeIncrements(engine)).to.deep.equal([1, 2]);
|
|
expect(engine.automation.turns).to.deep.equal([1, 2]);
|
|
automove(engine);
|
|
expect(timeIncrements(engine)).to.deep.equal([1, 2]);
|
|
});
|
|
|
|
it("can cancel a starting placement and rejects a submission stale after a manual placement", () => {
|
|
let engine = new Engine(setup.slice(0, 4));
|
|
const stale = command(engine, 0, ["terrans build m -4x-1"]);
|
|
engine = queue(engine, 0, ["terrans build m -4x-1"]);
|
|
engine = queue(engine, 0, []);
|
|
engine = move(json(engine), "nevlas build m -1x0", 1);
|
|
engine = move(json(engine), "nevlas build m 0x-4", 1);
|
|
expect(engine.playerToMove).to.equal(0);
|
|
expect(engine.players[0].data.occupied).to.have.length(1);
|
|
engine = move(json(engine), "terrans build m -4x-1", 0);
|
|
stale.revision = engine.automation.plans[0].revision;
|
|
stale.timings = [{ round: 0, phase: Phase.SetupBooster }];
|
|
stale.moves = ["terrans booster booster3"];
|
|
expect(() => move(json(engine), stale, 0)).to.throw("turn has changed");
|
|
});
|
|
|
|
it("stops an unavailable setup placement without making an alternative move", () => {
|
|
let engine = new Engine(setup.slice(0, 4));
|
|
engine = queue(engine, 0, ["terrans build m -1x2"]);
|
|
engine = move(json(engine), "nevlas build m -1x0", 1);
|
|
engine = move(json(engine), "nevlas build m 0x-4", 1);
|
|
expect(engine.playerToMove).to.equal(0);
|
|
expect(engine.players[0].data.occupied).to.have.length(1);
|
|
expect(engine.automation.plans[0].notice.kind).to.equal("stopped");
|
|
expect(engine.automation.plans[0].moves).to.deep.equal([]);
|
|
expect(timeIncrements(engine)).to.deep.equal([0, 2]);
|
|
});
|
|
|
|
it("waits for the initial booster pick after a queued mine, then rechecks availability", () => {
|
|
let engine = new Engine(setup.slice(0, 4));
|
|
const cmd = command(engine, 0, ["terrans build m -4x-1", "terrans booster booster7"]);
|
|
cmd.timings = [
|
|
{ round: 0, phase: Phase.SetupBuilding },
|
|
{ round: 0, phase: Phase.SetupBooster },
|
|
];
|
|
engine = move(json(engine), cmd, 0);
|
|
engine = move(json(engine), "nevlas build m -1x0", 1);
|
|
engine = move(json(engine), "nevlas build m 0x-4", 1);
|
|
expect(engine.automation.plans[0].moves).to.deep.equal(["terrans booster booster7"]);
|
|
engine = move(json(engine), "nevlas booster booster7", 1);
|
|
expect(engine.phase).to.equal(Phase.SetupBooster);
|
|
expect(engine.playerToMove).to.equal(0);
|
|
expect(engine.automation.plans[0].notice.kind).to.equal("stopped");
|
|
expect(engine.players[0].data.tiles.booster).to.equal(null);
|
|
});
|
|
|
|
it("keeps a setup queue through income into round one without adding invented setup moves", () => {
|
|
let engine = new Engine(setup.slice(0, 4));
|
|
const cmd = command(engine, 0, ["terrans build m -4x-1", "terrans booster booster3", "terrans up nav."]);
|
|
cmd.timings = [
|
|
{ round: 0, phase: Phase.SetupBuilding },
|
|
{ round: 0, phase: Phase.SetupBooster },
|
|
{ round: 1, phase: Phase.RoundMove },
|
|
];
|
|
engine = move(json(engine), cmd, 0);
|
|
engine = move(json(engine), "nevlas build m -1x0", 1);
|
|
engine = move(json(engine), "nevlas build m 0x-4", 1);
|
|
engine = move(json(engine), "nevlas booster booster7", 1);
|
|
expect(engine.phase).to.equal(Phase.RoundMove);
|
|
expect(engine.round).to.equal(1);
|
|
expect(engine.moveHistory).to.deep.equal(new Engine([...setup, "terrans up nav."]).moveHistory);
|
|
expect(engine.automation.plans[0].moves).to.deep.equal([]);
|
|
expect(timeIncrements(engine)).to.deep.equal([3, 3]);
|
|
});
|
|
|
|
it("rejects premoves before faction selection and setup timings in a running round", () => {
|
|
expect(() => queue(new Engine(setup.slice(0, 1)), 0, ["p1 faction terrans"])).to.throw("after faction selection");
|
|
const engine = new Engine(setup.slice(0, 4));
|
|
const cmd = command(engine, 0, ["terrans build m -4x-1"]);
|
|
cmd.timings = [{ round: 1, phase: Phase.SetupBuilding }];
|
|
expect(() => move(json(engine), cmd, 0)).to.throw("Invalid premove schedule");
|
|
cmd.timings = [{ round: 0, phase: Phase.RoundMove }];
|
|
expect(() => move(json(engine), cmd, 0)).to.throw("Invalid premove schedule");
|
|
});
|
|
|
|
it("keeps the 3c Trading Station constraint and executes after a real neighbour arrives", () => {
|
|
let engine = game();
|
|
Object.assign(engine.players[0].data, { ores: 20, credits: 20, qics: 10 });
|
|
setPlayerSettings(engine, 1, { autoCharge: "2" });
|
|
const credits = engine.players[1].data.credits;
|
|
engine = queue(engine, 1, ["nevlas build ts 4A11 cheap."]);
|
|
expect(engine.automation.plans[1].moves).to.deep.equal(["nevlas build ts 4A11 cheap."]);
|
|
expect(engine.players[1].data.credits).to.equal(credits);
|
|
engine = move(json(engine), "terrans build m 4B0.", 0);
|
|
expect(engine.automation.plans[1].notice.kind).to.equal("played");
|
|
expect(engine.players[1].data.credits).to.equal(credits - 3);
|
|
expect(timeIncrements(engine)).to.deep.equal([1, 1]);
|
|
});
|
|
|
|
it("stops an isolated 3c-only upgrade atomically instead of spending 6c", () => {
|
|
let engine = game();
|
|
const credits = engine.players[1].data.credits;
|
|
const qics = engine.players[1].data.qics;
|
|
const ores = engine.players[1].data.ores;
|
|
expect(credits).to.be.at.least(6);
|
|
engine = queue(engine, 1, ["nevlas spend 1q for 1o. build ts 4A11 cheap."]);
|
|
engine = move(json(engine), "terrans up nav.", 0);
|
|
expect(engine.automation.plans[1].notice.kind).to.equal("stopped");
|
|
expect(engine.automation.plans[1].notice.text).to.contain("not available for 3c");
|
|
expect(engine.automation.plans[1].moves).to.deep.equal([]);
|
|
expect(engine.players[1].data.credits).to.equal(credits);
|
|
expect(engine.players[1].data.qics).to.equal(qics);
|
|
expect(engine.players[1].data.ores).to.equal(ores);
|
|
expect(timeIncrements(engine)).to.deep.equal([1, 0]);
|
|
});
|
|
|
|
it("can queue a power spend before a charge, then uses real autoleech when the turn arrives", () => {
|
|
let engine = game();
|
|
Object.assign(engine.players[1].data.power, { area1: 0, area2: 1, area3: 0 });
|
|
setPlayerSettings(engine, 1, { autoCharge: "2" });
|
|
const credits = engine.players[1].data.credits;
|
|
engine = queue(engine, 1, ["nevlas spend 1pw for 1c. up nav."]);
|
|
expect(engine.players[1].data.power.area3).to.equal(0);
|
|
expect(engine.players[1].data.credits).to.equal(credits);
|
|
expect(engine.players[1].data.analysis).to.equal(false);
|
|
expect(timeIncrements(engine)).to.deep.equal([0, 0]);
|
|
engine = move(json(engine), "terrans build ts -1x2.", 0);
|
|
expect(engine.automation.plans[1].notice.kind).to.equal("played");
|
|
expect(engine.players[1].data.credits).to.equal(credits + 1);
|
|
expect(engine.players[1].data.power.area3).to.equal(0);
|
|
expect(timeIncrements(engine)).to.deep.equal([1, 1]);
|
|
});
|
|
|
|
it("stops instead of waiting or fabricating power when the anticipated charge never arrives", () => {
|
|
let engine = game();
|
|
Object.assign(engine.players[1].data.power, { area1: 0, area2: 1, area3: 0 });
|
|
const credits = engine.players[1].data.credits;
|
|
engine = queue(engine, 1, ["nevlas spend 1pw for 1c. up nav."]);
|
|
engine = move(json(engine), "terrans up nav.", 0);
|
|
expect(engine.playerToMove).to.equal(1);
|
|
expect(engine.automation.plans[1].notice.kind).to.equal("stopped");
|
|
expect(engine.automation.plans[1].moves).to.deep.equal([]);
|
|
expect(engine.players[1].data.power.area3).to.equal(0);
|
|
expect(engine.players[1].data.credits).to.equal(credits);
|
|
expect(timeIncrements(engine)).to.deep.equal([1, 0]);
|
|
});
|
|
|
|
it("still rejects an unaffordable move submitted to play immediately", () => {
|
|
const engine = game();
|
|
engine.players[0].data.knowledge = 0;
|
|
expect(() => queue(engine, 0, ["terrans up nav."])).to.throw();
|
|
expect(engine.players[0].data.knowledge).to.equal(0);
|
|
});
|
|
|
|
it("saves a private off-turn queue without changing turns, logs or clocks", () => {
|
|
let engine = game();
|
|
const before = engine.moveHistory.length;
|
|
const cmd = command(engine, 1, ["nevlas up nav."]);
|
|
expect(canMoveOutOfTurn(json(engine), cmd, 1)).to.equal(true);
|
|
engine = move(json(engine), cmd, 1);
|
|
expect(toSave(engine)).to.equal(engine);
|
|
expect(isLiveUpdate(engine)).to.equal(true);
|
|
expect(engine.playerToMove).to.equal(0);
|
|
expect(engine.moveHistory.length).to.equal(before);
|
|
expect(timeIncrements(engine)).to.deep.equal([0, 0]);
|
|
expect(stripSecret(engine, 1).automation.plans[1].moves).to.deep.equal(cmd.moves);
|
|
expect(stripSecret(engine, 0).automation.plans).to.deep.equal({});
|
|
expect(logSlice(engine).state.automation.plans).to.deep.equal({});
|
|
});
|
|
it("runs the queue after auto-leech and awards each completed main turn exactly once", () => {
|
|
let engine = queue(game(), 1, ["nevlas up nav."]);
|
|
engine = move(json(engine), "terrans build ts -1x2.", 0);
|
|
expect(engine.moveHistory.slice(-3)[1]).to.match(/^nevlas charge/);
|
|
expect(engine.moveHistory.slice(-1)[0]).to.match(/^nevlas up nav/);
|
|
expect(timeIncrements(engine)).to.deep.equal([1, 1]);
|
|
expect(isLiveUpdate(engine)).to.equal(false);
|
|
expect(engine.automation.plans[1].moves).to.deep.equal([]);
|
|
automove(engine);
|
|
expect(timeIncrements(engine)).to.deep.equal([1, 1]);
|
|
});
|
|
it("keeps a queue through a manual charge decision and executes after it", () => {
|
|
let engine = game();
|
|
engine.move("terrans build ts -1x2.");
|
|
automove(engine);
|
|
engine.move("nevlas build ts -1x0.");
|
|
expect(engine.phase).to.equal(Phase.RoundLeech);
|
|
engine = queue(engine, 0, ["terrans up nav."]);
|
|
expect(engine.automation.plans[0].moves).to.have.length(1);
|
|
expect(timeIncrements(engine)).to.deep.equal([0, 0]);
|
|
engine = move(json(engine), "terrans charge 2pw", 0);
|
|
expect(engine.automation.plans[0].moves).to.have.length(0);
|
|
expect(timeIncrements(engine)).to.deep.equal([2, 0]);
|
|
expect(engine.automation.turns).to.deep.equal([1, 0]);
|
|
});
|
|
it("runs several seats' queues, with autoleech between them", () => {
|
|
let engine = queue(game(), 1, ["nevlas build ts -1x0."]);
|
|
setPlayerSettings(engine, 0, { autoCharge: "2" });
|
|
engine = queue(engine, 0, ["terrans build ts -1x2.", "terrans up nav."]);
|
|
expect(engine.automation.plans[0].moves).to.have.length(0);
|
|
expect(engine.automation.plans[1].moves).to.have.length(0);
|
|
expect(timeIncrements(engine)).to.deep.equal([2, 1]);
|
|
});
|
|
it("stops an illegal chain atomically, leaving the resources and active turn intact", () => {
|
|
let engine = queue(game(), 1, ["nevlas up nav.", "nevlas build ts -1x0."]);
|
|
engine.players[1].data.knowledge = 0;
|
|
const wallet = engine.players[1].data.ores;
|
|
engine = move(json(engine), "terrans up nav.", 0);
|
|
expect(engine.playerToMove).to.equal(1);
|
|
expect(engine.players[1].data.ores).to.equal(wallet);
|
|
expect(engine.automation.plans[1].moves).to.deep.equal([]);
|
|
expect(engine.automation.plans[1].notice.kind).to.equal("stopped");
|
|
expect(timeIncrements(engine)).to.deep.equal([1, 0]);
|
|
});
|
|
it("rejects another seat's moves, incomplete plans, stale turns and stale queue edits", () => {
|
|
const engine = game();
|
|
expect(() => queue(engine, 1, ["terrans up nav."])).to.throw("own moves");
|
|
expect(() => queue(engine, 1, ["nevlas burn 1"])).to.throw();
|
|
const stale = command(engine, 1, ["nevlas up nav."]);
|
|
queue(engine, 1, ["nevlas up terra."]);
|
|
expect(() => move(engine, stale, 1)).to.throw("queue changed");
|
|
const turn = command(engine, 1, ["nevlas up nav."]);
|
|
automation(engine).turns[1]++;
|
|
expect(() => move(engine, turn, 1)).to.throw("turn has changed");
|
|
expect(canMoveOutOfTurn(engine, "nevlas up nav.", 1)).to.equal(false);
|
|
expect(canMoveOutOfTurn(engine, stale, -1)).to.equal(false);
|
|
});
|
|
it("makes duplicate submissions harmless, even after the move has executed", () => {
|
|
let engine = game();
|
|
const cmd = command(engine, 0, ["terrans up nav."]);
|
|
engine = move(engine, cmd, 0);
|
|
const history = [...engine.moveHistory];
|
|
engine = move(json(engine), cmd, 0);
|
|
expect(engine.moveHistory).to.deep.equal(history);
|
|
expect(timeIncrements(engine)).to.deep.equal([1, 0]);
|
|
expect(engine.automation.plans[0].moves).to.have.length(0);
|
|
});
|
|
|
|
it("rolls back free conversions when a later command in the same premove becomes illegal", () => {
|
|
let engine = queue(game(), 1, ["nevlas spend 1q for 1o. up nav."]);
|
|
engine.players[1].data.knowledge = 0;
|
|
const before = [engine.players[1].data.qics, engine.players[1].data.ores];
|
|
engine = move(json(engine), "terrans up nav.", 0);
|
|
expect([engine.players[1].data.qics, engine.players[1].data.ores]).to.deep.equal(before);
|
|
expect(engine.automation.plans[1].notice.kind).to.equal("stopped");
|
|
});
|
|
|
|
it("allows cancellation even when the retained prefix is no longer legal", () => {
|
|
let engine = queue(game(), 1, ["nevlas up nav.", "nevlas build ts -1x0."]);
|
|
engine.players[1].data.knowledge = 0;
|
|
engine = queue(engine, 1, ["nevlas up nav."]);
|
|
expect(engine.automation.plans[1].moves).to.deep.equal(["nevlas up nav."]);
|
|
engine = queue(engine, 1, []);
|
|
expect(engine.automation.plans[1].moves).to.deep.equal([]);
|
|
expect(timeIncrements(engine)).to.deep.equal([0, 0]);
|
|
});
|
|
|
|
it("keeps a queued move after a pass and executes it in the next round", () => {
|
|
let engine = queue(game(), 0, ["terrans pass booster4", "terrans up nav."]);
|
|
expect(engine.round).to.equal(1);
|
|
expect(engine.automation.plans[0].moves).to.deep.equal(["terrans up nav."]);
|
|
expect(timeIncrements(engine)).to.deep.equal([1, 0]);
|
|
engine = move(json(engine), "nevlas pass booster5", 1);
|
|
expect(engine.round).to.equal(2);
|
|
expect(engine.automation.plans[0].moves).to.deep.equal([]);
|
|
expect(engine.players[0].data.research.nav).to.equal(1);
|
|
expect(timeIncrements(engine)).to.deep.equal([2, 1]);
|
|
});
|
|
|
|
it("can queue after already passing without reopening the current round", () => {
|
|
let engine = move(game(), "terrans pass booster4", 0);
|
|
const before = [...engine.moveHistory];
|
|
engine = queue(engine, 0, ["terrans up nav."]);
|
|
expect(engine.moveHistory).to.deep.equal(before);
|
|
expect(isLiveUpdate(engine)).to.equal(true);
|
|
expect(engine.automation.plans[0].timings).to.deep.equal([{ round: 2, phase: Phase.RoundMove }]);
|
|
engine = move(json(engine), "nevlas pass booster5", 1);
|
|
expect(engine.automation.plans[0].moves).to.deep.equal([]);
|
|
expect(timeIncrements(engine)).to.deep.equal([2, 1]);
|
|
});
|
|
|
|
it("queues and executes more than three moves without losing the tail", () => {
|
|
let engine = game();
|
|
engine.players[1].data.knowledge = 12;
|
|
const moves = ["nevlas up nav.", "nevlas up terra.", "nevlas up int.", "nevlas pass booster3"];
|
|
engine = queue(engine, 1, moves);
|
|
expect(stripSecret(engine, 1).automation.plans[1].moves).to.deep.equal(moves);
|
|
expect(engine.moveHistory).to.deep.equal(game().moveHistory);
|
|
|
|
engine = move(json(engine), "terrans pass booster4", 0);
|
|
expect(engine.automation.plans[1].moves).to.deep.equal([]);
|
|
expect(engine.automation.plans[1].notice.kind).to.equal("played");
|
|
for (const planned of moves) {
|
|
expect(engine.moveHistory.some((move) => move.startsWith(planned.replace(/\.$/, "")))).to.equal(true);
|
|
}
|
|
expect(engine.players[1].data.research.nav).to.equal(1);
|
|
expect(engine.players[1].data.research.terra).to.equal(1);
|
|
expect(engine.players[1].data.research.int).to.equal(1);
|
|
expect(engine.round).to.equal(2);
|
|
});
|
|
|
|
const passedItarsGame = () => {
|
|
const engine = new Engine([
|
|
"init 2 Gaudy-plow-4506",
|
|
"p1 faction itars",
|
|
"p2 faction ivits",
|
|
"itars build m 3B3",
|
|
"itars build m 2B2",
|
|
"ivits build PI 4B3",
|
|
"ivits booster booster3",
|
|
"itars booster booster8",
|
|
"ivits income t",
|
|
"itars pass booster9",
|
|
]);
|
|
engine.players.forEach((player) => {
|
|
player.settings.autoIncome = false;
|
|
// Keep a real trade-off: maximising usable power can waste a charge.
|
|
// Income with a dominant order now resolves even without autoIncome.
|
|
player.data.power = new Power();
|
|
player.events[Operator.Income] = player.events[Operator.Income].filter(
|
|
(event) => !event.rewards.some((reward) => [Resource.GainToken, Resource.ChargePower].includes(reward.type))
|
|
);
|
|
player.loadEvents(Event.parse(["+2t", "+2t", "+2pw", "+3pw"], null));
|
|
});
|
|
return engine;
|
|
};
|
|
|
|
it("queues next-round income and a main turn while waiting for the opponent's manual income", () => {
|
|
let engine = passedItarsGame();
|
|
engine = move(
|
|
engine,
|
|
{
|
|
...command(engine, 0, ["itars income 2t,2t", "itars up nav."]),
|
|
timings: [
|
|
{ round: 2, phase: Phase.RoundIncome },
|
|
{ round: 2, phase: Phase.RoundMove },
|
|
],
|
|
},
|
|
0
|
|
);
|
|
expect(isLiveUpdate(engine)).to.equal(true);
|
|
engine = move(json(engine), "ivits pass booster8", 1);
|
|
expect(engine.phase).to.equal(Phase.RoundIncome);
|
|
expect(engine.playerToMove).to.equal(1);
|
|
expect(engine.automation.plans[0].moves).to.deep.equal(["itars up nav."]);
|
|
expect(timeIncrements(engine)).to.deep.equal([1, 1]);
|
|
engine = move(json(engine), "ivits income 2t,2t", 1);
|
|
expect(engine.automation.plans[0].moves).to.deep.equal([]);
|
|
expect(engine.players[0].data.research.nav).to.equal(1);
|
|
expect(timeIncrements(engine)).to.deep.equal([2, 2]);
|
|
});
|
|
|
|
it("waits for an unplanned manual income choice without clearing next-round moves", () => {
|
|
let engine = queue(passedItarsGame(), 0, ["itars up nav."]);
|
|
engine = move(json(engine), "ivits pass booster8", 1);
|
|
expect(engine.phase).to.equal(Phase.RoundIncome);
|
|
expect(engine.playerToMove).to.equal(0);
|
|
expect(engine.automation.plans[0].moves).to.have.length(1);
|
|
engine = move(json(engine), "itars income 2t,2t", 0);
|
|
expect(engine.automation.plans[0].moves).to.have.length(1);
|
|
engine = move(json(engine), "ivits income 2t,2t", 1);
|
|
expect(engine.automation.plans[0].moves).to.have.length(0);
|
|
expect(timeIncrements(engine)).to.deep.equal([2, 2]);
|
|
});
|
|
|
|
it("does not replay or credit income that automatic income has already resolved", () => {
|
|
let engine = passedItarsGame();
|
|
engine.players.forEach((player) => {
|
|
player.settings.autoIncome = true;
|
|
});
|
|
engine = move(
|
|
engine,
|
|
{
|
|
...command(engine, 0, ["itars income 2t,2t", "itars up nav."]),
|
|
timings: [
|
|
{ round: 2, phase: Phase.RoundIncome },
|
|
{ round: 2, phase: Phase.RoundMove },
|
|
],
|
|
},
|
|
0
|
|
);
|
|
engine = move(json(engine), "ivits pass booster8", 1);
|
|
expect(engine.automation.plans[0].moves).to.deep.equal([]);
|
|
expect(engine.players[0].data.research.nav).to.equal(1);
|
|
expect(engine.automation.plans[0].runs[0].steps.map((step) => step.status)).to.deep.equal(["skipped", "played"]);
|
|
expect(timeIncrements(engine)).to.deep.equal([1, 1]);
|
|
});
|
|
|
|
it("can cancel next-round moves and rejects schedules that go backwards", () => {
|
|
let engine = queue(passedItarsGame(), 0, ["itars up nav."]);
|
|
engine = queue(engine, 0, []);
|
|
engine = move(json(engine), "ivits pass booster8", 1);
|
|
engine = move(json(engine), "itars income 2t,2t", 0);
|
|
engine = move(json(engine), "ivits income 2t,2t", 1);
|
|
expect(engine.players[0].data.research.nav).to.equal(0);
|
|
expect(() =>
|
|
move(
|
|
engine,
|
|
{
|
|
...command(engine, 0, ["itars up nav.", "itars up terra."]),
|
|
timings: [
|
|
{ round: 3, phase: Phase.RoundMove },
|
|
{ round: 2, phase: Phase.RoundMove },
|
|
],
|
|
},
|
|
0
|
|
)
|
|
).to.throw("round order");
|
|
});
|
|
it("preserves increment totals and clears plans when replaying an existing game", async () => {
|
|
let engine = queue(game(), 1, ["nevlas up nav."]);
|
|
engine = move(engine, "terrans up nav.", 0);
|
|
const counts = timeIncrements(engine);
|
|
engine = await replay(json(engine), { to: setup.length });
|
|
expect(timeIncrements(engine)).to.deep.equal(counts);
|
|
expect(engine.automation.plans[1].moves).to.deep.equal([]);
|
|
expect(engine.automation.plans[1].runs[0].status).to.equal("stopped");
|
|
});
|
|
});
|
|
|
|
describe("planned power safety", () => {
|
|
it("preserves baseline alignment through prefix cancellation and private history", () => {
|
|
const engine = game();
|
|
const moves = ["nevlas up nav.", "nevlas up gaia."];
|
|
const preview = Engine.fromData(json(engine));
|
|
preview.forcePremovePreviewTurn(1);
|
|
const power = [capturePremovePower(preview, 1, moves[0])];
|
|
preview.forcePremovePreviewTurn(1);
|
|
preview.players[1].data.knowledge = 4;
|
|
power.push(capturePremovePower(preview, 1, moves[1]));
|
|
setPremoves(engine, { ...command(engine, 1, moves), power }, 1);
|
|
setPremoves(engine, command(engine, 1, moves.slice(0, 1)), 1);
|
|
expect(engine.automation.plans[1].power).to.deep.equal(power.slice(0, 1));
|
|
expect(engine.automation.plans[1].runs[0].steps[0].power).to.deep.equal(power[0]);
|
|
expect(stripSecret(engine, 0).automation.plans).to.deep.equal({});
|
|
engine.forcePremovePreviewTurn(1);
|
|
expect(playNextPremove(engine)).to.equal(true);
|
|
expect(engine.automation.plans[1].power).to.deep.equal([]);
|
|
expect(engine.automation.plans[1].runs[0].steps[0].power).to.deep.equal(power[0]);
|
|
});
|
|
|
|
for (const originallyFull of [false, true]) {
|
|
it(`checks original charge waste before any live command (originally full: ${originallyFull})`, () => {
|
|
const engine = game();
|
|
engine.players[0].data.research.nav = 2;
|
|
Object.assign(engine.players[0].data.power, {
|
|
area1: originallyFull ? 0 : 3,
|
|
area2: 0,
|
|
area3: originallyFull ? 3 : 0,
|
|
});
|
|
const planned = "terrans spend 1q for 1o. up nav.";
|
|
const preview = Engine.fromData(json(engine));
|
|
const power = [capturePremovePower(preview, 0, planned)];
|
|
setPremoves(engine, { ...command(engine, 0, [planned]), power }, 0);
|
|
const saved = Engine.fromData(json(engine));
|
|
expect(saved.automation.plans[0].power).to.deep.equal(power);
|
|
saved.players[0].data.chargePower(6);
|
|
const before = JSON.stringify(saved.players[0].data);
|
|
const history = saved.moveHistory.length;
|
|
expect(playNextPremove(saved)).to.equal(originallyFull);
|
|
if (!originallyFull) {
|
|
expect(JSON.stringify(saved.players[0].data)).to.equal(before);
|
|
expect(saved.moveHistory.length).to.equal(history);
|
|
expect(saved.automation.plans[0].notice.text).to.contain("waste more power");
|
|
}
|
|
});
|
|
}
|
|
|
|
it("rejects malformed baselines and stops risky legacy plans rather than inventing intent", () => {
|
|
const engine = game();
|
|
engine.players[0].data.research.nav = 2;
|
|
Object.assign(engine.players[0].data.power, { area1: 0, area2: 0, area3: 3 });
|
|
const request = command(engine, 0, ["terrans up nav."]);
|
|
expect(() =>
|
|
setPremoves(
|
|
engine,
|
|
{ ...request, power: [[{ command: "up nav", events: [{ kind: "charge", waste: -1, area2: 0, area3: 0 }] }]] },
|
|
0
|
|
)
|
|
).to.throw("Invalid premove power");
|
|
setPremoves(engine, request, 0);
|
|
expect(playNextPremove(engine)).to.equal(false);
|
|
expect(engine.automation.plans[0].notice.text).to.contain("older plan");
|
|
});
|
|
});
|