1313
1414use PHPUnit \Framework \Attributes \CoversClass ;
1515use PHPUnit \Framework \Attributes \Small ;
16+ use PHPUnit \Framework \Attributes \TestWith ;
1617use PHPUnit \Framework \TestCase ;
1718use Symfony \AI \Platform \Bridge \OpenAi \Whisper ;
1819use Symfony \AI \Platform \Bridge \OpenAi \Whisper \ModelClient ;
1920use Symfony \AI \Platform \Bridge \OpenAi \Whisper \Task ;
21+ use Symfony \AI \Platform \Exception \InvalidArgumentException ;
2022use Symfony \Component \HttpClient \MockHttpClient ;
2123use Symfony \Component \HttpClient \Response \MockResponse ;
2224
2325#[CoversClass(ModelClient::class)]
2426#[Small]
2527final class ModelClientTest extends TestCase
2628{
29+ public function testItThrowsExceptionWhenApiKeyIsEmpty ()
30+ {
31+ $ this ->expectException (InvalidArgumentException::class);
32+ $ this ->expectExceptionMessage ('The API key must not be empty. ' );
33+
34+ new ModelClient (new MockHttpClient (), '' );
35+ }
36+
37+ #[TestWith(['api-key-without-prefix ' ])]
38+ #[TestWith(['pk-api-key ' ])]
39+ #[TestWith(['SK-api-key ' ])]
40+ #[TestWith(['skapikey ' ])]
41+ #[TestWith(['sk api-key ' ])]
42+ #[TestWith(['sk ' ])]
43+ public function testItThrowsExceptionWhenApiKeyDoesNotStartWithSk (string $ invalidApiKey )
44+ {
45+ $ this ->expectException (InvalidArgumentException::class);
46+ $ this ->expectExceptionMessage ('The API key must start with "sk-". ' );
47+
48+ new ModelClient (new MockHttpClient (), $ invalidApiKey );
49+ }
50+
51+ public function testItAcceptsValidApiKey ()
52+ {
53+ $ modelClient = new ModelClient (new MockHttpClient (), 'sk-valid-api-key ' );
54+
55+ $ this ->assertInstanceOf (ModelClient::class, $ modelClient );
56+ }
57+
2758 public function testItSupportsWhisperModel ()
2859 {
29- $ client = new ModelClient (new MockHttpClient (), 'test-key ' );
60+ $ client = new ModelClient (new MockHttpClient (), 'sk- test-key ' );
3061 $ this ->assertTrue ($ client ->supports (new Whisper ()));
3162 }
3263
@@ -41,7 +72,7 @@ function ($method, $url): MockResponse {
4172 },
4273 ]);
4374
44- $ client = new ModelClient ($ httpClient , 'test-key ' );
75+ $ client = new ModelClient ($ httpClient , 'sk- test-key ' );
4576 $ client ->request (new Whisper (), ['file ' => 'audio-data ' ]);
4677
4778 $ this ->assertSame (1 , $ httpClient ->getRequestsCount ());
@@ -58,7 +89,7 @@ function ($method, $url): MockResponse {
5889 },
5990 ]);
6091
61- $ client = new ModelClient ($ httpClient , 'test-key ' );
92+ $ client = new ModelClient ($ httpClient , 'sk- test-key ' );
6293 $ client ->request (new Whisper (), ['file ' => 'audio-data ' ], ['task ' => Task::TRANSCRIPTION ]);
6394
6495 $ this ->assertSame (1 , $ httpClient ->getRequestsCount ());
@@ -75,7 +106,7 @@ function ($method, $url): MockResponse {
75106 },
76107 ]);
77108
78- $ client = new ModelClient ($ httpClient , 'test-key ' );
109+ $ client = new ModelClient ($ httpClient , 'sk- test-key ' );
79110 $ client ->request (new Whisper (), ['file ' => 'audio-data ' ], ['task ' => Task::TRANSLATION ]);
80111
81112 $ this ->assertSame (1 , $ httpClient ->getRequestsCount ());
0 commit comments