fix: tests

This commit is contained in:
cha0s 2022-03-21 22:07:54 -05:00
parent d31096853b
commit c4e9cc9cd2
7 changed files with 534 additions and 512 deletions

View File

@ -1,142 +1,134 @@
// import {
// buildCondition,
// buildExpression,
// buildInvoke,
// } from '@avocado/behavior';
// import {Flecks} from '@flecks/core';
// import {normalize} from '@flecks/socket';
// import {expect} from 'chai';
import {Flecks} from '@flecks/core/server';
import {normalize} from '@flecks/socket';
import {expect} from 'chai';
// let flecks;
// let Entity;
// beforeEach(async () => {
// flecks = Flecks.mock({
// '@avocado/behavior': require('@avocado/behavior'),
// '@avocado/entity': require('../src'),
// '@avocado/resource': require('@avocado/resource'),
// '@avocado/traits': require('@avocado/traits'),
// '@flecks/socket': require('@flecks/socket'),
// });
// await Promise.all(flecks.invokeFlat('@flecks/core/starting'));
// ({Entity} = flecks.get('$avocado/resource.resources'));
// });
// describe('Alive', () => {
// let entity;
// beforeEach(async () => {
// entity = await Entity.load({
// traits: {
// Alive: {},
// },
// });
// });
// it('exists', async () => {
// expect(entity.is('Alive')).to.be.true;
// });
// if ('http' !== process.env.FLECKS_CORE_BUILD_TARGET) {
// it('can die', async () => {
// let isDying = false;
// entity.once('startedDying', () => {
// isDying = true;
// });
// entity.life = 0;
// entity.tick(0);
// expect(isDying).to.be.true;
// });
// }
// it('clamps life', async () => {
// entity.life = 120;
// expect(entity.life).to.equal(100);
// entity.maxLife = 50;
// expect(entity.life).to.equal(50);
// });
// if ('http' !== process.env.FLECKS_CORE_BUILD_TARGET) {
// it('can have a custom death condition', async () => {
// const entity = await Entity.load({
// traits: {
// Alive: {
// params: {
// deathCondition: buildCondition('<=', [
// buildExpression(['entity', 'life']),
// 10,
// ]),
// },
// },
// },
// });
// let isDying = false;
// entity.on('startedDying', () => {
// isDying = true;
// });
// entity.tick(0);
// expect(isDying).to.be.false;
// entity.life = 10;
// entity.tick(0);
// expect(isDying).to.be.true;
// });
// }
// it('runs actions on death', async () => {
// let didActions;
// const entity = await Entity.load({
// traits: {
// Alive: {
// params: {
// deathActions: {
// type: 'expressions',
// expressions: [
// buildInvoke(['entity', 'ded']),
// ],
// },
// },
// },
// },
// });
// entity.ded = () => {
// didActions = true;
// };
// const handle = setInterval(() => {
// entity.tick();
// }, 16.66);
// await entity.die();
// clearInterval(handle);
// expect(didActions).to.be.true;
// });
// describe('Packets', () => {
// let entity2;
// beforeEach(async () => {
// entity2 = await Entity.load({
// traits: {
// Alive: {},
// },
// });
// });
// it('generates and accepts life packets', async () => {
// entity.life = 80;
// entity.maxLife = 90;
// const packets = entity.trait('Alive').packetsFor();
// expect(packets).to.have.lengthOf(1);
// expect(packets[0][0]).to.equal('TraitUpdateAlive');
// expect(packets[0][1]).to.deep.equal({life: 80, maxLife: 90});
// entity2.trait('Alive').acceptPacket(normalize(flecks, packets[0]));
// expect(entity2.life).to.equal(80);
// expect(entity2.maxLife).to.equal(90);
// });
// if ('http' !== process.env.FLECKS_CORE_BUILD_TARGET) {
// it('generates and accepts death packets', async () => {
// entity.life = 0;
// entity.tick();
// const packets = entity.trait('Alive').packetsFor();
// expect(packets).to.have.lengthOf(2);
// expect(packets[0][0]).to.equal('Died');
// expect(packets[1][0]).to.equal('TraitUpdateAlive');
// expect(packets[1][1]).to.deep.equal({life: 0, maxLife: 100});
// const promise = new Promise((resolve) => {
// entity2.once('startedDying', resolve);
// });
// entity2.trait('Alive').acceptPacket(normalize(flecks, packets[0]));
// entity2.trait('Alive').acceptPacket(normalize(flecks, packets[1]));
// expect(entity2.life).to.equal(0);
// return promise;
// });
// }
// });
// });
let flecks;
let Entity;
beforeEach(async () => {
flecks = Flecks.bootstrap({
config: {
'@avocado/behavior': {},
'@avocado/entity:./src': {},
'@avocado/graphics': {},
'@avocado/resource': {},
'@avocado/traits': {},
'@flecks/core': {},
'@flecks/react': {},
'@flecks/socket': {},
},
});
await Promise.all(flecks.invokeFlat('@flecks/core.starting'));
({Entity} = flecks.get('$avocado/resource.resources'));
});
describe('Alive', () => {
let entity;
beforeEach(async () => {
entity = await Entity.load({
traits: {
Alive: {},
},
});
});
it('exists', async () => {
expect(entity.is('Alive')).to.be.true;
});
if ('http' !== process.env.FLECKS_CORE_BUILD_TARGET) {
it('can die', async () => {
let isDying = false;
entity.once('startedDying', () => {
isDying = true;
});
entity.life = 0;
entity.tick(0);
expect(isDying).to.be.true;
});
}
it('clamps life', async () => {
entity.life = 120;
expect(entity.life).to.equal(100);
entity.maxLife = 50;
expect(entity.life).to.equal(50);
});
if ('http' !== process.env.FLECKS_CORE_BUILD_TARGET) {
it('can have a custom death condition', async () => {
const entity = await Entity.load({
traits: {
Alive: {
params: {
deathCheck: 'return entity.life <= 10',
},
},
},
});
let isDying = false;
entity.on('startedDying', () => {
isDying = true;
});
entity.tick(0);
expect(isDying).to.be.false;
entity.life = 10;
entity.tick(0);
expect(isDying).to.be.true;
});
}
it('runs actions on death', async () => {
let didActions;
const entity = await Entity.load({
traits: {
Alive: {
params: {
deathScript: 'entity.ded();',
},
},
},
});
entity.ded = () => {
didActions = true;
};
const handle = setInterval(() => {
entity.tick();
}, 16.66);
await entity.die();
clearInterval(handle);
expect(didActions).to.be.true;
});
describe('Packets', () => {
let entity2;
beforeEach(async () => {
entity2 = await Entity.load({
traits: {
Alive: {},
},
});
});
it('generates and accepts life packets', async () => {
entity.life = 80;
entity.maxLife = 90;
const packets = entity.trait('Alive').packetsFor();
expect(packets).to.have.lengthOf(1);
expect(packets[0][0]).to.equal('TraitUpdateAlive');
expect(packets[0][1]).to.deep.equal({life: 80, maxLife: 90});
entity2.trait('Alive').acceptPacket(normalize(flecks, packets[0]));
expect(entity2.life).to.equal(80);
expect(entity2.maxLife).to.equal(90);
});
if ('http' !== process.env.FLECKS_CORE_BUILD_TARGET) {
it('generates and accepts death packets', async () => {
entity.life = 0;
entity.tick();
const packets = entity.trait('Alive').packetsFor();
expect(packets).to.have.lengthOf(2);
expect(packets[0][0]).to.equal('Died');
expect(packets[1][0]).to.equal('TraitUpdateAlive');
expect(packets[1][1]).to.deep.equal({life: 0, maxLife: 100});
const promise = new Promise((resolve) => {
entity2.once('startedDying', resolve);
});
entity2.trait('Alive').acceptPacket(normalize(flecks, packets[0]));
entity2.trait('Alive').acceptPacket(normalize(flecks, packets[1]));
expect(entity2.life).to.equal(0);
return promise;
});
}
});
});

