2121use WordPress \AiClient \Messages \DTO \MessagePart ;
2222use WordPress \AiClient \Messages \DTO \UserMessage ;
2323use WordPress \AiClient \Providers \Http \Exception \ClientException ;
24+ use WordPress \AiClient \Providers \Http \Exception \NetworkException ;
25+ use WordPress \AiClient \Providers \Http \Exception \ServerException ;
2426use WordPress \AiClient \Providers \ProviderRegistry ;
2527
2628class 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