mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 15:54:32 +00:00
refactor(test): share telegram create bot harness
This commit is contained in:
@@ -1,163 +1,22 @@
|
|||||||
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||||
import { escapeRegExp, formatEnvelopeTimestamp } from "../../test/helpers/envelope-timestamp.js";
|
import { escapeRegExp, formatEnvelopeTimestamp } from "../../test/helpers/envelope-timestamp.js";
|
||||||
import { resetInboundDedupe } from "../auto-reply/reply/inbound-dedupe.js";
|
import {
|
||||||
|
getOnHandler,
|
||||||
|
getLoadConfigMock,
|
||||||
|
onSpy,
|
||||||
|
replySpy,
|
||||||
|
sendMessageSpy,
|
||||||
|
setMessageReactionSpy,
|
||||||
|
setMyCommandsSpy,
|
||||||
|
} from "./bot.create-telegram-bot.test-harness.js";
|
||||||
import { createTelegramBot } from "./bot.js";
|
import { createTelegramBot } from "./bot.js";
|
||||||
|
|
||||||
const { sessionStorePath } = vi.hoisted(() => ({
|
const loadConfig = getLoadConfigMock();
|
||||||
sessionStorePath: `/tmp/openclaw-telegram-${Math.random().toString(16).slice(2)}.json`,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const { loadWebMedia } = vi.hoisted(() => ({
|
|
||||||
loadWebMedia: vi.fn(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../web/media.js", () => ({
|
|
||||||
loadWebMedia,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const { loadConfig } = vi.hoisted(() => ({
|
|
||||||
loadConfig: vi.fn(() => ({})),
|
|
||||||
}));
|
|
||||||
vi.mock("../config/config.js", async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof import("../config/config.js")>();
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
loadConfig,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
vi.mock("../config/sessions.js", async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof import("../config/sessions.js")>();
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
resolveStorePath: vi.fn((storePath) => storePath ?? sessionStorePath),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
const { readChannelAllowFromStore, upsertChannelPairingRequest } = vi.hoisted(() => ({
|
|
||||||
readChannelAllowFromStore: vi.fn(async () => [] as string[]),
|
|
||||||
upsertChannelPairingRequest: vi.fn(async () => ({
|
|
||||||
code: "PAIRCODE",
|
|
||||||
created: true,
|
|
||||||
})),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../pairing/pairing-store.js", () => ({
|
|
||||||
readChannelAllowFromStore,
|
|
||||||
upsertChannelPairingRequest,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const useSpy = vi.fn();
|
|
||||||
const middlewareUseSpy = vi.fn();
|
|
||||||
const onSpy = vi.fn();
|
|
||||||
const stopSpy = vi.fn();
|
|
||||||
const commandSpy = vi.fn();
|
|
||||||
const botCtorSpy = vi.fn();
|
|
||||||
const answerCallbackQuerySpy = vi.fn(async () => undefined);
|
|
||||||
const sendChatActionSpy = vi.fn();
|
|
||||||
const setMessageReactionSpy = vi.fn(async () => undefined);
|
|
||||||
const setMyCommandsSpy = vi.fn(async () => undefined);
|
|
||||||
const sendMessageSpy = vi.fn(async () => ({ message_id: 77 }));
|
|
||||||
const sendAnimationSpy = vi.fn(async () => ({ message_id: 78 }));
|
|
||||||
const sendPhotoSpy = vi.fn(async () => ({ message_id: 79 }));
|
|
||||||
type ApiStub = {
|
|
||||||
config: { use: (arg: unknown) => void };
|
|
||||||
answerCallbackQuery: typeof answerCallbackQuerySpy;
|
|
||||||
sendChatAction: typeof sendChatActionSpy;
|
|
||||||
setMessageReaction: typeof setMessageReactionSpy;
|
|
||||||
setMyCommands: typeof setMyCommandsSpy;
|
|
||||||
sendMessage: typeof sendMessageSpy;
|
|
||||||
sendAnimation: typeof sendAnimationSpy;
|
|
||||||
sendPhoto: typeof sendPhotoSpy;
|
|
||||||
};
|
|
||||||
const apiStub: ApiStub = {
|
|
||||||
config: { use: useSpy },
|
|
||||||
answerCallbackQuery: answerCallbackQuerySpy,
|
|
||||||
sendChatAction: sendChatActionSpy,
|
|
||||||
setMessageReaction: setMessageReactionSpy,
|
|
||||||
setMyCommands: setMyCommandsSpy,
|
|
||||||
sendMessage: sendMessageSpy,
|
|
||||||
sendAnimation: sendAnimationSpy,
|
|
||||||
sendPhoto: sendPhotoSpy,
|
|
||||||
};
|
|
||||||
|
|
||||||
vi.mock("grammy", () => ({
|
|
||||||
Bot: class {
|
|
||||||
api = apiStub;
|
|
||||||
use = middlewareUseSpy;
|
|
||||||
on = onSpy;
|
|
||||||
stop = stopSpy;
|
|
||||||
command = commandSpy;
|
|
||||||
catch = vi.fn();
|
|
||||||
constructor(
|
|
||||||
public token: string,
|
|
||||||
public options?: { client?: { fetch?: typeof fetch } },
|
|
||||||
) {
|
|
||||||
botCtorSpy(token, options);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
InputFile: class {},
|
|
||||||
webhookCallback: vi.fn(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
const sequentializeMiddleware = vi.fn();
|
|
||||||
const sequentializeSpy = vi.fn(() => sequentializeMiddleware);
|
|
||||||
let _sequentializeKey: ((ctx: unknown) => string) | undefined;
|
|
||||||
vi.mock("@grammyjs/runner", () => ({
|
|
||||||
sequentialize: (keyFn: (ctx: unknown) => string) => {
|
|
||||||
_sequentializeKey = keyFn;
|
|
||||||
return sequentializeSpy();
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
|
|
||||||
const throttlerSpy = vi.fn(() => "throttler");
|
|
||||||
|
|
||||||
vi.mock("@grammyjs/transformer-throttler", () => ({
|
|
||||||
apiThrottler: () => throttlerSpy(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../auto-reply/reply.js", () => {
|
|
||||||
const replySpy = vi.fn(async (_ctx, opts) => {
|
|
||||||
await opts?.onReplyStart?.();
|
|
||||||
return undefined;
|
|
||||||
});
|
|
||||||
return { getReplyFromConfig: replySpy, __replySpy: replySpy };
|
|
||||||
});
|
|
||||||
|
|
||||||
let replyModule: typeof import("../auto-reply/reply.js");
|
|
||||||
|
|
||||||
const getOnHandler = (event: string) => {
|
|
||||||
const handler = onSpy.mock.calls.find((call) => call[0] === event)?.[1];
|
|
||||||
if (!handler) {
|
|
||||||
throw new Error(`Missing handler for event: ${event}`);
|
|
||||||
}
|
|
||||||
return handler as (ctx: Record<string, unknown>) => Promise<void>;
|
|
||||||
};
|
|
||||||
|
|
||||||
const ORIGINAL_TZ = process.env.TZ;
|
const ORIGINAL_TZ = process.env.TZ;
|
||||||
describe("createTelegramBot", () => {
|
describe("createTelegramBot", () => {
|
||||||
beforeAll(async () => {
|
|
||||||
replyModule = await import("../auto-reply/reply.js");
|
|
||||||
});
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
process.env.TZ = "UTC";
|
process.env.TZ = "UTC";
|
||||||
resetInboundDedupe();
|
|
||||||
loadConfig.mockReturnValue({
|
|
||||||
channels: {
|
|
||||||
telegram: { dmPolicy: "open", allowFrom: ["*"] },
|
|
||||||
},
|
|
||||||
});
|
|
||||||
loadWebMedia.mockReset();
|
|
||||||
sendAnimationSpy.mockReset();
|
|
||||||
sendPhotoSpy.mockReset();
|
|
||||||
setMessageReactionSpy.mockReset();
|
|
||||||
answerCallbackQuerySpy.mockReset();
|
|
||||||
setMyCommandsSpy.mockReset();
|
|
||||||
middlewareUseSpy.mockReset();
|
|
||||||
sequentializeSpy.mockReset();
|
|
||||||
botCtorSpy.mockReset();
|
|
||||||
_sequentializeKey = undefined;
|
|
||||||
});
|
});
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
process.env.TZ = ORIGINAL_TZ;
|
process.env.TZ = ORIGINAL_TZ;
|
||||||
@@ -167,7 +26,6 @@ describe("createTelegramBot", () => {
|
|||||||
|
|
||||||
it("accepts group messages when mentionPatterns match (without @botUsername)", async () => {
|
it("accepts group messages when mentionPatterns match (without @botUsername)", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
|
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
@@ -215,7 +73,6 @@ describe("createTelegramBot", () => {
|
|||||||
|
|
||||||
it("accepts group messages when mentionPatterns match even if another user is mentioned", async () => {
|
it("accepts group messages when mentionPatterns match even if another user is mentioned", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
|
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
@@ -256,7 +113,6 @@ describe("createTelegramBot", () => {
|
|||||||
|
|
||||||
it("keeps group envelope headers stable (sender identity is separate)", async () => {
|
it("keeps group envelope headers stable (sender identity is separate)", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
|
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
@@ -307,7 +163,6 @@ describe("createTelegramBot", () => {
|
|||||||
it("reacts to mention-gated group messages when ackReaction is enabled", async () => {
|
it("reacts to mention-gated group messages when ackReaction is enabled", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
setMessageReactionSpy.mockReset();
|
setMessageReactionSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
|
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
@@ -352,7 +207,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("skips group messages when requireMention is enabled and no mention matches", async () => {
|
it("skips group messages when requireMention is enabled and no mention matches", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
|
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
@@ -384,7 +238,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("allows group messages when requireMention is enabled but mentions cannot be detected", async () => {
|
it("allows group messages when requireMention is enabled but mentions cannot be detected", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
|
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
@@ -419,7 +272,6 @@ describe("createTelegramBot", () => {
|
|||||||
it("includes reply-to context when a Telegram reply is received", async () => {
|
it("includes reply-to context when a Telegram reply is received", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
sendMessageSpy.mockReset();
|
sendMessageSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
|
|
||||||
createTelegramBot({ token: "tok" });
|
createTelegramBot({ token: "tok" });
|
||||||
|
|||||||
@@ -1,167 +1,21 @@
|
|||||||
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { resetInboundDedupe } from "../auto-reply/reply/inbound-dedupe.js";
|
import {
|
||||||
|
commandSpy,
|
||||||
|
getOnHandler,
|
||||||
|
getLoadConfigMock,
|
||||||
|
onSpy,
|
||||||
|
replySpy,
|
||||||
|
sendMessageSpy,
|
||||||
|
} from "./bot.create-telegram-bot.test-harness.js";
|
||||||
import { createTelegramBot } from "./bot.js";
|
import { createTelegramBot } from "./bot.js";
|
||||||
|
|
||||||
const { sessionStorePath } = vi.hoisted(() => ({
|
const loadConfig = getLoadConfigMock();
|
||||||
sessionStorePath: `/tmp/openclaw-telegram-${Math.random().toString(16).slice(2)}.json`,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const { loadWebMedia } = vi.hoisted(() => ({
|
|
||||||
loadWebMedia: vi.fn(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../web/media.js", () => ({
|
|
||||||
loadWebMedia,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const { loadConfig } = vi.hoisted(() => ({
|
|
||||||
loadConfig: vi.fn(() => ({})),
|
|
||||||
}));
|
|
||||||
vi.mock("../config/config.js", async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof import("../config/config.js")>();
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
loadConfig,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
vi.mock("../config/sessions.js", async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof import("../config/sessions.js")>();
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
resolveStorePath: vi.fn((storePath) => storePath ?? sessionStorePath),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
const { readChannelAllowFromStore, upsertChannelPairingRequest } = vi.hoisted(() => ({
|
|
||||||
readChannelAllowFromStore: vi.fn(async () => [] as string[]),
|
|
||||||
upsertChannelPairingRequest: vi.fn(async () => ({
|
|
||||||
code: "PAIRCODE",
|
|
||||||
created: true,
|
|
||||||
})),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../pairing/pairing-store.js", () => ({
|
|
||||||
readChannelAllowFromStore,
|
|
||||||
upsertChannelPairingRequest,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const useSpy = vi.fn();
|
|
||||||
const middlewareUseSpy = vi.fn();
|
|
||||||
const onSpy = vi.fn();
|
|
||||||
const stopSpy = vi.fn();
|
|
||||||
const commandSpy = vi.fn();
|
|
||||||
const botCtorSpy = vi.fn();
|
|
||||||
const answerCallbackQuerySpy = vi.fn(async () => undefined);
|
|
||||||
const sendChatActionSpy = vi.fn();
|
|
||||||
const setMessageReactionSpy = vi.fn(async () => undefined);
|
|
||||||
const setMyCommandsSpy = vi.fn(async () => undefined);
|
|
||||||
const sendMessageSpy = vi.fn(async () => ({ message_id: 77 }));
|
|
||||||
const sendAnimationSpy = vi.fn(async () => ({ message_id: 78 }));
|
|
||||||
const sendPhotoSpy = vi.fn(async () => ({ message_id: 79 }));
|
|
||||||
type ApiStub = {
|
|
||||||
config: { use: (arg: unknown) => void };
|
|
||||||
answerCallbackQuery: typeof answerCallbackQuerySpy;
|
|
||||||
sendChatAction: typeof sendChatActionSpy;
|
|
||||||
setMessageReaction: typeof setMessageReactionSpy;
|
|
||||||
setMyCommands: typeof setMyCommandsSpy;
|
|
||||||
sendMessage: typeof sendMessageSpy;
|
|
||||||
sendAnimation: typeof sendAnimationSpy;
|
|
||||||
sendPhoto: typeof sendPhotoSpy;
|
|
||||||
};
|
|
||||||
const apiStub: ApiStub = {
|
|
||||||
config: { use: useSpy },
|
|
||||||
answerCallbackQuery: answerCallbackQuerySpy,
|
|
||||||
sendChatAction: sendChatActionSpy,
|
|
||||||
setMessageReaction: setMessageReactionSpy,
|
|
||||||
setMyCommands: setMyCommandsSpy,
|
|
||||||
sendMessage: sendMessageSpy,
|
|
||||||
sendAnimation: sendAnimationSpy,
|
|
||||||
sendPhoto: sendPhotoSpy,
|
|
||||||
};
|
|
||||||
|
|
||||||
vi.mock("grammy", () => ({
|
|
||||||
Bot: class {
|
|
||||||
api = apiStub;
|
|
||||||
use = middlewareUseSpy;
|
|
||||||
on = onSpy;
|
|
||||||
stop = stopSpy;
|
|
||||||
command = commandSpy;
|
|
||||||
catch = vi.fn();
|
|
||||||
constructor(
|
|
||||||
public token: string,
|
|
||||||
public options?: { client?: { fetch?: typeof fetch } },
|
|
||||||
) {
|
|
||||||
botCtorSpy(token, options);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
InputFile: class {},
|
|
||||||
webhookCallback: vi.fn(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
const sequentializeMiddleware = vi.fn();
|
|
||||||
const sequentializeSpy = vi.fn(() => sequentializeMiddleware);
|
|
||||||
let _sequentializeKey: ((ctx: unknown) => string) | undefined;
|
|
||||||
vi.mock("@grammyjs/runner", () => ({
|
|
||||||
sequentialize: (keyFn: (ctx: unknown) => string) => {
|
|
||||||
_sequentializeKey = keyFn;
|
|
||||||
return sequentializeSpy();
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
|
|
||||||
const throttlerSpy = vi.fn(() => "throttler");
|
|
||||||
|
|
||||||
vi.mock("@grammyjs/transformer-throttler", () => ({
|
|
||||||
apiThrottler: () => throttlerSpy(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../auto-reply/reply.js", () => {
|
|
||||||
const replySpy = vi.fn(async (_ctx, opts) => {
|
|
||||||
await opts?.onReplyStart?.();
|
|
||||||
return undefined;
|
|
||||||
});
|
|
||||||
return { getReplyFromConfig: replySpy, __replySpy: replySpy };
|
|
||||||
});
|
|
||||||
|
|
||||||
let replyModule: typeof import("../auto-reply/reply.js");
|
|
||||||
|
|
||||||
const getOnHandler = (event: string) => {
|
|
||||||
const handler = onSpy.mock.calls.find((call) => call[0] === event)?.[1];
|
|
||||||
if (!handler) {
|
|
||||||
throw new Error(`Missing handler for event: ${event}`);
|
|
||||||
}
|
|
||||||
return handler as (ctx: Record<string, unknown>) => Promise<void>;
|
|
||||||
};
|
|
||||||
|
|
||||||
describe("createTelegramBot", () => {
|
describe("createTelegramBot", () => {
|
||||||
beforeAll(async () => {
|
|
||||||
replyModule = await import("../auto-reply/reply.js");
|
|
||||||
});
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
resetInboundDedupe();
|
|
||||||
loadConfig.mockReturnValue({
|
|
||||||
channels: {
|
|
||||||
telegram: { dmPolicy: "open", allowFrom: ["*"] },
|
|
||||||
},
|
|
||||||
});
|
|
||||||
loadWebMedia.mockReset();
|
|
||||||
sendAnimationSpy.mockReset();
|
|
||||||
sendPhotoSpy.mockReset();
|
|
||||||
setMessageReactionSpy.mockReset();
|
|
||||||
answerCallbackQuerySpy.mockReset();
|
|
||||||
setMyCommandsSpy.mockReset();
|
|
||||||
middlewareUseSpy.mockReset();
|
|
||||||
sequentializeSpy.mockReset();
|
|
||||||
botCtorSpy.mockReset();
|
|
||||||
_sequentializeKey = undefined;
|
|
||||||
});
|
|
||||||
|
|
||||||
// groupPolicy tests
|
// groupPolicy tests
|
||||||
|
|
||||||
it("applies topic skill filters and system prompts", async () => {
|
it("applies topic skill filters and system prompts", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
|
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
@@ -216,7 +70,6 @@ describe("createTelegramBot", () => {
|
|||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
sendMessageSpy.mockReset();
|
sendMessageSpy.mockReset();
|
||||||
commandSpy.mockReset();
|
commandSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
replySpy.mockResolvedValue({ text: "response" });
|
replySpy.mockResolvedValue({ text: "response" });
|
||||||
|
|
||||||
@@ -260,7 +113,6 @@ describe("createTelegramBot", () => {
|
|||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
sendMessageSpy.mockReset();
|
sendMessageSpy.mockReset();
|
||||||
commandSpy.mockReset();
|
commandSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
replySpy.mockResolvedValue({ text: "response" });
|
replySpy.mockResolvedValue({ text: "response" });
|
||||||
|
|
||||||
@@ -306,7 +158,6 @@ describe("createTelegramBot", () => {
|
|||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
sendMessageSpy.mockReset();
|
sendMessageSpy.mockReset();
|
||||||
commandSpy.mockReset();
|
commandSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
replySpy.mockImplementation(async (_ctx, opts) => {
|
replySpy.mockImplementation(async (_ctx, opts) => {
|
||||||
await opts?.onToolResult?.({ text: "tool update" });
|
await opts?.onToolResult?.({ text: "tool update" });
|
||||||
@@ -347,7 +198,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("dedupes duplicate message updates by update_id", async () => {
|
it("dedupes duplicate message updates by update_id", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
|
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
|
|||||||
@@ -1,167 +1,19 @@
|
|||||||
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { resetInboundDedupe } from "../auto-reply/reply/inbound-dedupe.js";
|
import {
|
||||||
|
getLoadConfigMock,
|
||||||
|
getOnHandler,
|
||||||
|
onSpy,
|
||||||
|
replySpy,
|
||||||
|
} from "./bot.create-telegram-bot.test-harness.js";
|
||||||
import { createTelegramBot } from "./bot.js";
|
import { createTelegramBot } from "./bot.js";
|
||||||
|
|
||||||
const { sessionStorePath } = vi.hoisted(() => ({
|
const loadConfig = getLoadConfigMock();
|
||||||
sessionStorePath: `/tmp/openclaw-telegram-${Math.random().toString(16).slice(2)}.json`,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const { loadWebMedia } = vi.hoisted(() => ({
|
|
||||||
loadWebMedia: vi.fn(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../web/media.js", () => ({
|
|
||||||
loadWebMedia,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const { loadConfig } = vi.hoisted(() => ({
|
|
||||||
loadConfig: vi.fn(() => ({})),
|
|
||||||
}));
|
|
||||||
vi.mock("../config/config.js", async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof import("../config/config.js")>();
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
loadConfig,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
vi.mock("../config/sessions.js", async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof import("../config/sessions.js")>();
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
resolveStorePath: vi.fn((storePath) => storePath ?? sessionStorePath),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
const { readChannelAllowFromStore, upsertChannelPairingRequest } = vi.hoisted(() => ({
|
|
||||||
readChannelAllowFromStore: vi.fn(async () => [] as string[]),
|
|
||||||
upsertChannelPairingRequest: vi.fn(async () => ({
|
|
||||||
code: "PAIRCODE",
|
|
||||||
created: true,
|
|
||||||
})),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../pairing/pairing-store.js", () => ({
|
|
||||||
readChannelAllowFromStore,
|
|
||||||
upsertChannelPairingRequest,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const useSpy = vi.fn();
|
|
||||||
const middlewareUseSpy = vi.fn();
|
|
||||||
const onSpy = vi.fn();
|
|
||||||
const stopSpy = vi.fn();
|
|
||||||
const commandSpy = vi.fn();
|
|
||||||
const botCtorSpy = vi.fn();
|
|
||||||
const answerCallbackQuerySpy = vi.fn(async () => undefined);
|
|
||||||
const sendChatActionSpy = vi.fn();
|
|
||||||
const setMessageReactionSpy = vi.fn(async () => undefined);
|
|
||||||
const setMyCommandsSpy = vi.fn(async () => undefined);
|
|
||||||
const sendMessageSpy = vi.fn(async () => ({ message_id: 77 }));
|
|
||||||
const sendAnimationSpy = vi.fn(async () => ({ message_id: 78 }));
|
|
||||||
const sendPhotoSpy = vi.fn(async () => ({ message_id: 79 }));
|
|
||||||
type ApiStub = {
|
|
||||||
config: { use: (arg: unknown) => void };
|
|
||||||
answerCallbackQuery: typeof answerCallbackQuerySpy;
|
|
||||||
sendChatAction: typeof sendChatActionSpy;
|
|
||||||
setMessageReaction: typeof setMessageReactionSpy;
|
|
||||||
setMyCommands: typeof setMyCommandsSpy;
|
|
||||||
sendMessage: typeof sendMessageSpy;
|
|
||||||
sendAnimation: typeof sendAnimationSpy;
|
|
||||||
sendPhoto: typeof sendPhotoSpy;
|
|
||||||
};
|
|
||||||
const apiStub: ApiStub = {
|
|
||||||
config: { use: useSpy },
|
|
||||||
answerCallbackQuery: answerCallbackQuerySpy,
|
|
||||||
sendChatAction: sendChatActionSpy,
|
|
||||||
setMessageReaction: setMessageReactionSpy,
|
|
||||||
setMyCommands: setMyCommandsSpy,
|
|
||||||
sendMessage: sendMessageSpy,
|
|
||||||
sendAnimation: sendAnimationSpy,
|
|
||||||
sendPhoto: sendPhotoSpy,
|
|
||||||
};
|
|
||||||
|
|
||||||
vi.mock("grammy", () => ({
|
|
||||||
Bot: class {
|
|
||||||
api = apiStub;
|
|
||||||
use = middlewareUseSpy;
|
|
||||||
on = onSpy;
|
|
||||||
stop = stopSpy;
|
|
||||||
command = commandSpy;
|
|
||||||
catch = vi.fn();
|
|
||||||
constructor(
|
|
||||||
public token: string,
|
|
||||||
public options?: { client?: { fetch?: typeof fetch } },
|
|
||||||
) {
|
|
||||||
botCtorSpy(token, options);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
InputFile: class {},
|
|
||||||
webhookCallback: vi.fn(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
const sequentializeMiddleware = vi.fn();
|
|
||||||
const sequentializeSpy = vi.fn(() => sequentializeMiddleware);
|
|
||||||
let _sequentializeKey: ((ctx: unknown) => string) | undefined;
|
|
||||||
vi.mock("@grammyjs/runner", () => ({
|
|
||||||
sequentialize: (keyFn: (ctx: unknown) => string) => {
|
|
||||||
_sequentializeKey = keyFn;
|
|
||||||
return sequentializeSpy();
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
|
|
||||||
const throttlerSpy = vi.fn(() => "throttler");
|
|
||||||
|
|
||||||
vi.mock("@grammyjs/transformer-throttler", () => ({
|
|
||||||
apiThrottler: () => throttlerSpy(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../auto-reply/reply.js", () => {
|
|
||||||
const replySpy = vi.fn(async (_ctx, opts) => {
|
|
||||||
await opts?.onReplyStart?.();
|
|
||||||
return undefined;
|
|
||||||
});
|
|
||||||
return { getReplyFromConfig: replySpy, __replySpy: replySpy };
|
|
||||||
});
|
|
||||||
|
|
||||||
let replyModule: typeof import("../auto-reply/reply.js");
|
|
||||||
|
|
||||||
const getOnHandler = (event: string) => {
|
|
||||||
const handler = onSpy.mock.calls.find((call) => call[0] === event)?.[1];
|
|
||||||
if (!handler) {
|
|
||||||
throw new Error(`Missing handler for event: ${event}`);
|
|
||||||
}
|
|
||||||
return handler as (ctx: Record<string, unknown>) => Promise<void>;
|
|
||||||
};
|
|
||||||
|
|
||||||
describe("createTelegramBot", () => {
|
describe("createTelegramBot", () => {
|
||||||
beforeAll(async () => {
|
|
||||||
replyModule = await import("../auto-reply/reply.js");
|
|
||||||
});
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
resetInboundDedupe();
|
|
||||||
loadConfig.mockReturnValue({
|
|
||||||
channels: {
|
|
||||||
telegram: { dmPolicy: "open", allowFrom: ["*"] },
|
|
||||||
},
|
|
||||||
});
|
|
||||||
loadWebMedia.mockReset();
|
|
||||||
sendAnimationSpy.mockReset();
|
|
||||||
sendPhotoSpy.mockReset();
|
|
||||||
setMessageReactionSpy.mockReset();
|
|
||||||
answerCallbackQuerySpy.mockReset();
|
|
||||||
setMyCommandsSpy.mockReset();
|
|
||||||
middlewareUseSpy.mockReset();
|
|
||||||
sequentializeSpy.mockReset();
|
|
||||||
botCtorSpy.mockReset();
|
|
||||||
_sequentializeKey = undefined;
|
|
||||||
});
|
|
||||||
|
|
||||||
// groupPolicy tests
|
// groupPolicy tests
|
||||||
|
|
||||||
it("blocks all group messages when groupPolicy is 'disabled'", async () => {
|
it("blocks all group messages when groupPolicy is 'disabled'", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: {
|
channels: {
|
||||||
@@ -191,7 +43,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("blocks group messages from senders not in allowFrom when groupPolicy is 'allowlist'", async () => {
|
it("blocks group messages from senders not in allowFrom when groupPolicy is 'allowlist'", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: {
|
channels: {
|
||||||
@@ -220,7 +71,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("allows group messages from senders in allowFrom (by ID) when groupPolicy is 'allowlist'", async () => {
|
it("allows group messages from senders in allowFrom (by ID) when groupPolicy is 'allowlist'", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: {
|
channels: {
|
||||||
@@ -250,7 +100,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("blocks group messages when allowFrom is configured with @username entries (numeric IDs required)", async () => {
|
it("blocks group messages when allowFrom is configured with @username entries (numeric IDs required)", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: {
|
channels: {
|
||||||
@@ -280,7 +129,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("allows group messages from telegram:-prefixed allowFrom entries when groupPolicy is 'allowlist'", async () => {
|
it("allows group messages from telegram:-prefixed allowFrom entries when groupPolicy is 'allowlist'", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: {
|
channels: {
|
||||||
@@ -310,7 +158,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("allows group messages from tg:-prefixed allowFrom entries case-insensitively when groupPolicy is 'allowlist'", async () => {
|
it("allows group messages from tg:-prefixed allowFrom entries case-insensitively when groupPolicy is 'allowlist'", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: {
|
channels: {
|
||||||
@@ -340,7 +187,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("allows all group messages when groupPolicy is 'open'", async () => {
|
it("allows all group messages when groupPolicy is 'open'", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: {
|
channels: {
|
||||||
|
|||||||
@@ -1,167 +1,19 @@
|
|||||||
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { resetInboundDedupe } from "../auto-reply/reply/inbound-dedupe.js";
|
import {
|
||||||
|
getOnHandler,
|
||||||
|
getLoadConfigMock,
|
||||||
|
onSpy,
|
||||||
|
replySpy,
|
||||||
|
} from "./bot.create-telegram-bot.test-harness.js";
|
||||||
import { createTelegramBot } from "./bot.js";
|
import { createTelegramBot } from "./bot.js";
|
||||||
|
|
||||||
const { sessionStorePath } = vi.hoisted(() => ({
|
const loadConfig = getLoadConfigMock();
|
||||||
sessionStorePath: `/tmp/openclaw-telegram-${Math.random().toString(16).slice(2)}.json`,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const { loadWebMedia } = vi.hoisted(() => ({
|
|
||||||
loadWebMedia: vi.fn(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../web/media.js", () => ({
|
|
||||||
loadWebMedia,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const { loadConfig } = vi.hoisted(() => ({
|
|
||||||
loadConfig: vi.fn(() => ({})),
|
|
||||||
}));
|
|
||||||
vi.mock("../config/config.js", async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof import("../config/config.js")>();
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
loadConfig,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
vi.mock("../config/sessions.js", async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof import("../config/sessions.js")>();
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
resolveStorePath: vi.fn((storePath) => storePath ?? sessionStorePath),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
const { readChannelAllowFromStore, upsertChannelPairingRequest } = vi.hoisted(() => ({
|
|
||||||
readChannelAllowFromStore: vi.fn(async () => [] as string[]),
|
|
||||||
upsertChannelPairingRequest: vi.fn(async () => ({
|
|
||||||
code: "PAIRCODE",
|
|
||||||
created: true,
|
|
||||||
})),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../pairing/pairing-store.js", () => ({
|
|
||||||
readChannelAllowFromStore,
|
|
||||||
upsertChannelPairingRequest,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const useSpy = vi.fn();
|
|
||||||
const middlewareUseSpy = vi.fn();
|
|
||||||
const onSpy = vi.fn();
|
|
||||||
const stopSpy = vi.fn();
|
|
||||||
const commandSpy = vi.fn();
|
|
||||||
const botCtorSpy = vi.fn();
|
|
||||||
const answerCallbackQuerySpy = vi.fn(async () => undefined);
|
|
||||||
const sendChatActionSpy = vi.fn();
|
|
||||||
const setMessageReactionSpy = vi.fn(async () => undefined);
|
|
||||||
const setMyCommandsSpy = vi.fn(async () => undefined);
|
|
||||||
const sendMessageSpy = vi.fn(async () => ({ message_id: 77 }));
|
|
||||||
const sendAnimationSpy = vi.fn(async () => ({ message_id: 78 }));
|
|
||||||
const sendPhotoSpy = vi.fn(async () => ({ message_id: 79 }));
|
|
||||||
type ApiStub = {
|
|
||||||
config: { use: (arg: unknown) => void };
|
|
||||||
answerCallbackQuery: typeof answerCallbackQuerySpy;
|
|
||||||
sendChatAction: typeof sendChatActionSpy;
|
|
||||||
setMessageReaction: typeof setMessageReactionSpy;
|
|
||||||
setMyCommands: typeof setMyCommandsSpy;
|
|
||||||
sendMessage: typeof sendMessageSpy;
|
|
||||||
sendAnimation: typeof sendAnimationSpy;
|
|
||||||
sendPhoto: typeof sendPhotoSpy;
|
|
||||||
};
|
|
||||||
const apiStub: ApiStub = {
|
|
||||||
config: { use: useSpy },
|
|
||||||
answerCallbackQuery: answerCallbackQuerySpy,
|
|
||||||
sendChatAction: sendChatActionSpy,
|
|
||||||
setMessageReaction: setMessageReactionSpy,
|
|
||||||
setMyCommands: setMyCommandsSpy,
|
|
||||||
sendMessage: sendMessageSpy,
|
|
||||||
sendAnimation: sendAnimationSpy,
|
|
||||||
sendPhoto: sendPhotoSpy,
|
|
||||||
};
|
|
||||||
|
|
||||||
vi.mock("grammy", () => ({
|
|
||||||
Bot: class {
|
|
||||||
api = apiStub;
|
|
||||||
use = middlewareUseSpy;
|
|
||||||
on = onSpy;
|
|
||||||
stop = stopSpy;
|
|
||||||
command = commandSpy;
|
|
||||||
catch = vi.fn();
|
|
||||||
constructor(
|
|
||||||
public token: string,
|
|
||||||
public options?: { client?: { fetch?: typeof fetch } },
|
|
||||||
) {
|
|
||||||
botCtorSpy(token, options);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
InputFile: class {},
|
|
||||||
webhookCallback: vi.fn(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
const sequentializeMiddleware = vi.fn();
|
|
||||||
const sequentializeSpy = vi.fn(() => sequentializeMiddleware);
|
|
||||||
let _sequentializeKey: ((ctx: unknown) => string) | undefined;
|
|
||||||
vi.mock("@grammyjs/runner", () => ({
|
|
||||||
sequentialize: (keyFn: (ctx: unknown) => string) => {
|
|
||||||
_sequentializeKey = keyFn;
|
|
||||||
return sequentializeSpy();
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
|
|
||||||
const throttlerSpy = vi.fn(() => "throttler");
|
|
||||||
|
|
||||||
vi.mock("@grammyjs/transformer-throttler", () => ({
|
|
||||||
apiThrottler: () => throttlerSpy(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../auto-reply/reply.js", () => {
|
|
||||||
const replySpy = vi.fn(async (_ctx, opts) => {
|
|
||||||
await opts?.onReplyStart?.();
|
|
||||||
return undefined;
|
|
||||||
});
|
|
||||||
return { getReplyFromConfig: replySpy, __replySpy: replySpy };
|
|
||||||
});
|
|
||||||
|
|
||||||
let replyModule: typeof import("../auto-reply/reply.js");
|
|
||||||
|
|
||||||
const getOnHandler = (event: string) => {
|
|
||||||
const handler = onSpy.mock.calls.find((call) => call[0] === event)?.[1];
|
|
||||||
if (!handler) {
|
|
||||||
throw new Error(`Missing handler for event: ${event}`);
|
|
||||||
}
|
|
||||||
return handler as (ctx: Record<string, unknown>) => Promise<void>;
|
|
||||||
};
|
|
||||||
|
|
||||||
describe("createTelegramBot", () => {
|
describe("createTelegramBot", () => {
|
||||||
beforeAll(async () => {
|
|
||||||
replyModule = await import("../auto-reply/reply.js");
|
|
||||||
});
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
resetInboundDedupe();
|
|
||||||
loadConfig.mockReturnValue({
|
|
||||||
channels: {
|
|
||||||
telegram: { dmPolicy: "open", allowFrom: ["*"] },
|
|
||||||
},
|
|
||||||
});
|
|
||||||
loadWebMedia.mockReset();
|
|
||||||
sendAnimationSpy.mockReset();
|
|
||||||
sendPhotoSpy.mockReset();
|
|
||||||
setMessageReactionSpy.mockReset();
|
|
||||||
answerCallbackQuerySpy.mockReset();
|
|
||||||
setMyCommandsSpy.mockReset();
|
|
||||||
middlewareUseSpy.mockReset();
|
|
||||||
sequentializeSpy.mockReset();
|
|
||||||
botCtorSpy.mockReset();
|
|
||||||
_sequentializeKey = undefined;
|
|
||||||
});
|
|
||||||
|
|
||||||
// groupPolicy tests
|
// groupPolicy tests
|
||||||
|
|
||||||
it("dedupes duplicate callback_query updates by update_id", async () => {
|
it("dedupes duplicate callback_query updates by update_id", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
|
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
@@ -198,7 +50,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("allows distinct callback_query ids without update_id", async () => {
|
it("allows distinct callback_query ids without update_id", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
|
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
|
|||||||
@@ -1,151 +1,40 @@
|
|||||||
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
import { escapeRegExp, formatEnvelopeTimestamp } from "../../test/helpers/envelope-timestamp.js";
|
import { escapeRegExp, formatEnvelopeTimestamp } from "../../test/helpers/envelope-timestamp.js";
|
||||||
import { resetInboundDedupe } from "../auto-reply/reply/inbound-dedupe.js";
|
import {
|
||||||
|
answerCallbackQuerySpy,
|
||||||
|
botCtorSpy,
|
||||||
|
getLoadConfigMock,
|
||||||
|
getLoadWebMediaMock,
|
||||||
|
getOnHandler,
|
||||||
|
getReadChannelAllowFromStoreMock,
|
||||||
|
getUpsertChannelPairingRequestMock,
|
||||||
|
middlewareUseSpy,
|
||||||
|
onSpy,
|
||||||
|
replySpy,
|
||||||
|
sendAnimationSpy,
|
||||||
|
sendChatActionSpy,
|
||||||
|
sendMessageSpy,
|
||||||
|
sendPhotoSpy,
|
||||||
|
sequentializeKey,
|
||||||
|
sequentializeSpy,
|
||||||
|
setMessageReactionSpy,
|
||||||
|
setMyCommandsSpy,
|
||||||
|
throttlerSpy,
|
||||||
|
useSpy,
|
||||||
|
} from "./bot.create-telegram-bot.test-harness.js";
|
||||||
import { createTelegramBot, getTelegramSequentialKey } from "./bot.js";
|
import { createTelegramBot, getTelegramSequentialKey } from "./bot.js";
|
||||||
import { resolveTelegramFetch } from "./fetch.js";
|
import { resolveTelegramFetch } from "./fetch.js";
|
||||||
|
|
||||||
const { sessionStorePath } = vi.hoisted(() => ({
|
const loadConfig = getLoadConfigMock();
|
||||||
sessionStorePath: `/tmp/openclaw-telegram-throttler-${Math.random().toString(16).slice(2)}.json`,
|
const loadWebMedia = getLoadWebMediaMock();
|
||||||
}));
|
const readChannelAllowFromStore = getReadChannelAllowFromStoreMock();
|
||||||
const { loadWebMedia } = vi.hoisted(() => ({
|
const upsertChannelPairingRequest = getUpsertChannelPairingRequestMock();
|
||||||
loadWebMedia: vi.fn(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../web/media.js", () => ({
|
|
||||||
loadWebMedia,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const { loadConfig } = vi.hoisted(() => ({
|
|
||||||
loadConfig: vi.fn(() => ({})),
|
|
||||||
}));
|
|
||||||
vi.mock("../config/config.js", async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof import("../config/config.js")>();
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
loadConfig,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
vi.mock("../config/sessions.js", async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof import("../config/sessions.js")>();
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
resolveStorePath: vi.fn((storePath) => storePath ?? sessionStorePath),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
const { readChannelAllowFromStore, upsertChannelPairingRequest } = vi.hoisted(() => ({
|
|
||||||
readChannelAllowFromStore: vi.fn(async () => [] as string[]),
|
|
||||||
upsertChannelPairingRequest: vi.fn(async () => ({
|
|
||||||
code: "PAIRCODE",
|
|
||||||
created: true,
|
|
||||||
})),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../pairing/pairing-store.js", () => ({
|
|
||||||
readChannelAllowFromStore,
|
|
||||||
upsertChannelPairingRequest,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const useSpy = vi.fn();
|
|
||||||
const middlewareUseSpy = vi.fn();
|
|
||||||
const onSpy = vi.fn();
|
|
||||||
const stopSpy = vi.fn();
|
|
||||||
const commandSpy = vi.fn();
|
|
||||||
const botCtorSpy = vi.fn();
|
|
||||||
const answerCallbackQuerySpy = vi.fn(async () => undefined);
|
|
||||||
const sendChatActionSpy = vi.fn();
|
|
||||||
const setMessageReactionSpy = vi.fn(async () => undefined);
|
|
||||||
const setMyCommandsSpy = vi.fn(async () => undefined);
|
|
||||||
const sendMessageSpy = vi.fn(async () => ({ message_id: 77 }));
|
|
||||||
const sendAnimationSpy = vi.fn(async () => ({ message_id: 78 }));
|
|
||||||
const sendPhotoSpy = vi.fn(async () => ({ message_id: 79 }));
|
|
||||||
type ApiStub = {
|
|
||||||
config: { use: (arg: unknown) => void };
|
|
||||||
answerCallbackQuery: typeof answerCallbackQuerySpy;
|
|
||||||
sendChatAction: typeof sendChatActionSpy;
|
|
||||||
setMessageReaction: typeof setMessageReactionSpy;
|
|
||||||
setMyCommands: typeof setMyCommandsSpy;
|
|
||||||
sendMessage: typeof sendMessageSpy;
|
|
||||||
sendAnimation: typeof sendAnimationSpy;
|
|
||||||
sendPhoto: typeof sendPhotoSpy;
|
|
||||||
};
|
|
||||||
const apiStub: ApiStub = {
|
|
||||||
config: { use: useSpy },
|
|
||||||
answerCallbackQuery: answerCallbackQuerySpy,
|
|
||||||
sendChatAction: sendChatActionSpy,
|
|
||||||
setMessageReaction: setMessageReactionSpy,
|
|
||||||
setMyCommands: setMyCommandsSpy,
|
|
||||||
sendMessage: sendMessageSpy,
|
|
||||||
sendAnimation: sendAnimationSpy,
|
|
||||||
sendPhoto: sendPhotoSpy,
|
|
||||||
};
|
|
||||||
|
|
||||||
vi.mock("grammy", () => ({
|
|
||||||
Bot: class {
|
|
||||||
api = apiStub;
|
|
||||||
use = middlewareUseSpy;
|
|
||||||
on = onSpy;
|
|
||||||
stop = stopSpy;
|
|
||||||
command = commandSpy;
|
|
||||||
catch = vi.fn();
|
|
||||||
constructor(
|
|
||||||
public token: string,
|
|
||||||
public options?: {
|
|
||||||
client?: { fetch?: typeof fetch; timeoutSeconds?: number };
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
botCtorSpy(token, options);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
InputFile: class {},
|
|
||||||
webhookCallback: vi.fn(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
const sequentializeMiddleware = vi.fn();
|
|
||||||
const sequentializeSpy = vi.fn(() => sequentializeMiddleware);
|
|
||||||
let sequentializeKey: ((ctx: unknown) => string) | undefined;
|
|
||||||
vi.mock("@grammyjs/runner", () => ({
|
|
||||||
sequentialize: (keyFn: (ctx: unknown) => string) => {
|
|
||||||
sequentializeKey = keyFn;
|
|
||||||
return sequentializeSpy();
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
|
|
||||||
const throttlerSpy = vi.fn(() => "throttler");
|
|
||||||
|
|
||||||
vi.mock("@grammyjs/transformer-throttler", () => ({
|
|
||||||
apiThrottler: () => throttlerSpy(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../auto-reply/reply.js", () => {
|
|
||||||
const replySpy = vi.fn(async (_ctx, opts) => {
|
|
||||||
await opts?.onReplyStart?.();
|
|
||||||
return undefined;
|
|
||||||
});
|
|
||||||
return { getReplyFromConfig: replySpy, __replySpy: replySpy };
|
|
||||||
});
|
|
||||||
|
|
||||||
let replyModule: typeof import("../auto-reply/reply.js");
|
|
||||||
|
|
||||||
const getOnHandler = (event: string) => {
|
|
||||||
const handler = onSpy.mock.calls.find((call) => call[0] === event)?.[1];
|
|
||||||
if (!handler) {
|
|
||||||
throw new Error(`Missing handler for event: ${event}`);
|
|
||||||
}
|
|
||||||
return handler as (ctx: Record<string, unknown>) => Promise<void>;
|
|
||||||
};
|
|
||||||
|
|
||||||
const ORIGINAL_TZ = process.env.TZ;
|
const ORIGINAL_TZ = process.env.TZ;
|
||||||
|
|
||||||
describe("createTelegramBot", () => {
|
describe("createTelegramBot", () => {
|
||||||
beforeAll(async () => {
|
|
||||||
replyModule = await import("../auto-reply/reply.js");
|
|
||||||
});
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
process.env.TZ = "UTC";
|
process.env.TZ = "UTC";
|
||||||
resetInboundDedupe();
|
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
agents: {
|
agents: {
|
||||||
defaults: {
|
defaults: {
|
||||||
@@ -165,7 +54,6 @@ describe("createTelegramBot", () => {
|
|||||||
middlewareUseSpy.mockReset();
|
middlewareUseSpy.mockReset();
|
||||||
sequentializeSpy.mockReset();
|
sequentializeSpy.mockReset();
|
||||||
botCtorSpy.mockReset();
|
botCtorSpy.mockReset();
|
||||||
sequentializeKey = undefined;
|
|
||||||
});
|
});
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
process.env.TZ = ORIGINAL_TZ;
|
process.env.TZ = ORIGINAL_TZ;
|
||||||
@@ -274,7 +162,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("routes callback_query payloads as messages and answers callbacks", async () => {
|
it("routes callback_query payloads as messages and answers callbacks", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
|
|
||||||
createTelegramBot({ token: "tok" });
|
createTelegramBot({ token: "tok" });
|
||||||
@@ -309,7 +196,6 @@ describe("createTelegramBot", () => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
|
|
||||||
createTelegramBot({ token: "tok" });
|
createTelegramBot({ token: "tok" });
|
||||||
@@ -349,7 +235,6 @@ describe("createTelegramBot", () => {
|
|||||||
it("requests pairing by default for unknown DM senders", async () => {
|
it("requests pairing by default for unknown DM senders", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
sendMessageSpy.mockReset();
|
sendMessageSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
|
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
@@ -388,7 +273,6 @@ describe("createTelegramBot", () => {
|
|||||||
it("does not resend pairing code when a request is already pending", async () => {
|
it("does not resend pairing code when a request is already pending", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
sendMessageSpy.mockReset();
|
sendMessageSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
|
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
|
|||||||
@@ -1,167 +1,21 @@
|
|||||||
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { resetInboundDedupe } from "../auto-reply/reply/inbound-dedupe.js";
|
import {
|
||||||
|
getLoadConfigMock,
|
||||||
|
getOnHandler,
|
||||||
|
onSpy,
|
||||||
|
replySpy,
|
||||||
|
sendChatActionSpy,
|
||||||
|
sendMessageSpy,
|
||||||
|
} from "./bot.create-telegram-bot.test-harness.js";
|
||||||
import { createTelegramBot } from "./bot.js";
|
import { createTelegramBot } from "./bot.js";
|
||||||
|
|
||||||
const { sessionStorePath } = vi.hoisted(() => ({
|
const loadConfig = getLoadConfigMock();
|
||||||
sessionStorePath: `/tmp/openclaw-telegram-${Math.random().toString(16).slice(2)}.json`,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const { loadWebMedia } = vi.hoisted(() => ({
|
|
||||||
loadWebMedia: vi.fn(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../web/media.js", () => ({
|
|
||||||
loadWebMedia,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const { loadConfig } = vi.hoisted(() => ({
|
|
||||||
loadConfig: vi.fn(() => ({})),
|
|
||||||
}));
|
|
||||||
vi.mock("../config/config.js", async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof import("../config/config.js")>();
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
loadConfig,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
vi.mock("../config/sessions.js", async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof import("../config/sessions.js")>();
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
resolveStorePath: vi.fn((storePath) => storePath ?? sessionStorePath),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
const { readChannelAllowFromStore, upsertChannelPairingRequest } = vi.hoisted(() => ({
|
|
||||||
readChannelAllowFromStore: vi.fn(async () => [] as string[]),
|
|
||||||
upsertChannelPairingRequest: vi.fn(async () => ({
|
|
||||||
code: "PAIRCODE",
|
|
||||||
created: true,
|
|
||||||
})),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../pairing/pairing-store.js", () => ({
|
|
||||||
readChannelAllowFromStore,
|
|
||||||
upsertChannelPairingRequest,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const useSpy = vi.fn();
|
|
||||||
const middlewareUseSpy = vi.fn();
|
|
||||||
const onSpy = vi.fn();
|
|
||||||
const stopSpy = vi.fn();
|
|
||||||
const commandSpy = vi.fn();
|
|
||||||
const botCtorSpy = vi.fn();
|
|
||||||
const answerCallbackQuerySpy = vi.fn(async () => undefined);
|
|
||||||
const sendChatActionSpy = vi.fn();
|
|
||||||
const setMessageReactionSpy = vi.fn(async () => undefined);
|
|
||||||
const setMyCommandsSpy = vi.fn(async () => undefined);
|
|
||||||
const sendMessageSpy = vi.fn(async () => ({ message_id: 77 }));
|
|
||||||
const sendAnimationSpy = vi.fn(async () => ({ message_id: 78 }));
|
|
||||||
const sendPhotoSpy = vi.fn(async () => ({ message_id: 79 }));
|
|
||||||
type ApiStub = {
|
|
||||||
config: { use: (arg: unknown) => void };
|
|
||||||
answerCallbackQuery: typeof answerCallbackQuerySpy;
|
|
||||||
sendChatAction: typeof sendChatActionSpy;
|
|
||||||
setMessageReaction: typeof setMessageReactionSpy;
|
|
||||||
setMyCommands: typeof setMyCommandsSpy;
|
|
||||||
sendMessage: typeof sendMessageSpy;
|
|
||||||
sendAnimation: typeof sendAnimationSpy;
|
|
||||||
sendPhoto: typeof sendPhotoSpy;
|
|
||||||
};
|
|
||||||
const apiStub: ApiStub = {
|
|
||||||
config: { use: useSpy },
|
|
||||||
answerCallbackQuery: answerCallbackQuerySpy,
|
|
||||||
sendChatAction: sendChatActionSpy,
|
|
||||||
setMessageReaction: setMessageReactionSpy,
|
|
||||||
setMyCommands: setMyCommandsSpy,
|
|
||||||
sendMessage: sendMessageSpy,
|
|
||||||
sendAnimation: sendAnimationSpy,
|
|
||||||
sendPhoto: sendPhotoSpy,
|
|
||||||
};
|
|
||||||
|
|
||||||
vi.mock("grammy", () => ({
|
|
||||||
Bot: class {
|
|
||||||
api = apiStub;
|
|
||||||
use = middlewareUseSpy;
|
|
||||||
on = onSpy;
|
|
||||||
stop = stopSpy;
|
|
||||||
command = commandSpy;
|
|
||||||
catch = vi.fn();
|
|
||||||
constructor(
|
|
||||||
public token: string,
|
|
||||||
public options?: { client?: { fetch?: typeof fetch } },
|
|
||||||
) {
|
|
||||||
botCtorSpy(token, options);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
InputFile: class {},
|
|
||||||
webhookCallback: vi.fn(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
const sequentializeMiddleware = vi.fn();
|
|
||||||
const sequentializeSpy = vi.fn(() => sequentializeMiddleware);
|
|
||||||
let _sequentializeKey: ((ctx: unknown) => string) | undefined;
|
|
||||||
vi.mock("@grammyjs/runner", () => ({
|
|
||||||
sequentialize: (keyFn: (ctx: unknown) => string) => {
|
|
||||||
_sequentializeKey = keyFn;
|
|
||||||
return sequentializeSpy();
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
|
|
||||||
const throttlerSpy = vi.fn(() => "throttler");
|
|
||||||
|
|
||||||
vi.mock("@grammyjs/transformer-throttler", () => ({
|
|
||||||
apiThrottler: () => throttlerSpy(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../auto-reply/reply.js", () => {
|
|
||||||
const replySpy = vi.fn(async (_ctx, opts) => {
|
|
||||||
await opts?.onReplyStart?.();
|
|
||||||
return undefined;
|
|
||||||
});
|
|
||||||
return { getReplyFromConfig: replySpy, __replySpy: replySpy };
|
|
||||||
});
|
|
||||||
|
|
||||||
let replyModule: typeof import("../auto-reply/reply.js");
|
|
||||||
|
|
||||||
const getOnHandler = (event: string) => {
|
|
||||||
const handler = onSpy.mock.calls.find((call) => call[0] === event)?.[1];
|
|
||||||
if (!handler) {
|
|
||||||
throw new Error(`Missing handler for event: ${event}`);
|
|
||||||
}
|
|
||||||
return handler as (ctx: Record<string, unknown>) => Promise<void>;
|
|
||||||
};
|
|
||||||
|
|
||||||
describe("createTelegramBot", () => {
|
describe("createTelegramBot", () => {
|
||||||
beforeAll(async () => {
|
|
||||||
replyModule = await import("../auto-reply/reply.js");
|
|
||||||
});
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
resetInboundDedupe();
|
|
||||||
loadConfig.mockReturnValue({
|
|
||||||
channels: {
|
|
||||||
telegram: { dmPolicy: "open", allowFrom: ["*"] },
|
|
||||||
},
|
|
||||||
});
|
|
||||||
loadWebMedia.mockReset();
|
|
||||||
sendAnimationSpy.mockReset();
|
|
||||||
sendPhotoSpy.mockReset();
|
|
||||||
setMessageReactionSpy.mockReset();
|
|
||||||
answerCallbackQuerySpy.mockReset();
|
|
||||||
setMyCommandsSpy.mockReset();
|
|
||||||
middlewareUseSpy.mockReset();
|
|
||||||
sequentializeSpy.mockReset();
|
|
||||||
botCtorSpy.mockReset();
|
|
||||||
_sequentializeKey = undefined;
|
|
||||||
});
|
|
||||||
|
|
||||||
// groupPolicy tests
|
// groupPolicy tests
|
||||||
|
|
||||||
it("matches tg:-prefixed allowFrom entries case-insensitively in group allowlist", async () => {
|
it("matches tg:-prefixed allowFrom entries case-insensitively in group allowlist", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: {
|
channels: {
|
||||||
@@ -192,7 +46,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("blocks group messages when groupPolicy allowlist has no groupAllowFrom", async () => {
|
it("blocks group messages when groupPolicy allowlist has no groupAllowFrom", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: {
|
channels: {
|
||||||
@@ -221,7 +74,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("allows control commands with TG-prefixed groupAllowFrom entries", async () => {
|
it("allows control commands with TG-prefixed groupAllowFrom entries", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: {
|
channels: {
|
||||||
@@ -252,7 +104,6 @@ describe("createTelegramBot", () => {
|
|||||||
it("isolates forum topic sessions and carries thread metadata", async () => {
|
it("isolates forum topic sessions and carries thread metadata", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
sendChatActionSpy.mockReset();
|
sendChatActionSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
|
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
@@ -298,7 +149,6 @@ describe("createTelegramBot", () => {
|
|||||||
it("falls back to General topic thread id for typing in forums", async () => {
|
it("falls back to General topic thread id for typing in forums", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
sendChatActionSpy.mockReset();
|
sendChatActionSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
|
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
@@ -338,7 +188,6 @@ describe("createTelegramBot", () => {
|
|||||||
it("routes General topic replies using thread id 1", async () => {
|
it("routes General topic replies using thread id 1", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
sendMessageSpy.mockReset();
|
sendMessageSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
replySpy.mockResolvedValue({ text: "response" });
|
replySpy.mockResolvedValue({ text: "response" });
|
||||||
|
|
||||||
|
|||||||
@@ -1,167 +1,19 @@
|
|||||||
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { resetInboundDedupe } from "../auto-reply/reply/inbound-dedupe.js";
|
import {
|
||||||
|
getLoadConfigMock,
|
||||||
|
getOnHandler,
|
||||||
|
onSpy,
|
||||||
|
replySpy,
|
||||||
|
} from "./bot.create-telegram-bot.test-harness.js";
|
||||||
import { createTelegramBot } from "./bot.js";
|
import { createTelegramBot } from "./bot.js";
|
||||||
|
|
||||||
const { sessionStorePath } = vi.hoisted(() => ({
|
const loadConfig = getLoadConfigMock();
|
||||||
sessionStorePath: `/tmp/openclaw-telegram-${Math.random().toString(16).slice(2)}.json`,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const { loadWebMedia } = vi.hoisted(() => ({
|
|
||||||
loadWebMedia: vi.fn(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../web/media.js", () => ({
|
|
||||||
loadWebMedia,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const { loadConfig } = vi.hoisted(() => ({
|
|
||||||
loadConfig: vi.fn(() => ({})),
|
|
||||||
}));
|
|
||||||
vi.mock("../config/config.js", async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof import("../config/config.js")>();
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
loadConfig,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
vi.mock("../config/sessions.js", async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof import("../config/sessions.js")>();
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
resolveStorePath: vi.fn((storePath) => storePath ?? sessionStorePath),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
const { readChannelAllowFromStore, upsertChannelPairingRequest } = vi.hoisted(() => ({
|
|
||||||
readChannelAllowFromStore: vi.fn(async () => [] as string[]),
|
|
||||||
upsertChannelPairingRequest: vi.fn(async () => ({
|
|
||||||
code: "PAIRCODE",
|
|
||||||
created: true,
|
|
||||||
})),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../pairing/pairing-store.js", () => ({
|
|
||||||
readChannelAllowFromStore,
|
|
||||||
upsertChannelPairingRequest,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const useSpy = vi.fn();
|
|
||||||
const middlewareUseSpy = vi.fn();
|
|
||||||
const onSpy = vi.fn();
|
|
||||||
const stopSpy = vi.fn();
|
|
||||||
const commandSpy = vi.fn();
|
|
||||||
const botCtorSpy = vi.fn();
|
|
||||||
const answerCallbackQuerySpy = vi.fn(async () => undefined);
|
|
||||||
const sendChatActionSpy = vi.fn();
|
|
||||||
const setMessageReactionSpy = vi.fn(async () => undefined);
|
|
||||||
const setMyCommandsSpy = vi.fn(async () => undefined);
|
|
||||||
const sendMessageSpy = vi.fn(async () => ({ message_id: 77 }));
|
|
||||||
const sendAnimationSpy = vi.fn(async () => ({ message_id: 78 }));
|
|
||||||
const sendPhotoSpy = vi.fn(async () => ({ message_id: 79 }));
|
|
||||||
type ApiStub = {
|
|
||||||
config: { use: (arg: unknown) => void };
|
|
||||||
answerCallbackQuery: typeof answerCallbackQuerySpy;
|
|
||||||
sendChatAction: typeof sendChatActionSpy;
|
|
||||||
setMessageReaction: typeof setMessageReactionSpy;
|
|
||||||
setMyCommands: typeof setMyCommandsSpy;
|
|
||||||
sendMessage: typeof sendMessageSpy;
|
|
||||||
sendAnimation: typeof sendAnimationSpy;
|
|
||||||
sendPhoto: typeof sendPhotoSpy;
|
|
||||||
};
|
|
||||||
const apiStub: ApiStub = {
|
|
||||||
config: { use: useSpy },
|
|
||||||
answerCallbackQuery: answerCallbackQuerySpy,
|
|
||||||
sendChatAction: sendChatActionSpy,
|
|
||||||
setMessageReaction: setMessageReactionSpy,
|
|
||||||
setMyCommands: setMyCommandsSpy,
|
|
||||||
sendMessage: sendMessageSpy,
|
|
||||||
sendAnimation: sendAnimationSpy,
|
|
||||||
sendPhoto: sendPhotoSpy,
|
|
||||||
};
|
|
||||||
|
|
||||||
vi.mock("grammy", () => ({
|
|
||||||
Bot: class {
|
|
||||||
api = apiStub;
|
|
||||||
use = middlewareUseSpy;
|
|
||||||
on = onSpy;
|
|
||||||
stop = stopSpy;
|
|
||||||
command = commandSpy;
|
|
||||||
catch = vi.fn();
|
|
||||||
constructor(
|
|
||||||
public token: string,
|
|
||||||
public options?: { client?: { fetch?: typeof fetch } },
|
|
||||||
) {
|
|
||||||
botCtorSpy(token, options);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
InputFile: class {},
|
|
||||||
webhookCallback: vi.fn(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
const sequentializeMiddleware = vi.fn();
|
|
||||||
const sequentializeSpy = vi.fn(() => sequentializeMiddleware);
|
|
||||||
let _sequentializeKey: ((ctx: unknown) => string) | undefined;
|
|
||||||
vi.mock("@grammyjs/runner", () => ({
|
|
||||||
sequentialize: (keyFn: (ctx: unknown) => string) => {
|
|
||||||
_sequentializeKey = keyFn;
|
|
||||||
return sequentializeSpy();
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
|
|
||||||
const throttlerSpy = vi.fn(() => "throttler");
|
|
||||||
|
|
||||||
vi.mock("@grammyjs/transformer-throttler", () => ({
|
|
||||||
apiThrottler: () => throttlerSpy(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../auto-reply/reply.js", () => {
|
|
||||||
const replySpy = vi.fn(async (_ctx, opts) => {
|
|
||||||
await opts?.onReplyStart?.();
|
|
||||||
return undefined;
|
|
||||||
});
|
|
||||||
return { getReplyFromConfig: replySpy, __replySpy: replySpy };
|
|
||||||
});
|
|
||||||
|
|
||||||
let replyModule: typeof import("../auto-reply/reply.js");
|
|
||||||
|
|
||||||
const getOnHandler = (event: string) => {
|
|
||||||
const handler = onSpy.mock.calls.find((call) => call[0] === event)?.[1];
|
|
||||||
if (!handler) {
|
|
||||||
throw new Error(`Missing handler for event: ${event}`);
|
|
||||||
}
|
|
||||||
return handler as (ctx: Record<string, unknown>) => Promise<void>;
|
|
||||||
};
|
|
||||||
|
|
||||||
describe("createTelegramBot", () => {
|
describe("createTelegramBot", () => {
|
||||||
beforeAll(async () => {
|
|
||||||
replyModule = await import("../auto-reply/reply.js");
|
|
||||||
});
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
resetInboundDedupe();
|
|
||||||
loadConfig.mockReturnValue({
|
|
||||||
channels: {
|
|
||||||
telegram: { dmPolicy: "open", allowFrom: ["*"] },
|
|
||||||
},
|
|
||||||
});
|
|
||||||
loadWebMedia.mockReset();
|
|
||||||
sendAnimationSpy.mockReset();
|
|
||||||
sendPhotoSpy.mockReset();
|
|
||||||
setMessageReactionSpy.mockReset();
|
|
||||||
answerCallbackQuerySpy.mockReset();
|
|
||||||
setMyCommandsSpy.mockReset();
|
|
||||||
middlewareUseSpy.mockReset();
|
|
||||||
sequentializeSpy.mockReset();
|
|
||||||
botCtorSpy.mockReset();
|
|
||||||
_sequentializeKey = undefined;
|
|
||||||
});
|
|
||||||
|
|
||||||
// groupPolicy tests
|
// groupPolicy tests
|
||||||
|
|
||||||
it("blocks @username allowFrom entries when groupPolicy is 'allowlist' (numeric IDs required)", async () => {
|
it("blocks @username allowFrom entries when groupPolicy is 'allowlist' (numeric IDs required)", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: {
|
channels: {
|
||||||
@@ -191,7 +43,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("allows direct messages regardless of groupPolicy", async () => {
|
it("allows direct messages regardless of groupPolicy", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: {
|
channels: {
|
||||||
@@ -220,7 +71,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("allows direct messages with tg/Telegram-prefixed allowFrom entries", async () => {
|
it("allows direct messages with tg/Telegram-prefixed allowFrom entries", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: {
|
channels: {
|
||||||
@@ -248,7 +98,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("allows direct messages with telegram:-prefixed allowFrom entries", async () => {
|
it("allows direct messages with telegram:-prefixed allowFrom entries", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: {
|
channels: {
|
||||||
@@ -276,7 +125,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("matches direct message allowFrom against sender user id when chat id differs", async () => {
|
it("matches direct message allowFrom against sender user id when chat id differs", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: {
|
channels: {
|
||||||
@@ -304,7 +152,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("falls back to direct message chat id when sender user id is missing", async () => {
|
it("falls back to direct message chat id when sender user id is missing", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: {
|
channels: {
|
||||||
@@ -331,7 +178,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("allows group messages with wildcard in allowFrom when groupPolicy is 'allowlist'", async () => {
|
it("allows group messages with wildcard in allowFrom when groupPolicy is 'allowlist'", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: {
|
channels: {
|
||||||
@@ -361,7 +207,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("blocks group messages with no sender ID when groupPolicy is 'allowlist'", async () => {
|
it("blocks group messages with no sender ID when groupPolicy is 'allowlist'", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: {
|
channels: {
|
||||||
@@ -390,7 +235,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("matches telegram:-prefixed allowFrom entries in group allowlist", async () => {
|
it("matches telegram:-prefixed allowFrom entries in group allowlist", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: {
|
channels: {
|
||||||
|
|||||||
@@ -1,167 +1,23 @@
|
|||||||
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { resetInboundDedupe } from "../auto-reply/reply/inbound-dedupe.js";
|
import {
|
||||||
|
getLoadConfigMock,
|
||||||
|
getLoadWebMediaMock,
|
||||||
|
getOnHandler,
|
||||||
|
onSpy,
|
||||||
|
replySpy,
|
||||||
|
sendAnimationSpy,
|
||||||
|
sendPhotoSpy,
|
||||||
|
} from "./bot.create-telegram-bot.test-harness.js";
|
||||||
import { createTelegramBot } from "./bot.js";
|
import { createTelegramBot } from "./bot.js";
|
||||||
|
|
||||||
const { sessionStorePath } = vi.hoisted(() => ({
|
const loadConfig = getLoadConfigMock();
|
||||||
sessionStorePath: `/tmp/openclaw-telegram-${Math.random().toString(16).slice(2)}.json`,
|
const loadWebMedia = getLoadWebMediaMock();
|
||||||
}));
|
|
||||||
|
|
||||||
const { loadWebMedia } = vi.hoisted(() => ({
|
|
||||||
loadWebMedia: vi.fn(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../web/media.js", () => ({
|
|
||||||
loadWebMedia,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const { loadConfig } = vi.hoisted(() => ({
|
|
||||||
loadConfig: vi.fn(() => ({})),
|
|
||||||
}));
|
|
||||||
vi.mock("../config/config.js", async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof import("../config/config.js")>();
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
loadConfig,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
vi.mock("../config/sessions.js", async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof import("../config/sessions.js")>();
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
resolveStorePath: vi.fn((storePath) => storePath ?? sessionStorePath),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
const { readChannelAllowFromStore, upsertChannelPairingRequest } = vi.hoisted(() => ({
|
|
||||||
readChannelAllowFromStore: vi.fn(async () => [] as string[]),
|
|
||||||
upsertChannelPairingRequest: vi.fn(async () => ({
|
|
||||||
code: "PAIRCODE",
|
|
||||||
created: true,
|
|
||||||
})),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../pairing/pairing-store.js", () => ({
|
|
||||||
readChannelAllowFromStore,
|
|
||||||
upsertChannelPairingRequest,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const useSpy = vi.fn();
|
|
||||||
const middlewareUseSpy = vi.fn();
|
|
||||||
const onSpy = vi.fn();
|
|
||||||
const stopSpy = vi.fn();
|
|
||||||
const commandSpy = vi.fn();
|
|
||||||
const botCtorSpy = vi.fn();
|
|
||||||
const answerCallbackQuerySpy = vi.fn(async () => undefined);
|
|
||||||
const sendChatActionSpy = vi.fn();
|
|
||||||
const setMessageReactionSpy = vi.fn(async () => undefined);
|
|
||||||
const setMyCommandsSpy = vi.fn(async () => undefined);
|
|
||||||
const sendMessageSpy = vi.fn(async () => ({ message_id: 77 }));
|
|
||||||
const sendAnimationSpy = vi.fn(async () => ({ message_id: 78 }));
|
|
||||||
const sendPhotoSpy = vi.fn(async () => ({ message_id: 79 }));
|
|
||||||
type ApiStub = {
|
|
||||||
config: { use: (arg: unknown) => void };
|
|
||||||
answerCallbackQuery: typeof answerCallbackQuerySpy;
|
|
||||||
sendChatAction: typeof sendChatActionSpy;
|
|
||||||
setMessageReaction: typeof setMessageReactionSpy;
|
|
||||||
setMyCommands: typeof setMyCommandsSpy;
|
|
||||||
sendMessage: typeof sendMessageSpy;
|
|
||||||
sendAnimation: typeof sendAnimationSpy;
|
|
||||||
sendPhoto: typeof sendPhotoSpy;
|
|
||||||
};
|
|
||||||
const apiStub: ApiStub = {
|
|
||||||
config: { use: useSpy },
|
|
||||||
answerCallbackQuery: answerCallbackQuerySpy,
|
|
||||||
sendChatAction: sendChatActionSpy,
|
|
||||||
setMessageReaction: setMessageReactionSpy,
|
|
||||||
setMyCommands: setMyCommandsSpy,
|
|
||||||
sendMessage: sendMessageSpy,
|
|
||||||
sendAnimation: sendAnimationSpy,
|
|
||||||
sendPhoto: sendPhotoSpy,
|
|
||||||
};
|
|
||||||
|
|
||||||
vi.mock("grammy", () => ({
|
|
||||||
Bot: class {
|
|
||||||
api = apiStub;
|
|
||||||
use = middlewareUseSpy;
|
|
||||||
on = onSpy;
|
|
||||||
stop = stopSpy;
|
|
||||||
command = commandSpy;
|
|
||||||
catch = vi.fn();
|
|
||||||
constructor(
|
|
||||||
public token: string,
|
|
||||||
public options?: { client?: { fetch?: typeof fetch } },
|
|
||||||
) {
|
|
||||||
botCtorSpy(token, options);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
InputFile: class {},
|
|
||||||
webhookCallback: vi.fn(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
const sequentializeMiddleware = vi.fn();
|
|
||||||
const sequentializeSpy = vi.fn(() => sequentializeMiddleware);
|
|
||||||
let _sequentializeKey: ((ctx: unknown) => string) | undefined;
|
|
||||||
vi.mock("@grammyjs/runner", () => ({
|
|
||||||
sequentialize: (keyFn: (ctx: unknown) => string) => {
|
|
||||||
_sequentializeKey = keyFn;
|
|
||||||
return sequentializeSpy();
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
|
|
||||||
const throttlerSpy = vi.fn(() => "throttler");
|
|
||||||
|
|
||||||
vi.mock("@grammyjs/transformer-throttler", () => ({
|
|
||||||
apiThrottler: () => throttlerSpy(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../auto-reply/reply.js", () => {
|
|
||||||
const replySpy = vi.fn(async (_ctx, opts) => {
|
|
||||||
await opts?.onReplyStart?.();
|
|
||||||
return undefined;
|
|
||||||
});
|
|
||||||
return { getReplyFromConfig: replySpy, __replySpy: replySpy };
|
|
||||||
});
|
|
||||||
|
|
||||||
let replyModule: typeof import("../auto-reply/reply.js");
|
|
||||||
|
|
||||||
const getOnHandler = (event: string) => {
|
|
||||||
const handler = onSpy.mock.calls.find((call) => call[0] === event)?.[1];
|
|
||||||
if (!handler) {
|
|
||||||
throw new Error(`Missing handler for event: ${event}`);
|
|
||||||
}
|
|
||||||
return handler as (ctx: Record<string, unknown>) => Promise<void>;
|
|
||||||
};
|
|
||||||
|
|
||||||
describe("createTelegramBot", () => {
|
describe("createTelegramBot", () => {
|
||||||
beforeAll(async () => {
|
|
||||||
replyModule = await import("../auto-reply/reply.js");
|
|
||||||
});
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
resetInboundDedupe();
|
|
||||||
loadConfig.mockReturnValue({
|
|
||||||
channels: {
|
|
||||||
telegram: { dmPolicy: "open", allowFrom: ["*"] },
|
|
||||||
},
|
|
||||||
});
|
|
||||||
loadWebMedia.mockReset();
|
|
||||||
sendAnimationSpy.mockReset();
|
|
||||||
sendPhotoSpy.mockReset();
|
|
||||||
setMessageReactionSpy.mockReset();
|
|
||||||
answerCallbackQuerySpy.mockReset();
|
|
||||||
setMyCommandsSpy.mockReset();
|
|
||||||
middlewareUseSpy.mockReset();
|
|
||||||
sequentializeSpy.mockReset();
|
|
||||||
botCtorSpy.mockReset();
|
|
||||||
_sequentializeKey = undefined;
|
|
||||||
});
|
|
||||||
|
|
||||||
// groupPolicy tests
|
// groupPolicy tests
|
||||||
|
|
||||||
it("routes DMs by telegram accountId binding", async () => {
|
it("routes DMs by telegram accountId binding", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
|
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
@@ -205,7 +61,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("allows per-group requireMention override", async () => {
|
it("allows per-group requireMention override", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: {
|
channels: {
|
||||||
@@ -236,7 +91,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("allows per-topic requireMention override", async () => {
|
it("allows per-topic requireMention override", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: {
|
channels: {
|
||||||
@@ -278,7 +132,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("honors groups default when no explicit group override exists", async () => {
|
it("honors groups default when no explicit group override exists", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: {
|
channels: {
|
||||||
@@ -306,7 +159,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("does not block group messages when bot username is unknown", async () => {
|
it("does not block group messages when bot username is unknown", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: {
|
channels: {
|
||||||
@@ -333,7 +185,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("routes forum topic messages using parent group binding", async () => {
|
it("routes forum topic messages using parent group binding", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
|
|
||||||
// Binding specifies the base group ID without topic suffix.
|
// Binding specifies the base group ID without topic suffix.
|
||||||
@@ -389,7 +240,6 @@ describe("createTelegramBot", () => {
|
|||||||
|
|
||||||
it("prefers specific topic binding over parent group binding", async () => {
|
it("prefers specific topic binding over parent group binding", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
|
|
||||||
// Both a specific topic binding and a parent group binding are configured.
|
// Both a specific topic binding and a parent group binding are configured.
|
||||||
@@ -451,7 +301,6 @@ describe("createTelegramBot", () => {
|
|||||||
|
|
||||||
it("sends GIF replies as animations", async () => {
|
it("sends GIF replies as animations", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
|
|
||||||
replySpy.mockResolvedValueOnce({
|
replySpy.mockResolvedValueOnce({
|
||||||
|
|||||||
@@ -1,173 +1,24 @@
|
|||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { resetInboundDedupe } from "../auto-reply/reply/inbound-dedupe.js";
|
import {
|
||||||
|
getOnHandler,
|
||||||
|
getLoadConfigMock,
|
||||||
|
onSpy,
|
||||||
|
replySpy,
|
||||||
|
sendMessageSpy,
|
||||||
|
} from "./bot.create-telegram-bot.test-harness.js";
|
||||||
import { createTelegramBot } from "./bot.js";
|
import { createTelegramBot } from "./bot.js";
|
||||||
|
|
||||||
const { sessionStorePath } = vi.hoisted(() => ({
|
const loadConfig = getLoadConfigMock();
|
||||||
sessionStorePath: `/tmp/openclaw-telegram-reply-threading-${Math.random()
|
|
||||||
.toString(16)
|
|
||||||
.slice(2)}.json`,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const { loadWebMedia } = vi.hoisted(() => ({
|
|
||||||
loadWebMedia: vi.fn(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../web/media.js", () => ({
|
|
||||||
loadWebMedia,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const { loadConfig } = vi.hoisted(() => ({
|
|
||||||
loadConfig: vi.fn(() => ({})),
|
|
||||||
}));
|
|
||||||
vi.mock("../config/config.js", async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof import("../config/config.js")>();
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
loadConfig,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
vi.mock("../config/sessions.js", async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof import("../config/sessions.js")>();
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
resolveStorePath: vi.fn((storePath) => storePath ?? sessionStorePath),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
const { readChannelAllowFromStore, upsertChannelPairingRequest } = vi.hoisted(() => ({
|
|
||||||
readChannelAllowFromStore: vi.fn(async () => [] as string[]),
|
|
||||||
upsertChannelPairingRequest: vi.fn(async () => ({
|
|
||||||
code: "PAIRCODE",
|
|
||||||
created: true,
|
|
||||||
})),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../pairing/pairing-store.js", () => ({
|
|
||||||
readChannelAllowFromStore,
|
|
||||||
upsertChannelPairingRequest,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const useSpy = vi.fn();
|
|
||||||
const middlewareUseSpy = vi.fn();
|
|
||||||
const onSpy = vi.fn();
|
|
||||||
const stopSpy = vi.fn();
|
|
||||||
const commandSpy = vi.fn();
|
|
||||||
const botCtorSpy = vi.fn();
|
|
||||||
const answerCallbackQuerySpy = vi.fn(async () => undefined);
|
|
||||||
const sendChatActionSpy = vi.fn();
|
|
||||||
const setMessageReactionSpy = vi.fn(async () => undefined);
|
|
||||||
const setMyCommandsSpy = vi.fn(async () => undefined);
|
|
||||||
const sendMessageSpy = vi.fn(async () => ({ message_id: 77 }));
|
|
||||||
const sendAnimationSpy = vi.fn(async () => ({ message_id: 78 }));
|
|
||||||
const sendPhotoSpy = vi.fn(async () => ({ message_id: 79 }));
|
|
||||||
type ApiStub = {
|
|
||||||
config: { use: (arg: unknown) => void };
|
|
||||||
answerCallbackQuery: typeof answerCallbackQuerySpy;
|
|
||||||
sendChatAction: typeof sendChatActionSpy;
|
|
||||||
setMessageReaction: typeof setMessageReactionSpy;
|
|
||||||
setMyCommands: typeof setMyCommandsSpy;
|
|
||||||
sendMessage: typeof sendMessageSpy;
|
|
||||||
sendAnimation: typeof sendAnimationSpy;
|
|
||||||
sendPhoto: typeof sendPhotoSpy;
|
|
||||||
};
|
|
||||||
const apiStub: ApiStub = {
|
|
||||||
config: { use: useSpy },
|
|
||||||
answerCallbackQuery: answerCallbackQuerySpy,
|
|
||||||
sendChatAction: sendChatActionSpy,
|
|
||||||
setMessageReaction: setMessageReactionSpy,
|
|
||||||
setMyCommands: setMyCommandsSpy,
|
|
||||||
sendMessage: sendMessageSpy,
|
|
||||||
sendAnimation: sendAnimationSpy,
|
|
||||||
sendPhoto: sendPhotoSpy,
|
|
||||||
};
|
|
||||||
|
|
||||||
vi.mock("grammy", () => ({
|
|
||||||
Bot: class {
|
|
||||||
api = apiStub;
|
|
||||||
use = middlewareUseSpy;
|
|
||||||
on = onSpy;
|
|
||||||
stop = stopSpy;
|
|
||||||
command = commandSpy;
|
|
||||||
catch = vi.fn();
|
|
||||||
constructor(
|
|
||||||
public token: string,
|
|
||||||
public options?: { client?: { fetch?: typeof fetch } },
|
|
||||||
) {
|
|
||||||
botCtorSpy(token, options);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
InputFile: class {},
|
|
||||||
webhookCallback: vi.fn(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
const sequentializeMiddleware = vi.fn();
|
|
||||||
const sequentializeSpy = vi.fn(() => sequentializeMiddleware);
|
|
||||||
let _sequentializeKey: ((ctx: unknown) => string) | undefined;
|
|
||||||
vi.mock("@grammyjs/runner", () => ({
|
|
||||||
sequentialize: (keyFn: (ctx: unknown) => string) => {
|
|
||||||
_sequentializeKey = keyFn;
|
|
||||||
return sequentializeSpy();
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
|
|
||||||
const throttlerSpy = vi.fn(() => "throttler");
|
|
||||||
|
|
||||||
vi.mock("@grammyjs/transformer-throttler", () => ({
|
|
||||||
apiThrottler: () => throttlerSpy(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../auto-reply/reply.js", () => {
|
|
||||||
const replySpy = vi.fn(async (_ctx, opts) => {
|
|
||||||
await opts?.onReplyStart?.();
|
|
||||||
return undefined;
|
|
||||||
});
|
|
||||||
return { getReplyFromConfig: replySpy, __replySpy: replySpy };
|
|
||||||
});
|
|
||||||
|
|
||||||
let replyModule: typeof import("../auto-reply/reply.js");
|
|
||||||
|
|
||||||
const getOnHandler = (event: string) => {
|
|
||||||
const handler = onSpy.mock.calls.find((call) => call[0] === event)?.[1];
|
|
||||||
if (!handler) {
|
|
||||||
throw new Error(`Missing handler for event: ${event}`);
|
|
||||||
}
|
|
||||||
return handler as (ctx: Record<string, unknown>) => Promise<void>;
|
|
||||||
};
|
|
||||||
|
|
||||||
describe("createTelegramBot", () => {
|
describe("createTelegramBot", () => {
|
||||||
beforeAll(async () => {
|
|
||||||
replyModule = await import("../auto-reply/reply.js");
|
|
||||||
});
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
resetInboundDedupe();
|
|
||||||
loadConfig.mockReturnValue({
|
|
||||||
channels: {
|
|
||||||
telegram: { dmPolicy: "open", allowFrom: ["*"] },
|
|
||||||
},
|
|
||||||
});
|
|
||||||
loadWebMedia.mockReset();
|
|
||||||
sendAnimationSpy.mockReset();
|
|
||||||
sendPhotoSpy.mockReset();
|
|
||||||
setMessageReactionSpy.mockReset();
|
|
||||||
answerCallbackQuerySpy.mockReset();
|
|
||||||
setMyCommandsSpy.mockReset();
|
|
||||||
middlewareUseSpy.mockReset();
|
|
||||||
sequentializeSpy.mockReset();
|
|
||||||
botCtorSpy.mockReset();
|
|
||||||
_sequentializeKey = undefined;
|
|
||||||
});
|
|
||||||
|
|
||||||
// groupPolicy tests
|
// groupPolicy tests
|
||||||
|
|
||||||
it("sends replies without native reply threading", async () => {
|
it("sends replies without native reply threading", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
sendMessageSpy.mockReset();
|
sendMessageSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
replySpy.mockResolvedValue({ text: "a".repeat(4500) });
|
replySpy.mockResolvedValue({ text: "a".repeat(4500) });
|
||||||
|
|
||||||
@@ -192,7 +43,6 @@ describe("createTelegramBot", () => {
|
|||||||
it("honors replyToMode=first for threaded replies", async () => {
|
it("honors replyToMode=first for threaded replies", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
sendMessageSpy.mockReset();
|
sendMessageSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
replySpy.mockResolvedValue({
|
replySpy.mockResolvedValue({
|
||||||
text: "a".repeat(4500),
|
text: "a".repeat(4500),
|
||||||
@@ -222,7 +72,6 @@ describe("createTelegramBot", () => {
|
|||||||
it("prefixes final replies with responsePrefix", async () => {
|
it("prefixes final replies with responsePrefix", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
sendMessageSpy.mockReset();
|
sendMessageSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
replySpy.mockResolvedValue({ text: "final reply" });
|
replySpy.mockResolvedValue({ text: "final reply" });
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
@@ -250,7 +99,6 @@ describe("createTelegramBot", () => {
|
|||||||
it("honors replyToMode=all for threaded replies", async () => {
|
it("honors replyToMode=all for threaded replies", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
sendMessageSpy.mockReset();
|
sendMessageSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
replySpy.mockResolvedValue({
|
replySpy.mockResolvedValue({
|
||||||
text: "a".repeat(4500),
|
text: "a".repeat(4500),
|
||||||
@@ -277,7 +125,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("blocks group messages when telegram.groups is set without a wildcard", async () => {
|
it("blocks group messages when telegram.groups is set without a wildcard", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: {
|
channels: {
|
||||||
@@ -306,7 +153,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("skips group messages without mention when requireMention is enabled", async () => {
|
it("skips group messages without mention when requireMention is enabled", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: {
|
channels: {
|
||||||
@@ -331,7 +177,6 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
it("honors routed group activation from session store", async () => {
|
it("honors routed group activation from session store", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
const storeDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-telegram-"));
|
const storeDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-telegram-"));
|
||||||
const storePath = path.join(storeDir, "sessions.json");
|
const storePath = path.join(storeDir, "sessions.json");
|
||||||
|
|||||||
201
src/telegram/bot.create-telegram-bot.test-harness.ts
Normal file
201
src/telegram/bot.create-telegram-bot.test-harness.ts
Normal file
@@ -0,0 +1,201 @@
|
|||||||
|
import { beforeEach, vi } from "vitest";
|
||||||
|
import { resetInboundDedupe } from "../auto-reply/reply/inbound-dedupe.js";
|
||||||
|
|
||||||
|
const { sessionStorePath } = vi.hoisted(() => ({
|
||||||
|
sessionStorePath: `/tmp/openclaw-telegram-${Math.random().toString(16).slice(2)}.json`,
|
||||||
|
}));
|
||||||
|
|
||||||
|
const { loadWebMedia } = vi.hoisted(() => ({
|
||||||
|
loadWebMedia: vi.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
export function getLoadWebMediaMock() {
|
||||||
|
return loadWebMedia;
|
||||||
|
}
|
||||||
|
|
||||||
|
vi.mock("../web/media.js", () => ({
|
||||||
|
loadWebMedia,
|
||||||
|
}));
|
||||||
|
|
||||||
|
const { loadConfig } = vi.hoisted(() => ({
|
||||||
|
loadConfig: vi.fn(() => ({})),
|
||||||
|
}));
|
||||||
|
|
||||||
|
export function getLoadConfigMock() {
|
||||||
|
return loadConfig;
|
||||||
|
}
|
||||||
|
vi.mock("../config/config.js", async (importOriginal) => {
|
||||||
|
const actual = await importOriginal<typeof import("../config/config.js")>();
|
||||||
|
return {
|
||||||
|
...actual,
|
||||||
|
loadConfig,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
vi.mock("../config/sessions.js", async (importOriginal) => {
|
||||||
|
const actual = await importOriginal<typeof import("../config/sessions.js")>();
|
||||||
|
return {
|
||||||
|
...actual,
|
||||||
|
resolveStorePath: vi.fn((storePath) => storePath ?? sessionStorePath),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
const { readChannelAllowFromStore, upsertChannelPairingRequest } = vi.hoisted(() => ({
|
||||||
|
readChannelAllowFromStore: vi.fn(async () => [] as string[]),
|
||||||
|
upsertChannelPairingRequest: vi.fn(async () => ({
|
||||||
|
code: "PAIRCODE",
|
||||||
|
created: true,
|
||||||
|
})),
|
||||||
|
}));
|
||||||
|
|
||||||
|
export function getReadChannelAllowFromStoreMock() {
|
||||||
|
return readChannelAllowFromStore;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getUpsertChannelPairingRequestMock() {
|
||||||
|
return upsertChannelPairingRequest;
|
||||||
|
}
|
||||||
|
|
||||||
|
vi.mock("../pairing/pairing-store.js", () => ({
|
||||||
|
readChannelAllowFromStore,
|
||||||
|
upsertChannelPairingRequest,
|
||||||
|
}));
|
||||||
|
|
||||||
|
export const useSpy = vi.fn();
|
||||||
|
export const middlewareUseSpy = vi.fn();
|
||||||
|
export const onSpy = vi.fn();
|
||||||
|
export const stopSpy = vi.fn();
|
||||||
|
export const commandSpy = vi.fn();
|
||||||
|
export const botCtorSpy = vi.fn();
|
||||||
|
export const answerCallbackQuerySpy = vi.fn(async () => undefined);
|
||||||
|
export const sendChatActionSpy = vi.fn();
|
||||||
|
export const setMessageReactionSpy = vi.fn(async () => undefined);
|
||||||
|
export const setMyCommandsSpy = vi.fn(async () => undefined);
|
||||||
|
export const deleteMyCommandsSpy = vi.fn(async () => undefined);
|
||||||
|
export const getMeSpy = vi.fn(async () => ({
|
||||||
|
username: "openclaw_bot",
|
||||||
|
has_topics_enabled: true,
|
||||||
|
}));
|
||||||
|
export const sendMessageSpy = vi.fn(async () => ({ message_id: 77 }));
|
||||||
|
export const sendAnimationSpy = vi.fn(async () => ({ message_id: 78 }));
|
||||||
|
export const sendPhotoSpy = vi.fn(async () => ({ message_id: 79 }));
|
||||||
|
|
||||||
|
type ApiStub = {
|
||||||
|
config: { use: (arg: unknown) => void };
|
||||||
|
answerCallbackQuery: typeof answerCallbackQuerySpy;
|
||||||
|
sendChatAction: typeof sendChatActionSpy;
|
||||||
|
setMessageReaction: typeof setMessageReactionSpy;
|
||||||
|
setMyCommands: typeof setMyCommandsSpy;
|
||||||
|
deleteMyCommands: typeof deleteMyCommandsSpy;
|
||||||
|
getMe: typeof getMeSpy;
|
||||||
|
sendMessage: typeof sendMessageSpy;
|
||||||
|
sendAnimation: typeof sendAnimationSpy;
|
||||||
|
sendPhoto: typeof sendPhotoSpy;
|
||||||
|
};
|
||||||
|
|
||||||
|
const apiStub: ApiStub = {
|
||||||
|
config: { use: useSpy },
|
||||||
|
answerCallbackQuery: answerCallbackQuerySpy,
|
||||||
|
sendChatAction: sendChatActionSpy,
|
||||||
|
setMessageReaction: setMessageReactionSpy,
|
||||||
|
setMyCommands: setMyCommandsSpy,
|
||||||
|
deleteMyCommands: deleteMyCommandsSpy,
|
||||||
|
getMe: getMeSpy,
|
||||||
|
sendMessage: sendMessageSpy,
|
||||||
|
sendAnimation: sendAnimationSpy,
|
||||||
|
sendPhoto: sendPhotoSpy,
|
||||||
|
};
|
||||||
|
|
||||||
|
vi.mock("grammy", () => ({
|
||||||
|
Bot: class {
|
||||||
|
api = apiStub;
|
||||||
|
use = middlewareUseSpy;
|
||||||
|
on = onSpy;
|
||||||
|
stop = stopSpy;
|
||||||
|
command = commandSpy;
|
||||||
|
catch = vi.fn();
|
||||||
|
constructor(
|
||||||
|
public token: string,
|
||||||
|
public options?: { client?: { fetch?: typeof fetch } },
|
||||||
|
) {
|
||||||
|
botCtorSpy(token, options);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
InputFile: class {},
|
||||||
|
webhookCallback: vi.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
const sequentializeMiddleware = vi.fn();
|
||||||
|
export const sequentializeSpy = vi.fn(() => sequentializeMiddleware);
|
||||||
|
export let sequentializeKey: ((ctx: unknown) => string) | undefined;
|
||||||
|
vi.mock("@grammyjs/runner", () => ({
|
||||||
|
sequentialize: (keyFn: (ctx: unknown) => string) => {
|
||||||
|
sequentializeKey = keyFn;
|
||||||
|
return sequentializeSpy();
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
export const throttlerSpy = vi.fn(() => "throttler");
|
||||||
|
|
||||||
|
vi.mock("@grammyjs/transformer-throttler", () => ({
|
||||||
|
apiThrottler: () => throttlerSpy(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
export const replySpy = vi.fn(async (_ctx, opts) => {
|
||||||
|
await opts?.onReplyStart?.();
|
||||||
|
return undefined;
|
||||||
|
});
|
||||||
|
|
||||||
|
vi.mock("../auto-reply/reply.js", () => ({
|
||||||
|
getReplyFromConfig: replySpy,
|
||||||
|
__replySpy: replySpy,
|
||||||
|
}));
|
||||||
|
|
||||||
|
export const getOnHandler = (event: string) => {
|
||||||
|
const handler = onSpy.mock.calls.find((call) => call[0] === event)?.[1];
|
||||||
|
if (!handler) {
|
||||||
|
throw new Error(`Missing handler for event: ${event}`);
|
||||||
|
}
|
||||||
|
return handler as (ctx: Record<string, unknown>) => Promise<void>;
|
||||||
|
};
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
resetInboundDedupe();
|
||||||
|
loadConfig.mockReturnValue({
|
||||||
|
channels: {
|
||||||
|
telegram: { dmPolicy: "open", allowFrom: ["*"] },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
loadWebMedia.mockReset();
|
||||||
|
onSpy.mockReset();
|
||||||
|
commandSpy.mockReset();
|
||||||
|
stopSpy.mockReset();
|
||||||
|
useSpy.mockReset();
|
||||||
|
|
||||||
|
sendAnimationSpy.mockReset();
|
||||||
|
sendAnimationSpy.mockResolvedValue({ message_id: 78 });
|
||||||
|
sendPhotoSpy.mockReset();
|
||||||
|
sendPhotoSpy.mockResolvedValue({ message_id: 79 });
|
||||||
|
sendMessageSpy.mockReset();
|
||||||
|
sendMessageSpy.mockResolvedValue({ message_id: 77 });
|
||||||
|
|
||||||
|
setMessageReactionSpy.mockReset();
|
||||||
|
setMessageReactionSpy.mockResolvedValue(undefined);
|
||||||
|
answerCallbackQuerySpy.mockReset();
|
||||||
|
answerCallbackQuerySpy.mockResolvedValue(undefined);
|
||||||
|
sendChatActionSpy.mockReset();
|
||||||
|
sendChatActionSpy.mockResolvedValue(undefined);
|
||||||
|
setMyCommandsSpy.mockReset();
|
||||||
|
setMyCommandsSpy.mockResolvedValue(undefined);
|
||||||
|
deleteMyCommandsSpy.mockReset();
|
||||||
|
deleteMyCommandsSpy.mockResolvedValue(undefined);
|
||||||
|
getMeSpy.mockReset();
|
||||||
|
getMeSpy.mockResolvedValue({
|
||||||
|
username: "openclaw_bot",
|
||||||
|
has_topics_enabled: true,
|
||||||
|
});
|
||||||
|
middlewareUseSpy.mockReset();
|
||||||
|
sequentializeSpy.mockReset();
|
||||||
|
botCtorSpy.mockReset();
|
||||||
|
sequentializeKey = undefined;
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user