diff --git a/src/platform/src/Bridge/OpenAi/Whisper/ModelClient.php b/src/platform/src/Bridge/OpenAi/Whisper/ModelClient.php index e40dbfa8c..de945ffb9 100644 --- a/src/platform/src/Bridge/OpenAi/Whisper/ModelClient.php +++ b/src/platform/src/Bridge/OpenAi/Whisper/ModelClient.php @@ -30,6 +30,9 @@ public function __construct( if ('' === $apiKey) { throw new InvalidArgumentException('The API key must not be empty.'); } + if (!str_starts_with($apiKey, 'sk-')) { + throw new InvalidArgumentException('The API key must start with "sk-".'); + } } public function supports(Model $model): bool diff --git a/src/platform/tests/Bridge/OpenAi/Whisper/ModelClientTest.php b/src/platform/tests/Bridge/OpenAi/Whisper/ModelClientTest.php index 3b37f8206..238c3bcec 100644 --- a/src/platform/tests/Bridge/OpenAi/Whisper/ModelClientTest.php +++ b/src/platform/tests/Bridge/OpenAi/Whisper/ModelClientTest.php @@ -13,10 +13,12 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Small; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use Symfony\AI\Platform\Bridge\OpenAi\Whisper; use Symfony\AI\Platform\Bridge\OpenAi\Whisper\ModelClient; use Symfony\AI\Platform\Bridge\OpenAi\Whisper\Task; +use Symfony\AI\Platform\Exception\InvalidArgumentException; use Symfony\Component\HttpClient\MockHttpClient; use Symfony\Component\HttpClient\Response\MockResponse; @@ -24,9 +26,38 @@ #[Small] final class ModelClientTest extends TestCase { + public function testItThrowsExceptionWhenApiKeyIsEmpty() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('The API key must not be empty.'); + + new ModelClient(new MockHttpClient(), ''); + } + + #[TestWith(['api-key-without-prefix'])] + #[TestWith(['pk-api-key'])] + #[TestWith(['SK-api-key'])] + #[TestWith(['skapikey'])] + #[TestWith(['sk api-key'])] + #[TestWith(['sk'])] + public function testItThrowsExceptionWhenApiKeyDoesNotStartWithSk(string $invalidApiKey) + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('The API key must start with "sk-".'); + + new ModelClient(new MockHttpClient(), $invalidApiKey); + } + + public function testItAcceptsValidApiKey() + { + $modelClient = new ModelClient(new MockHttpClient(), 'sk-valid-api-key'); + + $this->assertInstanceOf(ModelClient::class, $modelClient); + } + public function testItSupportsWhisperModel() { - $client = new ModelClient(new MockHttpClient(), 'test-key'); + $client = new ModelClient(new MockHttpClient(), 'sk-test-key'); $this->assertTrue($client->supports(new Whisper())); } @@ -41,7 +72,7 @@ function ($method, $url): MockResponse { }, ]); - $client = new ModelClient($httpClient, 'test-key'); + $client = new ModelClient($httpClient, 'sk-test-key'); $client->request(new Whisper(), ['file' => 'audio-data']); $this->assertSame(1, $httpClient->getRequestsCount()); @@ -58,7 +89,7 @@ function ($method, $url): MockResponse { }, ]); - $client = new ModelClient($httpClient, 'test-key'); + $client = new ModelClient($httpClient, 'sk-test-key'); $client->request(new Whisper(), ['file' => 'audio-data'], ['task' => Task::TRANSCRIPTION]); $this->assertSame(1, $httpClient->getRequestsCount()); @@ -75,7 +106,7 @@ function ($method, $url): MockResponse { }, ]); - $client = new ModelClient($httpClient, 'test-key'); + $client = new ModelClient($httpClient, 'sk-test-key'); $client->request(new Whisper(), ['file' => 'audio-data'], ['task' => Task::TRANSLATION]); $this->assertSame(1, $httpClient->getRequestsCount());