Files
openclaw/extensions/telegram/src/bot-native-commands.skills-allowlist.test.ts
scoootscooob e5bca0832f refactor: move Telegram channel implementation to extensions/ (#45635)
* refactor: move Telegram channel implementation to extensions/telegram/src/

Move all Telegram channel code (123 files + 10 bot/ files + 8 channel plugin
files) from src/telegram/ and src/channels/plugins/*/telegram.ts to
extensions/telegram/src/. Leave thin re-export shims at original locations so
cross-cutting src/ imports continue to resolve.

- Fix all relative import paths in moved files (../X/ -> ../../../src/X/)
- Fix vi.mock paths in 60 test files
- Fix inline typeof import() expressions
- Update tsconfig.plugin-sdk.dts.json rootDir to "." for cross-directory DTS
- Update write-plugin-sdk-entry-dts.ts for new rootDir structure
- Move channel plugin files with correct path remapping

* fix: support keyed telegram send deps

* fix: sync telegram extension copies with latest main

* fix: correct import paths and remove misplaced files in telegram extension

* fix: sync outbound-adapter with main (add sendTelegramPayloadMessages) and fix delivery.test import path
2026-03-14 02:50:17 -07:00

127 lines
4.2 KiB
TypeScript

import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import { writeSkill } from "../../../src/agents/skills.e2e-test-helpers.js";
import type { OpenClawConfig } from "../../../src/config/config.js";
import type { TelegramAccountConfig } from "../../../src/config/types.js";
import { registerTelegramNativeCommands } from "./bot-native-commands.js";
const pluginCommandMocks = vi.hoisted(() => ({
getPluginCommandSpecs: vi.fn(() => []),
matchPluginCommand: vi.fn(() => null),
executePluginCommand: vi.fn(async () => ({ text: "ok" })),
}));
const deliveryMocks = vi.hoisted(() => ({
deliverReplies: vi.fn(async () => ({ delivered: true })),
}));
vi.mock("../../../src/plugins/commands.js", () => ({
getPluginCommandSpecs: pluginCommandMocks.getPluginCommandSpecs,
matchPluginCommand: pluginCommandMocks.matchPluginCommand,
executePluginCommand: pluginCommandMocks.executePluginCommand,
}));
vi.mock("./bot/delivery.js", () => ({
deliverReplies: deliveryMocks.deliverReplies,
}));
const tempDirs: string[] = [];
async function makeWorkspace(prefix: string) {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), prefix));
tempDirs.push(dir);
return dir;
}
describe("registerTelegramNativeCommands skill allowlist integration", () => {
afterEach(async () => {
pluginCommandMocks.getPluginCommandSpecs.mockClear().mockReturnValue([]);
pluginCommandMocks.matchPluginCommand.mockClear().mockReturnValue(null);
pluginCommandMocks.executePluginCommand.mockClear().mockResolvedValue({ text: "ok" });
deliveryMocks.deliverReplies.mockClear().mockResolvedValue({ delivered: true });
await Promise.all(
tempDirs
.splice(0, tempDirs.length)
.map((dir) => fs.rm(dir, { recursive: true, force: true })),
);
});
it("registers only allowlisted skills for the bound agent menu", async () => {
const workspaceDir = await makeWorkspace("openclaw-telegram-skills-");
await writeSkill({
dir: path.join(workspaceDir, "skills", "alpha-skill"),
name: "alpha-skill",
description: "Alpha skill",
});
await writeSkill({
dir: path.join(workspaceDir, "skills", "beta-skill"),
name: "beta-skill",
description: "Beta skill",
});
const setMyCommands = vi.fn().mockResolvedValue(undefined);
const cfg: OpenClawConfig = {
agents: {
list: [
{ id: "alpha", workspace: workspaceDir, skills: ["alpha-skill"] },
{ id: "beta", workspace: workspaceDir, skills: ["beta-skill"] },
],
},
bindings: [
{
agentId: "alpha",
match: { channel: "telegram", accountId: "bot-a" },
},
],
};
registerTelegramNativeCommands({
bot: {
api: {
setMyCommands,
sendMessage: vi.fn().mockResolvedValue(undefined),
},
command: vi.fn(),
} as unknown as Parameters<typeof registerTelegramNativeCommands>[0]["bot"],
cfg,
runtime: { log: vi.fn() } as unknown as Parameters<
typeof registerTelegramNativeCommands
>[0]["runtime"],
accountId: "bot-a",
telegramCfg: {} as TelegramAccountConfig,
allowFrom: [],
groupAllowFrom: [],
replyToMode: "off",
textLimit: 4000,
useAccessGroups: false,
nativeEnabled: true,
nativeSkillsEnabled: true,
nativeDisabledExplicit: false,
resolveGroupPolicy: () =>
({
allowlistEnabled: false,
allowed: true,
}) as ReturnType<
Parameters<typeof registerTelegramNativeCommands>[0]["resolveGroupPolicy"]
>,
resolveTelegramGroupConfig: () => ({
groupConfig: undefined,
topicConfig: undefined,
}),
shouldSkipUpdate: () => false,
opts: { token: "token" },
});
await vi.waitFor(() => {
expect(setMyCommands).toHaveBeenCalled();
});
const registeredCommands = setMyCommands.mock.calls[0]?.[0] as Array<{
command: string;
description: string;
}>;
expect(registeredCommands.some((entry) => entry.command === "alpha_skill")).toBe(true);
expect(registeredCommands.some((entry) => entry.command === "beta_skill")).toBe(false);
});
});