mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 13:40:46 +00:00
86a06821a5
## Description: A test case for AllianceExtensionExecution was not actually checking if the alliance was extended correctly. The method to extend the alliance, MutableAlliance::extend, was never called during execution of AllianceExtensionExecution. The reason the extend() method was not being called is because both mock players were not alive. This PR spies on the MutableAlliance object to verify that extend() is correctly called. Related: https://github.com/openfrontio/OpenFrontIO/pull/1536 ## 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 - [x] I have read and accepted the CLA agreement (only required once). ## Please put your Discord username so you can be contacted if a bug or regression is found: bypie5
82 lines
2.7 KiB
TypeScript
82 lines
2.7 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, Player, PlayerType } from "../src/core/game/Game";
|
|
import { playerInfo, setup } from "./util/Setup";
|
|
|
|
let game: Game;
|
|
let player1: Player;
|
|
let player2: 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),
|
|
],
|
|
);
|
|
|
|
player1 = game.player("player1");
|
|
player2 = game.player("player2");
|
|
|
|
while (game.inSpawnPhase()) {
|
|
game.executeNextTick();
|
|
}
|
|
});
|
|
|
|
test("Successfully extends existing alliance", () => {
|
|
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.createdAt() + game.config().allianceDuration();
|
|
|
|
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.createdAt() + game.config().allianceDuration();
|
|
|
|
expect(expirationAfter).toBeGreaterThanOrEqual(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();
|
|
});
|
|
});
|