Files
OpenFrontIO/tests/core/game/TrainStation.test.ts
DevelopingTom c80ccaece9 Record train trading stats (#2891)
## Description:

The current gold stats don’t include gold generated by trains, even
though this is a significant part of the economy for many players.

This PR tracks those stats with two values:
- other players trains visits the player station
- the player trains visits any station

Linked to this infra PR: https://github.com/openfrontio/infra/pull/242

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

IngloriousTom
2026-01-14 01:00:55 +00:00

135 lines
3.8 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(),
stats: vi.fn().mockReturnValue({
trainExternalTrade: vi.fn(),
trainSelfTrade: 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);
});
});