Files
OpenFrontIO/tests/client/controllers/WarshipSelectionController.test.ts
T
evanpelle 7b1557b886 controllers push to the WebGL view directly, drop ClientGameRunner relays
BuildPreviewController and WarshipSelectionController now take the WebGL
view in their constructor and call view.updateGhostPreview /
view.setSelectedUnits themselves instead of emitting bus events that
ClientGameRunner forwarded. Splits the old mountWebGLDebugRenderer in
two — createWebGLView builds the view up front so the renderer can wire
controllers to it, mountWebGLDebugRenderer does the per-frame plumbing
after the transformHandler exists. GhostPreviewUpdatedEvent had no
remaining consumers and is removed.
2026-05-16 22:58:31 -07:00

97 lines
2.6 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;
let view: 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 = {};
view = { setSelectedUnits: vi.fn() };
});
it("tracks the selected unit on single-unit selection (rendering is WebGL)", () => {
const ui = new WarshipSelectionController(
game,
eventBus,
transformHandler,
view,
);
const unit = {
id: () => 1,
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,
view,
);
const unit = {
id: () => 1,
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,
view,
);
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();
});
});