|
| 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 | +} |
0 commit comments