fix(cli): run plugin gateway_stop hooks before message exit (#16580)
Merged via /review-pr -> /prepare-pr -> /merge-pr. Prepared head SHA: 8542ac77ae183e19a0700c3bb0304ab06bb7d568 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;
|
||||
|
||||
Reference in New Issue
Block a user