mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 19:54:57 +00:00
test(telegram): table-drive channel override and id helper cases
This commit is contained in:
@@ -3,14 +3,24 @@ import { isNumericTelegramUserId, normalizeTelegramAllowFromEntry } from "./allo
|
||||
|
||||
describe("telegram allow-from helpers", () => {
|
||||
it("normalizes tg/telegram prefixes", () => {
|
||||
expect(normalizeTelegramAllowFromEntry(" TG:123 ")).toBe("123");
|
||||
expect(normalizeTelegramAllowFromEntry("telegram:@someone")).toBe("@someone");
|
||||
const cases = [
|
||||
{ value: " TG:123 ", expected: "123" },
|
||||
{ value: "telegram:@someone", expected: "@someone" },
|
||||
] as const;
|
||||
for (const testCase of cases) {
|
||||
expect(normalizeTelegramAllowFromEntry(testCase.value)).toBe(testCase.expected);
|
||||
}
|
||||
});
|
||||
|
||||
it("accepts signed numeric IDs", () => {
|
||||
expect(isNumericTelegramUserId("123456789")).toBe(true);
|
||||
expect(isNumericTelegramUserId("-1001234567890")).toBe(true);
|
||||
expect(isNumericTelegramUserId("@someone")).toBe(false);
|
||||
expect(isNumericTelegramUserId("12 34")).toBe(false);
|
||||
const cases = [
|
||||
{ value: "123456789", expected: true },
|
||||
{ value: "-1001234567890", expected: true },
|
||||
{ value: "@someone", expected: false },
|
||||
{ value: "12 34", expected: false },
|
||||
] as const;
|
||||
for (const testCase of cases) {
|
||||
expect(isNumericTelegramUserId(testCase.value)).toBe(testCase.expected);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,55 +2,56 @@ import { describe, expect, it, vi } from "vitest";
|
||||
import { fetchTelegramChatId } from "./api.js";
|
||||
|
||||
describe("fetchTelegramChatId", () => {
|
||||
it("returns stringified id when Telegram getChat succeeds", async () => {
|
||||
const cases = [
|
||||
{
|
||||
name: "returns stringified id when Telegram getChat succeeds",
|
||||
fetchImpl: vi.fn(async () => ({
|
||||
ok: true,
|
||||
json: async () => ({ ok: true, result: { id: 12345 } }),
|
||||
})),
|
||||
expected: "12345",
|
||||
},
|
||||
{
|
||||
name: "returns null when response is not ok",
|
||||
fetchImpl: vi.fn(async () => ({
|
||||
ok: false,
|
||||
json: async () => ({}),
|
||||
})),
|
||||
expected: null,
|
||||
},
|
||||
{
|
||||
name: "returns null on transport failures",
|
||||
fetchImpl: vi.fn(async () => {
|
||||
throw new Error("network failed");
|
||||
}),
|
||||
expected: null,
|
||||
},
|
||||
] as const;
|
||||
|
||||
for (const testCase of cases) {
|
||||
it(testCase.name, async () => {
|
||||
vi.stubGlobal("fetch", testCase.fetchImpl);
|
||||
|
||||
const id = await fetchTelegramChatId({
|
||||
token: "abc",
|
||||
chatId: "@user",
|
||||
});
|
||||
|
||||
expect(id).toBe(testCase.expected);
|
||||
});
|
||||
}
|
||||
|
||||
it("calls Telegram getChat endpoint", async () => {
|
||||
const fetchMock = vi.fn(async () => ({
|
||||
ok: true,
|
||||
json: async () => ({ ok: true, result: { id: 12345 } }),
|
||||
}));
|
||||
vi.stubGlobal("fetch", fetchMock);
|
||||
|
||||
const id = await fetchTelegramChatId({
|
||||
token: "abc",
|
||||
chatId: "@user",
|
||||
});
|
||||
|
||||
expect(id).toBe("12345");
|
||||
await fetchTelegramChatId({ token: "abc", chatId: "@user" });
|
||||
expect(fetchMock).toHaveBeenCalledWith(
|
||||
"https://api.telegram.org/botabc/getChat?chat_id=%40user",
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
|
||||
it("returns null when response is not ok", async () => {
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn(async () => ({
|
||||
ok: false,
|
||||
json: async () => ({}),
|
||||
})),
|
||||
);
|
||||
|
||||
const id = await fetchTelegramChatId({
|
||||
token: "abc",
|
||||
chatId: "@user",
|
||||
});
|
||||
|
||||
expect(id).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null on transport failures", async () => {
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn(async () => {
|
||||
throw new Error("network failed");
|
||||
}),
|
||||
);
|
||||
|
||||
const id = await fetchTelegramChatId({
|
||||
token: "abc",
|
||||
chatId: "@user",
|
||||
});
|
||||
|
||||
expect(id).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user