| import { test, expect, describe, spyOn } from "bun:test"; |
| import { jinaFetch } from "../jina_fetch"; |
|
|
| describe("WebFetchTool - Jina Fetch", () => { |
|
|
| test("应该能成功抓取网页并返回格式化的 JSON", async () => { |
| const testUrl = "https://httpbin.org/html"; |
| const resultJson = await jinaFetch(testUrl); |
|
|
| |
| expect(resultJson).not.toBeNull(); |
|
|
| if (resultJson) { |
| const data = JSON.parse(resultJson); |
| |
| |
| expect(data).toMatchObject({ |
| url: testUrl, |
| extractor: "jina" |
| }); |
| |
| expect(typeof data.text).toBe("string"); |
| expect(typeof data.length).toBe("number"); |
| |
| |
| expect(data.text.length).toBeGreaterThan(0); |
| |
| |
| |
| expect(data.text).toMatch(/#|Title/); |
|
|
| console.log(`✅ 测试成功,抓取内容长度: ${data.length}`); |
| } |
| }, 30000); |
|
|
| test("面对无效 URL 应该返回 null 而不崩溃", async () => { |
| |
| |
| const errorSpy = spyOn(console, "error").mockImplementation(() => {}); |
| |
| const invalidUrl = "https://not-a-real-url-123456.com"; |
| const result = await jinaFetch(invalidUrl); |
| |
| |
| expect(result).toBeNull(); |
| |
| |
| expect(errorSpy).toHaveBeenCalled(); |
|
|
| |
| errorSpy.mockRestore(); |
| }); |
|
|
| test("面对空输入或非法格式应该直接返回 null", async () => { |
| const result = await jinaFetch(""); |
| expect(result).toBeNull(); |
| }); |
|
|
| }); |
|
|