Skip to content

Commit

Permalink
unit tests pass again.
Browse files Browse the repository at this point in the history
  • Loading branch information
bhouston committed Jul 22, 2023
1 parent 2e34081 commit 9282617
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 66 deletions.
4 changes: 3 additions & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export default {
preset: 'ts-jest',
testEnvironment: 'node',
'ts-jest': {
useESM: true
},
resolver: 'ts-jest-resolver',
testMatch: ['<rootDir>/packages/*/src/**/*.test.ts'],
moduleNameMapper: { '^uuid$': 'uuid' } // see: https://stackoverflow.com/questions/73203367/jest-syntaxerror-unexpected-token-export-with-uuid-library
Expand Down
29 changes: 15 additions & 14 deletions packages/core/src/Graphs/IO/readGraphFromJSON.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { Logger } from '../../Diagnostics/Logger.js';
import { getCoreRegistry } from '../../Profiles/Core/registerCoreProfile.js';
import { ManualLifecycleEventEmitter } from '../../Profiles/Core/Abstractions/Drivers/ManualLifecycleEventEmitter.js';
import { registerCoreProfile } from '../../Profiles/Core/registerCoreProfile.js';
import { readGraphFromJSON } from './readGraphFromJSON.js';
Logger.onWarn.clear();

describe('readGraphFromJSON', () => {
const registry = getCoreRegistry();
const registry = registerCoreProfile({
values: {},
nodes: {},
dependencies: {
ILogger: new Logger(),
ILifecycleEventEmitter: new ManualLifecycleEventEmitter()
}
});

it('throws if node ids are not unique', () => {
const json = {
variables: [],
Expand All @@ -20,9 +29,7 @@ describe('readGraphFromJSON', () => {
}
]
};
expect(() =>
readGraphFromJSON({ graphJson: json, ...registry, dependencies: {} })
).toThrow();
expect(() => readGraphFromJSON({ graphJson: json, registry })).toThrow();
});

it("throws if input keys don't match known sockets", () => {
Expand All @@ -39,9 +46,7 @@ describe('readGraphFromJSON', () => {
}
]
};
expect(() =>
readGraphFromJSON({ graphJson: json, ...registry, dependencies: {} })
).toThrow();
expect(() => readGraphFromJSON({ graphJson: json, registry })).toThrow();
});

it('throws if input points to non-existent node', () => {
Expand All @@ -65,9 +70,7 @@ describe('readGraphFromJSON', () => {
}
]
};
expect(() =>
readGraphFromJSON({ graphJson: json, ...registry, dependencies: {} })
).toThrow();
expect(() => readGraphFromJSON({ graphJson: json, registry })).toThrow();
});

it('throws if input points to non-existent socket', () => {
Expand All @@ -91,8 +94,6 @@ describe('readGraphFromJSON', () => {
}
]
};
expect(() =>
readGraphFromJSON({ graphJson: json, ...registry, dependencies: {} })
).toThrow();
expect(() => readGraphFromJSON({ graphJson: json, registry })).toThrow();
});
});
6 changes: 1 addition & 5 deletions packages/core/src/Nodes/Validation/validateNodeRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import { IRegistry } from '../../Registry.js';
const nodeTypeNameRegex = /^\w+(\/\w+)*$/;
const socketNameRegex = /^\w+$/;

