Files
TJWaterAgent/node-tests/cli/tjwaterCli.node.mjs
T

382 lines
18 KiB
JavaScript

import { strict as assert } from "node:assert";
import { spawn } from "node:child_process";
import { mkdtemp, rm, writeFile } from "node:fs/promises";
import { createServer } from "node:http";
import { tmpdir } from "node:os";
import { test } from "node:test";
import { fileURLToPath } from "node:url";
import { dirname, join, resolve } from "node:path";
const cliPath = resolve(dirname(fileURLToPath(import.meta.url)), "../../cli/tjwater-cli");
const visibleCommandPaths = [
"analysis age",
"analysis burst",
"analysis burst-detection detect",
"analysis burst-detection schemes get",
"analysis burst-detection schemes list",
"analysis contaminant",
"analysis flushing",
"analysis leakage identify",
"analysis leakage schemes get",
"analysis leakage schemes list",
"analysis sensor-placement kmeans",
"analysis valve",
"component option get",
"component option schema",
"data scada get",
"data scada list",
"data scheme get",
"data scheme list",
"data scheme schema",
"data timeseries composite",
"data timeseries composite pipeline-health",
"data timeseries realtime links",
"data timeseries realtime nodes",
"data timeseries realtime simulation-by-id-time",
"data timeseries realtime simulation-by-time-property",
"data timeseries scada query",
"data timeseries scheme links",
"data timeseries scheme node-field",
"data timeseries scheme simulation",
"network get-all-pipes-properties",
"network get-all-pumps-properties",
"network get-all-reservoirs-properties",
"network get-all-tanks-properties",
"network get-all-valves-properties",
"network get-junction-properties",
"network get-pipe-properties",
"network get-pump-properties",
"network get-reservoir-properties",
"network get-tank-properties",
"network get-valve-properties",
"simulation run",
];
const hiddenCommandPaths = [
"analysis burst-location locate",
"analysis burst-location schemes get",
"analysis burst-location schemes list",
"analysis risk network",
"analysis risk pipe-history",
"analysis risk pipe-now",
];
function runCommand(command, args, input, options = {}) {
return new Promise((resolveRun, reject) => {
const child = spawn(command, args, {
cwd: options.cwd,
stdio: ["pipe", "pipe", "pipe"],
});
let stdout = "";
let stderr = "";
child.stdout.on("data", (chunk) => {
stdout += chunk.toString("utf8");
});
child.stderr.on("data", (chunk) => {
stderr += chunk.toString("utf8");
});
child.on("error", reject);
child.on("close", (exitCode) => resolveRun({ exitCode, stdout, stderr }));
if (input !== undefined) child.stdin.end(JSON.stringify(input));
else child.stdin.end();
});
}
function runCli(args, input) {
return runCommand(cliPath, args, input);
}
function parseJsonResult(result) {
return JSON.parse(result.stdout);
}
async function startJsonServer(responseData) {
const seen = [];
const server = createServer(async (req, res) => {
const chunks = [];
for await (const chunk of req) chunks.push(Buffer.from(chunk));
const text = Buffer.concat(chunks).toString("utf8");
seen.push({
body: text ? JSON.parse(text) : null,
headers: req.headers,
method: req.method,
url: req.url,
});
res.setHeader("content-type", "application/json");
res.end(JSON.stringify(responseData));
});
await new Promise((resolveListen, reject) => {
server.once("error", reject);
server.listen(0, "127.0.0.1", resolveListen);
});
const address = server.address();
return {
seen,
url: `http://127.0.0.1:${address.port}`,
close: () => new Promise((resolveClose) => server.close(resolveClose)),
};
}
function normalizeSeenRequest(request) {
const url = new URL(request.url, "http://127.0.0.1");
const query = {};
for (const key of [...new Set(url.searchParams.keys())].sort()) {
const values = url.searchParams.getAll(key);
query[key] = values.length === 1 ? values[0] : values;
}
return {
body: request.body,
headers: {
authorization: request.headers.authorization,
"x-project-id": request.headers["x-project-id"],
},
method: request.method,
path: url.pathname,
query,
};
}
async function runAgainstServer(name, runner, args, auth, responseData = { accepted: true }) {
const server = await startJsonServer(responseData);
try {
const result = await runner(["--auth-stdin", ...args], { ...auth, server: server.url });
if (!result.stdout.trim()) {
throw new Error(`${name}: CLI produced empty stdout; exit=${result.exitCode}; stderr=${result.stderr}`);
}
return {
exitCode: result.exitCode,
payload: parseJsonResult(result),
requests: server.seen.map(normalizeSeenRequest),
stderr: result.stderr,
};
} finally {
await server.close();
}
}
test("emits structured JSON help compatible with tjwater-cli/v1", async () => {
const result = await runCli(["help", "simulation", "run"]);
assert.equal(result.exitCode, 0, result.stderr);
const payload = JSON.parse(result.stdout);
assert.equal(payload.schema_version, "tjwater-cli/v1");
assert.equal(payload.command, "simulation run");
assert.equal(payload.usage, "tjwater-cli simulation run --start-time <START_TIME> --duration <DURATION>");
});
test("discovers every visible command and keeps internal commands hidden", async () => {
const rootResult = await runCli(["help"]);
assert.equal(rootResult.exitCode, 0, rootResult.stderr);
assert.deepEqual(
parseJsonResult(rootResult).commands.map(({ command }) => command),
["analysis", "component", "data", "network", "simulation"],
);
for (const command of visibleCommandPaths) {
const result = await runCli(["help", ...command.split(" ")]);
assert.equal(result.exitCode, 0, `${command}: ${result.stderr}`);
const payload = parseJsonResult(result);
assert.equal(payload.ok, true, command);
assert.equal(payload.command, command, command);
assert.equal(payload.schema_version, "tjwater-cli/v1", command);
assert.ok(payload.usage, `${command}: missing usage`);
assert.ok(payload.examples.length > 0, `${command}: missing examples`);
}
for (const command of hiddenCommandPaths) {
const result = await runCli(["help", ...command.split(" ")]);
assert.equal(result.exitCode, 0, `${command}: ${result.stderr}`);
const payload = parseJsonResult(result);
assert.equal(payload.ok, false, command);
assert.equal(payload.error.code, "COMMAND_NOT_FOUND", command);
}
});
test("sends auth headers and simulation body through the backend API contract", async () => {
const server = await startJsonServer({ accepted: true });
try {
const result = await runCli(
[
"--auth-stdin",
"simulation",
"run",
"--start-time",
"2025-01-02T03:00:00+08:00",
"--duration",
"60",
],
{
server: server.url,
access_token: "token-1",
project_id: "project-1",
headers: { "x-extra": "extra" },
},
);
assert.equal(result.exitCode, 0, result.stderr);
assert.equal(server.seen[0].method, "POST");
assert.equal(server.seen[0].url, "/api/v1/simulation-runs");
assert.equal(server.seen[0].headers.authorization, "Bearer token-1");
assert.equal(server.seen[0].headers["x-extra"], "extra");
assert.deepEqual(server.seen[0].body, {
start_time: "2025-01-02T03:00:00+08:00",
duration: 60,
});
const payload = JSON.parse(result.stdout);
assert.equal(payload.ok, true);
assert.match(payload.next_commands[0], /--end-time 2025-01-02T04:00:00\+08:00/);
} finally {
await server.close();
}
});
test("uses project scoped headers for realtime data commands", async () => {
const server = await startJsonServer([{ id: "P1" }]);
try {
const result = await runCli(
[
"--auth-stdin",
"data",
"timeseries",
"realtime",
"links",
"--start-time",
"2025-01-02T03:00:00+08:00",
"--end-time",
"2025-01-02T04:00:00+08:00",
],
{
server: server.url,
accessToken: "token-2",
projectId: "project-1",
},
);
assert.equal(result.exitCode, 0, result.stderr);
assert.equal(server.seen[0].method, "GET");
assert.equal(
server.seen[0].url,
"/api/v1/timeseries/realtime/links?start_time=2025-01-02T03%3A00%3A00%2B08%3A00&end_time=2025-01-02T04%3A00%3A00%2B08%3A00",
);
assert.equal(server.seen[0].headers.authorization, "Bearer token-2");
assert.equal(server.seen[0].headers["x-project-id"], "project-1");
} finally {
await server.close();
}
});
test("maps CLI pipe and junction types to backend link and node types", async () => {
const server = await startJsonServer({ accepted: true });
const auth = { server: server.url, access_token: "token-3", project_id: "project-1" };
const at = "2025-01-02T03:30:00+08:00";
try {
for (const args of [
["data", "timeseries", "realtime", "simulation-by-id-time", "--id", "J1", "--type", "junction", "--time", at],
["data", "timeseries", "realtime", "simulation-by-time-property", "--type", "pipe", "--time", at, "--property", "flow"],
["data", "timeseries", "scheme", "simulation", "--query", "by-id-time", "--id", "P1", "--type", "pipe", "--time", at, "--scheme", "scheme_case"],
]) {
const result = await runCli(["--auth-stdin", ...args], auth);
assert.equal(result.exitCode, 0, result.stderr);
}
const queries = server.seen.map(normalizeSeenRequest).map((request) => request.query.type);
assert.deepEqual(queries, ["node", "link", "link"]);
} finally {
await server.close();
}
});
test("executes every command and key variant against the backend contract", async () => {
const tempDir = await mkdtemp(join(tmpdir(), "tjwater-cli-parity-"));
try {
const burstFile = join(tempDir, "burst.json");
const valveFile = join(tempDir, "valve.json");
const pressureFile = join(tempDir, "pressure.json");
const flowFile = join(tempDir, "flow.json");
await writeFile(burstFile, JSON.stringify([{ id: "B1", size: 12.5 }]));
await writeFile(valveFile, JSON.stringify([{ valve: "V1", opening: 0.5 }]));
await writeFile(pressureFile, JSON.stringify({ burst_pressure: [1.1], normal_pressure: [2.2] }));
await writeFile(flowFile, JSON.stringify({ burst_flow: [3.3], normal_flow: [4.4] }));
const auth = {
access_token: "token",
network: "tjwater",
project_id: "project-1",
username: "alice",
headers: { "x-extra": "extra" },
};
const start = "2025-01-02T03:00:00+08:00";
const end = "2025-01-02T04:00:00+08:00";
const at = "2025-01-02T03:30:00+08:00";
const cases = [
["network get-junction-properties", ["network", "get-junction-properties", "--junction", "J1"]],
["network get-pipe-properties", ["network", "get-pipe-properties", "--pipe", "P1"]],
["network get-all-pipes-properties", ["network", "get-all-pipes-properties"]],
["network get-reservoir-properties", ["network", "get-reservoir-properties", "--reservoir", "R1"]],
["network get-all-reservoirs-properties", ["network", "get-all-reservoirs-properties"]],
["network get-tank-properties", ["network", "get-tank-properties", "--tank", "T1"]],
["network get-all-tanks-properties", ["network", "get-all-tanks-properties"]],
["network get-pump-properties", ["network", "get-pump-properties", "--pump", "PU1"]],
["network get-all-pumps-properties", ["network", "get-all-pumps-properties"]],
["network get-valve-properties", ["network", "get-valve-properties", "--valve", "V1"]],
["network get-all-valves-properties", ["network", "get-all-valves-properties"]],
["component option schema", ["component", "option", "schema", "--kind", "time"]],
["component option get", ["component", "option", "get", "--kind", "pump-energy", "--pump", "P1"]],
["simulation run", ["simulation", "run", "--start-time", start, "--duration", "60"]],
["analysis burst", ["analysis", "burst", "--start-time", start, "--duration", "900", "--burst-file", burstFile, "--scheme", "burst_case"]],
["analysis valve close", ["analysis", "valve", "--mode", "close", "--start-time", start, "--valve", "V1", "--valve", "V2", "--duration", "900", "--scheme", "valve_case"]],
["analysis valve isolation", ["analysis", "valve", "--mode", "isolation", "--element", "E1", "--disabled-valve", "V3"]],
["analysis flushing", ["analysis", "flushing", "--start-time", start, "--valve-setting-file", valveFile, "--drainage-node", "N1", "--flow", "100.5", "--duration", "900", "--scheme", "flush_case"]],
["analysis age", ["analysis", "age", "--start-time", start, "--duration", "900"]],
["analysis contaminant", ["analysis", "contaminant", "--start-time", start, "--duration", "900", "--source-node", "N1", "--concentration", "10.5", "--pattern", "P1", "--scheme", "contam_case"]],
["analysis sensor-placement kmeans", ["analysis", "sensor-placement", "kmeans", "--count", "5", "--min-diameter", "100", "--scheme", "place_case"]],
["analysis leakage identify", ["analysis", "leakage", "identify", "--start-time", start, "--end-time", end, "--scheme", "leak_case"]],
["analysis leakage schemes list", ["analysis", "leakage", "schemes", "list"]],
["analysis leakage schemes get", ["analysis", "leakage", "schemes", "get", "leak_case"]],
["analysis burst-detection detect", ["analysis", "burst-detection", "detect", "--start-time", start, "--end-time", end, "--scheme", "detect_case"]],
["analysis burst-detection schemes list", ["analysis", "burst-detection", "schemes", "list"]],
["analysis burst-detection schemes get", ["analysis", "burst-detection", "schemes", "get", "detect_case"]],
["analysis burst-location locate", ["analysis", "burst-location", "locate", "--start-time", start, "--end-time", end, "--burst-leakage", "50.5", "--scheme", "locate_case", "--data-source", "simulation", "--pressure-file", pressureFile, "--flow-file", flowFile, "--use-scada-flow"]],
["analysis burst-location schemes list", ["analysis", "burst-location", "schemes", "list"]],
["analysis burst-location schemes get", ["analysis", "burst-location", "schemes", "get", "locate_case"]],
["analysis risk pipe-now", ["analysis", "risk", "pipe-now", "--pipe", "P1"]],
["analysis risk pipe-history", ["analysis", "risk", "pipe-history", "--pipe", "P1"]],
["analysis risk network", ["analysis", "risk", "network"]],
["data realtime links", ["data", "timeseries", "realtime", "links", "--start-time", start, "--end-time", end]],
["data realtime nodes", ["data", "timeseries", "realtime", "nodes", "--start-time", start, "--end-time", end]],
["data realtime simulation-by-id-time", ["data", "timeseries", "realtime", "simulation-by-id-time", "--id", "J1", "--type", "junction", "--time", at]],
["data realtime simulation-by-time-property", ["data", "timeseries", "realtime", "simulation-by-time-property", "--type", "pipe", "--time", at, "--property", "flow"]],
["data scheme links", ["data", "timeseries", "scheme", "links", "--start-time", start, "--end-time", end, "--scheme", "scheme_case", "--scheme-type", "simulation"]],
["data scheme node-field", ["data", "timeseries", "scheme", "node-field", "--node", "J1", "--field", "pressure", "--start-time", start, "--end-time", end, "--scheme", "scheme_case"]],
["data scheme simulation by-id", ["data", "timeseries", "scheme", "simulation", "--query", "by-id-time", "--id", "J1", "--time", at, "--type", "junction", "--scheme", "scheme_case"]],
["data scheme simulation by-property", ["data", "timeseries", "scheme", "simulation", "--query", "by-scheme-time-property", "--time", at, "--type", "pipe", "--property", "flow", "--scheme", "scheme_case"]],
["data scada query", ["data", "timeseries", "scada", "query", "--device-id", "D1", "--device-id", "D2", "--start-time", start, "--end-time", end, "--field", "monitored_value"]],
["data composite scada-simulation", ["data", "timeseries", "composite", "--kind", "scada-simulation", "--feature", "D1", "--feature", "D2", "--start-time", start, "--end-time", end, "--scheme", "scheme_case"]],
["data composite element-simulation", ["data", "timeseries", "composite", "--kind", "element-simulation", "--feature", "J1:pressure", "--start-time", start, "--end-time", end]],
["data composite element-scada", ["data", "timeseries", "composite", "--kind", "element-scada", "--feature", "J1", "--start-time", start, "--end-time", end, "--use-cleaned"]],
["data composite pipeline-health", ["data", "timeseries", "composite", "pipeline-health", "--pipe", "P1", "--start-time", start, "--end-time", end]],
["data scada get", ["data", "scada", "get", "--kind", "info", "--id", "SCADA-001"]],
["data scada list", ["data", "scada", "list", "--kind", "info"]],
["data scheme schema", ["data", "scheme", "schema"]],
["data scheme get", ["data", "scheme", "get", "--name", "scheme_case"]],
["data scheme list", ["data", "scheme", "list"]],
];
for (const [name, args] of cases) {
const run = await runAgainstServer(name, runCli, args, auth);
assert.equal(run.exitCode, 0, `${name}: ${run.stderr}`);
assert.equal(run.payload.ok, true, name);
assert.equal(run.payload.schema_version, "tjwater-cli/v1", name);
assert.ok(run.requests.length > 0, `${name}: no backend request`);
for (const request of run.requests) {
assert.match(request.path, /^\/api\/v1\//, name);
assert.equal(request.headers.authorization, "Bearer token", name);
assert.equal(request.headers["x-project-id"], "project-1", name);
}
}
} finally {
await rm(tempDir, { force: true, recursive: true });
}
});