mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-13 19:10:39 +00:00
fix(cli): run plugin gateway_stop hooks before message exit (#16580)
Merged via /review-pr -> /prepare-pr -> /merge-pr.
Prepared head SHA: 8542ac77ae
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
This commit is contained in:
committed by
GitHub
parent
3821d74019
commit
8217d77ece
@@ -14,6 +14,28 @@ vi.mock("../../plugin-registry.js", () => ({
|
||||
ensurePluginRegistryLoaded: vi.fn(),
|
||||
}));
|
||||
|
||||
const hasHooksMock = vi.fn(() => false);
|
||||
const runGatewayStopMock = vi.fn(async () => {});
|
||||
const runGlobalGatewayStopSafelyMock = vi.fn(
|
||||
async (params: {
|
||||
event: { reason?: string };
|
||||
ctx: Record<string, unknown>;
|
||||
onError?: (err: unknown) => void;
|
||||
}) => {
|
||||
if (!hasHooksMock("gateway_stop")) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await runGatewayStopMock(params.event, params.ctx);
|
||||
} catch (err) {
|
||||
params.onError?.(err);
|
||||
}
|
||||
},
|
||||
);
|
||||
vi.mock("../../../plugins/hook-runner-global.js", () => ({
|
||||
runGlobalGatewayStopSafely: (...args: unknown[]) => runGlobalGatewayStopSafelyMock(...args),
|
||||
}));
|
||||
|
||||
const exitMock = vi.fn((): never => {
|
||||
throw new Error("exit");
|
||||
});
|
||||
@@ -33,6 +55,9 @@ describe("runMessageAction", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
messageCommandMock.mockReset().mockResolvedValue(undefined);
|
||||
hasHooksMock.mockReset().mockReturnValue(false);
|
||||
runGatewayStopMock.mockReset().mockResolvedValue(undefined);
|
||||
runGlobalGatewayStopSafelyMock.mockClear();
|
||||
exitMock.mockReset().mockImplementation((): never => {
|
||||
throw new Error("exit");
|
||||
});
|
||||
@@ -50,6 +75,19 @@ describe("runMessageAction", () => {
|
||||
expect(exitMock).toHaveBeenCalledWith(0);
|
||||
});
|
||||
|
||||
it("runs gateway_stop hooks before exit when registered", async () => {
|
||||
hasHooksMock.mockReturnValueOnce(true);
|
||||
const fakeCommand = { help: vi.fn() } as never;
|
||||
const { runMessageAction } = createMessageCliHelpers(fakeCommand, "discord");
|
||||
|
||||
await expect(
|
||||
runMessageAction("send", { channel: "discord", target: "123", message: "hi" }),
|
||||
).rejects.toThrow("exit");
|
||||
|
||||
expect(runGatewayStopMock).toHaveBeenCalledWith({ reason: "cli message action complete" }, {});
|
||||
expect(exitMock).toHaveBeenCalledWith(0);
|
||||
});
|
||||
|
||||
it("calls exit(1) when message delivery fails", async () => {
|
||||
messageCommandMock.mockRejectedValueOnce(new Error("send failed"));
|
||||
const fakeCommand = { help: vi.fn() } as never;
|
||||
@@ -64,6 +102,50 @@ describe("runMessageAction", () => {
|
||||
expect(exitMock).toHaveBeenCalledWith(1);
|
||||
});
|
||||
|
||||
it("runs gateway_stop hooks on failure before exit(1)", async () => {
|
||||
hasHooksMock.mockReturnValueOnce(true);
|
||||
messageCommandMock.mockRejectedValueOnce(new Error("send failed"));
|
||||
const fakeCommand = { help: vi.fn() } as never;
|
||||
const { runMessageAction } = createMessageCliHelpers(fakeCommand, "discord");
|
||||
|
||||
await expect(
|
||||
runMessageAction("send", { channel: "discord", target: "123", message: "hi" }),
|
||||
).rejects.toThrow("exit");
|
||||
|
||||
expect(runGatewayStopMock).toHaveBeenCalledWith({ reason: "cli message action complete" }, {});
|
||||
expect(exitMock).toHaveBeenCalledWith(1);
|
||||
});
|
||||
|
||||
it("logs gateway_stop failure and still exits with success code", async () => {
|
||||
hasHooksMock.mockReturnValueOnce(true);
|
||||
runGatewayStopMock.mockRejectedValueOnce(new Error("hook failed"));
|
||||
const fakeCommand = { help: vi.fn() } as never;
|
||||
const { runMessageAction } = createMessageCliHelpers(fakeCommand, "discord");
|
||||
|
||||
await expect(
|
||||
runMessageAction("send", { channel: "discord", target: "123", message: "hi" }),
|
||||
).rejects.toThrow("exit");
|
||||
|
||||
expect(errorMock).toHaveBeenCalledWith("gateway_stop hook failed: Error: hook failed");
|
||||
expect(exitMock).toHaveBeenCalledWith(0);
|
||||
});
|
||||
|
||||
it("logs gateway_stop failure and preserves failure exit code when send fails", async () => {
|
||||
hasHooksMock.mockReturnValueOnce(true);
|
||||
messageCommandMock.mockRejectedValueOnce(new Error("send failed"));
|
||||
runGatewayStopMock.mockRejectedValueOnce(new Error("hook failed"));
|
||||
const fakeCommand = { help: vi.fn() } as never;
|
||||
const { runMessageAction } = createMessageCliHelpers(fakeCommand, "discord");
|
||||
|
||||
await expect(
|
||||
runMessageAction("send", { channel: "discord", target: "123", message: "hi" }),
|
||||
).rejects.toThrow("exit");
|
||||
|
||||
expect(errorMock).toHaveBeenNthCalledWith(1, "Error: send failed");
|
||||
expect(errorMock).toHaveBeenNthCalledWith(2, "gateway_stop hook failed: Error: hook failed");
|
||||
expect(exitMock).toHaveBeenCalledWith(1);
|
||||
});
|
||||
|
||||
it("does not call exit(0) when the action throws", async () => {
|
||||
messageCommandMock.mockRejectedValueOnce(new Error("boom"));
|
||||
const fakeCommand = { help: vi.fn() } as never;
|
||||
|
||||
@@ -2,6 +2,7 @@ import type { Command } from "commander";
|
||||
import { messageCommand } from "../../../commands/message.js";
|
||||
import { danger, setVerbose } from "../../../globals.js";
|
||||
import { CHANNEL_TARGET_DESCRIPTION } from "../../../infra/outbound/channel-target.js";
|
||||
import { runGlobalGatewayStopSafely } from "../../../plugins/hook-runner-global.js";
|
||||
import { defaultRuntime } from "../../../runtime.js";
|
||||
import { runCommandWithRuntime } from "../../cli-utils.js";
|
||||
import { createDefaultDeps } from "../../deps.js";
|
||||
@@ -22,6 +23,14 @@ function normalizeMessageOptions(opts: Record<string, unknown>): Record<string,
|
||||
};
|
||||
}
|
||||
|
||||
async function runPluginStopHooks(): Promise<void> {
|
||||
await runGlobalGatewayStopSafely({
|
||||
event: { reason: "cli message action complete" },
|
||||
ctx: {},
|
||||
onError: (err) => defaultRuntime.error(danger(`gateway_stop hook failed: ${String(err)}`)),
|
||||
});
|
||||
}
|
||||
|
||||
export function createMessageCliHelpers(
|
||||
message: Command,
|
||||
messageChannelOptions: string,
|
||||
@@ -59,13 +68,10 @@ export function createMessageCliHelpers(
|
||||
(err) => {
|
||||
failed = true;
|
||||
defaultRuntime.error(danger(String(err)));
|
||||
defaultRuntime.exit(1);
|
||||
},
|
||||
);
|
||||
if (failed) {
|
||||
return;
|
||||
}
|
||||
defaultRuntime.exit(0);
|
||||
await runPluginStopHooks();
|
||||
defaultRuntime.exit(failed ? 1 : 0);
|
||||
};
|
||||
|
||||
// `message` is only used for `message.help({ error: true })`, keep the
|
||||
|
||||
Reference in New Issue
Block a user