mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 09:02:45 +00:00
test(telegram): dedupe native-command test setup
This commit is contained in:
@@ -1,8 +1,7 @@
|
|||||||
import { describe, expect, it, vi } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
import type { OpenClawConfig } from "../config/config.js";
|
import type { OpenClawConfig } from "../config/config.js";
|
||||||
import type { TelegramAccountConfig } from "../config/types.js";
|
|
||||||
import type { RuntimeEnv } from "../runtime.js";
|
|
||||||
import { registerTelegramNativeCommands } from "./bot-native-commands.js";
|
import { registerTelegramNativeCommands } from "./bot-native-commands.js";
|
||||||
|
import { createNativeCommandTestParams } from "./bot-native-commands.test-helpers.js";
|
||||||
|
|
||||||
// All mocks scoped to this file only — does not affect bot-native-commands.test.ts
|
// All mocks scoped to this file only — does not affect bot-native-commands.test.ts
|
||||||
|
|
||||||
@@ -43,35 +42,6 @@ vi.mock("./bot/delivery.js", () => ({
|
|||||||
deliverReplies: vi.fn(async () => ({ delivered: true })),
|
deliverReplies: vi.fn(async () => ({ delivered: true })),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const buildParams = (cfg: OpenClawConfig, accountId = "default") => ({
|
|
||||||
bot: {
|
|
||||||
api: {
|
|
||||||
setMyCommands: vi.fn().mockResolvedValue(undefined),
|
|
||||||
sendMessage: vi.fn().mockResolvedValue(undefined),
|
|
||||||
},
|
|
||||||
command: vi.fn(),
|
|
||||||
} as unknown as Parameters<typeof registerTelegramNativeCommands>[0]["bot"],
|
|
||||||
cfg,
|
|
||||||
runtime: {} as unknown as RuntimeEnv,
|
|
||||||
accountId,
|
|
||||||
telegramCfg: {} as TelegramAccountConfig,
|
|
||||||
allowFrom: [],
|
|
||||||
groupAllowFrom: [],
|
|
||||||
replyToMode: "off" as const,
|
|
||||||
textLimit: 4096,
|
|
||||||
useAccessGroups: false,
|
|
||||||
nativeEnabled: true,
|
|
||||||
nativeSkillsEnabled: true,
|
|
||||||
nativeDisabledExplicit: false,
|
|
||||||
resolveGroupPolicy: () => ({ allowlistEnabled: false, allowed: true }),
|
|
||||||
resolveTelegramGroupConfig: () => ({
|
|
||||||
groupConfig: undefined,
|
|
||||||
topicConfig: undefined,
|
|
||||||
}),
|
|
||||||
shouldSkipUpdate: () => false,
|
|
||||||
opts: { token: "token" },
|
|
||||||
});
|
|
||||||
|
|
||||||
function createDeferred<T>() {
|
function createDeferred<T>() {
|
||||||
let resolve!: (value: T | PromiseLike<T>) => void;
|
let resolve!: (value: T | PromiseLike<T>) => void;
|
||||||
const promise = new Promise<T>((res) => {
|
const promise = new Promise<T>((res) => {
|
||||||
@@ -80,39 +50,51 @@ function createDeferred<T>() {
|
|||||||
return { promise, resolve };
|
return { promise, resolve };
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("registerTelegramNativeCommands — session metadata", () => {
|
type TelegramCommandHandler = (ctx: unknown) => Promise<void>;
|
||||||
it("calls recordSessionMetaFromInbound after a native slash command", async () => {
|
|
||||||
sessionMocks.recordSessionMetaFromInbound.mockReset().mockResolvedValue(undefined);
|
|
||||||
sessionMocks.resolveStorePath.mockReset().mockReturnValue("/tmp/openclaw-sessions.json");
|
|
||||||
|
|
||||||
const commandHandlers = new Map<string, (ctx: unknown) => Promise<void>>();
|
function buildStatusCommandContext() {
|
||||||
const cfg: OpenClawConfig = {};
|
return {
|
||||||
|
match: "",
|
||||||
|
message: {
|
||||||
|
message_id: 1,
|
||||||
|
date: Math.floor(Date.now() / 1000),
|
||||||
|
chat: { id: 100, type: "private" as const },
|
||||||
|
from: { id: 200, username: "bob" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
registerTelegramNativeCommands({
|
function registerAndResolveStatusHandler(cfg: OpenClawConfig): TelegramCommandHandler {
|
||||||
...buildParams(cfg),
|
const commandHandlers = new Map<string, TelegramCommandHandler>();
|
||||||
allowFrom: ["*"],
|
registerTelegramNativeCommands({
|
||||||
|
...createNativeCommandTestParams({
|
||||||
bot: {
|
bot: {
|
||||||
api: {
|
api: {
|
||||||
setMyCommands: vi.fn().mockResolvedValue(undefined),
|
setMyCommands: vi.fn().mockResolvedValue(undefined),
|
||||||
sendMessage: vi.fn().mockResolvedValue(undefined),
|
sendMessage: vi.fn().mockResolvedValue(undefined),
|
||||||
},
|
},
|
||||||
command: vi.fn((name: string, cb: (ctx: unknown) => Promise<void>) => {
|
command: vi.fn((name: string, cb: TelegramCommandHandler) => {
|
||||||
commandHandlers.set(name, cb);
|
commandHandlers.set(name, cb);
|
||||||
}),
|
}),
|
||||||
} as unknown as Parameters<typeof registerTelegramNativeCommands>[0]["bot"],
|
} as unknown as Parameters<typeof registerTelegramNativeCommands>[0]["bot"],
|
||||||
});
|
cfg,
|
||||||
|
allowFrom: ["*"],
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
const handler = commandHandlers.get("status");
|
const handler = commandHandlers.get("status");
|
||||||
expect(handler).toBeTruthy();
|
expect(handler).toBeTruthy();
|
||||||
await handler?.({
|
return handler as TelegramCommandHandler;
|
||||||
match: "",
|
}
|
||||||
message: {
|
|
||||||
message_id: 1,
|
describe("registerTelegramNativeCommands — session metadata", () => {
|
||||||
date: Math.floor(Date.now() / 1000),
|
it("calls recordSessionMetaFromInbound after a native slash command", async () => {
|
||||||
chat: { id: 100, type: "private" },
|
sessionMocks.recordSessionMetaFromInbound.mockReset().mockResolvedValue(undefined);
|
||||||
from: { id: 200, username: "bob" },
|
sessionMocks.resolveStorePath.mockReset().mockReturnValue("/tmp/openclaw-sessions.json");
|
||||||
},
|
|
||||||
});
|
const cfg: OpenClawConfig = {};
|
||||||
|
const handler = registerAndResolveStatusHandler(cfg);
|
||||||
|
await handler(buildStatusCommandContext());
|
||||||
|
|
||||||
expect(sessionMocks.recordSessionMetaFromInbound).toHaveBeenCalledTimes(1);
|
expect(sessionMocks.recordSessionMetaFromInbound).toHaveBeenCalledTimes(1);
|
||||||
const call = (
|
const call = (
|
||||||
@@ -130,35 +112,9 @@ describe("registerTelegramNativeCommands — session metadata", () => {
|
|||||||
sessionMocks.resolveStorePath.mockReset().mockReturnValue("/tmp/openclaw-sessions.json");
|
sessionMocks.resolveStorePath.mockReset().mockReturnValue("/tmp/openclaw-sessions.json");
|
||||||
replyMocks.dispatchReplyWithBufferedBlockDispatcher.mockReset().mockResolvedValue(undefined);
|
replyMocks.dispatchReplyWithBufferedBlockDispatcher.mockReset().mockResolvedValue(undefined);
|
||||||
|
|
||||||
const commandHandlers = new Map<string, (ctx: unknown) => Promise<void>>();
|
|
||||||
const cfg: OpenClawConfig = {};
|
const cfg: OpenClawConfig = {};
|
||||||
|
const handler = registerAndResolveStatusHandler(cfg);
|
||||||
registerTelegramNativeCommands({
|
const runPromise = handler(buildStatusCommandContext());
|
||||||
...buildParams(cfg),
|
|
||||||
allowFrom: ["*"],
|
|
||||||
bot: {
|
|
||||||
api: {
|
|
||||||
setMyCommands: vi.fn().mockResolvedValue(undefined),
|
|
||||||
sendMessage: vi.fn().mockResolvedValue(undefined),
|
|
||||||
},
|
|
||||||
command: vi.fn((name: string, cb: (ctx: unknown) => Promise<void>) => {
|
|
||||||
commandHandlers.set(name, cb);
|
|
||||||
}),
|
|
||||||
} as unknown as Parameters<typeof registerTelegramNativeCommands>[0]["bot"],
|
|
||||||
});
|
|
||||||
|
|
||||||
const handler = commandHandlers.get("status");
|
|
||||||
expect(handler).toBeTruthy();
|
|
||||||
|
|
||||||
const runPromise = handler?.({
|
|
||||||
match: "",
|
|
||||||
message: {
|
|
||||||
message_id: 1,
|
|
||||||
date: Math.floor(Date.now() / 1000),
|
|
||||||
chat: { id: 100, type: "private" },
|
|
||||||
from: { id: 200, username: "bob" },
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
await vi.waitFor(() => {
|
await vi.waitFor(() => {
|
||||||
expect(sessionMocks.recordSessionMetaFromInbound).toHaveBeenCalledTimes(1);
|
expect(sessionMocks.recordSessionMetaFromInbound).toHaveBeenCalledTimes(1);
|
||||||
|
|||||||
46
src/telegram/bot-native-commands.test-helpers.ts
Normal file
46
src/telegram/bot-native-commands.test-helpers.ts
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
import type { OpenClawConfig } from "../config/config.js";
|
||||||
|
import type { TelegramAccountConfig } from "../config/types.js";
|
||||||
|
import type { RuntimeEnv } from "../runtime.js";
|
||||||
|
import type { registerTelegramNativeCommands } from "./bot-native-commands.js";
|
||||||
|
|
||||||
|
type RegisterTelegramNativeCommandParams = Parameters<typeof registerTelegramNativeCommands>[0];
|
||||||
|
|
||||||
|
export function createNativeCommandTestParams(params: {
|
||||||
|
bot: RegisterTelegramNativeCommandParams["bot"];
|
||||||
|
cfg?: OpenClawConfig;
|
||||||
|
runtime?: RuntimeEnv;
|
||||||
|
accountId?: string;
|
||||||
|
telegramCfg?: TelegramAccountConfig;
|
||||||
|
allowFrom?: string[];
|
||||||
|
groupAllowFrom?: string[];
|
||||||
|
replyToMode?: RegisterTelegramNativeCommandParams["replyToMode"];
|
||||||
|
textLimit?: number;
|
||||||
|
useAccessGroups?: boolean;
|
||||||
|
nativeEnabled?: boolean;
|
||||||
|
nativeSkillsEnabled?: boolean;
|
||||||
|
nativeDisabledExplicit?: boolean;
|
||||||
|
opts?: RegisterTelegramNativeCommandParams["opts"];
|
||||||
|
}): RegisterTelegramNativeCommandParams {
|
||||||
|
return {
|
||||||
|
bot: params.bot,
|
||||||
|
cfg: params.cfg ?? {},
|
||||||
|
runtime: params.runtime ?? ({} as RuntimeEnv),
|
||||||
|
accountId: params.accountId ?? "default",
|
||||||
|
telegramCfg: params.telegramCfg ?? ({} as TelegramAccountConfig),
|
||||||
|
allowFrom: params.allowFrom ?? [],
|
||||||
|
groupAllowFrom: params.groupAllowFrom ?? [],
|
||||||
|
replyToMode: params.replyToMode ?? "off",
|
||||||
|
textLimit: params.textLimit ?? 4096,
|
||||||
|
useAccessGroups: params.useAccessGroups ?? false,
|
||||||
|
nativeEnabled: params.nativeEnabled ?? true,
|
||||||
|
nativeSkillsEnabled: params.nativeSkillsEnabled ?? true,
|
||||||
|
nativeDisabledExplicit: params.nativeDisabledExplicit ?? false,
|
||||||
|
resolveGroupPolicy: () => ({ allowlistEnabled: false, allowed: true }),
|
||||||
|
resolveTelegramGroupConfig: () => ({
|
||||||
|
groupConfig: undefined,
|
||||||
|
topicConfig: undefined,
|
||||||
|
}),
|
||||||
|
shouldSkipUpdate: () => false,
|
||||||
|
opts: params.opts ?? { token: "token" },
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ import { TELEGRAM_COMMAND_NAME_PATTERN } from "../config/telegram-custom-command
|
|||||||
import type { TelegramAccountConfig } from "../config/types.js";
|
import type { TelegramAccountConfig } from "../config/types.js";
|
||||||
import type { RuntimeEnv } from "../runtime.js";
|
import type { RuntimeEnv } from "../runtime.js";
|
||||||
import { registerTelegramNativeCommands } from "./bot-native-commands.js";
|
import { registerTelegramNativeCommands } from "./bot-native-commands.js";
|
||||||
|
import { createNativeCommandTestParams } from "./bot-native-commands.test-helpers.js";
|
||||||
|
|
||||||
const { listSkillCommandsForAgents } = vi.hoisted(() => ({
|
const { listSkillCommandsForAgents } = vi.hoisted(() => ({
|
||||||
listSkillCommandsForAgents: vi.fn(() => []),
|
listSkillCommandsForAgents: vi.fn(() => []),
|
||||||
@@ -63,34 +64,20 @@ describe("registerTelegramNativeCommands", () => {
|
|||||||
deliveryMocks.deliverReplies.mockResolvedValue({ delivered: true });
|
deliveryMocks.deliverReplies.mockResolvedValue({ delivered: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
const buildParams = (cfg: OpenClawConfig, accountId = "default") => ({
|
const buildParams = (cfg: OpenClawConfig, accountId = "default") =>
|
||||||
bot: {
|
createNativeCommandTestParams({
|
||||||
api: {
|
bot: {
|
||||||
setMyCommands: vi.fn().mockResolvedValue(undefined),
|
api: {
|
||||||
sendMessage: vi.fn().mockResolvedValue(undefined),
|
setMyCommands: vi.fn().mockResolvedValue(undefined),
|
||||||
},
|
sendMessage: vi.fn().mockResolvedValue(undefined),
|
||||||
command: vi.fn(),
|
},
|
||||||
} as unknown as Parameters<typeof registerTelegramNativeCommands>[0]["bot"],
|
command: vi.fn(),
|
||||||
cfg,
|
} as unknown as Parameters<typeof registerTelegramNativeCommands>[0]["bot"],
|
||||||
runtime: {} as unknown as RuntimeEnv,
|
cfg,
|
||||||
accountId,
|
runtime: {} as RuntimeEnv,
|
||||||
telegramCfg: {} as TelegramAccountConfig,
|
accountId,
|
||||||
allowFrom: [],
|
telegramCfg: {} as TelegramAccountConfig,
|
||||||
groupAllowFrom: [],
|
});
|
||||||
replyToMode: "off" as const,
|
|
||||||
textLimit: 4096,
|
|
||||||
useAccessGroups: false,
|
|
||||||
nativeEnabled: true,
|
|
||||||
nativeSkillsEnabled: true,
|
|
||||||
nativeDisabledExplicit: false,
|
|
||||||
resolveGroupPolicy: () => ({ allowlistEnabled: false, allowed: true }),
|
|
||||||
resolveTelegramGroupConfig: () => ({
|
|
||||||
groupConfig: undefined,
|
|
||||||
topicConfig: undefined,
|
|
||||||
}),
|
|
||||||
shouldSkipUpdate: () => false,
|
|
||||||
opts: { token: "token" },
|
|
||||||
});
|
|
||||||
|
|
||||||
it("scopes skill commands when account binding exists", () => {
|
it("scopes skill commands when account binding exists", () => {
|
||||||
const cfg: OpenClawConfig = {
|
const cfg: OpenClawConfig = {
|
||||||
|
|||||||
Reference in New Issue
Block a user