- 工程形态:@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>
445 lines
15 KiB
TypeScript
445 lines
15 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
||
import {
|
||
resolveToolchain,
|
||
generateCmakeToolchain,
|
||
detectToolchain,
|
||
detectHostGcc,
|
||
parseFileOutput,
|
||
verifyArtifact,
|
||
ToolchainSession,
|
||
TOOLCHAIN_REGISTRY,
|
||
KNOWN_TRIPLES,
|
||
isKnownTriple,
|
||
normalizeTripleFromEnv,
|
||
type ToolchainTriple,
|
||
} from "../src/extensions/cross-compile.js";
|
||
import type { ExecFn, ExecResult } from "../src/shared/types.js";
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// mock ExecFn 工厂:按 (command, args) 匹配返回结果,不匹配返回 not-found
|
||
// ---------------------------------------------------------------------------
|
||
function makeMockExec(
|
||
table: Array<{
|
||
match: (cmd: string, args: string[]) => boolean;
|
||
result: ExecResult;
|
||
}>,
|
||
): ExecFn {
|
||
return async (cmd, args) => {
|
||
for (const row of table) {
|
||
if (row.match(cmd, args)) return row.result;
|
||
}
|
||
return { stdout: "", stderr: "command not found", exitCode: 127 };
|
||
};
|
||
}
|
||
|
||
const ok = (stdout: string): ExecResult => ({ stdout, stderr: "", exitCode: 0 });
|
||
const fail = (stderr: string, code = 1): ExecResult => ({
|
||
stdout: "",
|
||
stderr,
|
||
exitCode: code,
|
||
});
|
||
|
||
// ===========================================================================
|
||
// resolveToolchain
|
||
// ===========================================================================
|
||
|
||
describe("cross-compile resolveToolchain — aarch64-linux-gnu", () => {
|
||
it("前缀是 aarch64-linux-gnu-", () => {
|
||
const tc = resolveToolchain("aarch64-linux-gnu");
|
||
expect(tc.prefix).toBe("aarch64-linux-gnu-");
|
||
});
|
||
|
||
it("gcc/cxx/objcopy 含 triple 前缀", () => {
|
||
const tc = resolveToolchain("aarch64-linux-gnu");
|
||
expect(tc.gcc).toBe("aarch64-linux-gnu-gcc");
|
||
expect(tc.cxx).toBe("aarch64-linux-gnu-g++");
|
||
expect(tc.objcopy).toBe("aarch64-linux-gnu-objcopy");
|
||
});
|
||
|
||
it("cmakeSystemName=Linux, cmakeSystemProcessor=aarch64, elfArch=aarch64", () => {
|
||
const tc = resolveToolchain("aarch64-linux-gnu");
|
||
expect(tc.cmakeSystemName).toBe("Linux");
|
||
expect(tc.cmakeSystemProcessor).toBe("aarch64");
|
||
expect(tc.elfArch).toBe("aarch64");
|
||
});
|
||
});
|
||
|
||
describe("cross-compile resolveToolchain — arm-none-eabi (bare metal)", () => {
|
||
it("前缀 + 编译器名正确", () => {
|
||
const tc = resolveToolchain("arm-none-eabi");
|
||
expect(tc.prefix).toBe("arm-none-eabi-");
|
||
expect(tc.gcc).toBe("arm-none-eabi-gcc");
|
||
expect(tc.cxx).toBe("arm-none-eabi-g++");
|
||
expect(tc.objcopy).toBe("arm-none-eabi-objcopy");
|
||
});
|
||
|
||
it("cmakeSystemName=Generic (bare metal 无 OS)", () => {
|
||
const tc = resolveToolchain("arm-none-eabi");
|
||
expect(tc.cmakeSystemName).toBe("Generic");
|
||
expect(tc.cmakeSystemProcessor).toBe("arm");
|
||
});
|
||
});
|
||
|
||
describe("cross-compile resolveToolchain — 边界 (fail-closed)", () => {
|
||
it("未知 triple 抛错", () => {
|
||
expect(() => resolveToolchain("x86_64-linux-gnu" as ToolchainTriple)).toThrow(
|
||
/unknown toolchain triple/i,
|
||
);
|
||
});
|
||
|
||
it("注册表至少含 aarch64 + arm-none-eabi", () => {
|
||
expect(Object.keys(TOOLCHAIN_REGISTRY)).toContain("aarch64-linux-gnu");
|
||
expect(Object.keys(TOOLCHAIN_REGISTRY)).toContain("arm-none-eabi");
|
||
});
|
||
|
||
it("返回的对象是独立副本(改不影响注册表)", () => {
|
||
const a = resolveToolchain("aarch64-linux-gnu");
|
||
const b = resolveToolchain("aarch64-linux-gnu");
|
||
expect(a).not.toBe(b);
|
||
expect(a.gcc).toBe(b.gcc);
|
||
});
|
||
});
|
||
|
||
// ===========================================================================
|
||
// triple 校验 / env 规整(纯逻辑)
|
||
// ===========================================================================
|
||
|
||
describe("cross-compile isKnownTriple / normalizeTripleFromEnv", () => {
|
||
it("已知 triple 返回 true 并收窄类型", () => {
|
||
const v: string = "aarch64-linux-gnu";
|
||
expect(isKnownTriple(v)).toBe(true);
|
||
});
|
||
|
||
it("未知 triple 返回 false", () => {
|
||
expect(isKnownTriple("x86_64-linux-gnu")).toBe(false);
|
||
});
|
||
|
||
it("normalizeTripleFromEnv 已知值原样返回", () => {
|
||
expect(normalizeTripleFromEnv("arm-none-eabi")).toBe("arm-none-eabi");
|
||
});
|
||
|
||
it("normalizeTripleFromEnv 空/未知返回 null (fail-closed)", () => {
|
||
expect(normalizeTripleFromEnv(undefined)).toBeNull();
|
||
expect(normalizeTripleFromEnv("")).toBeNull();
|
||
expect(normalizeTripleFromEnv("x86_64-linux-gnu")).toBeNull();
|
||
});
|
||
|
||
it("KNOWN_TRIPLES 含四个 triple", () => {
|
||
expect(KNOWN_TRIPLES).toHaveLength(4);
|
||
expect(KNOWN_TRIPLES).toContain("riscv64-unknown-elf");
|
||
});
|
||
});
|
||
|
||
// ===========================================================================
|
||
// generateCmakeToolchain
|
||
// ===========================================================================
|
||
|
||
describe("cross-compile generateCmakeToolchain", () => {
|
||
it("含 CMAKE_C_COMPILER 且值含 triple 前缀", () => {
|
||
const tc = resolveToolchain("aarch64-linux-gnu");
|
||
const cmake = generateCmakeToolchain(tc);
|
||
expect(cmake).toContain("CMAKE_C_COMPILER");
|
||
expect(cmake).toContain("aarch64-linux-gnu-gcc");
|
||
});
|
||
|
||
it("含 CMAKE_CXX_COMPILER 和 CMAKE_OBJCOPY", () => {
|
||
const tc = resolveToolchain("aarch64-linux-gnu");
|
||
const cmake = generateCmakeToolchain(tc);
|
||
expect(cmake).toContain("CMAKE_CXX_COMPILER");
|
||
expect(cmake).toContain("aarch64-linux-gnu-g++");
|
||
expect(cmake).toContain("CMAKE_OBJCOPY");
|
||
expect(cmake).toContain("aarch64-linux-gnu-objcopy");
|
||
});
|
||
|
||
it("CMAKE_SYSTEM_NAME=Linux (aarch64) / Generic (arm-none-eabi)", () => {
|
||
const aarch = generateCmakeToolchain(resolveToolchain("aarch64-linux-gnu"));
|
||
expect(aarch).toMatch(/CMAKE_SYSTEM_NAME\s+Linux/);
|
||
|
||
const arm = generateCmakeToolchain(resolveToolchain("arm-none-eabi"));
|
||
expect(arm).toMatch(/CMAKE_SYSTEM_NAME\s+Generic/);
|
||
});
|
||
|
||
it("含 CMAKE_SYSTEM_PROCESSOR", () => {
|
||
const tc = resolveToolchain("aarch64-linux-gnu");
|
||
const cmake = generateCmakeToolchain(tc);
|
||
expect(cmake).toContain("CMAKE_SYSTEM_PROCESSOR");
|
||
expect(cmake).toContain("aarch64");
|
||
});
|
||
|
||
it("有 sysroot 时生成 CMAKE_SYSROOT + FIND_ROOT_PATH", () => {
|
||
const tc = resolveToolchain("aarch64-linux-gnu");
|
||
const sysroot = "/usr/aarch64-linux-gnu/sysroot";
|
||
const cmake = generateCmakeToolchain(tc, sysroot);
|
||
expect(cmake).toContain("CMAKE_SYSROOT");
|
||
expect(cmake).toContain(sysroot);
|
||
expect(cmake).toContain("CMAKE_FIND_ROOT_PATH");
|
||
expect(cmake).toContain("CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY");
|
||
expect(cmake).toContain("CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER");
|
||
});
|
||
|
||
it("无 sysroot 时不生成 CMAKE_SYSROOT(bare-metal)但 FIND_ROOT_PATH_MODE 仍在", () => {
|
||
const tc = resolveToolchain("arm-none-eabi");
|
||
const cmake = generateCmakeToolchain(tc);
|
||
expect(cmake).not.toContain("CMAKE_SYSROOT");
|
||
expect(cmake).toContain("CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY");
|
||
});
|
||
|
||
it("生成的文件不含 F 编号字样(域命名)", () => {
|
||
const cmake = generateCmakeToolchain(resolveToolchain("aarch64-linux-gnu"));
|
||
expect(cmake).not.toMatch(/\bF\d+\b/);
|
||
});
|
||
});
|
||
|
||
// ===========================================================================
|
||
// detectToolchain (mock ExecFn)
|
||
// ===========================================================================
|
||
|
||
describe("cross-compile detectToolchain — mock", () => {
|
||
it("工具链可用时 available=true 并提取 version + sysroot", async () => {
|
||
const exec = makeMockExec([
|
||
{
|
||
match: (c, a) => c === "aarch64-linux-gnu-gcc" && a.includes("--version"),
|
||
result: ok("aarch64-linux-gnu-gcc (Debian 12.2.0) 12.2.0\nCopyright (C) 2022"),
|
||
},
|
||
{
|
||
match: (c, a) => c === "aarch64-linux-gnu-gcc" && a.includes("-print-sysroot"),
|
||
result: ok("/usr/aarch64-linux-gnu/lib/../sysroot"),
|
||
},
|
||
{ match: (c) => c === "which", result: ok("/usr/bin/aarch64-linux-gnu-gcc") },
|
||
]);
|
||
const avail = await detectToolchain(exec, "aarch64-linux-gnu");
|
||
expect(avail.available).toBe(true);
|
||
expect(avail.version).toContain("12.2.0");
|
||
expect(avail.sysroot).toBe("/usr/aarch64-linux-gnu/lib/../sysroot");
|
||
expect(avail.gccPath).toBe("/usr/bin/aarch64-linux-gnu-gcc");
|
||
});
|
||
|
||
it("工具链未装时 available=false (fail-closed)", async () => {
|
||
const exec = makeMockExec([
|
||
{
|
||
match: (c, a) => c === "aarch64-linux-gnu-gcc" && a.includes("--version"),
|
||
result: fail("aarch64-linux-gnu-gcc: command not found", 127),
|
||
},
|
||
]);
|
||
const avail = await detectToolchain(exec, "aarch64-linux-gnu");
|
||
expect(avail.available).toBe(false);
|
||
expect(avail.error).toContain("not found");
|
||
});
|
||
|
||
it("sysroot 为 / 时不采纳(根目录无意义)", async () => {
|
||
const exec = makeMockExec([
|
||
{
|
||
match: (c, a) => c === "arm-none-eabi-gcc" && a.includes("--version"),
|
||
result: ok("arm-none-eabi-gcc (15:12.2) 12.2.0"),
|
||
},
|
||
{
|
||
match: (c, a) => c === "arm-none-eabi-gcc" && a.includes("-print-sysroot"),
|
||
result: ok("/"),
|
||
},
|
||
{ match: (c) => c === "which", result: ok("/usr/bin/arm-none-eabi-gcc") },
|
||
]);
|
||
const avail = await detectToolchain(exec, "arm-none-eabi");
|
||
expect(avail.available).toBe(true);
|
||
expect(avail.sysroot).toBeUndefined();
|
||
});
|
||
});
|
||
|
||
// ===========================================================================
|
||
// detectHostGcc (tool_call 钩子纯逻辑,fail-closed)
|
||
// ===========================================================================
|
||
|
||
describe("cross-compile detectHostGcc — 宿主 gcc 拦截 (fail-closed)", () => {
|
||
it("裸 gcc 被拦截", () => {
|
||
const d = detectHostGcc("gcc main.c -o main");
|
||
expect(d.block).toBe(true);
|
||
expect(d.compiler).toBe("gcc");
|
||
expect(d.reason).toContain("xcompile");
|
||
});
|
||
|
||
it("裸 g++ 被拦截", () => {
|
||
const d = detectHostGcc("g++ main.cpp -o main");
|
||
expect(d.block).toBe(true);
|
||
expect(d.compiler).toBe("g++");
|
||
});
|
||
|
||
it("裸 cc 被拦截", () => {
|
||
const d = detectHostGcc("cc -O2 main.c -o main");
|
||
expect(d.block).toBe(true);
|
||
});
|
||
|
||
it("aarch64-linux-gnu-gcc 不被拦截(前缀白名单)", () => {
|
||
const d = detectHostGcc("aarch64-linux-gnu-gcc main.c -o main");
|
||
expect(d.block).toBe(false);
|
||
});
|
||
|
||
it("arm-none-eabi-g++ 不被拦截", () => {
|
||
const d = detectHostGcc("arm-none-eabi-g++ -c main.cpp -o main.o");
|
||
expect(d.block).toBe(false);
|
||
});
|
||
|
||
it("CMAKE_C_COMPILER=gcc 透传被拦截", () => {
|
||
const d = detectHostGcc("cmake -DCMAKE_C_COMPILER=gcc ..");
|
||
expect(d.block).toBe(true);
|
||
expect(d.compiler).toBe("gcc");
|
||
});
|
||
|
||
it("/usr/bin/gcc 被拦截(绝对路径宿主 gcc)", () => {
|
||
const d = detectHostGcc("/usr/bin/gcc main.c -o main");
|
||
expect(d.block).toBe(true);
|
||
});
|
||
|
||
it("安全命令不拦截", () => {
|
||
for (const cmd of [
|
||
"aarch64-linux-gnu-gcc -o app main.c",
|
||
"cmake --build .",
|
||
"make -j4",
|
||
"ls -la",
|
||
"cat /sys/class/gpio/gpio33/value",
|
||
]) {
|
||
expect(detectHostGcc(cmd).block).toBe(false);
|
||
}
|
||
});
|
||
|
||
it("空命令不拦截", () => {
|
||
expect(detectHostGcc("").block).toBe(false);
|
||
expect(detectHostGcc(" ").block).toBe(false);
|
||
});
|
||
});
|
||
|
||
// ===========================================================================
|
||
// parseFileOutput / verifyArtifact
|
||
// ===========================================================================
|
||
|
||
describe("cross-compile parseFileOutput — 架构校验", () => {
|
||
it("x86-64 产物 vs 期望 aarch64 → mismatch", () => {
|
||
const r = parseFileOutput(
|
||
"main: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked",
|
||
"aarch64",
|
||
);
|
||
expect(r.match).toBe(false);
|
||
expect(r.detected).toBe("x86-64");
|
||
expect(r.error).toContain("mismatch");
|
||
expect(r.error).toContain("aarch64");
|
||
expect(r.error).toContain("x86-64");
|
||
});
|
||
|
||
it("aarch64 产物 vs 期望 aarch64 → match", () => {
|
||
const r = parseFileOutput(
|
||
"main: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV)",
|
||
"aarch64",
|
||
);
|
||
expect(r.match).toBe(true);
|
||
expect(r.detected).toBe("aarch64");
|
||
expect(r.error).toBeUndefined();
|
||
});
|
||
|
||
it("ARM bare-metal 产物 vs 期望 ARM → match", () => {
|
||
const r = parseFileOutput(
|
||
"firmware.elf: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV)",
|
||
"ARM",
|
||
);
|
||
expect(r.match).toBe(true);
|
||
expect(r.detected).toBe("ARM");
|
||
});
|
||
|
||
it("无 ELF 字样 → 解析失败 (fail-closed)", () => {
|
||
const r = parseFileOutput("main: data", "aarch64");
|
||
expect(r.match).toBe(false);
|
||
expect(r.error).toContain("could not parse");
|
||
});
|
||
});
|
||
|
||
describe("cross-compile verifyArtifact — mock ExecFn", () => {
|
||
it("file 返回 x86-64 → mismatch", async () => {
|
||
const exec = makeMockExec([
|
||
{
|
||
match: (c) => c === "file",
|
||
result: ok("main: ELF 64-bit LSB executable, x86-64, version 1 (SYSV)"),
|
||
},
|
||
]);
|
||
const r = await verifyArtifact(exec, "/tmp/main", "aarch64");
|
||
expect(r.match).toBe(false);
|
||
expect(r.detected).toBe("x86-64");
|
||
});
|
||
|
||
it("file 命令失败 → fail-closed 报错", async () => {
|
||
const exec = makeMockExec([
|
||
{ match: (c) => c === "file", result: fail("file: /tmp/main: No such file", 1) },
|
||
]);
|
||
const r = await verifyArtifact(exec, "/tmp/main", "aarch64");
|
||
expect(r.match).toBe(false);
|
||
expect(r.error).toContain("file command failed");
|
||
});
|
||
});
|
||
|
||
// ===========================================================================
|
||
// ToolchainSession — 状态跨 tool call 保留
|
||
// ===========================================================================
|
||
|
||
describe("cross-compile ToolchainSession — 状态保留", () => {
|
||
it("select 后 getCurrent 返回所选 triple", () => {
|
||
const session = new ToolchainSession();
|
||
const tc = session.select("aarch64-linux-gnu");
|
||
expect(tc.gcc).toBe("aarch64-linux-gnu-gcc");
|
||
expect(session.getCurrent()).toBe("aarch64-linux-gnu");
|
||
});
|
||
|
||
it("切换 triple 后 info 重置", () => {
|
||
const session = new ToolchainSession();
|
||
session.select("aarch64-linux-gnu");
|
||
expect(session.getInfo()).toBeNull();
|
||
session.select("arm-none-eabi");
|
||
expect(session.getCurrent()).toBe("arm-none-eabi");
|
||
expect(session.getInfo()).toBeNull();
|
||
});
|
||
|
||
it("detect 后 info 缓存,再次调用不重复探测", async () => {
|
||
let callCount = 0;
|
||
const exec: ExecFn = async (cmd, args) => {
|
||
callCount++;
|
||
if (cmd === "aarch64-linux-gnu-gcc" && args.includes("--version"))
|
||
return ok("gcc 12.2.0");
|
||
if (cmd === "aarch64-linux-gnu-gcc" && args.includes("-print-sysroot"))
|
||
return ok("/sysroot");
|
||
if (cmd === "which") return ok("/usr/bin/aarch64-linux-gnu-gcc");
|
||
return fail("nope", 127);
|
||
};
|
||
const session = new ToolchainSession();
|
||
session.select("aarch64-linux-gnu");
|
||
const a = await session.detect(exec);
|
||
expect(a.available).toBe(true);
|
||
const firstCount = callCount;
|
||
const b = await session.detect(exec); // 应走缓存
|
||
expect(b).toBe(a);
|
||
expect(callCount).toBe(firstCount);
|
||
});
|
||
|
||
it("未 select 就 detect → fail-closed 报错", async () => {
|
||
const session = new ToolchainSession();
|
||
const exec: ExecFn = async () => ok("");
|
||
const a = await session.detect(exec);
|
||
expect(a.available).toBe(false);
|
||
expect(a.error).toContain("no triple");
|
||
});
|
||
|
||
it("getResolved 返回当前 toolchain(已 select)", () => {
|
||
const session = new ToolchainSession();
|
||
session.select("arm-none-eabi");
|
||
const tc = session.getResolved();
|
||
expect(tc?.prefix).toBe("arm-none-eabi-");
|
||
});
|
||
|
||
it("getResolved 未 select 返回 null", () => {
|
||
const session = new ToolchainSession();
|
||
expect(session.getResolved()).toBeNull();
|
||
});
|
||
|
||
it("reset 清空状态", () => {
|
||
const session = new ToolchainSession();
|
||
session.select("aarch64-linux-gnu");
|
||
session.reset();
|
||
expect(session.getCurrent()).toBeNull();
|
||
expect(session.getResolved()).toBeNull();
|
||
});
|
||
});
|