|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpLlm\LlmChain\Tests\Bridge\Anthropic; |
| 6 | + |
| 7 | +use PhpLlm\LlmChain\Bridge\Anthropic\ModelHandler; |
| 8 | +use PhpLlm\LlmChain\Model\Response\ToolCallResponse; |
| 9 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use Symfony\Component\HttpClient\MockHttpClient; |
| 12 | +use Symfony\Component\HttpClient\Response\JsonMockResponse; |
| 13 | + |
| 14 | +#[CoversClass(ModelHandler::class)] |
| 15 | +final class ModelHandlerTest extends TestCase |
| 16 | +{ |
| 17 | + public function testConvertThrowsExceptionWhenContentIsToolUseAndLacksText(): void |
| 18 | + { |
| 19 | + $httpClient = new MockHttpClient(new JsonMockResponse([ |
| 20 | + 'content' => [ |
| 21 | + [ |
| 22 | + 'type' => 'tool_use', |
| 23 | + 'id' => 'toolu_01UM4PcTjC1UDiorSXVHSVFM', |
| 24 | + 'name' => 'xxx_tool', |
| 25 | + 'input' => ['action' => 'get_data'], |
| 26 | + ], |
| 27 | + ], |
| 28 | + ])); |
| 29 | + $httpResponse = $httpClient->request('POST', 'https://api.anthropic.com/v1/messages'); |
| 30 | + $handler = new ModelHandler($httpClient, 'test-api-key'); |
| 31 | + |
| 32 | + $response = $handler->convert($httpResponse); |
| 33 | + self::assertInstanceOf(ToolCallResponse::class, $response); |
| 34 | + self::assertCount(1, $response->getContent()); |
| 35 | + self::assertSame('toolu_01UM4PcTjC1UDiorSXVHSVFM', $response->getContent()[0]->id); |
| 36 | + self::assertSame('xxx_tool', $response->getContent()[0]->name); |
| 37 | + self::assertSame(['action' => 'get_data'], $response->getContent()[0]->arguments); |
| 38 | + } |
| 39 | +} |
0 commit comments