mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 04:52:43 +00:00
refactor(telegram): dedupe native command session-meta fixtures
This commit is contained in:
@@ -131,37 +131,22 @@ function registerAndResolveStatusHandler(params: {
|
|||||||
sendMessage: ReturnType<typeof vi.fn>;
|
sendMessage: ReturnType<typeof vi.fn>;
|
||||||
} {
|
} {
|
||||||
const { cfg, allowFrom, groupAllowFrom, resolveTelegramGroupConfig } = params;
|
const { cfg, allowFrom, groupAllowFrom, resolveTelegramGroupConfig } = params;
|
||||||
const commandHandlers = new Map<string, TelegramCommandHandler>();
|
return registerAndResolveCommandHandlerBase({
|
||||||
const sendMessage = vi.fn().mockResolvedValue(undefined);
|
commandName: "status",
|
||||||
registerTelegramNativeCommands({
|
cfg,
|
||||||
...createNativeCommandTestParams({
|
allowFrom: allowFrom ?? ["*"],
|
||||||
bot: {
|
groupAllowFrom: groupAllowFrom ?? [],
|
||||||
api: {
|
useAccessGroups: true,
|
||||||
setMyCommands: vi.fn().mockResolvedValue(undefined),
|
resolveTelegramGroupConfig,
|
||||||
sendMessage,
|
|
||||||
},
|
|
||||||
command: vi.fn((name: string, cb: TelegramCommandHandler) => {
|
|
||||||
commandHandlers.set(name, cb);
|
|
||||||
}),
|
|
||||||
} as unknown as Parameters<typeof registerTelegramNativeCommands>[0]["bot"],
|
|
||||||
cfg,
|
|
||||||
allowFrom: allowFrom ?? ["*"],
|
|
||||||
groupAllowFrom: groupAllowFrom ?? [],
|
|
||||||
resolveTelegramGroupConfig,
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const handler = commandHandlers.get("status");
|
|
||||||
expect(handler).toBeTruthy();
|
|
||||||
return { handler: handler as TelegramCommandHandler, sendMessage };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function registerAndResolveCommandHandler(params: {
|
function registerAndResolveCommandHandlerBase(params: {
|
||||||
commandName: string;
|
commandName: string;
|
||||||
cfg: OpenClawConfig;
|
cfg: OpenClawConfig;
|
||||||
allowFrom?: string[];
|
allowFrom: string[];
|
||||||
groupAllowFrom?: string[];
|
groupAllowFrom: string[];
|
||||||
useAccessGroups?: boolean;
|
useAccessGroups: boolean;
|
||||||
resolveTelegramGroupConfig?: RegisterTelegramHandlerParams["resolveTelegramGroupConfig"];
|
resolveTelegramGroupConfig?: RegisterTelegramHandlerParams["resolveTelegramGroupConfig"];
|
||||||
}): {
|
}): {
|
||||||
handler: TelegramCommandHandler;
|
handler: TelegramCommandHandler;
|
||||||
@@ -189,9 +174,9 @@ function registerAndResolveCommandHandler(params: {
|
|||||||
}),
|
}),
|
||||||
} as unknown as Parameters<typeof registerTelegramNativeCommands>[0]["bot"],
|
} as unknown as Parameters<typeof registerTelegramNativeCommands>[0]["bot"],
|
||||||
cfg,
|
cfg,
|
||||||
allowFrom: allowFrom ?? [],
|
allowFrom,
|
||||||
groupAllowFrom: groupAllowFrom ?? [],
|
groupAllowFrom,
|
||||||
useAccessGroups: useAccessGroups ?? true,
|
useAccessGroups,
|
||||||
resolveTelegramGroupConfig,
|
resolveTelegramGroupConfig,
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
@@ -201,6 +186,72 @@ function registerAndResolveCommandHandler(params: {
|
|||||||
return { handler: handler as TelegramCommandHandler, sendMessage };
|
return { handler: handler as TelegramCommandHandler, sendMessage };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function registerAndResolveCommandHandler(params: {
|
||||||
|
commandName: string;
|
||||||
|
cfg: OpenClawConfig;
|
||||||
|
allowFrom?: string[];
|
||||||
|
groupAllowFrom?: string[];
|
||||||
|
useAccessGroups?: boolean;
|
||||||
|
resolveTelegramGroupConfig?: RegisterTelegramHandlerParams["resolveTelegramGroupConfig"];
|
||||||
|
}): {
|
||||||
|
handler: TelegramCommandHandler;
|
||||||
|
sendMessage: ReturnType<typeof vi.fn>;
|
||||||
|
} {
|
||||||
|
const {
|
||||||
|
commandName,
|
||||||
|
cfg,
|
||||||
|
allowFrom,
|
||||||
|
groupAllowFrom,
|
||||||
|
useAccessGroups,
|
||||||
|
resolveTelegramGroupConfig,
|
||||||
|
} = params;
|
||||||
|
return registerAndResolveCommandHandlerBase({
|
||||||
|
commandName,
|
||||||
|
cfg,
|
||||||
|
allowFrom: allowFrom ?? [],
|
||||||
|
groupAllowFrom: groupAllowFrom ?? [],
|
||||||
|
useAccessGroups: useAccessGroups ?? true,
|
||||||
|
resolveTelegramGroupConfig,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function createConfiguredAcpTopicBinding(boundSessionKey: string) {
|
||||||
|
return {
|
||||||
|
spec: {
|
||||||
|
channel: "telegram",
|
||||||
|
accountId: "default",
|
||||||
|
conversationId: "-1001234567890:topic:42",
|
||||||
|
parentConversationId: "-1001234567890",
|
||||||
|
agentId: "codex",
|
||||||
|
mode: "persistent",
|
||||||
|
},
|
||||||
|
record: {
|
||||||
|
bindingId: "config:acp:telegram:default:-1001234567890:topic:42",
|
||||||
|
targetSessionKey: boundSessionKey,
|
||||||
|
targetKind: "session",
|
||||||
|
conversation: {
|
||||||
|
channel: "telegram",
|
||||||
|
accountId: "default",
|
||||||
|
conversationId: "-1001234567890:topic:42",
|
||||||
|
parentConversationId: "-1001234567890",
|
||||||
|
},
|
||||||
|
status: "active",
|
||||||
|
boundAt: 0,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function expectUnauthorizedNewCommandBlocked(sendMessage: ReturnType<typeof vi.fn>) {
|
||||||
|
expect(replyMocks.dispatchReplyWithBufferedBlockDispatcher).not.toHaveBeenCalled();
|
||||||
|
expect(persistentBindingMocks.resolveConfiguredAcpBindingRecord).not.toHaveBeenCalled();
|
||||||
|
expect(persistentBindingMocks.ensureConfiguredAcpBindingSession).not.toHaveBeenCalled();
|
||||||
|
expect(sendMessage).toHaveBeenCalledWith(
|
||||||
|
-1001234567890,
|
||||||
|
"You are not authorized to use this command.",
|
||||||
|
expect.objectContaining({ message_thread_id: 42 }),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
describe("registerTelegramNativeCommands — session metadata", () => {
|
describe("registerTelegramNativeCommands — session metadata", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
persistentBindingMocks.resolveConfiguredAcpBindingRecord.mockClear();
|
persistentBindingMocks.resolveConfiguredAcpBindingRecord.mockClear();
|
||||||
@@ -254,29 +305,9 @@ describe("registerTelegramNativeCommands — session metadata", () => {
|
|||||||
|
|
||||||
it("routes Telegram native commands through configured ACP topic bindings", async () => {
|
it("routes Telegram native commands through configured ACP topic bindings", async () => {
|
||||||
const boundSessionKey = "agent:codex:acp:binding:telegram:default:feedface";
|
const boundSessionKey = "agent:codex:acp:binding:telegram:default:feedface";
|
||||||
persistentBindingMocks.resolveConfiguredAcpBindingRecord.mockReturnValue({
|
persistentBindingMocks.resolveConfiguredAcpBindingRecord.mockReturnValue(
|
||||||
spec: {
|
createConfiguredAcpTopicBinding(boundSessionKey),
|
||||||
channel: "telegram",
|
);
|
||||||
accountId: "default",
|
|
||||||
conversationId: "-1001234567890:topic:42",
|
|
||||||
parentConversationId: "-1001234567890",
|
|
||||||
agentId: "codex",
|
|
||||||
mode: "persistent",
|
|
||||||
},
|
|
||||||
record: {
|
|
||||||
bindingId: "config:acp:telegram:default:-1001234567890:topic:42",
|
|
||||||
targetSessionKey: boundSessionKey,
|
|
||||||
targetKind: "session",
|
|
||||||
conversation: {
|
|
||||||
channel: "telegram",
|
|
||||||
accountId: "default",
|
|
||||||
conversationId: "-1001234567890:topic:42",
|
|
||||||
parentConversationId: "-1001234567890",
|
|
||||||
},
|
|
||||||
status: "active",
|
|
||||||
boundAt: 0,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
persistentBindingMocks.ensureConfiguredAcpBindingSession.mockResolvedValue({
|
persistentBindingMocks.ensureConfiguredAcpBindingSession.mockResolvedValue({
|
||||||
ok: true,
|
ok: true,
|
||||||
sessionKey: boundSessionKey,
|
sessionKey: boundSessionKey,
|
||||||
@@ -359,29 +390,9 @@ describe("registerTelegramNativeCommands — session metadata", () => {
|
|||||||
|
|
||||||
it("aborts native command dispatch when configured ACP topic binding cannot initialize", async () => {
|
it("aborts native command dispatch when configured ACP topic binding cannot initialize", async () => {
|
||||||
const boundSessionKey = "agent:codex:acp:binding:telegram:default:feedface";
|
const boundSessionKey = "agent:codex:acp:binding:telegram:default:feedface";
|
||||||
persistentBindingMocks.resolveConfiguredAcpBindingRecord.mockReturnValue({
|
persistentBindingMocks.resolveConfiguredAcpBindingRecord.mockReturnValue(
|
||||||
spec: {
|
createConfiguredAcpTopicBinding(boundSessionKey),
|
||||||
channel: "telegram",
|
);
|
||||||
accountId: "default",
|
|
||||||
conversationId: "-1001234567890:topic:42",
|
|
||||||
parentConversationId: "-1001234567890",
|
|
||||||
agentId: "codex",
|
|
||||||
mode: "persistent",
|
|
||||||
},
|
|
||||||
record: {
|
|
||||||
bindingId: "config:acp:telegram:default:-1001234567890:topic:42",
|
|
||||||
targetSessionKey: boundSessionKey,
|
|
||||||
targetKind: "session",
|
|
||||||
conversation: {
|
|
||||||
channel: "telegram",
|
|
||||||
accountId: "default",
|
|
||||||
conversationId: "-1001234567890:topic:42",
|
|
||||||
parentConversationId: "-1001234567890",
|
|
||||||
},
|
|
||||||
status: "active",
|
|
||||||
boundAt: 0,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
persistentBindingMocks.ensureConfiguredAcpBindingSession.mockResolvedValue({
|
persistentBindingMocks.ensureConfiguredAcpBindingSession.mockResolvedValue({
|
||||||
ok: false,
|
ok: false,
|
||||||
sessionKey: boundSessionKey,
|
sessionKey: boundSessionKey,
|
||||||
@@ -405,29 +416,9 @@ describe("registerTelegramNativeCommands — session metadata", () => {
|
|||||||
|
|
||||||
it("keeps /new blocked in ACP-bound Telegram topics when sender is unauthorized", async () => {
|
it("keeps /new blocked in ACP-bound Telegram topics when sender is unauthorized", async () => {
|
||||||
const boundSessionKey = "agent:codex:acp:binding:telegram:default:feedface";
|
const boundSessionKey = "agent:codex:acp:binding:telegram:default:feedface";
|
||||||
persistentBindingMocks.resolveConfiguredAcpBindingRecord.mockReturnValue({
|
persistentBindingMocks.resolveConfiguredAcpBindingRecord.mockReturnValue(
|
||||||
spec: {
|
createConfiguredAcpTopicBinding(boundSessionKey),
|
||||||
channel: "telegram",
|
);
|
||||||
accountId: "default",
|
|
||||||
conversationId: "-1001234567890:topic:42",
|
|
||||||
parentConversationId: "-1001234567890",
|
|
||||||
agentId: "codex",
|
|
||||||
mode: "persistent",
|
|
||||||
},
|
|
||||||
record: {
|
|
||||||
bindingId: "config:acp:telegram:default:-1001234567890:topic:42",
|
|
||||||
targetSessionKey: boundSessionKey,
|
|
||||||
targetKind: "session",
|
|
||||||
conversation: {
|
|
||||||
channel: "telegram",
|
|
||||||
accountId: "default",
|
|
||||||
conversationId: "-1001234567890:topic:42",
|
|
||||||
parentConversationId: "-1001234567890",
|
|
||||||
},
|
|
||||||
status: "active",
|
|
||||||
boundAt: 0,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
persistentBindingMocks.ensureConfiguredAcpBindingSession.mockResolvedValue({
|
persistentBindingMocks.ensureConfiguredAcpBindingSession.mockResolvedValue({
|
||||||
ok: true,
|
ok: true,
|
||||||
sessionKey: boundSessionKey,
|
sessionKey: boundSessionKey,
|
||||||
@@ -442,14 +433,7 @@ describe("registerTelegramNativeCommands — session metadata", () => {
|
|||||||
});
|
});
|
||||||
await handler(buildStatusTopicCommandContext());
|
await handler(buildStatusTopicCommandContext());
|
||||||
|
|
||||||
expect(replyMocks.dispatchReplyWithBufferedBlockDispatcher).not.toHaveBeenCalled();
|
expectUnauthorizedNewCommandBlocked(sendMessage);
|
||||||
expect(persistentBindingMocks.resolveConfiguredAcpBindingRecord).not.toHaveBeenCalled();
|
|
||||||
expect(persistentBindingMocks.ensureConfiguredAcpBindingSession).not.toHaveBeenCalled();
|
|
||||||
expect(sendMessage).toHaveBeenCalledWith(
|
|
||||||
-1001234567890,
|
|
||||||
"You are not authorized to use this command.",
|
|
||||||
expect.objectContaining({ message_thread_id: 42 }),
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("keeps /new blocked for unbound Telegram topics when sender is unauthorized", async () => {
|
it("keeps /new blocked for unbound Telegram topics when sender is unauthorized", async () => {
|
||||||
@@ -464,13 +448,6 @@ describe("registerTelegramNativeCommands — session metadata", () => {
|
|||||||
});
|
});
|
||||||
await handler(buildStatusTopicCommandContext());
|
await handler(buildStatusTopicCommandContext());
|
||||||
|
|
||||||
expect(replyMocks.dispatchReplyWithBufferedBlockDispatcher).not.toHaveBeenCalled();
|
expectUnauthorizedNewCommandBlocked(sendMessage);
|
||||||
expect(persistentBindingMocks.resolveConfiguredAcpBindingRecord).not.toHaveBeenCalled();
|
|
||||||
expect(persistentBindingMocks.ensureConfiguredAcpBindingSession).not.toHaveBeenCalled();
|
|
||||||
expect(sendMessage).toHaveBeenCalledWith(
|
|
||||||
-1001234567890,
|
|
||||||
"You are not authorized to use this command.",
|
|
||||||
expect.objectContaining({ message_thread_id: 42 }),
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user