View File

@ -1,58 +1,63 @@
// import {Flecks} from '@flecks/core';
// import {normalize} from '@flecks/socket';
// import {expect} from 'chai';
import {Flecks} from '@flecks/core/server';
import {normalize} from '@flecks/socket';
import {expect} from 'chai';
// let flecks;
// let Entity;
// beforeEach(async () => {
// flecks = Flecks.mock({
// '@avocado/entity': require('../src'),
// '@avocado/resource': require('@avocado/resource'),
// '@avocado/traits': require('@avocado/traits'),
// '@flecks/socket': require('@flecks/socket'),
// });
// await Promise.all(flecks.invokeFlat('@flecks/core/starting'));
// ({Entity} = flecks.get('$avocado/resource.resources'));
// });
// describe('Directional', () => {
// let entity;
// beforeEach(async () => {
// entity = await Entity.load({
// traits: {
// Directional: {
// params: {
// directionCount: 4,
// },
// },
// },
// });
// });
// it('exists', async () => {
// expect(entity.is('Directional')).to.be.true;
// });
// it('tracks movement', async () => {
// entity.emit('movementRequest', [1, 0]);
// expect(entity.direction).to.equal(1);
// entity.emit('movementRequest', [0, 1]);
// expect(entity.direction).to.equal(2);
// entity.emit('movementRequest', [-1, 0]);
// expect(entity.direction).to.equal(3);
// entity.emit('movementRequest', [0, -1]);
// expect(entity.direction).to.equal(0);
// });
// it('generates and accepts direction packets', async () => {
// entity.direction = 2;
// const packets = entity.trait('Directional').packetsFor();
// expect(packets).to.have.lengthOf(1);
// expect(packets[0][0]).to.equal('TraitUpdateDirectionalDirection');
// expect(packets[0][1]).to.equal(2);
// const entity2 = await Entity.load({
// traits: {
// Directional: {},
// },
// });
// expect(entity2.direction).to.equal(0);
// entity2.trait('Directional').acceptPacket(normalize(flecks, packets[0]));
// expect(entity2.direction).to.equal(2);
// });
// });
let flecks;
let Entity;
beforeEach(async () => {
flecks = Flecks.bootstrap({
config: {
'@avocado/entity:./src': {},
'@avocado/graphics': {},
'@avocado/resource': {},
'@avocado/traits': {},
'@flecks/core': {},
'@flecks/react': {},
'@flecks/socket': {},
},
});
await Promise.all(flecks.invokeFlat('@flecks/core.starting'));
({Entity} = flecks.get('$avocado/resource.resources'));
});
describe('Directional', () => {
let entity;
beforeEach(async () => {
entity = await Entity.load({
traits: {
Directional: {
params: {
directionCount: 4,
},
},
},
});
});
it('exists', async () => {
expect(entity.is('Directional')).to.be.true;
});
it('tracks movement', async () => {
entity.emit('movementRequest', [1, 0]);
expect(entity.direction).to.equal(1);
entity.emit('movementRequest', [0, 1]);
expect(entity.direction).to.equal(2);
entity.emit('movementRequest', [-1, 0]);
expect(entity.direction).to.equal(3);
entity.emit('movementRequest', [0, -1]);
expect(entity.direction).to.equal(0);
});
it('generates and accepts direction packets', async () => {
entity.direction = 2;
const packets = entity.trait('Directional').packetsFor();
expect(packets).to.have.lengthOf(1);
expect(packets[0][0]).to.equal('TraitUpdateDirectionalDirection');
expect(packets[0][1]).to.equal(2);
const entity2 = await Entity.load({
traits: {
Directional: {},
},
});
expect(entity2.direction).to.equal(0);
entity2.trait('Directional').acceptPacket(normalize(flecks, packets[0]));
expect(entity2.direction).to.equal(2);
});
});

