Changeset 61787
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/ai-client/class-wp-ai-client-prompt-builder.php
r61700 r61787 166 166 */ 167 167 public function __construct( ProviderRegistry $registry, $prompt = null ) { 168 $this->builder = new PromptBuilder( $registry, $prompt ); 168 try { 169 $this->builder = new PromptBuilder( $registry, $prompt ); 170 } catch ( Exception $e ) { 171 $this->builder = new PromptBuilder( $registry ); 172 $this->error = new WP_Error( 173 'prompt_builder_error', 174 $e->getMessage(), 175 array( 176 'exception_class' => get_class( $e ), 177 ) 178 ); 179 } 169 180 170 181 /** -
trunk/tests/phpunit/tests/ai-client/wpAiClientPromptBuilder.php
r61777 r61787 1348 1348 * Tests parseMessage with empty string returns WP_Error on termination. 1349 1349 * 1350 * The SDK constructor throws immediately for empty strings, so the exception1351 * is caught in the constructor and stored.1352 *1353 1350 * @ticket 64591 1354 1351 */ 1355 1352 public function test_parse_message_empty_string_returns_wp_error() { 1356 // The empty string exception is thrown by the SDK's PromptBuilder constructor, 1357 // which happens before our __call() error handling. We must catch it manually. 1358 try { 1359 $builder = new WP_AI_Client_Prompt_Builder( $this->registry, ' ' ); 1360 // If we get here, the SDK didn't throw. Test would need adjusting. 1361 $result = $builder->generate_result(); 1362 $this->assertWPError( $result ); 1363 } catch ( InvalidArgumentException $e ) { 1364 $this->assertStringContainsString( 'Cannot create a message from an empty string', $e->getMessage() ); 1365 } 1353 $builder = new WP_AI_Client_Prompt_Builder( $this->registry, ' ' ); 1354 $result = $builder->generate_result(); 1355 1356 $this->assertWPError( $result ); 1357 $this->assertSame( 'prompt_builder_error', $result->get_error_code() ); 1358 $this->assertStringContainsString( 'Cannot create a message from an empty string', $result->get_error_message() ); 1366 1359 } 1367 1360 … … 1372 1365 */ 1373 1366 public function test_parse_message_empty_array_returns_wp_error() { 1374 try { 1375 $builder = new WP_AI_Client_Prompt_Builder( $this->registry, array() ); 1376 $result = $builder->generate_result(); 1377 $this->assertWPError( $result ); 1378 } catch ( InvalidArgumentException $e ) { 1379 $this->assertStringContainsString( 'Cannot create a message from an empty array', $e->getMessage() ); 1380 } 1367 $builder = new WP_AI_Client_Prompt_Builder( $this->registry, array() ); 1368 $result = $builder->generate_result(); 1369 1370 $this->assertWPError( $result ); 1371 $this->assertSame( 'prompt_builder_error', $result->get_error_code() ); 1372 $this->assertStringContainsString( 'Cannot create a message from an empty array', $result->get_error_message() ); 1381 1373 } 1382 1374 … … 1387 1379 */ 1388 1380 public function test_parse_message_invalid_type_returns_wp_error() { 1389 try { 1390 $builder = new WP_AI_Client_Prompt_Builder( $this->registry, 123 ); 1391 $result = $builder->generate_result(); 1392 $this->assertWPError( $result ); 1393 } catch ( InvalidArgumentException $e ) { 1394 $this->assertStringContainsString( 'Input must be a string, MessagePart, MessagePartArrayShape', $e->getMessage() ); 1395 } 1381 $builder = new WP_AI_Client_Prompt_Builder( $this->registry, 123 ); 1382 $result = $builder->generate_result(); 1383 1384 $this->assertWPError( $result ); 1385 $this->assertSame( 'prompt_builder_error', $result->get_error_code() ); 1386 $this->assertStringContainsString( 'Input must be a string, MessagePart, MessagePartArrayShape', $result->get_error_message() ); 1387 } 1388 1389 /** 1390 * Tests that wp_ai_client_prompt() with an empty string does not throw. 1391 * 1392 * Constructor exceptions are caught and surfaced as WP_Error from 1393 * generating methods, consistent with the __call() wrapping behavior. 1394 * 1395 * @ticket 64591 1396 */ 1397 public function test_wp_ai_client_prompt_empty_string_returns_wp_error() { 1398 $builder = wp_ai_client_prompt( ' ' ); 1399 1400 $this->assertInstanceOf( WP_AI_Client_Prompt_Builder::class, $builder ); 1401 1402 $result = $builder->generate_text(); 1403 1404 $this->assertWPError( $result ); 1405 $this->assertSame( 'prompt_builder_error', $result->get_error_code() ); 1396 1406 } 1397 1407
Note: See TracChangeset
for help on using the changeset viewer.