feat(hwpod): add local CLI execution mode
This commit is contained in:
@@ -36,6 +36,9 @@ if (argv[0] === "tasktree") {
|
||||
console.log(JSON.stringify({ ok: false, operation: "hwpod", error: { code: error?.code ?? "hwpod_cli_error", message: error?.message ?? String(error) } }, null, 2));
|
||||
process.exitCode = 1;
|
||||
}
|
||||
} else if (argv[0] === "hwpod") {
|
||||
const { mainHwpodCli } = await import("../../src/hwpod-harness-lib.ts");
|
||||
await mainHwpodCli(argv.slice(1));
|
||||
} else if (argv[0] === "workbench") {
|
||||
const { runWorkbenchCli } = await import("../../src/workbench-cli.ts");
|
||||
try {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
||||
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { test } from "bun:test";
|
||||
@@ -56,6 +56,69 @@ test("hwpod-cli workspace apply-patch accepts --patch-content alias", async () =
|
||||
}
|
||||
});
|
||||
|
||||
function hwpodSpecText() {
|
||||
return "kind: Hwpod\nmetadata:\n name: test-hwpod\nspec:\n nodeBinding:\n nodeId: test-node\n workspace:\n path: F:\\\\Work\\\\HWLAB-CASE-F103\n targetDevice:\n board: D601-F103-V2\n debugProbe:\n type: daplink\n ioProbe:\n uart:\n port: COM9\n";
|
||||
test("hwlab-cli hwpod L0 executes workspace edits and a bounded build helper", async () => {
|
||||
const root = await mkdtemp(path.join(os.tmpdir(), "hwpod-cli-l0-"));
|
||||
const specPath = path.join(root, "hwpod-spec.yaml");
|
||||
const sourcePath = path.join(root, "app.c");
|
||||
const artifactPath = path.join(root, "firmware.hex");
|
||||
const patch = "*** Begin Patch\n*** Update File: app.c\n@@\n-old\n+new\n*** End Patch\n";
|
||||
try {
|
||||
await writeFile(specPath, hwpodSpecText(root, "node build.mjs"), "utf8");
|
||||
await writeFile(sourcePath, "old\n", "utf8");
|
||||
await writeFile(path.join(root, "build.mjs"), "import { writeFileSync } from 'node:fs'; writeFileSync('firmware.hex', ':00000001FF\\n'); console.log('build completed');\n", "utf8");
|
||||
|
||||
const edited = await runHwpodCli(["workspace", "apply-patch", "--local", "--spec", specPath, "--patch-content", patch]);
|
||||
assert.equal(edited.exitCode, 0);
|
||||
assert.equal(edited.payload.action, "hwpod-cli.local");
|
||||
assert.equal(edited.payload.mode, "l0-native-function");
|
||||
assert.equal(edited.payload.transport, "native-function");
|
||||
assert.equal(await readFile(sourcePath, "utf8"), "new\n");
|
||||
|
||||
const listed = await runHwpodCli(["workspace", "ls", ".", "--local", "--spec", specPath]);
|
||||
assert.equal(listed.payload.body.results[0].output.entries.some((item: any) => item.name === "app.c"), true);
|
||||
|
||||
const searched = await runHwpodCli(["workspace", "rg", "new", ".", "--local", "--spec", specPath]);
|
||||
assert.equal(searched.payload.body.results[0].output.matchCount, 1);
|
||||
|
||||
const written = await runHwpodCli(["workspace", "write", "generated.txt", "--content", "alpha\n", "--local", "--spec", specPath]);
|
||||
assert.equal(written.exitCode, 0);
|
||||
assert.equal(await readFile(path.join(root, "generated.txt"), "utf8"), "alpha\n");
|
||||
|
||||
const replaced = await runHwpodCli(["workspace", "replace", "app.c", "--find", "new", "--replace", "newer", "--local", "--spec", specPath]);
|
||||
assert.equal(replaced.exitCode, 0);
|
||||
const inserted = await runHwpodCli(["workspace", "insert-after", "app.c", "--anchor", "newer", "--line", "marker", "--local", "--spec", specPath]);
|
||||
assert.equal(inserted.exitCode, 0);
|
||||
|
||||
const read = await runHwpodCli(["workspace", "cat", "app.c", "--local", "--spec", specPath]);
|
||||
assert.equal(read.payload.body.results[0].output.content, "newer\nmarker\n");
|
||||
|
||||
const child = Bun.spawn([
|
||||
process.execPath,
|
||||
path.join(process.cwd(), "tools/hwlab-cli/bin/hwlab-cli.ts"),
|
||||
"hwpod",
|
||||
"build",
|
||||
"--local",
|
||||
"--spec",
|
||||
specPath,
|
||||
], { cwd: process.cwd(), stdout: "pipe", stderr: "pipe" });
|
||||
const [stdout, stderr, exitCode] = await Promise.all([new Response(child.stdout).text(), new Response(child.stderr).text(), child.exited]);
|
||||
assert.equal(exitCode, 0, stderr);
|
||||
const built = JSON.parse(stdout);
|
||||
assert.equal(built.action, "hwpod-cli.local");
|
||||
assert.equal(built.mode, "l0-native-function");
|
||||
assert.match(built.body.results[0].output.stdout, /build completed/u);
|
||||
assert.equal(await readFile(artifactPath, "utf8"), ":00000001FF\n");
|
||||
} finally {
|
||||
await rm(root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("HWPOD L0 local mode fails closed without an explicit spec", async () => {
|
||||
const result = await runHwpodCli(["workspace", "ls", ".", "--local"]);
|
||||
assert.equal(result.exitCode, 1);
|
||||
assert.equal(result.payload.error.code, "hwpod_l0_spec_required");
|
||||
});
|
||||
|
||||
function hwpodSpecText(workspacePath = "F:\\Work\\HWLAB-CASE-F103", buildCommand = "") {
|
||||
return `kind: Hwpod\nmetadata:\n name: test-hwpod\nspec:\n nodeBinding:\n nodeId: test-node\n workspace:\n path: ${JSON.stringify(workspacePath)}\n${buildCommand ? ` buildCommand: ${JSON.stringify(buildCommand)}\n` : ""} targetDevice:\n board: D601-F103-V2\n debugProbe:\n type: daplink\n ioProbe:\n uart:\n port: COM9\n`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user