Files
openclaw-zero-token/cli/system-cli.test.ts
sjhu 571e14a236 feat: upgrade to upstream v2026.3.28
Major upgrade from e26988a38 to upstream v2026.3.28 (f9b107928).
Key changes:
- Upstream src/, ui/, extensions/ (89 bundled extensions)
- Zero-token web providers preserved in src/zero-token/
- AskOnce plugin restored and registered as CLI command
- Added missing packages: @anthropic-ai/vertex-sdk, @modelcontextprotocol/sdk
- Fixed tsconfig rootDir, skipLibCheck for plugin-sdk DTS build
- Added askonce to bundled plugin metadata and package.json exports
- Fixed AskOnce CLI command registration (missing commands metadata)
- Restored AskOnce adapter imports (correct 5-level relative paths)
- Removed stale migration artifacts from root directory
2026-03-30 17:58:12 +08:00

95 lines
3.0 KiB
TypeScript

import { Command } from "commander";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createCliRuntimeCapture } from "./test-runtime-capture.js";
const callGatewayFromCli = vi.fn();
const addGatewayClientOptions = vi.fn((command: Command) => command);
const { runtimeLogs, runtimeErrors, defaultRuntime, resetRuntimeCapture } =
createCliRuntimeCapture();
vi.mock("./gateway-rpc.js", () => ({
addGatewayClientOptions,
callGatewayFromCli,
}));
vi.mock("../runtime.js", async (importOriginal) => ({
...(await importOriginal<typeof import("../runtime.js")>()),
defaultRuntime,
writeRuntimeJson: (runtime: { log: (...args: unknown[]) => void }, value: unknown, space = 2) =>
runtime.log(JSON.stringify(value, null, space > 0 ? space : undefined)),
}));
const { registerSystemCli } = await import("./system-cli.js");
describe("system-cli", () => {
async function runCli(args: string[]) {
const program = new Command();
registerSystemCli(program);
try {
await program.parseAsync(args, { from: "user" });
} catch (err) {
if (!(err instanceof Error && err.message.startsWith("__exit__:"))) {
throw err;
}
}
}
beforeEach(() => {
vi.clearAllMocks();
resetRuntimeCapture();
callGatewayFromCli.mockResolvedValue({ ok: true });
});
it("runs system event with default wake mode and text output", async () => {
await runCli(["system", "event", "--text", " hello world "]);
expect(callGatewayFromCli).toHaveBeenCalledWith(
"wake",
expect.objectContaining({ text: " hello world " }),
{ mode: "next-heartbeat", text: "hello world" },
{ expectFinal: false },
);
expect(runtimeLogs).toEqual(["ok"]);
});
it("prints JSON for event when --json is enabled", async () => {
callGatewayFromCli.mockResolvedValueOnce({ id: "wake-1" });
await runCli(["system", "event", "--text", "hello", "--json"]);
expect(runtimeLogs).toEqual([JSON.stringify({ id: "wake-1" }, null, 2)]);
});
it("handles invalid wake mode as runtime error", async () => {
await runCli(["system", "event", "--text", "hello", "--mode", "later"]);
expect(callGatewayFromCli).not.toHaveBeenCalled();
expect(runtimeErrors[0]).toContain("--mode must be now or next-heartbeat");
});
it.each([
{ args: ["system", "heartbeat", "last"], method: "last-heartbeat", params: undefined },
{
args: ["system", "heartbeat", "enable"],
method: "set-heartbeats",
params: { enabled: true },
},
{
args: ["system", "heartbeat", "disable"],
method: "set-heartbeats",
params: { enabled: false },
},
{ args: ["system", "presence"], method: "system-presence", params: undefined },
])("routes $args to gateway", async ({ args, method, params }) => {
callGatewayFromCli.mockResolvedValueOnce({ method });
await runCli(args);
expect(callGatewayFromCli).toHaveBeenCalledWith(method, expect.any(Object), params, {
expectFinal: false,
});
expect(runtimeLogs).toEqual([JSON.stringify({ method }, null, 2)]);
});
});