From 2ff0f1325a6ebc419eb9a0103f735d666c4bf355 Mon Sep 17 00:00:00 2001 From: Oskar Stark Date: Mon, 24 Nov 2025 14:36:34 +0100 Subject: [PATCH] [Agent][YouTubeTranscriber] Add test case --- src/agent/composer.json | 2 + .../tests/Fixtures/Tool/youtube-captions.json | 35 ++++++++++++++++++ .../Fixtures/Tool/youtube-transcript.xml | 6 +++ .../Fixtures/Tool/youtube-video-page.html | 10 +++++ .../Toolbox/Tool/YouTubeTranscriberTest.php | 37 +++++++++++++++++++ 5 files changed, 90 insertions(+) create mode 100644 src/agent/tests/Fixtures/Tool/youtube-captions.json create mode 100644 src/agent/tests/Fixtures/Tool/youtube-transcript.xml create mode 100644 src/agent/tests/Fixtures/Tool/youtube-video-page.html create mode 100644 src/agent/tests/Toolbox/Tool/YouTubeTranscriberTest.php diff --git a/src/agent/composer.json b/src/agent/composer.json index 3fc57d34a..823cb164d 100644 --- a/src/agent/composer.json +++ b/src/agent/composer.json @@ -34,6 +34,8 @@ }, "require-dev": { "mrmysql/youtube-transcript": "^0.0.5", + "nyholm/psr7": "^1.8", + "php-http/discovery": "^1.19", "phpstan/phpstan": "^2.0", "phpstan/phpstan-strict-rules": "^2.0", "phpunit/phpunit": "^11.5.13", diff --git a/src/agent/tests/Fixtures/Tool/youtube-captions.json b/src/agent/tests/Fixtures/Tool/youtube-captions.json new file mode 100644 index 000000000..f81de66c9 --- /dev/null +++ b/src/agent/tests/Fixtures/Tool/youtube-captions.json @@ -0,0 +1,35 @@ +{ + "playabilityStatus": { + "status": "OK" + }, + "captions": { + "playerCaptionsTracklistRenderer": { + "captionTracks": [ + { + "baseUrl": "https://www.youtube.com/api/timedtext?v=dQw4w9WgXcQ", + "name": { + "runs": [ + { + "text": "English" + } + ] + }, + "languageCode": "en", + "isTranslatable": true + } + ], + "translationLanguages": [ + { + "languageName": { + "runs": [ + { + "text": "Spanish" + } + ] + }, + "languageCode": "es" + } + ] + } + } +} diff --git a/src/agent/tests/Fixtures/Tool/youtube-transcript.xml b/src/agent/tests/Fixtures/Tool/youtube-transcript.xml new file mode 100644 index 000000000..f3048d434 --- /dev/null +++ b/src/agent/tests/Fixtures/Tool/youtube-transcript.xml @@ -0,0 +1,6 @@ + + + Hello and welcome to this video + Today we will learn about PHP + Thank you for watching + diff --git a/src/agent/tests/Fixtures/Tool/youtube-video-page.html b/src/agent/tests/Fixtures/Tool/youtube-video-page.html new file mode 100644 index 000000000..7474944c6 --- /dev/null +++ b/src/agent/tests/Fixtures/Tool/youtube-video-page.html @@ -0,0 +1,10 @@ + + + + + + + + diff --git a/src/agent/tests/Toolbox/Tool/YouTubeTranscriberTest.php b/src/agent/tests/Toolbox/Tool/YouTubeTranscriberTest.php new file mode 100644 index 000000000..95201cd32 --- /dev/null +++ b/src/agent/tests/Toolbox/Tool/YouTubeTranscriberTest.php @@ -0,0 +1,37 @@ + + * + * 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\YouTubeTranscriber; +use Symfony\Component\HttpClient\MockHttpClient; +use Symfony\Component\HttpClient\Response\MockResponse; + +final class YouTubeTranscriberTest extends TestCase +{ + public function testInvoke() + { + $httpClient = new MockHttpClient([ + MockResponse::fromFile(__DIR__.'/../../Fixtures/Tool/youtube-video-page.html'), + MockResponse::fromFile(__DIR__.'/../../Fixtures/Tool/youtube-captions.json'), + MockResponse::fromFile(__DIR__.'/../../Fixtures/Tool/youtube-transcript.xml'), + ]); + + $transcriber = new YouTubeTranscriber($httpClient); + + $result = $transcriber('dQw4w9WgXcQ'); + + $this->assertStringContainsString('Hello and welcome to this video', $result); + $this->assertStringContainsString('Today we will learn about PHP', $result); + $this->assertStringContainsString('Thank you for watching', $result); + } +}