test(webfetch): keep tool execution in scope

Execute the webfetch tool inside the same provided Effect scope used for initialization so the HTTP client layer stays alive for the assertions instead of interrupting the test fibers.
This commit is contained in:
Kit Langton
2026-04-11 21:12:33 -04:00
parent a550ebf682
commit 7bdb4e6280

View File

@@ -26,9 +26,10 @@ async function withFetch(fetch: (req: Request) => Response | Promise<Response>,
await fn(server.url)
}
function initTool() {
function exec(args: { url: string; format: "text" | "markdown" | "html" }) {
return WebFetchTool.pipe(
Effect.flatMap((info) => info.init()),
Effect.flatMap((tool) => tool.execute(args, ctx)),
Effect.provide(Layer.mergeAll(FetchHttpClient.layer, Truncate.defaultLayer, Agent.defaultLayer)),
Effect.runPromise,
)
@@ -43,10 +44,7 @@ describe("tool.webfetch", () => {
await Instance.provide({
directory: projectRoot,
fn: async () => {
const webfetch = await initTool()
const result = await Effect.runPromise(
webfetch.execute({ url: new URL("/image.png", url).toString(), format: "markdown" }, ctx),
)
const result = await exec({ url: new URL("/image.png", url).toString(), format: "markdown" })
expect(result.output).toBe("Image fetched successfully")
expect(result.attachments).toBeDefined()
expect(result.attachments?.length).toBe(1)
@@ -74,10 +72,7 @@ describe("tool.webfetch", () => {
await Instance.provide({
directory: projectRoot,
fn: async () => {
const webfetch = await initTool()
const result = await Effect.runPromise(
webfetch.execute({ url: new URL("/image.svg", url).toString(), format: "html" }, ctx),
)
const result = await exec({ url: new URL("/image.svg", url).toString(), format: "html" })
expect(result.output).toContain("<svg")
expect(result.attachments).toBeUndefined()
},
@@ -97,10 +92,7 @@ describe("tool.webfetch", () => {
await Instance.provide({
directory: projectRoot,
fn: async () => {
const webfetch = await initTool()
const result = await Effect.runPromise(
webfetch.execute({ url: new URL("/file.txt", url).toString(), format: "text" }, ctx),
)
const result = await exec({ url: new URL("/file.txt", url).toString(), format: "text" })
expect(result.output).toBe("hello from webfetch")
expect(result.attachments).toBeUndefined()
},