View File

@ -1,93 +1,98 @@
// import {Trait, traits} from '@avocado/traits';
// import {Flecks} from '@flecks/core';
// import {expect} from 'chai';
import {Trait} from '@avocado/traits';
import {Flecks} from '@flecks/core/server';
import {expect} from 'chai';
// let flecks;
// let Entity;
// beforeEach(async () => {
// flecks = Flecks.mock({
// '@avocado/entity': require('../src'),
// '@avocado/resource': require('@avocado/resource'),
// '@avocado/traits': require('@avocado/traits'),
// });
// await Promise.all(flecks.invokeFlat('@flecks/core/starting'));
// ({Entity} = flecks.get('$avocado/resource.resources'));
// });
// it('has sane defaults', () => {
// const entity = new Entity();
// expect(entity.traits).to.deep.equal({});
// expect(entity.traitTypes()).to.deep.equal([]);
// });
// it('can add and remove traits', async () => {
// const entity = new Entity();
// const TestTrait = class extends Trait {
let flecks;
let Entity;
beforeEach(async () => {
flecks = Flecks.bootstrap({
config: {
'@avocado/entity:./src': {},
'@avocado/graphics': {},
'@avocado/resource': {},
'@avocado/traits': {},
'@flecks/core': {},
'@flecks/react': {},
},
});
await Promise.all(flecks.invokeFlat('@flecks/core.starting'));
({Entity} = flecks.get('$avocado/resource.resources'));
});
it('has sane defaults', () => {
const entity = new Entity();
expect(entity.traits).to.deep.equal({});
expect(entity.traitTypes()).to.deep.equal([]);
});
it('can add and remove traits', async () => {
const entity = new Entity();
const TestTrait = class extends Trait {
// static get type() {
// return 'TestTrait';
// }
static get type() {
return 'TestTrait';
}
// };
// flecks.set('$avocado/traits.traits.TestTrait', TestTrait);
// await entity.addTrait('TestTrait');
// expect(entity.is('TestTrait')).to.be.true;
// entity.removeTrait('TestTrait');
// expect(entity.is('TestTrait')).to.be.false;
// });
// it('can add traits asynchronously', async () => {
// const DELAY = 30;
// class AsyncTrait extends Trait {
};
flecks.set('$avocado/traits.traits.TestTrait', TestTrait);
await entity.addTrait('TestTrait');
expect(entity.is('TestTrait')).to.be.true;
entity.removeTrait('TestTrait');
expect(entity.is('TestTrait')).to.be.false;
});
it('can add traits asynchronously', async () => {
const DELAY = 30;
class AsyncTrait extends Trait {
// static async extendJson(json) {
// const extended = await super.extendJson(json);
// await new Promise((resolve) => setTimeout(resolve, DELAY));
// return extended;
// }
static async extendJson(json) {
const extended = await super.extendJson(json);
await new Promise((resolve) => setTimeout(resolve, DELAY));
return extended;
}
// }
// flecks.set('$avocado/traits.traits.Async', AsyncTrait);
// let start = Date.now();
// const entity = await Entity.load({
// traits: {
// Async: {},
// },
// });
// expect(Date.now() - start).to.be.at.least(DELAY * 0.9);
// });
// it('can invoke hooks', async () => {
// class AnotherTrait extends Trait {
}
flecks.set('$avocado/traits.traits.Async', AsyncTrait);
const start = Date.now();
await Entity.load({
traits: {
Async: {},
},
});
expect(Date.now() - start).to.be.at.least(DELAY * 0.9);
});
it('can invoke hooks', async () => {
class AnotherTrait extends Trait {
// hooks() {
// return {
hooks() {
return {
// testHook: () => 69,
testHook: () => 69,
// };
// }
};
}
// static get type() {
// return 'AnotherTrait';
// }
static get type() {
return 'AnotherTrait';
}
// }
// class YetAnotherTrait extends Trait {
}
class YetAnotherTrait extends Trait {
// hooks() {
// return {
hooks() {
return {
// testHook: () => 420,
testHook: () => 420,
// };
// }
};
}
// static get type() {
// return 'YetAnotherTrait';
// }
static get type() {
return 'YetAnotherTrait';
}
// }
// flecks.set('$avocado/traits.traits.AnotherTrait', AnotherTrait);
// flecks.set('$avocado/traits.traits.YetAnotherTrait', YetAnotherTrait);
// const entity = new Entity();
// await entity.addTrait('AnotherTrait');
// await entity.addTrait('YetAnotherTrait');
// expect(entity.invokeHook('testHook')).to.deep.equal({AnotherTrait: 69, YetAnotherTrait: 420});
// });
}
flecks.set('$avocado/traits.traits.AnotherTrait', AnotherTrait);
flecks.set('$avocado/traits.traits.YetAnotherTrait', YetAnotherTrait);
const entity = new Entity();
await entity.addTrait('AnotherTrait');
await entity.addTrait('YetAnotherTrait');
expect(entity.invokeHook('testHook')).to.deep.equal({AnotherTrait: 69, YetAnotherTrait: 420});
});

