Changeset 63298
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/ai-client/class-wp-ai-client-prompt-builder.php
r62591 r63298 44 44 * the returned instance will be in an error state, and all subsequent method 45 45 * calls will be no-ops that just return the same error state instance. Only 46 * when a generating method is called, the WP_Error will be returned. 46 * when a generating method is called, the WP_Error will be returned. The 47 * support check methods are the exception to the no-op behavior: they return 48 * false rather than the error state instance. 47 49 * 48 50 * @since 7.0.0 … … 80 82 * @method self as_output_speech_voice(string $voice) Sets the output speech voice. 81 83 * @method self as_json_response(?array<string, mixed> $schema = null) Configures the prompt for JSON response output. 82 * @method bool |WP_Erroris_supported(?CapabilityEnum $capability = null) Checks if the prompt is supported for the given capability.84 * @method bool is_supported(?CapabilityEnum $capability = null) Checks if the prompt is supported for the given capability. 83 85 * @method bool is_supported_for_text_generation() Checks if the prompt is supported for text generation. 84 86 * @method bool is_supported_for_image_generation() Checks if the prompt is supported for image generation. … … 283 285 * This allows WordPress developers to use snake_case naming conventions. It catches 284 286 * any exceptions thrown, stores them, and returns a WP_Error when a terminate method 285 * is called .287 * is called, or false when a support check method is called. 286 288 * 287 289 * @since 7.0.0 … … 364 366 if ( self::is_generating_method( $name ) ) { 365 367 return $this->error; 368 } 369 if ( self::is_support_check_method( $name ) ) { 370 return false; 366 371 } 367 372 return $this; -
trunk/tests/phpunit/tests/ai-client/wpAiClientPromptBuilder.php
r62591 r63298 2650 2650 2651 2651 /** 2652 * Tests that support check methods return false when the wrapped builder throws. 2653 * 2654 * @ticket 65781 2655 * 2656 * @dataProvider data_support_check_methods 2657 * 2658 * @param string $method The support check method on the wrapper. 2659 * @param string $wrapped_method The matching method on the wrapped builder. 2660 */ 2661 public function test_support_check_methods_return_false_when_builder_throws( $method, $wrapped_method ) { 2662 $registry = AiClient::defaultRegistry(); 2663 $prompt_builder = new WP_AI_Client_Prompt_Builder( $registry, 'Test text' ); 2664 2665 $wrapped_builder = $this->createMock( PromptBuilder::class ); 2666 $wrapped_builder->method( $wrapped_method ) 2667 ->willThrowException( new RuntimeException( 'Thrown by the wrapped builder.' ) ); 2668 2669 $builder_property = new ReflectionProperty( WP_AI_Client_Prompt_Builder::class, 'builder' ); 2670 self::set_accessible( $builder_property ); 2671 $builder_property->setValue( $prompt_builder, $wrapped_builder ); 2672 2673 $result = $prompt_builder->{$method}(); 2674 2675 $this->assertFalse( $result, $method . ' should return false when the wrapped builder throws' ); 2676 2677 // The error is still recorded, so a generating method returns it. 2678 $error = $prompt_builder->generate_text(); 2679 $this->assertWPError( $error, 'The caught error should still be returned by generating methods' ); 2680 $this->assertSame( 2681 'prompt_builder_error', 2682 $error->get_error_code(), 2683 'The recorded error should be the one thrown by the wrapped builder' 2684 ); 2685 } 2686 2687 /** 2688 * Data provider. 2689 * 2690 * @return array<string, array{0: string, 1: string}> Support check method names, keyed by the method on the wrapper. 2691 */ 2692 public static function data_support_check_methods(): array { 2693 return array( 2694 'is_supported' => array( 'is_supported', 'isSupported' ), 2695 'is_supported_for_text_generation' => array( 'is_supported_for_text_generation', 'isSupportedForTextGeneration' ), 2696 'is_supported_for_image_generation' => array( 'is_supported_for_image_generation', 'isSupportedForImageGeneration' ), 2697 'is_supported_for_text_to_speech_conversion' => array( 'is_supported_for_text_to_speech_conversion', 'isSupportedForTextToSpeechConversion' ), 2698 'is_supported_for_video_generation' => array( 'is_supported_for_video_generation', 'isSupportedForVideoGeneration' ), 2699 'is_supported_for_speech_generation' => array( 'is_supported_for_speech_generation', 'isSupportedForSpeechGeneration' ), 2700 'is_supported_for_music_generation' => array( 'is_supported_for_music_generation', 'isSupportedForMusicGeneration' ), 2701 'is_supported_for_embedding_generation' => array( 'is_supported_for_embedding_generation', 'isSupportedForEmbeddingGeneration' ), 2702 ); 2703 } 2704 2705 /** 2706 * Tests that a throw from the bundled client during a support check is handled. 2707 * 2708 * This covers the route reported on the ticket. The document modality has no 2709 * capability to infer, so the wrapped builder throws while determining whether 2710 * the prompt is supported. 2711 * 2712 * @ticket 65781 2713 */ 2714 public function test_is_supported_returns_false_when_the_bundled_builder_throws() { 2715 $registry = AiClient::defaultRegistry(); 2716 $prompt_builder = new WP_AI_Client_Prompt_Builder( $registry, 'Test text' ); 2717 2718 $result = $prompt_builder->as_output_modalities( ModalityEnum::document() )->is_supported(); 2719 2720 $this->assertFalse( $result, 'is_supported should return false when the wrapped builder throws' ); 2721 2722 $error = $prompt_builder->generate_text(); 2723 $this->assertWPError( $error, 'The caught error should still be returned by generating methods' ); 2724 $this->assertSame( 2725 'prompt_builder_error', 2726 $error->get_error_code(), 2727 'The document modality should still throw from the wrapped builder. If it no longer does, this test needs another route to a throw' 2728 ); 2729 } 2730 2731 /** 2652 2732 * Tests that generating methods return WP_Error when in error state. 2653 2733 *
Note:
See TracChangeset
for help on using the changeset viewer.