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