1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
import { createLogger, type HostAPI, type ToolExecuteContext } from "@dispatch/kernel";
import { afterEach, describe, expect, it, vi } from "vitest";
import { activate, extension, manifest } from "./extension.js";
function stubCtx(overrides?: Partial<ToolExecuteContext>): ToolExecuteContext {
return {
toolCallId: "test-call-1",
onOutput: () => {},
signal: new AbortController().signal,
log: createLogger(
{ extensionId: "test" },
{ emit: () => {} },
{ now: () => 0, newId: () => "id" },
),
...overrides,
};
}
function makeFakeHost(): { host: HostAPI; defineTool: ReturnType<typeof vi.fn> } {
const defineTool = vi.fn();
const host = {
defineTool,
logger: {
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
span: vi.fn(() => ({ end: vi.fn() })),
},
} as unknown as HostAPI;
return { host, defineTool };
}
const ORIG_FETCH = globalThis.fetch;
const ORIG_ENV = process.env.FIRECRAWL_BASE_URL;
function restoreEnv(): void {
if (ORIG_ENV === undefined) {
delete process.env.FIRECRAWL_BASE_URL;
} else {
process.env.FIRECRAWL_BASE_URL = ORIG_ENV;
}
}
afterEach(() => {
globalThis.fetch = ORIG_FETCH;
restoreEnv();
});
function stubFetchCapture(): { calls: Array<{ url: string }> } {
const calls: Array<{ url: string }> = [];
globalThis.fetch = vi.fn(async (input: string | URL | Request) => {
calls.push({ url: String(input) });
return new Response(JSON.stringify({ success: true, data: [] }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}) as unknown as typeof globalThis.fetch;
return { calls };
}
describe("tool-web-search activation", () => {
it("registers the 'web_search' tool (defineTool called)", () => {
const { host, defineTool } = makeFakeHost();
activate(host);
expect(defineTool).toHaveBeenCalledTimes(1);
const registered = defineTool.mock.calls[0]?.[0];
if (!registered) throw new Error("no tool registered");
expect(registered.name).toBe("web_search");
expect(registered.concurrencySafe).toBe(true);
});
it("uses FIRECRAWL_BASE_URL from env", async () => {
process.env.FIRECRAWL_BASE_URL = "http://env-firecrawl.local/v1";
const { calls } = stubFetchCapture();
const { host, defineTool } = makeFakeHost();
activate(host);
const tool = defineTool.mock.calls[0]?.[0];
if (!tool) throw new Error("no tool registered");
await tool.execute({ query: "hello" }, stubCtx());
expect(calls.length).toBeGreaterThan(0);
expect(calls[0]?.url).toContain("http://env-firecrawl.local/v1/search");
});
it("uses default base URL when env unset", async () => {
delete process.env.FIRECRAWL_BASE_URL;
const { calls } = stubFetchCapture();
const { host, defineTool } = makeFakeHost();
activate(host);
const tool = defineTool.mock.calls[0]?.[0];
if (!tool) throw new Error("no tool registered");
await tool.execute({ query: "hello" }, stubCtx());
expect(calls.length).toBeGreaterThan(0);
expect(calls[0]?.url).toContain("100.102.55.49:31329/v1/search");
});
});
describe("tool-web-search manifest", () => {
it("declares network capability + web_search contribution", () => {
expect(manifest.id).toBe("tool-web-search");
expect(manifest.capabilities).toEqual({ network: true });
expect(manifest.contributes).toEqual({ tools: ["web_search"] });
expect(manifest.trust).toBe("bundled");
expect(manifest.activation).toBe("eager");
});
it("extension bundles the manifest + activate", () => {
expect(extension.manifest).toBe(manifest);
expect(typeof extension.activate).toBe("function");
});
});
|