Skip to content

Commit 4adba60

Browse files
committed
Add tests for Tavily tool
1 parent 5396cd2 commit 4adba60

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\AI\Agent\Bridge\Tavily\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\AI\Agent\Bridge\Tavily\Tavily;
16+
use Symfony\Component\HttpClient\MockHttpClient;
17+
use Symfony\Component\HttpClient\Response\JsonMockResponse;
18+
19+
/**
20+
* @author Oskar Stark <[email protected]>
21+
*/
22+
final class TavilyTest extends TestCase
23+
{
24+
public function testSearchReturnsResults()
25+
{
26+
$result = JsonMockResponse::fromFile(__DIR__.'/fixtures/search-results.json');
27+
$httpClient = new MockHttpClient($result);
28+
$tavily = new Tavily($httpClient, 'test-api-key');
29+
30+
$response = $tavily->search('latest AI news');
31+
32+
$this->assertIsString($response);
33+
$this->assertStringContainsString('results', $response);
34+
}
35+
36+
public function testSearchPassesCorrectParameters()
37+
{
38+
$result = JsonMockResponse::fromFile(__DIR__.'/fixtures/search-results.json');
39+
$httpClient = new MockHttpClient($result);
40+
$tavily = new Tavily($httpClient, 'test-api-key', ['include_images' => true]);
41+
42+
$tavily->search('test query');
43+
44+
$requestUrl = $result->getRequestUrl();
45+
$this->assertSame('https://api.tavily.com/search', $requestUrl);
46+
47+
$requestOptions = $result->getRequestOptions();
48+
$this->assertArrayHasKey('body', $requestOptions);
49+
$body = json_decode($requestOptions['body'], true);
50+
$this->assertSame('test query', $body['query']);
51+
$this->assertSame('test-api-key', $body['api_key']);
52+
$this->assertTrue($body['include_images']);
53+
}
54+
55+
public function testSearchAddsSourcesFromResults()
56+
{
57+
$result = JsonMockResponse::fromFile(__DIR__.'/fixtures/search-results.json');
58+
$httpClient = new MockHttpClient($result);
59+
$tavily = new Tavily($httpClient, 'test-api-key');
60+
61+
$tavily->search('test query');
62+
63+
$sources = $tavily->getSourceMap()->getSources();
64+
$this->assertCount(2, $sources);
65+
$this->assertSame('AI breakthrough announced', $sources[0]->getName());
66+
$this->assertSame('https://example.com/ai-news', $sources[0]->getReference());
67+
}
68+
69+
public function testExtractReturnsResults()
70+
{
71+
$result = JsonMockResponse::fromFile(__DIR__.'/fixtures/extract-results.json');
72+
$httpClient = new MockHttpClient($result);
73+
$tavily = new Tavily($httpClient, 'test-api-key');
74+
75+
$response = $tavily->extract(['https://example.com/article']);
76+
77+
$this->assertIsString($response);
78+
$this->assertStringContainsString('results', $response);
79+
}
80+
81+
public function testExtractPassesCorrectParameters()
82+
{
83+
$result = JsonMockResponse::fromFile(__DIR__.'/fixtures/extract-results.json');
84+
$httpClient = new MockHttpClient($result);
85+
$tavily = new Tavily($httpClient, 'test-api-key');
86+
87+
$urls = ['https://example.com/article1', 'https://example.com/article2'];
88+
$tavily->extract($urls);
89+
90+
$requestUrl = $result->getRequestUrl();
91+
$this->assertSame('https://api.tavily.com/extract', $requestUrl);
92+
93+
$requestOptions = $result->getRequestOptions();
94+
$this->assertArrayHasKey('body', $requestOptions);
95+
$body = json_decode($requestOptions['body'], true);
96+
$this->assertSame($urls, $body['urls']);
97+
$this->assertSame('test-api-key', $body['api_key']);
98+
}
99+
100+
public function testExtractAddsSourcesFromResults()
101+
{
102+
$result = JsonMockResponse::fromFile(__DIR__.'/fixtures/extract-results.json');
103+
$httpClient = new MockHttpClient($result);
104+
$tavily = new Tavily($httpClient, 'test-api-key');
105+
106+
$tavily->extract(['https://example.com/article']);
107+
108+
$sources = $tavily->getSourceMap()->getSources();
109+
$this->assertCount(1, $sources);
110+
$this->assertSame('Example Article', $sources[0]->getName());
111+
$this->assertSame('https://example.com/article', $sources[0]->getReference());
112+
}
113+
114+
public function testHandlesEmptySearchResults()
115+
{
116+
$httpClient = new MockHttpClient(new JsonMockResponse(['results' => []]));
117+
$tavily = new Tavily($httpClient, 'test-api-key');
118+
119+
$response = $tavily->search('query with no results');
120+
121+
$this->assertIsString($response);
122+
$sources = $tavily->getSourceMap()->getSources();
123+
$this->assertEmpty($sources);
124+
}
125+
126+
public function testHandlesEmptyExtractResults()
127+
{
128+
$httpClient = new MockHttpClient(new JsonMockResponse(['results' => []]));
129+
$tavily = new Tavily($httpClient, 'test-api-key');
130+
131+
$response = $tavily->extract(['https://nonexistent.com']);
132+
133+
$this->assertIsString($response);
134+
$sources = $tavily->getSourceMap()->getSources();
135+
$this->assertEmpty($sources);
136+
}
137+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"results": [
3+
{
4+
"title": "Example Article",
5+
"url": "https://example.com/article",
6+
"raw_content": "This is the full content of the article that was extracted from the webpage. It contains detailed information about the topic.",
7+
"score": 1.0
8+
}
9+
],
10+
"response_time": 0.89
11+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"query": "latest AI news",
3+
"results": [
4+
{
5+
"title": "AI breakthrough announced",
6+
"url": "https://example.com/ai-news",
7+
"content": "Major AI company announces breakthrough in language models.",
8+
"raw_content": "Major AI company announces breakthrough in language models with new architecture that improves efficiency.",
9+
"score": 0.95
10+
},
11+
{
12+
"title": "Machine Learning Advances",
13+
"url": "https://example.com/ml-advances",
14+
"content": "New research in machine learning shows promise.",
15+
"raw_content": "New research in machine learning shows promise for solving complex problems in various domains.",
16+
"score": 0.87
17+
}
18+
],
19+
"images": [],
20+
"response_time": 1.23
21+
}

0 commit comments

Comments
 (0)