
























@@ -121,6 +121,32 @@ function requireCommandHandler(
121121return handler;
122122}
123123124+function expectRegisteredCommand(
125+commands: Array<{ command: string; description: string }>,
126+expected: { command: string; description: string },
127+): void {
128+expect(
129+commands.some(
130+(command) =>
131+command.command === expected.command && command.description === expected.description,
132+),
133+).toBe(true);
134+}
135+136+function expectLastDeliveredReplyText(text: string): void {
137+const calls = deliverReplies.mock.calls as unknown[][];
138+const payload = calls.at(-1)?.[0] as { replies?: Array<{ text?: string }> } | undefined;
139+expect(payload?.replies?.map((reply) => reply.text)).toEqual([text]);
140+}
141+142+function mockCall(mock: { mock: { calls: unknown[][] } }, index: number): unknown[] {
143+const call = mock.mock.calls[index];
144+if (!call) {
145+throw new Error(`expected mock call ${index}`);
146+}
147+return call;
148+}
149+124150describe("registerTelegramNativeCommands real plugin registry", () => {
125151beforeAll(async () => {
126152({ setActivePluginRegistry } = await import("openclaw/plugin-sdk/plugin-test-runtime"));
@@ -150,19 +176,13 @@ describe("registerTelegramNativeCommands real plugin registry", () => {
150176const { bot, commandHandlers, sendMessage, setMyCommands } = createCommandBot();
151177152178const registeredCommands = await registerPairMenu({ bot, setMyCommands });
153-expect(registeredCommands).toEqual(
154-expect.arrayContaining([{ command: "pair", description: "Pair device" }]),
155-);
179+expectRegisteredCommand(registeredCommands, { command: "pair", description: "Pair device" });
156180157181const handler = requireCommandHandler(commandHandlers, "pair");
158182159183await handler(createPrivateCommandContext({ match: "now" }));
160184161-expect(deliverReplies).toHaveBeenCalledWith(
162-expect.objectContaining({
163-replies: [expect.objectContaining({ text: "paired:now" })],
164-}),
165-);
185+expectLastDeliveredReplyText("paired:now");
166186expect(sendMessage).not.toHaveBeenCalledWith(123, "Command not found.");
167187});
168188@@ -182,17 +202,15 @@ describe("registerTelegramNativeCommands real plugin registry", () => {
182202183203await handler(createPrivateCommandContext({ match: "now" }));
184204185-expect(sendMessage).toHaveBeenCalledWith(
186-100,
187-expect.stringContaining("Running pair now"),
188-undefined,
189-);
190-expect(editMessageTelegram).toHaveBeenCalledWith(
191-100,
192-999,
193-"paired:now",
194-expect.objectContaining({ accountId: "default" }),
195-);
205+const sendCall = mockCall(sendMessage, 0);
206+expect(sendCall[0]).toBe(100);
207+expect(sendCall[1]).toContain("Running pair now");
208+expect(sendCall[2]).toBeUndefined();
209+const editCall = mockCall(editMessageTelegram, 0);
210+expect(editCall[0]).toBe(100);
211+expect(editCall[1]).toBe(999);
212+expect(editCall[2]).toBe("paired:now");
213+expect((editCall[3] as { accountId?: string }).accountId).toBe("default");
196214expect(deliverReplies).not.toHaveBeenCalled();
197215});
198216@@ -207,19 +225,16 @@ describe("registerTelegramNativeCommands real plugin registry", () => {
207225discord: "pairdiscord",
208226},
209227});
210-expect(registeredCommands).toEqual(
211-expect.arrayContaining([{ command: "pair_device", description: "Pair device" }]),
212-);
228+expectRegisteredCommand(registeredCommands, {
229+command: "pair_device",
230+description: "Pair device",
231+});
213232214233const handler = requireCommandHandler(commandHandlers, "pair_device");
215234216235await handler(createPrivateCommandContext({ match: "now", messageId: 2 }));
217236218-expect(deliverReplies).toHaveBeenCalledWith(
219-expect.objectContaining({
220-replies: [expect.objectContaining({ text: "paired:now" })],
221-}),
222-);
237+expectLastDeliveredReplyText("paired:now");
223238expect(sendMessage).not.toHaveBeenCalledWith(123, "Command not found.");
224239});
225240@@ -266,11 +281,7 @@ describe("registerTelegramNativeCommands real plugin registry", () => {
266281}),
267282);
268283269-expect(deliverReplies).toHaveBeenCalledWith(
270-expect.objectContaining({
271-replies: [expect.objectContaining({ text: "paired:now" })],
272-}),
273-);
284+expectLastDeliveredReplyText("paired:now");
274285expect(sendMessage).not.toHaveBeenCalled();
275286});
276287});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。