Skip to content

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.

test: add SDK integration tests for UI, layout, slider, and app tick

esengined5cc94c26299
diff --git a/sdk/tests/app-tick.test.ts b/sdk/tests/app-tick.test.ts
new file mode 100644
index 000000000..b9bc170ff
--- /dev/null
+++ b/sdk/tests/app-tick.test.ts
@@ -0,0 +1,102 @@
+import { describe, it, expect } from 'vitest';
+import { App } from '../src/app';
+import { Schedule, defineSystem } from '../src/system';
+import { Time } from '../src/resource';
+import { defineResource, Res } from '../src/resource';
+
+describe('App.tick()', () => {
+ it('should lazily initialize runner and Time resource', () => {
+ const app = App.new();
+ expect(app.hasResource(Time)).toBe(false);
+
+ app.tick(1 / 60);
+
+ expect(app.hasResource(Time)).toBe(true);
+ const time = app.getResource(Time);
+ expect(time.frameCount).toBe(1);
+ expect(time.delta).toBeCloseTo(1 / 60);
+ });
+
+ it('should run Startup schedule only once', () => {
+ const app = App.new();
+ let startupCount = 0;
+
+ app.addSystemToSchedule(Schedule.Startup, defineSystem(
+ [], () => { startupCount++; }, { name: 'TestStartup' }
+ ));
+
+ app.tick(1 / 60);
+ app.tick(1 / 60);
+ app.tick(1 / 60);
+
+ expect(startupCount).toBe(1);
+ });
+
+ it('should execute schedules in correct order', () => {
+ const app = App.new();
+ const order: string[] = [];
+
+ app.addSystemToSchedule(Schedule.First, defineSystem(
+ [], () => { order.push('First'); }, { name: 'S_First' }
+ ));
+ app.addSystemToSchedule(Schedule.PreUpdate, defineSystem(
+ [], () => { order.push('PreUpdate'); }, { name: 'S_PreUpdate' }
+ ));
+ app.addSystemToSchedule(Schedule.Update, defineSystem(
+ [], () => { order.push('Update'); }, { name: 'S_Update' }
+ ));
+ app.addSystemToSchedule(Schedule.PostUpdate, defineSystem(
+ [], () => { order.push('PostUpdate'); }, { name: 'S_PostUpdate' }
+ ));
+ app.addSystemToSchedule(Schedule.Last, defineSystem(
+ [], () => { order.push('Last'); }, { name: 'S_Last' }
+ ));
+
+ app.tick(1 / 60);
+
+ expect(order).toEqual(['First', 'PreUpdate', 'Update', 'PostUpdate', 'Last']);
+ });
+
+ it('should skip Fixed schedules', () => {
+ const app = App.new();
+ let fixedRan = false;
+
+ app.addSystemToSchedule(Schedule.FixedUpdate, defineSystem(
+ [], () => { fixedRan = true; }, { name: 'S_FixedUpdate' }
+ ));
+
+ app.tick(1 / 60);
+
+ expect(fixedRan).toBe(false);
+ });
+
+ it('should accumulate elapsed time across ticks', () => {
+ const app = App.new();
+
+ app.tick(0.1);
+ app.tick(0.2);
+ app.tick(0.05);
+
+ const time = app.getResource(Time);
+ expect(time.elapsed).toBeCloseTo(0.35);
+ expect(time.frameCount).toBe(3);
+ expect(time.delta).toBeCloseTo(0.05);
+ });
+
+ it('should pass resources to systems via Res()', () => {
+ const app = App.new();
+ const MyRes = defineResource({ value: 42 }, 'MyRes');
+ app.insertResource(MyRes, { value: 42 });
+
+ let capturedValue = 0;
+ app.addSystemToSchedule(Schedule.Update, defineSystem(
+ [Res(MyRes)],
+ (res: { value: number }) => { capturedValue = res.value; },
+ { name: 'S_ReadRes' }
+ ));
+
+ app.tick(1 / 60);
+
+ expect(capturedValue).toBe(42);
+ });
+});
diff --git a/sdk/tests/emscripten-wrapper.test.ts b/sdk/tests/emscripten-wrapper.test.ts
new file mode 100644
index 000000000..fc713d522
--- /dev/null
+++ b/sdk/tests/emscripten-wrapper.test.ts
@@ -0,0 +1,173 @@
+/**
+ * Diagnostic test: verify Object.entries behavior with Emscripten reference wrappers
+ */
+import { describe, it, expect, beforeAll } from 'vitest';
+import { existsSync } from 'fs';
+import { resolve } from 'path';
+import { App } from '../src/app';
+import { Transform } from '../src/component';
+import { UIRect } from '../src/ui/UIRect';
+import type { UIRectData } from '../src/ui/UIRect';
+import type { ESEngineModule, CppRegistry } from '../src/wasm';
+import { loadWasmModule } from './helpers/loadWasm';
+
+const WASM_PATH = resolve(__dirname, '../../desktop/public/wasm/esengine.wasm');
+const HAS_WASM = existsSync(WASM_PATH);
+
+describe.skipIf(!HAS_WASM)('Emscripten wrapper diagnostics', () => {
+ let module: ESEngineModule;
+
+ beforeAll(async () => {
+ module = await loadWasmModule();
+ });
+
+ it('Object.entries on Emscripten getUIRect reference wrapper', () => {
+ const registry = new module.Registry() as unknown as CppRegistry;
+ const entity = registry.create();
+
+ // Add UIRect with specific values
+ registry.addUIRect(entity, {
+ anchorMin: { x: 0, y: 0 },
+ anchorMax: { x: 0.5, y: 1 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 50, y: 50 },
+ pivot: { x: 0.5, y: 0.5 },
+ } as any);
+
+ // Get the reference wrapper
+ const ref = registry.getUIRect(entity);
+
+ // Check values via getter
+ console.log('=== Direct property access ===');
+ console.log('ref.anchorMin:', JSON.stringify(ref.anchorMin));
+ console.log('ref.anchorMax:', JSON.stringify(ref.anchorMax));
+ console.log('ref.size:', JSON.stringify(ref.size));
+
+ // Check Object.entries
+ const entries = Object.entries(ref as any);
+ console.log('\n=== Object.entries ===');
+ console.log('entries length:', entries.length);
+ console.log('entries:', JSON.stringify(entries));
+
+ // Check Object.keys
+ const keys = Object.keys(ref as any);
+ console.log('\n=== Object.keys ===');
+ console.log('keys:', JSON.stringify(keys));
+
+ // Check own property names
+ const ownProps = Object.getOwnPropertyNames(ref as any);
+ console.log('\n=== Object.getOwnPropertyNames ===');
+ console.log('ownProps:', JSON.stringify(ownProps));
+
+ // Check own property descriptors
+ const descs = Object.getOwnPropertyDescriptors(ref as any);
+ console.log('\n=== Own property descriptors ===');
+ for (const [k, d] of Object.entries(descs)) {
+ console.log(` ${k}: value=${d.value}, get=${typeof d.get}, set=${typeof d.set}, enum=${d.enumerable}`);
+ }
+
+ // Check prototype properties
+ const proto = Object.getPrototypeOf(ref);
+ if (proto) {
+ const protoDescs = Object.getOwnPropertyDescriptors(proto);
+ console.log('\n=== Prototype property descriptors ===');
+ for (const [k, d] of Object.entries(protoDescs)) {
+ console.log(` ${k}: value=${typeof d.value}, get=${typeof d.get}, set=${typeof d.set}, enum=${d.enumerable}`);
+ }
+ }
+
+ expect(ref.anchorMax.x).toBeCloseTo(0.5, 5);
+ expect(ref.anchorMax.y).toBeCloseTo(1, 5);
+
+ registry.destroy(entity);
+ (registry as any).delete();
+ });
+
+ it('world.insert with Emscripten wrapper preserves values', () => {
+ const app = App.new();
+ const registry = new module.Registry() as unknown as CppRegistry;
+ app.connectCpp(registry, module);
+
+ const world = app.world;
+ const entity = world.spawn();
+
+ // Insert UIRect with specific stretch anchors
+ world.insert(entity, UIRect, {
+ anchorMin: { x: 0, y: 0 },
+ anchorMax: { x: 1, y: 1 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 0, y: 0 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
+
+ // Get the reference wrapper
+ const ref = world.get(entity, UIRect) as UIRectData;
+
+ console.log('\n=== Before modification ===');
+ console.log('anchorMax:', JSON.stringify(ref.anchorMax));
+
+ // Modify via wrapper (like applyDirectionalFill does)
+ ref.anchorMin = { x: 0, y: 0 };
+ ref.anchorMax = { x: 0.5, y: 1 };
+ ref.offsetMin = { x: 0, y: 0 };
+ ref.offsetMax = { x: 0, y: 0 };
+
+ console.log('After setter, anchorMax:', JSON.stringify(ref.anchorMax));
+
+ // Re-insert (like applyDirectionalFill does)
+ world.insert(entity, UIRect, ref);
+
+ // Read back
+ const after = world.get(entity, UIRect) as UIRectData;
+ console.log('\n=== After world.insert ===');
+ console.log('anchorMax:', JSON.stringify(after.anchorMax));
+ console.log('size:', JSON.stringify(after.size));
+ console.log('pivot:', JSON.stringify(after.pivot));
+
+ // THIS IS THE KEY CHECK
+ expect(after.anchorMax.x).toBeCloseTo(0.5, 5);
+ expect(after.anchorMax.y).toBeCloseTo(1, 5);
+ expect(after.anchorMin.x).toBeCloseTo(0, 5);
+ expect(after.anchorMin.y).toBeCloseTo(0, 5);
+
+ world.despawn(entity);
+ world.disconnectCpp();
+ (registry as any).delete();
+ });
+
+ it('nested field modification on Emscripten wrapper', () => {
+ const registry = new module.Registry() as unknown as CppRegistry;
+ const entity = registry.create();
+
+ registry.addUIRect(entity, {
+ anchorMin: { x: 0, y: 0 },
+ anchorMax: { x: 1, y: 1 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 200, y: 200 },
+ pivot: { x: 0.5, y: 0.5 },
+ } as any);
+
+ const ref = registry.getUIRect(entity);
+
+ // Try nested modification (the problematic pattern from syncHandleRect)
+ console.log('\n=== Nested field modification ===');
+ console.log('Before: anchorMin.x =', ref.anchorMin.x);
+
+ // This modifies a COPY, not C++ memory
+ ref.anchorMin.x = 0.7;
+ console.log('After ref.anchorMin.x = 0.7, actual value =', ref.anchorMin.x);
+
+ // This SHOULD write to C++ memory
+ ref.anchorMin = { x: 0.7, y: 0.3 };
+ console.log('After ref.anchorMin = {0.7, 0.3}, actual value =', ref.anchorMin.x, ref.anchorMin.y);
+
+ expect(ref.anchorMin.x).toBeCloseTo(0.7, 5);
+ expect(ref.anchorMin.y).toBeCloseTo(0.3, 5);
+
+ registry.destroy(entity);
+ (registry as any).delete();
+ });
+});
diff --git a/sdk/tests/entity-hierarchy.test.ts b/sdk/tests/entity-hierarchy.test.ts
new file mode 100644
index 000000000..0635c02f3
--- /dev/null
+++ b/sdk/tests/entity-hierarchy.test.ts
@@ -0,0 +1,341 @@
+/**
+ * Integration tests: Entity parent-child hierarchy via real WASM module.
+ *
+ * Requires pre-built WASM at desktop/public/wasm/esengine.wasm.
+ * Run `node build-tools/cli.js build -t web` first if missing.
+ */
+import { describe, it, expect, beforeAll } from 'vitest';
+import { existsSync } from 'fs';
+import { resolve } from 'path';
+import { World } from '../src/world';
+import { Transform, Sprite, Parent, Children } from '../src/component';
+import type { ESEngineModule, CppRegistry } from '../src/wasm';
+import { loadWasmModule } from './helpers/loadWasm';
+
+const WASM_PATH = resolve(__dirname, '../../desktop/public/wasm/esengine.wasm');
+const HAS_WASM = existsSync(WASM_PATH);
+
+describe.skipIf(!HAS_WASM)('Entity Hierarchy (WASM integration)', () => {
+ let module: ESEngineModule;
+
+ beforeAll(async () => {
+ module = await loadWasmModule();
+ });
+
+ function createWorld(): { world: World; registry: CppRegistry } {
+ const registry = new module.Registry() as unknown as CppRegistry;
+ const world = new World();
+ world.connectCpp(registry, module);
+ return { world, registry };
+ }
+
+ function disposeWorld(world: World, registry: CppRegistry): void {
+ for (const e of world.getAllEntities()) {
+ try { world.despawn(e); } catch (_) {}
+ }
+ world.disconnectCpp();
+ (registry as any).delete();
+ }
+
+ describe('setParent / getChildEntities', () => {
+ it('should establish parent-child relationship', () => {
+ const { world, registry } = createWorld();
+
+ const parent = world.spawn();
+ const child = world.spawn();
+
+ world.setParent(child, parent);
+
+ expect(registry.hasParent(child)).toBe(true);
+ const parentComp = registry.getParent(child);
+ expect(parentComp.entity).toBe(parent);
+
+ expect(registry.hasChildren(parent)).toBe(true);
+
+ disposeWorld(world, registry);
+ });
+
+ it('should return child entities via getChildEntities', () => {
+ const { world, registry } = createWorld();
+
+ const parent = world.spawn();
+ const child1 = world.spawn();
+ const child2 = world.spawn();
+ const child3 = world.spawn();
+
+ world.setParent(child1, parent);
+ world.setParent(child2, parent);
+ world.setParent(child3, parent);
+
+ const children = module.getChildEntities(registry, parent);
+ expect(children).toHaveLength(3);
+ expect(children).toContain(child1);
+ expect(children).toContain(child2);
+ expect(children).toContain(child3);
+
+ disposeWorld(world, registry);
+ });
+
+ it('should return empty array for entity with no children', () => {
+ const { world, registry } = createWorld();
+
+ const entity = world.spawn();
+ const children = module.getChildEntities(registry, entity);
+ expect(children).toHaveLength(0);
+
+ disposeWorld(world, registry);
+ });
+
+ it('should support multi-level hierarchy', () => {
+ const { world, registry } = createWorld();
+
+ const root = world.spawn();
+ const mid = world.spawn();
+ const leaf = world.spawn();
+
+ world.setParent(mid, root);
+ world.setParent(leaf, mid);
+
+ const rootChildren = module.getChildEntities(registry, root);
+ expect(rootChildren).toHaveLength(1);
+ expect(rootChildren).toContain(mid);
+
+ const midChildren = module.getChildEntities(registry, mid);
+ expect(midChildren).toHaveLength(1);
+ expect(midChildren).toContain(leaf);
+
+ const leafChildren = module.getChildEntities(registry, leaf);
+ expect(leafChildren).toHaveLength(0);
+
+ expect(registry.getParent(mid).entity).toBe(root);
+ expect(registry.getParent(leaf).entity).toBe(mid);
+
+ disposeWorld(world, registry);
+ });
+
+ it('should remove parent relationship via removeParent', () => {
+ const { world, registry } = createWorld();
+
+ const parent = world.spawn();
+ const child = world.spawn();
+
+ world.setParent(child, parent);
+ expect(registry.hasParent(child)).toBe(true);
+
+ world.removeParent(child);
+ expect(registry.hasParent(child)).toBe(false);
+
+ disposeWorld(world, registry);
+ });
+
+ it('should reparent entity to a different parent', () => {
+ const { world, registry } = createWorld();
+
+ const parent1 = world.spawn();
+ const parent2 = world.spawn();
+ const child = world.spawn();
+
+ world.setParent(child, parent1);
+ expect(module.getChildEntities(registry, parent1)).toHaveLength(1);
+ expect(module.getChildEntities(registry, parent2)).toHaveLength(0);
+
+ world.setParent(child, parent2);
+ expect(module.getChildEntities(registry, parent1)).toHaveLength(0);
+ expect(module.getChildEntities(registry, parent2)).toHaveLength(1);
+ expect(registry.getParent(child).entity).toBe(parent2);
+
+ disposeWorld(world, registry);
+ });
+ });
+
+ describe('builtin component CRUD via World', () => {
+ it('should insert and get Transform', () => {
+ const { world, registry } = createWorld();
+
+ const entity = world.spawn();
+ world.insert(entity, Transform, {
+ position: { x: 10, y: 20, z: 30 },
+ rotation: { x: 0, y: 0, z: 0, w: 1 },
+ scale: { x: 2, y: 2, z: 2 },
+ });
+
+ expect(world.has(entity, Transform)).toBe(true);
+
+ const lt = world.get(entity, Transform);
+ expect(lt.position.x).toBeCloseTo(10);
+ expect(lt.position.y).toBeCloseTo(20);
+ expect(lt.position.z).toBeCloseTo(30);
+ expect(lt.scale.x).toBeCloseTo(2);
+
+ disposeWorld(world, registry);
+ });
+
+ it('should insert and get Sprite', () => {
+ const { world, registry } = createWorld();
+
+ const entity = world.spawn();
+ world.insert(entity, Sprite, {
+ texture: 0,
+ color: { r: 1, g: 0, b: 0, a: 1 },
+ size: { x: 64, y: 32 },
+ uvOffset: { x: 0, y: 0 },
+ uvScale: { x: 1, y: 1 },
+ layer: 5,
+ flipX: true,
+ flipY: false,
+ });
+
+ expect(world.has(entity, Sprite)).toBe(true);
+
+ const sprite = registry.getSprite(entity);
+ expect(sprite.layer).toBe(5);
+ expect(sprite.flipX).toBe(true);
+ expect(sprite.flipY).toBe(false);
+
+ disposeWorld(world, registry);
+ });
+
+ it('should remove builtin component', () => {
+ const { world, registry } = createWorld();
+
+ const entity = world.spawn();
+ world.insert(entity, Transform);
+ expect(world.has(entity, Transform)).toBe(true);
+
+ world.remove(entity, Transform);
+ expect(world.has(entity, Transform)).toBe(false);
+ expect(registry.hasTransform(entity)).toBe(false);
+
+ disposeWorld(world, registry);
+ });
+
+ it('should update existing component via insert', () => {
+ const { world, registry } = createWorld();
+
+ const entity = world.spawn();
+ world.insert(entity, Transform, {
+ position: { x: 1, y: 2, z: 3 },
+ });
+
+ world.insert(entity, Transform, {
+ position: { x: 100, y: 200, z: 300 },
+ });
+
+ const lt = registry.getTransform(entity);
+ expect(lt.position.x).toBeCloseTo(100);
+ expect(lt.position.y).toBeCloseTo(200);
+ expect(lt.position.z).toBeCloseTo(300);
+
+ disposeWorld(world, registry);
+ });
+
+ it('should insert multiple builtin components on same entity', () => {
+ const { world, registry } = createWorld();
+
+ const entity = world.spawn();
+ world.insert(entity, Transform);
+ world.insert(entity, Sprite, {
+ texture: 0,
+ color: { r: 1, g: 1, b: 1, a: 1 },
+ size: { x: 32, y: 32 },
+ uvOffset: { x: 0, y: 0 },
+ uvScale: { x: 1, y: 1 },
+ layer: 0,
+ flipX: false,
+ flipY: false,
+ });
+
+ expect(registry.hasTransform(entity)).toBe(true);
+ expect(registry.hasSprite(entity)).toBe(true);
+
+ disposeWorld(world, registry);
+ });
+ });
+
+ describe('entity despawn with hierarchy', () => {
+ it('should despawn entity and clean up', () => {
+ const { world, registry } = createWorld();
+
+ const entity = world.spawn();
+ world.insert(entity, Transform);
+ world.insert(entity, Sprite, {
+ texture: 0,
+ color: { r: 1, g: 1, b: 1, a: 1 },
+ size: { x: 32, y: 32 },
+ uvOffset: { x: 0, y: 0 },
+ uvScale: { x: 1, y: 1 },
+ layer: 0,
+ flipX: false,
+ flipY: false,
+ });
+
+ world.despawn(entity);
+
+ expect(world.valid(entity)).toBe(false);
+ expect(world.entityCount()).toBe(0);
+
+ disposeWorld(world, registry);
+ });
+
+ it('should handle spawning many entities and despawning some', () => {
+ const { world, registry } = createWorld();
+
+ const entities: number[] = [];
+ for (let i = 0; i < 100; i++) {
+ const e = world.spawn();
+ world.insert(e, Transform, {
+ position: { x: i, y: i * 2, z: 0 },
+ });
+ entities.push(e);
+ }
+
+ expect(world.entityCount()).toBe(100);
+
+ for (let i = 0; i < 50; i++) {
+ world.despawn(entities[i * 2] as any);
+ }
+
+ expect(world.entityCount()).toBe(50);
+
+ disposeWorld(world, registry);
+ });
+ });
+
+ describe('World.valid and entity lifecycle', () => {
+ it('should report valid for living entities', () => {
+ const { world, registry } = createWorld();
+
+ const e1 = world.spawn();
+ const e2 = world.spawn();
+
+ expect(world.valid(e1)).toBe(true);
+ expect(world.valid(e2)).toBe(true);
+
+ world.despawn(e1);
+ expect(world.valid(e1)).toBe(false);
+ expect(world.valid(e2)).toBe(true);
+
+ disposeWorld(world, registry);
+ });
+
+ it('should track entity count correctly', () => {
+ const { world, registry } = createWorld();
+
+ expect(world.entityCount()).toBe(0);
+
+ const e1 = world.spawn();
+ expect(world.entityCount()).toBe(1);
+
+ const e2 = world.spawn();
+ expect(world.entityCount()).toBe(2);
+
+ world.despawn(e1);
+ expect(world.entityCount()).toBe(1);
+
+ world.despawn(e2);
+ expect(world.entityCount()).toBe(0);
+
+ disposeWorld(world, registry);
+ });
+ });
+});
diff --git a/sdk/tests/helpers/loadWasm.ts b/sdk/tests/helpers/loadWasm.ts
new file mode 100644
index 000000000..7b1cc899b
--- /dev/null
+++ b/sdk/tests/helpers/loadWasm.ts
@@ -0,0 +1,15 @@
+import { readFileSync } from 'fs';
+import { resolve } from 'path';
+import type { ESEngineModule } from '../../src/wasm';
+
+const WASM_DIR = resolve(__dirname, '../../../desktop/public/wasm');
+
+let cachedModule: ESEngineModule | null = null;
+
+export async function loadWasmModule(): Promise<ESEngineModule> {
+ if (cachedModule) return cachedModule;
+ const wasmBinary = readFileSync(resolve(WASM_DIR, 'esengine.wasm'));
+ const factory = (await import(resolve(WASM_DIR, 'esengine.js'))).default;
+ cachedModule = await factory({ wasmBinary }) as ESEngineModule;
+ return cachedModule;
+}
diff --git a/sdk/tests/slider-layout.test.ts b/sdk/tests/slider-layout.test.ts
new file mode 100644
index 000000000..d840ec6fd
--- /dev/null
+++ b/sdk/tests/slider-layout.test.ts
@@ -0,0 +1,998 @@
+/**
+ * Integration tests: Slider fill/handle layout via real WASM module.
+ *
+ * Simulates how the editor renders a Slider:
+ * 1. Create root ScreenSpace → Slider background → Fill + Handle as children
+ * 2. Call applyDirectionalFill / computeHandleAnchors to set anchors
+ * 3. Run app.tick() to compute layout
+ * 4. Verify computed width/height and world positions
+ *
+ * Requires pre-built WASM at desktop/public/wasm/esengine.wasm.
+ */
+import { describe, it, expect, beforeAll } from 'vitest';
+import { existsSync } from 'fs';
+import { resolve } from 'path';
+import { App } from '../src/app';
+import { Transform, Sprite } from '../src/component';
+import { UIRect } from '../src/ui/UIRect';
+import { ScreenSpace } from '../src/ui/ScreenSpace';
+import { UICameraInfo } from '../src/ui/UICameraInfo';
+import { Input, InputState } from '../src/input';
+import { UIEvents, UIEventQueue } from '../src/ui/UIEvents';
+import { uiLayoutPlugin } from '../src/ui/UILayoutPlugin';
+import { uiRenderOrderPlugin } from '../src/ui/UIRenderOrderPlugin';
+import { uiInteractionPlugin } from '../src/ui/UIInteractionPlugin';
+import { sliderPlugin } from '../src/ui/SliderPlugin';
+import { Slider } from '../src/ui/Slider';
+import { applyDirectionalFill, computeHandleAnchors, computeFillAnchors } from '../src/ui/uiHelpers';
+import { FillDirection } from '../src/ui/uiTypes';
+import type { UIRectData } from '../src/ui/UIRect';
+import type { ESEngineModule, CppRegistry } from '../src/wasm';
+import { loadWasmModule } from './helpers/loadWasm';
+
+const WASM_PATH = resolve(__dirname, '../../desktop/public/wasm/esengine.wasm');
+const HAS_WASM = existsSync(WASM_PATH);
+
+describe.skipIf(!HAS_WASM)('Slider Layout (WASM integration)', () => {
+ let module: ESEngineModule;
+
+ beforeAll(async () => {
+ module = await loadWasmModule();
+ });
+
+ function createEditorApp(): { app: App; registry: CppRegistry } {
+ const app = App.new();
+ const registry = new module.Registry() as unknown as CppRegistry;
+ app.connectCpp(registry, module);
+
+ app.insertResource(UICameraInfo, {
+ viewProjection: new Float32Array(16),
+ vpX: 0, vpY: 0, vpW: 0, vpH: 0,
+ screenW: 0, screenH: 0,
+ worldLeft: 0, worldBottom: 0, worldRight: 0, worldTop: 0,
+ worldMouseX: 0, worldMouseY: 0,
+ valid: false,
+ });
+
+ app.addPlugin(uiLayoutPlugin);
+ app.addPlugin(uiRenderOrderPlugin);
+ return { app, registry };
+ }
+
+ function disposeApp(app: App, registry: CppRegistry): void {
+ const world = app.world;
+ for (const e of world.getAllEntities()) {
+ try { world.despawn(e); } catch (_) {}
+ }
+ world.disconnectCpp();
+ (registry as any).delete();
+ }
+
+ function setCanvasRect(app: App, left: number, bottom: number, right: number, top: number): void {
+ const cam = app.getResource(UICameraInfo);
+ cam.worldLeft = left;
+ cam.worldBottom = bottom;
+ cam.worldRight = right;
+ cam.worldTop = top;
+ cam.valid = true;
+ }
+
+ function makeTransform() {
+ return {
+ position: { x: 0, y: 0, z: 0 },
+ rotation: { x: 0, y: 0, z: 0, w: 1 },
+ scale: { x: 1, y: 1, z: 1 },
+ };
+ }
+
+ function makeSprite(w = 100, h = 100) {
+ return {
+ texture: 0,
+ color: { r: 1, g: 1, b: 1, a: 1 },
+ size: { x: w, y: h },
+ uvOffset: { x: 0, y: 0 },
+ uvScale: { x: 1, y: 1 },
+ layer: 0,
+ flipX: false,
+ flipY: false,
+ };
+ }
+
+ function makeFullRect() {
+ return {
+ anchorMin: { x: 0, y: 0 },
+ anchorMax: { x: 1, y: 1 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 0, y: 0 },
+ pivot: { x: 0.5, y: 0.5 },
+ };
+ }
+
+ function createSliderHierarchy(app: App) {
+ const world = app.world;
+
+ const root = world.spawn();
+ world.insert(root, ScreenSpace, {});
+ world.insert(root, UIRect, makeFullRect());
+ world.insert(root, Transform, makeTransform());
+
+ const sliderBg = world.spawn();
+ world.setParent(sliderBg, root);
+ world.insert(sliderBg, UIRect, {
+ anchorMin: { x: 0.5, y: 0.5 },
+ anchorMax: { x: 0.5, y: 0.5 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 200, y: 20 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(sliderBg, Transform, makeTransform());
+ world.insert(sliderBg, Sprite, makeSprite(200, 20));
+
+ const fill = world.spawn();
+ world.setParent(fill, sliderBg);
+ world.insert(fill, UIRect, {
+ anchorMin: { x: 0, y: 0 },
+ anchorMax: { x: 0.5, y: 1 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 0, y: 0 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(fill, Transform, makeTransform());
+ world.insert(fill, Sprite, makeSprite(100, 20));
+
+ const handle = world.spawn();
+ world.setParent(handle, sliderBg);
+ world.insert(handle, UIRect, {
+ anchorMin: { x: 0.5, y: 0 },
+ anchorMax: { x: 0.5, y: 1 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 20, y: 20 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(handle, Transform, makeTransform());
+ world.insert(handle, Sprite, makeSprite(20, 20));
+
+ return { root, sliderBg, fill, handle };
+ }
+
+ describe('computeFillAnchors (pure logic)', () => {
+ it('should compute LeftToRight at 50%', () => {
+ const anchors = computeFillAnchors(FillDirection.LeftToRight, 0.5);
+ expect(anchors.anchorMin).toEqual({ x: 0, y: 0 });
+ expect(anchors.anchorMax).toEqual({ x: 0.5, y: 1 });
+ });
+
+ it('should compute LeftToRight at 0%', () => {
+ const anchors = computeFillAnchors(FillDirection.LeftToRight, 0);
+ expect(anchors.anchorMin).toEqual({ x: 0, y: 0 });
+ expect(anchors.anchorMax).toEqual({ x: 0, y: 1 });
+ });
+
+ it('should compute LeftToRight at 100%', () => {
+ const anchors = computeFillAnchors(FillDirection.LeftToRight, 1);
+ expect(anchors.anchorMin).toEqual({ x: 0, y: 0 });
+ expect(anchors.anchorMax).toEqual({ x: 1, y: 1 });
+ });
+
+ it('should compute RightToLeft at 50%', () => {
+ const anchors = computeFillAnchors(FillDirection.RightToLeft, 0.5);
+ expect(anchors.anchorMin).toEqual({ x: 0.5, y: 0 });
+ expect(anchors.anchorMax).toEqual({ x: 1, y: 1 });
+ });
+
+ it('should compute BottomToTop at 75%', () => {
+ const anchors = computeFillAnchors(FillDirection.BottomToTop, 0.75);
+ expect(anchors.anchorMin).toEqual({ x: 0, y: 0 });
+ expect(anchors.anchorMax).toEqual({ x: 1, y: 0.75 });
+ });
+
+ it('should compute TopToBottom at 30%', () => {
+ const anchors = computeFillAnchors(FillDirection.TopToBottom, 0.3);
+ expect(anchors.anchorMin).toEqual({ x: 0, y: 0.7 });
+ expect(anchors.anchorMax).toEqual({ x: 1, y: 1 });
+ });
+ });
+
+ describe('computeHandleAnchors (pure logic)', () => {
+ it('should position handle at value for LeftToRight', () => {
+ const anchors = computeHandleAnchors(FillDirection.LeftToRight, 0.5);
+ expect(anchors.anchorMin).toEqual({ x: 0.5, y: 0 });
+ expect(anchors.anchorMax).toEqual({ x: 0.5, y: 1 });
+ });
+
+ it('should position handle at start for LeftToRight 0%', () => {
+ const anchors = computeHandleAnchors(FillDirection.LeftToRight, 0);
+ expect(anchors.anchorMin).toEqual({ x: 0, y: 0 });
+ expect(anchors.anchorMax).toEqual({ x: 0, y: 1 });
+ });
+
+ it('should position handle at end for LeftToRight 100%', () => {
+ const anchors = computeHandleAnchors(FillDirection.LeftToRight, 1);
+ expect(anchors.anchorMin).toEqual({ x: 1, y: 0 });
+ expect(anchors.anchorMax).toEqual({ x: 1, y: 1 });
+ });
+ });
+
+ describe('fill layout after applyDirectionalFill + tick', () => {
+ it('should compute fill width = 50% of slider background', () => {
+ const { app, registry } = createEditorApp();
+ const { root, sliderBg, fill, handle } = createSliderHierarchy(app);
+
+ applyDirectionalFill(app.world, fill, FillDirection.LeftToRight, 0.5);
+
+ setCanvasRect(app, -400, -300, 400, 300);
+ app.tick(1 / 60);
+
+ const bgW = module.getUIRectComputedWidth(registry, sliderBg);
+ const bgH = module.getUIRectComputedHeight(registry, sliderBg);
+ expect(bgW).toBeCloseTo(200, 0);
+ expect(bgH).toBeCloseTo(20, 0);
+
+ const fillW = module.getUIRectComputedWidth(registry, fill);
+ const fillH = module.getUIRectComputedHeight(registry, fill);
+ expect(fillW).toBeCloseTo(100, 0);
+ expect(fillH).toBeCloseTo(20, 0);
+
+ disposeApp(app, registry);
+ });
+
+ it('should compute fill width = 0 at value 0', () => {
+ const { app, registry } = createEditorApp();
+ const { fill } = createSliderHierarchy(app);
+
+ applyDirectionalFill(app.world, fill, FillDirection.LeftToRight, 0);
+
+ setCanvasRect(app, -400, -300, 400, 300);
+ app.tick(1 / 60);
+
+ const fillW = module.getUIRectComputedWidth(registry, fill);
+ expect(fillW).toBeCloseTo(0, 0);
+
+ disposeApp(app, registry);
+ });
+
+ it('should compute fill width = 100% at value 1', () => {
+ const { app, registry } = createEditorApp();
+ const { sliderBg, fill } = createSliderHierarchy(app);
+
+ applyDirectionalFill(app.world, fill, FillDirection.LeftToRight, 1);
+
+ setCanvasRect(app, -400, -300, 400, 300);
+ app.tick(1 / 60);
+
+ const bgW = module.getUIRectComputedWidth(registry, sliderBg);
+ const fillW = module.getUIRectComputedWidth(registry, fill);
+ expect(fillW).toBeCloseTo(bgW, 0);
+
+ disposeApp(app, registry);
+ });
+
+ it('should compute fill width = 25% at value 0.25', () => {
+ const { app, registry } = createEditorApp();
+ const { fill } = createSliderHierarchy(app);
+
+ applyDirectionalFill(app.world, fill, FillDirection.LeftToRight, 0.25);
+
+ setCanvasRect(app, -400, -300, 400, 300);
+ app.tick(1 / 60);
+
+ const fillW = module.getUIRectComputedWidth(registry, fill);
+ expect(fillW).toBeCloseTo(50, 0);
+
+ disposeApp(app, registry);
+ });
+
+ it('should position fill at left side, not centered (LeftToRight 50%)', () => {
+ const { app, registry } = createEditorApp();
+ const { sliderBg, fill } = createSliderHierarchy(app);
+
+ applyDirectionalFill(app.world, fill, FillDirection.LeftToRight, 0.5);
+
+ setCanvasRect(app, -400, -300, 400, 300);
+ app.tick(1 / 60);
+ app.tick(1 / 60);
+
+ const fillTransform = registry.getTransform(fill);
+ const bgTransform = registry.getTransform(sliderBg);
+
+ console.log('bgTransform:', JSON.stringify(bgTransform));
+ console.log('fillTransform:', JSON.stringify(fillTransform));
+ console.log('fill UIRect:', JSON.stringify(registry.getUIRect(fill)));
+ console.log('fill Sprite:', JSON.stringify(registry.getSprite(fill)));
+
+ // Slider bg: 200 wide, centered (position.x = 0)
+ expect(bgTransform.position.x).toBeCloseTo(0, 0);
+
+ // Fill: 100 wide, should be at x = -50 (left side of 200-wide parent)
+ // NOT at x = 0 (center)
+ expect(fillTransform.position.x).toBeCloseTo(-50, 0);
+
+ disposeApp(app, registry);
+ });
+ });
+
+ describe('DEBUG: full pipeline sprite bounds', () => {
+ it('should verify sprite left/right edges for LeftToRight fill', () => {
+ const { app, registry } = createEditorApp();
+ const { root, sliderBg, fill } = createSliderHierarchy(app);
+
+ setCanvasRect(app, -400, -300, 400, 300);
+
+ const values = [0, 0.25, 0.5, 0.75, 1.0];
+ for (const value of values) {
+ applyDirectionalFill(app.world, fill, FillDirection.LeftToRight, value);
+ app.tick(1 / 60);
+
+ const fillT = registry.getTransform(fill);
+ const bgT = registry.getTransform(sliderBg);
+ const rootT = registry.getTransform(root);
+ const fillS = registry.getSprite(fill);
+ const fillR = registry.getUIRect(fill);
+ const bgR = registry.getUIRect(sliderBg);
+
+ // hierarchy: root→sliderBg→fill
+ // root has no parent → worldPos = position
+ // sliderBg's parent is root → worldPos = root.worldPos + sliderBg.position
+ // fill's parent is sliderBg → worldPos = sliderBg.worldPos + fill.position
+ const rootWorldX = rootT.position.x;
+ const bgWorldX = rootWorldX + bgT.position.x;
+ const fillWorldX = bgWorldX + fillT.position.x;
+
+ // Sprite renders centered at worldPos, width = sprite.size.x
+ const spriteLeft = fillWorldX - fillS.size.x / 2;
+ const spriteRight = fillWorldX + fillS.size.x / 2;
+ const parentLeft = bgWorldX - 100; // slider is 200 wide, centered
+ const parentRight = bgWorldX + 100;
+ const expectedFillWidth = 200 * value;
+
+ console.log(`\n--- value=${value} ---`);
+ console.log(` root pos.x=${rootT.position.x}`);
+ console.log(` bg pos.x=${bgT.position.x}, worldX=${bgWorldX}`);
+ console.log(` fill pos.x=${fillT.position.x}, worldX=${fillWorldX}`);
+ console.log(` fill sprite.size=${fillS.size.x}x${fillS.size.y}`);
+ console.log(` fill UIRect anchors: min=${fillR.anchorMin.x},${fillR.anchorMin.y} max=${fillR.anchorMax.x},${fillR.anchorMax.y}`);
+ console.log(` sprite bounds: [${spriteLeft}, ${spriteRight}]`);
+ console.log(` parent bounds: [${parentLeft}, ${parentRight}]`);
+ console.log(` expected fill width: ${expectedFillWidth}`);
+
+ // Sprite size should match fill value
+ expect(fillS.size.x).toBeCloseTo(expectedFillWidth, 0);
+
+ // Fill position should be LEFT-aligned, not centered
+ // For LeftToRight, fill's left edge should match parent's left edge
+ if (value > 0) {
+ expect(spriteLeft).toBeCloseTo(parentLeft, 0);
+ }
+ }
+
+ disposeApp(app, registry);
+ });
+
+ it('should verify Children.entities is populated after setParent', () => {
+ const { app, registry } = createEditorApp();
+ const { root, sliderBg, fill, handle } = createSliderHierarchy(app);
+
+ const rootChildren = registry.getChildren(root);
+ const bgChildren = registry.getChildren(sliderBg);
+
+ const rootEntities: number[] = [];
+ const bgEntities: number[] = [];
+ for (let i = 0; i < rootChildren.entities.size(); i++) {
+ rootEntities.push(rootChildren.entities.get(i));
+ }
+ for (let i = 0; i < bgChildren.entities.size(); i++) {
+ bgEntities.push(bgChildren.entities.get(i));
+ }
+
+ console.log('\n--- Hierarchy check ---');
+ console.log(`root children: [${rootEntities}]`);
+ console.log(`sliderBg children: [${bgEntities}]`);
+ console.log(`root entity=${root}, sliderBg=${sliderBg}, fill=${fill}, handle=${handle}`);
+
+ expect(rootEntities).toContain(sliderBg);
+ expect(bgEntities).toContain(fill);
+ expect(bgEntities).toContain(handle);
+
+ const fillParent = registry.getParent(fill);
+ const handleParent = registry.getParent(handle);
+ console.log(`fill parent=${fillParent.entity}, handle parent=${handleParent.entity}`);
+ expect(fillParent.entity).toBe(sliderBg);
+ expect(handleParent.entity).toBe(sliderBg);
+
+ disposeApp(app, registry);
+ });
+
+ it('should test with SliderPlugin running (full runtime simulation)', () => {
+ const app = App.new();
+ const registry = new module.Registry() as unknown as CppRegistry;
+ app.connectCpp(registry, module);
+
+ app.insertResource(UICameraInfo, {
+ viewProjection: new Float32Array(16),
+ vpX: 0, vpY: 0, vpW: 0, vpH: 0,
+ screenW: 0, screenH: 0,
+ worldLeft: 0, worldBottom: 0, worldRight: 0, worldTop: 0,
+ worldMouseX: 0, worldMouseY: 0,
+ valid: false,
+ });
+ app.insertResource(Input, new InputState());
+ app.insertResource(UIEvents, new UIEventQueue());
+
+ // Register layout + interaction + slider plugins (skip text/image which need canvas)
+ app.addPlugin(uiLayoutPlugin);
+ app.addPlugin(uiRenderOrderPlugin);
+ app.addPlugin(uiInteractionPlugin);
+ app.addPlugin(sliderPlugin);
+
+ const world = app.world;
+
+ // Create scene hierarchy: root → slider → fill + handle
+ const root = world.spawn();
+ world.insert(root, ScreenSpace, {});
+ world.insert(root, UIRect, makeFullRect());
+ world.insert(root, Transform, makeTransform());
+
+ const sliderBg = world.spawn();
+ world.setParent(sliderBg, root);
+ world.insert(sliderBg, UIRect, {
+ anchorMin: { x: 0.5, y: 0.5 },
+ anchorMax: { x: 0.5, y: 0.5 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 200, y: 20 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(sliderBg, Transform, makeTransform());
+ world.insert(sliderBg, Sprite, makeSprite(200, 20));
+
+ const fill = world.spawn();
+ world.setParent(fill, sliderBg);
+ world.insert(fill, UIRect, makeFullRect());
+ world.insert(fill, Transform, makeTransform());
+ world.insert(fill, Sprite, makeSprite(200, 20));
+
+ const handle = world.spawn();
+ world.setParent(handle, sliderBg);
+ world.insert(handle, UIRect, {
+ anchorMin: { x: 0.5, y: 0.5 },
+ anchorMax: { x: 0.5, y: 0.5 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 20, y: 20 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(handle, Transform, makeTransform());
+ world.insert(handle, Sprite, makeSprite(20, 20));
+
+ // Register Slider component with fillEntity pointing to fill
+ world.insert(sliderBg, Slider, {
+ value: 0.5,
+ minValue: 0,
+ maxValue: 1,
+ direction: FillDirection.LeftToRight,
+ fillEntity: fill,
+ handleEntity: handle,
+ wholeNumbers: false,
+ });
+
+ // Set camera and tick
+ setCanvasRect(app, -400, -300, 400, 300);
+ app.tick(1 / 60);
+ app.tick(1 / 60);
+
+ const fillT = registry.getTransform(fill);
+ const bgT = registry.getTransform(sliderBg);
+ const fillS = registry.getSprite(fill);
+ const fillR = registry.getUIRect(fill);
+
+ const bgWorldX = bgT.position.x;
+ const fillWorldX = bgWorldX + fillT.position.x;
+ const spriteLeft = fillWorldX - fillS.size.x / 2;
+
+ console.log('\n--- SliderPlugin full test (value=0.5) ---');
+ console.log(` bg pos.x=${bgT.position.x}`);
+ console.log(` fill pos.x=${fillT.position.x}`);
+ console.log(` fill sprite.size=${fillS.size.x}x${fillS.size.y}`);
+ console.log(` fill UIRect: min=${fillR.anchorMin.x},${fillR.anchorMin.y} max=${fillR.anchorMax.x},${fillR.anchorMax.y}`);
+ console.log(` fill worldX=${fillWorldX}, sprite left=${spriteLeft}`);
+
+ // Fill should be 100 wide (50% of 200)
+ expect(fillS.size.x).toBeCloseTo(100, 0);
+ // Fill position should be -50 (left-aligned)
+ expect(fillT.position.x).toBeCloseTo(-50, 0);
+ // Sprite left edge should be at parent left (-100)
+ expect(spriteLeft).toBeCloseTo(-100, 0);
+
+ disposeApp(app, registry);
+ });
+
+ it('should verify worldPosition via TransformSystem', () => {
+ const app = App.new();
+ const registry = new module.Registry() as unknown as CppRegistry;
+ app.connectCpp(registry, module);
+
+ app.insertResource(UICameraInfo, {
+ viewProjection: new Float32Array(16),
+ vpX: 0, vpY: 0, vpW: 0, vpH: 0,
+ screenW: 0, screenH: 0,
+ worldLeft: 0, worldBottom: 0, worldRight: 0, worldTop: 0,
+ worldMouseX: 0, worldMouseY: 0,
+ valid: false,
+ });
+ app.insertResource(Input, new InputState());
+ app.insertResource(UIEvents, new UIEventQueue());
+
+ app.addPlugin(uiLayoutPlugin);
+ app.addPlugin(uiRenderOrderPlugin);
+ app.addPlugin(uiInteractionPlugin);
+ app.addPlugin(sliderPlugin);
+
+ const world = app.world;
+
+ const root = world.spawn();
+ world.insert(root, ScreenSpace, {});
+ world.insert(root, UIRect, makeFullRect());
+ world.insert(root, Transform, makeTransform());
+
+ const sliderBg = world.spawn();
+ world.setParent(sliderBg, root);
+ world.insert(sliderBg, UIRect, {
+ anchorMin: { x: 0.5, y: 0.5 },
+ anchorMax: { x: 0.5, y: 0.5 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 200, y: 20 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(sliderBg, Transform, makeTransform());
+ world.insert(sliderBg, Sprite, makeSprite(200, 20));
+
+ const fill = world.spawn();
+ world.setParent(fill, sliderBg);
+ world.insert(fill, UIRect, makeFullRect());
+ world.insert(fill, Transform, makeTransform());
+ world.insert(fill, Sprite, makeSprite(200, 20));
+
+ const handle = world.spawn();
+ world.setParent(handle, sliderBg);
+ world.insert(handle, UIRect, {
+ anchorMin: { x: 0.5, y: 0.5 },
+ anchorMax: { x: 0.5, y: 0.5 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 20, y: 20 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(handle, Transform, makeTransform());
+ world.insert(handle, Sprite, makeSprite(20, 20));
+
+ world.insert(sliderBg, Slider, {
+ value: 0.5,
+ minValue: 0,
+ maxValue: 1,
+ direction: FillDirection.LeftToRight,
+ fillEntity: fill,
+ handleEntity: handle,
+ wholeNumbers: false,
+ });
+
+ setCanvasRect(app, -400, -300, 400, 300);
+ app.tick(1 / 60);
+ app.tick(1 / 60);
+
+ // Now run TransformSystem to compute worldPosition
+ module.transform_update(registry);
+
+ const fillT = registry.getTransform(fill);
+ const bgT = registry.getTransform(sliderBg);
+ const rootT = registry.getTransform(root);
+ const fillS = registry.getSprite(fill);
+
+ console.log('\n--- TransformSystem worldPosition test ---');
+ console.log(` root: pos=(${rootT.position.x}, ${rootT.position.y}), worldPos=(${rootT.worldPosition.x}, ${rootT.worldPosition.y})`);
+ console.log(` bg: pos=(${bgT.position.x}, ${bgT.position.y}), worldPos=(${bgT.worldPosition.x}, ${bgT.worldPosition.y})`);
+ console.log(` fill: pos=(${fillT.position.x}, ${fillT.position.y}), worldPos=(${fillT.worldPosition.x}, ${fillT.worldPosition.y})`);
+ console.log(` fill sprite.size=(${fillS.size.x}, ${fillS.size.y})`);
+
+ const spriteLeft = fillT.worldPosition.x - fillS.size.x / 2;
+ const spriteRight = fillT.worldPosition.x + fillS.size.x / 2;
+ const sliderLeft = bgT.worldPosition.x - 100;
+ const sliderRight = bgT.worldPosition.x + 100;
+
+ console.log(` sprite bounds: [${spriteLeft}, ${spriteRight}]`);
+ console.log(` slider bounds: [${sliderLeft}, ${sliderRight}]`);
+
+ // worldPosition should be computed from hierarchy
+ expect(bgT.worldPosition.x).toBeCloseTo(0, 0);
+ expect(fillT.worldPosition.x).toBeCloseTo(-50, 0);
+
+ // Sprite left edge should match slider left edge
+ expect(spriteLeft).toBeCloseTo(sliderLeft, 0);
+
+ disposeApp(app, registry);
+ });
+ });
+
+ describe('handle layout after anchor update + tick', () => {
+ it('should position handle sprite at 50%', () => {
+ const { app, registry } = createEditorApp();
+ const { sliderBg, handle } = createSliderHierarchy(app);
+
+ const anchors = computeHandleAnchors(FillDirection.LeftToRight, 0.5);
+ app.world.insert(handle, UIRect, {
+ anchorMin: anchors.anchorMin,
+ anchorMax: anchors.anchorMax,
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 20, y: 20 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
+
+ setCanvasRect(app, -400, -300, 400, 300);
+ app.tick(1 / 60);
+
+ const handleW = module.getUIRectComputedWidth(registry, handle);
+ const handleH = module.getUIRectComputedHeight(registry, handle);
+ expect(handleW).toBeCloseTo(20, 0);
+ expect(handleH).toBeCloseTo(20, 0);
+
+ disposeApp(app, registry);
+ });
+ });
+
+ describe('render order in slider hierarchy', () => {
+ it('should order: background < fill < handle', () => {
+ const { app, registry } = createEditorApp();
+ const { sliderBg, fill, handle } = createSliderHierarchy(app);
+
+ setCanvasRect(app, -400, -300, 400, 300);
+ app.tick(1 / 60);
+
+ const bgLayer = registry.getSprite(sliderBg).layer;
+ const fillLayer = registry.getSprite(fill).layer;
+ const handleLayer = registry.getSprite(handle).layer;
+
+ expect(fillLayer).toBeGreaterThan(bgLayer);
+ expect(handleLayer).toBeGreaterThan(bgLayer);
+
+ disposeApp(app, registry);
+ });
+ });
+
+ describe('dynamic fill changes across ticks', () => {
+ it('should update fill width when value changes', () => {
+ const { app, registry } = createEditorApp();
+ const { fill } = createSliderHierarchy(app);
+
+ setCanvasRect(app, -400, -300, 400, 300);
+
+ applyDirectionalFill(app.world, fill, FillDirection.LeftToRight, 0.3);
+ app.tick(1 / 60);
+ expect(module.getUIRectComputedWidth(registry, fill)).toBeCloseTo(60, 0);
+
+ applyDirectionalFill(app.world, fill, FillDirection.LeftToRight, 0.7);
+ app.tick(1 / 60);
+ expect(module.getUIRectComputedWidth(registry, fill)).toBeCloseTo(140, 0);
+
+ applyDirectionalFill(app.world, fill, FillDirection.LeftToRight, 0);
+ app.tick(1 / 60);
+ expect(module.getUIRectComputedWidth(registry, fill)).toBeCloseTo(0, 0);
+
+ disposeApp(app, registry);
+ });
+ });
+
+ describe('two sliders side by side', () => {
+ it('should compute independent layouts for two sliders', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const root = world.spawn();
+ world.insert(root, ScreenSpace, {});
+ world.insert(root, UIRect, makeFullRect());
+ world.insert(root, Transform, makeTransform());
+
+ // Slider 1 (top half)
+ const slider1Bg = world.spawn();
+ world.setParent(slider1Bg, root);
+ world.insert(slider1Bg, UIRect, {
+ anchorMin: { x: 0.5, y: 0.75 },
+ anchorMax: { x: 0.5, y: 0.75 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 200, y: 20 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(slider1Bg, Transform, makeTransform());
+ world.insert(slider1Bg, Sprite, makeSprite(200, 20));
+
+ const fill1 = world.spawn();
+ world.setParent(fill1, slider1Bg);
+ world.insert(fill1, UIRect, {
+ anchorMin: { x: 0, y: 0 },
+ anchorMax: { x: 0.3, y: 1 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 0, y: 0 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(fill1, Transform, makeTransform());
+ world.insert(fill1, Sprite, makeSprite(60, 20));
+
+ // Slider 2 (bottom half)
+ const slider2Bg = world.spawn();
+ world.setParent(slider2Bg, root);
+ world.insert(slider2Bg, UIRect, {
+ anchorMin: { x: 0.5, y: 0.25 },
+ anchorMax: { x: 0.5, y: 0.25 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 300, y: 20 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(slider2Bg, Transform, makeTransform());
+ world.insert(slider2Bg, Sprite, makeSprite(300, 20));
+
+ const fill2 = world.spawn();
+ world.setParent(fill2, slider2Bg);
+ world.insert(fill2, UIRect, {
+ anchorMin: { x: 0, y: 0 },
+ anchorMax: { x: 0.8, y: 1 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 0, y: 0 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(fill2, Transform, makeTransform());
+ world.insert(fill2, Sprite, makeSprite(240, 20));
+
+ setCanvasRect(app, -400, -300, 400, 300);
+ app.tick(1 / 60);
+
+ // Slider 1: bg = 200w, fill = 30% = 60w
+ expect(module.getUIRectComputedWidth(registry, slider1Bg)).toBeCloseTo(200, 0);
+ expect(module.getUIRectComputedWidth(registry, fill1)).toBeCloseTo(60, 0);
+ expect(module.getUIRectComputedHeight(registry, fill1)).toBeCloseTo(20, 0);
+
+ // Slider 2: bg = 300w, fill = 80% = 240w
+ expect(module.getUIRectComputedWidth(registry, slider2Bg)).toBeCloseTo(300, 0);
+ expect(module.getUIRectComputedWidth(registry, fill2)).toBeCloseTo(240, 0);
+ expect(module.getUIRectComputedHeight(registry, fill2)).toBeCloseTo(20, 0);
+
+ // Both backgrounds should have sprites
+ expect(registry.hasSprite(slider1Bg)).toBe(true);
+ expect(registry.hasSprite(slider2Bg)).toBe(true);
+ expect(registry.hasSprite(fill1)).toBe(true);
+ expect(registry.hasSprite(fill2)).toBe(true);
+
+ // Render order: fill should be above background
+ expect(registry.getSprite(fill1).layer).toBeGreaterThan(registry.getSprite(slider1Bg).layer);
+ expect(registry.getSprite(fill2).layer).toBeGreaterThan(registry.getSprite(slider2Bg).layer);
+
+ disposeApp(app, registry);
+ });
+ });
+
+ describe('fill sprite size sync via layout system', () => {
+ it('should update fill Sprite.size to match computed UIRect size', () => {
+ const { app, registry } = createEditorApp();
+ const { fill } = createSliderHierarchy(app);
+
+ applyDirectionalFill(app.world, fill, FillDirection.LeftToRight, 0.5);
+
+ setCanvasRect(app, -400, -300, 400, 300);
+ app.tick(1 / 60);
+
+ const sprite = registry.getSprite(fill);
+ const computedW = module.getUIRectComputedWidth(registry, fill);
+ const computedH = module.getUIRectComputedHeight(registry, fill);
+ expect(sprite.size.x).toBeCloseTo(computedW, 0);
+ expect(sprite.size.y).toBeCloseTo(computedH, 0);
+
+ disposeApp(app, registry);
+ });
+ });
+
+ describe('editor-like slider creation flow', () => {
+ it('should reproduce exact editor slider hierarchy and verify fill visibility', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ // Simulate editor scene: Canvas root → Slider → Fill + Handle
+ // This mirrors what HierarchyContextMenu.createSliderEntity does
+
+ const canvasRoot = world.spawn();
+ world.insert(canvasRoot, ScreenSpace, {});
+ world.insert(canvasRoot, UIRect, makeFullRect());
+ world.insert(canvasRoot, Transform, makeTransform());
+
+ // Slider background (same as editor: size 200x20, centered anchors)
+ const slider = world.spawn();
+ world.setParent(slider, canvasRoot);
+ world.insert(slider, UIRect, {
+ anchorMin: { x: 0.5, y: 0.5 },
+ anchorMax: { x: 0.5, y: 0.5 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 200, y: 20 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(slider, Transform, makeTransform());
+ // Editor gives slider a smaller Sprite (200x8) than UIRect (200x20)
+ world.insert(slider, Sprite, makeSprite(200, 8));
+
+ // Fill child (same as editor)
+ const fill = world.spawn();
+ world.setParent(fill, slider);
+ world.insert(fill, UIRect, {
+ anchorMin: { x: 0, y: 0 },
+ anchorMax: { x: 0.5, y: 1 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 100, y: 100 }, // default SDK size, ignored in stretch
+ pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(fill, Transform, makeTransform());
+ world.insert(fill, Sprite, makeSprite(200, 8));
+
+ // Handle child (same as editor)
+ const handle = world.spawn();
+ world.setParent(handle, slider);
+ world.insert(handle, UIRect, {
+ anchorMin: { x: 0.5, y: 0.5 },
+ anchorMax: { x: 0.5, y: 0.5 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 20, y: 20 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(handle, Transform, makeTransform());
+ world.insert(handle, Sprite, makeSprite(20, 20));
+
+ // Step 1: applyFillStates (editor calls this after loading scene)
+ applyDirectionalFill(world, fill, FillDirection.LeftToRight, 0.5);
+
+ // Verify fill UIRect anchors were set correctly
+ const fillRect = registry.getUIRect(fill);
+ expect(fillRect.anchorMin.x).toBeCloseTo(0);
+ expect(fillRect.anchorMin.y).toBeCloseTo(0);
+ expect(fillRect.anchorMax.x).toBeCloseTo(0.5);
+ expect(fillRect.anchorMax.y).toBeCloseTo(1);
+
+ // Step 2: render() calls tick() which runs C++ layout
+ setCanvasRect(app, -400, -300, 400, 300);
+ app.tick(1 / 60);
+
+ // Verify slider bg got correct size (fixed-size centered)
+ const sliderW = module.getUIRectComputedWidth(registry, slider);
+ const sliderH = module.getUIRectComputedHeight(registry, slider);
+ expect(sliderW).toBeCloseTo(200, 0);
+ expect(sliderH).toBeCloseTo(20, 0);
+
+ // Verify fill got correct size (50% of 200 = 100)
+ const fillW = module.getUIRectComputedWidth(registry, fill);
+ const fillH = module.getUIRectComputedHeight(registry, fill);
+ expect(fillW).toBeCloseTo(100, 0);
+ expect(fillH).toBeCloseTo(20, 0);
+
+ // Verify fill sprite was updated by C++ layout
+ const fillSprite = registry.getSprite(fill);
+ expect(fillSprite.size.x).toBeCloseTo(100, 0);
+ expect(fillSprite.size.y).toBeCloseTo(20, 0);
+
+ // Verify handle got correct size
+ const handleW = module.getUIRectComputedWidth(registry, handle);
+ const handleH = module.getUIRectComputedHeight(registry, handle);
+ expect(handleW).toBeCloseTo(20, 0);
+ expect(handleH).toBeCloseTo(20, 0);
+
+ // Verify render order
+ const sliderLayer = registry.getSprite(slider).layer;
+ const fillLayer = registry.getSprite(fill).layer;
+ const handleLayer = registry.getSprite(handle).layer;
+ expect(fillLayer).toBeGreaterThan(sliderLayer);
+ expect(handleLayer).toBeGreaterThan(sliderLayer);
+
+ disposeApp(app, registry);
+ });
+
+ it('should handle two sliders at different positions', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const canvasRoot = world.spawn();
+ world.insert(canvasRoot, ScreenSpace, {});
+ world.insert(canvasRoot, UIRect, makeFullRect());
+ world.insert(canvasRoot, Transform, makeTransform());
+
+ function createEditorSlider(yOffset: number, value: number) {
+ const bg = world.spawn();
+ world.setParent(bg, canvasRoot);
+ world.insert(bg, UIRect, {
+ anchorMin: { x: 0.5, y: 0.5 },
+ anchorMax: { x: 0.5, y: 0.5 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 200, y: 20 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(bg, Transform, {
+ position: { x: 0, y: yOffset, z: 0 },
+ rotation: { x: 0, y: 0, z: 0, w: 1 },
+ scale: { x: 1, y: 1, z: 1 },
+ });
+ world.insert(bg, Sprite, makeSprite(200, 8));
+
+ const fill = world.spawn();
+ world.setParent(fill, bg);
+ world.insert(fill, UIRect, {
+ anchorMin: { x: 0, y: 0 },
+ anchorMax: { x: value, y: 1 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 100, y: 100 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(fill, Transform, makeTransform());
+ world.insert(fill, Sprite, makeSprite(200, 8));
+
+ const handle = world.spawn();
+ world.setParent(handle, bg);
+ world.insert(handle, UIRect, {
+ anchorMin: { x: value, y: 0 },
+ anchorMax: { x: value, y: 1 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 20, y: 20 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(handle, Transform, makeTransform());
+ world.insert(handle, Sprite, makeSprite(20, 20));
+
+ return { bg, fill, handle };
+ }
+
+ const slider1 = createEditorSlider(50, 0.3);
+ const slider2 = createEditorSlider(-50, 0.7);
+
+ // Apply fill states (like editor does after load)
+ applyDirectionalFill(world, slider1.fill, FillDirection.LeftToRight, 0.3);
+ applyDirectionalFill(world, slider2.fill, FillDirection.LeftToRight, 0.7);
+
+ setCanvasRect(app, -400, -300, 400, 300);
+ app.tick(1 / 60);
+
+ // Slider 1: fill = 30% of 200 = 60
+ const fill1W = module.getUIRectComputedWidth(registry, slider1.fill);
+ expect(fill1W).toBeCloseTo(60, 0);
+ expect(module.getUIRectComputedHeight(registry, slider1.fill)).toBeCloseTo(20, 0);
+
+ // Slider 2: fill = 70% of 200 = 140
+ const fill2W = module.getUIRectComputedWidth(registry, slider2.fill);
+ expect(fill2W).toBeCloseTo(140, 0);
+ expect(module.getUIRectComputedHeight(registry, slider2.fill)).toBeCloseTo(20, 0);
+
+ // Both fills should have positive sprite sizes
+ const fill1Sprite = registry.getSprite(slider1.fill);
+ const fill2Sprite = registry.getSprite(slider2.fill);
+ expect(fill1Sprite.size.x).toBeGreaterThan(0);
+ expect(fill2Sprite.size.x).toBeGreaterThan(0);
+
+ // Both handles should be visible
+ const handle1Sprite = registry.getSprite(slider1.handle);
+ const handle2Sprite = registry.getSprite(slider2.handle);
+ expect(handle1Sprite.size.x).toBeCloseTo(20, 0);
+ expect(handle2Sprite.size.x).toBeCloseTo(20, 0);
+
+ // Both bgs should be rendered
+ expect(registry.getSprite(slider1.bg).size.x).toBeCloseTo(200, 0);
+ expect(registry.getSprite(slider2.bg).size.x).toBeCloseTo(200, 0);
+
+ disposeApp(app, registry);
+ });
+ });
+});
diff --git a/sdk/tests/slider-system.test.ts b/sdk/tests/slider-system.test.ts
new file mode 100644
index 000000000..5b2b1241c
--- /dev/null
+++ b/sdk/tests/slider-system.test.ts
@@ -0,0 +1,271 @@
+/**
+ * Integration test: verify SliderPlugin system produces correct fill layout.
+ * This uses the actual SliderPlugin (not just applyDirectionalFill) to
+ * catch system ordering issues.
+ */
+import { describe, it, expect, beforeAll } from 'vitest';
+import { existsSync } from 'fs';
+import { resolve } from 'path';
+import { App } from '../src/app';
+import { Transform, Sprite } from '../src/component';
+import { UIRect } from '../src/ui/UIRect';
+import { ScreenSpace } from '../src/ui/ScreenSpace';
+import { UICameraInfo } from '../src/ui/UICameraInfo';
+import { Input, InputState } from '../src/input';
+import { UIEvents, UIEventQueue } from '../src/ui/UIEvents';
+import { uiLayoutPlugin } from '../src/ui/UILayoutPlugin';
+import { uiRenderOrderPlugin } from '../src/ui/UIRenderOrderPlugin';
+import { uiInteractionPlugin } from '../src/ui/UIInteractionPlugin';
+import { sliderPlugin } from '../src/ui/SliderPlugin';
+import { Slider, FillDirection } from '../src/ui/Slider';
+import type { SliderData } from '../src/ui/Slider';
+import type { UIRectData } from '../src/ui/UIRect';
+import type { SpriteData, TransformData } from '../src/component';
+import type { ESEngineModule, CppRegistry } from '../src/wasm';
+import { loadWasmModule } from './helpers/loadWasm';
+import type { Entity } from '../src/types';
+
+const WASM_PATH = resolve(__dirname, '../../desktop/public/wasm/esengine.wasm');
+const HAS_WASM = existsSync(WASM_PATH);
+
+describe.skipIf(!HAS_WASM)('SliderPlugin system integration', () => {
+ let module: ESEngineModule;
+
+ beforeAll(async () => {
+ module = await loadWasmModule();
+ });
+
+ function createFullApp(): { app: App; registry: CppRegistry } {
+ const app = App.new();
+ const registry = new module.Registry() as unknown as CppRegistry;
+ app.connectCpp(registry, module);
+
+ app.insertResource(UICameraInfo, {
+ viewProjection: new Float32Array(16),
+ vpX: 0, vpY: 0, vpW: 0, vpH: 0,
+ screenW: 0, screenH: 0,
+ worldLeft: -100, worldBottom: -100, worldRight: 100, worldTop: 100,
+ worldMouseX: 0, worldMouseY: 0,
+ valid: true,
+ });
+ app.insertResource(Input, new InputState());
+ app.insertResource(UIEvents, new UIEventQueue());
+
+ app.addPlugin(uiLayoutPlugin);
+ app.addPlugin(uiRenderOrderPlugin);
+ app.addPlugin(uiInteractionPlugin);
+ app.addPlugin(sliderPlugin);
+
+ return { app, registry };
+ }
+
+ function makeTransform() {
+ return {
+ position: { x: 0, y: 0, z: 0 },
+ rotation: { x: 0, y: 0, z: 0, w: 1 },
+ scale: { x: 1, y: 1, z: 1 },
+ };
+ }
+
+ function makeSprite(w = 100, h = 100) {
+ return {
+ texture: 0,
+ color: { r: 1, g: 1, b: 1, a: 1 },
+ size: { x: w, y: h },
+ uvOffset: { x: 0, y: 0 },
+ uvScale: { x: 1, y: 1 },
+ layer: 0,
+ flipX: false,
+ flipY: false,
+ };
+ }
+
+ it('SliderPlugin should produce left-aligned fill (value=0.5)', () => {
+ const { app, registry } = createFullApp();
+ const world = app.world;
+
+ const root = world.spawn();
+ world.insert(root, ScreenSpace, {});
+ world.insert(root, UIRect, {
+ anchorMin: { x: 0, y: 0 }, anchorMax: { x: 1, y: 1 },
+ offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
+ size: { x: 0, y: 0 }, pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(root, Transform, makeTransform());
+
+ const sliderBg = world.spawn();
+ world.setParent(sliderBg, root);
+ world.insert(sliderBg, UIRect, {
+ anchorMin: { x: 0.5, y: 0.5 }, anchorMax: { x: 0.5, y: 0.5 },
+ offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
+ size: { x: 200, y: 20 }, pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(sliderBg, Transform, makeTransform());
+ world.insert(sliderBg, Sprite, makeSprite(200, 20));
+
+ const fill = world.spawn();
+ world.setParent(fill, sliderBg);
+ world.insert(fill, UIRect, {
+ anchorMin: { x: 0, y: 0 }, anchorMax: { x: 1, y: 1 },
+ offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
+ size: { x: 0, y: 0 }, pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(fill, Transform, makeTransform());
+ world.insert(fill, Sprite, makeSprite(200, 20));
+
+ const handle = world.spawn();
+ world.setParent(handle, sliderBg);
+ world.insert(handle, UIRect, {
+ anchorMin: { x: 0.5, y: 0 }, anchorMax: { x: 0.5, y: 1 },
+ offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
+ size: { x: 20, y: 20 }, pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(handle, Transform, makeTransform());
+ world.insert(handle, Sprite, makeSprite(20, 20));
+
+ world.insert(sliderBg, Slider, {
+ value: 0.5,
+ minValue: 0,
+ maxValue: 1,
+ direction: FillDirection.LeftToRight,
+ fillEntity: fill,
+ handleEntity: handle,
+ wholeNumbers: false,
+ });
+
+ for (let i = 0; i < 3; i++) {
+ app.tick(1 / 60);
+ }
+
+ // Run TransformSystem to compute worldPositions (normally done by renderer)
+ module.transform_update(registry);
+
+ const fillT = world.get(fill, Transform) as TransformData;
+ const fillS = world.get(fill, Sprite) as SpriteData;
+ const fillR = world.get(fill, UIRect) as UIRectData;
+ const bgT = world.get(sliderBg, Transform) as TransformData;
+
+ console.log('\n=== After SliderPlugin + TransformSystem ===');
+ console.log('slider bg position:', bgT.position.x, bgT.position.y);
+ console.log('slider bg worldPosition:', bgT.worldPosition.x, bgT.worldPosition.y);
+ console.log('fill position:', fillT.position.x, fillT.position.y);
+ console.log('fill worldPosition:', fillT.worldPosition.x, fillT.worldPosition.y);
+ console.log('fill sprite.size:', fillS.size.x, fillS.size.y);
+ console.log('fill UIRect anchors:', fillR.anchorMin.x, fillR.anchorMin.y, '->', fillR.anchorMax.x, fillR.anchorMax.y);
+
+ expect(fillS.size.x).toBeCloseTo(100, 0);
+ expect(fillT.position.x).toBeCloseTo(-50, 0);
+
+ // Verify worldPosition is correct (this is what the renderer uses)
+ expect(bgT.worldPosition.x).toBeCloseTo(0, 0);
+ expect(fillT.worldPosition.x).toBeCloseTo(-50, 0);
+
+ // Simulate renderer quad computation
+ const left = fillT.worldPosition.x - fillS.size.x * 0.5;
+ const right = fillT.worldPosition.x + fillS.size.x * 0.5;
+ const parentLeft = bgT.worldPosition.x - 100;
+ const parentRight = bgT.worldPosition.x + 100;
+
+ console.log('fill quad bounds: [', left, ',', right, ']');
+ console.log('parent quad bounds: [', parentLeft, ',', parentRight, ']');
+
+ // Fill left edge must align with parent left edge
+ expect(left).toBeCloseTo(parentLeft, 0);
+ // Fill right edge must be at parent center (50% fill)
+ expect(right).toBeCloseTo(0, 0);
+
+ for (const e of world.getAllEntities()) {
+ try { world.despawn(e); } catch (_) {}
+ }
+ world.disconnectCpp();
+ (registry as any).delete();
+ });
+
+ it('full pipeline: UILayout → SliderPlugin → UILayoutLate → TransformSystem', () => {
+ const { app, registry } = createFullApp();
+ const world = app.world;
+
+ const root = world.spawn();
+ world.insert(root, ScreenSpace, {});
+ world.insert(root, UIRect, {
+ anchorMin: { x: 0, y: 0 }, anchorMax: { x: 1, y: 1 },
+ offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
+ size: { x: 0, y: 0 }, pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(root, Transform, makeTransform());
+
+ const sliderBg = world.spawn();
+ world.setParent(sliderBg, root);
+ world.insert(sliderBg, UIRect, {
+ anchorMin: { x: 0.5, y: 0.5 }, anchorMax: { x: 0.5, y: 0.5 },
+ offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
+ size: { x: 200, y: 20 }, pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(sliderBg, Transform, makeTransform());
+ world.insert(sliderBg, Sprite, makeSprite(200, 20));
+
+ const fill = world.spawn();
+ world.setParent(fill, sliderBg);
+ world.insert(fill, UIRect, {
+ anchorMin: { x: 0, y: 0 }, anchorMax: { x: 1, y: 1 },
+ offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
+ size: { x: 0, y: 0 }, pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(fill, Transform, makeTransform());
+ world.insert(fill, Sprite, makeSprite(200, 20));
+
+ world.insert(sliderBg, Slider, {
+ value: 0,
+ minValue: 0,
+ maxValue: 1,
+ direction: FillDirection.LeftToRight,
+ fillEntity: fill,
+ handleEntity: 0 as Entity,
+ wholeNumbers: false,
+ });
+
+ const testValues = [0, 0.25, 0.5, 0.75, 1.0];
+ for (const value of testValues) {
+ world.insert(sliderBg, Slider, {
+ value,
+ minValue: 0,
+ maxValue: 1,
+ direction: FillDirection.LeftToRight,
+ fillEntity: fill,
+ handleEntity: 0 as Entity,
+ wholeNumbers: false,
+ });
+
+ app.tick(1 / 60);
+ module.transform_update(registry);
+
+ const fillT = world.get(fill, Transform) as TransformData;
+ const fillS = world.get(fill, Sprite) as SpriteData;
+ const bgT = world.get(sliderBg, Transform) as TransformData;
+
+ const expectedWidth = 200 * value;
+ const expectedPosX = value > 0 ? -(200 - expectedWidth) / 2 : 0;
+
+ // Simulate renderer quad computation
+ const quadLeft = fillT.worldPosition.x - fillS.size.x * 0.5;
+ const quadRight = fillT.worldPosition.x + fillS.size.x * 0.5;
+ const parentLeft = bgT.worldPosition.x - 100;
+
+ console.log(`\nvalue=${value}: pos.x=${fillT.position.x}, worldPos.x=${fillT.worldPosition.x}, sprite.size.x=${fillS.size.x}, quad=[${quadLeft}, ${quadRight}], parentLeft=${parentLeft}`);
+
+ expect(fillS.size.x).toBeCloseTo(expectedWidth, 0);
+ if (value > 0) {
+ expect(fillT.position.x).toBeCloseTo(expectedPosX, 0);
+ expect(fillT.worldPosition.x).toBeCloseTo(expectedPosX, 0);
+ // Fill quad left edge must align with parent left edge
+ expect(quadLeft).toBeCloseTo(parentLeft, 0);
+ }
+ }
+
+ for (const e of world.getAllEntities()) {
+ try { world.despawn(e); } catch (_) {}
+ }
+ world.disconnectCpp();
+ (registry as any).delete();
+ });
+});
diff --git a/sdk/tests/ui-components.test.ts b/sdk/tests/ui-components.test.ts
new file mode 100644
index 000000000..f468d858f
--- /dev/null
+++ b/sdk/tests/ui-components.test.ts
@@ -0,0 +1,715 @@
+/**
+ * Integration tests: UI components (UIRect, UIMask, FlexContainer, FlexItem,
+ * Interactable, UIInteraction, ScreenSpace) via real WASM module.
+ *
+ * Requires pre-built WASM at desktop/public/wasm/esengine.wasm.
+ * Run `node build-tools/cli.js build -t web` first if missing.
+ */
+import { describe, it, expect, beforeAll } from 'vitest';
+import { existsSync } from 'fs';
+import { resolve } from 'path';
+import { App } from '../src/app';
+import { World } from '../src/world';
+import { Transform, Sprite } from '../src/component';
+import { UIRect } from '../src/ui/UIRect';
+import { ScreenSpace } from '../src/ui/ScreenSpace';
+import { UIMask, MaskMode } from '../src/ui/UIMask';
+import { FlexContainer, FlexDirection, JustifyContent, AlignItems } from '../src/ui/FlexContainer';
+import { FlexItem } from '../src/ui/FlexItem';
+import { Interactable } from '../src/ui/Interactable';
+import { UIInteraction } from '../src/ui/UIInteraction';
+import { UICameraInfo, type UICameraData } from '../src/ui/UICameraInfo';
+import { uiLayoutPlugin } from '../src/ui/UILayoutPlugin';
+import { uiRenderOrderPlugin } from '../src/ui/UIRenderOrderPlugin';
+import type { ESEngineModule, CppRegistry } from '../src/wasm';
+import { loadWasmModule } from './helpers/loadWasm';
+
+const WASM_PATH = resolve(__dirname, '../../desktop/public/wasm/esengine.wasm');
+const HAS_WASM = existsSync(WASM_PATH);
+
+describe.skipIf(!HAS_WASM)('UI Components (WASM integration)', () => {
+ let module: ESEngineModule;
+
+ beforeAll(async () => {
+ module = await loadWasmModule();
+ });
+
+ function createEditorApp(): { app: App; registry: CppRegistry } {
+ const app = App.new();
+ const registry = new module.Registry() as unknown as CppRegistry;
+ app.connectCpp(registry, module);
+
+ app.insertResource(UICameraInfo, {
+ viewProjection: new Float32Array(16),
+ vpX: 0, vpY: 0, vpW: 0, vpH: 0,
+ screenW: 0, screenH: 0,
+ worldLeft: 0, worldBottom: 0, worldRight: 0, worldTop: 0,
+ worldMouseX: 0, worldMouseY: 0,
+ valid: false,
+ });
+
+ app.addPlugin(uiLayoutPlugin);
+ app.addPlugin(uiRenderOrderPlugin);
+ return { app, registry };
+ }
+
+ function disposeApp(app: App, registry: CppRegistry): void {
+ const world = app.world;
+ for (const e of world.getAllEntities()) {
+ try { world.despawn(e); } catch (_) {}
+ }
+ world.disconnectCpp();
+ (registry as any).delete();
+ }
+
+ function setCanvasRect(app: App, left: number, bottom: number, right: number, top: number): void {
+ const cam = app.getResource(UICameraInfo);
+ cam.worldLeft = left;
+ cam.worldBottom = bottom;
+ cam.worldRight = right;
+ cam.worldTop = top;
+ cam.valid = true;
+ }
+
+ function makeTransform() {
+ return {
+ position: { x: 0, y: 0, z: 0 },
+ rotation: { x: 0, y: 0, z: 0, w: 1 },
+ scale: { x: 1, y: 1, z: 1 },
+ };
+ }
+
+ function makeSprite(layer = 0) {
+ return {
+ texture: 0,
+ color: { r: 1, g: 1, b: 1, a: 1 },
+ size: { x: 100, y: 100 },
+ uvOffset: { x: 0, y: 0 },
+ uvScale: { x: 1, y: 1 },
+ layer,
+ flipX: false,
+ flipY: false,
+ };
+ }
+
+ function makeFullRect() {
+ return {
+ anchorMin: { x: 0, y: 0 },
+ anchorMax: { x: 1, y: 1 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 0, y: 0 },
+ pivot: { x: 0.5, y: 0.5 },
+ };
+ }
+
+ function makeCenterRect(w: number, h: number) {
+ return {
+ anchorMin: { x: 0.5, y: 0.5 },
+ anchorMax: { x: 0.5, y: 0.5 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: w, y: h },
+ pivot: { x: 0.5, y: 0.5 },
+ };
+ }
+
+ describe('UIRect CRUD', () => {
+ it('should insert and read UIRect via CppRegistry', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const entity = world.spawn();
+ world.insert(entity, UIRect, makeCenterRect(200, 100));
+
+ expect(registry.hasUIRect(entity)).toBe(true);
+
+ const rect = registry.getUIRect(entity);
+ expect(rect.size.x).toBeCloseTo(200);
+ expect(rect.size.y).toBeCloseTo(100);
+ expect(rect.pivot.x).toBeCloseTo(0.5);
+ expect(rect.pivot.y).toBeCloseTo(0.5);
+
+ disposeApp(app, registry);
+ });
+
+ it('should remove UIRect', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const entity = world.spawn();
+ world.insert(entity, UIRect, makeFullRect());
+ expect(registry.hasUIRect(entity)).toBe(true);
+
+ world.remove(entity, UIRect);
+ expect(registry.hasUIRect(entity)).toBe(false);
+
+ disposeApp(app, registry);
+ });
+ });
+
+ describe('UIMask', () => {
+ it('should insert UIMask with Scissor mode', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const entity = world.spawn();
+ world.insert(entity, UIMask, { enabled: true, mode: MaskMode.Scissor });
+
+ expect(registry.hasUIMask(entity)).toBe(true);
+ const mask = registry.getUIMask(entity);
+ expect(mask.enabled).toBe(true);
+ expect(mask.mode).toBe(MaskMode.Scissor);
+
+ disposeApp(app, registry);
+ });
+
+ it('should insert UIMask with Stencil mode', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const entity = world.spawn();
+ world.insert(entity, UIMask, { enabled: true, mode: MaskMode.Stencil });
+
+ const mask = registry.getUIMask(entity);
+ expect(mask.mode).toBe(MaskMode.Stencil);
+
+ disposeApp(app, registry);
+ });
+
+ it('should remove UIMask', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const entity = world.spawn();
+ world.insert(entity, UIMask, { enabled: true, mode: MaskMode.Scissor });
+ world.remove(entity, UIMask);
+
+ expect(registry.hasUIMask(entity)).toBe(false);
+
+ disposeApp(app, registry);
+ });
+ });
+
+ describe('FlexContainer and FlexItem', () => {
+ it('should insert FlexContainer with default values', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const entity = world.spawn();
+ world.insert(entity, FlexContainer);
+
+ expect(registry.hasFlexContainer(entity)).toBe(true);
+ const fc = registry.getFlexContainer(entity);
+ expect(fc.direction).toBe(FlexDirection.Row);
+ expect(fc.justifyContent).toBe(JustifyContent.Start);
+ expect(fc.alignItems).toBe(AlignItems.Stretch);
+
+ disposeApp(app, registry);
+ });
+
+ it('should insert FlexContainer with custom values', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const entity = world.spawn();
+ world.insert(entity, FlexContainer, {
+ direction: FlexDirection.Column,
+ justifyContent: JustifyContent.Center,
+ alignItems: AlignItems.Center,
+ gap: { x: 10, y: 5 },
+ padding: { x: 8, y: 8, z: 8, w: 8 },
+ });
+
+ const fc = registry.getFlexContainer(entity);
+ expect(fc.direction).toBe(FlexDirection.Column);
+ expect(fc.justifyContent).toBe(JustifyContent.Center);
+ expect(fc.alignItems).toBe(AlignItems.Center);
+ expect(fc.gap.x).toBeCloseTo(10);
+ expect(fc.gap.y).toBeCloseTo(5);
+
+ disposeApp(app, registry);
+ });
+
+ it('should insert FlexItem with defaults', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const entity = world.spawn();
+ world.insert(entity, FlexItem);
+
+ expect(registry.hasFlexItem(entity)).toBe(true);
+ const fi = registry.getFlexItem(entity);
+ expect(fi.flexGrow).toBeCloseTo(0);
+ expect(fi.flexShrink).toBeCloseTo(1);
+ expect(fi.order).toBe(0);
+
+ disposeApp(app, registry);
+ });
+
+ it('should insert FlexItem with custom grow/shrink', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const entity = world.spawn();
+ world.insert(entity, FlexItem, {
+ flexGrow: 2,
+ flexShrink: 0,
+ flexBasis: 100,
+ order: 3,
+ });
+
+ const fi = registry.getFlexItem(entity);
+ expect(fi.flexGrow).toBeCloseTo(2);
+ expect(fi.flexShrink).toBeCloseTo(0);
+ expect(fi.flexBasis).toBeCloseTo(100);
+ expect(fi.order).toBe(3);
+
+ disposeApp(app, registry);
+ });
+
+ it('should remove FlexContainer and FlexItem', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const entity = world.spawn();
+ world.insert(entity, FlexContainer);
+ world.insert(entity, FlexItem);
+
+ world.remove(entity, FlexContainer);
+ world.remove(entity, FlexItem);
+
+ expect(registry.hasFlexContainer(entity)).toBe(false);
+ expect(registry.hasFlexItem(entity)).toBe(false);
+
+ disposeApp(app, registry);
+ });
+ });
+
+ describe('Interactable and UIInteraction', () => {
+ it('should insert Interactable with defaults', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const entity = world.spawn();
+ world.insert(entity, Interactable);
+
+ expect(registry.hasInteractable(entity)).toBe(true);
+ const inter = registry.getInteractable(entity);
+ expect(inter.enabled).toBe(true);
+ expect(inter.blockRaycast).toBe(true);
+ expect(inter.raycastTarget).toBe(true);
+
+ disposeApp(app, registry);
+ });
+
+ it('should insert disabled Interactable', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const entity = world.spawn();
+ world.insert(entity, Interactable, {
+ enabled: false,
+ blockRaycast: false,
+ raycastTarget: false,
+ });
+
+ const inter = registry.getInteractable(entity);
+ expect(inter.enabled).toBe(false);
+ expect(inter.blockRaycast).toBe(false);
+ expect(inter.raycastTarget).toBe(false);
+
+ disposeApp(app, registry);
+ });
+
+ it('should insert UIInteraction with defaults', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const entity = world.spawn();
+ world.insert(entity, UIInteraction);
+
+ expect(registry.hasUIInteraction(entity)).toBe(true);
+ const ui = registry.getUIInteraction(entity);
+ expect(ui.hovered).toBe(false);
+ expect(ui.pressed).toBe(false);
+ expect(ui.justPressed).toBe(false);
+ expect(ui.justReleased).toBe(false);
+
+ disposeApp(app, registry);
+ });
+ });
+
+ describe('ScreenSpace', () => {
+ it('should insert and check ScreenSpace tag', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const entity = world.spawn();
+ world.insert(entity, ScreenSpace, {});
+
+ expect(registry.hasScreenSpace(entity)).toBe(true);
+
+ disposeApp(app, registry);
+ });
+
+ it('should remove ScreenSpace', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const entity = world.spawn();
+ world.insert(entity, ScreenSpace, {});
+ world.remove(entity, ScreenSpace);
+
+ expect(registry.hasScreenSpace(entity)).toBe(false);
+
+ disposeApp(app, registry);
+ });
+ });
+
+ describe('UI layout with deep hierarchy', () => {
+ it('should compute layout for 3-level nested UIRect', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const root = world.spawn();
+ world.insert(root, ScreenSpace, {});
+ world.insert(root, UIRect, makeFullRect());
+ world.insert(root, Transform, makeTransform());
+
+ const mid = world.spawn();
+ world.setParent(mid, root);
+ world.insert(mid, UIRect, {
+ anchorMin: { x: 0, y: 0 },
+ anchorMax: { x: 1, y: 1 },
+ offsetMin: { x: 20, y: 20 },
+ offsetMax: { x: -20, y: -20 },
+ size: { x: 0, y: 0 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(mid, Transform, makeTransform());
+
+ const leaf = world.spawn();
+ world.setParent(leaf, mid);
+ world.insert(leaf, UIRect, {
+ anchorMin: { x: 0, y: 0 },
+ anchorMax: { x: 1, y: 1 },
+ offsetMin: { x: 10, y: 10 },
+ offsetMax: { x: -10, y: -10 },
+ size: { x: 0, y: 0 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(leaf, Transform, makeTransform());
+ world.insert(leaf, Sprite, makeSprite());
+
+ setCanvasRect(app, -400, -300, 400, 300);
+ app.tick(1 / 60);
+
+ const rootW = module.getUIRectComputedWidth(registry, root);
+ const rootH = module.getUIRectComputedHeight(registry, root);
+ expect(rootW).toBeCloseTo(800, 0);
+ expect(rootH).toBeCloseTo(600, 0);
+
+ const midW = module.getUIRectComputedWidth(registry, mid);
+ const midH = module.getUIRectComputedHeight(registry, mid);
+ expect(midW).toBeCloseTo(760, 0);
+ expect(midH).toBeCloseTo(560, 0);
+
+ const leafW = module.getUIRectComputedWidth(registry, leaf);
+ const leafH = module.getUIRectComputedHeight(registry, leaf);
+ expect(leafW).toBeCloseTo(740, 0);
+ expect(leafH).toBeCloseTo(540, 0);
+
+ disposeApp(app, registry);
+ });
+
+ it('should compute fixed-size centered child correctly', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const root = world.spawn();
+ world.insert(root, ScreenSpace, {});
+ world.insert(root, UIRect, makeFullRect());
+ world.insert(root, Transform, makeTransform());
+
+ const child = world.spawn();
+ world.setParent(child, root);
+ world.insert(child, UIRect, makeCenterRect(200, 150));
+ world.insert(child, Transform, makeTransform());
+ world.insert(child, Sprite, makeSprite());
+
+ setCanvasRect(app, -400, -300, 400, 300);
+ app.tick(1 / 60);
+
+ const w = module.getUIRectComputedWidth(registry, child);
+ const h = module.getUIRectComputedHeight(registry, child);
+ expect(w).toBeCloseTo(200, 0);
+ expect(h).toBeCloseTo(150, 0);
+
+ disposeApp(app, registry);
+ });
+ });
+
+ describe('render order with multiple roots', () => {
+ it('should assign render order across sibling entities', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const root = world.spawn();
+ world.insert(root, ScreenSpace, {});
+ world.insert(root, UIRect, makeFullRect());
+ world.insert(root, Transform, makeTransform());
+ world.insert(root, Sprite, makeSprite());
+
+ const children: number[] = [];
+ for (let i = 0; i < 5; i++) {
+ const child = world.spawn();
+ world.setParent(child, root);
+ world.insert(child, UIRect, makeCenterRect(50, 50));
+ world.insert(child, Transform, makeTransform());
+ world.insert(child, Sprite, makeSprite());
+ children.push(child);
+ }
+
+ setCanvasRect(app, -400, -300, 400, 300);
+ app.tick(1 / 60);
+
+ const rootLayer = registry.getSprite(root).layer;
+ for (const child of children) {
+ const childLayer = registry.getSprite(child).layer;
+ expect(childLayer).toBeGreaterThan(rootLayer);
+ }
+
+ disposeApp(app, registry);
+ });
+
+ it('should maintain correct order: parent < child < grandchild', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const root = world.spawn();
+ world.insert(root, ScreenSpace, {});
+ world.insert(root, UIRect, makeFullRect());
+ world.insert(root, Transform, makeTransform());
+ world.insert(root, Sprite, makeSprite());
+
+ const mid = world.spawn();
+ world.setParent(mid, root);
+ world.insert(mid, UIRect, makeFullRect());
+ world.insert(mid, Transform, makeTransform());
+ world.insert(mid, Sprite, makeSprite());
+
+ const leaf = world.spawn();
+ world.setParent(leaf, mid);
+ world.insert(leaf, UIRect, makeCenterRect(50, 50));
+ world.insert(leaf, Transform, makeTransform());
+ world.insert(leaf, Sprite, makeSprite());
+
+ setCanvasRect(app, -400, -300, 400, 300);
+ app.tick(1 / 60);
+
+ const rootLayer = registry.getSprite(root).layer;
+ const midLayer = registry.getSprite(mid).layer;
+ const leafLayer = registry.getSprite(leaf).layer;
+
+ expect(midLayer).toBeGreaterThan(rootLayer);
+ expect(leafLayer).toBeGreaterThan(midLayer);
+
+ disposeApp(app, registry);
+ });
+ });
+
+ describe('anchor-based layout variations', () => {
+ it('should compute left-anchored child (25% width)', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const root = world.spawn();
+ world.insert(root, ScreenSpace, {});
+ world.insert(root, UIRect, makeFullRect());
+ world.insert(root, Transform, makeTransform());
+
+ const child = world.spawn();
+ world.setParent(child, root);
+ world.insert(child, UIRect, {
+ anchorMin: { x: 0, y: 0 },
+ anchorMax: { x: 0.25, y: 1 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 0, y: 0 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(child, Transform, makeTransform());
+ world.insert(child, Sprite, makeSprite());
+
+ setCanvasRect(app, -400, -300, 400, 300);
+ app.tick(1 / 60);
+
+ const w = module.getUIRectComputedWidth(registry, child);
+ const h = module.getUIRectComputedHeight(registry, child);
+ expect(w).toBeCloseTo(200, 0);
+ expect(h).toBeCloseTo(600, 0);
+
+ disposeApp(app, registry);
+ });
+
+ it('should compute bottom-anchored child (50% height)', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const root = world.spawn();
+ world.insert(root, ScreenSpace, {});
+ world.insert(root, UIRect, makeFullRect());
+ world.insert(root, Transform, makeTransform());
+
+ const child = world.spawn();
+ world.setParent(child, root);
+ world.insert(child, UIRect, {
+ anchorMin: { x: 0, y: 0 },
+ anchorMax: { x: 1, y: 0.5 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 0, y: 0 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(child, Transform, makeTransform());
+ world.insert(child, Sprite, makeSprite());
+
+ setCanvasRect(app, -400, -300, 400, 300);
+ app.tick(1 / 60);
+
+ const w = module.getUIRectComputedWidth(registry, child);
+ const h = module.getUIRectComputedHeight(registry, child);
+ expect(w).toBeCloseTo(800, 0);
+ expect(h).toBeCloseTo(300, 0);
+
+ disposeApp(app, registry);
+ });
+
+ it('should compute offset from edges', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const root = world.spawn();
+ world.insert(root, ScreenSpace, {});
+ world.insert(root, UIRect, makeFullRect());
+ world.insert(root, Transform, makeTransform());
+
+ const child = world.spawn();
+ world.setParent(child, root);
+ world.insert(child, UIRect, {
+ anchorMin: { x: 0, y: 0 },
+ anchorMax: { x: 1, y: 1 },
+ offsetMin: { x: 50, y: 50 },
+ offsetMax: { x: -50, y: -50 },
+ size: { x: 0, y: 0 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(child, Transform, makeTransform());
+ world.insert(child, Sprite, makeSprite());
+
+ setCanvasRect(app, -400, -300, 400, 300);
+ app.tick(1 / 60);
+
+ const w = module.getUIRectComputedWidth(registry, child);
+ const h = module.getUIRectComputedHeight(registry, child);
+ expect(w).toBeCloseTo(700, 0);
+ expect(h).toBeCloseTo(500, 0);
+
+ disposeApp(app, registry);
+ });
+ });
+
+ describe('dynamic layout changes', () => {
+ it('should update layout when anchors change between ticks', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const root = world.spawn();
+ world.insert(root, ScreenSpace, {});
+ world.insert(root, UIRect, makeFullRect());
+ world.insert(root, Transform, makeTransform());
+
+ const child = world.spawn();
+ world.setParent(child, root);
+ world.insert(child, UIRect, {
+ anchorMin: { x: 0, y: 0 },
+ anchorMax: { x: 0.5, y: 1 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 0, y: 0 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(child, Transform, makeTransform());
+ world.insert(child, Sprite, makeSprite());
+
+ setCanvasRect(app, -400, -300, 400, 300);
+ app.tick(1 / 60);
+
+ expect(module.getUIRectComputedWidth(registry, child)).toBeCloseTo(400, 0);
+
+ const rect = registry.getUIRect(child);
+ rect.anchorMax.x = 1;
+ registry.addUIRect(child, rect);
+
+ app.tick(1 / 60);
+
+ expect(module.getUIRectComputedWidth(registry, child)).toBeCloseTo(800, 0);
+
+ disposeApp(app, registry);
+ });
+
+ it('should handle adding new child between ticks', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const root = world.spawn();
+ world.insert(root, ScreenSpace, {});
+ world.insert(root, UIRect, makeFullRect());
+ world.insert(root, Transform, makeTransform());
+
+ setCanvasRect(app, -400, -300, 400, 300);
+ app.tick(1 / 60);
+
+ const child = world.spawn();
+ world.setParent(child, root);
+ world.insert(child, UIRect, makeCenterRect(300, 200));
+ world.insert(child, Transform, makeTransform());
+ world.insert(child, Sprite, makeSprite());
+
+ app.tick(1 / 60);
+
+ const w = module.getUIRectComputedWidth(registry, child);
+ const h = module.getUIRectComputedHeight(registry, child);
+ expect(w).toBeCloseTo(300, 0);
+ expect(h).toBeCloseTo(200, 0);
+
+ disposeApp(app, registry);
+ });
+
+ it('should handle changing canvas rect between ticks', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const root = world.spawn();
+ world.insert(root, ScreenSpace, {});
+ world.insert(root, UIRect, makeFullRect());
+ world.insert(root, Transform, makeTransform());
+ world.insert(root, Sprite, makeSprite());
+
+ setCanvasRect(app, -400, -300, 400, 300);
+ app.tick(1 / 60);
+
+ expect(module.getUIRectComputedWidth(registry, root)).toBeCloseTo(800, 0);
+ expect(module.getUIRectComputedHeight(registry, root)).toBeCloseTo(600, 0);
+
+ setCanvasRect(app, -500, -400, 500, 400);
+ app.tick(1 / 60);
+
+ expect(module.getUIRectComputedWidth(registry, root)).toBeCloseTo(1000, 0);
+ expect(module.getUIRectComputedHeight(registry, root)).toBeCloseTo(800, 0);
+
+ disposeApp(app, registry);
+ });
+ });
+});
diff --git a/sdk/tests/ui-layout-integration.test.ts b/sdk/tests/ui-layout-integration.test.ts
new file mode 100644
index 000000000..35ef473a0
--- /dev/null
+++ b/sdk/tests/ui-layout-integration.test.ts
@@ -0,0 +1,329 @@
+/**
+ * Integration tests: App.tick() drives UI layout via real WASM module.
+ *
+ * Requires pre-built WASM at desktop/public/wasm/esengine.wasm.
+ * Run `node build-tools/cli.js build -t web` first if missing.
+ */
+import { describe, it, expect, beforeAll } from 'vitest';
+import { existsSync } from 'fs';
+import { resolve } from 'path';
+import { App } from '../src/app';
+import { World } from '../src/world';
+import { UIRect, type UIRectData } from '../src/ui/UIRect';
+import { ScreenSpace } from '../src/ui/ScreenSpace';
+import { UICameraInfo, type UICameraData } from '../src/ui/UICameraInfo';
+import { uiLayoutPlugin } from '../src/ui/UILayoutPlugin';
+import { uiRenderOrderPlugin } from '../src/ui/UIRenderOrderPlugin';
+import { Transform, Sprite } from '../src/component';
+import type { ESEngineModule, CppRegistry } from '../src/wasm';
+import { loadWasmModule } from './helpers/loadWasm';
+
+const WASM_PATH = resolve(__dirname, '../../desktop/public/wasm/esengine.wasm');
+const HAS_WASM = existsSync(WASM_PATH);
+
+describe.skipIf(!HAS_WASM)('UI Layout via App.tick() (WASM integration)', () => {
+ let module: ESEngineModule;
+
+ beforeAll(async () => {
+ module = await loadWasmModule();
+ });
+
+ function createEditorApp(): { app: App; registry: CppRegistry } {
+ const app = App.new();
+ const registry = new module.Registry() as unknown as CppRegistry;
+ app.connectCpp(registry, module);
+
+ app.insertResource(UICameraInfo, {
+ viewProjection: new Float32Array(16),
+ vpX: 0, vpY: 0, vpW: 0, vpH: 0,
+ screenW: 0, screenH: 0,
+ worldLeft: 0, worldBottom: 0, worldRight: 0, worldTop: 0,
+ worldMouseX: 0, worldMouseY: 0,
+ valid: false,
+ });
+
+ app.addPlugin(uiLayoutPlugin);
+ app.addPlugin(uiRenderOrderPlugin);
+ return { app, registry };
+ }
+
+ function disposeApp(app: App, registry: CppRegistry): void {
+ const world = app.world;
+ for (const e of world.getAllEntities()) {
+ try { world.despawn(e); } catch (_) {}
+ }
+ world.disconnectCpp();
+ (registry as any).delete();
+ }
+
+ function setCanvasRect(app: App, left: number, bottom: number, right: number, top: number): void {
+ const cam = app.getResource(UICameraInfo);
+ cam.worldLeft = left;
+ cam.worldBottom = bottom;
+ cam.worldRight = right;
+ cam.worldTop = top;
+ cam.valid = true;
+ }
+
+ it('should call uiLayout_update via tick without error', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const root = world.spawn();
+ world.insert(root, ScreenSpace, {});
+ world.insert(root, UIRect, {
+ anchorMin: { x: 0, y: 0 },
+ anchorMax: { x: 1, y: 1 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 0, y: 0 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(root, Transform, {
+ position: { x: 0, y: 0, z: 0 },
+ rotation: { x: 0, y: 0, z: 0, w: 1 },
+ scale: { x: 1, y: 1, z: 1 },
+ });
+
+ setCanvasRect(app, -400, -300, 400, 300);
+
+ expect(() => app.tick(1 / 60)).not.toThrow();
+
+ disposeApp(app, registry);
+ });
+
+ it('should compute layout for child UIRect entities', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const root = world.spawn();
+ world.insert(root, ScreenSpace, {});
+ world.insert(root, UIRect, {
+ anchorMin: { x: 0, y: 0 },
+ anchorMax: { x: 1, y: 1 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 0, y: 0 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(root, Transform, {
+ position: { x: 0, y: 0, z: 0 },
+ rotation: { x: 0, y: 0, z: 0, w: 1 },
+ scale: { x: 1, y: 1, z: 1 },
+ });
+
+ const child = world.spawn();
+ world.setParent(child, root);
+ world.insert(child, UIRect, {
+ anchorMin: { x: 0, y: 0 },
+ anchorMax: { x: 1, y: 1 },
+ offsetMin: { x: 10, y: 10 },
+ offsetMax: { x: -10, y: -10 },
+ size: { x: 0, y: 0 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(child, Transform, {
+ position: { x: 0, y: 0, z: 0 },
+ rotation: { x: 0, y: 0, z: 0, w: 1 },
+ scale: { x: 1, y: 1, z: 1 },
+ });
+ world.insert(child, Sprite, {
+ texture: 0,
+ color: { r: 1, g: 1, b: 1, a: 1 },
+ size: { x: 100, y: 100 },
+ uvOffset: { x: 0, y: 0 },
+ uvScale: { x: 1, y: 1 },
+ layer: 0,
+ flipX: false,
+ flipY: false,
+ });
+
+ setCanvasRect(app, -400, -300, 400, 300);
+
+ app.tick(1 / 60);
+
+ const computedW = module.getUIRectComputedWidth(registry, child);
+ const computedH = module.getUIRectComputedHeight(registry, child);
+ expect(computedW).toBeCloseTo(780, 0);
+ expect(computedH).toBeCloseTo(580, 0);
+
+ disposeApp(app, registry);
+ });
+
+ it('should update render order after tick', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const root = world.spawn();
+ world.insert(root, ScreenSpace, {});
+ world.insert(root, UIRect, {
+ anchorMin: { x: 0, y: 0 }, anchorMax: { x: 1, y: 1 },
+ offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
+ size: { x: 0, y: 0 }, pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(root, Transform, {
+ position: { x: 0, y: 0, z: 0 },
+ rotation: { x: 0, y: 0, z: 0, w: 1 },
+ scale: { x: 1, y: 1, z: 1 },
+ });
+ world.insert(root, Sprite, {
+ texture: 0, color: { r: 1, g: 1, b: 1, a: 1 },
+ size: { x: 800, y: 600 },
+ uvOffset: { x: 0, y: 0 }, uvScale: { x: 1, y: 1 },
+ layer: 0, flipX: false, flipY: false,
+ });
+
+ const child = world.spawn();
+ world.setParent(child, root);
+ world.insert(child, UIRect, {
+ anchorMin: { x: 0.5, y: 0.5 }, anchorMax: { x: 0.5, y: 0.5 },
+ offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
+ size: { x: 100, y: 100 }, pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(child, Transform, {
+ position: { x: 0, y: 0, z: 0 },
+ rotation: { x: 0, y: 0, z: 0, w: 1 },
+ scale: { x: 1, y: 1, z: 1 },
+ });
+ world.insert(child, Sprite, {
+ texture: 0, color: { r: 1, g: 1, b: 1, a: 1 },
+ size: { x: 100, y: 100 },
+ uvOffset: { x: 0, y: 0 }, uvScale: { x: 1, y: 1 },
+ layer: 0, flipX: false, flipY: false,
+ });
+
+ setCanvasRect(app, -400, -300, 400, 300);
+
+ app.tick(1 / 60);
+
+ const rootSprite = registry.getSprite(root);
+ const childSprite = registry.getSprite(child);
+ expect(childSprite.layer).toBeGreaterThan(rootSprite.layer);
+
+ disposeApp(app, registry);
+ });
+
+ it('should handle fill anchors for slider-like entities', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const root = world.spawn();
+ world.insert(root, ScreenSpace, {});
+ world.insert(root, UIRect, {
+ anchorMin: { x: 0, y: 0 }, anchorMax: { x: 1, y: 1 },
+ offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
+ size: { x: 0, y: 0 }, pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(root, Transform, {
+ position: { x: 0, y: 0, z: 0 },
+ rotation: { x: 0, y: 0, z: 0, w: 1 },
+ scale: { x: 1, y: 1, z: 1 },
+ });
+
+ const background = world.spawn();
+ world.setParent(background, root);
+ world.insert(background, UIRect, {
+ anchorMin: { x: 0, y: 0 }, anchorMax: { x: 1, y: 1 },
+ offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
+ size: { x: 0, y: 0 }, pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(background, Transform, {
+ position: { x: 0, y: 0, z: 0 },
+ rotation: { x: 0, y: 0, z: 0, w: 1 },
+ scale: { x: 1, y: 1, z: 1 },
+ });
+ world.insert(background, Sprite, {
+ texture: 0, color: { r: 0.5, g: 0.5, b: 0.5, a: 1 },
+ size: { x: 200, y: 30 },
+ uvOffset: { x: 0, y: 0 }, uvScale: { x: 1, y: 1 },
+ layer: 0, flipX: false, flipY: false,
+ });
+
+ const fill = world.spawn();
+ world.setParent(fill, background);
+ world.insert(fill, UIRect, {
+ anchorMin: { x: 0, y: 0 }, anchorMax: { x: 0.5, y: 1 },
+ offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
+ size: { x: 0, y: 0 }, pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(fill, Transform, {
+ position: { x: 0, y: 0, z: 0 },
+ rotation: { x: 0, y: 0, z: 0, w: 1 },
+ scale: { x: 1, y: 1, z: 1 },
+ });
+ world.insert(fill, Sprite, {
+ texture: 0, color: { r: 0, g: 0.8, b: 0, a: 1 },
+ size: { x: 100, y: 30 },
+ uvOffset: { x: 0, y: 0 }, uvScale: { x: 1, y: 1 },
+ layer: 0, flipX: false, flipY: false,
+ });
+
+ setCanvasRect(app, -400, -300, 400, 300);
+
+ app.tick(1 / 60);
+
+ const fillW = module.getUIRectComputedWidth(registry, fill);
+ const fillH = module.getUIRectComputedHeight(registry, fill);
+ const bgW = module.getUIRectComputedWidth(registry, background);
+
+ expect(fillW).toBeGreaterThan(0);
+ expect(fillH).toBeGreaterThan(0);
+ expect(fillW).toBeCloseTo(bgW * 0.5, 0);
+
+ disposeApp(app, registry);
+ });
+
+ it('should handle multiple tick calls without error', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const root = world.spawn();
+ world.insert(root, ScreenSpace, {});
+ world.insert(root, UIRect, {
+ anchorMin: { x: 0, y: 0 }, anchorMax: { x: 1, y: 1 },
+ offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
+ size: { x: 0, y: 0 }, pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(root, Transform, {
+ position: { x: 0, y: 0, z: 0 },
+ rotation: { x: 0, y: 0, z: 0, w: 1 },
+ scale: { x: 1, y: 1, z: 1 },
+ });
+
+ setCanvasRect(app, -400, -300, 400, 300);
+
+ for (let i = 0; i < 60; i++) {
+ expect(() => app.tick(1 / 60)).not.toThrow();
+ }
+
+ disposeApp(app, registry);
+ });
+
+ it('should skip layout when UICameraInfo.valid is false', () => {
+ const { app, registry } = createEditorApp();
+ const world = app.world;
+
+ const root = world.spawn();
+ world.insert(root, ScreenSpace, {});
+ world.insert(root, UIRect, {
+ anchorMin: { x: 0, y: 0 }, anchorMax: { x: 1, y: 1 },
+ offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
+ size: { x: 0, y: 0 }, pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(root, Transform, {
+ position: { x: 0, y: 0, z: 0 },
+ rotation: { x: 0, y: 0, z: 0, w: 1 },
+ scale: { x: 1, y: 1, z: 1 },
+ });
+
+ const cam = app.getResource(UICameraInfo);
+ cam.valid = false;
+
+ expect(() => app.tick(1 / 60)).not.toThrow();
+
+ const w = module.getUIRectComputedWidth(registry, root);
+ expect(w).toBe(0);
+
+ disposeApp(app, registry);
+ });
+});