test(telegram): dedupe native-command test setup

This commit is contained in:
Peter Steinberger
2026-02-22 07:48:35 +00:00
parent cd7faea93b
commit 94e5a46187
3 changed files with 98 additions and 109 deletions

View File

@@ -1,8 +1,7 @@
import { describe, expect, it, vi } from "vitest";
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 { createNativeCommandTestParams } from "./bot-native-commands.test-helpers.js";
// 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 })),
}));
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>() {
let resolve!: (value: T | PromiseLike<T>) => void;
const promise = new Promise<T>((res) => {
@@ -80,39 +50,51 @@ function createDeferred<T>() {
return { promise, resolve };
}
describe("registerTelegramNativeCommands — session metadata", () => {
it("calls recordSessionMetaFromInbound after a native slash command", async () => {
sessionMocks.recordSessionMetaFromInbound.mockReset().mockResolvedValue(undefined);
sessionMocks.resolveStorePath.mockReset().mockReturnValue("/tmp/openclaw-sessions.json");
type TelegramCommandHandler = (ctx: unknown) => Promise<void>;
const commandHandlers = new Map<string, (ctx: unknown) => Promise<void>>();
const cfg: OpenClawConfig = {};
function buildStatusCommandContext() {
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({
...buildParams(cfg),
allowFrom: ["*"],
function registerAndResolveStatusHandler(cfg: OpenClawConfig): TelegramCommandHandler {
const commandHandlers = new Map<string, TelegramCommandHandler>();
registerTelegramNativeCommands({
...createNativeCommandTestParams({
bot: {
api: {
setMyCommands: 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);
}),
} as unknown as Parameters<typeof registerTelegramNativeCommands>[0]["bot"],
});
cfg,
allowFrom: ["*"],
}),
});
const handler = commandHandlers.get("status");
expect(handler).toBeTruthy();
await handler?.({
match: "",
message: {
message_id: 1,
date: Math.floor(Date.now() / 1000),
chat: { id: 100, type: "private" },
from: { id: 200, username: "bob" },
},
});
const handler = commandHandlers.get("status");
expect(handler).toBeTruthy();
return handler as TelegramCommandHandler;
}
describe("registerTelegramNativeCommands — session metadata", () => {
it("calls recordSessionMetaFromInbound after a native slash command", async () => {
sessionMocks.recordSessionMetaFromInbound.mockReset().mockResolvedValue(undefined);
sessionMocks.resolveStorePath.mockReset().mockReturnValue("/tmp/openclaw-sessions.json");
const cfg: OpenClawConfig = {};
const handler = registerAndResolveStatusHandler(cfg);
await handler(buildStatusCommandContext());
expect(sessionMocks.recordSessionMetaFromInbound).toHaveBeenCalledTimes(1);
const call = (
@@ -130,35 +112,9 @@ describe("registerTelegramNativeCommands — session metadata", () => {
sessionMocks.resolveStorePath.mockReset().mockReturnValue("/tmp/openclaw-sessions.json");
replyMocks.dispatchReplyWithBufferedBlockDispatcher.mockReset().mockResolvedValue(undefined);
const commandHandlers = new Map<string, (ctx: unknown) => Promise<void>>();
const cfg: OpenClawConfig = {};
registerTelegramNativeCommands({
...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" },
},
});
const handler = registerAndResolveStatusHandler(cfg);
const runPromise = handler(buildStatusCommandContext());
await vi.waitFor(() => {
expect(sessionMocks.recordSessionMetaFromInbound).toHaveBeenCalledTimes(1);

View 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" },
};
}

View File

@@ -6,6 +6,7 @@ import { TELEGRAM_COMMAND_NAME_PATTERN } from "../config/telegram-custom-command
import type { TelegramAccountConfig } from "../config/types.js";
import type { RuntimeEnv } from "../runtime.js";
import { registerTelegramNativeCommands } from "./bot-native-commands.js";
import { createNativeCommandTestParams } from "./bot-native-commands.test-helpers.js";
const { listSkillCommandsForAgents } = vi.hoisted(() => ({
listSkillCommandsForAgents: vi.fn(() => []),
@@ -63,34 +64,20 @@ describe("registerTelegramNativeCommands", () => {
deliveryMocks.deliverReplies.mockResolvedValue({ 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" },
});
const buildParams = (cfg: OpenClawConfig, accountId = "default") =>
createNativeCommandTestParams({
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 RuntimeEnv,
accountId,
telegramCfg: {} as TelegramAccountConfig,
});
it("scopes skill commands when account binding exists", () => {
const cfg: OpenClawConfig = {