openclaw/extensions/telegram/src/api-fetch.test.ts
Cypherm 6b4c24c2e5
feat(telegram): support custom apiRoot for alternative API endpoints (#48842)
* feat(telegram): support custom apiRoot for alternative API endpoints

Add `apiRoot` config option to allow users to specify custom Telegram Bot
API endpoints (e.g., self-hosted Bot API servers). Threads the configured
base URL through all Telegram API call sites: bot creation, send, probe,
audit, media download, and api-fetch. Extends SSRF policy to dynamically
trust custom apiRoot hostname for media downloads.

Closes #28535

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix(telegram): thread apiRoot through allowFrom lookups

* fix(telegram): honor lookup transport and local file paths

* refactor(telegram): unify username lookup plumbing

* fix(telegram): restore doctor lookup imports

* fix: document Telegram apiRoot support (#48842) (thanks @Cypherm)

---------

Co-authored-by: Cypherm <28184436+Cypherm@users.noreply.github.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Ayaan Zaidi <hi@obviy.us>
2026-03-21 10:10:38 +05:30

82 lines
2.1 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { fetchTelegramChatId } from "./api-fetch.js";
describe("fetchTelegramChatId", () => {
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);
await fetchTelegramChatId({ token: "abc", chatId: "@user" });
expect(fetchMock).toHaveBeenCalledWith(
"https://api.telegram.org/botabc/getChat?chat_id=%40user",
undefined,
);
});
it("uses caller-provided fetch impl when present", async () => {
const customFetch = vi.fn(async () => ({
ok: true,
json: async () => ({ ok: true, result: { id: 12345 } }),
}));
vi.stubGlobal(
"fetch",
vi.fn(async () => {
throw new Error("global fetch should not be called");
}),
);
await fetchTelegramChatId({
token: "abc",
chatId: "@user",
fetchImpl: customFetch as unknown as typeof fetch,
});
expect(customFetch).toHaveBeenCalledWith(
"https://api.telegram.org/botabc/getChat?chat_id=%40user",
undefined,
);
});
});