@@ -1347,22 +1347,15 @@ public function test_validate_messages_non_user_last_returns_wp_error() {
13471347 /**
13481348 * Tests parseMessage with empty string returns WP_Error on termination.
13491349 *
1350- * The SDK constructor throws immediately for empty strings, so the exception
1351- * is caught in the constructor and stored.
1352- *
13531350 * @ticket 64591
13541351 */
13551352 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 () );
13661359 }
13671360
13681361 /**
@@ -1371,13 +1364,12 @@ public function test_parse_message_empty_string_returns_wp_error() {
13711364 * @ticket 64591
13721365 */
13731366 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 () );
13811373 }
13821374
13831375 /**
@@ -1386,13 +1378,31 @@ public function test_parse_message_empty_array_returns_wp_error() {
13861378 * @ticket 64591
13871379 */
13881380 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 () );
13961406 }
13971407
13981408 /**
0 commit comments