mihomo-kit/tools/test-graph.js
2026-10-29 20:59:47 +03:00

74 lines
3.5 KiB
JavaScript

#!/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);
}
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: { now: "FOREIGN" }, FOREIGN: { now: "nl-1" }, UPLINK: { 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"] },
];
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.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.unmatched == 1 && L.unmatchedRules["без правила"] == 1, "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);