View File

@ -1,59 +1,64 @@
// import {Flecks} from '@flecks/core';
// import {expect} from 'chai';
import {Flecks} from '@flecks/core/server';
import {expect} from 'chai';
// let flecks;
// let Entity;
// let EntityList;
// beforeEach(async () => {
// flecks = Flecks.mock({
// '@avocado/entity': require('../src'),
// '@avocado/resource': require('@avocado/resource'),
// '@avocado/traits': require('@avocado/traits'),
// });
// await Promise.all(flecks.invokeFlat('@flecks/core/starting'));
// ({Entity, EntityList} = flecks.get('$avocado/resource.resources'));
// });
// describe('Mobile', () => {
// let entity;
// beforeEach(async () => {
// entity = await Entity.load({
// traits: {
// Mobile: {},
// Positioned: {},
// },
// });
// });
// it('exists', async () => {
// expect(entity.is('Mobile')).to.be.true;
// });
// it('can request movement', async () => {
// entity.speed = 100;
// entity.requestMovement([1, 0]);
// entity.tick(1);
// expect(entity.position).to.deep.equal([100, 0]);
// entity.tick(1);
// expect(entity.position).to.deep.equal([100, 0]);
// entity.isMobile = false;
// entity.requestMovement([1, 0]);
// entity.tick(1);
// expect(entity.position).to.deep.equal([100, 0]);
// });
// it('can force movement', async () => {
// expect(entity.speed).to.equal(0);
// entity.forceMovement([10, 0]);
// expect(entity.position).to.deep.equal([10, 0]);
// });
// it('can move for a time', async () => {
// entity.speed = 10;
// const tickingPromise = entity.moveFor([1, 0], 1);
// entity.addTickingPromise(tickingPromise);
// expect(entity.position).to.deep.equal([0, 0]);
// entity.tick(0.25);
// expect(entity.position).to.deep.equal([2.5, 0]);
// entity.tick(0.25);
// expect(entity.position).to.deep.equal([5, 0]);
// entity.tick(0.5);
// expect(entity.position).to.deep.equal([10, 0]);
// return tickingPromise;
// });
// });
let flecks;
let Entity;
beforeEach(async () => {
flecks = Flecks.bootstrap({
config: {
'@avocado/entity:./src': {},
'@avocado/graphics': {},
'@avocado/resource': {},
'@avocado/traits': {},
'@flecks/core': {},
'@flecks/react': {},
'@flecks/socket': {},
},
});
await Promise.all(flecks.invokeFlat('@flecks/core.starting'));
({Entity} = flecks.get('$avocado/resource.resources'));
});
describe('Mobile', () => {
let entity;
beforeEach(async () => {
entity = await Entity.load({
traits: {
Mobile: {},
Positioned: {},
},
});
});
it('exists', async () => {
expect(entity.is('Mobile')).to.be.true;
});
it('can request movement', async () => {
entity.speed = 100;
entity.requestMovement([1, 0]);
entity.tick(1);
expect(entity.position).to.deep.equal([100, 0]);
entity.tick(1);
expect(entity.position).to.deep.equal([100, 0]);
entity.isMobile = false;
entity.requestMovement([1, 0]);
entity.tick(1);
expect(entity.position).to.deep.equal([100, 0]);
});
it('can force movement', async () => {
expect(entity.speed).to.equal(0);
entity.forceMovement([10, 0]);
expect(entity.position).to.deep.equal([10, 0]);
});
it('can move for a time', async () => {
entity.speed = 10;
const tickingPromise = entity.moveFor([1, 0], 1);
entity.addTickingPromise(tickingPromise);
expect(entity.position).to.deep.equal([0, 0]);
entity.tick(0.25);
expect(entity.position).to.deep.equal([2.5, 0]);
entity.tick(0.25);
expect(entity.position).to.deep.equal([5, 0]);
entity.tick(0.5);
expect(entity.position).to.deep.equal([10, 0]);
return tickingPromise;
});
});

