Skip to content

Commit c75a24b

Browse files
committed
minor #969 [Agent][Scraper] Add test case (OskarStark)
This PR was squashed before being merged into the main branch. Discussion ---------- [Agent][Scraper] Add test case | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Docs? | no | Issues | -- | License | MIT Tests the Scraper tool's ability to extract title and content from HTML pages using MockHttpClient, and verifies that sources are properly tracked in the SourceMap. Commits ------- 7a35937 [Agent][Scraper] Add test case
2 parents 78e0ce5 + 7a35937 commit c75a24b

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Example Page Title</title>
6+
</head>
7+
<body>
8+
<h1>Welcome to Example Page</h1>
9+
<p>This is some visible text content.</p>
10+
<p>More content here with useful information.</p>
11+
</body>
12+
</html>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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\Tests\Toolbox\Tool;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\AI\Agent\Toolbox\Tool\Scraper;
16+
use Symfony\Component\HttpClient\MockHttpClient;
17+
use Symfony\Component\HttpClient\Response\MockResponse;
18+
19+
final class ScraperTest extends TestCase
20+
{
21+
public function testInvoke()
22+
{
23+
$htmlContent = file_get_contents(__DIR__.'/../../Fixtures/Tool/scraper-page.html');
24+
$response = new MockResponse($htmlContent);
25+
$httpClient = new MockHttpClient($response);
26+
27+
$scraper = new Scraper($httpClient);
28+
29+
$result = $scraper('https://example.com');
30+
31+
$this->assertArrayHasKey('title', $result);
32+
$this->assertArrayHasKey('content', $result);
33+
$this->assertSame('Example Page Title', $result['title']);
34+
$this->assertStringContainsString('Welcome to Example Page', $result['content']);
35+
$this->assertStringContainsString('This is some visible text content.', $result['content']);
36+
}
37+
38+
public function testSourceIsAdded()
39+
{
40+
$htmlContent = file_get_contents(__DIR__.'/../../Fixtures/Tool/scraper-page.html');
41+
$response = new MockResponse($htmlContent);
42+
$httpClient = new MockHttpClient($response);
43+
44+
$scraper = new Scraper($httpClient);
45+
46+
$scraper('https://example.com');
47+
48+
$sources = $scraper->getSourceMap()->getSources();
49+
$this->assertCount(1, $sources);
50+
$this->assertSame('Example Page Title', $sources[0]->getName());
51+
$this->assertSame('https://example.com', $sources[0]->getReference());
52+
$this->assertStringContainsString('Welcome to Example Page', $sources[0]->getContent());
53+
}
54+
}

0 commit comments

Comments
 (0)