-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
|
@@ -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": { | ||
|
@@ -55,4 +57,4 @@ | |
}, | ||
"description": "Drop on Cosmos integration test", | ||
"repository": "[email protected]:hadronlabs-org/lionco-contracts.git" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters