1 |
|
2 |
|
3 | import chai from 'chai'
|
4 | import * as api from '../src/api.js'
|
5 | import grlib from 'graphlib'
|
6 | import fs from 'fs'
|
7 | import _ from 'lodash'
|
8 |
|
9 | var expect = chai.expect
|
10 |
|
11 | describe('Graphlib to KGraph conversion', () => {
|
12 | it('can convert a node into the KGraph format', () => {
|
13 | var knode = api.convertNode({v: 'a', value: {inputPorts: {b: 'c'}}})
|
14 | expect(knode.id).to.equal('a')
|
15 | expect(knode.labels).to.have.length(1)
|
16 | expect(knode.labels[0].text).to.equal('a')
|
17 | expect(knode.ports).to.have.length(1)
|
18 | expect(knode.ports[0].id).to.equal('a_b_in')
|
19 | })
|
20 |
|
21 | it('can convert graphlib graphs', () => {
|
22 | var g = grlib.json.read(JSON.parse(fs.readFileSync('test/fixtures/fac.json')))
|
23 | var newGraph = api.convertGraph(g)
|
24 | var facEdges = newGraph.children[2].edges
|
25 | expect(facEdges[0].sourcePort).to.equal('fac_n_in')
|
26 | expect(facEdges[2].sourcePort).to.equal('fac_n_in')
|
27 | expect(facEdges[4].targetPort).to.equal('fac_fac_out')
|
28 | expect(facEdges[facEdges.length - 1].targetPort).to.equal('fac_fac_out')
|
29 | })
|
30 |
|
31 | it('can store meta information inside the KGraph', () => {
|
32 | var g = grlib.json.read(JSON.parse(fs.readFileSync('test/fixtures/fac.json')))
|
33 | var newGraph = api.convertGraph(g)
|
34 | var facEdges = newGraph.children[2].edges
|
35 | expect(facEdges[0].meta.sourceType).to.equal('number')
|
36 | expect(facEdges[2].meta.targetType).to.equal('generic')
|
37 | expect(newGraph.edges[0].meta.sourceType).to.equal('string')
|
38 | expect(newGraph.children[2].ports).to.have.length(2)
|
39 | expect(newGraph.children[2].ports[0].meta.type).to.equal('number')
|
40 | })
|
41 |
|
42 | it('keeps edges for nodes without parents', () => {
|
43 | var g = grlib.json.read(JSON.parse(fs.readFileSync('test/fixtures/fac2.json')))
|
44 | var newGraph = api.convertGraph(g)
|
45 | expect(newGraph.edges).to.have.length(4)
|
46 | })
|
47 |
|
48 | it('supports edges that go directly through compound nodes', () => {
|
49 | var g = grlib.json.read(JSON.parse(fs.readFileSync('test/fixtures/through-compound.json')))
|
50 | var newGraph = api.convertGraph(g)
|
51 | expect(newGraph.edges).to.be.empty
|
52 | expect(newGraph.children[0].edges[0].sourcePort).to.equal('c_0_i_in')
|
53 | expect(newGraph.children[0].edges[0].targetPort).to.equal('c_0_out_out')
|
54 | })
|
55 |
|
56 | it('supports styled nodes and edges', () => {
|
57 | var g = grlib.json.read(JSON.parse(fs.readFileSync('test/fixtures/styled.json')))
|
58 | var newGraph = api.convertGraph(g)
|
59 | expect(newGraph.children.some((node) => _.get(node, 'meta.style.color') === '#ff0000')).to.be.true
|
60 | expect(_.get(newGraph.edges[0], 'meta.style.color')).to.equal('#0000ff')
|
61 | })
|
62 | })
|