|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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\Platform\Tests\Bridge\Anthropic; |
| 13 | + |
| 14 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 15 | +use PHPUnit\Framework\Attributes\Small; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Symfony\AI\Platform\Bridge\Anthropic\ResultConverter; |
| 18 | +use Symfony\AI\Platform\Exception\RateExceededException; |
| 19 | +use Symfony\AI\Platform\Result\RawHttpResult; |
| 20 | +use Symfony\Component\HttpClient\MockHttpClient; |
| 21 | +use Symfony\Component\HttpClient\Response\MockResponse; |
| 22 | + |
| 23 | +#[CoversClass(ResultConverter::class)] |
| 24 | +#[Small] |
| 25 | +final class ResultConverterRateLimitTest extends TestCase |
| 26 | +{ |
| 27 | + public function testRateLimitExceededThrowsException() |
| 28 | + { |
| 29 | + $httpClient = new MockHttpClient([ |
| 30 | + new MockResponse('{"type":"error","error":{"type":"rate_limit_error","message":"This request would exceed the rate limit for your organization"}}', [ |
| 31 | + 'http_code' => 429, |
| 32 | + 'response_headers' => [ |
| 33 | + 'retry-after' => '60', |
| 34 | + ], |
| 35 | + ]), |
| 36 | + ]); |
| 37 | + |
| 38 | + $httpResponse = $httpClient->request('POST', 'https://api.anthropic.com/v1/messages'); |
| 39 | + $handler = new ResultConverter(); |
| 40 | + |
| 41 | + $this->expectException(RateExceededException::class); |
| 42 | + $this->expectExceptionMessage('Rate limit exceeded'); |
| 43 | + |
| 44 | + try { |
| 45 | + $handler->convert(new RawHttpResult($httpResponse)); |
| 46 | + } catch (RateExceededException $e) { |
| 47 | + $this->assertSame(60.0, $e->retryAfter); |
| 48 | + throw $e; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public function testRateLimitExceededWithoutRetryAfter() |
| 53 | + { |
| 54 | + $httpClient = new MockHttpClient([ |
| 55 | + new MockResponse('{"type":"error","error":{"type":"rate_limit_error","message":"This request would exceed the rate limit for your organization"}}', [ |
| 56 | + 'http_code' => 429, |
| 57 | + ]), |
| 58 | + ]); |
| 59 | + |
| 60 | + $httpResponse = $httpClient->request('POST', 'https://api.anthropic.com/v1/messages'); |
| 61 | + $handler = new ResultConverter(); |
| 62 | + |
| 63 | + $this->expectException(RateExceededException::class); |
| 64 | + $this->expectExceptionMessage('Rate limit exceeded'); |
| 65 | + |
| 66 | + try { |
| 67 | + $handler->convert(new RawHttpResult($httpResponse)); |
| 68 | + } catch (RateExceededException $e) { |
| 69 | + $this->assertNull($e->retryAfter); |
| 70 | + throw $e; |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments