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: expand resource and query test coverage to 100%

esengine9a6be9fe7133
diff --git a/sdk/tests/query.test.ts b/sdk/tests/query.test.ts
index 8534d94e4..8b64a0e2c 100644
--- a/sdk/tests/query.test.ts
+++ b/sdk/tests/query.test.ts
@@ -1,6 +1,11 @@
import { describe, it, expect, beforeEach } from 'vitest';
import { World } from '../src/world';
-import { Query, QueryInstance, Mut } from '../src/query';
+import {
+ Query, QueryInstance, Mut,
+ Added, Changed, Removed,
+ isMutWrapper, isAddedWrapper, isChangedWrapper,
+ RemovedQueryInstance,
+} from '../src/query';
import { defineComponent } from '../src/component';
@@ -253,4 +258,297 @@ describe('Query System', () => {
}).toThrow(/Cannot remove component during query iteration/);
});
});
+
+ // =========================================================================
+ // with / without filters
+ // =========================================================================
+
+ describe('with / without filters', () => {
+ it('should filter entities that also have a with component', () => {
+ const e1 = world.spawn();
+ world.insert(e1, Position, { x: 1, y: 1 });
+ world.insert(e1, Enemy, {});
+
+ const e2 = world.spawn();
+ world.insert(e2, Position, { x: 2, y: 2 });
+
+ const query = Query(Position).with(Enemy);
+ const results: number[] = [];
+ new QueryInstance(world, query).forEach((entity) => {
+ results.push(entity);
+ });
+ expect(results).toEqual([e1]);
+ });
+
+ it('should exclude entities with a without component', () => {
+ const e1 = world.spawn();
+ world.insert(e1, Position, { x: 1, y: 1 });
+ world.insert(e1, Enemy, {});
+
+ const e2 = world.spawn();
+ world.insert(e2, Position, { x: 2, y: 2 });
+
+ const query = Query(Position).without(Enemy);
+ const results: number[] = [];
+ new QueryInstance(world, query).forEach((entity) => {
+ results.push(entity);
+ });
+ expect(results).toEqual([e2]);
+ });
+
+ it('should chain with and without', () => {
+ const e1 = world.spawn();
+ world.insert(e1, Position, { x: 1, y: 1 });
+ world.insert(e1, Velocity, { dx: 1, dy: 1 });
+ world.insert(e1, Enemy, {});
+
+ const e2 = world.spawn();
+ world.insert(e2, Position, { x: 2, y: 2 });
+ world.insert(e2, Velocity, { dx: 2, dy: 2 });
+
+ const e3 = world.spawn();
+ world.insert(e3, Position, { x: 3, y: 3 });
+ world.insert(e3, Enemy, {});
+
+ const query = Query(Position).with(Velocity).without(Enemy);
+ const results: number[] = [];
+ new QueryInstance(world, query).forEach((entity) => {
+ results.push(entity);
+ });
+ expect(results).toEqual([e2]);
+ });
+ });
+
+ // =========================================================================
+ // Added / Changed wrappers and type guards
+ // =========================================================================
+
+ describe('Added / Changed wrappers', () => {
+ it('Added should create an added filter wrapper', () => {
+ const wrapper = Added(Position);
+ expect(wrapper._filterType).toBe('added');
+ expect(wrapper._component).toBe(Position);
+ });
+
+ it('Changed should create a changed filter wrapper', () => {
+ const wrapper = Changed(Position);
+ expect(wrapper._filterType).toBe('changed');
+ expect(wrapper._component).toBe(Position);
+ });
+
+ it('isAddedWrapper should detect Added wrappers', () => {
+ expect(isAddedWrapper(Added(Position))).toBe(true);
+ expect(isAddedWrapper(Changed(Position))).toBe(false);
+ expect(isAddedWrapper(Mut(Position))).toBe(false);
+ expect(isAddedWrapper(null)).toBe(false);
+ });
+
+ it('isChangedWrapper should detect Changed wrappers', () => {
+ expect(isChangedWrapper(Changed(Position))).toBe(true);
+ expect(isChangedWrapper(Added(Position))).toBe(false);
+ expect(isChangedWrapper(Mut(Position))).toBe(false);
+ expect(isChangedWrapper(null)).toBe(false);
+ });
+
+ it('isMutWrapper should detect Mut wrappers', () => {
+ expect(isMutWrapper(Mut(Position))).toBe(true);
+ expect(isMutWrapper(Added(Position))).toBe(false);
+ expect(isMutWrapper(Position)).toBe(false);
+ expect(isMutWrapper(null)).toBe(false);
+ });
+ });
+
+ describe('Added query filtering', () => {
+ it('should only return entities added since last run tick', () => {
+ const e1 = world.spawn();
+ world.insert(e1, Position, { x: 1, y: 1 });
+
+ const query = Query(Added(Position));
+ const instance = new QueryInstance(world, query, -1);
+ const results: number[] = [];
+ instance.forEach((entity) => {
+ results.push(entity);
+ });
+ expect(results).toContain(e1);
+ });
+ });
+
+ describe('Changed query filtering', () => {
+ it('should only return entities changed since last run tick', () => {
+ const e1 = world.spawn();
+ world.insert(e1, Position, { x: 1, y: 1 });
+
+ const query = Query(Changed(Position));
+ const instance = new QueryInstance(world, query, -1);
+ const results: number[] = [];
+ instance.forEach((entity) => {
+ results.push(entity);
+ });
+ expect(results).toContain(e1);
+ });
+
+ it('should skip entities not changed since a future tick', () => {
+ const e1 = world.spawn();
+ world.insert(e1, Position, { x: 1, y: 1 });
+
+ const query = Query(Changed(Position));
+ const instance = new QueryInstance(world, query, Infinity);
+ const results: number[] = [];
+ instance.forEach((entity) => {
+ results.push(entity);
+ });
+ expect(results).toHaveLength(0);
+ });
+ });
+
+ // =========================================================================
+ // single / isEmpty / count / toArray
+ // =========================================================================
+
+ describe('single()', () => {
+ it('should return first result', () => {
+ const e1 = world.spawn();
+ world.insert(e1, Position, { x: 10, y: 20 });
+
+ const query = Query(Position);
+ const instance = new QueryInstance(world, query);
+ const result = instance.single();
+ expect(result).not.toBeNull();
+ expect(result![0]).toBe(e1);
+ });
+
+ it('should return null when no results', () => {
+ const query = Query(Position);
+ const instance = new QueryInstance(world, query);
+ expect(instance.single()).toBeNull();
+ });
+ });
+
+ describe('isEmpty()', () => {
+ it('should return true when no matching entities', () => {
+ const query = Query(Position);
+ const instance = new QueryInstance(world, query);
+ expect(instance.isEmpty()).toBe(true);
+ });
+
+ it('should return false when matching entities exist', () => {
+ const e1 = world.spawn();
+ world.insert(e1, Position, { x: 0, y: 0 });
+
+ const query = Query(Position);
+ const instance = new QueryInstance(world, query);
+ expect(instance.isEmpty()).toBe(false);
+ });
+ });
+
+ describe('count()', () => {
+ it('should return 0 for empty query', () => {
+ const query = Query(Position);
+ expect(new QueryInstance(world, query).count()).toBe(0);
+ });
+
+ it('should return correct count', () => {
+ for (let i = 0; i < 3; i++) {
+ const e = world.spawn();
+ world.insert(e, Position, { x: i, y: i });
+ }
+
+ const query = Query(Position);
+ expect(new QueryInstance(world, query).count()).toBe(3);
+ });
+ });
+
+ describe('toArray()', () => {
+ it('should return empty array for no results', () => {
+ const query = Query(Position);
+ expect(new QueryInstance(world, query).toArray()).toEqual([]);
+ });
+
+ it('should return all results as array', () => {
+ const e1 = world.spawn();
+ world.insert(e1, Position, { x: 1, y: 1 });
+ const e2 = world.spawn();
+ world.insert(e2, Position, { x: 2, y: 2 });
+
+ const query = Query(Position);
+ const arr = new QueryInstance(world, query).toArray();
+ expect(arr).toHaveLength(2);
+ expect(arr[0][0]).toBe(e1);
+ expect(arr[1][0]).toBe(e2);
+ });
+ });
+
+ // =========================================================================
+ // Mut write-back with multiple entities
+ // =========================================================================
+
+ describe('Mut write-back across multiple entities', () => {
+ it('should write back mutations for all entities', () => {
+ const e1 = world.spawn();
+ world.insert(e1, Position, { x: 0, y: 0 });
+
+ const e2 = world.spawn();
+ world.insert(e2, Position, { x: 0, y: 0 });
+
+ const e3 = world.spawn();
+ world.insert(e3, Position, { x: 0, y: 0 });
+
+ const query = Query(Mut(Position));
+ new QueryInstance(world, query).forEach((entity, pos) => {
+ pos.x = entity * 10;
+ pos.y = entity * 20;
+ });
+
+ expect(world.get(e1, Position)).toEqual({ x: e1 * 10, y: e1 * 20 });
+ expect(world.get(e2, Position)).toEqual({ x: e2 * 10, y: e2 * 20 });
+ expect(world.get(e3, Position)).toEqual({ x: e3 * 10, y: e3 * 20 });
+ });
+ });
+
+ // =========================================================================
+ // Removed query
+ // =========================================================================
+
+ describe('Removed query', () => {
+ it('should create a removed descriptor', () => {
+ const desc = Removed(Position);
+ expect(desc._type).toBe('removed');
+ expect(desc._component).toBe(Position);
+ });
+
+ it('should iterate over removed entities', () => {
+ const e1 = world.spawn();
+ world.insert(e1, Position, { x: 1, y: 1 });
+ world.remove(e1, Position);
+
+ const instance = new RemovedQueryInstance(world, Position, -1);
+ const removed = instance.toArray();
+ expect(removed).toContain(e1);
+ });
+
+ it('should report isEmpty correctly', () => {
+ const instance = new RemovedQueryInstance(world, Position, Infinity);
+ expect(instance.isEmpty()).toBe(true);
+
+ const e1 = world.spawn();
+ world.insert(e1, Position, { x: 1, y: 1 });
+ world.remove(e1, Position);
+
+ const instance2 = new RemovedQueryInstance(world, Position, -1);
+ expect(instance2.isEmpty()).toBe(false);
+ });
+
+ it('should be iterable', () => {
+ const e1 = world.spawn();
+ world.insert(e1, Position, { x: 1, y: 1 });
+ world.remove(e1, Position);
+
+ const instance = new RemovedQueryInstance(world, Position, -1);
+ const results: number[] = [];
+ for (const entity of instance) {
+ results.push(entity);
+ }
+ expect(results).toContain(e1);
+ });
+ });
});
diff --git a/sdk/tests/resource.test.ts b/sdk/tests/resource.test.ts
index 3a4fce2b0..6c1bc008e 100644
--- a/sdk/tests/resource.test.ts
+++ b/sdk/tests/resource.test.ts
@@ -1,5 +1,5 @@
import { describe, it, expect, beforeEach } from 'vitest';
-import { defineResource, Res, ResMut } from '../src/resource';
+import { defineResource, Res, ResMut, ResourceStorage, ResMutInstance } from '../src/resource';
describe('Resource System', () => {
describe('defineResource', () => {
@@ -129,4 +129,134 @@ describe('Resource System', () => {
expect(Physics._default.substeps).toBe(4);
});
});
+
+ describe('defineResource auto-naming', () => {
+ it('should generate a name when none provided', () => {
+ const res = defineResource({ v: 0 });
+ expect(res._name).toMatch(/^Resource_\d+$/);
+ });
+ });
+});
+
+// =============================================================================
+// ResourceStorage
+// =============================================================================
+
+describe('ResourceStorage', () => {
+ let storage: ResourceStorage;
+ const Counter = defineResource({ count: 0 }, 'StorageCounter');
+ const Config = defineResource({ debug: false }, 'StorageConfig');
+
+ beforeEach(() => {
+ storage = new ResourceStorage();
+ });
+
+ describe('insert and get', () => {
+ it('should store and retrieve a resource', () => {
+ storage.insert(Counter, { count: 42 });
+ expect(storage.get(Counter)).toEqual({ count: 42 });
+ });
+
+ it('should return default when not inserted', () => {
+ expect(storage.get(Counter)).toEqual({ count: 0 });
+ });
+
+ it('should overwrite on re-insert', () => {
+ storage.insert(Counter, { count: 1 });
+ storage.insert(Counter, { count: 2 });
+ expect(storage.get(Counter)).toEqual({ count: 2 });
+ });
+ });
+
+ describe('set', () => {
+ it('should set a resource value', () => {
+ storage.set(Counter, { count: 99 });
+ expect(storage.get(Counter)).toEqual({ count: 99 });
+ });
+ });
+
+ describe('has', () => {
+ it('should return false for missing resource', () => {
+ expect(storage.has(Counter)).toBe(false);
+ });
+
+ it('should return true after insert', () => {
+ storage.insert(Counter, { count: 0 });
+ expect(storage.has(Counter)).toBe(true);
+ });
+ });
+
+ describe('remove', () => {
+ it('should remove an inserted resource', () => {
+ storage.insert(Counter, { count: 10 });
+ storage.remove(Counter);
+ expect(storage.has(Counter)).toBe(false);
+ });
+
+ it('should return default after removal', () => {
+ storage.insert(Counter, { count: 10 });
+ storage.remove(Counter);
+ expect(storage.get(Counter)).toEqual({ count: 0 });
+ });
+ });
+
+ describe('getResMut', () => {
+ it('should return a ResMutInstance', () => {
+ storage.insert(Counter, { count: 5 });
+ const mut = storage.getResMut(Counter);
+ expect(mut).toBeInstanceOf(ResMutInstance);
+ expect(mut.get()).toEqual({ count: 5 });
+ });
+
+ it('should set value back to storage', () => {
+ storage.insert(Counter, { count: 0 });
+ const mut = storage.getResMut(Counter);
+ mut.set({ count: 42 });
+ expect(storage.get(Counter)).toEqual({ count: 42 });
+ });
+
+ it('should modify value in-place', () => {
+ storage.insert(Counter, { count: 10 });
+ const mut = storage.getResMut(Counter);
+ mut.modify((v) => { v.count += 5; });
+ expect(storage.get(Counter)).toEqual({ count: 15 });
+ });
+
+ it('should reuse the same ResMutInstance on subsequent calls', () => {
+ storage.insert(Counter, { count: 0 });
+ const mut1 = storage.getResMut(Counter);
+ const mut2 = storage.getResMut(Counter);
+ expect(mut1).toBe(mut2);
+ });
+
+ it('should update cached instance value on subsequent call', () => {
+ storage.insert(Counter, { count: 1 });
+ const mut = storage.getResMut(Counter);
+ storage.set(Counter, { count: 99 });
+ const mut2 = storage.getResMut(Counter);
+ expect(mut2.get()).toEqual({ count: 99 });
+ });
+
+ it('should use default when resource not inserted', () => {
+ const mut = storage.getResMut(Counter);
+ expect(mut.get()).toEqual({ count: 0 });
+ });
+
+ it('should cleanup pool on remove', () => {
+ storage.insert(Counter, { count: 1 });
+ const mut1 = storage.getResMut(Counter);
+ storage.remove(Counter);
+ const mut2 = storage.getResMut(Counter);
+ expect(mut1).not.toBe(mut2);
+ });
+ });
+
+ describe('multiple resources', () => {
+ it('should store different resources independently', () => {
+ storage.insert(Counter, { count: 10 });
+ storage.insert(Config, { debug: true });
+ expect(storage.get(Counter)).toEqual({ count: 10 });
+ expect(storage.get(Config)).toEqual({ debug: true });
+ });
+ });
});