2024-06-10 22:42:30 -05:00
|
|
|
import {expect, test} from 'vitest';
|
|
|
|
|
2024-08-14 17:46:59 -05:00
|
|
|
import Ecs from './ecs.js';
|
|
|
|
import {wrapComponents} from './test-helper.js';
|
2024-06-10 22:42:30 -05:00
|
|
|
import Query from './query.js';
|
|
|
|
|
2024-08-14 17:46:59 -05:00
|
|
|
const Components = wrapComponents([
|
2024-08-03 11:20:08 -05:00
|
|
|
['A', {a: {type: 'int32', defaultValue: 64}}],
|
|
|
|
['B', {b: {type: 'int32', defaultValue: 32}}],
|
|
|
|
['C', {c: {type: 'int32'}}],
|
2024-08-14 17:46:59 -05:00
|
|
|
]);
|
2024-06-10 22:42:30 -05:00
|
|
|
|
2024-08-03 11:20:08 -05:00
|
|
|
const ecsTest = test.extend({
|
|
|
|
ecs: async ({}, use) => {
|
|
|
|
const ecs = new Ecs({Components});
|
|
|
|
await ecs.createManySpecific([
|
|
|
|
[1, {B: {}}],
|
|
|
|
[2, {A: {}, B: {}, C: {}}],
|
|
|
|
[3, {A: {}}],
|
|
|
|
[4, {C: {}}],
|
|
|
|
]);
|
|
|
|
await use(ecs);
|
2024-06-26 07:41:07 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-08-03 11:20:08 -05:00
|
|
|
async function testQuery(ecs, parameters, expected) {
|
|
|
|
const query = new Query(parameters, ecs);
|
2024-06-10 22:42:30 -05:00
|
|
|
query.reindex([1, 2, 3]);
|
|
|
|
expect(query.count)
|
|
|
|
.to.equal(expected.length);
|
|
|
|
for (const _ of query.select()) {
|
2024-06-26 07:41:07 -05:00
|
|
|
expect(expected.includes(_.id))
|
2024-06-10 22:42:30 -05:00
|
|
|
.to.equal(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-03 11:20:08 -05:00
|
|
|
ecsTest('can query all', async ({ecs}) => {
|
|
|
|
await testQuery(ecs, [], [1, 2, 3]);
|
2024-06-10 22:42:30 -05:00
|
|
|
});
|
|
|
|
|
2024-08-03 11:20:08 -05:00
|
|
|
ecsTest('can query some', async ({ecs}) => {
|
|
|
|
await testQuery(ecs, ['A'], [2, 3]);
|
|
|
|
await testQuery(ecs, ['A', 'B'], [2]);
|
2024-06-10 22:42:30 -05:00
|
|
|
});
|
|
|
|
|
2024-08-03 11:20:08 -05:00
|
|
|
ecsTest('can query excluding', async ({ecs}) => {
|
|
|
|
await testQuery(ecs, ['!A'], [1]);
|
|
|
|
await testQuery(ecs, ['A', '!B'], [3]);
|
2024-06-10 22:42:30 -05:00
|
|
|
});
|
|
|
|
|
2024-08-03 11:20:08 -05:00
|
|
|
ecsTest('can deindex', async ({ecs}) => {
|
|
|
|
const query = new Query(['A'], ecs);
|
2024-06-10 22:42:30 -05:00
|
|
|
query.reindex([1, 2, 3]);
|
|
|
|
expect(query.count)
|
|
|
|
.to.equal(2);
|
|
|
|
query.deindex([2]);
|
|
|
|
expect(query.count)
|
|
|
|
.to.equal(1);
|
|
|
|
});
|
|
|
|
|
2024-08-03 11:20:08 -05:00
|
|
|
ecsTest('can reindex', async ({ecs}) => {
|
|
|
|
const query = new Query(['B'], ecs);
|
|
|
|
query.reindex([1, 2]);
|
2024-06-10 22:42:30 -05:00
|
|
|
expect(query.count)
|
|
|
|
.to.equal(2);
|
2024-08-03 11:20:08 -05:00
|
|
|
ecs.destroyMany(new Set([2]));
|
|
|
|
query.reindex([1, 2]);
|
2024-06-10 22:42:30 -05:00
|
|
|
expect(query.count)
|
|
|
|
.to.equal(1);
|
|
|
|
});
|
|
|
|
|
2024-08-03 11:20:08 -05:00
|
|
|
ecsTest('can select', async ({ecs}) => {
|
|
|
|
const query = new Query(['A'], ecs);
|
2024-06-10 22:42:30 -05:00
|
|
|
query.reindex([1, 2, 3]);
|
|
|
|
const it = query.select();
|
2024-06-26 07:41:07 -05:00
|
|
|
const {value: {A}} = it.next();
|
|
|
|
expect(A.a)
|
2024-08-03 11:20:08 -05:00
|
|
|
.to.equal(64);
|
2024-06-10 22:42:30 -05:00
|
|
|
});
|