-'use strict';
-
-const Code = require('code'); // assertion library
-const Lab = require('lab');
-const Serpentity = require('..');
+import { describe, it, beforeEach, mock } from 'node:test';
+import assert from 'node:assert';
+import Serpentity, { Component, Entity, Node, System } from '../lib/serpentity.js';
const internals = {
- system: class TestSystem extends Serpentity.System {
+ context: {},
+ system: class TestSystem extends System {
added(engine) {
this.testNodes = engine.getNodes(internals.node);
this.addedCalled = true;
this.addedEngine = engine;
+ this.emittedEvents = [];
+ this.beforeCall = null;
+ this.afterCall = null;
+
+ this.changeObserver = (event) => {
+ this.emittedEvents.push(event)
+ };
+
+ this.nodeAddedObserver = ({ node }) => {
+ node.test.addEventListener('change', this.changeObserver);
+ };
+
+ this.nodeRemovedObserver = ({ node }) => {
+ node.test.removeEventListener('change', this.changeObserver);
+ };
+
+ for (const node of this.testNodes) {
+ node.test.addEventListener('change', this.changeObserver);
+ }
+
+ this.testNodes.addEventListener('nodeAdded', this.nodeAddedObserver);
+ this.testNodes.addEventListener('nodeRemoved', this.nodeRemovedObserver);
+
+ super.added(); // not needed, but takes care of coverage :P
}
removed(engine) {
+ this.testNodes.removeEventListener('nodeAdded', this.nodeAddedObserver);
+ this.nodeAddedObserver = null;
+
+ this.testNodes.removeEventListener('nodeRemoved', this.nodeRemovedObserver);
+ this.nodeRemovedObserver = null;
+
+ for (const node of this.testNodes) {
+ node.test.removeEventListener('change', this.changeObserver);
+ }
+ this.changeObserver = null;
+
this.testNodes = null;
this.removedCalled = true;
this.removedEngine = engine;
+ this.emittedEvents = null;
+ this.beforeCall = null;
+ this.afterCall = null;
+ super.removed(); // not needed, but takes care of coverage :P
}
update(dt) {
this.updateDt = dt;
for (const node of this.testNodes) {
+ this.beforeCall = this.beforeCall === null ? node.test.called : this.beforeCall;
node.test.called = true;
+ this.afterCall = this.afterCall === null ? node.test.called : this.afterCall;
node.entity.called = true;
}
while (Date.now() === this.updateCalled) { /* pass some time */ }
+ super.update(); // not needed, but takes care of coverage :P
}
},
- component: class TestComponent extends Serpentity.Component {
+ component: class TestComponent extends Component {
constructor(config) {
super(config);
this.called = false;
}
},
- node: class TestNode extends Serpentity.Node {},
+ node: class TestNode extends Node {},
delta: 10
};
test: internals.component
};
-const lab = exports.lab = Lab.script();
-
-lab.experiment('loading', () => {
+describe('Loading', () => {
- lab.test('Serpentity should be exported', (done) => {
+ it('Should export the main class', () => {
- Code.expect(typeof Serpentity).to.not.be.undefined();
- done();
+ assert(typeof Serpentity === 'function');
});
- lab.test('Serpentity should include the Entity class', (done) => {
+ it('Should export the Entity class', () => {
- Code.expect(typeof Serpentity.Entity).to.not.be.undefined();
- done();
+ assert(typeof Entity === 'function');
});
- lab.test('Serpentity should include the Component class', (done) => {
+ it('Should export the Component class', () => {
- Code.expect(typeof Serpentity.Component).to.not.be.undefined();
- done();
+ assert(typeof Component === 'function');
});
- lab.test('Serpentity should include the System class', (done) => {
+ it('Should export the System class', () => {
- Code.expect(typeof Serpentity.System).to.not.be.undefined();
- done();
+ assert(typeof System === 'function');
});
- lab.test('Serpentity should include the Node class', (done) => {
+ it('Should export the Node class', () => {
- Code.expect(typeof Serpentity.Node).to.not.be.undefined();
- done();
- });
-
- lab.test('Serpentity should include the NodeCollection class', (done) => {
-
- Code.expect(typeof Serpentity.NodeCollection).to.not.be.undefined();
- done();
+ assert(typeof Node === 'function');
});
});
-lab.experiment('Engine Tests', () => {
+describe('Engine', () => {
- lab.beforeEach((done) => {
+ beforeEach(() => {
- this.engine = new Serpentity();
+ internals.context.engine = new Serpentity();
- this.regularSystem = new internals.system();
- this.highPrioritySystem = new internals.system();
- this.lowPrioritySystem = new internals.system();
+ internals.context.regularSystem = new internals.system();
+ internals.context.highPrioritySystem = new internals.system();
+ internals.context.lowPrioritySystem = new internals.system();
- this.firstEntity = new Serpentity.Entity();
- this.firstEntity.addComponent(new internals.component());
- this.secondEntity = new Serpentity.Entity();
- this.secondEntity.addComponent(new internals.component());
- this.emptyEntity = new Serpentity.Entity();
+ internals.context.firstEntity = new Entity();
+ internals.context.firstEntity.addComponent(new internals.component());
+ internals.context.secondEntity = new Entity();
+ internals.context.secondEntity.addComponent(new internals.component());
+ internals.context.emptyEntity = new Entity();
// Add entity before the systems
- this.engine.addEntity(this.firstEntity);
+ internals.context.engine.addEntity(internals.context.firstEntity);
- this.engine.addSystem(this.regularSystem, 100);
- this.engine.addSystem(this.highPrioritySystem, 0);
- this.engine.addSystem(this.lowPrioritySystem, 1000);
+ internals.context.engine.addSystem(internals.context.regularSystem, 100);
+ internals.context.engine.addSystem(internals.context.highPrioritySystem, 0);
+ internals.context.engine.addSystem(internals.context.lowPrioritySystem, 1000);
// Add entity after the systems
- this.engine.addEntity(this.secondEntity);
- this.engine.addEntity(this.emptyEntity);
-
- done();
+ internals.context.engine.addEntity(internals.context.secondEntity);
+ internals.context.engine.addEntity(internals.context.emptyEntity);
});
- lab.test('Engine should call added callback on added systems', (done) => {
+ it('should call added callback on added systems', () => {
// Ensure the added callback is being called
- Code.expect(this.regularSystem.addedCalled).to.be.true();
- Code.expect(this.highPrioritySystem.addedCalled).to.be.true();
- Code.expect(this.lowPrioritySystem.addedCalled).to.be.true();
-
- done();
+ assert(internals.context.regularSystem.addedCalled);
+ assert(internals.context.highPrioritySystem.addedCalled);
+ assert(internals.context.lowPrioritySystem.addedCalled);
});
- lab.test('Engine should send the engine instance in added callback', (done) => {
+ it('should send the engine instance in added callback', () => {
// Ensure the added callback is sending the engine
- Code.expect(this.regularSystem.addedEngine instanceof Serpentity).to.be.true();
- Code.expect(this.highPrioritySystem.addedEngine instanceof Serpentity).to.be.true();
- Code.expect(this.lowPrioritySystem.addedEngine instanceof Serpentity).to.be.true();
-
- done();
+ assert(internals.context.regularSystem.addedEngine instanceof Serpentity);
+ assert(internals.context.highPrioritySystem.addedEngine instanceof Serpentity);
+ assert(internals.context.lowPrioritySystem.addedEngine instanceof Serpentity);
});
- lab.test('Engine should not add duplicate systems', (done) => {
+ it('should not add duplicate systems', () => {
- const originalSystemsLength = this.engine.systems.length;
- const added = this.engine.addSystem(this.regularSystem, 0);
- const newSystemsLength = this.engine.systems.length;
+ const originalSystemsLength = internals.context.engine.systems.length;
+ const added = internals.context.engine.addSystem(internals.context.regularSystem, 0);
+ const newSystemsLength = internals.context.engine.systems.length;
// Ensure we don't add the same system twice
- Code.expect(added).to.be.false();
- Code.expect(originalSystemsLength).to.be.equal(newSystemsLength);
-
- done();
+ assert(!added);
+ assert.deepEqual(newSystemsLength, originalSystemsLength);
});
- lab.test('Engine should call update callback on added systems', (done) => {
+ it('should call update callback on added systems', () => {
- this.engine.update(internals.delta);
+ internals.context.engine.update(internals.delta);
// Ensure update function called
- Code.expect(!!this.regularSystem.updateCalled).to.be.true();
- Code.expect(!!this.highPrioritySystem.updateCalled).to.be.true();
- Code.expect(!!this.lowPrioritySystem.updateCalled).to.be.true();
+ assert(!!internals.context.regularSystem.updateCalled);
+ assert(!!internals.context.highPrioritySystem.updateCalled);
+ assert(!!internals.context.lowPrioritySystem.updateCalled);
+ });
+
+ it('should keep proxied object behavior as expected', () => {
+
+ internals.context.engine.update(internals.delta);
- done();
+ assert(internals.context.highPrioritySystem.beforeCall === false);
+ assert(internals.context.highPrioritySystem.afterCall === true);
});
- lab.test('Engine should call update callback in the order of priorities', (done) => {
+ it('should emit an event for every changed property', () => {
- this.engine.update(internals.delta);
+ internals.context.engine.update(internals.delta);
- // Ensure order of priorities
- Code.expect(this.regularSystem.updateCalled).to.be.lessThan(this.lowPrioritySystem.updateCalled);
- Code.expect(this.regularSystem.updateCalled).to.be.greaterThan(this.highPrioritySystem.updateCalled);
+ assert(internals.context.regularSystem.emittedEvents[0].property === 'called');
+ assert(internals.context.regularSystem.emittedEvents[0].from === false);
+ assert(internals.context.regularSystem.emittedEvents[0].to === true);
+ // 3 systems x 2 entities.
+ assert(internals.context.regularSystem.emittedEvents.length === 6);
+ });
+
+ it('should call update callback in the order of priorities', () => {
- done();
+ internals.context.engine.update(internals.delta);
+
+ // Ensure order of priorities
+ assert(internals.context.regularSystem.updateCalled < internals.context.lowPrioritySystem.updateCalled);
+ assert(internals.context.regularSystem.updateCalled > internals.context.highPrioritySystem.updateCalled);
});
- lab.test('Engine should send the delta in the update callback', (done) => {
+ it('should send the delta in the update callback', () => {
- this.engine.update(internals.delta);
+ internals.context.engine.update(internals.delta);
// Ensure delta is being sent
- Code.expect(this.regularSystem.updateDt).to.be.equal(internals.delta);
- Code.expect(this.highPrioritySystem.updateDt).to.be.equal(internals.delta);
- Code.expect(this.lowPrioritySystem.updateDt).to.be.equal(internals.delta);
-
- done();
+ assert.deepEqual(internals.context.regularSystem.updateDt, internals.delta);
+ assert.deepEqual(internals.context.highPrioritySystem.updateDt, internals.delta);
+ assert.deepEqual(internals.context.lowPrioritySystem.updateDt, internals.delta);
});
- lab.test('System remove callback', (done) => {
+ it('should no longer call removed systems', () => {
- const originalSystemLength = this.engine.systems.length;
- const originalRemoved = this.engine.removeSystem(this.lowPrioritySystem);
- const intermediateSystemLength = this.engine.systems.length;
- const finalRemoved = this.engine.removeSystem(this.lowPrioritySystem);
- const finalSystemLength = this.engine.systems.length;
- this.engine.update(internals.delta);
+ const originalSystemLength = internals.context.engine.systems.length;
+ const originalRemoved = internals.context.engine.removeSystem(internals.context.lowPrioritySystem);
+ const intermediateSystemLength = internals.context.engine.systems.length;
+ const finalRemoved = internals.context.engine.removeSystem(internals.context.lowPrioritySystem);
+ const finalSystemLength = internals.context.engine.systems.length;
+ internals.context.engine.update(internals.delta);
- // Check for return value
- Code.expect(originalRemoved).to.be.true();
- Code.expect(finalRemoved).to.be.false();
+ // Check for return value
+ assert(originalRemoved);
+ assert(!finalRemoved);
// Confirm that only removed if found by checking length of systems
// array
- Code.expect(originalSystemLength).to.be.above(intermediateSystemLength);
- Code.expect(finalSystemLength).to.be.equal(intermediateSystemLength);
+ assert(originalSystemLength > intermediateSystemLength);
+ assert.deepEqual(finalSystemLength, intermediateSystemLength);
// Ensure callback is sent
- Code.expect(!!this.regularSystem.removedCalled).to.be.false();
- Code.expect(!!this.highPrioritySystem.removedCalled).to.be.false();
- Code.expect(!!this.lowPrioritySystem.removedCalled).to.be.true();
+ assert(!internals.context.regularSystem.removedCalled);
+ assert(!internals.context.highPrioritySystem.removedCalled);
+ assert(!!internals.context.lowPrioritySystem.removedCalled);
// Ensure update is no longer sent
- Code.expect(!!this.regularSystem.updateCalled).to.be.true();
- Code.expect(!!this.highPrioritySystem.updateCalled).to.be.true();
- Code.expect(!!this.lowPrioritySystem.updateCalled).to.be.false();
-
- done();
+ assert(!!internals.context.regularSystem.updateCalled);
+ assert(!!internals.context.highPrioritySystem.updateCalled);
+ assert(!internals.context.lowPrioritySystem.updateCalled);
});
- lab.test('Entity node selection', (done) => {
+ it('should only call nodes in selected node collections', () => {
- this.engine.update(internals.delta);
+ internals.context.engine.update(internals.delta);
// Ensure component is called for each entity
- Code.expect(!!this.firstEntity._components[0].called).to.be.true();
- Code.expect(!!this.secondEntity._components[0].called).to.be.true();
+ assert(!!internals.context.firstEntity._components[0].called);
+ assert(!!internals.context.secondEntity._components[0].called);
// Ensure entity not in node collection not called
- Code.expect(!!this.firstEntity.called).to.be.true();
- Code.expect(!!this.secondEntity.called).to.be.true();
- Code.expect(!!this.emptyEntity.called).to.be.false();
-
- done();
+ assert(!!internals.context.firstEntity.called);
+ assert(!!internals.context.secondEntity.called);
+ assert(!internals.context.emptyEntity.called);
});
- lab.test('Entity node removal', (done) => {
+ it('should stop showing removed entities', () => {
- this.engine.removeEntity(this.secondEntity);
- this.engine.update(internals.delta);
+ internals.context.engine.removeEntity(internals.context.secondEntity);
+ internals.context.engine.update(internals.delta);
- Code.expect(!!this.firstEntity._components[0].called).to.be.true();
- Code.expect(!!this.secondEntity._components[0].called).to.be.false();
+ assert(!!internals.context.firstEntity._components[0].called);
+ assert(!internals.context.secondEntity._components[0].called);
- Code.expect(!!this.firstEntity.called).to.be.true();
- Code.expect(!!this.secondEntity.called).to.be.false();
- Code.expect(!!this.emptyEntity.called).to.be.false();
-
- done();
+ assert(!!internals.context.firstEntity.called);
+ assert(!internals.context.secondEntity.called);
+ assert(!internals.context.emptyEntity.called);
});
- lab.test('Entity should not add duplicate components', (done) => {
- const originalComponentsLength = this.secondEntity._components.length;
- const result = this.secondEntity.addComponent(new internals.component());
- const newComponentsLength = this.secondEntity._components.length;
+ it('should not add duplicate components to entities', () => {
- Code.expect(result).to.be.false();
- Code.expect(originalComponentsLength).to.be.equal(newComponentsLength);
+ const originalComponentsLength = internals.context.secondEntity._components.length;
+ const result = internals.context.secondEntity.addComponent(new internals.component());
+ const newComponentsLength = internals.context.secondEntity._components.length;
- done();
+ assert(!result);
+ assert.deepEqual(newComponentsLength, originalComponentsLength);
});
- lab.test('Entity should allow access to components by class', (done) => {
- const firstComponent = this.firstEntity.getComponent(internals.component);
- const emptyComponent = this.emptyEntity.getComponent(internals.component);
+ it('should allow access to components by class', () => {
- Code.expect(firstComponent instanceof internals.component).to.be.true();
- Code.expect(emptyComponent).to.be.equal(undefined);
+ const firstComponent = internals.context.firstEntity.getComponent(internals.component);
+ const emptyComponent = internals.context.emptyEntity.getComponent(internals.component);
- done();
+ assert(firstComponent instanceof internals.component);
+ assert.deepEqual(emptyComponent, undefined);
});
- lab.test('Engine should not add duplicate entities', (done) => {
- const originalEntitiesLength = this.engine.entities.length;
- const added = this.engine.addEntity(this.firstEntity);
- const finalEntitiesLength = this.engine.entities.length;
+ it('should not add duplicate entities', () => {
+
+ const originalEntitiesLength = internals.context.engine.entities.length;
+ const added = internals.context.engine.addEntity(internals.context.firstEntity);
+ const finalEntitiesLength = internals.context.engine.entities.length;
- Code.expect(added).to.be.false();
+ assert(!added);
- Code.expect(originalEntitiesLength).to.be.equal(finalEntitiesLength);
- done();
+ assert.deepEqual(finalEntitiesLength, originalEntitiesLength);
});
- lab.test('Engine should remove entities', (done) => {
+ it('should remove entities', () => {
- const originalEntityLength = this.engine.entities.length;
- const originalRemoved = this.engine.removeEntity(this.firstEntity);
- const intermediateEntityLength = this.engine.entities.length;
- const finalRemoved = this.engine.removeEntity(this.firstEntity);
- const finalEntityLength = this.engine.entities.length;
- this.engine.update(internals.delta);
+ const originalEntityLength = internals.context.engine.entities.length;
+ const originalRemoved = internals.context.engine.removeEntity(internals.context.firstEntity);
+ const intermediateEntityLength = internals.context.engine.entities.length;
+ const finalRemoved = internals.context.engine.removeEntity(internals.context.firstEntity);
+ const finalEntityLength = internals.context.engine.entities.length;
+ internals.context.engine.update(internals.delta);
- // Check for return value
- Code.expect(originalRemoved).to.be.true();
- Code.expect(finalRemoved).to.be.false();
+ // Check for return value
+ assert(originalRemoved);
+ assert(!finalRemoved);
// Confirm that only removed if found by checking length of systems
// array
- Code.expect(originalEntityLength).to.be.above(intermediateEntityLength);
- Code.expect(finalEntityLength).to.be.equal(intermediateEntityLength);
+ assert(originalEntityLength > intermediateEntityLength);
+ assert.deepEqual(finalEntityLength, intermediateEntityLength);
// Ensure callback is sent
- Code.expect(!!this.firstEntity.called).to.be.false();
- Code.expect(!!this.secondEntity.called).to.be.true();
+ assert(!internals.context.firstEntity.called);
+ assert(!!internals.context.secondEntity.called);
+ });
+
+ it('should not add duplicate entities', () => {
+
+ const originalEntitiesLength = internals.context.engine.entities.length;
+ const added = internals.context.engine.addEntity(internals.context.firstEntity);
+ const finalEntitiesLength = internals.context.engine.entities.length;
+
+ assert(!added);
- done();
+ assert.deepEqual(finalEntitiesLength, originalEntitiesLength);
});
});