A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
test(editor): stand up vitest + regression net for the editor core
esengine59cd4d5cc933
diff --git a/.github/workflows/cross-platform.yml b/.github/workflows/cross-platform.ymlindex a9f6c727b..cc8691882 100644--- a/.github/workflows/cross-platform.yml+++ b/.github/workflows/cross-platform.yml@@ -136,15 +136,17 @@ jobs:- name: Build SDKrun: pnpm --filter ./sdk build- # Now editor (depends on SDK build output)+ # Now editor (depends on SDK build output). The editor package is+ # @estella/editor at ./desktop — the old ./editor filter matched no+ # package, so these steps silently no-op'd and never ran in CI.- name: Type-check Editor- run: pnpm --filter ./editor exec tsc --noEmit+ run: pnpm --filter ./desktop run typecheck- name: Run Editor tests- run: pnpm --filter ./editor exec vitest run+ run: pnpm --filter ./desktop run test- name: Build Editor- run: pnpm --filter ./editor build+ run: pnpm --filter ./desktop run build# Build WASM targets individually and verify outputs- name: Build Web targetdiff --git a/desktop/package.json b/desktop/package.jsonindex c8d94a567..be66d831b 100644--- a/desktop/package.json+++ b/desktop/package.json@@ -9,7 +9,9 @@"dev": "vite","build": "tsc --noEmit && vite build","preview": "vite preview",- "typecheck": "tsc --noEmit"+ "typecheck": "tsc --noEmit",+ "test": "vitest run",+ "test:watch": "vitest"},"dependencies": {"esengine": "workspace:*",@@ -30,6 +32,7 @@"typescript": "^6.0.3","vite": "^8.0.16","vite-plugin-electron": "^1.0.4",- "vite-plugin-electron-renderer": "^1.0.0"+ "vite-plugin-electron-renderer": "^1.0.0",+ "vitest": "^2.1.0"}}diff --git a/desktop/tests/engine-commands.test.ts b/desktop/tests/engine-commands.test.tsnew file mode 100644index 000000000..4e6e38faf--- /dev/null+++ b/desktop/tests/engine-commands.test.ts@@ -0,0 +1,110 @@+/**+ * @file Regression net for the editor's read/write/undo core (SceneCommands ++ * SceneQuery) against a real headless World. This is the safety net for+ * the JSON-first rewrite (REARCH_SERIALIZATION.md), which re-targets these+ * modules from the live World to the data model.+ *+ * EngineHost (the engine-boot singleton that needs a canvas + WebGL) is mocked+ * to return a per-test headless World built from the WASM SDK; the rest of the+ * editor logic (EditorHistory, EntityHandles, schema) runs for real.+ */+import { describe, it, expect, beforeAll, beforeEach, vi } from 'vitest';+import { App, Transform } from 'esengine';+import type { ESEngineModule } from 'esengine';+import { loadWasmModule, HAS_WASM } from './helpers/loadWasm';++// Per-test World, injected into the mocked EngineHost. `vi.hoisted` so the+// mock factory (hoisted above imports) can close over it.+const host = vi.hoisted(() => ({ world: null as unknown as App['world'] }));+vi.mock('@/engine/EngineHost', () => ({+ EngineHost: {+ mutableWorld: () => host.world,+ get world() {+ return host.world;+ },+ getResource: () => undefined,+ },+}));++import { SceneCommands } from '@/engine/SceneCommands';+import { SceneQuery } from '@/engine/SceneQuery';+import { EditorHistory } from '@/engine/EditorHistory';++describe.skipIf(!HAS_WASM)('SceneCommands / SceneQuery (headless World)', () => {+ let module: ESEngineModule;+ beforeAll(async () => {+ module = await loadWasmModule();+ });+ beforeEach(() => {+ const app = App.new();+ const registry = new module.Registry();+ app.connectCpp(registry as never, module);+ host.world = app.world;+ EditorHistory.clear();+ });++ it('addEntity spawns an entity with a Transform; undo/redo round-trips', () => {+ const id = SceneCommands.addEntity();+ expect(id).not.toBeNull();+ expect(host.world.valid(id!)).toBe(true);+ expect(host.world.has(id!, Transform)).toBe(true);+ expect(host.world.getAllEntities().length).toBe(1);++ EditorHistory.undo();+ expect(host.world.getAllEntities().length).toBe(0);++ EditorHistory.redo();+ expect(host.world.getAllEntities().length).toBe(1);+ });++ it('setField writes a component field; undo reverts to the prior value', () => {+ const id = SceneCommands.addEntity()!;+ SceneCommands.setField(id, 'Transform', 'position', 'vec3', [10, 20, 30]);+ expect(host.world.get(id, Transform).position).toMatchObject({ x: 10, y: 20, z: 30 });++ EditorHistory.undo();+ expect(host.world.get(id, Transform).position).toMatchObject({ x: 0, y: 0, z: 0 });++ EditorHistory.redo();+ expect(host.world.get(id, Transform).position).toMatchObject({ x: 10, y: 20, z: 30 });+ });++ it('a gesture coalesces multiple setField writes into one undo step', () => {+ const id = SceneCommands.addEntity()!;+ SceneCommands.beginGesture('Drag');+ SceneCommands.setField(id, 'Transform', 'position', 'vec3', [1, 0, 0]);+ SceneCommands.setField(id, 'Transform', 'position', 'vec3', [2, 0, 0]);+ SceneCommands.setField(id, 'Transform', 'position', 'vec3', [3, 0, 0]);+ SceneCommands.endGesture();+ expect(host.world.get(id, Transform).position.x).toBe(3);++ EditorHistory.undo(); // one step undoes the whole drag+ expect(host.world.get(id, Transform).position.x).toBe(0);+ });++ it('deleteEntity removes the entity; undo re-creates it', () => {+ const id = SceneCommands.addEntity()!;+ expect(host.world.getAllEntities().length).toBe(1);++ SceneCommands.deleteEntity(id);+ expect(host.world.getAllEntities().length).toBe(0);++ EditorHistory.undo();+ expect(host.world.getAllEntities().length).toBe(1);+ });++ it('SceneQuery.readInspector lists the entity\'s editable components', () => {+ const id = SceneCommands.addEntity()!;+ const comps = SceneQuery.readInspector(id);+ const transform = comps.find((c) => c.name === 'Transform');+ expect(transform).toBeDefined();+ expect(transform!.fields.some((f) => f.key === 'position')).toBe(true);+ });++ it('SceneQuery.readSceneTree returns a node per root entity', () => {+ SceneCommands.addEntity();+ SceneCommands.addEntity();+ const tree = SceneQuery.readSceneTree();+ expect(tree.length).toBe(2);+ });+});diff --git a/desktop/tests/helpers/loadWasm.ts b/desktop/tests/helpers/loadWasm.tsnew file mode 100644index 000000000..4194fc064--- /dev/null+++ b/desktop/tests/helpers/loadWasm.ts@@ -0,0 +1,33 @@+import { existsSync, readFileSync } from 'node:fs';+import { resolve } from 'node:path';+import { fileURLToPath, URL } from 'node:url';+import type { ESEngineModule } from 'esengine';++// Where the editor's engine-coupled tests find the built WASM SDK. Mirrors the+// SDK test helper: the in-repo CMake/Emscripten output, then desktop/public+// (the synced copy). Tests skip via `describe.skipIf(!HAS_WASM)` if absent.+const __dirname = fileURLToPath(new URL('.', import.meta.url));+function resolveWasmDir(): string {+ const candidates = [+ process.env.ESENGINE_WASM_DIR,+ resolve(__dirname, '../../../build/wasm/web'),+ resolve(__dirname, '../../public/wasm'),+ ].filter((c): c is string => Boolean(c));+ for (const dir of candidates) {+ if (existsSync(resolve(dir, 'esengine.wasm'))) return dir;+ }+ return resolve(__dirname, '../../../build/wasm/web');+}++export const WASM_DIR = resolveWasmDir();+export const WASM_FILE = resolve(WASM_DIR, 'esengine.wasm');+export const HAS_WASM = existsSync(WASM_FILE);++let cached: ESEngineModule | null = null;+export async function loadWasmModule(): Promise<ESEngineModule> {+ if (cached) return cached;+ const wasmBinary = readFileSync(WASM_FILE);+ const factory = (await import(resolve(WASM_DIR, 'esengine.js'))).default;+ cached = (await factory({ wasmBinary })) as ESEngineModule;+ return cached;+}diff --git a/desktop/tests/schema.test.ts b/desktop/tests/schema.test.tsnew file mode 100644index 000000000..a5624091f--- /dev/null+++ b/desktop/tests/schema.test.ts@@ -0,0 +1,64 @@+/**+ * @file Editor schema pure-function tests — establishes the desktop vitest+ * harness and pins the inspector value-shape inference / conversions that+ * the JSON-first rewrite (REARCH_SERIALIZATION.md) will lean on.+ */+import { describe, it, expect } from 'vitest';+import { prettyLabel, hexToRgb, angleZToQuat, inferField } from '@/engine/schema';++describe('prettyLabel', () => {+ it('splits camelCase and capitalizes', () => {+ expect(prettyLabel('orthoSize')).toBe('Ortho Size');+ expect(prettyLabel('position')).toBe('Position');+ expect(prettyLabel('isActive')).toBe('Is Active');+ });+});++describe('hexToRgb', () => {+ it('parses #rrggbb into 0..1 channels', () => {+ expect(hexToRgb('#ffffff')).toEqual({ r: 1, g: 1, b: 1 });+ expect(hexToRgb('#000000')).toEqual({ r: 0, g: 0, b: 0 });+ const red = hexToRgb('#ff0000');+ expect(red.r).toBe(1);+ expect(red.g).toBe(0);+ expect(red.b).toBe(0);+ });+ it('falls back to white on malformed input', () => {+ expect(hexToRgb('nope')).toEqual({ r: 1, g: 1, b: 1 });+ });+});++describe('angleZToQuat', () => {+ it('round-trips 0° to identity', () => {+ const q = angleZToQuat(0);+ expect(q.x).toBe(0);+ expect(q.y).toBe(0);+ expect(q.z).toBeCloseTo(0, 6);+ expect(q.w).toBeCloseTo(1, 6);+ });+ it('encodes 90° into z/w', () => {+ const q = angleZToQuat(90);+ expect(q.z).toBeCloseTo(Math.SQRT1_2, 6);+ expect(q.w).toBeCloseTo(Math.SQRT1_2, 6);+ });+});++describe('inferField', () => {+ it('infers scalar/bool/string types from the live value', () => {+ expect(inferField('size', 5, false)).toMatchObject({ type: 'number', value: 5 });+ expect(inferField('flipX', true, false)).toMatchObject({ type: 'bool', value: true });+ expect(inferField('name', 'hi', false)).toMatchObject({ type: 'string', value: 'hi' });+ });+ it('infers vec2 / vec3 from object shape', () => {+ expect(inferField('p', { x: 1, y: 2 }, false)).toMatchObject({ type: 'vec2', value: [1, 2] });+ expect(inferField('p', { x: 1, y: 2, z: 3 }, false)).toMatchObject({ type: 'vec3', value: [1, 2, 3] });+ });+ it('treats a color key as a hex color', () => {+ const f = inferField('color', { r: 1, g: 0, b: 0 }, true);+ expect(f?.type).toBe('color');+ expect(f?.value).toBe('#ff0000');+ });+ it('returns null for an unknown shape', () => {+ expect(inferField('weird', { foo: 1 }, false)).toBeNull();+ });+});diff --git a/desktop/vitest.config.ts b/desktop/vitest.config.tsnew file mode 100644index 000000000..d268bba8a--- /dev/null+++ b/desktop/vitest.config.ts@@ -0,0 +1,17 @@+import { defineConfig } from 'vitest/config';+import { fileURLToPath, URL } from 'node:url';++// Dedicated test config — deliberately NOT the app's vite.config.ts (which loads+// the Electron plugins). Tests run in plain Node; engine-coupled modules mock+// EngineHost and drive a real headless World from the built WASM SDK.+export default defineConfig({+ resolve: {+ alias: {+ '@': fileURLToPath(new URL('./src', import.meta.url)),+ },+ },+ test: {+ environment: 'node',+ include: ['tests/**/*.test.ts'],+ },+});