diff --git a/src/gateway/server-methods/channels.start.test.ts b/src/gateway/server-methods/channels.start.test.ts index 125a8f77839a..132877b7965c 100644 --- a/src/gateway/server-methods/channels.start.test.ts +++ b/src/gateway/server-methods/channels.start.test.ts @@ -26,6 +26,25 @@ vi.mock("../../channels/plugins/index.js", () => ({ import { channelsHandlers } from "./channels.js"; +function createChannelRuntimeSnapshot(running: boolean): ChannelRuntimeSnapshot { + return { + channels: { + whatsapp: { + accountId: "default-account", + running, + }, + }, + channelAccounts: { + whatsapp: { + "default-account": { + accountId: "default-account", + running, + }, + }, + }, + }; +} + function createOptions( params: Record, overrides?: Partial, @@ -41,29 +60,33 @@ function createOptions( startChannel: vi.fn(), stopChannel: vi.fn(), markChannelLoggedOut: vi.fn(), - getRuntimeSnapshot: vi.fn( - (): ChannelRuntimeSnapshot => ({ - channels: { - whatsapp: { - accountId: "default-account", - running: true, - }, - }, - channelAccounts: { - whatsapp: { - "default-account": { - accountId: "default-account", - running: true, - }, - }, - }, - }), - ), + getRuntimeSnapshot: vi.fn(() => createChannelRuntimeSnapshot(true)), }, ...overrides, } as unknown as GatewayRequestHandlerOptions; } +async function runChannelsStart(running: boolean) { + const startChannel = vi.fn(); + const respond = vi.fn(); + + await channelsHandlers["channels.start"]( + createOptions( + { channel: "whatsapp" }, + { + respond, + context: { + getRuntimeConfig: mocks.getRuntimeConfig, + startChannel, + getRuntimeSnapshot: vi.fn(() => createChannelRuntimeSnapshot(running)), + } as unknown as GatewayRequestHandlerOptions["context"], + }, + ), + ); + + return { respond, startChannel }; +} + describe("channelsHandlers channels.start", () => { beforeEach(() => { vi.clearAllMocks(); @@ -81,39 +104,7 @@ describe("channelsHandlers channels.start", () => { }); it("resolves the default account and starts the channel runtime", async () => { - const startChannel = vi.fn(); - const respond = vi.fn(); - - await channelsHandlers["channels.start"]( - createOptions( - { channel: "whatsapp" }, - { - respond, - context: { - getRuntimeConfig: mocks.getRuntimeConfig, - startChannel, - getRuntimeSnapshot: vi.fn( - (): ChannelRuntimeSnapshot => ({ - channels: { - whatsapp: { - accountId: "default-account", - running: true, - }, - }, - channelAccounts: { - whatsapp: { - "default-account": { - accountId: "default-account", - running: true, - }, - }, - }, - }), - ), - } as unknown as GatewayRequestHandlerOptions["context"], - }, - ), - ); + const { respond, startChannel } = await runChannelsStart(true); expect(mocks.applyPluginAutoEnable).toHaveBeenCalledWith({ config: {}, @@ -131,39 +122,7 @@ describe("channelsHandlers channels.start", () => { }); it("reports started=false when the channel runtime remains stopped", async () => { - const startChannel = vi.fn(); - const respond = vi.fn(); - - await channelsHandlers["channels.start"]( - createOptions( - { channel: "whatsapp" }, - { - respond, - context: { - getRuntimeConfig: mocks.getRuntimeConfig, - startChannel, - getRuntimeSnapshot: vi.fn( - (): ChannelRuntimeSnapshot => ({ - channels: { - whatsapp: { - accountId: "default-account", - running: false, - }, - }, - channelAccounts: { - whatsapp: { - "default-account": { - accountId: "default-account", - running: false, - }, - }, - }, - }), - ), - } as unknown as GatewayRequestHandlerOptions["context"], - }, - ), - ); + const { respond, startChannel } = await runChannelsStart(false); expect(startChannel).toHaveBeenCalledWith("whatsapp", "default-account"); expect(respond).toHaveBeenCalledWith(