mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 15:34:31 +00:00
test(telegram): table-drive pairing DM scenarios
This commit is contained in:
@@ -262,80 +262,67 @@ describe("createTelegramBot", () => {
|
|||||||
expect(payload.Body).toContain("hello world");
|
expect(payload.Body).toContain("hello world");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("requests pairing by default for unknown DM senders", async () => {
|
it("handles pairing DM flows for new and already-pending requests", async () => {
|
||||||
onSpy.mockReset();
|
const cases = [
|
||||||
sendMessageSpy.mockReset();
|
{
|
||||||
replySpy.mockReset();
|
name: "new unknown sender",
|
||||||
|
upsertResults: [{ code: "PAIRME12", created: true }],
|
||||||
loadConfig.mockReturnValue({
|
messages: ["hello"],
|
||||||
channels: { telegram: { dmPolicy: "pairing" } },
|
expectedSendCount: 1,
|
||||||
});
|
expectPairingText: true,
|
||||||
readChannelAllowFromStore.mockResolvedValue([]);
|
|
||||||
upsertChannelPairingRequest.mockResolvedValue({
|
|
||||||
code: "PAIRME12",
|
|
||||||
created: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
createTelegramBot({ token: "tok" });
|
|
||||||
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
|
||||||
|
|
||||||
await handler({
|
|
||||||
message: {
|
|
||||||
chat: { id: 1234, type: "private" },
|
|
||||||
text: "hello",
|
|
||||||
date: 1736380800,
|
|
||||||
from: { id: 999, username: "random" },
|
|
||||||
},
|
},
|
||||||
me: { username: "openclaw_bot" },
|
{
|
||||||
getFile: async () => ({ download: async () => new Uint8Array() }),
|
name: "already pending request",
|
||||||
});
|
upsertResults: [
|
||||||
|
{ code: "PAIRME12", created: true },
|
||||||
|
{ code: "PAIRME12", created: false },
|
||||||
|
],
|
||||||
|
messages: ["hello", "hello again"],
|
||||||
|
expectedSendCount: 1,
|
||||||
|
expectPairingText: false,
|
||||||
|
},
|
||||||
|
] as const;
|
||||||
|
|
||||||
expect(replySpy).not.toHaveBeenCalled();
|
for (const testCase of cases) {
|
||||||
expect(sendMessageSpy).toHaveBeenCalledTimes(1);
|
onSpy.mockReset();
|
||||||
expect(sendMessageSpy.mock.calls[0]?.[0]).toBe(1234);
|
sendMessageSpy.mockReset();
|
||||||
const pairingText = String(sendMessageSpy.mock.calls[0]?.[1]);
|
replySpy.mockReset();
|
||||||
expect(pairingText).toContain("Your Telegram user id: 999");
|
loadConfig.mockReturnValue({
|
||||||
expect(pairingText).toContain("Pairing code:");
|
channels: { telegram: { dmPolicy: "pairing" } },
|
||||||
expect(pairingText).toContain("PAIRME12");
|
});
|
||||||
expect(pairingText).toContain("openclaw pairing approve telegram PAIRME12");
|
readChannelAllowFromStore.mockResolvedValue([]);
|
||||||
expect(pairingText).not.toContain("<code>");
|
upsertChannelPairingRequest.mockReset();
|
||||||
});
|
for (const result of testCase.upsertResults) {
|
||||||
it("does not resend pairing code when a request is already pending", async () => {
|
upsertChannelPairingRequest.mockResolvedValueOnce(result);
|
||||||
onSpy.mockReset();
|
}
|
||||||
sendMessageSpy.mockReset();
|
|
||||||
replySpy.mockReset();
|
|
||||||
|
|
||||||
loadConfig.mockReturnValue({
|
createTelegramBot({ token: "tok" });
|
||||||
channels: { telegram: { dmPolicy: "pairing" } },
|
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
||||||
});
|
for (const text of testCase.messages) {
|
||||||
readChannelAllowFromStore.mockResolvedValue([]);
|
await handler({
|
||||||
upsertChannelPairingRequest
|
message: {
|
||||||
.mockResolvedValueOnce({ code: "PAIRME12", created: true })
|
chat: { id: 1234, type: "private" },
|
||||||
.mockResolvedValueOnce({ code: "PAIRME12", created: false });
|
text,
|
||||||
|
date: 1736380800,
|
||||||
|
from: { id: 999, username: "random" },
|
||||||
|
},
|
||||||
|
me: { username: "openclaw_bot" },
|
||||||
|
getFile: async () => ({ download: async () => new Uint8Array() }),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
createTelegramBot({ token: "tok" });
|
expect(replySpy, testCase.name).not.toHaveBeenCalled();
|
||||||
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
expect(sendMessageSpy, testCase.name).toHaveBeenCalledTimes(testCase.expectedSendCount);
|
||||||
|
if (testCase.expectPairingText) {
|
||||||
const message = {
|
expect(sendMessageSpy.mock.calls[0]?.[0], testCase.name).toBe(1234);
|
||||||
chat: { id: 1234, type: "private" },
|
const pairingText = String(sendMessageSpy.mock.calls[0]?.[1]);
|
||||||
text: "hello",
|
expect(pairingText, testCase.name).toContain("Your Telegram user id: 999");
|
||||||
date: 1736380800,
|
expect(pairingText, testCase.name).toContain("Pairing code:");
|
||||||
from: { id: 999, username: "random" },
|
expect(pairingText, testCase.name).toContain("PAIRME12");
|
||||||
};
|
expect(pairingText, testCase.name).toContain("openclaw pairing approve telegram PAIRME12");
|
||||||
|
expect(pairingText, testCase.name).not.toContain("<code>");
|
||||||
await handler({
|
}
|
||||||
message,
|
}
|
||||||
me: { username: "openclaw_bot" },
|
|
||||||
getFile: async () => ({ download: async () => new Uint8Array() }),
|
|
||||||
});
|
|
||||||
await handler({
|
|
||||||
message: { ...message, text: "hello again" },
|
|
||||||
me: { username: "openclaw_bot" },
|
|
||||||
getFile: async () => ({ download: async () => new Uint8Array() }),
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(replySpy).not.toHaveBeenCalled();
|
|
||||||
expect(sendMessageSpy).toHaveBeenCalledTimes(1);
|
|
||||||
});
|
});
|
||||||
it("triggers typing cue via onReplyStart", async () => {
|
it("triggers typing cue via onReplyStart", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
|
|||||||
Reference in New Issue
Block a user