fix(cli): harden message helper exit flow

This commit is contained in:
Gustavo Madeira Santana
2026-02-14 16:47:04 -05:00
parent 3a7905c4df
commit 78dffc9e99
2 changed files with 30 additions and 7 deletions

View File

@@ -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");

View File

@@ -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);
};