@crewhaus/tool-web 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.test.ts DELETED
@@ -1,774 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, mock, spyOn, test } from "bun:test";
2
- import {
3
- WebFetchPermissionError,
4
- _resetWebFetchConfig,
5
- _setDnsLookup,
6
- _setRawFetch,
7
- getWebFetchConfig,
8
- htmlToMarkdown,
9
- registerWebFetchConfig,
10
- webFetch,
11
- webSearch,
12
- } from "./index";
13
-
14
- const HTML_PAGE = `<!doctype html>
15
- <html><head><title>Example Domain</title>
16
- <style>p{color:red}</style>
17
- <script>document.write("evil")</script>
18
- </head><body>
19
- <main>
20
- <h1>Example Domain</h1>
21
- <p>This domain is for use in illustrative examples in documents.</p>
22
- <p>You may use this domain in literature without prior coordination or asking for permission.</p>
23
- <a href="https://www.iana.org/domains/example">More information...</a>
24
- </main>
25
- </body></html>`;
26
-
27
- const ORIGINAL_ENV: Record<string, string | undefined> = {};
28
-
29
- beforeEach(() => {
30
- _resetWebFetchConfig();
31
- _setRawFetch(undefined);
32
- // Keep the SSRF DNS backstop offline + deterministic: every host resolves
33
- // to a public IP by default. Individual tests override to exercise rebinding.
34
- _setDnsLookup(async () => ({ address: "93.184.216.34", family: 4 }));
35
- for (const key of ["CREWHAUS_SEARCH_PROVIDER", "CREWHAUS_SEARCH_API_KEY"]) {
36
- ORIGINAL_ENV[key] = process.env[key];
37
- delete process.env[key];
38
- }
39
- });
40
-
41
- afterEach(() => {
42
- _resetWebFetchConfig();
43
- _setRawFetch(undefined);
44
- _setDnsLookup(undefined);
45
- for (const [key, value] of Object.entries(ORIGINAL_ENV)) {
46
- if (value === undefined) delete process.env[key];
47
- else process.env[key] = value;
48
- }
49
- });
50
-
51
- describe("WebFetch — registered tool metadata", () => {
52
- test("name + flags", () => {
53
- expect(webFetch.name).toBe("WebFetch");
54
- expect(webFetch.readOnly).toBe(true);
55
- expect(webFetch.concurrencySafe).toBe(true);
56
- expect(webFetch.destructive).toBe(false);
57
- });
58
- });
59
-
60
- describe("htmlToMarkdown", () => {
61
- test("strips script + style and renders main content", () => {
62
- const md = htmlToMarkdown(HTML_PAGE);
63
- expect(md).toContain("Example Domain");
64
- expect(md).toContain("illustrative examples");
65
- expect(md).not.toContain("color:red");
66
- expect(md).not.toContain("evil");
67
- });
68
-
69
- test("renders headings as ATX (#)", () => {
70
- const md = htmlToMarkdown("<h1>Title</h1><p>x</p>");
71
- expect(md.startsWith("# Title")).toBe(true);
72
- });
73
-
74
- test("renders links inline", () => {
75
- const md = htmlToMarkdown('<p><a href="https://x.com">click</a></p>');
76
- expect(md).toContain("[click](https://x.com)");
77
- });
78
- });
79
-
80
- describe("WebFetch — happy path", () => {
81
- test("converts HTML to markdown and includes URL/Status header", async () => {
82
- _setRawFetch(
83
- async () =>
84
- new Response(HTML_PAGE, {
85
- status: 200,
86
- headers: { "content-type": "text/html; charset=utf-8" },
87
- }),
88
- );
89
- const result = await webFetch.execute({ url: "https://example.com/" });
90
- if (typeof result !== "string") throw new Error("expected string result");
91
- expect(result).toContain("URL: https://example.com/");
92
- expect(result).toContain("Status: 200");
93
- expect(result).toContain("Example Domain");
94
- });
95
-
96
- test("plain text passes through unchanged", async () => {
97
- _setRawFetch(
98
- async () =>
99
- new Response("hello world", { status: 200, headers: { "content-type": "text/plain" } }),
100
- );
101
- const result = await webFetch.execute({ url: "https://example.com/" });
102
- if (typeof result !== "string") throw new Error("expected string result");
103
- expect(result).toContain("hello world");
104
- });
105
-
106
- test("user prompt is prepended to the body, not sent to the model from the tool", async () => {
107
- _setRawFetch(
108
- async () =>
109
- new Response(HTML_PAGE, { status: 200, headers: { "content-type": "text/html" } }),
110
- );
111
- const result = await webFetch.execute({
112
- url: "https://example.com/",
113
- prompt: "summarise the page",
114
- });
115
- if (typeof result !== "string") throw new Error("expected string result");
116
- expect(result).toContain("[user prompt: summarise the page]");
117
- });
118
- });
119
-
120
- describe("T8 — WebFetch scheme + allow-list", () => {
121
- test("rejects file:// scheme", async () => {
122
- _setRawFetch(async () => {
123
- throw new Error("must not call");
124
- });
125
- await expect(webFetch.execute({ url: "file:///etc/passwd" })).rejects.toBeInstanceOf(
126
- WebFetchPermissionError,
127
- );
128
- });
129
-
130
- test("rejects javascript: scheme", async () => {
131
- _setRawFetch(async () => {
132
- throw new Error("must not call");
133
- });
134
- await expect(webFetch.execute({ url: "javascript:alert(1)" })).rejects.toBeInstanceOf(
135
- WebFetchPermissionError,
136
- );
137
- });
138
-
139
- test("empty allow-list = allow-all (Section 14 says optional)", async () => {
140
- _setRawFetch(
141
- async () => new Response("ok", { status: 200, headers: { "content-type": "text/plain" } }),
142
- );
143
- await expect(webFetch.execute({ url: "https://random.example/" })).resolves.toBeDefined();
144
- });
145
-
146
- test("populated allow-list permits exact host and subdomains", async () => {
147
- registerWebFetchConfig({ allowed_domains: ["example.com"] });
148
- _setRawFetch(
149
- async () => new Response("ok", { status: 200, headers: { "content-type": "text/plain" } }),
150
- );
151
- await expect(webFetch.execute({ url: "https://example.com/" })).resolves.toBeDefined();
152
- await expect(webFetch.execute({ url: "https://api.example.com/" })).resolves.toBeDefined();
153
- });
154
-
155
- test("populated allow-list rejects unrelated hosts", async () => {
156
- registerWebFetchConfig({ allowed_domains: ["example.com"] });
157
- _setRawFetch(
158
- async () => new Response("ok", { status: 200, headers: { "content-type": "text/plain" } }),
159
- );
160
- await expect(webFetch.execute({ url: "https://evil.com/" })).rejects.toBeInstanceOf(
161
- WebFetchPermissionError,
162
- );
163
- });
164
-
165
- test("case-mutated host still matches the allow-list (canonicalised)", async () => {
166
- registerWebFetchConfig({ allowed_domains: ["Example.com"] });
167
- _setRawFetch(
168
- async () => new Response("ok", { status: 200, headers: { "content-type": "text/plain" } }),
169
- );
170
- await expect(webFetch.execute({ url: "https://EXAMPLE.com/" })).resolves.toBeDefined();
171
- });
172
- });
173
-
174
- describe("WebFetch — SSRF guard (#141)", () => {
175
- test("rejects the cloud-metadata IP even with an empty allow-list", async () => {
176
- _setRawFetch(async () => {
177
- throw new Error("must not fetch");
178
- });
179
- await expect(
180
- webFetch.execute({
181
- url: "http://169.254.169.254/latest/meta-data/iam/security-credentials/",
182
- }),
183
- ).rejects.toBeInstanceOf(WebFetchPermissionError);
184
- });
185
-
186
- test("rejects loopback localhost", async () => {
187
- _setRawFetch(async () => {
188
- throw new Error("must not fetch");
189
- });
190
- await expect(webFetch.execute({ url: "http://localhost:6379/" })).rejects.toBeInstanceOf(
191
- WebFetchPermissionError,
192
- );
193
- });
194
-
195
- test("rejects an RFC1918 IP literal", async () => {
196
- _setRawFetch(async () => {
197
- throw new Error("must not fetch");
198
- });
199
- await expect(webFetch.execute({ url: "http://10.0.0.5/" })).rejects.toBeInstanceOf(
200
- WebFetchPermissionError,
201
- );
202
- });
203
-
204
- test("rejects the IPv6 loopback [::1]", async () => {
205
- _setRawFetch(async () => {
206
- throw new Error("must not fetch");
207
- });
208
- await expect(webFetch.execute({ url: "http://[::1]:8080/" })).rejects.toBeInstanceOf(
209
- WebFetchPermissionError,
210
- );
211
- });
212
-
213
- test("rejects DNS rebinding: a public host that resolves to 127.0.0.1", async () => {
214
- _setDnsLookup(async () => ({ address: "127.0.0.1", family: 4 }));
215
- _setRawFetch(async () => {
216
- throw new Error("must not fetch");
217
- });
218
- await expect(
219
- webFetch.execute({ url: "https://totally-legit.example/" }),
220
- ).rejects.toBeInstanceOf(WebFetchPermissionError);
221
- });
222
-
223
- test("guard fires even when the internal host is explicitly allow-listed", async () => {
224
- registerWebFetchConfig({ allowed_domains: ["169.254.169.254"] });
225
- _setRawFetch(async () => {
226
- throw new Error("must not fetch");
227
- });
228
- await expect(webFetch.execute({ url: "http://169.254.169.254/" })).rejects.toBeInstanceOf(
229
- WebFetchPermissionError,
230
- );
231
- });
232
-
233
- test("blocks a redirect that lands on an internal host (per-hop)", async () => {
234
- let hops = 0;
235
- _setRawFetch(async () => {
236
- hops++;
237
- return new Response(null, {
238
- status: 302,
239
- headers: { location: "http://169.254.169.254/" },
240
- });
241
- });
242
- await expect(webFetch.execute({ url: "https://example.com/" })).rejects.toBeInstanceOf(
243
- WebFetchPermissionError,
244
- );
245
- expect(hops).toBe(1);
246
- });
247
-
248
- test("still allows a normal public host (guard is not over-broad)", async () => {
249
- _setRawFetch(
250
- async () => new Response("ok", { status: 200, headers: { "content-type": "text/plain" } }),
251
- );
252
- await expect(webFetch.execute({ url: "https://example.com/" })).resolves.toBeDefined();
253
- });
254
-
255
- // `new URL` compresses [::ffff:127.0.0.1] to [::ffff:7f00:1]; the old
256
- // dotted-only regex missed the hex form. Now classified via the embedded v4.
257
- test("rejects the hex IPv4-mapped IPv6 loopback [::ffff:7f00:1]", async () => {
258
- _setRawFetch(async () => {
259
- throw new Error("must not fetch");
260
- });
261
- await expect(webFetch.execute({ url: "http://[::ffff:127.0.0.1]/" })).rejects.toBeInstanceOf(
262
- WebFetchPermissionError,
263
- );
264
- });
265
-
266
- test("rejects an octal-encoded loopback literal", async () => {
267
- _setRawFetch(async () => {
268
- throw new Error("must not fetch");
269
- });
270
- await expect(webFetch.execute({ url: "http://0177.0.0.1/" })).rejects.toBeInstanceOf(
271
- WebFetchPermissionError,
272
- );
273
- });
274
-
275
- // The actual rebinding defense: the socket is pinned to the IP we vetted.
276
- test("pins the connection to the resolved IP (closes the rebinding TOCTOU)", async () => {
277
- _setDnsLookup(async () => ({ address: "93.184.216.34", family: 4 }));
278
- let seenPin: string | undefined;
279
- _setRawFetch(async (_req, pinnedIp) => {
280
- seenPin = pinnedIp;
281
- return new Response("ok", { status: 200, headers: { "content-type": "text/plain" } });
282
- });
283
- await webFetch.execute({ url: "https://innocent.example/" });
284
- expect(seenPin).toBe("93.184.216.34");
285
- });
286
-
287
- test("pins a public IP-literal host to the literal itself", async () => {
288
- let seenPin: string | undefined;
289
- _setRawFetch(async (_req, pinnedIp) => {
290
- seenPin = pinnedIp;
291
- return new Response("ok", { status: 200, headers: { "content-type": "text/plain" } });
292
- });
293
- await webFetch.execute({ url: "https://93.184.216.34/" });
294
- expect(seenPin).toBe("93.184.216.34");
295
- });
296
- });
297
-
298
- describe("T8 — WebFetch redirects", () => {
299
- test("follows up to 5 redirects then aborts", async () => {
300
- let hops = 0;
301
- _setRawFetch(async () => {
302
- hops++;
303
- return new Response(null, {
304
- status: 302,
305
- headers: { location: `https://example.com/h${hops}` },
306
- });
307
- });
308
- await expect(webFetch.execute({ url: "https://example.com/" })).rejects.toBeInstanceOf(
309
- WebFetchPermissionError,
310
- );
311
- expect(hops).toBeGreaterThanOrEqual(6);
312
- });
313
-
314
- test("redirect to a non-allowed host is rejected on the hop", async () => {
315
- registerWebFetchConfig({ allowed_domains: ["example.com"] });
316
- let hops = 0;
317
- _setRawFetch(async () => {
318
- hops++;
319
- return new Response(null, {
320
- status: 302,
321
- headers: { location: "https://evil.com/" },
322
- });
323
- });
324
- await expect(webFetch.execute({ url: "https://example.com/" })).rejects.toBeInstanceOf(
325
- WebFetchPermissionError,
326
- );
327
- expect(hops).toBe(1);
328
- });
329
- });
330
-
331
- describe("T8 — WebFetch body cap", () => {
332
- test("rejects responses over 5 MB", async () => {
333
- const sixMb = "x".repeat(6 * 1024 * 1024);
334
- _setRawFetch(
335
- async () => new Response(sixMb, { status: 200, headers: { "content-type": "text/plain" } }),
336
- );
337
- await expect(webFetch.execute({ url: "https://example.com/big" })).rejects.toBeInstanceOf(
338
- WebFetchPermissionError,
339
- );
340
- });
341
- });
342
-
343
- describe("WebSearch — registered tool metadata", () => {
344
- test("name + flags", () => {
345
- expect(webSearch.name).toBe("WebSearch");
346
- expect(webSearch.readOnly).toBe(true);
347
- expect(webSearch.concurrencySafe).toBe(true);
348
- });
349
- });
350
-
351
- describe("WebSearch — env-driven dispatch", () => {
352
- test("returns a clean refusal when env is unset", async () => {
353
- _setRawFetch(async () => {
354
- throw new Error("must not call");
355
- });
356
- const result = await webSearch.execute({ query: "anything" });
357
- if (typeof result !== "string") throw new Error("expected string result");
358
- expect(result).toContain("WebSearch unavailable");
359
- expect(result).toContain("CREWHAUS_SEARCH_PROVIDER");
360
- });
361
-
362
- test("returns a clean refusal for an unknown provider", async () => {
363
- process.env["CREWHAUS_SEARCH_PROVIDER"] = "duckduckgo";
364
- process.env["CREWHAUS_SEARCH_API_KEY"] = "x";
365
- _setRawFetch(async () => {
366
- throw new Error("must not call");
367
- });
368
- const result = await webSearch.execute({ query: "anything" });
369
- if (typeof result !== "string") throw new Error("expected string result");
370
- expect(result).toContain("unknown provider");
371
- });
372
-
373
- test("contracts a Brave response into uniform hit list", async () => {
374
- process.env["CREWHAUS_SEARCH_PROVIDER"] = "brave";
375
- process.env["CREWHAUS_SEARCH_API_KEY"] = "test-token";
376
- _setRawFetch(async (req) => {
377
- expect(req.url).toContain("api.search.brave.com");
378
- expect(req.headers.get("x-subscription-token")).toBe("test-token");
379
- return new Response(
380
- JSON.stringify({
381
- web: {
382
- results: [
383
- {
384
- title: "Bun docs",
385
- url: "https://bun.sh/docs",
386
- description: "Fast all-in-one runtime",
387
- },
388
- { title: "Node docs", url: "https://nodejs.org", description: "JS runtime" },
389
- ],
390
- },
391
- }),
392
- { status: 200, headers: { "content-type": "application/json" } },
393
- );
394
- });
395
- const result = await webSearch.execute({ query: "bun runtime" });
396
- if (typeof result !== "string") throw new Error("expected string result");
397
- expect(result).toContain("Bun docs");
398
- expect(result).toContain("https://bun.sh/docs");
399
- expect(result).toContain("Node docs");
400
- });
401
-
402
- test("contracts a Tavily response into uniform hit list", async () => {
403
- process.env["CREWHAUS_SEARCH_PROVIDER"] = "tavily";
404
- process.env["CREWHAUS_SEARCH_API_KEY"] = "tvly-test";
405
- _setRawFetch(async (req) => {
406
- expect(req.url).toContain("api.tavily.com");
407
- const body = (await req.json()) as { api_key: string };
408
- expect(body.api_key).toBe("tvly-test");
409
- return new Response(
410
- JSON.stringify({
411
- results: [
412
- { title: "TS Handbook", url: "https://www.typescriptlang.org", content: "Docs" },
413
- ],
414
- }),
415
- { status: 200, headers: { "content-type": "application/json" } },
416
- );
417
- });
418
- const result = await webSearch.execute({ query: "typescript" });
419
- if (typeof result !== "string") throw new Error("expected string result");
420
- expect(result).toContain("TS Handbook");
421
- });
422
-
423
- test("blocked_domains filters out matching hits", async () => {
424
- process.env["CREWHAUS_SEARCH_PROVIDER"] = "brave";
425
- process.env["CREWHAUS_SEARCH_API_KEY"] = "x";
426
- _setRawFetch(
427
- async () =>
428
- new Response(
429
- JSON.stringify({
430
- web: {
431
- results: [
432
- { title: "good", url: "https://example.com/page", description: "" },
433
- { title: "bad", url: "https://evil.com/page", description: "" },
434
- ],
435
- },
436
- }),
437
- { status: 200, headers: { "content-type": "application/json" } },
438
- ),
439
- );
440
- const result = await webSearch.execute({
441
- query: "x",
442
- blocked_domains: ["evil.com"],
443
- });
444
- if (typeof result !== "string") throw new Error("expected string result");
445
- expect(result).toContain("good");
446
- expect(result).not.toContain("evil.com");
447
- });
448
-
449
- test("allowed_domains keeps only matching hosts (exact + subdomain)", async () => {
450
- process.env["CREWHAUS_SEARCH_PROVIDER"] = "brave";
451
- process.env["CREWHAUS_SEARCH_API_KEY"] = "x";
452
- _setRawFetch(
453
- async () =>
454
- new Response(
455
- JSON.stringify({
456
- web: {
457
- results: [
458
- { title: "keep-exact", url: "https://example.com/a", description: "" },
459
- { title: "keep-sub", url: "https://docs.example.com/b", description: "" },
460
- { title: "drop-other", url: "https://other.org/c", description: "" },
461
- { title: "drop-bad-url", url: "::not a url::", description: "" },
462
- ],
463
- },
464
- }),
465
- { status: 200, headers: { "content-type": "application/json" } },
466
- ),
467
- );
468
- const result = await webSearch.execute({
469
- query: "x",
470
- allowed_domains: ["Example.com"],
471
- });
472
- if (typeof result !== "string") throw new Error("expected string result");
473
- expect(result).toContain("keep-exact");
474
- expect(result).toContain("keep-sub");
475
- expect(result).not.toContain("drop-other");
476
- expect(result).not.toContain("drop-bad-url");
477
- });
478
-
479
- test("when every hit is filtered out, formatHits returns the empty marker", async () => {
480
- process.env["CREWHAUS_SEARCH_PROVIDER"] = "brave";
481
- process.env["CREWHAUS_SEARCH_API_KEY"] = "x";
482
- _setRawFetch(
483
- async () =>
484
- new Response(
485
- JSON.stringify({
486
- web: { results: [{ title: "gone", url: "https://nope.org/x", description: "" }] },
487
- }),
488
- { status: 200, headers: { "content-type": "application/json" } },
489
- ),
490
- );
491
- const result = await webSearch.execute({ query: "x", allowed_domains: ["example.com"] });
492
- expect(result).toBe("[no results]");
493
- });
494
- });
495
-
496
- describe("WebFetch — content-type branches", () => {
497
- test("application/json passes through unchanged", async () => {
498
- _setRawFetch(
499
- async () =>
500
- new Response('{"ok":true}', {
501
- status: 200,
502
- headers: { "content-type": "application/json" },
503
- }),
504
- );
505
- const result = await webFetch.execute({ url: "https://example.com/data.json" });
506
- if (typeof result !== "string") throw new Error("expected string result");
507
- expect(result).toContain('{"ok":true}');
508
- expect(result).toContain("Content-Type: application/json");
509
- });
510
-
511
- test("application/xhtml+xml is rendered as markdown", async () => {
512
- _setRawFetch(
513
- async () =>
514
- new Response("<h1>Xhtml Title</h1><p>body</p>", {
515
- status: 200,
516
- headers: { "content-type": "application/xhtml+xml" },
517
- }),
518
- );
519
- const result = await webFetch.execute({ url: "https://example.com/page.xhtml" });
520
- if (typeof result !== "string") throw new Error("expected string result");
521
- expect(result).toContain("# Xhtml Title");
522
- });
523
-
524
- test("a missing content-type (empty) is treated as text and passed through", async () => {
525
- // Construct a Response and strip its content-type so `ct === ""` fires.
526
- _setRawFetch(async () => {
527
- const res = new Response("raw bytes here", { status: 200 });
528
- res.headers.delete("content-type");
529
- return res;
530
- });
531
- const result = await webFetch.execute({ url: "https://example.com/blob" });
532
- if (typeof result !== "string") throw new Error("expected string result");
533
- expect(result).toContain("raw bytes here");
534
- });
535
-
536
- test("an unknown binary content-type yields the non-textual placeholder", async () => {
537
- _setRawFetch(
538
- async () =>
539
- new Response("PKzipbytes", {
540
- status: 200,
541
- headers: { "content-type": "application/zip" },
542
- }),
543
- );
544
- const result = await webFetch.execute({ url: "https://example.com/archive.zip" });
545
- if (typeof result !== "string") throw new Error("expected string result");
546
- expect(result).toContain("[non-textual content: application/zip");
547
- expect(result).toContain("not displayed]");
548
- });
549
-
550
- test("an empty (null-body) response yields an empty body string", async () => {
551
- // 204 responses have res.body === null — exercises the early return in
552
- // readBodyCapped.
553
- _setRawFetch(async () => new Response(null, { status: 204 }));
554
- const result = await webFetch.execute({ url: "https://example.com/empty" });
555
- if (typeof result !== "string") throw new Error("expected string result");
556
- expect(result).toContain("Status: 204");
557
- });
558
- });
559
-
560
- describe("WebFetch — invalid URL + redirect edge cases", () => {
561
- test("a malformed URL throws WebFetchPermissionError before any fetch", async () => {
562
- _setRawFetch(async () => {
563
- throw new Error("must not fetch");
564
- });
565
- await expect(webFetch.execute({ url: "not a url" })).rejects.toBeInstanceOf(
566
- WebFetchPermissionError,
567
- );
568
- });
569
-
570
- test("a redirect with an unparseable Location header is rejected", async () => {
571
- _setRawFetch(
572
- async () =>
573
- new Response(null, {
574
- status: 302,
575
- // A valid HTTP header value that `new URL(loc, base)` still rejects
576
- // (incomplete IPv6 literal) drives the redirect-parse catch branch.
577
- headers: { location: "http://[" },
578
- }),
579
- );
580
- await expect(webFetch.execute({ url: "https://example.com/" })).rejects.toBeInstanceOf(
581
- WebFetchPermissionError,
582
- );
583
- });
584
-
585
- test("a relative redirect Location is resolved against the current URL", async () => {
586
- let hop = 0;
587
- _setRawFetch(async (req) => {
588
- hop++;
589
- if (hop === 1) {
590
- return new Response(null, { status: 301, headers: { location: "/landing" } });
591
- }
592
- expect(req.url).toBe("https://example.com/landing");
593
- return new Response("arrived", { status: 200, headers: { "content-type": "text/plain" } });
594
- });
595
- const result = await webFetch.execute({ url: "https://example.com/start" });
596
- if (typeof result !== "string") throw new Error("expected string result");
597
- expect(result).toContain("arrived");
598
- expect(hop).toBe(2);
599
- });
600
-
601
- test("a 3xx without a Location header is treated as a terminal response", async () => {
602
- _setRawFetch(
603
- async () =>
604
- new Response("moved but no location", {
605
- status: 304,
606
- headers: { "content-type": "text/plain" },
607
- }),
608
- );
609
- const result = await webFetch.execute({ url: "https://example.com/" });
610
- if (typeof result !== "string") throw new Error("expected string result");
611
- expect(result).toContain("Status: 304");
612
- expect(result).toContain("moved but no location");
613
- });
614
- });
615
-
616
- describe("getWebFetchConfig", () => {
617
- test("reflects the most recently registered allow-list (lower-cased)", () => {
618
- expect(getWebFetchConfig().allowedDomains).toEqual([]);
619
- registerWebFetchConfig({ allowed_domains: ["Example.COM", "Foo.org"] });
620
- expect(getWebFetchConfig().allowedDomains).toEqual(["example.com", "foo.org"]);
621
- });
622
-
623
- test("registerWebFetchConfig honours the camelCase alias", () => {
624
- registerWebFetchConfig({ allowedDomains: ["Bar.NET"] });
625
- expect(getWebFetchConfig().allowedDomains).toEqual(["bar.net"]);
626
- });
627
- });
628
-
629
- describe("WebFetch — ctx.signal cancellation wiring", () => {
630
- test("an already-aborted ctx.signal still completes against a mocked fetch", async () => {
631
- // Drives the `ctx.signal.aborted === true` branch: ctrl.abort(reason) runs,
632
- // but our stub rawFetch ignores the signal and returns a Response.
633
- _setRawFetch(
634
- async () => new Response("ok", { status: 200, headers: { "content-type": "text/plain" } }),
635
- );
636
- const result = await webFetch.execute(
637
- { url: "https://example.com/" },
638
- { signal: AbortSignal.abort(new Error("pre-aborted")) },
639
- );
640
- if (typeof result !== "string") throw new Error("expected string result");
641
- expect(result).toContain("ok");
642
- });
643
-
644
- test("a later ctx.signal abort fires the once-listener without affecting the result", async () => {
645
- _setRawFetch(
646
- async () => new Response("ok", { status: 200, headers: { "content-type": "text/plain" } }),
647
- );
648
- const ctrl = new AbortController();
649
- const result = await webFetch.execute({ url: "https://example.com/" }, { signal: ctrl.signal });
650
- // Abort AFTER the fetch resolved — fires the registered "abort" listener
651
- // (its callback body runs) on the already-settled inner controller.
652
- ctrl.abort(new Error("late"));
653
- if (typeof result !== "string") throw new Error("expected string result");
654
- expect(result).toContain("ok");
655
- });
656
-
657
- test("the fetch-timeout setTimeout callback aborts the controller when fired", async () => {
658
- // Capture the timeout callback instead of waiting 30s, then invoke it by
659
- // hand so its body (ctrl.abort(new Error("fetch timeout"))) is exercised
660
- // deterministically — no real timer, no leaked handle.
661
- const captured: Array<() => void> = [];
662
- const setSpy = spyOn(globalThis, "setTimeout").mockImplementation(((fn: () => void) => {
663
- captured.push(fn);
664
- return 0 as unknown as ReturnType<typeof setTimeout>;
665
- }) as typeof setTimeout);
666
- const clearSpy = spyOn(globalThis, "clearTimeout").mockImplementation(
667
- (() => {}) as typeof clearTimeout,
668
- );
669
- try {
670
- _setRawFetch(
671
- async () => new Response("ok", { status: 200, headers: { "content-type": "text/plain" } }),
672
- );
673
- const result = await webFetch.execute({ url: "https://example.com/" });
674
- if (typeof result !== "string") throw new Error("expected string result");
675
- expect(result).toContain("ok");
676
- expect(captured.length).toBeGreaterThanOrEqual(1);
677
- // Invoke the captured timeout callback — covers the abort closure body.
678
- for (const fn of captured) fn();
679
- } finally {
680
- setSpy.mockRestore();
681
- clearSpy.mockRestore();
682
- }
683
- });
684
- });
685
-
686
- describe("WebSearch — ctx.signal cancellation wiring", () => {
687
- test("an already-aborted ctx.signal still returns the refusal (env unset)", async () => {
688
- const result = await webSearch.execute(
689
- { query: "anything" },
690
- { signal: AbortSignal.abort(new Error("pre-aborted")) },
691
- );
692
- if (typeof result !== "string") throw new Error("expected string result");
693
- expect(result).toContain("WebSearch unavailable");
694
- });
695
-
696
- test("a later ctx.signal abort fires the once-listener", async () => {
697
- const ctrl = new AbortController();
698
- const result = await webSearch.execute({ query: "anything" }, { signal: ctrl.signal });
699
- ctrl.abort(new Error("late"));
700
- if (typeof result !== "string") throw new Error("expected string result");
701
- expect(result).toContain("WebSearch unavailable");
702
- });
703
-
704
- test("the search-timeout setTimeout callback aborts the controller when fired", async () => {
705
- const captured: Array<() => void> = [];
706
- const setSpy = spyOn(globalThis, "setTimeout").mockImplementation(((fn: () => void) => {
707
- captured.push(fn);
708
- return 0 as unknown as ReturnType<typeof setTimeout>;
709
- }) as typeof setTimeout);
710
- const clearSpy = spyOn(globalThis, "clearTimeout").mockImplementation(
711
- (() => {}) as typeof clearTimeout,
712
- );
713
- try {
714
- const result = await webSearch.execute({ query: "anything" });
715
- if (typeof result !== "string") throw new Error("expected string result");
716
- expect(result).toContain("WebSearch unavailable");
717
- for (const fn of captured) fn();
718
- } finally {
719
- setSpy.mockRestore();
720
- clearSpy.mockRestore();
721
- }
722
- });
723
- });
724
-
725
- describe("WebFetch — production default fetch/dns wrappers", () => {
726
- test("defaultRawFetch delegates to globalThis.fetch when not stubbed", async () => {
727
- // Reset rawFetch to its production default, then mock the global fetch so
728
- // the default wrapper's body (globalThis.fetch(req)) runs with no network.
729
- const fetchSpy = spyOn(globalThis, "fetch").mockImplementation(
730
- (async () =>
731
- new Response("from global fetch", {
732
- status: 200,
733
- headers: { "content-type": "text/plain" },
734
- })) as unknown as typeof globalThis.fetch,
735
- );
736
- try {
737
- _setRawFetch(undefined); // rawFetch === defaultRawFetch now
738
- const result = await webFetch.execute({ url: "https://example.com/" });
739
- if (typeof result !== "string") throw new Error("expected string result");
740
- expect(result).toContain("from global fetch");
741
- expect(fetchSpy).toHaveBeenCalled();
742
- } finally {
743
- fetchSpy.mockRestore();
744
- _setRawFetch(undefined);
745
- }
746
- });
747
-
748
- test("defaultDnsLookup delegates to node:dns/promises lookup when not stubbed", async () => {
749
- // Mock the dns module so the production resolver wrapper body runs offline.
750
- const lookupMock = mock(async () => ({ address: "203.0.113.7", family: 4 }));
751
- await mock.module("node:dns/promises", () => ({ lookup: lookupMock }));
752
- const fetchSpy = spyOn(globalThis, "fetch").mockImplementation(
753
- (async () =>
754
- new Response("ok", {
755
- status: 200,
756
- headers: { "content-type": "text/plain" },
757
- })) as unknown as typeof globalThis.fetch,
758
- );
759
- try {
760
- _setDnsLookup(undefined); // dnsLookupFn === defaultDnsLookup now
761
- _setRawFetch(undefined);
762
- const result = await webFetch.execute({ url: "https://public.example/" });
763
- if (typeof result !== "string") throw new Error("expected string result");
764
- expect(result).toContain("ok");
765
- expect(lookupMock).toHaveBeenCalled();
766
- } finally {
767
- fetchSpy.mockRestore();
768
- // Restore the real module so later suites/files are unaffected.
769
- mock.restore();
770
- _setDnsLookup(undefined);
771
- _setRawFetch(undefined);
772
- }
773
- });
774
- });