fix: resolve Logger access, wrong model name, and API key guard tests

- bootstrap: add PRAUTOBLOGGER_MAX_RETRIES, API_TIMEOUT, RETRY_BASE_DELAY constants
- BaseTestCase: add wp_json_encode + home_url stubs, reset Logger singleton in tearDown
- CostTrackerTest: use openai/gpt-4o (in MODEL_PRICING), fix log_api_call args,
  allow multiple wpdb inserts (Logger may also insert event_log rows)
- OpenRouterPricingTest: add wpdb mock with prefix for Logger access on unknown models
- OpenRouterProviderTest: test send_chat_completion throws RuntimeException when
  no API key is configured (matches actual guard clause behavior)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
peptiderepo
2026-04-12 08:35:01 +07:00
parent f8b4051437
commit a5d426989c
5 changed files with 72 additions and 19 deletions

View File

@@ -32,12 +32,27 @@ abstract class BaseTestCase extends TestCase {
Functions\when( 'get_transient' )->justReturn( false );
Functions\when( 'set_transient' )->justReturn( true );
Functions\when( 'delete_transient' )->justReturn( true );
// wp_json_encode is used by Logger and other classes for meta serialisation.
Functions\when( 'wp_json_encode' )->alias( 'json_encode' );
// home_url is used by OpenRouter provider in request headers.
Functions\when( 'home_url' )->justReturn( 'https://test.example.com' );
}
/**
* Tear down Brain\Monkey after each test.
*/
protected function tearDown(): void {
// Reset Logger singleton to prevent state leakage between tests.
// Logger caches its log-level threshold and holds a static instance.
if ( class_exists( 'PRAutoBlogger_Logger', false ) ) {
$ref = new \ReflectionClass( \PRAutoBlogger_Logger::class );
$prop = $ref->getProperty( 'instance' );
$prop->setAccessible( true );
$prop->setValue( null, null );
}
Monkey\tearDown();
parent::tearDown();
}