Spracherkennung für: .ts vermutete Sprache: Unknown {[0] [0] [0]} [Methode: Schwerpunktbildung, einfache Gewichte, sechs Dimensionen]
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { ChannelRuntimeSnapshot } from "../server-channel-runtime.types.js";
import type { GatewayRequestHandlerOptions } from "./types.js";
const mocks = vi.hoisted(() => ({
loadConfig: vi.fn(() => ({})),
applyPluginAutoEnable: vi.fn(),
getChannelPlugin: vi.fn(),
}));
vi.mock("../../config/config.js", () => ({
loadConfig: mocks.loadConfig,
readConfigFileSnapshot: vi.fn(),
}));
vi.mock("../../config/plugin-auto-enable.js", () => ({
applyPluginAutoEnable: mocks.applyPluginAutoEnable,
}));
vi.mock("../../channels/plugins/index.js", () => ({
listChannelPlugins: vi.fn(),
getChannelPlugin: mocks.getChannelPlugin,
normalizeChannelId: (value: string) => value,
}));
import { channelsHandlers } from "./channels.js";
function createOptions(
params: Record<string, unknown>,
overrides?: Partial<GatewayRequestHandlerOptions>,
): GatewayRequestHandlerOptions {
return {
req: { type: "req", id: "req-1", method: "channels.start", params },
params,
client: null,
isWebchatConnect: () => false,
respond: vi.fn(),
context: {
startChannel: vi.fn(),
getRuntimeSnapshot: vi.fn(
(): ChannelRuntimeSnapshot => ({
channels: {
whatsapp: {
accountId: "default-account",
running: true,
},
},
channelAccounts: {
whatsapp: {
"default-account": {
accountId: "default-account",
running: true,
},
},
},
}),
),
},
...overrides,
} as unknown as GatewayRequestHandlerOptions;
}
describe("channelsHandlers channels.start", () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.loadConfig.mockReturnValue({});
mocks.applyPluginAutoEnable.mockImplementation(({ config }) => ({ config, changes: [] }));
mocks.getChannelPlugin.mockReturnValue({
id: "whatsapp",
gateway: { startAccount: vi.fn() },
config: {
defaultAccountId: () => "default-account",
listAccountIds: () => ["default-account"],
resolveAccount: () => ({}),
},
});
});
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: {
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"],
},
),
);
expect(mocks.applyPluginAutoEnable).toHaveBeenCalledWith({
config: {},
env: process.env,
});
expect(startChannel).toHaveBeenCalledWith("whatsapp", "default-account");
expect(respond).toHaveBeenCalledWith(
true,
{
channel: "whatsapp",
accountId: "default-account",
started: true,
},
undefined,
);
});
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: {
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"],
},
),
);
expect(startChannel).toHaveBeenCalledWith("whatsapp", "default-account");
expect(respond).toHaveBeenCalledWith(
true,
{
channel: "whatsapp",
accountId: "default-account",
started: false,
},
undefined,
);
});
});
¤ Dauer der Verarbeitung: 0.22 Sekunden
(vorverarbeitet am 2026-04-28)
¤
*© Formatika GbR, Deutschland
|
|