View File

@ -1,44 +1,49 @@
// import {Flecks} from '@flecks/core';
// import {expect} from 'chai';
import {Flecks} from '@flecks/core/server';
import {expect} from 'chai';
// let flecks;
// let Entity;
// let EntityList;
// beforeEach(async () => {
// flecks = Flecks.mock({
// '@avocado/entity': require('../src'),
// '@avocado/resource': require('@avocado/resource'),
// '@avocado/traits': require('@avocado/traits'),
// });
// await Promise.all(flecks.invokeFlat('@flecks/core/starting'));
// ({Entity, EntityList} = flecks.get('$avocado/resource.resources'));
// });
// describe('Perishable', () => {
// let entity;
// beforeEach(async () => {
// entity = await Entity.load({
// traits: {
// Perishable: {
// params: {
// ttl: 10,
// },
// },
// },
// });
// });
// it('exists', async () => {
// expect(entity.is('Perishable')).to.be.true;
// });
// it('expires', async () => {
// const promise = Promise.all([
// new Promise((resolve) => {
// entity.on('destroying', resolve);
// }),
// new Promise((resolve) => {
// entity.on('destroyed', resolve);
// }),
// ]);
// entity.tick(10);
// return promise;
// });
// });
let flecks;
let Entity;
beforeEach(async () => {
flecks = Flecks.bootstrap({
config: {
'@avocado/entity:./src': {},
'@avocado/graphics': {},
'@avocado/resource': {},
'@avocado/traits': {},
'@flecks/core': {},
'@flecks/react': {},
'@flecks/socket': {},
},
});
await Promise.all(flecks.invokeFlat('@flecks/core.starting'));
({Entity} = flecks.get('$avocado/resource.resources'));
});
describe('Perishable', () => {
let entity;
beforeEach(async () => {
entity = await Entity.load({
traits: {
Perishable: {
params: {
ttl: 10,
},
},
},
});
});
it('exists', async () => {
expect(entity.is('Perishable')).to.be.true;
});
it('expires', async () => {
const promise = Promise.all([
new Promise((resolve) => {
entity.on('destroying', resolve);
}),
new Promise((resolve) => {
entity.on('destroyed', resolve);
}),
]);
entity.tick(10);
return promise;
});
});

