|
| 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\AIBundle\Tests\Profiler; |
| 13 | + |
| 14 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 15 | +use PHPUnit\Framework\Attributes\UsesClass; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Symfony\AI\Agent\Toolbox\ToolboxInterface; |
| 18 | +use Symfony\AI\AIBundle\Profiler\DataCollector; |
| 19 | +use Symfony\AI\AIBundle\Profiler\TraceablePlatform; |
| 20 | +use Symfony\AI\Platform\Message\Content\Text; |
| 21 | +use Symfony\AI\Platform\Message\Message; |
| 22 | +use Symfony\AI\Platform\Message\MessageBag; |
| 23 | +use Symfony\AI\Platform\Model; |
| 24 | +use Symfony\AI\Platform\PlatformInterface; |
| 25 | +use Symfony\AI\Platform\Result\RawResultInterface; |
| 26 | +use Symfony\AI\Platform\Result\ResultPromise; |
| 27 | +use Symfony\AI\Platform\Result\StreamResult; |
| 28 | +use Symfony\AI\Platform\Result\TextResult; |
| 29 | + |
| 30 | +#[CoversClass(DataCollector::class)] |
| 31 | +#[UsesClass(TraceablePlatform::class)] |
| 32 | +#[UsesClass(ResultPromise::class)] |
| 33 | +class DataCollectorTest extends TestCase |
| 34 | +{ |
| 35 | + public function testCollectsDataForNonStreamingResponse() |
| 36 | + { |
| 37 | + $platform = $this->createMock(PlatformInterface::class); |
| 38 | + $traceablePlatform = new TraceablePlatform($platform); |
| 39 | + $messageBag = new MessageBag(Message::ofUser(new Text('Hello'))); |
| 40 | + $result = new TextResult('Assistant response'); |
| 41 | + |
| 42 | + $platform->method('invoke')->willReturn(new ResultPromise(static fn () => $result, $this->createStub(RawResultInterface::class))); |
| 43 | + |
| 44 | + $result = $traceablePlatform->invoke($this->createStub(Model::class), $messageBag, ['stream' => false]); |
| 45 | + $this->assertSame('Assistant response', $result->asText()); |
| 46 | + |
| 47 | + $dataCollector = new DataCollector([$traceablePlatform], $this->createStub(ToolboxInterface::class), []); |
| 48 | + $dataCollector->lateCollect(); |
| 49 | + |
| 50 | + $this->assertCount(1, $dataCollector->getPlatformCalls()); |
| 51 | + $this->assertSame('Assistant response', $dataCollector->getPlatformCalls()[0]['result']); |
| 52 | + } |
| 53 | + |
| 54 | + public function testCollectsDataForStreamingResponse() |
| 55 | + { |
| 56 | + $platform = $this->createMock(PlatformInterface::class); |
| 57 | + $traceablePlatform = new TraceablePlatform($platform); |
| 58 | + $messageBag = new MessageBag(Message::ofUser(new Text('Hello'))); |
| 59 | + $result = new StreamResult( |
| 60 | + (function () { |
| 61 | + yield 'Assistant '; |
| 62 | + yield 'response'; |
| 63 | + })(), |
| 64 | + ); |
| 65 | + |
| 66 | + $platform->method('invoke')->willReturn(new ResultPromise(static fn () => $result, $this->createStub(RawResultInterface::class))); |
| 67 | + |
| 68 | + $result = $traceablePlatform->invoke($this->createStub(Model::class), $messageBag, ['stream' => true]); |
| 69 | + $this->assertSame('Assistant response', implode('', iterator_to_array($result->asStream()))); |
| 70 | + |
| 71 | + $dataCollector = new DataCollector([$traceablePlatform], $this->createStub(ToolboxInterface::class), []); |
| 72 | + $dataCollector->lateCollect(); |
| 73 | + |
| 74 | + $this->assertCount(1, $dataCollector->getPlatformCalls()); |
| 75 | + $this->assertSame('Assistant response', $dataCollector->getPlatformCalls()[0]['result']); |
| 76 | + } |
| 77 | +} |
0 commit comments