- 工程形态:@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>
336 lines
10 KiB
TypeScript
336 lines
10 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
||
import {
|
||
parseDts,
|
||
findNode,
|
||
validateOverlay,
|
||
checkCompatible,
|
||
checkPhandle,
|
||
compileDts,
|
||
parseDtcErrors,
|
||
extractPhandleRefs,
|
||
DEFAULT_COMPATIBLE_DICT,
|
||
} from "../src/extensions/device-tree.js";
|
||
import type { ExecFn } from "../src/shared/types.js";
|
||
|
||
// 常用 DTS 片段
|
||
const SIMPLE_DTS = `/dts-v1/;
|
||
|
||
/ {
|
||
compatible = "vendor,board";
|
||
#address-cells = <1>;
|
||
#size-cells = <1>;
|
||
|
||
i2c0: i2c@40020000 {
|
||
compatible = "vendor,i2c";
|
||
reg = <0x40020000 0x1000>;
|
||
#address-cells = <1>;
|
||
#size-cells = <0>;
|
||
|
||
sensor: sensor@68 {
|
||
compatible = "vendor,sensor";
|
||
reg = <0x68>;
|
||
interrupt-parent = <&gpio4>;
|
||
interrupts = <17 2>;
|
||
};
|
||
};
|
||
|
||
gpio4: gpio@40004000 {
|
||
compatible = "vendor,gpio";
|
||
reg = <0x40004000 0x400>;
|
||
#gpio-cells = <2>;
|
||
phandle = <0x5>;
|
||
};
|
||
};
|
||
`;
|
||
|
||
describe("device-tree parseDts — 基础解析", () => {
|
||
it("解析根节点 + 两个子节点", () => {
|
||
const tree = parseDts(SIMPLE_DTS);
|
||
expect(tree.nodes).toHaveLength(1);
|
||
const root = tree.nodes[0];
|
||
expect(root).toBeDefined();
|
||
expect(root.fullName).toBe("/");
|
||
expect(root.children.length).toBe(2);
|
||
});
|
||
|
||
it("提取 compatible / reg / #address-cells 属性", () => {
|
||
const tree = parseDts(SIMPLE_DTS);
|
||
const root = tree.nodes[0];
|
||
expect(root.props["compatible"].raw).toBe('"vendor,board"');
|
||
expect(root.props["#address-cells"].raw).toBe("<1>");
|
||
expect(root.props["#size-cells"].raw).toBe("<1>");
|
||
});
|
||
|
||
it("节点 label 与 unit address 正确", () => {
|
||
const tree = parseDts(SIMPLE_DTS);
|
||
const root = tree.nodes[0];
|
||
const i2c = root.children.find((c) => c.label === "i2c0")!;
|
||
expect(i2c).toBeDefined();
|
||
expect(i2c.name).toBe("i2c");
|
||
expect(i2c.unitAddress).toBe("40020000");
|
||
expect(i2c.fullName).toBe("i2c@40020000");
|
||
|
||
const gpio = root.children.find((c) => c.label === "gpio4")!;
|
||
expect(gpio.unitAddress).toBe("40004000");
|
||
});
|
||
|
||
it("嵌套子节点 i2c0 → sensor@68", () => {
|
||
const i2c = parseDts(SIMPLE_DTS).nodes[0].children.find((c) => c.label === "i2c0")!;
|
||
expect(i2c.children).toHaveLength(1);
|
||
const sensor = i2c.children[0];
|
||
expect(sensor.fullName).toBe("sensor@68");
|
||
expect(sensor.label).toBe("sensor");
|
||
expect(sensor.props["reg"].raw).toBe("<0x68>");
|
||
});
|
||
|
||
it("phandle 引用提取自 interrupt-parent 属性", () => {
|
||
const tree = parseDts(SIMPLE_DTS);
|
||
const i2c = tree.nodes[0].children.find((c) => c.label === "i2c0")!;
|
||
const sensor = i2c.children[0];
|
||
expect(sensor.props["interrupt-parent"].phandleRefs).toEqual(["gpio4"]);
|
||
});
|
||
|
||
it("labels 表收集 i2c0 / sensor / gpio4", () => {
|
||
const tree = parseDts(SIMPLE_DTS);
|
||
expect(tree.labels.has("i2c0")).toBe(true);
|
||
expect(tree.labels.has("sensor")).toBe(true);
|
||
expect(tree.labels.has("gpio4")).toBe(true);
|
||
expect(tree.labels.size).toBe(3);
|
||
});
|
||
});
|
||
|
||
describe("device-tree parseDts — include 与 overlay", () => {
|
||
it("捕获 #include 路径", () => {
|
||
const tree = parseDts(`#include "stm32f411.dtsi"\n/dts-v1/;\n/ { };`);
|
||
expect(tree.includes).toContain("stm32f411.dtsi");
|
||
});
|
||
|
||
it("解析 overlay 引用节点 & label { }", () => {
|
||
const tree = parseDts(`&i2c0 { status = "okay"; };`);
|
||
expect(tree.nodes).toHaveLength(1);
|
||
expect(tree.nodes[0].refTarget).toBe("i2c0");
|
||
expect(tree.nodes[0].props["status"].raw).toBe('"okay"');
|
||
});
|
||
});
|
||
|
||
describe("device-tree findNode — 路径查找", () => {
|
||
it("按 label 路径查找 i2c0/sensor@68", () => {
|
||
const tree = parseDts(SIMPLE_DTS);
|
||
const root = tree.nodes[0];
|
||
const node = findNode(root, "i2c0/sensor@68");
|
||
expect(node).not.toBeNull();
|
||
expect(node!.fullName).toBe("sensor@68");
|
||
});
|
||
|
||
it("带前导 / 的路径也能找到", () => {
|
||
const tree = parseDts(SIMPLE_DTS);
|
||
const node = findNode(tree.nodes[0], "/i2c0/sensor@68");
|
||
expect(node).not.toBeNull();
|
||
});
|
||
|
||
it("按 label 查找 gpio4 节点", () => {
|
||
const tree = parseDts(SIMPLE_DTS);
|
||
const node = findNode(tree.nodes[0], "gpio4");
|
||
expect(node).not.toBeNull();
|
||
expect(node!.label).toBe("gpio4");
|
||
});
|
||
|
||
it("找不到路径返回 null", () => {
|
||
const tree = parseDts(SIMPLE_DTS);
|
||
expect(findNode(tree.nodes[0], "i2c0/nonexistent")).toBeNull();
|
||
});
|
||
|
||
it("空路径返回 null(边界)", () => {
|
||
const tree = parseDts(SIMPLE_DTS);
|
||
expect(findNode(tree.nodes[0], "")).toBeNull();
|
||
});
|
||
});
|
||
|
||
describe("device-tree validateOverlay — label 校验(fail-closed)", () => {
|
||
it("overlay 引用 base 中存在的 label → ok", () => {
|
||
const base = parseDts(SIMPLE_DTS);
|
||
const overlay = parseDts(`&i2c0 { status = "okay"; };`);
|
||
const r = validateOverlay(overlay, base);
|
||
expect(r.ok).toBe(true);
|
||
expect(r.errors).toHaveLength(0);
|
||
});
|
||
|
||
it("overlay 引用 base 中不存在的 label → 报 dangling 错误(fail-closed)", () => {
|
||
const base = parseDts(SIMPLE_DTS);
|
||
const overlay = parseDts(`&nonexistent_i2c { status = "okay"; };`);
|
||
const r = validateOverlay(overlay, base);
|
||
expect(r.ok).toBe(false);
|
||
expect(r.errors.length).toBeGreaterThan(0);
|
||
expect(r.errors[0]).toContain("dangling phandle reference");
|
||
expect(r.errors[0]).toContain("nonexistent_i2c");
|
||
});
|
||
|
||
it("overlay 属性值 <&label> 引用断链报错(fail-closed)", () => {
|
||
const base = parseDts(SIMPLE_DTS);
|
||
const overlay = parseDts(`&i2c0 { interrupt-parent = <&ghost>; };`);
|
||
const r = validateOverlay(overlay, base);
|
||
expect(r.ok).toBe(false);
|
||
const danglingErr = r.errors.find((e) => e.includes("&ghost"));
|
||
expect(danglingErr).toBeDefined();
|
||
});
|
||
|
||
it("overlay 引用 base 的 gpio4 label 通过校验", () => {
|
||
const base = parseDts(SIMPLE_DTS);
|
||
const overlay = parseDts(`&i2c0 { interrupt-parent = <&gpio4>; };`);
|
||
const r = validateOverlay(overlay, base);
|
||
expect(r.ok).toBe(true);
|
||
});
|
||
});
|
||
|
||
describe("device-tree checkCompatible — compatible 拼写校验", () => {
|
||
it("命中字典的 compatible 无 issue", () => {
|
||
const tree = parseDts(`/ { compatible = "rockchip,rk3588"; };`);
|
||
expect(checkCompatible(tree)).toHaveLength(0);
|
||
});
|
||
|
||
it("typo(少末尾字符)标 typo 并给建议", () => {
|
||
// st,stm32f411xe → 故意写 st,stm32f411x(少 e)
|
||
const tree = parseDts(`/ { compatible = "st,stm32f411x"; };`);
|
||
const issues = checkCompatible(tree);
|
||
expect(issues).toHaveLength(1);
|
||
expect(issues[0].kind).toBe("typo");
|
||
expect(issues[0].suggestion).toBeDefined();
|
||
expect(DEFAULT_COMPATIBLE_DICT).toContain(issues[0].suggestion);
|
||
});
|
||
|
||
it("完全陌生的 compatible 标 unknown(非错非对)", () => {
|
||
const tree = parseDts(`/ { compatible = "acme,board2000"; };`);
|
||
const issues = checkCompatible(tree);
|
||
expect(issues).toHaveLength(1);
|
||
expect(issues[0].kind).toBe("unknown");
|
||
});
|
||
|
||
it("多 compatible 串逐个校验", () => {
|
||
const tree = parseDts(`/ { compatible = "rockchip,rk3588", "acme,xyz"; };`);
|
||
const issues = checkCompatible(tree);
|
||
// rk3588 命中,acme,xyz 未知
|
||
expect(issues).toHaveLength(1);
|
||
expect(issues[0].compatible).toBe("acme,xyz");
|
||
});
|
||
});
|
||
|
||
describe("device-tree checkPhandle — phandle 引用图", () => {
|
||
it("无断链、未被引用的 phandle 标 orphan", () => {
|
||
const tree = parseDts(`/ {
|
||
gpio4: gpio@40004000 {
|
||
compatible = "vendor,gpio";
|
||
phandle = <0x5>;
|
||
};
|
||
gpio5: gpio@40005000 {
|
||
phandle = <0x6>;
|
||
};
|
||
};`);
|
||
const report = checkPhandle(tree);
|
||
expect(report.dangling).toHaveLength(0);
|
||
// gpio4 / gpio5 都未被 & 引用 → 都是 orphan
|
||
expect(report.orphans.length).toBe(2);
|
||
});
|
||
|
||
it("dangling 引用未定义 label(fail-closed)", () => {
|
||
const tree = parseDts(`/ {
|
||
sensor: sensor@68 {
|
||
interrupt-parent = <&nonexistent>;
|
||
};
|
||
};`);
|
||
const report = checkPhandle(tree);
|
||
expect(report.dangling).toHaveLength(1);
|
||
expect(report.dangling[0].ref).toBe("nonexistent");
|
||
});
|
||
|
||
it("正常引用不算 dangling 也不算 orphan", () => {
|
||
const tree = parseDts(`/ {
|
||
gpio4: gpio@40004000 {
|
||
phandle = <0x5>;
|
||
};
|
||
sensor: sensor@68 {
|
||
interrupt-parent = <&gpio4>;
|
||
};
|
||
};`);
|
||
const report = checkPhandle(tree);
|
||
expect(report.dangling).toHaveLength(0);
|
||
expect(report.orphans).toHaveLength(0);
|
||
});
|
||
});
|
||
|
||
describe("device-tree extractPhandleRefs", () => {
|
||
it("提取单个 <&label>", () => {
|
||
expect(extractPhandleRefs("<&gpio4>")).toEqual(["gpio4"]);
|
||
});
|
||
it("提取带参数的引用 <&gpio4 17>", () => {
|
||
expect(extractPhandleRefs("<&gpio4 17 2>")).toEqual(["gpio4"]);
|
||
});
|
||
it("提取多个引用", () => {
|
||
expect(extractPhandleRefs("<&gpio4 &gpio5>")).toEqual(["gpio4", "gpio5"]);
|
||
});
|
||
it("无引用返回空数组(边界)", () => {
|
||
expect(extractPhandleRefs("<0x68>")).toEqual([]);
|
||
});
|
||
});
|
||
|
||
describe("device-tree compileDts — 注入 ExecFn(无真实硬件)", () => {
|
||
it("dtc 成功 → ok:true 返回输出路径", async () => {
|
||
const exec: ExecFn = async () => ({
|
||
stdout: "",
|
||
stderr: "",
|
||
exitCode: 0,
|
||
});
|
||
const r = await compileDts(exec, "board.dts", { out: "out.dtb" });
|
||
expect(r.ok).toBe(true);
|
||
expect(r.out).toBe("out.dtb");
|
||
expect(r.errors).toHaveLength(0);
|
||
});
|
||
|
||
it("dtc 语法错误 → 结构化错误含行号", async () => {
|
||
const exec: ExecFn = async () => ({
|
||
stdout: "",
|
||
stderr: "Error: board.dts:42.1-12: syntax error\n",
|
||
exitCode: 1,
|
||
});
|
||
const r = await compileDts(exec, "board.dts", { out: "out.dtb" });
|
||
expect(r.ok).toBe(false);
|
||
expect(r.errors.length).toBeGreaterThan(0);
|
||
expect(r.errors[0].line).toBe(42);
|
||
expect(r.errors[0].message).toContain("syntax error");
|
||
});
|
||
|
||
it("exec 抛错 → fail-closed 返回错误", async () => {
|
||
const exec: ExecFn = async () => {
|
||
throw new Error("ENOENT dtc");
|
||
};
|
||
const r = await compileDts(exec, "board.dts");
|
||
expect(r.ok).toBe(false);
|
||
expect(r.errors[0].message).toContain("dtc exec failed");
|
||
});
|
||
|
||
it("overlay 模式加 -@ 参数", async () => {
|
||
let captured: string[] = [];
|
||
const exec: ExecFn = async (_cmd, args) => {
|
||
captured = args;
|
||
return { stdout: "", stderr: "", exitCode: 0 };
|
||
};
|
||
await compileDts(exec, "overlay.dts", { overlay: true, out: "overlay.dtbo" });
|
||
expect(captured).toContain("-@");
|
||
expect(captured).toContain("overlay.dts");
|
||
expect(captured).toContain("overlay.dtbo");
|
||
});
|
||
});
|
||
|
||
describe("device-tree parseDtcErrors", () => {
|
||
it("解析行号 + 消息", () => {
|
||
const errs = parseDtcErrors("Error: board.dts:42.1-12: syntax error");
|
||
expect(errs).toHaveLength(1);
|
||
expect(errs[0].line).toBe(42);
|
||
expect(errs[0].message).toContain("syntax error");
|
||
});
|
||
|
||
it("无行号的 FATAL 行也保留(边界)", () => {
|
||
const errs = parseDtcErrors("FATAL: something bad");
|
||
expect(errs.length).toBeGreaterThanOrEqual(1);
|
||
});
|
||
});
|