Files
OpenFrontIO/tests/core/game/TrainStation.test.ts
T
Wraith 26f5d40819 build: migrate build system to Vite and test runner to Vitest & Remove depracated husky usage (#2703)
- 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).

## Description:

migrate build system to Vite and test runner to Vitest & Remove
depracated husky usage

## 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
- [ ] 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:

wraith4081

---------

Co-authored-by: evanpelle <evanpelle@gmail.com>
2025-12-28 22:10:26 -08:00

131 lines
3.7 KiB
TypeScript

import { vi, type Mocked } from "vitest";
import { TrainExecution } from "../../../src/core/execution/TrainExecution";
import { Game, Player, Unit, UnitType } from "../../../src/core/game/Game";
import { Cluster, TrainStation } from "../../../src/core/game/TrainStation";
vi.mock("../../../src/core/game/Game");
vi.mock("../../../src/core/execution/TrainExecution");
vi.mock("../../../src/core/PseudoRandom");
describe("TrainStation", () => {
let game: Mocked<Game>;
let unit: Mocked<Unit>;
let player: Mocked<Player>;
let trainExecution: Mocked<TrainExecution>;
beforeEach(() => {
game = {
ticks: vi.fn().mockReturnValue(123),
config: vi.fn().mockReturnValue({
trainGold: (isFriendly: boolean) =>
isFriendly ? BigInt(1000) : BigInt(500),
}),
addUpdate: vi.fn(),
addExecution: vi.fn(),
} as any;
player = {
addGold: vi.fn(),
id: 1,
canTrade: vi.fn().mockReturnValue(true),
isFriendly: vi.fn().mockReturnValue(false),
} as any;
unit = {
owner: vi.fn().mockReturnValue(player),
level: vi.fn().mockReturnValue(1),
tile: vi.fn().mockReturnValue({ x: 0, y: 0 }),
type: vi.fn(),
isActive: vi.fn().mockReturnValue(true),
} as any;
trainExecution = {
loadCargo: vi.fn(),
owner: vi.fn().mockReturnValue(player),
level: vi.fn(),
} as any;
});
it("handles City stop", () => {
unit.type.mockReturnValue(UnitType.City);
const station = new TrainStation(game, unit);
station.onTrainStop(trainExecution);
expect(unit.owner().addGold).toHaveBeenCalledWith(1000n, unit.tile());
});
it("handles allied trade", () => {
unit.type.mockReturnValue(UnitType.City);
player.isFriendly.mockReturnValue(true);
const station = new TrainStation(game, unit);
station.onTrainStop(trainExecution);
expect(unit.owner().addGold).toHaveBeenCalledWith(1000n, unit.tile());
expect(trainExecution.owner().addGold).toHaveBeenCalledWith(
1000n,
unit.tile(),
);
});
it("checks trade availability (same owner)", () => {
const otherUnit = {
owner: vi.fn().mockReturnValue(unit.owner()),
} as any;
const station = new TrainStation(game, unit);
const otherStation = new TrainStation(game, otherUnit);
expect(station.tradeAvailable(otherStation.unit.owner())).toBe(true);
});
it("adds and retrieves neighbors", () => {
const stationA = new TrainStation(game, unit);
const stationB = new TrainStation(game, unit);
const railRoad = { from: stationA, to: stationB, tiles: [] } as any;
stationA.addRailroad(railRoad);
const neighbors = stationA.neighbors();
expect(neighbors).toContain(stationB);
});
it("removes neighboring rail", () => {
const stationA = new TrainStation(game, unit);
const stationB = new TrainStation(game, unit);
const railRoad = {
from: stationA,
to: stationB,
tiles: [{ x: 1, y: 1 }],
} as any;
stationA.addRailroad(railRoad);
expect(stationA.getRailroads().size).toBe(1);
stationA.removeNeighboringRails(stationB);
expect(game.addUpdate).toHaveBeenCalledWith(
expect.objectContaining({
isActive: false,
}),
);
expect(stationA.getRailroads().size).toBe(0);
});
it("assigns and retrieves cluster", () => {
const cluster: Cluster = {} as Cluster;
const station = new TrainStation(game, unit);
station.setCluster(cluster);
expect(station.getCluster()).toBe(cluster);
});
it("returns tile and active status", () => {
const station = new TrainStation(game, unit);
expect(station.tile()).toEqual({ x: 0, y: 0 });
expect(station.isActive()).toBe(true);
});
});