export function validateNodeRegistry({
registry
}: {
registry: IRegistry;
}): string[] {
export function validateNodeRegistry(registry: IRegistry): string[] {
const errorList: string[] = [];
// const graph = new Graph(registry);
const graph = makeGraphApi({
Expand Down
81 changes: 38 additions & 43 deletions packages/core/src/Profiles/Core/readCoreGraphs.test.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,55 @@
import * as customEventJson from '../../../../../graphs/core/events/CustomEvents.json';
import * as lifecycleJson from '../../../../../graphs/core/events/Lifecycle.json';
import * as branchJson from '../../../../../graphs/core/flow/Branch.json';
import * as flipFlopJson from '../../../../../graphs/core/flow/FlipFlop.json';
import * as forLoopJson from '../../../../../graphs/core/flow/ForLoop.json';
import * as performanceTestJson from '../../../../../graphs/core/flow/PerformanceTest.json';
import * as sequenceJson from '../../../../../graphs/core/flow/Sequence.json';
import * as helloWorldJson from '../../../../../graphs/core/HelloWorld.json';
import * as polynomialJson from '../../../../../graphs/core/logic/Polynomial.json';
import * as delayJson from '../../../../../graphs/core/time/Delay.json';
import * as frameCounterJson from '../../../../../graphs/core/variables/FrameCounter.json';
import * as initialValueJson from '../../../../../graphs/core/variables/InitialValue.json';
import * as setGetJson from '../../../../../graphs/core/variables/SetGet.json';
import fs from 'fs';
import path from 'path';

import { Logger } from '../../Diagnostics/Logger.js';
import { GraphInstance } from '../../Graphs/Graph.js';
import { GraphJSON } from '../../Graphs/IO/GraphJSON.js';
import { readGraphFromJSON } from '../../Graphs/IO/readGraphFromJSON.js';
import { validateGraphAcyclic } from '../../Graphs/Validation/validateGraphAcyclic.js';
import { validateGraphLinks } from '../../Graphs/Validation/validateGraphLinks.js';
import {
getCoreNodeDefinitions,
getCoreValueMap
} from './registerCoreProfile.js';
import { memo } from '../../memo.js';
import { getCoreNodesMap, getCoreValuesMap } from './registerCoreProfile.js';

const valueTypes = getCoreValueMap();
const nodeDefinitions = getCoreNodeDefinitions(valueTypes);
const valueTypes = getCoreValuesMap();
const nodeDefinitions = getCoreNodesMap();

Logger.onWarn.clear();

const exampleMap: { [key: string]: any } = {
branchJson,
delayJson,
flipFlopJson,
forLoopJson,
sequenceJson,
helloWorldJson,
setGetJson,
polynomialJson,
customEventJson,
lifecycleJson,
frameCounterJson,
initialValueJson,
performanceTestJson
};
const exampleMap = memo<Record<string, GraphJSON>>(() => {
const result = {} as Record<string, GraphJSON>;
// read all *.jsons in the folder '../../graphs/core':
const dir = path.join(__dirname, '../../../../../graphs/core');
const files = fs.readdirSync(dir);
for (const file of files) {
if (file.endsWith('.json')) {
// get filename
const filename = path.parse(file).name;
// read the file as a string and then parse it as JSON
const data = fs.readFileSync(path.join(dir, file), {
encoding: 'utf-8'
});
const json = JSON.parse(data);
// add the parsed JSON to the map
result[filename] = json;
}
}
return result;
})();

for (const key in exampleMap) {
describe(`${key}`, () => {
const exampleJson = exampleMap[key] as GraphJSON;
describe(`json core graphs`, () => {
for (const key in exampleMap) {
const exampleJson = exampleMap[key];

let parsedGraphJson: GraphInstance | undefined;
test('parse json to graph', () => {
test(`${key}`, () => {
expect(() => {
parsedGraphJson = readGraphFromJSON({
graphJson: exampleJson,
nodes: nodeDefinitions,
values: valueTypes,
dependencies: {}
registry: {
nodes: nodeDefinitions,
values: valueTypes,
dependencies: {}
}
});
}).not.toThrow();
// await fs.writeFile('./examples/test.json', JSON.stringify(writeGraphToJSON(graph), null, ' '), { encoding: 'utf-8' });
Expand All @@ -65,5 +60,5 @@ for (const key in exampleMap) {
expect(parsedGraphJson).toBeDefined();
}
});
});
}
}
});
8 changes: 6 additions & 2 deletions packages/core/src/Profiles/Core/registerCoreProfile.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { validateNodeRegistry } from '../../Nodes/Validation/validateNodeRegistry.js';
import { validateValueRegistry } from '../../Values/Validation/validateValueRegistry.js';
import { getCoreRegistry } from './registerCoreProfile.js';
import { registerCoreProfile } from './registerCoreProfile.js';

describe('core profile', () => {
const registry = getCoreRegistry();
const registry = registerCoreProfile({
values: {},
nodes: {},
dependencies: {}
});

test('validate node registry', () => {
expect(validateNodeRegistry(registry)).toHaveLength(0);
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/memo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@ export function memo<T>(func: () => T): () => T {
return cache;
};
}

export function asyncMemo<T>(func: () => Promise<T>): () => Promise<T> {
let cache: T | undefined;
return async (): Promise<T> => {
if (cache === undefined) {
cache = await func();
}
return cache;
};
}
2 changes: 1 addition & 1 deletion packages/core/src/validateRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function validateRegistry(registry: IRegistry): string[] {
const errorList: string[] = [];
errorList.push(
...validateValueRegistry(registry.values),
...validateNodeRegistry({ registry })
...validateNodeRegistry(registry)
);
return errorList;
}

0 comments on commit 9282617

Please sign in to comment.