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 tests for multi-touch, CameraUtils, and scene loading progress

esengineb23438cff356
diff --git a/sdk/tests/camera-utils.test.ts b/sdk/tests/camera-utils.test.ts
new file mode 100644
index 000000000..2d37103bb
--- /dev/null
+++ b/sdk/tests/camera-utils.test.ts
@@ -0,0 +1,96 @@
+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
+import { CameraUtils, initCameraAPI, shutdownCameraAPI } from '../src/camera/Camera';
+
+const mockUICameraData = {
+ viewProjection: new Float32Array([
+ 1, 0, 0, 0,
+ 0, 1, 0, 0,
+ 0, 0, 1, 0,
+ 0, 0, 0, 1,
+ ]),
+ vpX: 0,
+ vpY: 0,
+ vpW: 800,
+ vpH: 600,
+ screenW: 800,
+ screenH: 600,
+ worldLeft: -400,
+ worldRight: 400,
+ worldBottom: -300,
+ worldTop: 300,
+ worldMouseX: 50,
+ worldMouseY: -25,
+ valid: true,
+};
+
+function createMockApp(cameraData = mockUICameraData) {
+ return {
+ getResource: vi.fn(() => cameraData),
+ } as any;
+}
+
+describe('CameraUtils', () => {
+ afterEach(() => {
+ shutdownCameraAPI();
+ });
+
+ it('should return null when not initialized', () => {
+ expect(CameraUtils.screenToWorld(400, 300)).toBeNull();
+ expect(CameraUtils.worldToScreen(0, 0)).toBeNull();
+ expect(CameraUtils.getWorldMousePosition()).toBeNull();
+ expect(CameraUtils.getWorldBounds()).toBeNull();
+ });
+
+ it('should return null when camera is invalid', () => {
+ const app = createMockApp({ ...mockUICameraData, valid: false });
+ initCameraAPI(app);
+
+ expect(CameraUtils.screenToWorld(400, 300)).toBeNull();
+ expect(CameraUtils.worldToScreen(0, 0)).toBeNull();
+ });
+
+ it('should convert screen center to world origin with identity VP', () => {
+ initCameraAPI(createMockApp());
+
+ const result = CameraUtils.screenToWorld(400, 300);
+ expect(result).not.toBeNull();
+ expect(result!.x).toBeCloseTo(0, 1);
+ expect(result!.y).toBeCloseTo(0, 1);
+ });
+
+ it('should convert world origin back to screen center', () => {
+ initCameraAPI(createMockApp());
+
+ const result = CameraUtils.worldToScreen(0, 0);
+ expect(result).not.toBeNull();
+ expect(result!.x).toBeCloseTo(400, 0);
+ expect(result!.y).toBeCloseTo(300, 0);
+ });
+
+ it('should return world mouse position', () => {
+ initCameraAPI(createMockApp());
+
+ const result = CameraUtils.getWorldMousePosition();
+ expect(result).toEqual({ x: 50, y: -25 });
+ });
+
+ it('should return world bounds', () => {
+ initCameraAPI(createMockApp());
+
+ const bounds = CameraUtils.getWorldBounds();
+ expect(bounds).toEqual({
+ left: -400,
+ right: 400,
+ bottom: -300,
+ top: 300,
+ });
+ });
+
+ it('should return null after shutdown', () => {
+ initCameraAPI(createMockApp());
+ expect(CameraUtils.getWorldMousePosition()).not.toBeNull();
+
+ shutdownCameraAPI();
+ expect(CameraUtils.getWorldMousePosition()).toBeNull();
+ });
+});
diff --git a/sdk/tests/input-multitouch.test.ts b/sdk/tests/input-multitouch.test.ts
new file mode 100644
index 000000000..469ccf332
--- /dev/null
+++ b/sdk/tests/input-multitouch.test.ts
@@ -0,0 +1,81 @@
+import { describe, it, expect, beforeEach } from 'vitest';
+import { InputState } from '../src/input';
+
+describe('InputState multi-touch', () => {
+ let input: InputState;
+
+ beforeEach(() => {
+ input = new InputState();
+ });
+
+ it('should start with no touches', () => {
+ expect(input.getTouchCount()).toBe(0);
+ expect(input.getTouches()).toEqual([]);
+ expect(input.getTouch(0)).toBeNull();
+ expect(input.isTouchActive(0)).toBe(false);
+ });
+
+ it('should track a single touch', () => {
+ input.touches.set(0, { id: 0, x: 100, y: 200 });
+ input.touchesStarted.set(0, { id: 0, x: 100, y: 200 });
+
+ expect(input.getTouchCount()).toBe(1);
+ expect(input.isTouchActive(0)).toBe(true);
+ expect(input.getTouch(0)).toEqual({ id: 0, x: 100, y: 200 });
+ });
+
+ it('should track multiple simultaneous touches', () => {
+ input.touches.set(0, { id: 0, x: 10, y: 20 });
+ input.touches.set(1, { id: 1, x: 300, y: 400 });
+ input.touches.set(2, { id: 2, x: 500, y: 600 });
+
+ expect(input.getTouchCount()).toBe(3);
+ expect(input.getTouch(1)).toEqual({ id: 1, x: 300, y: 400 });
+ expect(input.isTouchActive(2)).toBe(true);
+ expect(input.isTouchActive(99)).toBe(false);
+ });
+
+ it('should update touch position', () => {
+ input.touches.set(0, { id: 0, x: 10, y: 20 });
+ const touch = input.touches.get(0)!;
+ touch.x = 50;
+ touch.y = 60;
+
+ expect(input.getTouch(0)).toEqual({ id: 0, x: 50, y: 60 });
+ });
+
+ it('should remove touch on end', () => {
+ input.touches.set(0, { id: 0, x: 10, y: 20 });
+ input.touches.set(1, { id: 1, x: 30, y: 40 });
+
+ input.touches.delete(0);
+ input.touchesEnded.add(0);
+
+ expect(input.getTouchCount()).toBe(1);
+ expect(input.isTouchActive(0)).toBe(false);
+ expect(input.isTouchActive(1)).toBe(true);
+ });
+
+ it('should clear per-frame touch state', () => {
+ input.touchesStarted.set(0, { id: 0, x: 10, y: 20 });
+ input.touchesEnded.add(1);
+ input.touches.set(0, { id: 0, x: 10, y: 20 });
+
+ input.clearFrameState();
+
+ expect(input.touchesStarted.size).toBe(0);
+ expect(input.touchesEnded.size).toBe(0);
+ expect(input.getTouchCount()).toBe(1);
+ });
+
+ it('getTouches should return array copy', () => {
+ input.touches.set(0, { id: 0, x: 1, y: 2 });
+ input.touches.set(1, { id: 1, x: 3, y: 4 });
+
+ const touches = input.getTouches();
+ expect(touches).toHaveLength(2);
+
+ input.touches.delete(0);
+ expect(touches).toHaveLength(2);
+ });
+});
diff --git a/sdk/tests/scene-loading-progress.test.ts b/sdk/tests/scene-loading-progress.test.ts
new file mode 100644
index 000000000..d7d051841
--- /dev/null
+++ b/sdk/tests/scene-loading-progress.test.ts
@@ -0,0 +1,111 @@
+import { describe, it, expect, vi } from 'vitest';
+import { Assets } from '../src/asset/Assets';
+import { Catalog } from '../src/asset/Catalog';
+import type { Backend } from '../src/asset/Backend';
+import type { SceneData } from '../src/scene';
+
+vi.mock('../src/resourceManager', () => ({
+ requireResourceManager: () => ({
+ createTexture: vi.fn(() => 1),
+ registerExternalTexture: vi.fn(() => 1),
+ releaseTexture: vi.fn(),
+ releaseBitmapFont: vi.fn(),
+ getTextureGLId: vi.fn(() => 1),
+ registerTextureWithPath: vi.fn(),
+ getTextureDimensions: vi.fn(() => ({ width: 64, height: 64 })),
+ setTextureMetadata: vi.fn(),
+ }),
+ evictTextureDimensions: vi.fn(),
+}));
+
+vi.mock('../src/platform', () => ({
+ platformCreateCanvas: () => ({
+ getContext: () => ({
+ drawImage: vi.fn(),
+ getImageData: () => ({ data: new Uint8Array(64 * 64 * 4) }),
+ }),
+ width: 64,
+ height: 64,
+ }),
+ platformCreateImage: () => {
+ const img = { onload: null as any, onerror: null as any, src: '', width: 64, height: 64, crossOrigin: '' };
+ setTimeout(() => img.onload?.(), 0);
+ return img;
+ },
+ platformNow: () => 0,
+ platformDevicePixelRatio: () => 1,
+}));
+
+function createMockBackend(): Backend {
+ return {
+ fetch: vi.fn(async () => new ArrayBuffer(8)),
+ fetchText: vi.fn(async () => '{}'),
+ resolvePath: vi.fn((p: string) => p),
+ };
+}
+
+function createSceneWithTextures(count: number): SceneData {
+ const entities = [];
+ for (let i = 0; i < count; i++) {
+ entities.push({
+ id: i,
+ name: `entity_${i}`,
+ parent: null,
+ children: [],
+ components: [{
+ type: 'Sprite',
+ data: { texture: `tex_${i}.png` },
+ }],
+ });
+ }
+ return { name: 'test', entities };
+}
+
+describe('Scene loading progress', () => {
+ it('should call onProgress with loaded/total counts', async () => {
+ const backend = createMockBackend();
+ const catalog = new Catalog({ assets: {}, atlasPages: [], atlasFrames: {} });
+ const assets = new Assets(backend, catalog, null as any);
+
+ const scene = createSceneWithTextures(3);
+ const progressCalls: [number, number][] = [];
+
+ await assets.preloadSceneAssets(scene, (loaded, total) => {
+ progressCalls.push([loaded, total]);
+ });
+
+ expect(progressCalls.length).toBeGreaterThan(0);
+
+ const [firstLoaded, firstTotal] = progressCalls[0];
+ expect(firstLoaded).toBe(0);
+ expect(firstTotal).toBe(3);
+
+ const lastCall = progressCalls[progressCalls.length - 1];
+ expect(lastCall[0]).toBe(lastCall[1]);
+ });
+
+ it('should not fail when onProgress is undefined', async () => {
+ const backend = createMockBackend();
+ const catalog = new Catalog({ assets: {}, atlasPages: [], atlasFrames: {} });
+ const assets = new Assets(backend, catalog, null as any);
+
+ const scene = createSceneWithTextures(1);
+ const result = await assets.preloadSceneAssets(scene);
+ expect(result.textureHandles).toBeDefined();
+ });
+
+ it('should report 0/0 for empty scene', async () => {
+ const backend = createMockBackend();
+ const catalog = new Catalog({ assets: {}, atlasPages: [], atlasFrames: {} });
+ const assets = new Assets(backend, catalog, null as any);
+
+ const scene: SceneData = { name: 'empty', entities: [] };
+ const progressCalls: [number, number][] = [];
+
+ await assets.preloadSceneAssets(scene, (loaded, total) => {
+ progressCalls.push([loaded, total]);
+ });
+
+ expect(progressCalls).toContainEqual([0, 0]);
+ });
+});