Skip to content

Commit

Permalink
feat: gv stuff in integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ratik committed Jul 5, 2024
1 parent eaf6b67 commit 156aaf8
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 1 deletion.
4 changes: 3 additions & 1 deletion integration_tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"test:validator-set": "vitest --run validator-set.test.ts --bail 1",
"test:distribution": "vitest --run distribution.test.ts --bail 1",
"test:auto-withdrawer": "vitest --run auto-withdrawer.test.ts --bail 1",
"test:gv": "vitest --run gv.test.ts --bail 1",
"debug": "vitest --inspect-brk --bail 1 --threads false --run core.test.ts",
"watch": "vitest",
"build-ts-client": "ts-node ./src/rebuild-client.ts",
Expand All @@ -39,6 +40,7 @@
"@neutron-org/contracts2ts": "^1.4.4",
"@neutron-org/cosmopark": "^1.6.3",
"bech32": "^1.1.4",
"dotparser": "^1.1.1",
"yaml": "^2.4.1"
},
"devDependencies": {
Expand All @@ -55,4 +57,4 @@
},
"description": "Drop on Cosmos integration test",
"repository": "[email protected]:hadronlabs-org/lionco-contracts.git"
}
}
108 changes: 108 additions & 0 deletions integration_tests/src/testcases/gv.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import path from 'path';
import { describe, expect, test } from 'vitest';
import fs from 'fs';
import dotparser, { Graph } from 'dotparser';

type MyNode = {
id: string;
label: string;
shape: string;
outs: string[];
};

class ParsedGraph {
nodes: Record<string, MyNode> = {};
constructor(content: string) {
this.nodes = parseAST(dotparser(content));
}
hasPath(checkNodes: string[]) {
if (!checkNodes || !checkNodes.length) {
return false;
}
const node = this.nodes[checkNodes.shift()];
if (!node) {
return false;
}
if (node.outs.length === 0 || !node.outs.includes(checkNodes[0])) {
return false;
}
if (checkNodes.length === 1) {
return true;
}
return this.hasPath(checkNodes);
}
}

const parseAST = (tree: Graph[]): Record<string, MyNode> => {
const out: Record<string, MyNode> = {};
if (!tree || !tree.length) {
throw new Error('No tree found');
}
const graph = tree[0];
for (const item of graph.children) {
if (item.type === 'node_stmt') {
out[item.node_id.id] = {
id: item.node_id.id.toString(),
label: item.attr_list
.find((attr) => attr.id === 'label')
?.eq.toString(),
shape:
item.attr_list.find((attr) => attr.id === 'shape')?.eq.toString() ||
'ellipse',
outs: [],
};
}
}

for (const item of graph.children) {
if (item.type === 'edge_stmt') {
const firstId = item.edge_list[0].id.toString();
const secondId = item.edge_list[1].id.toString();
out[firstId].outs.push(secondId);
}
}
return out;
};

describe('graphviz', () => {
test('init', () => {
const gvFile = path.resolve(__dirname, '../../../graph.gv');
const gvContent = fs.readFileSync(gvFile, 'utf-8');
const parsedTree = new ParsedGraph(gvContent);
expect(parsedTree).toBeDefined();
expect(parsedTree.hasPath(['a', 'b'])).toBe(false);
expect(
parsedTree.hasPath([
'K001',
'K000',
'K002',
'K003',
'K005',
'K007',
'K045',
'K009',
'K010',
'K011',
'K012',
'K004',
]),
).toBe(true);
expect(
parsedTree.hasPath([
'K001',
'K000',
'K002',
'K003',
'K005',
'K007',
'K045',
'K009',
'K010',
'K011',
'K012',
'K004',
'K001',
]),
).toBe(false);
});
});
5 changes: 5 additions & 0 deletions integration_tests/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1755,6 +1755,11 @@ doctrine@^3.0.0:
dependencies:
esutils "^2.0.2"

dotparser@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/dotparser/-/dotparser-1.1.1.tgz#c474942f329638b44ddb3dff27e9a6385dbab2f9"
integrity sha512-8ojhUts0HbLnXJgjTiJOddwVVBUk6hg4SJ5kGiuhzgK/f+y79TiWvICwx1oCWlVbBC8YI3nEaIQg9fjGYbGBXw==

eastasianwidth@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/eastasianwidth/-/eastasianwidth-0.2.0.tgz#696ce2ec0aa0e6ea93a397ffcf24aa7840c827cb"
Expand Down

0 comments on commit 156aaf8

Please sign in to comment.