Skip to content

Commit a396057

Browse files
Build/Test Tools: Skip the live AI provider tests only when the provider is unavailable.
The tests are now skipped only on a rate limit, a server error of the provider or a network error. Any other error fails them, including a response the provider changed and a disabled AI Client. See #66707.
1 parent fdcfa19 commit a396057

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

‎tests/phpunit/tests/ai-client/wpAiClientLiveProviders.php‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use WordPress\AiClient\Messages\DTO\MessagePart;
2222
use WordPress\AiClient\Messages\DTO\UserMessage;
2323
use WordPress\AiClient\Providers\Http\Exception\ClientException;
24+
use WordPress\AiClient\Providers\Http\Exception\NetworkException;
25+
use WordPress\AiClient\Providers\Http\Exception\ServerException;
2426
use WordPress\AiClient\Providers\ProviderRegistry;
2527

2628
class Tests_AI_Client_LiveProviders extends WP_UnitTestCase {
@@ -188,8 +190,8 @@ public static function data_providers_and_abilities(): array {
188190
/**
189191
* Asks a provider a question and runs the abilities the model calls until it answers.
190192
*
191-
* The test is skipped when the provider has no API key or is temporarily unavailable,
192-
* and fails when the provider rejects the key.
193+
* The test is skipped when the provider has no API key or is unavailable: it limits the
194+
* requests, has a server error or cannot be reached. Every other error fails the test.
193195
*
194196
* @param string $provider Provider ID.
195197
* @param string[] $abilities Names of the abilities given to the model.
@@ -213,14 +215,17 @@ private function ask( string $provider, array $abilities, string $question, arra
213215
/*
214216
* The AI Client reports a provider whose models cannot be listed as one without models,
215217
* so list them here to get the reason. A successful list is cached for the requests below.
218+
* Any other exception, such as a response the provider changed, is an error of the test.
216219
*/
217220
try {
218221
self::PROVIDERS[ $provider ]::modelMetadataDirectory()->listModelMetadata();
219-
} catch ( Exception $e ) {
220-
if ( $e instanceof ClientException && 429 !== $e->getCode() ) {
221-
$this->fail( "The provider rejected the $key_name: " . $e->getMessage() );
222-
}
222+
} catch ( NetworkException | ServerException $e ) {
223223
$this->markTestSkipped( 'The provider is unavailable: ' . $e->getMessage() );
224+
} catch ( ClientException $e ) {
225+
if ( 429 === $e->getCode() ) {
226+
$this->markTestSkipped( 'The provider is unavailable: ' . $e->getMessage() );
227+
}
228+
$this->fail( "The provider rejected the $key_name: " . $e->getMessage() );
224229
}
225230

226231
$resolver = new WP_AI_Client_Ability_Function_Resolver( ...$abilities );
@@ -240,11 +245,14 @@ private function ask( string $provider, array $abilities, string $question, arra
240245
$result = $builder->generate_text_result();
241246

242247
if ( is_wp_error( $result ) ) {
243-
$status = $result->get_error_data()['status'] ?? 0;
244-
if ( 429 === $status || $status >= 500 ) {
245-
$this->markTestSkipped( "The provider is unavailable ($status): " . $result->get_error_message() );
248+
$status = $result->get_error_data()['status'] ?? 0;
249+
$message = "($status): " . $result->get_error_message();
250+
251+
// Only a rate limit, a server error of the provider and a network error skip the test.
252+
if ( 429 === $status || in_array( $result->get_error_code(), array( 'prompt_network_error', 'prompt_upstream_server_error' ), true ) ) {
253+
$this->markTestSkipped( "The provider is unavailable $message" );
246254
}
247-
$this->fail( "The request failed ($status): " . $result->get_error_message() );
255+
$this->fail( "The request failed $message" );
248256
}
249257

250258
// OpenAI returns each function call as its own candidate, so keep them all, not just toMessage().

0 commit comments

Comments
 (0)