|
16 | 16 | use PHPUnit\Framework\Attributes\UsesClass;
|
17 | 17 | use PHPUnit\Framework\TestCase;
|
18 | 18 | use Symfony\AI\Platform\Bridge\Cerebras\Model;
|
19 |
| -use Symfony\AI\Platform\Bridge\Cerebras\ModelClient; |
20 | 19 | use Symfony\AI\Platform\Bridge\Cerebras\ResultConverter;
|
21 |
| -use Symfony\Component\HttpClient\MockHttpClient; |
| 20 | +use Symfony\AI\Platform\Exception\AuthenticationException; |
| 21 | +use Symfony\AI\Platform\Exception\NotFoundException; |
| 22 | +use Symfony\AI\Platform\Exception\RuntimeException; |
| 23 | +use Symfony\AI\Platform\Exception\ServiceUnavailableException; |
| 24 | +use Symfony\AI\Platform\Result\RawHttpResult; |
| 25 | +use Symfony\AI\Platform\Result\TextResult; |
| 26 | +use Symfony\Contracts\HttpClient\ResponseInterface; |
22 | 27 |
|
23 | 28 | /**
|
24 | 29 | * @author Junaid Farooq <[email protected]>
|
25 | 30 | */
|
26 | 31 | #[CoversClass(ResultConverter::class)]
|
27 | 32 | #[UsesClass(Model::class)]
|
| 33 | +#[UsesClass(TextResult::class)] |
28 | 34 | #[Small]
|
29 | 35 | class ResultConverterTest extends TestCase
|
30 | 36 | {
|
31 |
| - public function testItSupportsTheCorrectModel() |
| 37 | + public function testSupportsCorrectModel() |
32 | 38 | {
|
33 |
| - $client = new ModelClient(new MockHttpClient(), 'csk-1234567890abcdef'); |
| 39 | + $converter = new ResultConverter(); |
| 40 | + $model = new Model(Model::GPT_OSS_120B); |
34 | 41 |
|
35 |
| - $this->assertTrue($client->supports(new Model(Model::GPT_OSS_120B))); |
| 42 | + $this->assertTrue($converter->supports($model)); |
| 43 | + } |
| 44 | + |
| 45 | + public function testConvertSuccessfulTextResult() |
| 46 | + { |
| 47 | + $converter = new ResultConverter(); |
| 48 | + $httpResponse = self::createMock(ResponseInterface::class); |
| 49 | + $httpResponse->method('getStatusCode')->willReturn(200); |
| 50 | + $httpResponse->method('toArray')->willReturn([ |
| 51 | + 'choices' => [ |
| 52 | + [ |
| 53 | + 'message' => [ |
| 54 | + 'content' => 'Hello from Cerebras!', |
| 55 | + ], |
| 56 | + ], |
| 57 | + ], |
| 58 | + ]); |
| 59 | + |
| 60 | + $result = $converter->convert(new RawHttpResult($httpResponse)); |
| 61 | + |
| 62 | + $this->assertInstanceOf(TextResult::class, $result); |
| 63 | + $this->assertSame('Hello from Cerebras!', $result->getContent()); |
| 64 | + } |
| 65 | + |
| 66 | + public function testThrowsAuthenticationException() |
| 67 | + { |
| 68 | + $converter = new ResultConverter(); |
| 69 | + $httpResponse = self::createMock(ResponseInterface::class); |
| 70 | + $httpResponse->method('getStatusCode')->willReturn(401); |
| 71 | + $httpResponse->method('getContent') |
| 72 | + ->with(false) |
| 73 | + ->willReturn('{"error": {"message": "Invalid API key"}}'); |
| 74 | + |
| 75 | + $this->expectException(AuthenticationException::class); |
| 76 | + $this->expectExceptionMessage('Invalid API key'); |
| 77 | + |
| 78 | + $converter->convert(new RawHttpResult($httpResponse)); |
| 79 | + } |
| 80 | + |
| 81 | + public function testThrowsNotFoundException() |
| 82 | + { |
| 83 | + $converter = new ResultConverter(); |
| 84 | + $httpResponse = self::createMock(ResponseInterface::class); |
| 85 | + $httpResponse->method('getStatusCode')->willReturn(404); |
| 86 | + $httpResponse->method('getContent') |
| 87 | + ->with(false) |
| 88 | + ->willReturn('{"error": {"message": "Model not found"}}'); |
| 89 | + |
| 90 | + $this->expectException(NotFoundException::class); |
| 91 | + $this->expectExceptionMessage('Model not found'); |
| 92 | + |
| 93 | + $converter->convert(new RawHttpResult($httpResponse)); |
| 94 | + } |
| 95 | + |
| 96 | + public function testThrowsServiceUnavailableException() |
| 97 | + { |
| 98 | + $converter = new ResultConverter(); |
| 99 | + $httpResponse = self::createMock(ResponseInterface::class); |
| 100 | + $httpResponse->method('getStatusCode')->willReturn(503); |
| 101 | + $httpResponse->method('getContent') |
| 102 | + ->with(false) |
| 103 | + ->willReturn('{"error": {"message": "Service temporarily unavailable"}}'); |
| 104 | + |
| 105 | + $this->expectException(ServiceUnavailableException::class); |
| 106 | + $this->expectExceptionMessage('Service temporarily unavailable'); |
| 107 | + |
| 108 | + $converter->convert(new RawHttpResult($httpResponse)); |
| 109 | + } |
| 110 | + |
| 111 | + public function testThrowsRuntimeExceptionWhenNoContent() |
| 112 | + { |
| 113 | + $converter = new ResultConverter(); |
| 114 | + $httpResponse = self::createMock(ResponseInterface::class); |
| 115 | + $httpResponse->method('getStatusCode')->willReturn(200); |
| 116 | + $httpResponse->method('toArray')->willReturn([ |
| 117 | + 'choices' => [ |
| 118 | + [ |
| 119 | + 'message' => [], |
| 120 | + ], |
| 121 | + ], |
| 122 | + ]); |
| 123 | + |
| 124 | + $this->expectException(RuntimeException::class); |
| 125 | + $this->expectExceptionMessage('Response does not contain output.'); |
| 126 | + |
| 127 | + $converter->convert(new RawHttpResult($httpResponse)); |
| 128 | + } |
| 129 | + |
| 130 | + public function testThrowsCerebrasApiError() |
| 131 | + { |
| 132 | + $converter = new ResultConverter(); |
| 133 | + $httpResponse = self::createMock(ResponseInterface::class); |
| 134 | + $httpResponse->method('getStatusCode')->willReturn(200); |
| 135 | + $httpResponse->method('toArray')->willReturn([ |
| 136 | + 'type' => 'api_error', |
| 137 | + 'message' => 'Something went wrong with the API', |
| 138 | + ]); |
| 139 | + |
| 140 | + $this->expectException(RuntimeException::class); |
| 141 | + $this->expectExceptionMessage('Cerebras API error: "Something went wrong with the API"'); |
| 142 | + |
| 143 | + $converter->convert(new RawHttpResult($httpResponse)); |
| 144 | + } |
| 145 | + |
| 146 | + public function testThrowsGenericRuntimeExceptionForMissingChoices() |
| 147 | + { |
| 148 | + $converter = new ResultConverter(); |
| 149 | + $httpResponse = self::createMock(ResponseInterface::class); |
| 150 | + $httpResponse->method('getStatusCode')->willReturn(200); |
| 151 | + $httpResponse->method('toArray')->willReturn([]); |
| 152 | + |
| 153 | + $this->expectException(RuntimeException::class); |
| 154 | + $this->expectExceptionMessage('Response does not contain output.'); |
| 155 | + |
| 156 | + $converter->convert(new RawHttpResult($httpResponse)); |
36 | 157 | }
|
37 | 158 | }
|
0 commit comments