silphius/app/engine.test.js
2024-06-23 07:00:10 -05:00

63 lines
1.6 KiB
JavaScript

import {expect, test} from 'vitest';
import {RESOLUTION} from '@/constants.js'
import Server from '@/net/server/server.js';
import Engine from './engine.js';
class TestServer extends Server {
constructor() {
super();
this.data = {};
}
async readData(path) {
if (path in this.data) {
return this.data[path];
}
const error = new Error();
error.code = 'ENOENT';
throw error;
}
async writeData(path, view) {
this.data[path] = view;
}
}
test('visibility-based updates', async () => {
const engine = new Engine(TestServer);
// Connect an entity.
await engine.connectPlayer(0, 0);
const ecs = engine.ecses['homesteads/0'];
// Create an entity.
const entity = ecs.get(ecs.create({
Momentum: {x: 1, y: 0},
Position: {x: (RESOLUTION.x * 1.5) + 32 - 3, y: 20},
VisibleAabb: {},
}));
// Tick and get update. Should be a full update.
engine.tick(1);
expect(engine.updateFor(0))
.to.deep.include({2: {MainEntity: {}, ...ecs.get(2).toJSON()}, 3: ecs.get(3).toJSON()});
// Tick and get update. Should be a partial update.
engine.tick(1);
expect(engine.updateFor(0))
.to.deep.include({
3: {
Position: {x: (RESOLUTION.x * 1.5) + 32 - 1},
VisibleAabb: {
x0: 1199,
x1: 1263,
},
},
});
// Tick and get update. Should remove the entity.
engine.tick(1);
expect(engine.updateFor(0))
.to.deep.include({3: false});
// Aim back toward visible area and tick. Should be a full update for that entity.
entity.Momentum.x = -1;
engine.tick(1);
expect(engine.updateFor(0))
.to.deep.include({3: ecs.get(3).toJSON()});
});