Files
OpenFrontIO/tests/client/Platform.test.ts
T
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

135 lines
3.9 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
type NavigatorOverride = {
userAgent: string;
userAgentData?: { platform?: string };
maxTouchPoints?: number;
};
const setInnerWidth = (value: number) => {
Object.defineProperty(window, "innerWidth", {
configurable: true,
writable: true,
value,
});
};
const loadPlatform = async ({
userAgent,
userAgentData,
maxTouchPoints,
}: NavigatorOverride) => {
vi.resetModules();
vi.stubGlobal("navigator", {
userAgent,
userAgentData,
maxTouchPoints,
});
const { Platform } = await import("client/Platform");
return Platform;
};
afterEach(() => {
vi.unstubAllGlobals();
vi.clearAllMocks();
});
describe("Platform", () => {
it("detects iOS before macOS for iPhone-like user agents", async () => {
const platform = await loadPlatform({
userAgent:
"Mozilla/5.0 (iPhone; CPU iPhone OS 17_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Mobile/15E148 Safari/604.1",
});
expect(platform.os).toBe("iOS");
expect(platform.isIOS).toBe(true);
expect(platform.isMac).toBe(false);
});
it("detects macOS for Macintosh user agents", async () => {
const platform = await loadPlatform({
userAgent:
"Mozilla/5.0 (Macintosh; Intel Mac OS X 14_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15",
});
expect(platform.os).toBe("macOS");
expect(platform.isMac).toBe(true);
expect(platform.isIOS).toBe(false);
});
it("detects iOS for iPad desktop-mode user agents with touch support", async () => {
const platform = await loadPlatform({
userAgent:
"Mozilla/5.0 (Macintosh; Intel Mac OS X 14_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15",
maxTouchPoints: 5,
});
expect(platform.os).toBe("iOS");
expect(platform.isIOS).toBe(true);
expect(platform.isMac).toBe(false);
});
it("uses userAgentData platform when available", async () => {
const platform = await loadPlatform({
userAgent:
"Mozilla/5.0 (iPhone; CPU iPhone OS 17_5 like Mac OS X) AppleWebKit/605.1.15",
userAgentData: { platform: "Android" },
});
expect(platform.os).toBe("Android");
expect(platform.isAndroid).toBe(true);
});
it("normalizes non-canonical userAgentData platform values", async () => {
const macPlatform = await loadPlatform({
userAgent: "Mozilla/5.0",
userAgentData: { platform: "Macintosh" },
});
expect(macPlatform.os).toBe("macOS");
expect(macPlatform.isMac).toBe(true);
const chromeOsPlatform = await loadPlatform({
userAgent: "Mozilla/5.0",
userAgentData: { platform: "Chrome OS" },
});
expect(chromeOsPlatform.os).toBe("Linux");
expect(chromeOsPlatform.isLinux).toBe(true);
const unknownPlatform = await loadPlatform({
userAgent: "Mozilla/5.0",
userAgentData: { platform: "PlayStation" },
});
expect(unknownPlatform.os).toBe("Unknown");
expect(unknownPlatform.isMac).toBe(false);
expect(unknownPlatform.isWindows).toBe(false);
expect(unknownPlatform.isIOS).toBe(false);
expect(unknownPlatform.isAndroid).toBe(false);
expect(unknownPlatform.isLinux).toBe(false);
});
it("reports viewport breakpoint helpers from window.innerWidth", async () => {
const platform = await loadPlatform({
userAgent:
"Mozilla/5.0 (Macintosh; Intel Mac OS X 14_5) AppleWebKit/605.1.15",
});
setInnerWidth(767);
expect(platform.isMobileWidth).toBe(true);
expect(platform.isTabletWidth).toBe(false);
expect(platform.isDesktopWidth).toBe(false);
setInnerWidth(768);
expect(platform.isMobileWidth).toBe(false);
expect(platform.isTabletWidth).toBe(true);
expect(platform.isDesktopWidth).toBe(false);
setInnerWidth(1024);
expect(platform.isMobileWidth).toBe(false);
expect(platform.isTabletWidth).toBe(false);
expect(platform.isDesktopWidth).toBe(true);
});
});