Files
OpenFrontIO/tests/core/executions/WinCheckExecution.test.ts
T
wraith4081 c5f6d9116e build: migrate build system to Vite and test runner to Vitest & Remove
depracated husky usage

- Replace Webpack with Vite for faster client bundling and HMR.
- Migrate tests from Jest to Vitest and update configuration.
- Update Web Worker instantiation to standard ESM syntax.
- Implement Env utility in `src/core` for safe, hybrid environment
  variable access (Vite vs Node).
- Refactor configuration loaders to remove direct `process.env`
  dependencies in shared code.
- Update TypeScript environment definitions and project scripts for the
  new toolchain.
- Remove the [depracated usage of the
  husky](https://github.com/typicode/husky/releases/tag/v9.0.1).

build(vite): implement robust git commit resolution with fallback
2025-12-27 00:12:39 +03:00

85 lines
2.6 KiB
TypeScript

import { WinCheckExecution } from "../../../src/core/execution/WinCheckExecution";
import { GameMode } from "../../../src/core/game/Game";
import { setup } from "../../util/Setup";
describe("WinCheckExecution", () => {
let mg: any;
let winCheck: WinCheckExecution;
beforeEach(async () => {
mg = await setup("big_plains", {
infiniteGold: true,
gameMode: GameMode.FFA,
maxTimerValue: 5,
instantBuild: true,
});
mg.setWinner = vi.fn();
winCheck = new WinCheckExecution();
winCheck.init(mg, 0);
});
it("should call checkWinnerFFA in FFA mode", () => {
const spy = vi.spyOn(winCheck as any, "checkWinnerFFA");
winCheck.tick(10);
expect(spy).toHaveBeenCalled();
});
it("should call checkWinnerTeam in non-FFA mode", () => {
mg.config = vi.fn(() => ({
gameConfig: vi.fn(() => ({
maxTimerValue: 5,
gameMode: GameMode.Team,
})),
percentageTilesOwnedToWin: vi.fn(() => 50),
}));
winCheck.init(mg, 0);
const spy = vi.spyOn(winCheck as any, "checkWinnerTeam");
winCheck.tick(10);
expect(spy).toHaveBeenCalled();
});
it("should set winner in FFA if percentage is reached", () => {
const player = {
numTilesOwned: vi.fn(() => 81),
name: vi.fn(() => "P1"),
};
mg.players = vi.fn(() => [player]);
mg.numLandTiles = vi.fn(() => 100);
mg.numTilesWithFallout = vi.fn(() => 0);
winCheck.checkWinnerFFA();
expect(mg.setWinner).toHaveBeenCalledWith(player, expect.anything());
});
it("should set winner in FFA if timer is 0", () => {
const player = {
numTilesOwned: vi.fn(() => 10),
name: vi.fn(() => "P1"),
};
mg.players = vi.fn(() => [player]);
mg.numLandTiles = vi.fn(() => 100);
mg.numTilesWithFallout = vi.fn(() => 0);
mg.stats = vi.fn(() => ({ stats: () => ({ mocked: true }) }));
// Advance ticks until timeElapsed (in seconds) >= maxTimerValue * 60
// timeElapsed = (ticks - numSpawnPhaseTurns) / 10 =>
// ticks >= numSpawnPhaseTurns + maxTimerValue * 600
const threshold =
mg.config().numSpawnPhaseTurns() +
(mg.config().gameConfig().maxTimerValue ?? 0) * 600;
while (mg.ticks() < threshold) {
mg.executeNextTick();
}
winCheck.checkWinnerFFA();
expect(mg.setWinner).toHaveBeenCalledWith(player, expect.any(Object));
});
it("should not set winner if no players", () => {
mg.players = vi.fn(() => []);
winCheck.checkWinnerFFA();
expect(mg.setWinner).not.toHaveBeenCalled();
});
it("should return false for activeDuringSpawnPhase", () => {
expect(winCheck.activeDuringSpawnPhase()).toBe(false);
});
});