From 7a35937379aacde737f3a89f11ac33b574a06e25 Mon Sep 17 00:00:00 2001 From: Oskar Stark Date: Mon, 24 Nov 2025 13:34:53 +0100 Subject: [PATCH] [Agent][Scraper] Add test case --- .../tests/Fixtures/Tool/scraper-page.html | 12 +++++ src/agent/tests/Toolbox/Tool/ScraperTest.php | 54 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 src/agent/tests/Fixtures/Tool/scraper-page.html create mode 100644 src/agent/tests/Toolbox/Tool/ScraperTest.php 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 @@ + + + + + Example Page Title + + +

Welcome to Example Page

+

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()); + } +}