mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-18 04:57:27 +00:00
perf(test): fold telegram download tests into fetch suite
This commit is contained in:
@@ -1,35 +0,0 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { downloadTelegramFile, getTelegramFile, type TelegramFileInfo } from "./download.js";
|
||||
|
||||
describe("telegram download", () => {
|
||||
it("fetches file info", async () => {
|
||||
const json = vi.fn().mockResolvedValue({ ok: true, result: { file_path: "photos/1.jpg" } });
|
||||
vi.spyOn(global, "fetch" as never).mockResolvedValueOnce({
|
||||
ok: true,
|
||||
status: 200,
|
||||
statusText: "OK",
|
||||
json,
|
||||
} as Response);
|
||||
const info = await getTelegramFile("tok", "fid");
|
||||
expect(info.file_path).toBe("photos/1.jpg");
|
||||
});
|
||||
|
||||
it("downloads and saves", async () => {
|
||||
const info: TelegramFileInfo = {
|
||||
file_id: "fid",
|
||||
file_path: "photos/1.jpg",
|
||||
};
|
||||
const arrayBuffer = async () => new Uint8Array([1, 2, 3, 4]).buffer;
|
||||
vi.spyOn(global, "fetch" as never).mockResolvedValueOnce({
|
||||
ok: true,
|
||||
status: 200,
|
||||
statusText: "OK",
|
||||
body: true,
|
||||
arrayBuffer,
|
||||
headers: { get: () => "image/jpeg" },
|
||||
} as Response);
|
||||
const saved = await downloadTelegramFile("tok", info, 1024 * 1024);
|
||||
expect(saved.path).toBeTruthy();
|
||||
expect(saved.contentType).toBe("image/jpeg");
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,5 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { downloadTelegramFile, getTelegramFile, type TelegramFileInfo } from "./download.js";
|
||||
import { resetTelegramFetchStateForTests, resolveTelegramFetch } from "./fetch.js";
|
||||
|
||||
const setDefaultAutoSelectFamily = vi.hoisted(() => vi.fn());
|
||||
@@ -11,21 +12,21 @@ vi.mock("node:net", async () => {
|
||||
};
|
||||
});
|
||||
|
||||
const originalFetch = globalThis.fetch;
|
||||
|
||||
afterEach(() => {
|
||||
resetTelegramFetchStateForTests();
|
||||
setDefaultAutoSelectFamily.mockReset();
|
||||
vi.unstubAllEnvs();
|
||||
vi.clearAllMocks();
|
||||
if (originalFetch) {
|
||||
globalThis.fetch = originalFetch;
|
||||
} else {
|
||||
delete (globalThis as { fetch?: typeof fetch }).fetch;
|
||||
}
|
||||
});
|
||||
|
||||
describe("resolveTelegramFetch", () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
|
||||
afterEach(() => {
|
||||
resetTelegramFetchStateForTests();
|
||||
setDefaultAutoSelectFamily.mockReset();
|
||||
vi.unstubAllEnvs();
|
||||
vi.clearAllMocks();
|
||||
if (originalFetch) {
|
||||
globalThis.fetch = originalFetch;
|
||||
} else {
|
||||
delete (globalThis as { fetch?: typeof fetch }).fetch;
|
||||
}
|
||||
});
|
||||
|
||||
it("returns wrapped global fetch when available", async () => {
|
||||
const fetchMock = vi.fn(async () => ({}));
|
||||
globalThis.fetch = fetchMock as unknown as typeof fetch;
|
||||
@@ -60,3 +61,36 @@ describe("resolveTelegramFetch", () => {
|
||||
expect(setDefaultAutoSelectFamily).toHaveBeenCalledWith(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("telegram download", () => {
|
||||
it("fetches file info", async () => {
|
||||
const json = vi.fn().mockResolvedValue({ ok: true, result: { file_path: "photos/1.jpg" } });
|
||||
vi.spyOn(globalThis, "fetch" as never).mockResolvedValueOnce({
|
||||
ok: true,
|
||||
status: 200,
|
||||
statusText: "OK",
|
||||
json,
|
||||
} as Response);
|
||||
const info = await getTelegramFile("tok", "fid");
|
||||
expect(info.file_path).toBe("photos/1.jpg");
|
||||
});
|
||||
|
||||
it("downloads and saves", async () => {
|
||||
const info: TelegramFileInfo = {
|
||||
file_id: "fid",
|
||||
file_path: "photos/1.jpg",
|
||||
};
|
||||
const arrayBuffer = async () => new Uint8Array([1, 2, 3, 4]).buffer;
|
||||
vi.spyOn(globalThis, "fetch" as never).mockResolvedValueOnce({
|
||||
ok: true,
|
||||
status: 200,
|
||||
statusText: "OK",
|
||||
body: true,
|
||||
arrayBuffer,
|
||||
headers: { get: () => "image/jpeg" },
|
||||
} as Response);
|
||||
const saved = await downloadTelegramFile("tok", info, 1024 * 1024);
|
||||
expect(saved.path).toBeTruthy();
|
||||
expect(saved.contentType).toBe("image/jpeg");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user