Skip to content

Commit

Permalink
fix(integration): adopt changes after functional refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
MariamKhalatova committed Feb 22, 2024
1 parent c9d3122 commit 8d4b14e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 44 deletions.
14 changes: 13 additions & 1 deletion src/__tests__/integration/helpers/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,21 @@ import {promisify} from 'node:util';

export const execPromise = promisify(exec);

const stripAnsi = (text: string) => {
const pattern = [
'[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))',
].join('|');

const ansiRegex = new RegExp(pattern, 'g');

return text.replaceAll(ansiRegex, '');
};

export const getJSONFromText = (text: string) => {
const textWithoutAnsi = stripAnsi(text);
const jsonRegex = /\{[^]*\}/;
const jsonMatch = text.match(jsonRegex);
const jsonMatch = textWithoutAnsi.match(jsonRegex);

if (jsonMatch) {
const jsonString = jsonMatch[0];
Expand Down
64 changes: 29 additions & 35 deletions src/__tests__/integration/scenarios/sci-e.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {openYamlFileAsObject, saveYamlFileAs} from '../../../util/yaml';
import {Impl, hasInputs} from '../../../types/impl';
import {Manifest} from '../../../types/manifest';
import {
npmInstallPackage,
npmUninstallPackage,
Expand All @@ -9,61 +9,55 @@ import {sciEInputData} from '../test-data/sci-e';

describe('integration/sci-e', () => {
const modelName = 'sci-e';
const absoluteImplPath = `${__dirname}/../impls/sci-e.yaml`;
const relativeImplPath = 'src/__tests__/integration/impls/sci-e.yaml';
const absoluteImplPath = `${__dirname}/../manifest/sci-e.yaml`;
const relativeImplPath = 'src/__tests__/integration/manifest/sci-e.yaml';
const implTemplatePath = `${__dirname}/../templates/integration.yaml`;

beforeAll(() => {
return npmInstallPackage('@grnsft/if-models');
}, 15000);

it('output creation without ompl path.', async () => {
const file = await openYamlFileAsObject<Impl>(implTemplatePath);
const file = await openYamlFileAsObject<Manifest>(implTemplatePath);

file.initialize.models[0].name = modelName;
file.initialize.models[0].path = '@grnsft/if-models';
file.initialize.models[0].model = 'SciEModel';
file.graph.children.child.pipeline = [modelName];
file.graph.children.child.config = {};
file.graph.children.child.config[modelName] = {};
file.initialize.plugins[modelName] = {
method: 'SciE',
path: '@grnsft/if-models',
};

if (hasInputs(file.graph.children.child)) {
file.graph.children.child.inputs = sciEInputData['success-3-params'];
}
file.tree.children.child.pipeline = [modelName];
file.tree.children.child.config = {};
file.tree.children.child.config[modelName] = {};
file.tree.children.child.inputs = sciEInputData['success-3-params'];

await saveYamlFileAs(file, absoluteImplPath); // save yaml uses absolute path
const response = (
await execPromise(`npm run impact-engine -- --impl ${relativeImplPath}`)
await execPromise(`npm run if -- --manifest ${relativeImplPath}`)
).stdout; // exec promise uses relative path

const finalOmplParsed = getJSONFromText(response);

// assertions
if (
hasInputs(finalOmplParsed.graph.children['child']) &&
hasInputs(file.graph.children['child'])
) {
const path = finalOmplParsed.graph.children['child'].outputs![0];
const impPath = file.graph.children['child'].inputs[0];
const path = finalOmplParsed.tree.children['child'].outputs![0];
const impPath = file.tree.children['child'].inputs[0];

// assert timestamp
expect(
finalOmplParsed.graph.children['child'].inputs[0].timestamp
).toEqual(file.graph.children['child'].inputs[0].timestamp);
// assert timestamp
expect(finalOmplParsed.tree.children['child'].inputs[0].timestamp).toEqual(
file.tree.children['child'].inputs[0].timestamp
);

// assert duration
expect(
finalOmplParsed.graph.children['child'].inputs[0].duration
).toEqual(file.graph.children['child'].inputs[0].duration);
// assert duration
expect(finalOmplParsed.tree.children['child'].inputs[0].duration).toEqual(
file.tree.children['child'].inputs[0].duration
);

// assert total energy
const sum =
impPath['energy-cpu'] +
impPath['energy-memory'] +
impPath['energy-network'];
// assert total energy
const sum =
impPath['cpu/energy'] +
impPath['memory/energy'] +
impPath['network/energy'];

expect(path.energy).toEqual(sum);
}
expect(path.energy).toEqual(sum);
});

afterAll(() => {
Expand Down
10 changes: 5 additions & 5 deletions src/__tests__/integration/templates/integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ name: integration-template
description:
tags:
initialize:
models:
- name: ${name}
model: ${model}
path: ${path}
graph:
plugins:
'sci-e':
model: ${model}
path: ${path}
tree:
children:
child:
pipeline:
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/integration/test-data/sci-e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ export const sciEInputData = {
{
timestamp: '2023-08-06T00:00',
duration: 3600,
'energy-cpu': 10,
'energy-memory': 100,
'energy-network': 100,
'cpu/energy': 10,
'memory/energy': 100,
'network/energy': 100,
},
],
};

0 comments on commit 8d4b14e

Please sign in to comment.