avocado/packages/behavior/test/behaved.js

147 lines
4.1 KiB
JavaScript
Raw Normal View History

2021-01-03 23:40:26 -06:00
import {Latus} from '@latus/core';
import {expect} from 'chai';
import {
buildExpression,
buildExpressions,
buildInvoke,
buildValue,
} from '../src/builders';
const asyncWait = () => new Promise((resolve) => setTimeout(resolve, 0));
2021-01-05 11:25:37 -06:00
let latus;
let Entity;
beforeEach(async () => {
latus = Latus.mock({
'@avocado/behavior': require('../src'),
'@avocado/entity': require('@avocado/entity'),
'@avocado/resource': require('@avocado/resource'),
'@avocado/traits': require('@avocado/traits'),
2021-01-03 23:40:26 -06:00
});
2021-01-05 11:25:37 -06:00
await Promise.all(latus.invokeFlat('@latus/core/starting'));
2021-01-22 17:58:45 -06:00
({Entity} = latus.get('%resources'));
2021-01-05 11:25:37 -06:00
});
describe('Behaved', () => {
let entity;
beforeEach(async () => {
entity = await Entity.load({
traits: {
Behaved: {
params: {
routines: {
initial: buildExpressions([
buildInvoke(
['entity', 'forceMovement'],
[
buildValue([10, 0]),
],
),
buildInvoke(
['Timing', 'wait'],
[
buildValue(1),
],
),
]),
another: buildExpressions([
buildInvoke(
['entity', 'forceMovement'],
[
buildValue([20, 0]),
],
),
]),
one: buildExpressions([
buildInvoke(
['entity', 'forceMovement'],
[
buildValue([30, 0]),
],
),
buildInvoke(
['Timing', 'wait'],
[
buildValue(0),
],
),
buildInvoke(
['entity', 'jumpToRoutine'],
[
buildValue('two'),
],
),
]),
two: buildExpressions([
buildInvoke(
['entity', 'forceMovement'],
[
buildValue([40, 0]),
],
),
buildInvoke(
['entity', 'jumpToRoutine'],
[
buildValue('one'),
],
),
]),
2021-01-03 23:40:26 -06:00
},
},
2021-01-05 11:25:37 -06:00
},
Mobile: {},
Positioned: {},
},
2021-01-03 23:40:26 -06:00
});
});
2021-01-05 11:25:37 -06:00
it('exists', async () => {
expect(entity.is('Behaved')).to.be.true;
});
if ('client' !== process.env.SIDE) {
it('runs routines', async () => {
expect(entity.position).to.deep.equal([0, 0]);
entity.tick(0);
await asyncWait();
expect(entity.position).to.deep.equal([10, 0]);
entity.tick(1);
await asyncWait();
expect(entity.position).to.deep.equal([10, 0]);
entity.isBehaving = false;
entity.tick(1);
await asyncWait();
expect(entity.position).to.deep.equal([10, 0]);
entity.isBehaving = true;
entity.tick(1);
await asyncWait();
expect(entity.position).to.deep.equal([20, 0]);
});
it('can change routines', async () => {
entity.currentRoutine = 'another';
expect(entity.position).to.deep.equal([0, 0]);
entity.tick(0);
await asyncWait();
expect(entity.position).to.deep.equal([20, 0]);
});
it('allows routines to set routine', async () => {
entity.currentRoutine = 'one';
expect(entity.position).to.deep.equal([0, 0]);
entity.tick(0);
await asyncWait();
expect(entity.currentRoutine).to.equal('one');
expect(entity.position).to.deep.equal([30, 0]);
entity.tick(0);
await asyncWait();
expect(entity.currentRoutine).to.equal('one');
expect(entity.position).to.deep.equal([30, 0]);
entity.tick(0);
await asyncWait();
expect(entity.currentRoutine).to.equal('two');
expect(entity.position).to.deep.equal([30, 0]);
entity.tick(0);
await asyncWait();
expect(entity.currentRoutine).to.equal('one');
expect(entity.position).to.deep.equal([70, 0]);
});
}
2021-01-03 23:40:26 -06:00
});