diff options
| author | Adam Malczewski <[email protected]> | 2026-06-21 13:11:29 +0900 |
|---|---|---|
| committer | Adam Malczewski <[email protected]> | 2026-06-21 13:11:29 +0900 |
| commit | 8a4a624d16422467a8e85434c674bb591877e8ea (patch) | |
| tree | 54052da00bbc580742913e5c031b7cc1b160db19 /packages/tool-web-search/src/format.test.ts | |
| parent | d23de3254374d4d63c8e15c6ab9311c3c6f4da5b (diff) | |
| download | dispatch-8a4a624d16422467a8e85434c674bb591877e8ea.tar.gz dispatch-8a4a624d16422467a8e85434c674bb591877e8ea.zip | |
feat(tool-web-search): Firecrawl-backed web search tool
New standard tool extension with one tool web_search supporting 4 modes
(search, scrape, crawl, map) against a self-hosted Firecrawl instance.
Pure core: validateArgs (discriminated union by mode) + format* functions
+ truncateOutput. Injected edge: FirecrawlClient (injectable fetchFn/sleep/now,
AbortSignal.any for per-request timeout + caller cancellation). concurrencySafe
true, capabilities network. 38 tests, zero vi.mock.
Live-verified: umans-glm-5.2 called web_search → real Firecrawl results (also
the first live Umans API call).
Diffstat (limited to 'packages/tool-web-search/src/format.test.ts')
| -rw-r--r-- | packages/tool-web-search/src/format.test.ts | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/packages/tool-web-search/src/format.test.ts b/packages/tool-web-search/src/format.test.ts new file mode 100644 index 0000000..b98bc02 --- /dev/null +++ b/packages/tool-web-search/src/format.test.ts @@ -0,0 +1,87 @@ +import { describe, expect, it } from "vitest"; +import { + formatCrawlResults, + formatMapResults, + formatScrapeResult, + formatSearchResults, + truncateOutput, +} from "./format.js"; + +describe("formatSearchResults", () => { + it("formats title + url + description + optional markdown", () => { + const out = formatSearchResults([ + { title: "T1", url: "http://a", description: "desc", markdown: "md-body" }, + ]); + expect(out).toBe("### T1\nhttp://a\n\ndesc\n\nmd-body"); + }); + + it("joins multiple results with ---", () => { + const out = formatSearchResults([ + { title: "T1", url: "http://a", description: "d1" }, + { title: "T2", url: "http://b", description: "d2" }, + ]); + expect(out).toBe("### T1\nhttp://a\n\nd1\n\n---\n\n### T2\nhttp://b\n\nd2"); + }); + + it("empty data returns 'No results found.'", () => { + expect(formatSearchResults([])).toBe("No results found."); + expect(formatSearchResults(null)).toBe("No results found."); + expect(formatSearchResults(undefined)).toBe("No results found."); + }); +}); + +describe("formatScrapeResult", () => { + it("formats title + markdown", () => { + const out = formatScrapeResult({ + data: { markdown: "body", metadata: { title: "Title" } }, + }); + expect(out).toBe("# Title\n\nbody"); + }); + + it("omits title header when absent", () => { + const out = formatScrapeResult({ data: { markdown: "body" } }); + expect(out).toBe("body"); + }); +}); + +describe("formatCrawlResults", () => { + it("formats multiple pages", () => { + const out = formatCrawlResults([ + { markdown: "p1", metadata: { title: "P1", sourceURL: "http://p1" } }, + { markdown: "p2", metadata: { title: "P2", url: "http://p2" } }, + ]); + expect(out).toBe("## P1\nhttp://p1\n\np1\n\n---\n\n## P2\nhttp://p2\n\np2"); + }); + + it("empty data returns 'No pages crawled.'", () => { + expect(formatCrawlResults([])).toBe("No pages crawled."); + expect(formatCrawlResults(null)).toBe("No pages crawled."); + }); +}); + +describe("formatMapResults", () => { + it("formats links as bullet list", () => { + const out = formatMapResults(["http://a", "http://b"]); + expect(out).toBe("- http://a\n- http://b"); + }); + + it("empty links returns 'No links found.'", () => { + expect(formatMapResults([])).toBe("No links found."); + expect(formatMapResults(null)).toBe("No links found."); + }); +}); + +describe("truncateOutput", () => { + it("truncates with notice when over cap", () => { + const output = "a".repeat(100); + const result = truncateOutput(output, 50); + expect(result).toContain("a".repeat(50)); + expect(result).toContain("[Output truncated: exceeded 50 characters]"); + expect(result.length).toBeLessThan(output.length + 100); + }); + + it("returns as-is when under cap", () => { + expect(truncateOutput("short", 100)).toBe("short"); + expect(truncateOutput("exact", 5)).toBe("exact"); + }); +}); |
