mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 02:32:44 +00:00
refactor(telegram): extract sequential key module
This commit is contained in:
92
src/telegram/sequential-key.test.ts
Normal file
92
src/telegram/sequential-key.test.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
import type { Chat, Message } from "@grammyjs/types";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { getTelegramSequentialKey } from "./sequential-key.js";
|
||||
|
||||
const mockChat = (chat: Pick<Chat, "id"> & Partial<Pick<Chat, "type" | "is_forum">>): Chat =>
|
||||
chat as Chat;
|
||||
const mockMessage = (message: Pick<Message, "chat"> & Partial<Message>): Message =>
|
||||
({
|
||||
message_id: 1,
|
||||
date: 0,
|
||||
...message,
|
||||
}) as Message;
|
||||
|
||||
describe("getTelegramSequentialKey", () => {
|
||||
it.each([
|
||||
[{ message: mockMessage({ chat: mockChat({ id: 123 }) }) }, "telegram:123"],
|
||||
[
|
||||
{
|
||||
message: mockMessage({
|
||||
chat: mockChat({ id: 123, type: "private" }),
|
||||
message_thread_id: 9,
|
||||
}),
|
||||
},
|
||||
"telegram:123:topic:9",
|
||||
],
|
||||
[
|
||||
{
|
||||
message: mockMessage({
|
||||
chat: mockChat({ id: 123, type: "supergroup" }),
|
||||
message_thread_id: 9,
|
||||
}),
|
||||
},
|
||||
"telegram:123",
|
||||
],
|
||||
[
|
||||
{
|
||||
message: mockMessage({
|
||||
chat: mockChat({ id: 123, type: "supergroup", is_forum: true }),
|
||||
}),
|
||||
},
|
||||
"telegram:123:topic:1",
|
||||
],
|
||||
[{ update: { message: mockMessage({ chat: mockChat({ id: 555 }) }) } }, "telegram:555"],
|
||||
[
|
||||
{
|
||||
channelPost: mockMessage({ chat: mockChat({ id: -100777111222, type: "channel" }) }),
|
||||
},
|
||||
"telegram:-100777111222",
|
||||
],
|
||||
[
|
||||
{
|
||||
update: {
|
||||
channel_post: mockMessage({ chat: mockChat({ id: -100777111223, type: "channel" }) }),
|
||||
},
|
||||
},
|
||||
"telegram:-100777111223",
|
||||
],
|
||||
[
|
||||
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "/stop" }) },
|
||||
"telegram:123:control",
|
||||
],
|
||||
[{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "/status" }) }, "telegram:123"],
|
||||
[
|
||||
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "stop" }) },
|
||||
"telegram:123:control",
|
||||
],
|
||||
[
|
||||
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "stop please" }) },
|
||||
"telegram:123:control",
|
||||
],
|
||||
[
|
||||
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "do not do that" }) },
|
||||
"telegram:123:control",
|
||||
],
|
||||
[
|
||||
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "остановись" }) },
|
||||
"telegram:123:control",
|
||||
],
|
||||
[
|
||||
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "halt" }) },
|
||||
"telegram:123:control",
|
||||
],
|
||||
[{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "/abort" }) }, "telegram:123"],
|
||||
[{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "/abort now" }) }, "telegram:123"],
|
||||
[
|
||||
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "please do not do that" }) },
|
||||
"telegram:123",
|
||||
],
|
||||
])("resolves key %#", (input, expected) => {
|
||||
expect(getTelegramSequentialKey(input)).toBe(expected);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user