Files
OpenFrontIO/tests/client/controllers/WarshipSelectionController.test.ts
T
evanpelle a708a8c984 rename UILayer/StructureIconsLayer to controllers, move to src/client/controllers/
UILayer → WarshipSelectionController and StructureIconsLayer →
BuildPreviewController. These are the two real Controller implementations
(state + click handling, no rendering) — the new names + location reflect
what they actually do now that all rendering lives in WebGL passes.
2026-05-16 22:45:02 -07:00

78 lines
2.4 KiB
TypeScript

import { WarshipSelectionController } from "../../../src/client/controllers/WarshipSelectionController";
import { UnitSelectionEvent } from "../../../src/client/InputHandler";
describe("WarshipSelectionController", () => {
let game: any;
let eventBus: any;
let transformHandler: any;
beforeEach(() => {
game = {
width: () => 100,
height: () => 100,
config: () => ({
theme: () => ({
territoryColor: () => ({
lighten: () => ({ alpha: () => ({ toRgbString: () => "#fff" }) }),
}),
}),
}),
x: () => 10,
y: () => 10,
unitInfo: () => ({ maxHealth: 10, constructionDuration: 5 }),
myPlayer: () => ({ id: () => 1 }),
ticks: () => 1,
updatesSinceLastTick: () => undefined,
};
eventBus = { on: vi.fn() };
transformHandler = {};
});
it("tracks the selected unit on single-unit selection (rendering is WebGL)", () => {
const ui = new WarshipSelectionController(game, eventBus, transformHandler);
const unit = {
type: () => "Warship",
isActive: () => true,
tile: () => ({}),
owner: () => ({}),
};
const event = { isSelected: true, unit };
ui["onUnitSelection"](event as UnitSelectionEvent);
// selectedUnit is held for game-logic callers (the click handlers). The
// visual selection box is drawn by WebGL SelectionBoxPass — wired from
// ClientGameRunner via view.setSelectedUnits([unit.id()]).
expect(ui["selectedUnit"]).toBe(unit);
});
it("clears selection on deselect", () => {
const ui = new WarshipSelectionController(game, eventBus, transformHandler);
const unit = {
type: () => "Warship",
isActive: () => true,
tile: () => ({}),
owner: () => ({}),
};
ui["onUnitSelection"]({ isSelected: true, unit } as UnitSelectionEvent);
ui["onUnitSelection"]({
isSelected: false,
unit: null,
} as unknown as UnitSelectionEvent);
expect(ui["selectedUnit"]).toBeNull();
});
it("tracks multi-selection list", () => {
const ui = new WarshipSelectionController(game, eventBus, transformHandler);
const units = [
{ id: () => 1, isActive: () => true },
{ id: () => 2, isActive: () => true },
];
ui["onUnitSelection"]({
isSelected: true,
unit: null,
units,
} as unknown as UnitSelectionEvent);
expect(ui["multiSelectedWarships"]).toEqual(units);
expect(ui["selectedUnit"]).toBeNull();
});
});