Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 32 additions & 24 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Telegraf } from "telegraf";
import { Telegraf, Context } from "telegraf";
import { Message } from "telegraf/types";
import {
ConfigChatType,
Expand All @@ -19,33 +19,41 @@ import { forgetHistory } from "./helpers/history.ts";
import { commandGoogleOauth } from "./helpers/google.ts";
import useTools from "./helpers/useTools.ts";

export async function handleForget(ctx: Context) {
forgetHistory(ctx.chat!.id);
return await sendTelegramMessage(ctx.chat!.id, "OK", undefined, ctx);
}

export async function handleInfo(ctx: Context) {
const { msg, chat }: { msg?: Message.TextMessage; chat?: ConfigChatType } =
getCtxChatMsg(ctx);
if (!chat || !msg) return;
const answer = await getInfoMessage(msg, chat);
return sendTelegramMessage(ctx.chat!.id, answer, undefined, ctx);
}

export async function handleGoogleAuth(ctx: Context) {
const { msg, chat }: { msg?: Message.TextMessage; chat?: ConfigChatType } =
getCtxChatMsg(ctx);
if (!chat || !msg) return;
await commandGoogleOauth(msg);
}

export async function handleAddTool(ctx: Context) {
const { msg, chat }: { msg?: Message.TextMessage; chat?: ConfigChatType } =
getCtxChatMsg(ctx);
if (!chat || !msg) return;
await commandAddTool(msg, chat);
}

export async function initCommands(bot: Telegraf) {
bot.command("forget", async (ctx) => {
forgetHistory(ctx.chat.id);
return await sendTelegramMessage(ctx.chat.id, "OK", undefined, ctx);
});
bot.command("forget", handleForget);

bot.command("info", async (ctx) => {
const { msg, chat }: { msg?: Message.TextMessage; chat?: ConfigChatType } =
getCtxChatMsg(ctx);
if (!chat || !msg) return;
const answer = await getInfoMessage(msg, chat);
return sendTelegramMessage(ctx.chat.id, answer, undefined, ctx);
});
bot.command("info", handleInfo);

bot.command("google_auth", async (ctx) => {
const { msg, chat }: { msg?: Message.TextMessage; chat?: ConfigChatType } =
getCtxChatMsg(ctx);
if (!chat || !msg) return;
await commandGoogleOauth(msg);
});
bot.command("google_auth", handleGoogleAuth);

bot.command("add_tool", async (ctx) => {
const { msg, chat }: { msg?: Message.TextMessage; chat?: ConfigChatType } =
getCtxChatMsg(ctx);
if (!chat || !msg) return;
await commandAddTool(msg, chat);
});
bot.command("add_tool", handleAddTool);

await bot.telegram.setMyCommands([
{
Expand Down
87 changes: 83 additions & 4 deletions tests/commands.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { jest, describe, it, expect, beforeEach } from "@jest/globals";
import type { Message } from "telegraf/types";
import type { Context } from "telegraf";
import type { ConfigChatType } from "../src/types";

const mockUseTools = jest.fn();
Expand All @@ -11,6 +12,9 @@ const mockGetActionUserMsg = jest.fn();
const mockGetSystemMessage = jest.fn();
const mockGetTokensCount = jest.fn();
const mockResolveChatTools = jest.fn();
const mockForgetHistory = jest.fn();
const mockCommandGoogleOauth = jest.fn();
const mockGetCtxChatMsg = jest.fn();

jest.unstable_mockModule("../src/helpers/useTools.ts", () => ({
__esModule: true,
Expand All @@ -27,9 +31,11 @@ jest.unstable_mockModule("../src/telegram/send.ts", () => ({
}));

let actionCb: (ctx: unknown) => Promise<void>;
const mockAction = jest.fn((name: string, cb: (ctx: unknown) => Promise<void>) => {
actionCb = cb;
});
const mockAction = jest.fn(
(name: string, cb: (ctx: unknown) => Promise<void>) => {
actionCb = cb;
},
);

jest.unstable_mockModule("../src/bot", () => ({
__esModule: true,
Expand All @@ -48,7 +54,7 @@ jest.unstable_mockModule("../src/config.ts", () => ({
jest.unstable_mockModule("../src/telegram/context.ts", () => ({
__esModule: true,
getActionUserMsg: () => mockGetActionUserMsg(),
getCtxChatMsg: jest.fn(),
getCtxChatMsg: (...args: unknown[]) => mockGetCtxChatMsg(...args),
}));

jest.unstable_mockModule("../src/helpers/gpt.ts", () => ({
Expand All @@ -58,6 +64,16 @@ jest.unstable_mockModule("../src/helpers/gpt.ts", () => ({
resolveChatTools: (...args: unknown[]) => mockResolveChatTools(...args),
}));

jest.unstable_mockModule("../src/helpers/history.ts", () => ({
__esModule: true,
forgetHistory: (...args: unknown[]) => mockForgetHistory(...args),
}));

jest.unstable_mockModule("../src/helpers/google.ts", () => ({
__esModule: true,
commandGoogleOauth: (...args: unknown[]) => mockCommandGoogleOauth(...args),
}));

// for getInfoMessage internal call

let commands: typeof import("../src/commands.ts");
Expand Down Expand Up @@ -212,3 +228,66 @@ describe("getInfoMessage", () => {
expect(res).toContain("Настройки приватного режима");
});
});

describe("handleForget", () => {
it("forgets history and sends ok", async () => {
const ctx = { chat: { id: 1 } } as unknown as Context;
mockSendTelegramMessage.mockResolvedValue("ok");
await commands.handleForget(ctx);
expect(mockForgetHistory).toHaveBeenCalledWith(1);
expect(mockSendTelegramMessage).toHaveBeenCalledWith(
1,
"OK",
undefined,
ctx,
);
});
});

describe("handleInfo", () => {
it("sends info message", async () => {
const ctx = { chat: { id: 1 } } as unknown as Context;
const msg = createMsg();
const chat = {
completionParams: {},
chatParams: {},
toolParams: {},
} as ConfigChatType;
mockGetCtxChatMsg.mockReturnValue({ msg, chat });
mockSendTelegramMessage.mockResolvedValue("ok");
const expected = await commands.getInfoMessage(msg, chat);
await commands.handleInfo(ctx);
expect(mockSendTelegramMessage).toHaveBeenCalledWith(
1,
expected,
undefined,
ctx,
);
});
});

describe("handleGoogleAuth", () => {
it("calls oauth when data present", async () => {
const ctx = { chat: { id: 1 } } as unknown as Context;
const msg = createMsg();
const chat = {} as ConfigChatType;
mockGetCtxChatMsg.mockReturnValue({ msg, chat });
await commands.handleGoogleAuth(ctx);
expect(mockCommandGoogleOauth).toHaveBeenCalledWith(msg);
});
});

describe("handleAddTool", () => {
it("delegates to commandAddTool", async () => {
const ctx = { chat: { id: 1 } } as unknown as Context;
const msg = createMsg();
const chat = {
completionParams: {},
chatParams: {},
toolParams: {},
} as ConfigChatType;
mockGetCtxChatMsg.mockReturnValue({ msg, chat });
await commands.handleAddTool(ctx);
expect(mockSendTelegramMessage).toHaveBeenCalled();
});
});