Skip to content

Commit 86c5907

Browse files
Build/Test Tools: Tidy the live AI provider tests and document their constants.
Removes the unregister loops that ran before the core abilities are registered, resolves a named model once instead of on every request, and catches client errors apart from network and server errors. Documents the key and model constants in `wp-tests-config-sample.php` and `.env.example`, and says that the keys reach the PHP container that serves the local site. See #66707.
1 parent 938e9be commit 86c5907

3 files changed

Lines changed: 21 additions & 13 deletions

File tree

‎.env.example‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,11 @@ LOCAL_WP_TESTS_DOMAIN=example.org
9090
# npm run test:php -- --group external-http --filter Tests_AI_Client_LiveProviders
9191
#
9292
# The tests of a provider are skipped while its key is not set. A provider uses its
93-
# default model unless its `WP_TESTS_AI_*_MODEL` variable names another one.
93+
# default model unless its `WP_TESTS_AI_*_MODEL` variable names another one. The same
94+
# names can also be defined as constants in `wp-tests-config.php`.
9495
#
95-
# Only the tests read these names, so the keys are not used by the local site.
96+
# The keys are passed to the PHP container, which also serves the local site, but only
97+
# the tests read these names.
9698
##
9799
# WP_TESTS_AI_ANTHROPIC_API_KEY=
98100
# WP_TESTS_AI_GOOGLE_API_KEY=

‎tests/phpunit/tests/ai-client/wpAiClientLiveProviders.php‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,6 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ): void
118118
);
119119
update_option( 'timezone_string', 'Pacific/Chatham' );
120120

121-
foreach ( wp_get_abilities() as $ability ) {
122-
wp_unregister_ability( $ability->get_name() );
123-
}
124-
foreach ( wp_get_ability_categories() as $ability_category ) {
125-
wp_unregister_ability_category( $ability_category->get_slug() );
126-
}
127-
128121
// The core abilities can only be registered while their registration actions run.
129122
$wp_current_filter[] = 'wp_abilities_api_categories_init';
130123
wp_register_core_ability_categories();
@@ -242,16 +235,19 @@ private function ask( string $provider, array $abilities, string $question ): ar
242235
*/
243236
try {
244237
$provider_class::modelMetadataDirectory()->listModelMetadata();
245-
} catch ( NetworkException | ServerException | ClientException $e ) {
246-
if ( $e instanceof ClientException && 429 !== $e->getCode() ) {
238+
} catch ( ClientException $e ) {
239+
if ( 429 !== $e->getCode() ) {
247240
$this->fail( "The model list request failed, check the $key_name: " . $e->getMessage() );
248241
}
249242
$this->markTestSkipped( 'The provider is unavailable: ' . $e->getMessage() );
243+
} catch ( NetworkException | ServerException $e ) {
244+
$this->markTestSkipped( 'The provider is unavailable: ' . $e->getMessage() );
250245
}
251246

252247
if ( $model && ! $provider_class::modelMetadataDirectory()->hasModelMetadata( $model ) ) {
253248
$this->fail( "The provider has no \"$model\" model, check the $model_name." );
254249
}
250+
$model_instance = $model ? $registry->getProviderModel( $provider, $model ) : null;
255251

256252
$resolver = new WP_AI_Client_Ability_Function_Resolver( ...$abilities );
257253
$messages = array( new UserMessage( array( new MessagePart( $question ) ) ) );
@@ -273,8 +269,8 @@ static function ( string $ability_name ) use ( &$called ) {
273269
->using_system_instruction( 'Answer every question with the abilities. Quote the values they return unchanged.' )
274270
->using_abilities( ...$abilities );
275271

276-
if ( $model ) {
277-
$builder->using_model( $registry->getProviderModel( $provider, $model ) );
272+
if ( $model_instance ) {
273+
$builder->using_model( $model_instance );
278274
}
279275

280276
$result = $builder->generate_text_result();

‎wp-tests-config-sample.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@
2323
*/
2424
// define( 'WP_TESTS_FORCE_KNOWN_BUGS', true );
2525

26+
/*
27+
* API keys for the live AI provider tests, which send real requests.
28+
* The tests of a provider are skipped while its key is not set, here or in an environment
29+
* variable of the same name. A provider uses its default model unless its
30+
* `WP_TESTS_AI_*_MODEL` constant, such as `WP_TESTS_AI_GOOGLE_MODEL`, names another one.
31+
*/
32+
// define( 'WP_TESTS_AI_ANTHROPIC_API_KEY', '' );
33+
// define( 'WP_TESTS_AI_GOOGLE_API_KEY', '' );
34+
// define( 'WP_TESTS_AI_OPENAI_API_KEY', '' );
35+
2636
// Test with WordPress debug mode (default).
2737
define( 'WP_DEBUG', true );
2838

0 commit comments

Comments
 (0)