Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions src/core/execution/nation/NationStructureBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,6 @@ export class NationStructureBehavior {
if (this.tryBuildDefensePost()) {
return true;
}
// If the attack threshold is met, block other structures even when
// placement failed (no tile found / can't afford).
if (this.defensePostNeeded()) {
return false;
}
}

if (this.isOnStructureCooldown()) {
Expand Down
27 changes: 27 additions & 0 deletions tests/NationStructureBehavior.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,33 @@ describe("NationStructureBehavior.tryBuildDefensePost", () => {
});
});

describe("NationStructureBehavior.handleStructures", () => {
it("does not block economic structures when defense post placement fails", () => {
const game = {
config: () => ({
gameConfig: () => ({ difficulty: Difficulty.Hard }),
isUnitDisabled: () => false,
startingGold: () => 0n,
}),
unitInfo: () => ({ cost: () => 0n }),
ticks: () => 100,
};
const player = {
gold: () => 0n,
troops: () => 1000,
incomingAttacks: () => [{ troops: () => 1000, sourceTile: () => null, attacker: () => ({ id: () => "a" }) }],
};
const behavior = makeBehavior(game, player);
(behavior as any).placementsCount = 1;
vi.spyOn(behavior as any, "tryBuildDefensePost").mockReturnValue(false);
const doHandleSpy = vi.spyOn(behavior as any, "doHandleStructures").mockReturnValue(true);

const result = behavior.handleStructures();
expect(result).toBe(true);
expect(doHandleSpy).toHaveBeenCalled();
Comment on lines +524 to +545

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

rg -n -C 6 '\bsetup\s*\(' tests --glob '*.ts'
rg -n -C 8 '\b(handleStructures|tryBuildDefensePost|doHandleStructures)\b' \
  tests/NationStructureBehavior.test.ts \
  src/core/execution/nation/NationStructureBehavior.ts

Repository: openfrontio/OpenFrontIO

Length of output: 50380


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== file snippets =="
sed -n '500,560p' tests/NationStructureBehavior.test.ts | nl -ba -v500
sed -n '1,260p' src/core/execution/nation/NationStructureBehavior.ts | nl -ba -v1

echo "== direct tests for mockReturnValue around target =="
rg -n -C 4 'vi\.spyOn\(behavior as any, "doHandleStructures"\)\.mockReturnValue\(true\)|doHandleStructures' tests/NationStructureBehavior.test.ts src/core/execution/nation/NationStructureBehavior.ts

Repository: openfrontio/OpenFrontIO

Length of output: 221


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== file snippets =="
sed -n '1,60p' src/core/execution/nation/NationStructureBehavior.ts
sed -n '500,560p' tests/NationStructureBehavior.test.ts

echo "== relevant symbols =="
rg -n -C 6 'handleStructures|tryBuildDefensePost|doHandleStructures' \
  tests/NationStructureBehavior.test.ts \
  src/core/execution/nation/NationStructureBehavior.ts

echo "== imports around test =="
sed -n '1,50p' tests/NationStructureBehavior.test.ts

Repository: openfrontio/OpenFrontIO

Length of output: 23565


Run handleStructures() through map-backed simulation.

handleStructures() delegates normal construction to doHandleStructures(), but this test mocks that method to return true after a fake defense-post failure. Use setup() from tests/util/Setup.ts with a map from tests/testdata/maps/, use a real player and game instance, let defense-post placement fail for the scenario, then assert that normal structure handling creates a real structure without mocking doHandleStructures().

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/NationStructureBehavior.test.ts` around lines 524 - 545, The test
should exercise handleStructures() through setup() using a real map from
tests/testdata/maps/ and real game/player instances. Configure the scenario so
tryBuildDefensePost() fails, remove the doHandleStructures() mock, and assert
that normal handling creates a real structure while preserving the intended
defense-post failure conditions.

Sources: Coding guidelines, Learnings

});
});

// ── defensePostNeeded ────────────────────────────────────────────────────────

describe("NationStructureBehavior.defensePostNeeded", () => {
Expand Down
Loading