#!/usr/bin/env node // luci/graph.js on the rendered profiles (tests/expected/*.yaml): every name resolves, // the layout has no overlaps and runs left to right, and live() follows a connection // through the groups' choice and the dialer-proxy chain. const fs = require("fs"); const path = require("path"); const S = require("../luci/servers.js"); const G = require("../luci/graph.js"); let fail = 0; const check = (name, ok, msg) => { if (ok) return; fail = 1; console.log("FAIL " + name + ": " + msg); }; const dir = path.join(__dirname, "..", "tests", "expected"); for (const f of fs.readdirSync(dir).filter((f) => f.endsWith(".yaml"))) { const cfg = S.yamlLoad(fs.readFileSync(path.join(dir, f), "utf8")); const m = G.model(cfg); check(f, !m.warnings.length, m.warnings.join("; ")); check(f, m.list.filter((n) => n.kind == "rule").length > 0, "no rule nodes"); const lay = G.layout(m); const cols = {}; for (const n of m.list) { const b = lay.nodes[n.id]; check(f, b && [b.x, b.y, b.w, b.h].every(Number.isFinite), "no position for " + n.id); (cols[b.x] = cols[b.x] || []).push(b); } for (const x in cols) { const c = cols[x].sort((a, b) => a.y - b.y); for (let i = 1; i < c.length; i++) check(f, c[i].y >= c[i - 1].y + c[i - 1].h - 0.01, "nodes overlap in column x=" + x); } const lastX = Math.max(...m.list.map((n) => lay.nodes[n.id].x)); for (const n of m.list) { const b = lay.nodes[n.id]; if (n.exit) check(f, b.x == lastX, n.id + " is an exit but not in the last column"); if (b.aux) check(f, lay.lane != null && b.y > lay.lane, n.id + " is not under the lane"); // every proxy and subscription goes somewhere: through a dialer or directly if ((n.kind == "proxy" && !n.exit) || n.kind == "provider") check(f, m.dialerOf[n.id], n.id + " has no way out"); } if (cfg["proxy-groups"].some((g) => g.name == "BOOT-AUTO")) check(f, lay.nodes["g:BOOT-AUTO"].aux, "BOOT-AUTO is not in the lane"); for (const le of lay.edges) if (le.pts) for (let i = 1; i < le.pts.length; i++) check(f, le.pts[i].x > le.pts[i - 1].x, "edge runs right to left: " + le.e.from + " -> " + le.e.to); if (!fail) console.log("ok graph " + f + " (" + m.list.length + " nodes, " + lay.ranks + " columns)"); } // live: a connection by MATCH through MAIN -> FOREIGN -> a node of "example", which dials // through UPLINK -> CSQTT (router-csqtt: subscription nodes with dialer-proxy UPLINK) { const cfg = S.yamlLoad(fs.readFileSync(path.join(dir, "router-csqtt.yaml"), "utf8")); const m = G.model(cfg); // /proxies: groups and the config's proxies; the subscriptions' nodes only in /providers/proxies const proxies = { MAIN: { type: "Selector", now: "FOREIGN", all: ["FOREIGN", "DIRECT"] }, FOREIGN: { type: "URLTest", now: "nl-1", all: ["nl-1", "de-1"], fixed: "nl-1" }, UPLINK: { type: "Selector", now: "CSQTT" }, CSQTT: { history: [] }, }; const providers = { example: { proxies: [{ name: "nl-1", alive: true, history: [{ delay: 42 }] }, { name: "de-1", alive: false }] }, example2: { proxies: [{ name: "nl-1", alive: true }] }, default: { proxies: [{ name: "MAIN" }] }, }; const conns = [ { id: "c1", rule: "Match", rulePayload: "", chains: ["nl-1", "FOREIGN", "MAIN"], upload: 10, download: 20 }, { id: "c2", rule: "", rulePayload: "", chains: ["DIRECT"] }, { id: "c3", rule: "RuleSet", rulePayload: "ru-inside", chains: ["DIRECT"] }, ]; const L = G.live(m, proxies, conns, null, providers); const match = m.ruleIndex["match|"]; const want = [match, "g:MAIN", "g:FOREIGN", "v:example", "g:UPLINK", "p:CSQTT"]; const got = Object.keys(L.nodes); check("live", want.every((id) => L.nodes[id] && L.nodes[id].n == 1), "path " + JSON.stringify(got) + ", want " + JSON.stringify(want)); check("live", Object.keys(L.edges).length == want.length - 1, "edges on the path: " + Object.keys(L.edges).length); check("live", L.nodes["b:DIRECT"] && L.nodes["b:DIRECT"].byRule == 1, "rule straight to DIRECT counted at DIRECT"); check("live", L.groups["g:FOREIGN"].fixed == "nl-1", "fixed of FOREIGN"); const mem = L.members("g:FOREIGN"); check("live", mem.length == 2 && mem[0].delay == 42 && mem[1].alive === false, "members of FOREIGN: " + JSON.stringify(mem)); check("live", L.provider["v:example"] && L.provider["v:example"].alive == 1 && L.provider["v:example"].n == 2, "alive nodes of example"); check("live", L.nowProv["g:FOREIGN"] == "v:example", "FOREIGN's choice comes from example: " + L.nowProv["g:FOREIGN"]); check("live", L.internal == 1 && L.unmatched == 0, "internal " + L.internal + ", unmatched " + JSON.stringify(L.unmatchedRules)); check("live", L.delay["g:FOREIGN"] == 42, "delay of FOREIGN's choice"); if (!fail) console.log("ok graph live"); } process.exit(fail);