Fixed failing tests
This commit is contained in:
@@ -12,7 +12,7 @@ export default function () {
|
||||
|
||||
// Items
|
||||
active('itemUnequippedId', 'Unequipped items should be active');
|
||||
byAncestor('itemUnequippedChildId', 'Children of unequipped items should be inactive');
|
||||
byAncestor('itemUnEQChildId', 'Children of unequipped items should be inactive');
|
||||
active('itemEquippedId', 'Equipped items should be active');
|
||||
active('itemEquippedChildId', 'Children of equipped items should be active');
|
||||
|
||||
@@ -56,7 +56,7 @@ var testProperties = [
|
||||
parentId: 'charId',
|
||||
}),
|
||||
clean({
|
||||
_id: 'itemUnequippedChildId',
|
||||
_id: 'itemUnEQChildId',
|
||||
type: 'folder',
|
||||
parentId: 'itemUnequippedId',
|
||||
}),
|
||||
|
||||
@@ -34,7 +34,8 @@ export default function buildCreatureComputation(creatureId: string) {
|
||||
const creature = getCreature(creatureId);
|
||||
if (!creature) {
|
||||
throw new Meteor.Error('not-found',
|
||||
'Build computation failed, the creature was not found'
|
||||
'Build computation failed, the creature was not found.' +
|
||||
'\nid: ' + creatureId
|
||||
);
|
||||
}
|
||||
const variables = getVariables(creatureId);
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import Creatures from '/imports/api/creature/creatures/Creatures';
|
||||
import { TestCreature } from '/imports/api/engine/action/functions/actionEngineTest.testFn';
|
||||
import { buildComputationFromProps } from '/imports/api/engine/computation/buildCreatureComputation';
|
||||
import propsFromForest from '/imports/api/engine/computation/utility/propsFromForest.testFn';
|
||||
import { cleanAndValidate } from '/imports/api/utility/TypedSimpleSchema';
|
||||
|
||||
export default function buildTestComputation(testCreature: TestCreature) {
|
||||
const creature = cleanAndValidate(Creatures.simpleSchema(), {
|
||||
_id: testCreature._id,
|
||||
name: testCreature.name || 'Test Creature',
|
||||
dirty: true,
|
||||
owner: Random.id(),
|
||||
readers: [],
|
||||
writers: [],
|
||||
});
|
||||
const props = propsFromForest(testCreature.props, creature._id);
|
||||
return buildComputationFromProps(props, creature, {});
|
||||
}
|
||||
@@ -1,36 +1,42 @@
|
||||
import { buildComputationFromProps } from '/imports/api/engine/computation/buildCreatureComputation';
|
||||
import { assert } from 'chai';
|
||||
import computeCreatureComputation from '../../computeCreatureComputation';
|
||||
import clean from '../../utility/cleanProp.testFn';
|
||||
import computeCreatureComputation from '/imports/api/engine/computation/computeCreatureComputation';
|
||||
import buildTestComputation from './buildTestComputation';
|
||||
import type { ForestProp } from '/imports/api/engine/computation/utility/propsFromForest.testFn';
|
||||
import { CreaturePropertyTypes } from '/imports/api/creature/creatureProperties/CreatureProperties';
|
||||
|
||||
export default async function () {
|
||||
const computation = buildComputationFromProps(testProperties);
|
||||
const computation = buildTestComputation({
|
||||
_id: 'testCreatureId',
|
||||
props: testProperties,
|
||||
});
|
||||
await computeCreatureComputation(computation);
|
||||
|
||||
const prop = computation.propsById['actionId'];
|
||||
assert.equal(prop.summary.value, 'test summary 3 without referencing anything 7');
|
||||
assert.equal(prop.description.value, 'test description 12 with reference 0.25 prop');
|
||||
assert.equal(prop.uses.value, 7);
|
||||
const prop = computation.propsById['actionId'] as CreaturePropertyTypes['action'];
|
||||
assert.equal(prop.summary?.value, 'test summary 3 without referencing anything 7');
|
||||
assert.equal(prop.description?.value, 'test description 12 with reference 0.25 prop');
|
||||
assert.equal(prop.uses?.value, 7);
|
||||
assert.equal(prop.usesLeft, 2);
|
||||
|
||||
const rolled = computation.propsById['rolledDescriptionId'];
|
||||
assert.equal(rolled.summary.value, 'test roll gets compiled 8 properly');
|
||||
const rolled = computation.propsById['rolledDescriptionId'] as CreaturePropertyTypes['action'];
|
||||
assert.equal(rolled.summary?.value, 'test roll gets compiled 8 properly');
|
||||
|
||||
const itemConsumed = prop.resources.itemsConsumed[0];
|
||||
assert.equal(itemConsumed.quantity.value, 3);
|
||||
assert.exists(itemConsumed);
|
||||
assert.equal(itemConsumed.quantity?.value, 3);
|
||||
assert.equal(itemConsumed.available, 27);
|
||||
assert.equal(itemConsumed.itemName, 'Arrow');
|
||||
assert.equal(itemConsumed.itemIcon, 'itemIcon');
|
||||
assert.equal(itemConsumed.itemColor, 'itemColor');
|
||||
assert.equal(itemConsumed.itemIcon?.name, 'itemIcon');
|
||||
assert.equal(itemConsumed.itemColor, '#fff');
|
||||
|
||||
const attConsumed = prop.resources.attributesConsumed[0];
|
||||
assert.equal(attConsumed.quantity.value, 4);
|
||||
assert.exists(attConsumed);
|
||||
assert.equal(attConsumed.quantity?.value, 4);
|
||||
assert.equal(attConsumed.available, 9);
|
||||
assert.equal(attConsumed.statName, 'Resource Var');
|
||||
}
|
||||
|
||||
var testProperties = [
|
||||
clean({
|
||||
const testProperties: ForestProp[] = [
|
||||
{
|
||||
_id: 'actionId',
|
||||
type: 'action',
|
||||
summary: {
|
||||
@@ -55,6 +61,7 @@ var testProperties = [
|
||||
calculation: 'resourceConsumedQuantity'
|
||||
}
|
||||
}],
|
||||
conditions: [],
|
||||
},
|
||||
uses: {
|
||||
calculation: 'nonExistentProperty + 7',
|
||||
@@ -62,8 +69,8 @@ var testProperties = [
|
||||
usesUsed: 5,
|
||||
left: 1,
|
||||
right: 2,
|
||||
}),
|
||||
clean({
|
||||
},
|
||||
{
|
||||
_id: 'rolledDescriptionId',
|
||||
type: 'action',
|
||||
summary: {
|
||||
@@ -71,9 +78,9 @@ var testProperties = [
|
||||
},
|
||||
left: 3,
|
||||
right: 4,
|
||||
}),
|
||||
clean({
|
||||
_id: 'numItemsConumedId',
|
||||
},
|
||||
{
|
||||
_id: 'numItemsConsumedId',
|
||||
type: 'attribute',
|
||||
variableName: 'itemConsumedQuantity',
|
||||
baseValue: {
|
||||
@@ -81,9 +88,9 @@ var testProperties = [
|
||||
},
|
||||
left: 5,
|
||||
right: 6,
|
||||
}),
|
||||
clean({
|
||||
_id: 'numResourceConumedId',
|
||||
},
|
||||
{
|
||||
_id: 'numResourceConsumedId',
|
||||
type: 'attribute',
|
||||
variableName: 'resourceConsumedQuantity',
|
||||
baseValue: {
|
||||
@@ -91,8 +98,8 @@ var testProperties = [
|
||||
},
|
||||
left: 7,
|
||||
right: 8,
|
||||
}),
|
||||
clean({
|
||||
},
|
||||
{
|
||||
_id: 'resourceVarId',
|
||||
name: 'Resource Var',
|
||||
type: 'attribute',
|
||||
@@ -102,8 +109,8 @@ var testProperties = [
|
||||
},
|
||||
left: 9,
|
||||
right: 10,
|
||||
}),
|
||||
clean({
|
||||
},
|
||||
{
|
||||
_id: 'inlineRefResourceId',
|
||||
type: 'attribute',
|
||||
variableName: 'inlineRef',
|
||||
@@ -112,15 +119,15 @@ var testProperties = [
|
||||
},
|
||||
left: 11,
|
||||
right: 12,
|
||||
}),
|
||||
clean({
|
||||
},
|
||||
{
|
||||
_id: 'arrowId',
|
||||
type: 'item',
|
||||
name: 'Arrow',
|
||||
quantity: 27,
|
||||
icon: 'itemIcon',
|
||||
color: 'itemColor',
|
||||
icon: { name: 'itemIcon', shape: 'itemIconShape' },
|
||||
color: '#fff',
|
||||
left: 13,
|
||||
right: 14,
|
||||
}),
|
||||
},
|
||||
];
|
||||
@@ -2,7 +2,7 @@ import { buildComputationFromProps } from '/imports/api/engine/computation/build
|
||||
import { assert } from 'chai';
|
||||
import computeCreatureComputation from '../../computeCreatureComputation';
|
||||
import clean from '../../utility/cleanProp.testFn';
|
||||
import { propsFromForest } from '/imports/api/properties/tests/propTestBuilder.testFn';
|
||||
import propsFromForest from '/imports/api/engine/computation/utility/propsFromForest.testFn';
|
||||
|
||||
export default async function () {
|
||||
const computation = buildComputationFromProps(testProperties);
|
||||
@@ -1,7 +1,7 @@
|
||||
import { buildComputationFromProps } from '/imports/api/engine/computation/buildCreatureComputation';
|
||||
import { assert } from 'chai';
|
||||
import computeCreatureComputation from '../../computeCreatureComputation';
|
||||
import { propsFromForest } from '/imports/api/properties/tests/propTestBuilder.testFn';
|
||||
import propsFromForest from '/imports/api/engine/computation/utility/propsFromForest.testFn';
|
||||
|
||||
export default async function () {
|
||||
const computation = buildComputationFromProps(testProperties);
|
||||
@@ -2,12 +2,18 @@ import computeCreatureComputation from './computeCreatureComputation';
|
||||
import { buildComputationFromProps } from './buildCreatureComputation';
|
||||
import { assert } from 'chai';
|
||||
import CreatureProperties, { CreatureProperty } from '/imports/api/creature/creatureProperties/CreatureProperties';
|
||||
import computeTests from './computeComputation/tests/index';
|
||||
import Creatures, { Creature } from 'imports/api/creature/creatures/Creatures';
|
||||
import computeTests from '/imports/api/engine/computation/computeComputation/tstFns';
|
||||
import Creatures from '/imports/api/creature/creatures/Creatures';
|
||||
import { cleanAndValidate } from '/imports/api/utility/TypedSimpleSchema';
|
||||
import { createTestCreature } from '/imports/api/engine/action/functions/actionEngineTest.testFn';
|
||||
|
||||
describe('Compute computation', function () {
|
||||
it('Computes something at all', function () {
|
||||
const creature: Creature = Creatures.schema.clean({});
|
||||
it('Computes something at all', async function () {
|
||||
const creature = cleanAndValidate(Creatures.simpleSchema(), {
|
||||
owner: Random.id(),
|
||||
readers: [],
|
||||
writers: [],
|
||||
});
|
||||
const computation = buildComputationFromProps(testProperties, creature, {});
|
||||
computeCreatureComputation(computation);
|
||||
assert.exists(computation);
|
||||
@@ -30,8 +36,8 @@ const testProperties = [
|
||||
}),
|
||||
];
|
||||
|
||||
function clean(prop: Partial<CreatureProperty>): CreatureProperty {
|
||||
// @ts-expect-error don't have types for .simpleSchema
|
||||
function clean(prop: Partial<CreatureProperty>) {
|
||||
prop.root ??= { collection: 'creatures', id: 'testCreature' };
|
||||
const schema = CreatureProperties.simpleSchema(prop);
|
||||
return schema.clean(prop);
|
||||
return cleanAndValidate(schema, prop);
|
||||
}
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties';
|
||||
|
||||
export default function cleanProp(prop) {
|
||||
if (!prop.root) {
|
||||
prop.root = { collection: 'creatures', id: 'testCreature' }
|
||||
}
|
||||
let schema = CreatureProperties.simpleSchema(prop);
|
||||
return schema.clean(prop);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { SetRequired } from 'type-fest';
|
||||
import CreatureProperties, { CreatureProperty, CreaturePropertyTypes } from '/imports/api/creature/creatureProperties/CreatureProperties';
|
||||
import { cleanAndValidate } from '/imports/api/utility/TypedSimpleSchema';
|
||||
|
||||
export default function cleanProp<T extends SetRequired<Partial<CreatureProperty>, 'type'>>(prop: T): CreaturePropertyTypes[T['type']] {
|
||||
if (!prop.root) {
|
||||
prop.root = { collection: 'creatures', id: 'testCreature' }
|
||||
}
|
||||
const schema = CreatureProperties.simpleSchema(prop);
|
||||
return cleanAndValidate(schema, prop as Partial<CreatureProperty>) as CreaturePropertyTypes[T['type']];
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import type { CreatureProperty } from '/imports/api/creature/creatureProperties/CreatureProperties';
|
||||
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties';
|
||||
import { applyNestedSetProperties } from '/imports/api/parenting/parentingFunctions';
|
||||
import { cleanAndValidate } from '/imports/api/utility/TypedSimpleSchema';
|
||||
|
||||
export type ForestProp = Partial<CreatureProperty> & {
|
||||
type: CreatureProperty['type'];
|
||||
children?: ForestProp[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Take a forest of props, which can have sub-props nested in children: [], and return a list of
|
||||
* clean props with correct tree and ancestry data
|
||||
* @param props
|
||||
* @returns
|
||||
*/
|
||||
export default function propsFromForest(
|
||||
props: ForestProp[],
|
||||
creatureId = Random.id(),
|
||||
parentId?: string,
|
||||
recursionDepth = 0
|
||||
) {
|
||||
const result: CreatureProperty[] = [];
|
||||
props.forEach(prop => {
|
||||
const children = prop.children;
|
||||
// Check the property has a type
|
||||
if (!prop.type) {
|
||||
throw new Error('Type is required on every property, not found on doc: ' + JSON.stringify(prop, null, 2));
|
||||
}
|
||||
// Create the clean doc
|
||||
const doc = {
|
||||
...prop,
|
||||
left: result.length,
|
||||
root: { id: creatureId, collection: 'creatures' },
|
||||
};
|
||||
if (parentId) {
|
||||
doc.parentId = parentId;
|
||||
}
|
||||
if (!doc._id) {
|
||||
doc._id = Random.id();
|
||||
}
|
||||
delete doc.children;
|
||||
const creatureProp = cleanAndValidate(CreatureProperties.simpleSchema(doc), doc);
|
||||
|
||||
// Add the doc to the result and ancestry
|
||||
result.push(creatureProp);
|
||||
if (children) {
|
||||
result.push(...propsFromForest(children, creatureId, doc._id, recursionDepth + 1));
|
||||
}
|
||||
});
|
||||
|
||||
// Apply the nested set properties on the top level
|
||||
if (recursionDepth === 0) {
|
||||
applyNestedSetProperties(result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user