fix: correct happy-path sync test — taskType, WP response helpers, shared store

Three bugs in test_sync_success_caches_and_returns_models:
1. Mock data lacked taskType=imageInference so normalize_models() filtered
   everything out → sync() returned false (empty result).
2. wp_remote_retrieve_response_code and wp_remote_retrieve_body not mocked
   → Brain\Monkey MissingFunctionExpectations.
3. is_wp_error not mocked.
4. get_option/update_option used separate static stores so update_option
   write was invisible to the subsequent get_option in get_models().
This commit is contained in:
peptiderepo
2026-04-26 04:50:02 +00:00
parent 30282035b5
commit d9317ff567

View File

@@ -125,52 +125,61 @@ class RunwareModelCatalogTest extends BaseTestCase {
/** Happy path: successful sync caches models and get_models returns them. */
public function test_sync_success_caches_and_returns_models(): void {
// Mock a valid Runware API response.
Functions\when( 'wp_remote_post' )->justReturn(
// The mocked API response body — models must have taskType=imageInference
// so normalize_models() doesn't filter them out.
$fake_body = json_encode(
array(
'response' => array( 'code' => 200 ),
'body' => wp_json_encode(
'data' => array(
array(
'data' => array(
array(
'id' => 'test-model-1',
'name' => 'Test Model 1',
'cost' => 0.001,
'provider' => 'runware',
),
array(
'id' => 'test-model-2',
'name' => 'Test Model 2',
'cost' => 0.002,
'provider' => 'runware',
),
),
)
'id' => 'test-model-1',
'name' => 'Test Model 1',
'taskType' => 'imageInference',
),
array(
'id' => 'test-model-2',
'name' => 'Test Model 2',
'taskType' => 'imageInference',
),
),
)
);
// Mock the API key to exist.
Functions\when( 'get_option' )->alias( function ( $key, $default = false ) {
static $options = array();
if ( 'prautoblogger_runware_api_key' === $key ) {
return 'test-api-key';
$fake_response = array(
'response' => array( 'code' => 200 ),
'body' => $fake_body,
);
Functions\when( 'wp_remote_post' )->justReturn( $fake_response );
// Alias the WP response-parsing helpers so they work against our array.
Functions\when( 'wp_remote_retrieve_response_code' )->alias(
function ( $r ) { return (int) ( $r['response']['code'] ?? 0 ); }
);
Functions\when( 'wp_remote_retrieve_body' )->alias(
function ( $r ) { return (string) ( $r['body'] ?? '' ); }
);
Functions\when( 'is_wp_error' )->justReturn( false );
// Shared options store so get_option/update_option stay in sync
// within this test. The API key must be non-empty for sync() to proceed.
$store = array( 'prautoblogger_runware_api_key' => 'test-api-key-plain' );
Functions\when( 'get_option' )->alias(
function ( $key, $default = false ) use ( &$store ) {
return $store[ $key ] ?? $default;
}
return $options[ $key ] ?? $default;
} );
);
Functions\when( 'update_option' )->alias(
function ( $key, $value ) use ( &$store ) {
$store[ $key ] = $value;
return true;
}
);
Functions\when( 'update_option' )->alias( function ( $key, $value ) {
static $options = array();
$options[ $key ] = $value;
return true;
} );
// Perform the sync.
// Sync should succeed: API key present, HTTP 200, 2 imageInference models.
$catalog = new \PRAutoBlogger_Runware_Model_Catalog();
$result = $catalog->sync();
$this->assertTrue( $result, 'Sync should return true on success' );
$this->assertTrue( $result, 'Sync should return true on HTTP 200 with valid models' );
// Verify get_models returns the synced models (not fallback).
// get_models() should now return the synced list from cache.
$models = $catalog->get_models();
$this->assertIsArray( $models );
$this->assertCount( 2, $models );