Skip to content

Commit ddc4861

Browse files
Build/Test Tools: Simplify the live AI provider tests and read their settings from constants too.
An ability now counts as called when the `wp_after_execute_ability` action fires, which only happens after it ran without an error. A model named in `WP_TESTS_AI_*_MODEL` is used directly instead of as a preference, so no request goes to another model, and a model the provider does not list still fails the test. The API keys and models can also be set as constants, for example in `wp-tests-config.php`. The timezone option is set once for the class, and the super admin status is no longer revoked by hand, since the options are restored after each class. See #66707.
1 parent fc8333b commit ddc4861

1 file changed

Lines changed: 37 additions & 42 deletions

File tree

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

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44
*
55
* These tests send real requests. The tests of a provider only run when its API key is in the
66
* `WP_TESTS_AI_ANTHROPIC_API_KEY`, `WP_TESTS_AI_GOOGLE_API_KEY` or `WP_TESTS_AI_OPENAI_API_KEY`
7-
* environment variable:
7+
* environment variable or constant:
88
*
99
* WP_TESTS_AI_ANTHROPIC_API_KEY=your-key vendor/bin/phpunit --group external-http --filter Tests_AI_Client_LiveProviders
1010
*
11-
* The provider tools and the Connectors read other names, such as `ANTHROPIC_API_KEY`, so a key
12-
* set for them does not start these tests, and a key set for these tests is not used by a site.
13-
*
14-
* A provider uses its default model, unless its `WP_TESTS_AI_*_MODEL` environment variable,
15-
* such as `WP_TESTS_AI_GOOGLE_MODEL`, names another one.
11+
* A provider uses its default model, unless its `WP_TESTS_AI_*_MODEL` environment variable or
12+
* constant, such as `WP_TESTS_AI_GOOGLE_MODEL`, names another one.
1613
*
1714
* @group ai-client
1815
* @group abilities-api
@@ -37,6 +34,8 @@ class Tests_AI_Client_LiveProviders extends WP_UnitTestCase {
3734

3835
/**
3936
* Provider classes keyed by provider ID.
37+
*
38+
* @var array<string, class-string>
4039
*/
4140
private const PROVIDERS = array(
4241
'anthropic' => AnthropicProvider::class,
@@ -46,6 +45,8 @@ class Tests_AI_Client_LiveProviders extends WP_UnitTestCase {
4645

4746
/**
4847
* For each core ability, a question no other ability answers and the value the answer must contain.
48+
*
49+
* @var array<string, array{question: string, answer: string}>
4950
*/
5051
private const QUESTIONS = array(
5152
'core/get-site-info' => array(
@@ -58,6 +59,7 @@ class Tests_AI_Client_LiveProviders extends WP_UnitTestCase {
5859
),
5960
'core/get-environment-info' => array(
6061
'question' => 'Which PHP version does this site run?',
62+
// Without the suffix some builds add to `phpversion()`, such as `-1ubuntu2`, which a model may leave out.
6163
'answer' => PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION,
6264
),
6365
'core/settings-get' => array(
@@ -82,7 +84,7 @@ class Tests_AI_Client_LiveProviders extends WP_UnitTestCase {
8284
private static $admin_id;
8385

8486
/**
85-
* Creates the content the questions are about and registers the core abilities.
87+
* Creates the users, the post and the option the questions are about, and registers the core abilities.
8688
*
8789
* @param WP_UnitTest_Factory $factory Factory instance.
8890
*/
@@ -114,6 +116,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ): void
114116
'post_title' => 'Quokka Lighthouse Notes',
115117
)
116118
);
119+
update_option( 'timezone_string', 'Pacific/Chatham' );
117120

118121
foreach ( wp_get_abilities() as $ability ) {
119122
wp_unregister_ability( $ability->get_name() );
@@ -133,13 +136,9 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ): void
133136
}
134137

135138
/**
136-
* Revokes the super admin status and cleans up the registered abilities and categories.
139+
* Cleans up the registered abilities and categories.
137140
*/
138141
public static function wpTearDownAfterClass(): void {
139-
if ( is_multisite() ) {
140-
revoke_super_admin( self::$admin_id );
141-
}
142-
143142
foreach ( wp_get_abilities() as $ability ) {
144143
wp_unregister_ability( $ability->get_name() );
145144
}
@@ -149,13 +148,12 @@ public static function wpTearDownAfterClass(): void {
149148
}
150149

151150
/**
152-
* Signs in the administrator, sets the option one of the questions is about and allows slow responses.
151+
* Signs in the administrator and allows slow responses.
153152
*/
154153
public function set_up(): void {
155154
parent::set_up();
156155

157156
wp_set_current_user( self::$admin_id );
158-
update_option( 'timezone_string', 'Pacific/Chatham' );
159157

160158
// The request that lists the models of a provider has no timeout of its own.
161159
add_filter( 'http_request_timeout', static fn () => 30 );
@@ -209,8 +207,9 @@ public static function data_providers_and_abilities(): array {
209207
/**
210208
* Asks a provider a question and runs the abilities the model calls until it answers.
211209
*
212-
* The test is skipped when the provider has no API key or is unavailable: it limits the
213-
* requests, has a server error or cannot be reached. Every other error fails the test.
210+
* The test is skipped when the provider has no API key, its package is not installed or it is
211+
* unavailable: it limits the requests, has a server error or cannot be reached. Every other
212+
* error fails the test.
214213
*
215214
* @param string $provider Provider ID.
216215
* @param string[] $abilities Names of the abilities given to the model.
@@ -222,14 +221,14 @@ private function ask( string $provider, array $abilities, string $question ): ar
222221
$provider_class = self::PROVIDERS[ $provider ];
223222
$key_name = 'WP_TESTS_AI_' . strtoupper( $provider ) . '_API_KEY';
224223
$model_name = 'WP_TESTS_AI_' . strtoupper( $provider ) . '_MODEL';
225-
$api_key = getenv( $key_name );
226-
$model = getenv( $model_name );
224+
$api_key = defined( $key_name ) ? constant( $key_name ) : getenv( $key_name );
225+
$model = defined( $model_name ) ? constant( $model_name ) : getenv( $model_name );
227226

228227
if ( ! $api_key ) {
229-
$this->markTestSkipped( "The $key_name environment variable is not set." );
228+
$this->markTestSkipped( "The $key_name environment variable or constant is not set." );
230229
}
231230
if ( ! class_exists( $provider_class ) ) {
232-
$this->markTestSkipped( 'The AI provider packages are not installed. Run `composer update`.' );
231+
$this->markTestSkipped( 'The AI provider packages are not installed. Run `composer update`, or `npm run env:composer -- update` in the local Docker environment.' );
233232
}
234233

235234
$registry = new ProviderRegistry();
@@ -250,21 +249,32 @@ private function ask( string $provider, array $abilities, string $question ): ar
250249
$this->markTestSkipped( 'The provider is unavailable: ' . $e->getMessage() );
251250
}
252251

252+
if ( $model && ! $provider_class::modelMetadataDirectory()->hasModelMetadata( $model ) ) {
253+
$this->fail( "The provider has no \"$model\" model, check the $model_name." );
254+
}
255+
253256
$resolver = new WP_AI_Client_Ability_Function_Resolver( ...$abilities );
254257
$messages = array( new UserMessage( array( new MessagePart( $question ) ) ) );
255258
$called = array();
256259

260+
// The action only fires after an ability ran without an error, so only those count as called.
261+
add_action(
262+
'wp_after_execute_ability',
263+
static function ( string $ability_name ) use ( &$called ) {
264+
$called[ $ability_name ] = true;
265+
}
266+
);
267+
257268
// A model may call one ability per response: a request for each, one to answer and a spare one.
258269
$max_rounds = count( $abilities ) + 2;
259270

260271
for ( $round = 0; $round < $max_rounds; $round++ ) {
261272
$builder = ( new WP_AI_Client_Prompt_Builder( $registry, $messages ) )
262-
->using_provider( $provider )
263273
->using_system_instruction( 'Answer every question with the abilities. Quote the values they return unchanged.' )
264274
->using_abilities( ...$abilities );
265275

266276
if ( $model ) {
267-
$builder->using_model_preference( $model );
277+
$builder->using_model( $registry->getProviderModel( $provider, $model ) );
268278
}
269279

270280
$result = $builder->generate_text_result();
@@ -279,11 +289,6 @@ private function ask( string $provider, array $abilities, string $question ): ar
279289
$this->fail( 'The request failed: ' . $result->get_error_message() );
280290
}
281291

282-
// A model preference the provider cannot use for the prompt falls back to another model without an error.
283-
if ( $model && $result->getModelMetadata()->getId() !== $model ) {
284-
$this->fail( "The provider has no \"$model\" model for this prompt, check the $model_name." );
285-
}
286-
287292
// OpenAI returns each function call as its own candidate, so keep the message of each one.
288293
$has_calls = false;
289294
$answer = '';
@@ -297,27 +302,17 @@ private function ask( string $provider, array $abilities, string $question ): ar
297302
}
298303
}
299304

300-
if ( ! $resolver->has_ability_calls( $message ) ) {
301-
continue;
302-
}
303-
304-
// Only an ability that ran without an error counts as called.
305-
$response = $resolver->execute_abilities( $message );
306-
foreach ( $response->getParts() as $part ) {
307-
$data = $part->getFunctionResponse()->getResponse();
308-
if ( ! is_array( $data ) || ! isset( $data['error'] ) ) {
309-
$called[] = WP_AI_Client_Ability_Function_Resolver::function_name_to_ability_name( $part->getFunctionResponse()->getName() );
310-
}
305+
if ( $resolver->has_ability_calls( $message ) ) {
306+
$messages[] = $resolver->execute_abilities( $message );
307+
$has_calls = true;
311308
}
312-
$messages[] = $response;
313-
$has_calls = true;
314309
}
315310

316311
if ( ! $has_calls ) {
317-
return array( $answer, array_unique( $called ), $result->getModelMetadata()->getId() );
312+
return array( $answer, array_keys( $called ), $result->getModelMetadata()->getId() );
318313
}
319314
}
320315

321-
$this->fail( 'The model kept calling abilities without answering. Abilities that ran: ' . implode( ', ', array_unique( $called ) ) );
316+
$this->fail( 'The model kept calling abilities without answering. Abilities that ran: ' . implode( ', ', array_keys( $called ) ) );
322317
}
323318
}

0 commit comments

Comments
 (0)