refactor: share telegram native command auth harness

This commit is contained in:
Peter Steinberger 2026-03-13 18:48:29 +00:00
parent b1b6c7a982
commit 60dc46ad10
3 changed files with 129 additions and 254 deletions

View File

@ -1,26 +1,12 @@
import { describe, expect, it, vi } from "vitest"; import { describe, expect, it } from "vitest";
import type { OpenClawConfig } from "../config/config.js"; import type { OpenClawConfig } from "../config/config.js";
import type { ChannelGroupPolicy } from "../config/group-policy.js"; import type { ChannelGroupPolicy } from "../config/group-policy.js";
import type { TelegramAccountConfig } from "../config/types.js"; import type { TelegramAccountConfig } from "../config/types.js";
import type { RuntimeEnv } from "../runtime.js"; import {
import { registerTelegramNativeCommands } from "./bot-native-commands.js"; createNativeCommandsHarness,
createTelegramGroupCommandContext,
const getPluginCommandSpecs = vi.hoisted(() => vi.fn(() => [])); findNotAuthorizedCalls,
const matchPluginCommand = vi.hoisted(() => vi.fn(() => null)); } from "./bot-native-commands.test-helpers.js";
const executePluginCommand = vi.hoisted(() => vi.fn(async () => ({ text: "ok" })));
vi.mock("../plugins/commands.js", () => ({
getPluginCommandSpecs,
matchPluginCommand,
executePluginCommand,
}));
const deliverReplies = vi.hoisted(() => vi.fn(async () => {}));
vi.mock("./bot/delivery.js", () => ({ deliverReplies }));
vi.mock("../pairing/pairing-store.js", () => ({
readChannelAllowFromStore: vi.fn(async () => []),
}));
describe("native command auth in groups", () => { describe("native command auth in groups", () => {
function setup(params: { function setup(params: {
@ -32,32 +18,12 @@ describe("native command auth in groups", () => {
groupConfig?: Record<string, unknown>; groupConfig?: Record<string, unknown>;
resolveGroupPolicy?: () => ChannelGroupPolicy; resolveGroupPolicy?: () => ChannelGroupPolicy;
}) { }) {
const handlers: Record<string, (ctx: unknown) => Promise<void>> = {}; return createNativeCommandsHarness({
const sendMessage = vi.fn().mockResolvedValue(undefined);
const bot = {
api: {
setMyCommands: vi.fn().mockResolvedValue(undefined),
sendMessage,
},
command: (name: string, handler: (ctx: unknown) => Promise<void>) => {
handlers[name] = handler;
},
} as const;
registerTelegramNativeCommands({
bot: bot as unknown as Parameters<typeof registerTelegramNativeCommands>[0]["bot"],
cfg: params.cfg ?? ({} as OpenClawConfig), cfg: params.cfg ?? ({} as OpenClawConfig),
runtime: {} as unknown as RuntimeEnv,
accountId: "default",
telegramCfg: params.telegramCfg ?? ({} as TelegramAccountConfig), telegramCfg: params.telegramCfg ?? ({} as TelegramAccountConfig),
allowFrom: params.allowFrom ?? [], allowFrom: params.allowFrom ?? [],
groupAllowFrom: params.groupAllowFrom ?? [], groupAllowFrom: params.groupAllowFrom ?? [],
replyToMode: "off",
textLimit: 4000,
useAccessGroups: params.useAccessGroups ?? false, useAccessGroups: params.useAccessGroups ?? false,
nativeEnabled: true,
nativeSkillsEnabled: false,
nativeDisabledExplicit: false,
resolveGroupPolicy: resolveGroupPolicy:
params.resolveGroupPolicy ?? params.resolveGroupPolicy ??
(() => (() =>
@ -65,15 +31,8 @@ describe("native command auth in groups", () => {
allowlistEnabled: false, allowlistEnabled: false,
allowed: true, allowed: true,
}) as ChannelGroupPolicy), }) as ChannelGroupPolicy),
resolveTelegramGroupConfig: () => ({ groupConfig: params.groupConfig,
groupConfig: params.groupConfig as undefined,
topicConfig: undefined,
}),
shouldSkipUpdate: () => false,
opts: { token: "token" },
}); });
return { handlers, sendMessage };
} }
it("authorizes native commands in groups when sender is in groupAllowFrom", async () => { it("authorizes native commands in groups when sender is in groupAllowFrom", async () => {
@ -83,23 +42,11 @@ describe("native command auth in groups", () => {
// no allowFrom — sender is NOT in DM allowlist // no allowFrom — sender is NOT in DM allowlist
}); });
const ctx = { const ctx = createTelegramGroupCommandContext();
message: {
chat: { id: -100999, type: "supergroup", is_forum: true },
from: { id: 12345, username: "testuser" },
message_thread_id: 42,
message_id: 1,
date: 1700000000,
},
match: "",
};
await handlers.status?.(ctx); await handlers.status?.(ctx);
// should NOT send "not authorized" rejection const notAuthCalls = findNotAuthorizedCalls(sendMessage);
const notAuthCalls = sendMessage.mock.calls.filter(
(call) => typeof call[1] === "string" && call[1].includes("not authorized"),
);
expect(notAuthCalls).toHaveLength(0); expect(notAuthCalls).toHaveLength(0);
}); });
@ -117,22 +64,11 @@ describe("native command auth in groups", () => {
useAccessGroups: true, useAccessGroups: true,
}); });
const ctx = { const ctx = createTelegramGroupCommandContext();
message: {
chat: { id: -100999, type: "supergroup", is_forum: true },
from: { id: 12345, username: "testuser" },
message_thread_id: 42,
message_id: 1,
date: 1700000000,
},
match: "",
};
await handlers.status?.(ctx); await handlers.status?.(ctx);
const notAuthCalls = sendMessage.mock.calls.filter( const notAuthCalls = findNotAuthorizedCalls(sendMessage);
(call) => typeof call[1] === "string" && call[1].includes("not authorized"),
);
expect(notAuthCalls).toHaveLength(0); expect(notAuthCalls).toHaveLength(0);
}); });
@ -149,16 +85,7 @@ describe("native command auth in groups", () => {
useAccessGroups: true, useAccessGroups: true,
}); });
const ctx = { const ctx = createTelegramGroupCommandContext();
message: {
chat: { id: -100999, type: "supergroup", is_forum: true },
from: { id: 12345, username: "testuser" },
message_thread_id: 42,
message_id: 1,
date: 1700000000,
},
match: "",
};
await handlers.status?.(ctx); await handlers.status?.(ctx);
@ -189,16 +116,7 @@ describe("native command auth in groups", () => {
}) as ChannelGroupPolicy, }) as ChannelGroupPolicy,
}); });
const ctx = { const ctx = createTelegramGroupCommandContext();
message: {
chat: { id: -100999, type: "supergroup", is_forum: true },
from: { id: 12345, username: "testuser" },
message_thread_id: 42,
message_id: 1,
date: 1700000000,
},
match: "",
};
await handlers.status?.(ctx); await handlers.status?.(ctx);
@ -226,16 +144,7 @@ describe("native command auth in groups", () => {
}) as ChannelGroupPolicy, }) as ChannelGroupPolicy,
}); });
const ctx = { const ctx = createTelegramGroupCommandContext();
message: {
chat: { id: -100999, type: "supergroup", is_forum: true },
from: { id: 12345, username: "testuser" },
message_thread_id: 42,
message_id: 1,
date: 1700000000,
},
match: "",
};
await handlers.status?.(ctx); await handlers.status?.(ctx);
@ -253,22 +162,13 @@ describe("native command auth in groups", () => {
useAccessGroups: true, useAccessGroups: true,
}); });
const ctx = { const ctx = createTelegramGroupCommandContext({
message: { username: "intruder",
chat: { id: -100999, type: "supergroup", is_forum: true }, });
from: { id: 12345, username: "intruder" },
message_thread_id: 42,
message_id: 1,
date: 1700000000,
},
match: "",
};
await handlers.status?.(ctx); await handlers.status?.(ctx);
const notAuthCalls = sendMessage.mock.calls.filter( const notAuthCalls = findNotAuthorizedCalls(sendMessage);
(call) => typeof call[1] === "string" && call[1].includes("not authorized"),
);
expect(notAuthCalls.length).toBeGreaterThan(0); expect(notAuthCalls.length).toBeGreaterThan(0);
}); });
@ -279,16 +179,9 @@ describe("native command auth in groups", () => {
useAccessGroups: true, useAccessGroups: true,
}); });
const ctx = { const ctx = createTelegramGroupCommandContext({
message: { username: "intruder",
chat: { id: -100999, type: "supergroup", is_forum: true }, });
from: { id: 12345, username: "intruder" },
message_thread_id: 42,
message_id: 1,
date: 1700000000,
},
match: "",
};
await handlers.status?.(ctx); await handlers.status?.(ctx);

