|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import path from "path" |
| 3 | +import { Instance } from "../../src/project/instance" |
| 4 | +import { WebFetchTool } from "../../src/tool/webfetch" |
| 5 | + |
| 6 | +const projectRoot = path.join(import.meta.dir, "../..") |
| 7 | + |
| 8 | +const ctx = { |
| 9 | + sessionID: "test", |
| 10 | + messageID: "message", |
| 11 | + callID: "", |
| 12 | + agent: "build", |
| 13 | + abort: AbortSignal.any([]), |
| 14 | + messages: [], |
| 15 | + metadata: () => {}, |
| 16 | + ask: async () => {}, |
| 17 | +} |
| 18 | + |
| 19 | +async function withFetch( |
| 20 | + mockFetch: (input: string | URL | Request, init?: RequestInit) => Promise<Response>, |
| 21 | + fn: () => Promise<void>, |
| 22 | +) { |
| 23 | + const originalFetch = globalThis.fetch |
| 24 | + globalThis.fetch = mockFetch as unknown as typeof fetch |
| 25 | + try { |
| 26 | + await fn() |
| 27 | + } finally { |
| 28 | + globalThis.fetch = originalFetch |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +describe("tool.webfetch", () => { |
| 33 | + test("returns image responses as file attachments", async () => { |
| 34 | + const bytes = new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10]) |
| 35 | + await withFetch( |
| 36 | + async () => new Response(bytes, { status: 200, headers: { "content-type": "IMAGE/PNG; charset=binary" } }), |
| 37 | + async () => { |
| 38 | + await Instance.provide({ |
| 39 | + directory: projectRoot, |
| 40 | + fn: async () => { |
| 41 | + const webfetch = await WebFetchTool.init() |
| 42 | + const result = await webfetch.execute({ url: "https://example.com/image.png", format: "markdown" }, ctx) |
| 43 | + expect(result.output).toBe("Image fetched successfully") |
| 44 | + expect(result.attachments).toBeDefined() |
| 45 | + expect(result.attachments?.length).toBe(1) |
| 46 | + expect(result.attachments?.[0].type).toBe("file") |
| 47 | + expect(result.attachments?.[0].mime).toBe("image/png") |
| 48 | + expect(result.attachments?.[0].url.startsWith("data:image/png;base64,")).toBe(true) |
| 49 | + }, |
| 50 | + }) |
| 51 | + }, |
| 52 | + ) |
| 53 | + }) |
| 54 | + |
| 55 | + test("keeps svg as text output", async () => { |
| 56 | + const svg = '<svg xmlns="http://www.w3.org/2000/svg"><text>hello</text></svg>' |
| 57 | + await withFetch( |
| 58 | + async () => |
| 59 | + new Response(svg, { |
| 60 | + status: 200, |
| 61 | + headers: { "content-type": "image/svg+xml; charset=UTF-8" }, |
| 62 | + }), |
| 63 | + async () => { |
| 64 | + await Instance.provide({ |
| 65 | + directory: projectRoot, |
| 66 | + fn: async () => { |
| 67 | + const webfetch = await WebFetchTool.init() |
| 68 | + const result = await webfetch.execute({ url: "https://example.com/image.svg", format: "html" }, ctx) |
| 69 | + expect(result.output).toContain("<svg") |
| 70 | + expect(result.attachments).toBeUndefined() |
| 71 | + }, |
| 72 | + }) |
| 73 | + }, |
| 74 | + ) |
| 75 | + }) |
| 76 | + |
| 77 | + test("keeps text responses as text output", async () => { |
| 78 | + await withFetch( |
| 79 | + async () => |
| 80 | + new Response("hello from webfetch", { |
| 81 | + status: 200, |
| 82 | + headers: { "content-type": "text/plain; charset=utf-8" }, |
| 83 | + }), |
| 84 | + async () => { |
| 85 | + await Instance.provide({ |
| 86 | + directory: projectRoot, |
| 87 | + fn: async () => { |
| 88 | + const webfetch = await WebFetchTool.init() |
| 89 | + const result = await webfetch.execute({ url: "https://example.com/file.txt", format: "text" }, ctx) |
| 90 | + expect(result.output).toBe("hello from webfetch") |
| 91 | + expect(result.attachments).toBeUndefined() |
| 92 | + }, |
| 93 | + }) |
| 94 | + }, |
| 95 | + ) |
| 96 | + }) |
| 97 | +}) |
0 commit comments