Skip to content

Commit

Permalink
Fixed broken tests and added vs code mocha debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolas Howard committed Feb 8, 2024
1 parent a9bc843 commit 66d4672
Show file tree
Hide file tree
Showing 20 changed files with 308 additions and 125 deletions.
32 changes: 32 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\dist\\index.js"
}, {
"args": [
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/test/**/*.js"
],
"internalConsoleOptions": "openOnSessionStart",
"name": "Mocha Tests",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"request": "launch",
"skipFiles": [
"<node_internals>/**"
],
"type": "node"
}
]
}
21 changes: 8 additions & 13 deletions dist/bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/bundle.js.map

Large diffs are not rendered by default.

21 changes: 8 additions & 13 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/index.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
"sinon": "^14.0.1"
},
"scripts": {
"build": "npm-run-all build:node build:web build:typecheck",
"build": "npm-run-all build:format build:node build:web build:typecheck",
"watch": "npm run build:node -- --watch",
"test": "npm-run-all build:format build test:unit-test",
"test": "npm-run-all build test:unit-test",
"build:format": "prettier --write \"src/**/*.ts\" \"test/**/*.js\"",
"build:node": "esbuild ./src/index.js --bundle --sourcemap --outdir=dist --platform=node",
"build:web": "esbuild ./src/index.js --bundle --sourcemap --platform=browser --global-name=mistreevous --outfile=dist/bundle.js",
Expand Down
26 changes: 12 additions & 14 deletions src/BehaviourTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { GuardAttributeDetails } from "./attributes/guards/Guard";
import { BehaviourTreeOptions } from "./BehaviourTreeOptions";
import { convertMDSLToJSON } from "./mdsl/MDSLDefinitionParser";
import { RootNodeDefinition } from "./BehaviourTreeDefinition";
import { validateJSONDefinition } from "./BehaviourTreeDefinitionValidator";
import { validateDefinition, validateJSONDefinition } from "./BehaviourTreeDefinitionValidator";
import buildRootNode from "./BehaviourTreeBuilder";
import { isNullOrUndefined } from "./BehaviourTreeDefinitionUtilities";

Expand Down Expand Up @@ -56,6 +56,14 @@ export class BehaviourTree {
throw new Error("the agent must be an object and not null");
}

// We should validate the definition before we try to build the tree nodes.
const { succeeded, errorMessage } = validateDefinition(definition);

// Did our validation fail without error?
if (!succeeded) {
throw new Error(`invalid definition: ${errorMessage}`);
}

try {
// Parse the behaviour tree definition, create the populated tree of behaviour tree nodes, and get the root.
this.rootNode = this._createRootNode(definition);
Expand Down Expand Up @@ -244,19 +252,9 @@ export class BehaviourTree {
* @returns The root behaviour tree node.
*/
private _createRootNode(definition: string | RootNodeDefinition | RootNodeDefinition[]): Root {
let resolvedDefinition: RootNodeDefinition | RootNodeDefinition[];

// If the definition is a string then we will assume that it is an mdsl string which needs to be converted to a JSON definition.
if (typeof definition === "string") {
try {
resolvedDefinition = convertMDSLToJSON(definition);
} catch (exception) {
throw new Error(`invalid mdsl definition: ${(exception as Error).message}`);
}
} else {
// The definition is not a string, so we should assume that it is already a JSON definition.
resolvedDefinition = definition;
}
// Our definition will have to be converted to JSON if it is a MDSL string.
// TODO convertMDSLToJSON is called TWICE, once for validation and once here. Maybe return the JSON as part of the validation result?
const resolvedDefinition = typeof definition === "string" ? convertMDSLToJSON(definition) : definition;

// Build and populate the root node.
return buildRootNode(Array.isArray(resolvedDefinition) ? resolvedDefinition : [resolvedDefinition]);
Expand Down
4 changes: 2 additions & 2 deletions src/BehaviourTreeDefinitionValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ export function validateMDSLDefinition(definition: string): DefinitionValidation
try {
// The definition is a string which we can assume is MDSL, so attempt to parse it to a JSON definition in the form of an array of root node definitions.
rootNodeDefinitions = convertMDSLToJSON(definition);
} catch (error) {
} catch (exception) {
// We failed to parse the JSON from the MDSL, this is likely to be the result of it not being a valid MDSL string.
return createValidationFailureResult(`invalid MDSL: ${error}`);
return createValidationFailureResult((exception as Error).message);
}

// Unpack all of the root node definitions into arrays of main ('id' defined) and sub ('id' not defined) root node definitions.
Expand Down
2 changes: 1 addition & 1 deletion src/mdsl/MDSLDefinitionParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ function createWaitNode(tokens: string[], stringLiteralPlaceholders: StringLiter
nodeArguments
.filter((arg) => arg.type !== "number" || !arg.isInteger)
.forEach(() => {
throw new Error(`wait node duration arguments must be integer values`);
throw new Error(`wait node durations must be integer values`);
});

// We may have:
Expand Down
4 changes: 2 additions & 2 deletions test/BehaviourTreeDefinitionValidator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ describe("The validateDefinition function takes a tree definition as an argument
};

describe("where the type of that definition is", () => {
describe("mdsl", () => {
describe("MDSL", () => {
// TODO Add better validation to mdsl parsing to better match the json validation.
});

describe("json", () => {
describe("JSON", () => {
describe("returns a validation failure when", () => {
it("the definition doesn't contain a main root node (has no root node identifier defined)", () => {
const definition = {
Expand Down
Loading

0 comments on commit 66d4672

Please sign in to comment.