- 工程形态:@d-robotics/physical-byte-agent npm 库包
- 单一 barrel src/extensions/index.ts 装配 21 域扩展(package.json pi.extensions 清单 + .pi/extensions/index.ts 重导出,pi 自动加载)
- src/agent.ts PhysicalByteAgent(launch() 程序化调 createAgentSession + verifyAssembly() 离线自检)
- bin/pba.ts CLI、models.json(glm-5.2 条目)、README.md、AGENTS.md
- 命名:全部域命名(glm-provider/safety-gate/flash/jtag-debugger/loop-orchestrator…),去掉 F 编号
- 架构:内联逻辑 + 顶层仅 import type(esbuild 擦除)+ 工厂内动态 import defineTool/Type
- 逻辑离线可单测(不解析 pi),工厂在真 pi 内装配;fail-closed 语义保留
- 两命门:glm-provider.ts buildGlmModelConfig()(thinkingFormat:qwen + maxTokens:8192 + thinkingLevelMap 全 null)+ detectEmptyOutput();normalizeHexArg() + makeHexNormalizingPrepareArguments()
- 验证:tsc --noEmit 0 错(对 src/types/pi.d.ts 类型镜像)+ vitest 732 用例/21 文件全绿 + tsx 离线装配冒烟 21 扩展全就位
- 密钥铁律:grep sk-nAm 在 src/tests/bin/.pi = 0,key 全程 ${DROBOTICS_LLM_KEY} 占位
Co-Authored-By: Claude <noreply@anthropic.com>
268 lines
9.5 KiB
TypeScript
268 lines
9.5 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
||
import {
|
||
collectSignal,
|
||
matchExpect,
|
||
runVerify,
|
||
buildSignalCommand,
|
||
filterDmesg,
|
||
describeSignal,
|
||
toNumber,
|
||
} from "../src/extensions/hardware-verify.js";
|
||
import type { ExecFn, ExecResult, SignalSpec, ExpectSpec } from "../src/shared/types.js";
|
||
|
||
// ---- mock 工具 ----
|
||
|
||
/** 构造一个按 command→stdout 映射返回 exitCode=0 的 ExecFn。 */
|
||
function mockExec(map: Record<string, string | ExecResult>): ExecFn {
|
||
return async (command: string, args: string[]) => {
|
||
const key = command + " " + args.join(" ");
|
||
// 先试精确 key,再试 command 前缀
|
||
const hit = map[key] ?? map[command];
|
||
if (hit === undefined) {
|
||
return { stdout: "", stderr: `command not found: ${key}`, exitCode: 127 };
|
||
}
|
||
if (typeof hit === "string") return { stdout: hit, stderr: "", exitCode: 0 };
|
||
return hit;
|
||
};
|
||
}
|
||
|
||
const failResult = (stderr: string, code = 1): ExecResult => ({ stdout: "", stderr, exitCode: code });
|
||
|
||
describe("hardware-verify matchExpect — 各 mode", () => {
|
||
it("contains: 子串命中", () => {
|
||
expect(matchExpect("gpio33 value=1 active", { match: "value=1", mode: "contains" })).toBe(true);
|
||
});
|
||
it("contains: 子串未命中 → false", () => {
|
||
expect(matchExpect("gpio33 value=0", { match: "value=1", mode: "contains" })).toBe(false);
|
||
});
|
||
it("equals: 精确匹配(trim)", () => {
|
||
expect(matchExpect(" 1 ", { match: "1", mode: "equals" })).toBe(true);
|
||
});
|
||
it("regex: 正则命中", () => {
|
||
expect(matchExpect("[ 3.14] gpio-33 set HIGH", { match: "gpio.*33", mode: "regex" })).toBe(true);
|
||
});
|
||
it("regex: 非法正则 → false(不抛)", () => {
|
||
expect(matchExpect("abc", { match: "(", mode: "regex" })).toBe(false);
|
||
});
|
||
it("range: 数值落在区间内", () => {
|
||
expect(matchExpect("512", { match: 0, mode: "range", min: 0, max: 1023 })).toBe(true);
|
||
});
|
||
it("range: 超上界 → false", () => {
|
||
expect(matchExpect("2000", { match: 0, mode: "range", min: 0, max: 1023 })).toBe(false);
|
||
});
|
||
it("gt: 严格大于", () => {
|
||
expect(matchExpect("1", { match: 0, mode: "gt" })).toBe(true);
|
||
expect(matchExpect("0", { match: 0, mode: "gt" })).toBe(false);
|
||
});
|
||
it("gt: 非数字 → false", () => {
|
||
expect(matchExpect("err", { match: 1, mode: "gt" })).toBe(false);
|
||
});
|
||
});
|
||
|
||
describe("hardware-verify toNumber", () => {
|
||
it("解析整数", () => {
|
||
expect(toNumber("512")).toBe(512);
|
||
});
|
||
it("非数字 → NaN", () => {
|
||
expect(toNumber("abc")).toBeNaN();
|
||
});
|
||
});
|
||
|
||
describe("hardware-verify buildSignalCommand", () => {
|
||
it("gpio → gpioget chip line", () => {
|
||
const c = buildSignalCommand({ type: "gpio", args: { chip: "gpiochip0", line: "33" } });
|
||
expect(c).not.toBeNull();
|
||
expect(c!.command).toBe("gpioget");
|
||
expect(c!.args).toEqual(["gpiochip0", "33"]);
|
||
});
|
||
it("gpio 缺 chip → null(fail-closed 前置)", () => {
|
||
expect(buildSignalCommand({ type: "gpio", args: { line: "33" } })).toBeNull();
|
||
});
|
||
it("i2c → i2cget -y bus addr reg", () => {
|
||
const c = buildSignalCommand({ type: "i2c", args: { bus: "0", addr: "0x76", reg: "0x10" } });
|
||
expect(c!.command).toBe("i2cget");
|
||
expect(c!.args).toEqual(["-y", "0", "0x76", "0x10"]);
|
||
});
|
||
it("i2c 无 reg 也合法", () => {
|
||
const c = buildSignalCommand({ type: "i2c", args: { bus: "1", addr: "0x50" } });
|
||
expect(c!.args).toEqual(["-y", "1", "0x50"]);
|
||
});
|
||
it("sysfs → cat path", () => {
|
||
const c = buildSignalCommand({ type: "sysfs", args: { path: "/sys/class/gpio/gpio33/value" } });
|
||
expect(c!.command).toBe("cat");
|
||
expect(c!.args).toEqual(["/sys/class/gpio/gpio33/value"]);
|
||
});
|
||
it("stdout 缺 command → null", () => {
|
||
expect(buildSignalCommand({ type: "stdout", args: {} })).toBeNull();
|
||
});
|
||
});
|
||
|
||
describe("hardware-verify filterDmesg", () => {
|
||
it("按 grep 正则过滤行", () => {
|
||
const out = "[ 1.0] gpio-33 HIGH\n[ 2.0] i2c-0 ready\n[ 3.0] gpio-33 LOW";
|
||
const r = filterDmesg(out, "gpio.*33");
|
||
expect(r).toContain("gpio-33 HIGH");
|
||
expect(r).toContain("gpio-33 LOW");
|
||
expect(r).not.toContain("i2c-0");
|
||
});
|
||
it("无 grep 返回 trim 后的全部", () => {
|
||
expect(filterDmesg(" a\nb ")).toBe("a\nb");
|
||
});
|
||
});
|
||
|
||
describe("hardware-verify collectSignal(注入 mock)", () => {
|
||
it("gpio 采集返回 gpioget stdout", async () => {
|
||
const fn = mockExec({ "gpioget gpiochip0 33": "1" });
|
||
const v = await collectSignal({ type: "gpio", args: { chip: "gpiochip0", line: "33" } }, fn);
|
||
expect(v).toBe("1");
|
||
});
|
||
it("dmesg 采集应用 grep 过滤", async () => {
|
||
const fn = mockExec({ dmesg: "[1] gpio-33 HIGH\n[2] usb ready" });
|
||
const v = await collectSignal({ type: "dmesg", args: { grep: "gpio.*33" } }, fn);
|
||
expect(v).toBe("[1] gpio-33 HIGH");
|
||
});
|
||
it("fail-closed: 非零退出 → 抛错", async () => {
|
||
const fn = mockExec({ "gpioget gpiochip0 33": failResult("Permission denied", 1) });
|
||
await expect(
|
||
collectSignal({ type: "gpio", args: { chip: "gpiochip0", line: "33" } }, fn),
|
||
).rejects.toThrow(/exited 1/);
|
||
});
|
||
it("fail-closed: 命令构建失败(缺参数)→ 抛错", async () => {
|
||
const fn = mockExec({});
|
||
await expect(collectSignal({ type: "gpio", args: {} }, fn)).rejects.toThrow(/cannot build/);
|
||
});
|
||
it("fail-closed: ExecFn 抛异常 → 包装后抛出", async () => {
|
||
const fn: ExecFn = async () => {
|
||
throw new Error("board unreachable");
|
||
};
|
||
await expect(
|
||
collectSignal({ type: "gpio", args: { chip: "gpiochip0", line: "33" } }, fn),
|
||
).rejects.toThrow(/board unreachable/);
|
||
});
|
||
});
|
||
|
||
describe("hardware-verify describeSignal", () => {
|
||
it("gpio 描述", () => {
|
||
expect(describeSignal({ type: "gpio", args: { chip: "gpiochip0", line: "33" } })).toBe(
|
||
"gpiogpiochip0:33",
|
||
);
|
||
});
|
||
it("sysfs 描述", () => {
|
||
expect(describeSignal({ type: "sysfs", args: { path: "/sys/class/gpio/gpio33/value" } })).toBe(
|
||
"sysfs:/sys/class/gpio/gpio33/value",
|
||
);
|
||
});
|
||
});
|
||
|
||
describe("hardware-verify runVerify — 四元组 + fail-closed", () => {
|
||
it("全部匹配 → judge pass, rootCause null", async () => {
|
||
const fn = mockExec({
|
||
"gpioget gpiochip0 33": "1",
|
||
"cat /sys/class/gpio/gpio33/value": "1",
|
||
});
|
||
const rec = await runVerify(
|
||
"GPIO pin 33 output high",
|
||
[
|
||
{ type: "gpio", args: { chip: "gpiochip0", line: "33" } },
|
||
{ type: "sysfs", args: { path: "/sys/class/gpio/gpio33/value" } },
|
||
],
|
||
{ match: "1", mode: "equals", failClosed: true },
|
||
fn,
|
||
);
|
||
expect(rec.task).toBe("GPIO pin 33 output high");
|
||
expect(rec.judge).toBe("pass");
|
||
expect(rec.rootCause).toBeNull();
|
||
expect(rec.trace).toHaveLength(2);
|
||
expect(rec.trace[0]?.matched).toBe(true);
|
||
expect(rec.trace[1]?.matched).toBe(true);
|
||
});
|
||
|
||
it("GPIO 33 短路到地 → judge fail, rootCause 含 value 与 expected", async () => {
|
||
const fn = mockExec({
|
||
"gpioget gpiochip0 33": "0", // 短路到地,读出 0
|
||
});
|
||
const rec = await runVerify(
|
||
"GPIO pin 33 output high",
|
||
[{ type: "gpio", args: { chip: "gpiochip0", line: "33" } }],
|
||
{ match: "1", mode: "equals", failClosed: true },
|
||
fn,
|
||
);
|
||
expect(rec.judge).toBe("fail");
|
||
expect(rec.rootCause).toContain("0");
|
||
expect(rec.rootCause).toContain("expected");
|
||
expect(rec.rootCause).toContain("1");
|
||
expect(rec.trace[0]?.collected).toBe("0");
|
||
expect(rec.trace[0]?.matched).toBe(false);
|
||
});
|
||
|
||
it("采集失败 → fail-closed 判 fail,rootCause 含 collection-failed", async () => {
|
||
const fn = mockExec({
|
||
"gpioget gpiochip0 33": failResult("Permission denied", 1),
|
||
});
|
||
const rec = await runVerify(
|
||
"GPIO pin 33 output high",
|
||
[{ type: "gpio", args: { chip: "gpiochip0", line: "33" } }],
|
||
{ match: "1", mode: "equals", failClosed: true },
|
||
fn,
|
||
);
|
||
expect(rec.judge).toBe("fail");
|
||
expect(rec.rootCause).toContain("collection-failed");
|
||
expect(rec.trace[0]?.collected).toBe("");
|
||
expect(rec.trace[0]?.matched).toBe(false);
|
||
});
|
||
|
||
it("多信号部分失败 → rootCause 含每条失败描述(;分隔)", async () => {
|
||
const fn = mockExec({
|
||
"gpioget gpiochip0 33": "0", // 不匹配 expect 1
|
||
"cat /sys/class/gpio/gpio33/value": "1", // 匹配
|
||
"gpioget gpiochip0 34": failResult("no such line", 1), // 采集失败
|
||
});
|
||
const rec = await runVerify(
|
||
"multi-signal check",
|
||
[
|
||
{ type: "gpio", args: { chip: "gpiochip0", line: "33" } },
|
||
{ type: "sysfs", args: { path: "/sys/class/gpio/gpio33/value" } },
|
||
{ type: "gpio", args: { chip: "gpiochip0", line: "34" } },
|
||
],
|
||
{ match: "1", mode: "equals", failClosed: true },
|
||
fn,
|
||
);
|
||
expect(rec.judge).toBe("fail");
|
||
expect(rec.rootCause).toContain(";");
|
||
// 失败条数 = 2(gpio33 value 不匹配 + gpio34 采集失败)
|
||
const parts = rec.rootCause!.split("; ");
|
||
expect(parts.length).toBe(2);
|
||
});
|
||
|
||
it("空 signals 列表 → judge pass(vacuously true)", async () => {
|
||
const fn = mockExec({});
|
||
const rec = await runVerify("noop", [], { match: "1", mode: "equals" }, fn);
|
||
expect(rec.judge).toBe("pass");
|
||
expect(rec.trace).toHaveLength(0);
|
||
expect(rec.rootCause).toBeNull();
|
||
});
|
||
|
||
it("range 模式集成:电流在阈值内 → pass", async () => {
|
||
const fn = mockExec({ "cat /sys/hwmon/current": "320" });
|
||
const rec = await runVerify(
|
||
"motor stall current ok",
|
||
[{ type: "current", args: { path: "/sys/hwmon/current" } }],
|
||
{ match: 0, mode: "range", min: 0, max: 500, failClosed: true },
|
||
fn,
|
||
);
|
||
expect(rec.judge).toBe("pass");
|
||
});
|
||
|
||
it("range 模式集成:电流超阈值 → fail", async () => {
|
||
const fn = mockExec({ "cat /sys/hwmon/current": "1200" });
|
||
const rec = await runVerify(
|
||
"motor stall current ok",
|
||
[{ type: "current", args: { path: "/sys/hwmon/current" } }],
|
||
{ match: 0, mode: "range", min: 0, max: 500, failClosed: true },
|
||
fn,
|
||
);
|
||
expect(rec.judge).toBe("fail");
|
||
expect(rec.rootCause).toContain("1200");
|
||
});
|
||
});
|