diff --git a/src/agent/tests/Fixtures/Tool/scraper-page.html b/src/agent/tests/Fixtures/Tool/scraper-page.html new file mode 100644 index 000000000..84e85686f --- /dev/null +++ b/src/agent/tests/Fixtures/Tool/scraper-page.html @@ -0,0 +1,12 @@ + + +
+ +This is some visible text content.
+More content here with useful information.
+ + diff --git a/src/agent/tests/Toolbox/Tool/ScraperTest.php b/src/agent/tests/Toolbox/Tool/ScraperTest.php new file mode 100644 index 000000000..d45da4ea1 --- /dev/null +++ b/src/agent/tests/Toolbox/Tool/ScraperTest.php @@ -0,0 +1,54 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\AI\Agent\Tests\Toolbox\Tool; + +use PHPUnit\Framework\TestCase; +use Symfony\AI\Agent\Toolbox\Tool\Scraper; +use Symfony\Component\HttpClient\MockHttpClient; +use Symfony\Component\HttpClient\Response\MockResponse; + +final class ScraperTest extends TestCase +{ + public function testInvoke() + { + $htmlContent = file_get_contents(__DIR__.'/../../Fixtures/Tool/scraper-page.html'); + $response = new MockResponse($htmlContent); + $httpClient = new MockHttpClient($response); + + $scraper = new Scraper($httpClient); + + $result = $scraper('https://example.com'); + + $this->assertArrayHasKey('title', $result); + $this->assertArrayHasKey('content', $result); + $this->assertSame('Example Page Title', $result['title']); + $this->assertStringContainsString('Welcome to Example Page', $result['content']); + $this->assertStringContainsString('This is some visible text content.', $result['content']); + } + + public function testSourceIsAdded() + { + $htmlContent = file_get_contents(__DIR__.'/../../Fixtures/Tool/scraper-page.html'); + $response = new MockResponse($htmlContent); + $httpClient = new MockHttpClient($response); + + $scraper = new Scraper($httpClient); + + $scraper('https://example.com'); + + $sources = $scraper->getSourceMap()->getSources(); + $this->assertCount(1, $sources); + $this->assertSame('Example Page Title', $sources[0]->getName()); + $this->assertSame('https://example.com', $sources[0]->getReference()); + $this->assertStringContainsString('Welcome to Example Page', $sources[0]->getContent()); + } +}