Files
OpenFrontIO/tests/AutoUpgrade.test.ts
T
Kipstz c3576a50b9 Add auto-upgrade buildings feature with middle mouse click (#1597)
## Description:

This PR implements a new feature allowing automatic upgrade of the
nearest building using the middle mouse button. This feature greatly
simplifies the upgrade process that previously required a right-click +
building recreation.

## 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
- [x] I have read and accepted the CLA agreement (only required once).

## Please put your Discord username so you can be contacted if a bug or
regression is found:

Kipstzz

---------

Co-authored-by: Scott Anderson <662325+scottanderson@users.noreply.github.com>
2025-09-03 16:00:22 -07:00

156 lines
4.7 KiB
TypeScript

/**
* @jest-environment jsdom
*/
import { AutoUpgradeEvent } from "../src/client/InputHandler";
import { EventBus } from "../src/core/EventBus";
describe("AutoUpgrade Feature", () => {
let eventBus: EventBus;
beforeEach(() => {
eventBus = new EventBus();
});
describe("AutoUpgradeEvent", () => {
test("should create AutoUpgradeEvent with correct coordinates", () => {
const event = new AutoUpgradeEvent(100, 200);
expect(event.x).toBe(100);
expect(event.y).toBe(200);
});
test("should emit AutoUpgradeEvent when created", () => {
const mockEmit = jest.spyOn(eventBus, "emit");
const event = new AutoUpgradeEvent(150, 250);
eventBus.emit(event);
expect(mockEmit).toHaveBeenCalledWith(event);
expect(mockEmit).toHaveBeenCalledWith(
expect.objectContaining({
x: 150,
y: 250,
}),
);
});
});
describe("AutoUpgradeEvent Integration", () => {
test("should handle multiple AutoUpgradeEvents", () => {
const mockEmit = jest.spyOn(eventBus, "emit");
const event1 = new AutoUpgradeEvent(100, 200);
const event2 = new AutoUpgradeEvent(300, 400);
eventBus.emit(event1);
eventBus.emit(event2);
expect(mockEmit).toHaveBeenCalledTimes(2);
expect(mockEmit).toHaveBeenNthCalledWith(1, event1);
expect(mockEmit).toHaveBeenNthCalledWith(2, event2);
});
test("should handle AutoUpgradeEvent with zero coordinates", () => {
const event = new AutoUpgradeEvent(0, 0);
expect(event.x).toBe(0);
expect(event.y).toBe(0);
});
test("should handle AutoUpgradeEvent with negative coordinates", () => {
const event = new AutoUpgradeEvent(-100, -200);
expect(event.x).toBe(-100);
expect(event.y).toBe(-200);
});
test("should handle AutoUpgradeEvent with decimal coordinates", () => {
const event = new AutoUpgradeEvent(100.5, 200.7);
expect(event.x).toBe(100.5);
expect(event.y).toBe(200.7);
});
});
describe("AutoUpgradeEvent Event Bus Integration", () => {
test("should allow event listeners to subscribe to AutoUpgradeEvent", () => {
const mockListener = jest.fn();
const event = new AutoUpgradeEvent(100, 200);
eventBus.on(AutoUpgradeEvent, mockListener);
eventBus.emit(event);
expect(mockListener).toHaveBeenCalledWith(event);
});
test("should allow multiple listeners for AutoUpgradeEvent", () => {
const mockListener1 = jest.fn();
const mockListener2 = jest.fn();
const event = new AutoUpgradeEvent(100, 200);
eventBus.on(AutoUpgradeEvent, mockListener1);
eventBus.on(AutoUpgradeEvent, mockListener2);
eventBus.emit(event);
expect(mockListener1).toHaveBeenCalledWith(event);
expect(mockListener2).toHaveBeenCalledWith(event);
});
test("should not call unsubscribed listeners", () => {
const mockListener = jest.fn();
const event = new AutoUpgradeEvent(100, 200);
eventBus.on(AutoUpgradeEvent, mockListener);
eventBus.off(AutoUpgradeEvent, mockListener);
eventBus.emit(event);
expect(mockListener).not.toHaveBeenCalled();
});
});
describe("AutoUpgradeEvent Edge Cases", () => {
test("should handle very large coordinates", () => {
const event = new AutoUpgradeEvent(
Number.MAX_SAFE_INTEGER,
Number.MAX_SAFE_INTEGER,
);
expect(event.x).toBe(Number.MAX_SAFE_INTEGER);
expect(event.y).toBe(Number.MAX_SAFE_INTEGER);
});
test("should handle very small coordinates", () => {
const event = new AutoUpgradeEvent(
Number.MIN_SAFE_INTEGER,
Number.MIN_SAFE_INTEGER,
);
expect(event.x).toBe(Number.MIN_SAFE_INTEGER);
expect(event.y).toBe(Number.MIN_SAFE_INTEGER);
});
test("should handle NaN coordinates", () => {
const event = new AutoUpgradeEvent(NaN, NaN);
expect(isNaN(event.x)).toBe(true);
expect(isNaN(event.y)).toBe(true);
});
test("should handle Infinity coordinates", () => {
const event = new AutoUpgradeEvent(Infinity, -Infinity);
expect(event.x).toBe(Infinity);
expect(event.y).toBe(-Infinity);
});
});
describe("AutoUpgradeEvent Serialization", () => {
test("should maintain coordinate precision", () => {
const event = new AutoUpgradeEvent(100.123456789, 200.987654321);
expect(event.x).toBe(100.123456789);
expect(event.y).toBe(200.987654321);
});
test("should handle string conversion", () => {
const event = new AutoUpgradeEvent(100, 200);
const eventString = JSON.stringify(event);
const parsedEvent = JSON.parse(eventString);
expect(parsedEvent.x).toBe(100);
expect(parsedEvent.y).toBe(200);
});
});
});