summaryrefslogtreecommitdiffhomepage
path: root/packages/tool-web-search/src/format.test.ts
blob: b98bc02a52134b4635a89d18cf001586df2a2c6e (plain)
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
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");
	});
});