Files
OpenFrontIO/tests/AutoUpgrade.test.ts
Evan Pelle a7f992e9b0 refactor: convert to npm-workspaces monorepo (engine/core-public/shared/client/server)
Restructure the single src/ tree into an npm-workspaces monorepo under
packages/, rename core -> engine, extract a types-only core-public layer,
and break the pre-existing engine -> client dependency cycle.

Structure (packages/):
  core-public  public API/wire schemas + shared enums (clean leaf)
  shared       framework-agnostic helpers (clean leaf)
  engine       deterministic simulation (was src/core)
  client       rendering/UI (was src/client)
  server       coordination (was src/server)

Dependency DAG: engine -> {core-public, shared}; client -> {core-public,
shared, engine}; server -> {core-public, engine}.

- npm workspaces: root package.json workspaces + per-package package.json;
  tsconfig.base.json holds shared options + path aliases
  (core-public/* shared/* engine/* client/* server/*) resolved uniformly by
  tsc, Vite (resolve.tsconfigPaths), Vitest, and tsx. Lockfile regenerated.
- core-public: moved Schemas/ApiSchemas/CosmeticSchemas/StatsSchemas/
  ClanApiSchemas/WorkerSchemas/Base64/PatternDecoder; extracted the enums
  (GameTypes), GameEvent type, emoji table, and GraphicsOverrides schema.
  Engine re-exports the moved enums/types so existing imports keep working.
- Broke engine -> client cycle:
  - renderNumber/renderTroops -> shared/format
  - NameBoxCalculator moved into engine
  - username validation returns translation key + params; client translates
  - applyStateUpdate moved to client (operates on the render-only PlayerState)
  - Config/UnitGrid/execution-Util/GameImpl now use structural read
    interfaces (engine/game/ReadViews: PlayerLike/UnitLike/GameLike) instead
    of importing client view classes; client imports view classes from a new
    client/view barrel; deleted the engine/game/GameView re-export shim.
- Build/deploy updated: vite.config, index.html, eslint, Dockerfile
  (copies packages/ + tsconfig.base.json before npm ci), .vscode, tests.

Verified: tsc --noEmit clean; 1364 + 65 tests pass; production vite build
succeeds; engine has zero client/server imports; core-public and shared are
dependency leaves.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-10 16:51:03 +00:00

153 lines
4.7 KiB
TypeScript

import { AutoUpgradeEvent } from "client/InputHandler";
import { EventBus } from "engine/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 = vi.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 = vi.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 = vi.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 = vi.fn();
const mockListener2 = vi.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 = vi.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);
});
});
});