Skip to content

Commit 1cea0cd

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

File tree

3 files changed

+165
-0
lines changed

3 files changed

+165
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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->assertStringContainsString('results', $response);
33+
}
34+
35+
public function testSearchPassesCorrectParameters()
36+
{
37+
$result = JsonMockResponse::fromFile(__DIR__.'/fixtures/search-results.json');
38+
$httpClient = new MockHttpClient($result);
39+
$tavily = new Tavily($httpClient, 'test-api-key', ['include_images' => true]);
40+
41+
$tavily->search('test query');
42+
43+
$requestUrl = $result->getRequestUrl();
44+
$this->assertSame('https://api.tavily.com/search', $requestUrl);
45+
46+
$requestOptions = $result->getRequestOptions();
47+
$this->assertArrayHasKey('body', $requestOptions);
48+
$body = json_decode($requestOptions['body'], true);
49+
$this->assertSame('test query', $body['query']);
50+
$this->assertSame('test-api-key', $body['api_key']);
51+
$this->assertTrue($body['include_images']);
52+
}
53+
54+
public function testSearchAddsSourcesFromResults()
55+
{
56+
$result = JsonMockResponse::fromFile(__DIR__.'/fixtures/search-results.json');
57+
$httpClient = new MockHttpClient($result);
58+
$tavily = new Tavily($httpClient, 'test-api-key');
59+
60+
$tavily->search('test query');
61+
62+
$sources = $tavily->getSourceMap()->getSources();
63+
$this->assertCount(2, $sources);
64+
$this->assertSame('AI breakthrough announced', $sources[0]->getName());
65+
$this->assertSame('https://example.com/ai-news', $sources[0]->getReference());
66+
}
67+
68+
public function testExtractReturnsResults()
69+
{
70+
$result = JsonMockResponse::fromFile(__DIR__.'/fixtures/extract-results.json');
71+
$httpClient = new MockHttpClient($result);
72+
$tavily = new Tavily($httpClient, 'test-api-key');
73+
74+
$response = $tavily->extract(['https://example.com/article']);
75+
76+
$this->assertStringContainsString('results', $response);
77+
}
78+
79+
public function testExtractPassesCorrectParameters()
80+
{
81+
$result = JsonMockResponse::fromFile(__DIR__.'/fixtures/extract-results.json');
82+
$httpClient = new MockHttpClient($result);
83+
$tavily = new Tavily($httpClient, 'test-api-key');
84+
85+
$urls = ['https://example.com/article1', 'https://example.com/article2'];
86+
$tavily->extract($urls);
87+
88+
$requestUrl = $result->getRequestUrl();
89+
$this->assertSame('https://api.tavily.com/extract', $requestUrl);
90+
91+
$requestOptions = $result->getRequestOptions();
92+
$this->assertArrayHasKey('body', $requestOptions);
93+
$body = json_decode($requestOptions['body'], true);
94+
$this->assertSame($urls, $body['urls']);
95+
$this->assertSame('test-api-key', $body['api_key']);
96+
}
97+
98+
public function testExtractAddsSourcesFromResults()
99+
{
100+
$result = JsonMockResponse::fromFile(__DIR__.'/fixtures/extract-results.json');
101+
$httpClient = new MockHttpClient($result);
102+
$tavily = new Tavily($httpClient, 'test-api-key');
103+
104+
$tavily->extract(['https://example.com/article']);
105+
106+
$sources = $tavily->getSourceMap()->getSources();
107+
$this->assertCount(1, $sources);
108+
$this->assertSame('Example Article', $sources[0]->getName());
109+
$this->assertSame('https://example.com/article', $sources[0]->getReference());
110+
}
111+
112+
public function testHandlesEmptySearchResults()
113+
{
114+
$httpClient = new MockHttpClient(new JsonMockResponse(['results' => []]));
115+
$tavily = new Tavily($httpClient, 'test-api-key');
116+
117+
$tavily->search('query with no results');
118+
119+
$sources = $tavily->getSourceMap()->getSources();
120+
$this->assertEmpty($sources);
121+
}
122+
123+
public function testHandlesEmptyExtractResults()
124+
{
125+
$httpClient = new MockHttpClient(new JsonMockResponse(['results' => []]));
126+
$tavily = new Tavily($httpClient, 'test-api-key');
127+
128+
$tavily->extract(['https://nonexistent.com']);
129+
130+
$sources = $tavily->getSourceMap()->getSources();
131+
$this->assertEmpty($sources);
132+
}
133+
}
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)