View File

@ -1,26 +1,13 @@
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 { ChannelGroupPolicy } from "../config/group-policy.js";
import type { TelegramAccountConfig } from "../config/types.js"; import type { TelegramAccountConfig } from "../config/types.js";
import type { RuntimeEnv } from "../runtime.js"; import {
import { registerTelegramNativeCommands } from "./bot-native-commands.js"; createNativeCommandsHarness,
deliverReplies,
const getPluginCommandSpecs = vi.hoisted(() => vi.fn()); executePluginCommand,
const matchPluginCommand = vi.hoisted(() => vi.fn());
const executePluginCommand = vi.hoisted(() => vi.fn());
vi.mock("../plugins/commands.js", () => ({
getPluginCommandSpecs, getPluginCommandSpecs,
matchPluginCommand, matchPluginCommand,
executePluginCommand, } from "./bot-native-commands.test-helpers.js";
}));
const deliverReplies = vi.hoisted(() => vi.fn(async () => {}));
vi.mock("./bot/delivery.js", () => ({ deliverReplies }));
vi.mock("../pairing/pairing-store.js", () => ({
readChannelAllowFromStore: vi.fn(async () => []),
}));
describe("registerTelegramNativeCommands (plugin auth)", () => { describe("registerTelegramNativeCommands (plugin auth)", () => {
it("does not register plugin commands in menu when native=false but keeps handlers available", () => { it("does not register plugin commands in menu when native=false but keeps handlers available", () => {
@ -30,44 +17,10 @@ describe("registerTelegramNativeCommands (plugin auth)", () => {
})); }));
getPluginCommandSpecs.mockReturnValue(specs); getPluginCommandSpecs.mockReturnValue(specs);
const handlers: Record<string, (ctx: unknown) => Promise<void>> = {}; const { handlers, setMyCommands, log } = createNativeCommandsHarness({
const setMyCommands = vi.fn().mockResolvedValue(undefined);
const log = vi.fn();
const bot = {
api: {
setMyCommands,
sendMessage: vi.fn(),
},
command: (name: string, handler: (ctx: unknown) => Promise<void>) => {
handlers[name] = handler;
},
} as const;
registerTelegramNativeCommands({
bot: bot as unknown as Parameters<typeof registerTelegramNativeCommands>[0]["bot"],
cfg: {} as OpenClawConfig, cfg: {} as OpenClawConfig,
runtime: { log } as unknown as RuntimeEnv,
accountId: "default",
telegramCfg: {} as TelegramAccountConfig, telegramCfg: {} as TelegramAccountConfig,
allowFrom: [],
groupAllowFrom: [],
replyToMode: "off",
textLimit: 4000,
useAccessGroups: false,
nativeEnabled: false, nativeEnabled: false,
nativeSkillsEnabled: false,
nativeDisabledExplicit: false,
resolveGroupPolicy: () =>
({
allowlistEnabled: false,
allowed: true,
}) as ChannelGroupPolicy,
resolveTelegramGroupConfig: () => ({
groupConfig: undefined,
topicConfig: undefined,
}),
shouldSkipUpdate: () => false,
opts: { token: "token" },
}); });
expect(setMyCommands).not.toHaveBeenCalled(); expect(setMyCommands).not.toHaveBeenCalled();
@ -87,46 +40,11 @@ describe("registerTelegramNativeCommands (plugin auth)", () => {
matchPluginCommand.mockReturnValue({ command, args: undefined }); matchPluginCommand.mockReturnValue({ command, args: undefined });
executePluginCommand.mockResolvedValue({ text: "ok" }); executePluginCommand.mockResolvedValue({ text: "ok" });
const handlers: Record<string, (ctx: unknown) => Promise<void>> = {}; const { handlers, bot } = createNativeCommandsHarness({
const bot = { cfg: {} as OpenClawConfig,
api: { telegramCfg: {} as TelegramAccountConfig,
setMyCommands: vi.fn().mockResolvedValue(undefined),
sendMessage: vi.fn(),
},
command: (name: string, handler: (ctx: unknown) => Promise<void>) => {
handlers[name] = handler;
},
} as const;
const cfg = {} as OpenClawConfig;
const telegramCfg = {} as TelegramAccountConfig;
const resolveGroupPolicy = () =>
({
allowlistEnabled: false,
allowed: true,
}) as ChannelGroupPolicy;
registerTelegramNativeCommands({
bot: bot as unknown as Parameters<typeof registerTelegramNativeCommands>[0]["bot"],
cfg,
runtime: {} as unknown as RuntimeEnv,
accountId: "default",
telegramCfg,
allowFrom: ["999"], allowFrom: ["999"],
groupAllowFrom: [],
replyToMode: "off",
textLimit: 4000,
useAccessGroups: false,
nativeEnabled: false, nativeEnabled: false,
nativeSkillsEnabled: false,
nativeDisabledExplicit: false,
resolveGroupPolicy,
resolveTelegramGroupConfig: () => ({
groupConfig: undefined,
topicConfig: undefined,
}),
shouldSkipUpdate: () => false,
opts: { token: "token" },
}); });
const ctx = { const ctx = {

View File

@ -1,49 +1,113 @@
import { vi } from "vitest";
import type { OpenClawConfig } from "../config/config.js"; import type { OpenClawConfig } from "../config/config.js";
import type { ChannelGroupPolicy } from "../config/group-policy.js";
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 type { registerTelegramNativeCommands } from "./bot-native-commands.js"; import { registerTelegramNativeCommands } from "./bot-native-commands.js";
type RegisterTelegramNativeCommandParams = Parameters<typeof registerTelegramNativeCommands>[0]; const pluginCommandMocks = vi.hoisted(() => ({
getPluginCommandSpecs: vi.fn(() => []),
matchPluginCommand: vi.fn(() => null),
executePluginCommand: vi.fn(async () => ({ text: "ok" })),
}));
export const getPluginCommandSpecs = pluginCommandMocks.getPluginCommandSpecs;
export const matchPluginCommand = pluginCommandMocks.matchPluginCommand;
export const executePluginCommand = pluginCommandMocks.executePluginCommand;
export function createNativeCommandTestParams(params: { vi.mock("../plugins/commands.js", () => ({
bot: RegisterTelegramNativeCommandParams["bot"]; getPluginCommandSpecs: pluginCommandMocks.getPluginCommandSpecs,
matchPluginCommand: pluginCommandMocks.matchPluginCommand,
executePluginCommand: pluginCommandMocks.executePluginCommand,
}));
const deliveryMocks = vi.hoisted(() => ({
deliverReplies: vi.fn(async () => {}),
}));
export const deliverReplies = deliveryMocks.deliverReplies;
vi.mock("./bot/delivery.js", () => ({ deliverReplies: deliveryMocks.deliverReplies }));
vi.mock("../pairing/pairing-store.js", () => ({
readChannelAllowFromStore: vi.fn(async () => []),
}));
export function createNativeCommandsHarness(params?: {
cfg?: OpenClawConfig; cfg?: OpenClawConfig;
runtime?: RuntimeEnv; runtime?: RuntimeEnv;
accountId?: string;
telegramCfg?: TelegramAccountConfig; telegramCfg?: TelegramAccountConfig;
allowFrom?: string[]; allowFrom?: string[];
groupAllowFrom?: string[]; groupAllowFrom?: string[];
replyToMode?: RegisterTelegramNativeCommandParams["replyToMode"];
textLimit?: number;
useAccessGroups?: boolean; useAccessGroups?: boolean;
nativeEnabled?: boolean; nativeEnabled?: boolean;
nativeSkillsEnabled?: boolean; groupConfig?: Record<string, unknown>;
nativeDisabledExplicit?: boolean; resolveGroupPolicy?: () => ChannelGroupPolicy;
resolveTelegramGroupConfig?: RegisterTelegramNativeCommandParams["resolveTelegramGroupConfig"]; }) {
opts?: RegisterTelegramNativeCommandParams["opts"]; const handlers: Record<string, (ctx: unknown) => Promise<void>> = {};
}): RegisterTelegramNativeCommandParams { const sendMessage = vi.fn().mockResolvedValue(undefined);
return { const setMyCommands = vi.fn().mockResolvedValue(undefined);
bot: params.bot, const log = vi.fn();
cfg: params.cfg ?? {}, const bot = {
runtime: params.runtime ?? ({} as RuntimeEnv), api: {
accountId: params.accountId ?? "default", setMyCommands,
telegramCfg: params.telegramCfg ?? ({} as TelegramAccountConfig), sendMessage,
allowFrom: params.allowFrom ?? [], },
groupAllowFrom: params.groupAllowFrom ?? [], command: (name: string, handler: (ctx: unknown) => Promise<void>) => {
replyToMode: params.replyToMode ?? "off", handlers[name] = handler;
textLimit: params.textLimit ?? 4096, },
useAccessGroups: params.useAccessGroups ?? false, } as const;
nativeEnabled: params.nativeEnabled ?? true,
nativeSkillsEnabled: params.nativeSkillsEnabled ?? true, registerTelegramNativeCommands({
nativeDisabledExplicit: params.nativeDisabledExplicit ?? false, bot: bot as unknown as Parameters<typeof registerTelegramNativeCommands>[0]["bot"],
resolveGroupPolicy: () => ({ allowlistEnabled: false, allowed: true }), cfg: params?.cfg ?? ({} as OpenClawConfig),
resolveTelegramGroupConfig: runtime: params?.runtime ?? ({ log } as unknown as RuntimeEnv),
params.resolveTelegramGroupConfig ?? accountId: "default",
(() => ({ telegramCfg: params?.telegramCfg ?? ({} as TelegramAccountConfig),
groupConfig: undefined, allowFrom: params?.allowFrom ?? [],
groupAllowFrom: params?.groupAllowFrom ?? [],
replyToMode: "off",
textLimit: 4000,
useAccessGroups: params?.useAccessGroups ?? false,
nativeEnabled: params?.nativeEnabled ?? true,
nativeSkillsEnabled: false,
nativeDisabledExplicit: false,
resolveGroupPolicy:
params?.resolveGroupPolicy ??
(() =>
({
allowlistEnabled: false,
allowed: true,
}) as ChannelGroupPolicy),
resolveTelegramGroupConfig: () => ({
groupConfig: params?.groupConfig as undefined,
topicConfig: undefined, topicConfig: undefined,
})), }),
shouldSkipUpdate: () => false, shouldSkipUpdate: () => false,
opts: params.opts ?? { token: "token" }, opts: { token: "token" },
});
return { handlers, sendMessage, setMyCommands, log, bot };
}
export function createTelegramGroupCommandContext(params?: {
senderId?: number;
username?: string;
threadId?: number;
}) {
return {
message: {
chat: { id: -100999, type: "supergroup", is_forum: true },
from: {
id: params?.senderId ?? 12345,
username: params?.username ?? "testuser",
},
message_thread_id: params?.threadId ?? 42,
message_id: 1,
date: 1700000000,
},
match: "",
}; };
} }
export function findNotAuthorizedCalls(sendMessage: ReturnType<typeof vi.fn>) {
return sendMessage.mock.calls.filter(
(call) => typeof call[1] === "string" && call[1].includes("not authorized"),
);
}