From 84dce0b245ba053853d3392bf4e2dc8ca26992d1 Mon Sep 17 00:00:00 2001 From: Sagar Deshmukh Date: Fri, 5 Jun 2026 09:47:25 +0000 Subject: [PATCH] AI Client: fix sendRequestWithOptions error message inconsistency and add missing tests sendRequestWithOptions omitted the HTTP method from its NetworkException message, while sendRequest included it. Both paths now produce the same three-argument format so error messages carry consistent diagnostic context. Two new tests are added: - Verifies the method, URL, and underlying error appear in the exception message thrown by sendRequestWithOptions. - Verifies that generate_result() returns a WP_Error carrying the correct "AI features are not supported" message when wp_supports_ai() is false, covering a code path that was previously untested. https://core.trac.wordpress.org/ticket/64591 --- .../class-wp-ai-client-http-client.php | 5 ++-- .../tests/ai-client/wpAiClientHttpClient.php | 26 +++++++++++++++++++ .../ai-client/wpAiClientPromptBuilder.php | 16 ++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/ai-client/adapters/class-wp-ai-client-http-client.php b/src/wp-includes/ai-client/adapters/class-wp-ai-client-http-client.php index f6c6dea441d1c..1c1db9a26c707 100644 --- a/src/wp-includes/ai-client/adapters/class-wp-ai-client-http-client.php +++ b/src/wp-includes/ai-client/adapters/class-wp-ai-client-http-client.php @@ -104,8 +104,9 @@ public function sendRequestWithOptions( RequestInterface $request, RequestOption if ( is_wp_error( $response ) ) { $message = sprintf( - /* translators: 1: Request URL. 2: Error message. */ - __( 'Network error occurred while sending request to %1$s: %2$s' ), + /* translators: 1: HTTP method (e.g. GET, POST). 2: Request URL. 3: Error message. */ + __( 'Network error occurred while sending %1$s request to %2$s: %3$s' ), + $request->getMethod(), $url, $response->get_error_message() ); diff --git a/tests/phpunit/tests/ai-client/wpAiClientHttpClient.php b/tests/phpunit/tests/ai-client/wpAiClientHttpClient.php index 15fb8f07d2f18..ed45ca4e66ff9 100644 --- a/tests/phpunit/tests/ai-client/wpAiClientHttpClient.php +++ b/tests/phpunit/tests/ai-client/wpAiClientHttpClient.php @@ -329,6 +329,32 @@ static function () { $this->client->sendRequestWithOptions( $request, $options ); } + /** + * Test that sendRequestWithOptions includes the HTTP method in the NetworkException message. + * + * @ticket 64591 + */ + public function test_send_request_with_options_wp_error_message_includes_method() { + add_filter( + 'pre_http_request', + static function () { + return new WP_Error( 'http_request_failed', 'Connection refused' ); + } + ); + + $options = new WordPress\AiClient\Providers\Http\DTO\RequestOptions(); + $request = $this->psr17_factory->createRequest( 'POST', 'https://api.example.com/generate' ); + + try { + $this->client->sendRequestWithOptions( $request, $options ); + $this->fail( 'Expected NetworkException was not thrown.' ); + } catch ( WordPress\AiClient\Providers\Http\Exception\NetworkException $e ) { + $this->assertStringContainsString( 'POST', $e->getMessage() ); + $this->assertStringContainsString( 'https://api.example.com/generate', $e->getMessage() ); + $this->assertStringContainsString( 'Connection refused', $e->getMessage() ); + } + } + /** * Test seekable body is rewound before sending. * diff --git a/tests/phpunit/tests/ai-client/wpAiClientPromptBuilder.php b/tests/phpunit/tests/ai-client/wpAiClientPromptBuilder.php index e758a6868aa42..800ade63ccb24 100644 --- a/tests/phpunit/tests/ai-client/wpAiClientPromptBuilder.php +++ b/tests/phpunit/tests/ai-client/wpAiClientPromptBuilder.php @@ -2532,6 +2532,22 @@ public function test_is_supported_returns_false_when_ai_not_supported() { $this->assertFalse( $builder->is_supported() ); } + /** + * Tests that generate_result returns WP_Error with the AI-disabled message when AI is not supported. + * + * @ticket 64591 + */ + public function test_generate_result_returns_wp_error_when_ai_not_supported(): void { + add_filter( 'wp_supports_ai', '__return_false' ); + + $builder = new WP_AI_Client_Prompt_Builder( AiClient::defaultRegistry(), 'Test prompt' ); + $result = $builder->generate_result(); + + $this->assertWPError( $result ); + $this->assertSame( 'prompt_prevented', $result->get_error_code() ); + $this->assertSame( 'AI features are not supported in this environment.', $result->get_error_message() ); + } + /** * Tests that is_supported returns false when prevent prompt filter returns true. *