From 529f83fbbb6dd5663fcc68f0e3c4756a2ae55f74 Mon Sep 17 00:00:00 2001 From: Chris Huber Date: Wed, 6 May 2026 15:58:41 -0400 Subject: [PATCH] fix(test): inspect wp-ai-client prompt slot in refinement context test ImageGenerationPromptRefinementTest::test_refine_prompt_includes_post_context_when_provided walked $captured_request['messages'] looking for the user message it had just sent. As of #1784/#1791, RequestBuilder::wpAiClientPromptContext() lifts the latest user message into wp-ai-client's $request['prompt'] slot and routes earlier turns through with_history(); refine_prompt() only sends a single user message, so it lands in 'prompt' and never appears in 'messages'. The assertion was reading an empty string against 'Article context:'. Look at 'prompt' first, fall back to user-role history messages, so the test stays correct regardless of how many user turns the caller sends. --- .../ImageGenerationPromptRefinementTest.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/tests/Unit/Abilities/ImageGenerationPromptRefinementTest.php b/tests/Unit/Abilities/ImageGenerationPromptRefinementTest.php index b5ca6c8a..eb5fa392 100644 --- a/tests/Unit/Abilities/ImageGenerationPromptRefinementTest.php +++ b/tests/Unit/Abilities/ImageGenerationPromptRefinementTest.php @@ -151,12 +151,17 @@ public function test_refine_prompt_includes_post_context_when_provided(): void { ImageGenerationAbilities::refine_prompt( 'Crane meaning', 'This article explores the spiritual symbolism of cranes in various cultures.' ); $this->assertNotNull( $captured_request ); - // Directives prepend messages, so find the last user message (our refinement request). - $user_message = ''; - foreach ( array_reverse( $captured_request['messages'] ) as $msg ) { - if ( ( $msg['role'] ?? '' ) === 'user' ) { - $user_message = $msg['content'] ?? ''; - break; + // `RequestBuilder::wpAiClientPromptContext()` lifts the latest user + // message into wp-ai-client's `prompt` slot and routes earlier turns + // through `with_history()`. Search both surfaces so the assertion + // stays true regardless of how many user messages were sent. + $user_message = (string) ( $captured_request['prompt'] ?? '' ); + if ( '' === $user_message || ! str_contains( $user_message, 'Article context:' ) ) { + foreach ( array_reverse( $captured_request['messages'] ) as $msg ) { + if ( ( $msg['role'] ?? '' ) === 'user' ) { + $user_message = $msg['content'] ?? ''; + break; + } } } $this->assertStringContainsString( 'Article context:', $user_message );