|
| 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 Bridge\Perplexity; |
| 13 | + |
| 14 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 15 | +use PHPUnit\Framework\Attributes\Small; |
| 16 | +use PHPUnit\Framework\Attributes\TestWith; |
| 17 | +use PHPUnit\Framework\Attributes\UsesClass; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | +use Symfony\AI\Platform\Bridge\Perplexity\ModelClient; |
| 20 | +use Symfony\AI\Platform\Bridge\Perplexity\Perplexity; |
| 21 | +use Symfony\AI\Platform\Exception\InvalidArgumentException; |
| 22 | +use Symfony\Component\HttpClient\EventSourceHttpClient; |
| 23 | +use Symfony\Component\HttpClient\MockHttpClient; |
| 24 | +use Symfony\Component\HttpClient\Response\MockResponse; |
| 25 | +use Symfony\Contracts\HttpClient\ResponseInterface as HttpResponse; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author Mathieu Santostefano <msantostefano@proton.me> |
| 29 | + */ |
| 30 | +#[CoversClass(ModelClient::class)] |
| 31 | +#[UsesClass(Perplexity::class)] |
| 32 | +#[Small] |
| 33 | +final class ModelClientTest extends TestCase |
| 34 | +{ |
| 35 | + public function testItThrowsExceptionWhenApiKeyIsEmpty() |
| 36 | + { |
| 37 | + $this->expectException(InvalidArgumentException::class); |
| 38 | + $this->expectExceptionMessage('The API key must not be empty.'); |
| 39 | + |
| 40 | + new ModelClient(new MockHttpClient(), ''); |
| 41 | + } |
| 42 | + |
| 43 | + #[TestWith(['api-key-without-prefix'])] |
| 44 | + #[TestWith(['plx-api-key'])] |
| 45 | + #[TestWith(['PPLX-api-key'])] |
| 46 | + #[TestWith(['pplxapikey'])] |
| 47 | + #[TestWith(['pplx api-key'])] |
| 48 | + #[TestWith(['pplx'])] |
| 49 | + public function testItThrowsExceptionWhenApiKeyDoesNotStartWithPplx(string $invalidApiKey) |
| 50 | + { |
| 51 | + $this->expectException(InvalidArgumentException::class); |
| 52 | + $this->expectExceptionMessage('The API key must start with "pplx-".'); |
| 53 | + |
| 54 | + new ModelClient(new MockHttpClient(), $invalidApiKey); |
| 55 | + } |
| 56 | + |
| 57 | + public function testItAcceptsValidApiKey() |
| 58 | + { |
| 59 | + $modelClient = new ModelClient(new MockHttpClient(), 'pplx-valid-api-key'); |
| 60 | + |
| 61 | + $this->assertInstanceOf(ModelClient::class, $modelClient); |
| 62 | + } |
| 63 | + |
| 64 | + public function testItWrapsHttpClientInEventSourceHttpClient() |
| 65 | + { |
| 66 | + $httpClient = new MockHttpClient(); |
| 67 | + $modelClient = new ModelClient($httpClient, 'pplx-valid-api-key'); |
| 68 | + |
| 69 | + $this->assertInstanceOf(ModelClient::class, $modelClient); |
| 70 | + } |
| 71 | + |
| 72 | + public function testItAcceptsEventSourceHttpClientDirectly() |
| 73 | + { |
| 74 | + $httpClient = new EventSourceHttpClient(new MockHttpClient()); |
| 75 | + $modelClient = new ModelClient($httpClient, 'pplx-valid-api-key'); |
| 76 | + |
| 77 | + $this->assertInstanceOf(ModelClient::class, $modelClient); |
| 78 | + } |
| 79 | + |
| 80 | + public function testItIsSupportingTheCorrectModel() |
| 81 | + { |
| 82 | + $modelClient = new ModelClient(new MockHttpClient(), 'pplx-api-key'); |
| 83 | + |
| 84 | + $this->assertTrue($modelClient->supports(new Perplexity())); |
| 85 | + } |
| 86 | + |
| 87 | + public function testItIsExecutingTheCorrectRequest() |
| 88 | + { |
| 89 | + $resultCallback = static function (string $method, string $url, array $options): HttpResponse { |
| 90 | + self::assertSame('POST', $method); |
| 91 | + self::assertSame('https://api.perplexity.ai/chat/completions', $url); |
| 92 | + self::assertSame('Authorization: Bearer pplx-api-key', $options['normalized_headers']['authorization'][0]); |
| 93 | + self::assertSame('{"model":"sonar","messages":[{"role":"user","content":"test message"}]}', $options['body']); |
| 94 | + |
| 95 | + return new MockResponse(); |
| 96 | + }; |
| 97 | + $httpClient = new MockHttpClient([$resultCallback]); |
| 98 | + $modelClient = new ModelClient($httpClient, 'pplx-api-key'); |
| 99 | + $modelClient->request(new Perplexity(), ['model' => 'sonar', 'messages' => [['role' => 'user', 'content' => 'test message']]]); |
| 100 | + } |
| 101 | + |
| 102 | + public function testItIsExecutingTheCorrectRequestWithArrayPayload() |
| 103 | + { |
| 104 | + $resultCallback = static function (string $method, string $url, array $options): HttpResponse { |
| 105 | + self::assertSame('POST', $method); |
| 106 | + self::assertSame('https://api.perplexity.ai/chat/completions', $url); |
| 107 | + self::assertSame('Authorization: Bearer pplx-api-key', $options['normalized_headers']['authorization'][0]); |
| 108 | + self::assertSame('{"model":"sonar","messages":[{"role":"user","content":"Hello"}]}', $options['body']); |
| 109 | + |
| 110 | + return new MockResponse(); |
| 111 | + }; |
| 112 | + $httpClient = new MockHttpClient([$resultCallback]); |
| 113 | + $modelClient = new ModelClient($httpClient, 'pplx-api-key'); |
| 114 | + $modelClient->request(new Perplexity(), ['model' => 'sonar', 'messages' => [['role' => 'user', 'content' => 'Hello']]]); |
| 115 | + } |
| 116 | +} |
0 commit comments