Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { perplexitySearchTool } from '../src/search/tool'
3import {
4 firstFetchBody,
5 firstFetchCall,
6 firstFetchHeaders,
7 mockFetch,
8} from './test-utils'
9
10const INTEGRATION_HEADER = 'X-Pplx-Integration'
11
12const unusedContext = {
13 emitCustomEvent: () => {},
14}
15
16describe('perplexitySearchTool', () => {
17 beforeEach(() => {
18 vi.stubEnv('PERPLEXITY_API_KEY', 'test-key')
19 vi.stubEnv('PPLX_API_KEY', '')
20 })
21
22 afterEach(() => {
23 vi.unstubAllEnvs()
24 vi.restoreAllMocks()
25 })
26
27 it('exposes a sensible default name, description, and schema', () => {
28 const tool = perplexitySearchTool({
29 apiKey: 'k',
30 fetch: mockFetch({ results: [] }),
31 })
32 expect(tool.name).toBe('perplexity_search')
33 expect(tool.description).toMatch(/Perplexity Search API/i)
34 expect(tool.description.toLowerCase()).not.toContain('sonar')
35 expect(tool.inputSchema).toBeDefined()
36 expect(tool.outputSchema).toBeDefined()
37 })
38
39 it('executes the server tool against the mocked fetch', async () => {
40 const fetchMock = mockFetch({
41 results: [
42 {
43 title: 'A',
44 url: 'https://a.test',
45 snippet: 'snip',
46 date: '2025-03-01',
47 last_updated: '2025-03-02',
48 },
49 { title: 'B', url: 'https://b.test', snippet: 'snip2' },
50 ],
51 })
52
53 const tool = perplexitySearchTool({
54 apiKey: 'k',
55 fetch: fetchMock,
56 defaultMaxResults: 7,
57 })
58
59 expect(typeof tool.execute).toBe('function')
60 const out = await tool.execute!({ query: 'foo' }, unusedContext)
61 expect(out).toEqual({
62 results: [
63 {
64 title: 'A',
65 url: 'https://a.test',
66 snippet: 'snip',
67 date: '2025-03-01',
68 last_updated: '2025-03-02',
69 },
70 { title: 'B', url: 'https://b.test', snippet: 'snip2' },
71 ],
72 })
73
74 expect(firstFetchBody(fetchMock).max_results).toBe(7)
75 expect(firstFetchBody(fetchMock).query).toBe('foo')
76 expect(firstFetchHeaders(fetchMock)[INTEGRATION_HEADER]).toMatch(
77 /^tanstack\//,
78 )
79 })
80
81 it('lets the model max_results override defaultMaxResults', async () => {
82 const fetchMock = mockFetch({ results: [] })
83 const tool = perplexitySearchTool({
84 apiKey: 'k',
85 fetch: fetchMock,
86 defaultMaxResults: 7,
87 })
88
89 await tool.execute!({ query: 'q', max_results: 3 }, unusedContext)
90 expect(firstFetchBody(fetchMock).max_results).toBe(3)
91 })
92
93 it('passes through filter args from the model', async () => {
94 const fetchMock = mockFetch({ results: [] })
95 const tool = perplexitySearchTool({
96 apiKey: 'k',
97 fetch: fetchMock,
98 })
99
100 await tool.execute!(
101 {
102 query: 'q',
103 max_results: 2,
104 search_domain_filter: ['arxiv.org'],
105 search_recency_filter: 'week',
106 search_after_date_filter: '1/1/2026',
107 },
108 unusedContext,
109 )
110
111 expect(firstFetchBody(fetchMock)).toEqual({
112 query: 'q',
113 max_results: 2,
114 search_domain_filter: ['arxiv.org'],
115 search_recency_filter: 'week',
116 search_after_date_filter: '1/1/2026',
117 })
118 })
119
120 it('forwards abortSignal to fetch', async () => {
121 const abortController = new AbortController()
122 const fetchMock = mockFetch({ results: [] })
123 const tool = perplexitySearchTool({
124 apiKey: 'k',
125 fetch: fetchMock,
126 })
127
128 await tool.execute!(
129 { query: 'q' },
130 { ...unusedContext, abortSignal: abortController.signal },
131 )
132 expect(firstFetchCall(fetchMock).init.signal).toBe(abortController.signal)
133 })
134
135 it('throws when defaultMaxResults is outside the allowed range', () => {
136 expect(() => perplexitySearchTool({ defaultMaxResults: 0 })).toThrow(
137 /integer between 1 and 20/i,
138 )
139 expect(() => perplexitySearchTool({ defaultMaxResults: 21 })).toThrow(
140 /integer between 1 and 20/i,
141 )
142 expect(() => perplexitySearchTool({ defaultMaxResults: 1.5 })).toThrow(
143 /integer between 1 and 20/i,
144 )
145 })
146
147 it('honors custom name and description overrides', () => {
148 const tool = perplexitySearchTool({
149 apiKey: 'k',
150 fetch: mockFetch({ results: [] }),
151 name: 'web_search',
152 description: 'Custom desc.',
153 })
154 expect(tool.name).toBe('web_search')
155 expect(tool.description).toBe('Custom desc.')
156 })
157})