Files
OpenFrontIO/tests/AllianceExtensionExecution.test.ts
T
FloPinguin 4d5bb7a835 Cleanup nations (Part 1) 🧹 (#2637)
## Description:

1. Using the wording `"Nation"`, `"FakeHuman"` and `"NPC"` at the same
time is confusing.
So I renamed every mention of `"FakeHuman"` and `"NPC"` in the entire
project to `"Nation"`. Just like they are called ingame.

2. `BotBehavior.ts` was originally intended for sharing the logic
between nations and bots.
But at the moment, the logic there isn't really shared and it's
basically just about attacking.
So I renamed `BotBehavior.ts` to `AiAttackBehavior.ts`. I use "Ai" to
indicate that this file is used by bots AND nations.

3. Moved `execuction/utils/AllianceBehavior.ts` to
`execuction/nation/NationAllianceBehavior.ts` to make sure everybody
understands that this file is not about alliances in general. It's just
about nations and how they handle alliances.

4. Removed `difficultyModifier` from `DefaultConfig`. It's unused and I
think we usually want to finetune the difficulty instead of using that
method.

5. Added `assertNever` in all `switch (difficulty)` default cases.

## Please complete the following:

- [X] I have added screenshots for all UI updates
- [X] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [X] I have added relevant tests to the test directory
- [X] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced

## Please put your Discord username so you can be contacted if a bug or
regression is found:

FloPinguin
2025-12-18 16:20:23 -08:00

168 lines
5.8 KiB
TypeScript

import { AllianceExtensionExecution } from "../src/core/execution/alliance/AllianceExtensionExecution";
import { AllianceRequestExecution } from "../src/core/execution/alliance/AllianceRequestExecution";
import { AllianceRequestReplyExecution } from "../src/core/execution/alliance/AllianceRequestReplyExecution";
import { Game, MessageType, Player, PlayerType } from "../src/core/game/Game";
import { playerInfo, setup } from "./util/Setup";
let game: Game;
let player1: Player;
let player2: Player;
let player3: Player;
describe("AllianceExtensionExecution", () => {
beforeEach(async () => {
game = await setup(
"ocean_and_land",
{
infiniteGold: true,
instantBuild: true,
infiniteTroops: true,
},
[
playerInfo("player1", PlayerType.Human),
playerInfo("player2", PlayerType.Human),
playerInfo("player3", PlayerType.Nation),
],
);
player1 = game.player("player1");
player2 = game.player("player2");
player3 = game.player("player3");
while (game.inSpawnPhase()) {
game.executeNextTick();
}
});
test("Successfully extends existing alliance between Humans", () => {
jest.spyOn(player1, "canSendAllianceRequest").mockReturnValue(true);
jest.spyOn(player2, "isAlive").mockReturnValue(true);
jest.spyOn(player1, "isAlive").mockReturnValue(true);
game.addExecution(new AllianceRequestExecution(player1, player2.id()));
game.executeNextTick();
game.executeNextTick();
game.addExecution(
new AllianceRequestReplyExecution(player1.id(), player2, true),
);
game.executeNextTick();
game.executeNextTick();
expect(player1.allianceWith(player2)).toBeTruthy();
expect(player2.allianceWith(player1)).toBeTruthy();
const allianceBefore = player1.allianceWith(player2)!;
const allianceSpy = jest.spyOn(allianceBefore, "extend");
const expirationBefore = allianceBefore.expiresAt();
game.addExecution(new AllianceExtensionExecution(player1, player2.id()));
game.executeNextTick();
expect(allianceSpy).toHaveBeenCalledTimes(0); // both players must agree to extend
game.addExecution(new AllianceExtensionExecution(player2, player1.id()));
game.executeNextTick();
const allianceAfter = player1.allianceWith(player2)!;
expect(allianceAfter.id()).toBe(allianceBefore.id());
const expirationAfter = allianceAfter.expiresAt();
expect(expirationAfter).toBeGreaterThan(expirationBefore);
expect(allianceSpy).toHaveBeenCalledTimes(1);
});
test("Fails gracefully if no alliance exists", () => {
game.addExecution(new AllianceExtensionExecution(player1, player2.id()));
game.executeNextTick();
expect(player1.allianceWith(player2)).toBeFalsy();
expect(player2.allianceWith(player1)).toBeFalsy();
});
test("Successfully extends existing alliance between Human and non-Human", () => {
jest.spyOn(player1, "canSendAllianceRequest").mockReturnValue(true);
jest.spyOn(player3, "isAlive").mockReturnValue(true);
jest.spyOn(player1, "isAlive").mockReturnValue(true);
game.addExecution(new AllianceRequestExecution(player1, player3.id()));
game.executeNextTick();
game.executeNextTick();
game.addExecution(
new AllianceRequestReplyExecution(player1.id(), player3, true),
);
game.executeNextTick();
game.executeNextTick();
expect(player1.allianceWith(player3)).toBeTruthy();
expect(player3.allianceWith(player1)).toBeTruthy();
const allianceBefore = player1.allianceWith(player3)!;
const allianceSpy = jest.spyOn(allianceBefore, "extend");
const expirationBefore = allianceBefore.expiresAt();
game.addExecution(new AllianceExtensionExecution(player1, player3.id()));
game.executeNextTick();
expect(allianceSpy).toHaveBeenCalledTimes(0); // both players must agree to extend
game.addExecution(new AllianceExtensionExecution(player3, player1.id()));
game.executeNextTick();
const allianceAfter = player1.allianceWith(player3)!;
expect(allianceAfter.id()).toBe(allianceBefore.id());
const expirationAfter = allianceAfter.expiresAt();
expect(expirationAfter).toBeGreaterThan(expirationBefore);
expect(allianceSpy).toHaveBeenCalledTimes(1);
});
test("Sends message to other player when one player requests renewal", () => {
jest.spyOn(player1, "canSendAllianceRequest").mockReturnValue(true);
jest.spyOn(player2, "isAlive").mockReturnValue(true);
jest.spyOn(player1, "isAlive").mockReturnValue(true);
// Create alliance between player1 and player2
game.addExecution(new AllianceRequestExecution(player1, player2.id()));
game.executeNextTick();
game.executeNextTick();
game.addExecution(
new AllianceRequestReplyExecution(player1.id(), player2, true),
);
game.executeNextTick();
game.executeNextTick();
expect(player1.allianceWith(player2)).toBeTruthy();
expect(player2.allianceWith(player1)).toBeTruthy();
// Spy on displayMessage to verify it's called
const displayMessageSpy = jest.spyOn(game, "displayMessage");
// Player1 requests renewal
game.addExecution(new AllianceExtensionExecution(player1, player2.id()));
game.executeNextTick();
// Verify message was sent to player2
expect(displayMessageSpy).toHaveBeenCalledWith(
"events_display.wants_to_renew_alliance",
MessageType.RENEW_ALLIANCE,
player2.id(),
undefined,
{ name: player1.displayName() },
);
expect(displayMessageSpy).toHaveBeenCalledTimes(1);
// Request again - should not send duplicate message
game.addExecution(new AllianceExtensionExecution(player1, player2.id()));
game.executeNextTick();
// Should still be called only once (no duplicate)
expect(displayMessageSpy).toHaveBeenCalledTimes(1);
displayMessageSpy.mockRestore();
});
});