Files
pikasTech-HWLAB/tools/hwlab-cli/lib/cli.test.mjs
T
2026-05-26 05:31:57 +08:00

118 lines
3.3 KiB
JavaScript

import assert from "node:assert/strict";
import test from "node:test";
import {
HWLAB_M3_IO_API_ROUTE,
HWLAB_M3_STATUS_API_ROUTE
} from "../../../skills/hwlab-agent-runtime/scripts/src/m3-io-skill-client.mjs";
import { runCli } from "./cli.mjs";
async function captureCli(args, options = {}) {
let stdout = "";
let stderr = "";
const exitCode = await runCli(args, {
cwd: options.cwd ?? process.cwd(),
env: options.env ?? {
PATH: process.env.PATH
},
requestJson: options.requestJson,
spawnDetached: options.spawnDetached,
stdout: {
write(chunk) {
stdout += chunk;
}
},
stderr: {
write(chunk) {
stderr += chunk;
}
}
});
return { exitCode, stdout, stderr };
}
test("repo hwlab-cli cicd is removed from the active G14 release path", async () => {
const result = await captureCli(["cicd", "submit", "--kind", "ci-publish"]);
assert.equal(result.exitCode, 2);
const body = JSON.parse(result.stderr);
assert.equal(body.ok, false);
assert.equal(body.code, "legacy-cicd-removed");
assert.match(body.message, /G14 release path/u);
assert.match(body.replacement.observePipelineRuns, /tran G14:k3s/u);
});
test("repo hwlab-cli m3 status uses Skill CLI and calls only HWLAB API /v1/m3/status", async () => {
const calls = [];
const result = await captureCli(
[
"m3",
"status",
"--api-base-url",
"http://hwlab-cloud-api.hwlab-dev.svc.cluster.local:6667",
"--trace-id",
"trc_hwlab_cli_status"
],
{
requestJson: async (url, request) => {
calls.push({ url, request });
assert.equal(url, `http://hwlab-cloud-api.hwlab-dev.svc.cluster.local:6667${HWLAB_M3_STATUS_API_ROUTE}`);
assert.equal(request.method, "GET");
return {
ok: true,
status: 200,
body: {
status: "live",
sourceKind: "DEV-LIVE",
traceId: "trc_hwlab_cli_status",
boxes: [{
id: "boxsimu_2",
resourceId: "res_boxsimu_2",
online: true,
ports: {
DI1: {
value: false
}
}
}]
}
};
}
}
);
assert.equal(result.exitCode, 0, result.stderr);
const body = JSON.parse(result.stdout);
assert.equal(body.route, HWLAB_M3_STATUS_API_ROUTE);
assert.equal(body.method, "GET");
assert.equal(body.readback.value, false);
assert.equal(body.command.includes("hwlab-cloud-api.hwlab-dev"), false);
assert.equal(calls.length, 1);
});
test("repo hwlab-cli m3 io blocks direct hardware targets before request", async () => {
const result = await captureCli(
[
"m3",
"io",
"--action",
"di.read",
"--api-base-url",
"http://hwlab-patch-panel.hwlab-dev.svc.cluster.local:7301",
"--trace-id",
"trc_hwlab_cli_direct_blocked"
],
{
requestJson: async () => {
throw new Error("direct hardware target must not be called");
}
}
);
assert.equal(result.exitCode, 2);
const body = JSON.parse(result.stderr);
assert.equal(body.route, HWLAB_M3_IO_API_ROUTE);
assert.equal(body.blocker.code, "direct_hardware_target_blocked");
assert.equal(body.hwlabApi.redactedUrl, null);
assert.equal(JSON.stringify(body).includes("patch-panel.hwlab-dev"), false);
});