avocado/packages/behavior/test/behaved.js
2021-01-03 23:45:44 -06:00

152 lines
4.6 KiB
JavaScript

import {resource} from '@avocado/resource';
import {Latus} from '@latus/core';
import {expect} from 'chai';
import {
buildExpression,
buildExpressions,
buildInvoke,
buildValue,
} from '../src/builders';
const {name} = require('../package.json');
const asyncWait = () => new Promise((resolve) => setTimeout(resolve, 0));
describe(name, () => {
let latus;
let Entity;
beforeEach(async () => {
latus = Latus.mock([
['@avocado/behavior', `${__dirname}/../src`],
'@avocado/entity',
'@avocado/resource',
'@avocado/traits',
]);
await Promise.all(latus.invokeFlat('@latus/core/starting'));
({fromResourceType: {Entity}} = resource(latus));
});
describe('Traits', () => {
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'),
],
),
]),
},
},
},
Mobile: {},
Positioned: {},
},
});
});
it('exists', async () => {
expect(entity.is('Behaved')).to.be.true;
});
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]);
});
});
});
});