View File

@ -1,48 +1,52 @@
// import {Flecks} from '@flecks/core';
// import {normalize} from '@flecks/socket';
// import {expect} from 'chai';
import {Flecks} from '@flecks/core/server';
import {normalize} from '@flecks/socket';
import {expect} from 'chai';
// let flecks;
// let Entity;
// let EntityList;
// beforeEach(async () => {
// flecks = Flecks.mock({
// '@avocado/entity': require('../src'),
// '@avocado/resource': require('@avocado/resource'),
// '@avocado/traits': require('@avocado/traits'),
// '@flecks/socket': require('@flecks/socket'),
// });
// await Promise.all(flecks.invokeFlat('@flecks/core/starting'));
// ({Entity, EntityList} = flecks.get('$avocado/resource.resources'));
// });
// describe('Positioned', () => {
// let entity;
// beforeEach(async () => {
// entity = await Entity.load({
// traits: {
// Positioned: {},
// },
// });
// });
// it('exists', async () => {
// expect(entity.is('Positioned')).to.be.true;
// });
// if ('http' !== process.env.FLECKS_CORE_BUILD_TARGET) {
// it('generates and accepts movement packets', async () => {
// entity.setPosition([1, 1]);
// const packets = entity.trait('Positioned').packetsFor();
// expect(packets).to.have.lengthOf(1);
// expect(packets[0][0]).to.equal('TraitUpdatePositionedPosition');
// expect(packets[0][1]).to.deep.equal([1, 1]);
// const entity2 = await Entity.load({
// traits: {
// Positioned: {},
// },
// });
// expect(entity2.position).to.deep.equal([0, 0]);
// const trait = entity2.trait('Positioned');
// trait.acceptPacket(normalize(flecks, packets[0]));
// expect(trait.serverPosition).to.deep.equal([1, 1]);
// });
// }
// });
let flecks;
let Entity;
beforeEach(async () => {
flecks = Flecks.bootstrap({
config: {
'@avocado/entity:./src': {},
'@avocado/graphics': {},
'@avocado/resource': {},
'@avocado/traits': {},
'@flecks/core': {},
'@flecks/react': {},
'@flecks/socket': {},
},
});
await Promise.all(flecks.invokeFlat('@flecks/core.starting'));
({Entity} = flecks.get('$avocado/resource.resources'));
});
describe('Positioned', () => {
let entity;
beforeEach(async () => {
entity = await Entity.load({
traits: {
Positioned: {},
},
});
});
it('exists', async () => {
expect(entity.is('Positioned')).to.be.true;
});
if ('http' !== process.env.FLECKS_CORE_BUILD_TARGET) {
it('generates and accepts movement packets', async () => {
entity.setPosition([1, 1]);
const packets = entity.trait('Positioned').packetsFor();
expect(packets).to.have.lengthOf(1);
expect(packets[0][0]).to.equal('TraitUpdatePositionedPosition');
expect(packets[0][1]).to.deep.equal([1, 1]);
const entity2 = await Entity.load({
traits: {
Positioned: {},
},
});
expect(entity2.position).to.deep.equal([0, 0]);
const trait = entity2.trait('Positioned');
trait.acceptPacket(normalize(flecks, packets[0]));
expect(trait.serverPosition).to.deep.equal([1, 1]);
});
}
});

