mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-03 03:03:24 -04:00
fix(cli): harden message helper exit flow
This commit is contained in:
@@ -78,6 +78,22 @@ describe("runMessageAction", () => {
|
||||
expect(exitMock).not.toHaveBeenCalledWith(0);
|
||||
});
|
||||
|
||||
it("does not call exit(0) if the error path returns", async () => {
|
||||
messageCommandMock.mockRejectedValueOnce(new Error("boom"));
|
||||
exitMock.mockReset().mockImplementation(() => undefined as never);
|
||||
const fakeCommand = { help: vi.fn() } as never;
|
||||
const { runMessageAction } = createMessageCliHelpers(fakeCommand, "discord");
|
||||
|
||||
await expect(
|
||||
runMessageAction("send", { channel: "discord", target: "123", message: "hi" }),
|
||||
).resolves.toBeUndefined();
|
||||
|
||||
expect(errorMock).toHaveBeenCalledWith("Error: boom");
|
||||
expect(exitMock).toHaveBeenCalledOnce();
|
||||
expect(exitMock).toHaveBeenCalledWith(1);
|
||||
expect(exitMock).not.toHaveBeenCalledWith(0);
|
||||
});
|
||||
|
||||
it("passes action and maps account to accountId", async () => {
|
||||
const fakeCommand = { help: vi.fn() } as never;
|
||||
const { runMessageAction } = createMessageCliHelpers(fakeCommand, "discord");
|
||||
|
||||
@@ -14,6 +14,14 @@ export type MessageCliHelpers = {
|
||||
runMessageAction: (action: string, opts: Record<string, unknown>) => Promise<void>;
|
||||
};
|
||||
|
||||
function normalizeMessageOptions(opts: Record<string, unknown>): Record<string, unknown> {
|
||||
const { account, ...rest } = opts;
|
||||
return {
|
||||
...rest,
|
||||
accountId: typeof account === "string" ? account : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
export function createMessageCliHelpers(
|
||||
message: Command,
|
||||
messageChannelOptions: string,
|
||||
@@ -35,18 +43,13 @@ export function createMessageCliHelpers(
|
||||
setVerbose(Boolean(opts.verbose));
|
||||
ensurePluginRegistryLoaded();
|
||||
const deps = createDefaultDeps();
|
||||
let failed = false;
|
||||
await runCommandWithRuntime(
|
||||
defaultRuntime,
|
||||
async () => {
|
||||
await messageCommand(
|
||||
{
|
||||
...(() => {
|
||||
const { account, ...rest } = opts;
|
||||
return {
|
||||
...rest,
|
||||
accountId: typeof account === "string" ? account : undefined,
|
||||
};
|
||||
})(),
|
||||
...normalizeMessageOptions(opts),
|
||||
action,
|
||||
},
|
||||
deps,
|
||||
@@ -54,10 +57,14 @@ export function createMessageCliHelpers(
|
||||
);
|
||||
},
|
||||
(err) => {
|
||||
failed = true;
|
||||
defaultRuntime.error(danger(String(err)));
|
||||
defaultRuntime.exit(1);
|
||||
},
|
||||
);
|
||||
if (failed) {
|
||||
return;
|
||||
}
|
||||
defaultRuntime.exit(0);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user