|
| 1 | +const expect = require("expect"); |
| 2 | +const TreeServer = require("../server"); |
| 3 | +const DeviceTree = require("../").DeviceTree; |
| 4 | + |
| 5 | + |
| 6 | +const LOCALHOST = "127.0.0.1"; |
| 7 | +const PORT = 9008; |
| 8 | + |
| 9 | + |
| 10 | +const init = function(_src,_tgt) { |
| 11 | + const targets = _tgt === undefined ? [ "tgt1", "tgt2", "tgt3" ] : _tgt; |
| 12 | + const sources = _src === undefined ? [ "src1", "src2", "src3" ] : _src; |
| 13 | + const labels = function(endpoints) { |
| 14 | + let labels = []; |
| 15 | + for (let i = 0; i < endpoints.length; i++) { |
| 16 | + let endpoint = endpoints[i]; |
| 17 | + let l = { identifier: `Label-${i}` }; |
| 18 | + if (endpoint) { |
| 19 | + l.value = endpoint; |
| 20 | + } |
| 21 | + labels.push(l); |
| 22 | + } |
| 23 | + return labels; |
| 24 | + }; |
| 25 | + |
| 26 | + const buildConnections = function(s, t) { |
| 27 | + let connections = []; |
| 28 | + for (let i = 0; i < t.length; i++) { |
| 29 | + connections.push({target: `${i}`}); |
| 30 | + } |
| 31 | + return connections; |
| 32 | + }; |
| 33 | + |
| 34 | + return [ |
| 35 | + { |
| 36 | + // path "0" |
| 37 | + identifier: "scoreMaster", |
| 38 | + children: [ |
| 39 | + { |
| 40 | + // path "0.0" |
| 41 | + identifier: "identity", |
| 42 | + children: [ |
| 43 | + {identifier: "product", value: "S-CORE Master"}, |
| 44 | + {identifier: "company", value: "EVS"}, |
| 45 | + {identifier: "version", value: "1.2.0"}, |
| 46 | + {identifier: "author", value: "[email protected]"} |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + // path "0.1" |
| 51 | + identifier: "router", |
| 52 | + children: [ |
| 53 | + { |
| 54 | + // path 0.1.0 |
| 55 | + identifier: "matrix", |
| 56 | + type: "oneToN", |
| 57 | + mode: "linear", |
| 58 | + targetCount: targets.length, |
| 59 | + sourceCount: sources.length, |
| 60 | + connections: buildConnections(sources, targets), |
| 61 | + labels: ["0.1.1000"] |
| 62 | + }, |
| 63 | + { |
| 64 | + identifier: "labels", |
| 65 | + // path "0.1.1000" |
| 66 | + number: 1000, |
| 67 | + children: [ |
| 68 | + { |
| 69 | + identifier: "targets", |
| 70 | + // Must be 1 |
| 71 | + number: 1, |
| 72 | + children: labels(targets) |
| 73 | + }, |
| 74 | + { |
| 75 | + identifier: "sources", |
| 76 | + // Must be 2 |
| 77 | + number: 2, |
| 78 | + children: labels(sources) |
| 79 | + } |
| 80 | + ] |
| 81 | + } |
| 82 | + ] |
| 83 | + } |
| 84 | + ] |
| 85 | + } |
| 86 | + ]; |
| 87 | + |
| 88 | +} |
| 89 | +describe("server", function() { |
| 90 | + |
| 91 | + describe("JSONtoTree", function() { |
| 92 | + let jsonTree; |
| 93 | + before(function() { |
| 94 | + jsonTree = init(); |
| 95 | + }); |
| 96 | + it("should generate an ember tree from json", function() { |
| 97 | + const root = TreeServer.JSONtoTree(jsonTree); |
| 98 | + expect(root).toBeDefined(); |
| 99 | + expect(root.elements).toBeDefined(); |
| 100 | + expect(root.elements.length).toBe(1); |
| 101 | + console.log("root", root.elements[0].contents); |
| 102 | + expect(root.elements[0].contents.identifier).toBe("scoreMaster"); |
| 103 | + expect(root.elements[0].children.length).toBe(2); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe("Server - Client communication", function() { |
| 108 | + let server,client; |
| 109 | + before(function() { |
| 110 | + jsonTree = init(); |
| 111 | + const root = TreeServer.JSONtoTree(jsonTree); |
| 112 | + server = new TreeServer(LOCALHOST, PORT, root); |
| 113 | + //server._debug = true; |
| 114 | + return server.listen().then(() => { |
| 115 | + console.log("server listening"); |
| 116 | + }); |
| 117 | + }); |
| 118 | + after(function() { |
| 119 | + client.disconnect(); |
| 120 | + server.close(); |
| 121 | + }) |
| 122 | + it("should receive and decode the full tree", function () { |
| 123 | + client = new DeviceTree(LOCALHOST, PORT); |
| 124 | + //client._debug = true; |
| 125 | + return Promise.resolve() |
| 126 | + .then(() => client.connect()) |
| 127 | + .then(() => { |
| 128 | + console.log("client connected"); |
| 129 | + return client.getDirectory() |
| 130 | + }) |
| 131 | + .then(() => { |
| 132 | + expect(client.root).toBeDefined(); |
| 133 | + expect(client.root.elements).toBeDefined(); |
| 134 | + expect(client.root.elements.length).toBe(1); |
| 135 | + expect(client.root.elements[0].contents.identifier).toBe("scoreMaster"); |
| 136 | + return client.getDirectory(client.root.elements[0]); |
| 137 | + }) |
| 138 | + .then(() => { |
| 139 | + expect(client.root.elements[0].children.length).toBe(2); |
| 140 | + return client.getDirectory(client.root.elements[0].children[0]); |
| 141 | + }) |
| 142 | + .then(() => { |
| 143 | + expect(client.root.elements[0].children[0].children.length).toBe(4); |
| 144 | + expect(client.root.elements[0].children[0].children[3].contents.identifier).toBe("author"); |
| 145 | + }); |
| 146 | + }); |
| 147 | + }); |
| 148 | +}); |
0 commit comments