Files
OpenFrontIO/tests/core/executions/SAMLauncherExecution.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

272 lines
8.8 KiB
TypeScript

import { NukeExecution } from "engine/execution/NukeExecution";
import { SAMLauncherExecution } from "engine/execution/SAMLauncherExecution";
import { SpawnExecution } from "engine/execution/SpawnExecution";
import { UpgradeStructureExecution } from "engine/execution/UpgradeStructureExecution";
import {
Game,
Player,
PlayerInfo,
PlayerType,
UnitType,
} from "engine/game/Game";
import { GameID } from "core-public/Schemas";
import { setup } from "../../util/Setup";
import { constructionExecution, executeTicks } from "../../util/utils";
let game: Game;
const gameID: GameID = "game_id";
let attacker: Player;
let defender: Player;
let far_defender: Player;
let middle_defender: Player;
describe("SAM", () => {
beforeEach(async () => {
game = await setup("big_plains", {
infiniteGold: true,
instantBuild: true,
});
const defender_info = new PlayerInfo(
"defender_id",
PlayerType.Human,
null,
"defender_id",
);
const middle_defender_info = new PlayerInfo(
"middle_defender_id",
PlayerType.Human,
null,
"middle_defender_id",
);
const far_defender_info = new PlayerInfo(
"far_defender_id",
PlayerType.Human,
null,
"far_defender_id",
);
const attacker_info = new PlayerInfo(
"attacker_id",
PlayerType.Human,
null,
"attacker_id",
);
game.addPlayer(defender_info);
game.addPlayer(middle_defender_info);
game.addPlayer(far_defender_info);
game.addPlayer(attacker_info);
game.addExecution(
new SpawnExecution(
gameID,
game.player(defender_info.id).info(),
game.ref(1, 1),
),
new SpawnExecution(
gameID,
game.player(middle_defender_info.id).info(),
game.ref(50, 1),
),
new SpawnExecution(
gameID,
game.player(far_defender_info.id).info(),
game.ref(199, 1),
),
new SpawnExecution(
gameID,
game.player(attacker_info.id).info(),
game.ref(7, 7),
),
);
attacker = game.player("attacker_id");
defender = game.player("defender_id");
middle_defender = game.player("middle_defender_id");
far_defender = game.player("far_defender_id");
constructionExecution(game, attacker, 7, 7, UnitType.MissileSilo);
});
test("one sam should take down one nuke", async () => {
const sam = defender.buildUnit(UnitType.SAMLauncher, game.ref(1, 1), {});
game.addExecution(new SAMLauncherExecution(defender, null, sam));
// Sam will only target nukes it can destroy before it reaches its target
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const nuke = attacker.buildUnit(UnitType.AtomBomb, game.ref(1, 1), {
targetTile: game.ref(3, 1),
trajectory: [
{ tile: game.ref(1, 1), targetable: true },
{ tile: game.ref(2, 1), targetable: true },
{ tile: game.ref(3, 1), targetable: true },
],
});
executeTicks(game, 3);
expect(attacker.units(UnitType.AtomBomb)).toHaveLength(0);
});
test("sam should only get one nuke at a time", async () => {
const sam = defender.buildUnit(UnitType.SAMLauncher, game.ref(1, 1), {});
game.addExecution(new SAMLauncherExecution(defender, null, sam));
attacker.buildUnit(UnitType.AtomBomb, game.ref(2, 1), {
targetTile: game.ref(3, 1),
trajectory: [
{ tile: game.ref(1, 1), targetable: true },
{ tile: game.ref(2, 1), targetable: true },
{ tile: game.ref(3, 1), targetable: true },
],
});
attacker.buildUnit(UnitType.AtomBomb, game.ref(1, 2), {
targetTile: game.ref(1, 3),
trajectory: [
{ tile: game.ref(1, 1), targetable: true },
{ tile: game.ref(1, 2), targetable: true },
{ tile: game.ref(1, 3), targetable: true },
],
});
expect(attacker.units(UnitType.AtomBomb)).toHaveLength(2);
executeTicks(game, 3);
expect(attacker.units(UnitType.AtomBomb)).toHaveLength(1);
});
test("sam should cooldown as long as configured", async () => {
const sam = defender.buildUnit(UnitType.SAMLauncher, game.ref(1, 1), {});
game.addExecution(new SAMLauncherExecution(defender, null, sam));
expect(sam.isInCooldown()).toBeFalsy();
const nuke = attacker.buildUnit(UnitType.AtomBomb, game.ref(1, 1), {
targetTile: game.ref(1, 3),
trajectory: [
{ tile: game.ref(1, 1), targetable: true },
{ tile: game.ref(2, 1), targetable: true },
{ tile: game.ref(3, 1), targetable: true },
],
});
executeTicks(game, 3);
expect(nuke.isActive()).toBeFalsy();
for (let i = 0; i < game.config().SAMCooldown() - 3; i++) {
game.executeNextTick();
expect(sam.isInCooldown()).toBeTruthy();
}
executeTicks(game, 2);
expect(sam.isInCooldown()).toBeFalsy();
});
test("two sams should not target twice same nuke", async () => {
const sam1 = defender.buildUnit(UnitType.SAMLauncher, game.ref(1, 1), {});
game.addExecution(new SAMLauncherExecution(defender, null, sam1));
const sam2 = defender.buildUnit(UnitType.SAMLauncher, game.ref(1, 2), {});
game.addExecution(new SAMLauncherExecution(defender, null, sam2));
const nuke = attacker.buildUnit(UnitType.AtomBomb, game.ref(1, 1), {
targetTile: game.ref(1, 3),
trajectory: [
{ tile: game.ref(1, 1), targetable: true },
{ tile: game.ref(1, 2), targetable: true },
{ tile: game.ref(1, 3), targetable: true },
],
});
executeTicks(game, 3);
expect(nuke.isActive()).toBeFalsy();
expect([sam1, sam2].filter((s) => s.isInCooldown())).toHaveLength(1);
});
test("SAMs should target close to launch site", async () => {
const targetDistance = 199;
// Close SAM: should intercept the nuke
const sam = defender.buildUnit(UnitType.SAMLauncher, game.ref(1, 1), {});
game.addExecution(new SAMLauncherExecution(defender, null, sam));
const nukeExecution = new NukeExecution(
UnitType.AtomBomb,
attacker,
game.ref(targetDistance, 1),
null,
);
game.addExecution(nukeExecution);
// Long distance nuke: compute the proper number of ticks
const ticksToExecute = Math.ceil(
targetDistance / game.config().defaultNukeSpeed() + 1,
);
executeTicks(game, ticksToExecute);
expect(nukeExecution.isActive()).toBeFalsy();
expect(sam.isInCooldown()).toBeTruthy();
});
test("SAMs should target only nukes aimed at nearby targets if not close to launch site", async () => {
const targetDistance = 199;
// Middle SAM: should not intercept the nuke
const sam1 = middle_defender.buildUnit(
UnitType.SAMLauncher,
game.ref(50, 1),
{},
);
game.addExecution(new SAMLauncherExecution(defender, null, sam1));
// Far SAM: Should intercept the nuke. Use the far_defender so the SAM can be built
const sam2 = far_defender.buildUnit(
UnitType.SAMLauncher,
game.ref(targetDistance, 1),
{},
);
game.addExecution(new SAMLauncherExecution(far_defender, null, sam2));
const nukeExecution = new NukeExecution(
UnitType.AtomBomb,
attacker,
game.ref(targetDistance, 1),
null,
);
game.addExecution(nukeExecution);
// Long distance nuke: compute the proper number of ticks
const ticksToExecute = Math.ceil(
targetDistance / game.config().defaultNukeSpeed() + 1,
);
executeTicks(game, ticksToExecute);
expect(nukeExecution.isActive()).toBeFalsy();
expect(sam1.isInCooldown()).toBeFalsy();
expect(sam2.isInCooldown()).toBeTruthy();
});
test("SAM should have increased level after upgrade", async () => {
defender.buildUnit(UnitType.SAMLauncher, game.ref(1, 1), {});
expect(defender.units(UnitType.SAMLauncher)[0].level()).toEqual(1);
const upgradeStructureExecution = new UpgradeStructureExecution(
defender,
defender.units(UnitType.SAMLauncher)[0].id(),
);
game.addExecution(upgradeStructureExecution);
executeTicks(game, 2);
expect(defender.units(UnitType.SAMLauncher)[0].level()).toEqual(2);
});
test("SAM should reload expired missile timers even when not in cooldown", async () => {
// Upgrading to level 2 pushes a timer for the new slot. queue.length(1) < level(2)
// so isInCooldown() is false, but the expired timer still needs to be cleaned up.
const sam = defender.buildUnit(UnitType.SAMLauncher, game.ref(1, 1), {});
sam.increaseLevel();
expect(sam.level()).toBe(2);
game.addExecution(new SAMLauncherExecution(defender, null, sam));
expect(sam.missileTimerQueue()).toHaveLength(1);
expect(sam.isInCooldown()).toBeFalsy();
// Wait for the timer to expire — reload must fire even though isInCooldown() is false
executeTicks(game, game.config().SAMCooldown() + 1);
expect(sam.missileTimerQueue()).toHaveLength(0);
});
});