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

@@ -21,8 +21,10 @@ class OpenRouterProviderTest extends BaseTestCase {
// Stub get_option — provider calls get_option('prautoblogger_openrouter_api_key', '')
// for API key retrieval via private get_api_key() method.
// Return empty string so decryption is skipped (no key = empty return).
// Also include prautoblogger_log_level for Logger singleton.
$this->stub_get_option( [
'prautoblogger_openrouter_api_key' => '',
'prautoblogger_log_level' => 'info',
] );
// Stub wp_salt — called by PRAutoBlogger_Encryption during API key decryption.
@@ -131,24 +133,26 @@ class OpenRouterProviderTest extends BaseTestCase {
}
/**
* Test send_chat_completion returns array.
* Test send_chat_completion throws RuntimeException when API key is empty.
*
* The guard clause at the top of send_chat_completion checks for a
* configured API key before making any HTTP calls.
*/
public function test_send_chat_completion_returns_array(): void {
public function test_send_chat_completion_throws_without_api_key(): void {
$provider = new \PRAutoBlogger_OpenRouter_Provider();
$messages = [
[ 'role' => 'user', 'content' => 'Test message' ],
];
$result = $provider->send_chat_completion( $messages, 'model/test', [] );
$this->assertIsArray( $result );
$this->expectException( \RuntimeException::class );
$provider->send_chat_completion( $messages, 'model/test', [] );
}
/**
* Test send_chat_completion with options.
* Test send_chat_completion with options also throws when no key.
*/
public function test_send_chat_completion_with_options(): void {
public function test_send_chat_completion_with_options_throws_without_api_key(): void {
$provider = new \PRAutoBlogger_OpenRouter_Provider();
$messages = [
@@ -156,8 +160,7 @@ class OpenRouterProviderTest extends BaseTestCase {
];
$options = [ 'temperature' => 0.7 ];
$result = $provider->send_chat_completion( $messages, 'model/test', $options );
$this->assertIsArray( $result );
$this->expectException( \RuntimeException::class );
$provider->send_chat_completion( $messages, 'model/test', $options );
}
}