-
Notifications
You must be signed in to change notification settings - Fork 247
Expand file tree
/
Copy pathmod.rs
More file actions
80 lines (69 loc) · 2.47 KB
/
mod.rs
File metadata and controls
80 lines (69 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/// Common test utilities and centralized re-exports
///
/// This module provides:
/// 1. Setup functions for test environment initialization (signer & config)
/// 2. Centralized re-exports of commonly used mock utilities
use crate::{
get_request_signer_with_signer_key,
signer::{KoraSigner, SignerPool, SignerWithMetadata, SolanaMemorySigner},
state::{get_config, update_config, update_signer_pool},
tests::{account_mock, config_mock::ConfigMockBuilder, rpc_mock},
usage_limit::UsageTracker,
Config, KoraError,
};
use solana_sdk::{pubkey::Pubkey, signature::Keypair};
// Re-export mock utilities for centralized access
pub use account_mock::*;
pub use rpc_mock::*;
/// Setup or retrieve test signer for global state initialization
///
/// Returns the signer's public key.
pub fn setup_or_get_test_signer() -> Pubkey {
if let Ok(signer) = get_request_signer_with_signer_key(None) {
return signer.solana_pubkey();
}
let test_keypair = Keypair::new();
let signer = SolanaMemorySigner::new(test_keypair.insecure_clone());
let pool = SignerPool::new(vec![SignerWithMetadata::new(
"test_signer".to_string(),
KoraSigner::Memory(signer.clone()),
1,
)]);
match update_signer_pool(pool) {
Ok(_) => {}
Err(e) => {
panic!("Failed to update signer pool: {e}");
}
}
signer.solana_pubkey()
}
/// Setup or retrieve test config for global state initialization
///
/// Returns the config object.
pub fn setup_or_get_test_config() -> Config {
if let Ok(config) = get_config() {
return config.clone();
}
let config = ConfigMockBuilder::new().build();
match update_config(config.clone()) {
Ok(_) => config.clone(),
Err(e) => {
panic!("Failed to initialize config: {e}");
}
}
}
/// Initialize or update the global usage limiter (test only)
///
/// This function ignores "already initialized" errors for test flexibility.
/// Usage limiter initialization is optional and will not fail tests if unavailable.
pub async fn setup_or_get_test_usage_limiter() -> Result<(), KoraError> {
match UsageTracker::init_usage_limiter().await {
Ok(()) => Ok(()),
Err(KoraError::InternalServerError(ref msg)) if msg.contains("already initialized") => {
// In tests, ignore the already initialized error
// The limiter is already set up from a previous test
Ok(())
}
Err(e) => Err(e),
}
}