Files
OpenFrontIO/tests/core/executions/NukeExecution.test.ts
T
Evan Pelle a7f992e9b0 refactor: convert to npm-workspaces monorepo (engine/core-public/shared/client/server)
Restructure the single src/ tree into an npm-workspaces monorepo under
packages/, rename core -> engine, extract a types-only core-public layer,
and break the pre-existing engine -> client dependency cycle.

Structure (packages/):
  core-public  public API/wire schemas + shared enums (clean leaf)
  shared       framework-agnostic helpers (clean leaf)
  engine       deterministic simulation (was src/core)
  client       rendering/UI (was src/client)
  server       coordination (was src/server)

Dependency DAG: engine -> {core-public, shared}; client -> {core-public,
shared, engine}; server -> {core-public, engine}.

- npm workspaces: root package.json workspaces + per-package package.json;
  tsconfig.base.json holds shared options + path aliases
  (core-public/* shared/* engine/* client/* server/*) resolved uniformly by
  tsc, Vite (resolve.tsconfigPaths), Vitest, and tsx. Lockfile regenerated.
- core-public: moved Schemas/ApiSchemas/CosmeticSchemas/StatsSchemas/
  ClanApiSchemas/WorkerSchemas/Base64/PatternDecoder; extracted the enums
  (GameTypes), GameEvent type, emoji table, and GraphicsOverrides schema.
  Engine re-exports the moved enums/types so existing imports keep working.
- Broke engine -> client cycle:
  - renderNumber/renderTroops -> shared/format
  - NameBoxCalculator moved into engine
  - username validation returns translation key + params; client translates
  - applyStateUpdate moved to client (operates on the render-only PlayerState)
  - Config/UnitGrid/execution-Util/GameImpl now use structural read
    interfaces (engine/game/ReadViews: PlayerLike/UnitLike/GameLike) instead
    of importing client view classes; client imports view classes from a new
    client/view barrel; deleted the engine/game/GameView re-export shim.
- Build/deploy updated: vite.config, index.html, eslint, Dockerfile
  (copies packages/ + tsconfig.base.json before npm ci), .vscode, tests.

Verified: tsc --noEmit clean; 1364 + 65 tests pass; production vite build
succeeds; engine has zero client/server imports; core-public and shared are
dependency leaves.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-10 16:51:03 +00:00

248 lines
7.6 KiB
TypeScript

import { NukeExecution } from "engine/execution/NukeExecution";
import {
Game,
MessageType,
Player,
PlayerInfo,
PlayerType,
UnitType,
} from "engine/game/Game";
import { setup } from "../../util/Setup";
import { TestConfig } from "../../util/TestConfig";
import { executeTicks } from "../../util/utils";
let game: Game;
let player: Player;
let otherPlayer: Player;
describe("NukeExecution", () => {
beforeEach(async () => {
game = await setup(
"big_plains",
{ infiniteGold: true, instantBuild: true },
[
new PlayerInfo("player", PlayerType.Human, "client_id1", "player_id"),
new PlayerInfo("other", PlayerType.Human, "client_id2", "other_id"),
],
);
(game.config() as TestConfig).nukeMagnitudes = vi.fn(() => ({
inner: 10,
outer: 10,
}));
(game.config() as TestConfig).nukeAllianceBreakThreshold = vi.fn(() => 5);
player = game.player("player_id");
otherPlayer = game.player("other_id");
player.conquer(game.ref(1, 1));
});
test("nuke should destroy buildings and redraw out of range buildings", async () => {
// Build a city at (1,1)
player.buildUnit(UnitType.City, game.ref(1, 1), {});
// Build a missile silo in range
player.buildUnit(UnitType.MissileSilo, game.ref(1, 10), {});
// Build a SAM out of range
const sam = player.buildUnit(UnitType.SAMLauncher, game.ref(1, 11), {});
sam.touch = vi.fn();
// Build a Defense post out of range AND out of redraw range
const defensePost = player.buildUnit(
UnitType.DefensePost,
game.ref(1, 27),
{},
);
defensePost.touch = vi.fn();
// Add a nuke execution targeting the city
const nukeExec = new NukeExecution(
UnitType.AtomBomb,
player,
game.ref(1, 1),
game.ref(1, 2),
);
game.addExecution(nukeExec);
// Run enough ticks for the nuke to detonate
executeTicks(game, 10);
// The city and silo should be destroyed
expect(player.units(UnitType.City)).toHaveLength(0);
expect(player.units(UnitType.MissileSilo)).toHaveLength(0);
expect(player.units(UnitType.SAMLauncher)).toHaveLength(1);
expect(sam.touch).toHaveBeenCalled();
expect(defensePost.touch).not.toHaveBeenCalled();
});
test("nuke should only be targetable near src and dst", async () => {
player.buildUnit(UnitType.MissileSilo, game.ref(1, 1), {});
const nukeExec = new NukeExecution(
UnitType.AtomBomb,
player,
game.ref(199, 199),
game.ref(1, 1),
);
game.addExecution(nukeExec);
// targetable distance is 400
//near launch should be targetable (distance src < 400)
executeTicks(game, 2);
expect(nukeExec.getNuke()!.isTargetable()).toBeTruthy();
//mid air should not be targetable (distance src > 400, distance target > 400)
executeTicks(game, 38);
expect(nukeExec.getNuke()!.isTargetable()).toBeFalsy();
//near target should be targetable (distance target < 400)
executeTicks(game, 35);
expect(nukeExec.getNuke()!.isTargetable()).toBeTruthy();
});
test("nuke should break alliances on launch", async () => {
const req = player.createAllianceRequest(otherPlayer);
req!.accept();
player.conquer(game.ref(1, 1));
player.buildUnit(UnitType.MissileSilo, game.ref(1, 1), {});
for (let x = 90; x < 99; x++) {
for (let y = 90; y < 99; y++) {
otherPlayer.conquer(game.ref(x, y));
}
}
// Add a nuke targeting just outside the other player's territory.
game.addExecution(
new NukeExecution(UnitType.AtomBomb, player, game.ref(85, 85), null),
);
game.executeNextTick(); // init
game.executeNextTick(); // exec
expect(player.isTraitor()).toBe(true);
expect(player.isAlliedWith(otherPlayer)).toBe(false);
});
test("AtomBomb detonation emits NUKE_DETONATED to each impacted player", () => {
player.buildUnit(UnitType.MissileSilo, game.ref(1, 1), {});
// Give otherPlayer a cluster around (50,50) so the blast intersects them.
for (let x = 48; x < 53; x++) {
for (let y = 48; y < 53; y++) {
otherPlayer.conquer(game.ref(x, y));
}
}
const displayMessageSpy = vi.spyOn(game, "displayMessage");
game.addExecution(
new NukeExecution(
UnitType.AtomBomb,
player,
game.ref(50, 50),
game.ref(1, 1),
),
);
executeTicks(game, 200);
const detonatedCalls = displayMessageSpy.mock.calls.filter(
(call) => call[1] === MessageType.NUKE_DETONATED,
);
expect(detonatedCalls.length).toBeGreaterThan(0);
const otherCall = detonatedCalls.find(
(call) => call[2] === otherPlayer.id(),
);
expect(otherCall).toBeDefined();
expect(otherCall![0]).toBe("events_display.atom_bomb_detonated");
// focusPlayerID (7th positional) is the launcher
expect(otherCall![6]).toBe(player.id());
displayMessageSpy.mockRestore();
});
test("HydrogenBomb detonation emits NUKE_DETONATED with hydrogen_bomb key", () => {
player.buildUnit(UnitType.MissileSilo, game.ref(1, 1), {});
for (let x = 48; x < 53; x++) {
for (let y = 48; y < 53; y++) {
otherPlayer.conquer(game.ref(x, y));
}
}
const displayMessageSpy = vi.spyOn(game, "displayMessage");
game.addExecution(
new NukeExecution(
UnitType.HydrogenBomb,
player,
game.ref(50, 50),
game.ref(1, 1),
),
);
executeTicks(game, 300);
const detonatedCalls = displayMessageSpy.mock.calls.filter(
(call) => call[1] === MessageType.NUKE_DETONATED,
);
expect(detonatedCalls.length).toBeGreaterThan(0);
expect(detonatedCalls[0][0]).toBe("events_display.hydrogen_bomb_detonated");
displayMessageSpy.mockRestore();
});
test("MIRVWarhead detonation does NOT emit NUKE_DETONATED", () => {
player.buildUnit(UnitType.MissileSilo, game.ref(1, 1), {});
for (let x = 48; x < 53; x++) {
for (let y = 48; y < 53; y++) {
otherPlayer.conquer(game.ref(x, y));
}
}
const displayMessageSpy = vi.spyOn(game, "displayMessage");
game.addExecution(
new NukeExecution(
UnitType.MIRVWarhead,
player,
game.ref(50, 50),
game.ref(1, 1),
),
);
executeTicks(game, 200);
const detonatedCalls = displayMessageSpy.mock.calls.filter(
(call) => call[1] === MessageType.NUKE_DETONATED,
);
expect(detonatedCalls).toHaveLength(0);
displayMessageSpy.mockRestore();
});
test("nuke should break alliance when destroying ally's building even with few tiles", async () => {
const req = player.createAllianceRequest(otherPlayer);
req!.accept();
expect(player.isAlliedWith(otherPlayer)).toBe(true);
player.conquer(game.ref(1, 1));
player.buildUnit(UnitType.MissileSilo, game.ref(1, 1), {});
// Give the other player just a few tiles (below the threshold of 5)
// and build a port on one of them
otherPlayer.conquer(game.ref(50, 50));
otherPlayer.conquer(game.ref(51, 50));
otherPlayer.conquer(game.ref(50, 51));
otherPlayer.buildUnit(UnitType.Port, game.ref(50, 50), {});
expect(otherPlayer.units(UnitType.Port)).toHaveLength(1);
// Nuke targeting the ally's port - this should break alliance
// even though the tile count is below threshold
game.addExecution(
new NukeExecution(UnitType.AtomBomb, player, game.ref(50, 50), null),
);
game.executeNextTick(); // init
game.executeNextTick(); // exec
// Alliance should be broken because we're destroying ally's building
expect(player.isTraitor()).toBe(true);
expect(player.isAlliedWith(otherPlayer)).toBe(false);
});
});