File size: 3,271 Bytes
fc93158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../config/config.js";
import type { TelegramAccountConfig } from "../config/types.js";
import {
  createNativeCommandsHarness,
  deliverReplies,
  executePluginCommand,
  getPluginCommandSpecs,
  matchPluginCommand,
} from "./bot-native-commands.test-helpers.js";

type GetPluginCommandSpecsMock = {
  mockReturnValue: (
    value: ReturnType<typeof import("../plugins/commands.js").getPluginCommandSpecs>,
  ) => unknown;
};
type MatchPluginCommandMock = {
  mockReturnValue: (
    value: ReturnType<typeof import("../plugins/commands.js").matchPluginCommand>,
  ) => unknown;
};
type ExecutePluginCommandMock = {
  mockResolvedValue: (
    value: Awaited<ReturnType<typeof import("../plugins/commands.js").executePluginCommand>>,
  ) => unknown;
};

const getPluginCommandSpecsMock = getPluginCommandSpecs as unknown as GetPluginCommandSpecsMock;
const matchPluginCommandMock = matchPluginCommand as unknown as MatchPluginCommandMock;
const executePluginCommandMock = executePluginCommand as unknown as ExecutePluginCommandMock;

describe("registerTelegramNativeCommands (plugin auth)", () => {
  it("does not register plugin commands in menu when native=false but keeps handlers available", () => {
    const specs = Array.from({ length: 101 }, (_, i) => ({
      name: `cmd_${i}`,
      description: `Command ${i}`,
      acceptsArgs: false,
    }));
    getPluginCommandSpecsMock.mockReturnValue(specs);

    const { handlers, setMyCommands, log } = createNativeCommandsHarness({
      cfg: {} as OpenClawConfig,
      telegramCfg: {} as TelegramAccountConfig,
      nativeEnabled: false,
    });

    expect(setMyCommands).not.toHaveBeenCalled();
    expect(log).not.toHaveBeenCalledWith(expect.stringContaining("registering first 100"));
    expect(Object.keys(handlers)).toHaveLength(101);
  });

  it("allows requireAuth:false plugin command even when sender is unauthorized", async () => {
    const command = {
      name: "plugin",
      description: "Plugin command",
      pluginId: "test-plugin",
      requireAuth: false,
      handler: vi.fn(),
    } as const;

    getPluginCommandSpecsMock.mockReturnValue([
      { name: "plugin", description: "Plugin command", acceptsArgs: false },
    ]);
    matchPluginCommandMock.mockReturnValue({ command, args: undefined });
    executePluginCommandMock.mockResolvedValue({ text: "ok" });

    const { handlers, bot } = createNativeCommandsHarness({
      cfg: {} as OpenClawConfig,
      telegramCfg: {} as TelegramAccountConfig,
      allowFrom: ["999"],
      nativeEnabled: false,
    });

    const ctx = {
      message: {
        chat: { id: 123, type: "private" },
        from: { id: 111, username: "nope" },
        message_id: 10,
        date: 123456,
      },
      match: "",
    };

    await handlers.plugin?.(ctx);

    expect(matchPluginCommand).toHaveBeenCalled();
    expect(executePluginCommand).toHaveBeenCalledWith(
      expect.objectContaining({
        isAuthorizedSender: false,
      }),
    );
    expect(deliverReplies).toHaveBeenCalledWith(
      expect.objectContaining({
        replies: [{ text: "ok" }],
      }),
    );
    expect(bot.api.sendMessage).not.toHaveBeenCalled();
  });
});