View File

@ -1,89 +1,95 @@
// import {Flecks} from '@flecks/core';
// import {expect} from 'chai';
import {Flecks} from '@flecks/core/server';
import {expect} from 'chai';
// let flecks;
// let Entity;
// let EntityList;
// beforeEach(async () => {
// flecks = Flecks.mock({
// '@avocado/behavior': require('@avocado/behavior'),
// '@avocado/entity': require('../src'),
// '@avocado/resource': require('@avocado/resource'),
// '@avocado/traits': require('@avocado/traits'),
// });
// await Promise.all(flecks.invokeFlat('@flecks/core/starting'));
// ({Entity, EntityList} = flecks.get('$avocado/resource.resources'));
// });
// describe('Spawner', () => {
// let entity;
// let list;
// beforeEach(async () => {
// entity = await Entity.load({
// traits: {
// Spawner: {
// params: {
// spawns: {
// testy: {
// traits: {
// Alive: {},
// Positioned: {},
// },
// },
// },
// },
// },
// },
// });
// list = new EntityList();
// list.addEntity(entity);
// });
// it('exists', async () => {
// expect(entity.is('Spawner')).to.be.true;
// });
// it('can spawn from key', async () => {
// const spawned = await entity.spawn('testy');
// expect(spawned.is('Alive')).to.be.true;
// const spawned2 = await entity.spawn('testy', {
// traits: {
// Alive: {
// state: {
// life: 50,
// },
// },
// },
// });
// expect(spawned2.life).to.equal(50);
// const spawned3 = await entity.spawnAt('testy', [69, 420]);
// expect(spawned3.position).to.deep.equal([69, 420]);
// });
// it('can spawn from arbitrary JSON', async () => {
// const spawned = await entity.spawnRaw({
// traits: {
// Mobile: {},
// },
// });
// expect(spawned.is('Mobile')).to.be.true;
// const spawned2 = await entity.spawnRawAt(
// {
// traits: {
// Mobile: {},
// },
// },
// [311, 200],
// );
// expect(spawned2.position).to.deep.equal([311, 200]);
// });
// it('can kill all children', async () => {
// const COUNT = 15;
// for (let i = 0; i < COUNT; ++i) {
// await entity.spawnRaw({
// traits: {
// Mobile: {},
// },
// });
// }
// expect(Object.keys(list.entities)).to.have.lengthOf(COUNT + 1);
// await entity.killAllChildren();
// expect(Object.keys(list.entities)).to.have.lengthOf(1);
// });
// });
let flecks;
let Entity;
let EntityList;
beforeEach(async () => {
flecks = Flecks.bootstrap({
config: {
'@avocado/behavior': {},
'@avocado/entity:./src': {},
'@avocado/graphics': {},
'@avocado/resource': {},
'@avocado/traits': {},
'@flecks/core': {},
'@flecks/react': {},
'@flecks/socket': {},
},
});
await Promise.all(flecks.invokeFlat('@flecks/core.starting'));
({Entity, EntityList} = flecks.get('$avocado/resource.resources'));
});
describe('Spawner', () => {
let entity;
let list;
beforeEach(async () => {
entity = await Entity.load({
traits: {
Spawner: {
params: {
spawns: {
testy: {
traits: {
Alive: {},
Positioned: {},
},
},
},
},
},
},
});
list = new EntityList();
list.addEntity(entity);
});
it('exists', async () => {
expect(entity.is('Spawner')).to.be.true;
});
it('can spawn from key', async () => {
const spawned = await entity.spawn('testy');
expect(spawned.is('Alive')).to.be.true;
const spawned2 = await entity.spawn('testy', {
traits: {
Alive: {
state: {
life: 50,
},
},
},
});
expect(spawned2.life).to.equal(50);
const spawned3 = await entity.spawnAt('testy', [69, 420]);
expect(spawned3.position).to.deep.equal([69, 420]);
});
it('can spawn from arbitrary JSON', async () => {
const spawned = await entity.spawnRaw({
traits: {
Mobile: {},
},
});
expect(spawned.is('Mobile')).to.be.true;
const spawned2 = await entity.spawnRawAt(
{
traits: {
Mobile: {},
},
},
[311, 200],
);
expect(spawned2.position).to.deep.equal([311, 200]);
});
it('can kill all children', async () => {
const COUNT = 15;
await Promise.all(Array(COUNT).fill(0).map(() => (
entity.spawnRaw({
traits: {
Mobile: {},
},
})
)));
expect(Object.keys(list.entities)).to.have.lengthOf(COUNT + 1);
await entity.killAllChildren();
expect(Object.keys(list.entities)).to.have.lengthOf(1);
});
});