Files
khadhroony-solana-project/crates/ksp-config-lib/tests/public_api.rs
2026-09-03 16:26:18 +02:00

323 lines
21 KiB
Rust

// file: crates/ksp-config-lib/tests/public_api.rs
// version: 28
//! Integration tests for the public `ksp-config-lib` bootstrap, registry, JSON/profile/composite, environment-resolution, sensitivity,
//! Logging/Transport/Store adapters and management contracts.
#[test]
fn bootstrap_contract_is_available_from_crate_root() {
let result = ksp_config_lib::ConfigBootstrapOptions::defaults();
assert!(result.is_ok(), "default bootstrap options should be available: {result:?}");
if let std::result::Result::Ok(options) = result {
assert_eq!(options.cfg_path(), std::path::Path::new(ksp_config_lib::DEFAULT_CFG_PATH));
assert_eq!(options.schema_path(), std::path::Path::new(ksp_config_lib::DEFAULT_SCHEMA_PATH));
assert_eq!(ksp_config_lib::ARG_CFG_PATH, "--cfgpath");
assert_eq!(ksp_config_lib::ARG_SCHEMA_PATH, "--schemapath");
}
}
#[test]
fn programmatic_bootstrap_paths_are_independent() {
let result = ksp_config_lib::ConfigBootstrapOptions::from_paths("runtime-config", "runtime-schemas");
assert!(result.is_ok(), "programmatic bootstrap paths should be valid: {result:?}");
if let std::result::Result::Ok(options) = result {
assert_eq!(options.cfg_path(), std::path::Path::new("runtime-config"));
assert_eq!(options.schema_path(), std::path::Path::new("runtime-schemas"));
}
}
#[test]
fn cli_bootstrap_parser_is_available_from_crate_root() {
let args = [
std::ffi::OsString::from("consumer"),
std::ffi::OsString::from("--cfgpath=consumer-config"),
std::ffi::OsString::from("--schemapath"),
std::ffi::OsString::from("consumer-schemas"),
];
let result = ksp_config_lib::ConfigBootstrapOptions::from_args(&args);
assert!(result.is_ok(), "public bootstrap parser should accept KSP path arguments: {result:?}");
if let std::result::Result::Ok(options) = result {
assert_eq!(options.cfg_path(), std::path::Path::new("consumer-config"));
assert_eq!(options.schema_path(), std::path::Path::new("consumer-schemas"));
}
}
#[test]
fn logical_file_registry_is_available_from_crate_root() {
let args = [
std::ffi::OsString::from("consumer"),
std::ffi::OsString::from("--filemap=cfg.std.logging=consumer.logging.json"),
std::ffi::OsString::from("--filemap=schema.std.logging=consumer.logging.schema.json"),
];
let registry = ksp_config_lib::ConfigFileRegistry::from_args(&args);
let bootstrap = ksp_config_lib::ConfigBootstrapOptions::from_paths("consumer-config", "consumer-schemas");
let logging_id = ksp_config_lib::ConfigFileId::new(ksp_config_lib::FILE_ID_STD_LOGGING);
let schema_id = ksp_config_lib::ConfigFileId::new(ksp_config_lib::FILE_ID_SCHEMA_STD_LOGGING);
assert!(registry.is_ok(), "public registry should accept known filemap overrides: {registry:?}");
assert!(bootstrap.is_ok(), "bootstrap paths should remain available: {bootstrap:?}");
assert!(logging_id.is_ok(), "public logging file_id should be valid: {logging_id:?}");
assert!(schema_id.is_ok(), "public schema file_id should be valid: {schema_id:?}");
if let (std::result::Result::Ok(registry), std::result::Result::Ok(bootstrap), std::result::Result::Ok(logging_id), std::result::Result::Ok(schema_id)) =
(registry, bootstrap, logging_id, schema_id)
{
let logging = registry.resolve_path(&bootstrap, &logging_id);
let schema = registry.resolve_path(&bootstrap, &schema_id);
assert!(logging.is_ok(), "public logging path should resolve: {logging:?}");
assert!(schema.is_ok(), "public schema path should resolve: {schema:?}");
if let std::result::Result::Ok(logging) = logging {
assert_eq!(logging, std::path::PathBuf::from("consumer-config/consumer.logging.json"));
}
if let std::result::Result::Ok(schema) = schema {
assert_eq!(schema, std::path::PathBuf::from("consumer-schemas/consumer.logging.schema.json"));
}
assert_eq!(ksp_config_lib::ARG_FILE_MAP, "--filemap");
}
}
#[test]
fn registry_descriptor_inventory_is_available_from_crate_root() {
let registry = ksp_config_lib::ConfigFileRegistry::defaults();
assert!(registry.is_ok(), "public registry should remain constructible: {registry:?}");
if let std::result::Result::Ok(registry) = registry {
let descriptors: std::vec::Vec<&ksp_config_lib::ConfigFileDescriptor> = registry.descriptors().collect();
assert_eq!(descriptors.len(), 15);
assert_eq!(descriptors[0].file_id().as_str(), ksp_config_lib::FILE_ID_COMPOSITE_KSP_APP_BACKFILL_DESK);
assert_eq!(descriptors[1].file_id().as_str(), ksp_config_lib::FILE_ID_COMPOSITE_KSP_APP_SOLPRICES_DESK);
assert_eq!(descriptors[2].file_id().as_str(), ksp_config_lib::FILE_ID_COMPOSITE_KSP_APP_STORE_DESK);
assert_eq!(descriptors[3].file_id().as_str(), ksp_config_lib::FILE_ID_COMPOSITE_KSP_APP_WALLET_DESK);
assert_eq!(descriptors[4].file_id().as_str(), ksp_config_lib::FILE_ID_STD_LOGGING);
assert_eq!(descriptors[5].file_id().as_str(), ksp_config_lib::FILE_ID_STD_OFFCHAIN_TRANSPORT);
assert_eq!(descriptors[6].file_id().as_str(), ksp_config_lib::FILE_ID_STD_STORE);
assert_eq!(descriptors[7].file_id().as_str(), ksp_config_lib::FILE_ID_STD_TRANSPORT);
assert_eq!(descriptors[8].file_id().as_str(), ksp_config_lib::FILE_ID_STD_WALLET);
assert_eq!(descriptors[9].file_id().as_str(), ksp_config_lib::FILE_ID_SCHEMA_COMPOSITE);
assert_eq!(descriptors[10].file_id().as_str(), ksp_config_lib::FILE_ID_SCHEMA_STD_LOGGING);
assert_eq!(descriptors[11].file_id().as_str(), ksp_config_lib::FILE_ID_SCHEMA_STD_OFFCHAIN_TRANSPORT);
assert_eq!(descriptors[12].file_id().as_str(), ksp_config_lib::FILE_ID_SCHEMA_STD_STORE);
assert_eq!(descriptors[13].file_id().as_str(), ksp_config_lib::FILE_ID_SCHEMA_STD_TRANSPORT);
assert_eq!(descriptors[14].file_id().as_str(), ksp_config_lib::FILE_ID_SCHEMA_STD_WALLET);
let schema_file_id = descriptors[8].schema_file_id();
assert!(schema_file_id.is_some(), "public Wallet descriptor should preserve schema association");
if let std::option::Option::Some(schema_file_id) = schema_file_id {
assert_eq!(schema_file_id.as_str(), ksp_config_lib::FILE_ID_SCHEMA_STD_WALLET);
}
}
}
#[test]
fn validated_json_document_engine_is_available_from_crate_root() {
let workspace = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..");
let bootstrap = ksp_config_lib::ConfigBootstrapOptions::from_paths(workspace.join("config"), workspace.join("config/schemas"));
let registry = ksp_config_lib::ConfigFileRegistry::defaults();
let file_id = ksp_config_lib::ConfigFileId::new(ksp_config_lib::FILE_ID_STD_LOGGING);
assert!(bootstrap.is_ok(), "public bootstrap should accept committed Config roots: {bootstrap:?}");
assert!(registry.is_ok(), "public registry should remain constructible: {registry:?}");
assert!(file_id.is_ok(), "public Logging file_id should remain constructible: {file_id:?}");
if let (std::result::Result::Ok(bootstrap), std::result::Result::Ok(registry), std::result::Result::Ok(file_id)) = (bootstrap, registry, file_id) {
let engine = ksp_config_lib::ConfigDocumentEngine::new(bootstrap, registry);
let document = engine.load_validated_document(&file_id);
assert!(document.is_ok(), "public document engine should validate committed Logging configuration: {document:?}");
if let std::result::Result::Ok(document) = document {
assert_eq!(document.file_id(), &file_id);
assert_eq!(document.value().get("format_version"), std::option::Option::Some(&serde_json::Value::from(1)));
}
}
}
#[test]
fn resolved_profile_contract_is_available_from_crate_root() {
let workspace = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..");
let bootstrap = ksp_config_lib::ConfigBootstrapOptions::from_paths(workspace.join("config"), workspace.join("config/schemas"));
let registry = ksp_config_lib::ConfigFileRegistry::defaults();
let file_id = ksp_config_lib::ConfigFileId::new(ksp_config_lib::FILE_ID_STD_LOGGING);
assert!(bootstrap.is_ok(), "public bootstrap should accept committed Config roots: {bootstrap:?}");
assert!(registry.is_ok(), "public registry should remain constructible: {registry:?}");
assert!(file_id.is_ok(), "public Logging file_id should remain constructible: {file_id:?}");
if let (std::result::Result::Ok(bootstrap), std::result::Result::Ok(registry), std::result::Result::Ok(file_id)) = (bootstrap, registry, file_id) {
let engine = ksp_config_lib::ConfigDocumentEngine::new(bootstrap, registry);
let document = engine.load_validated_document(&file_id);
let resolved = engine.load_resolved_profile(&file_id, std::option::Option::None);
assert!(document.is_ok(), "public profile resolver should read committed source: {document:?}");
assert!(resolved.is_ok(), "public profile resolver should resolve committed default profile: {resolved:?}");
if let (std::result::Result::Ok(document), std::result::Result::Ok(resolved)) = (document, resolved) {
let default_profile = document.value().get("default_profile").and_then(serde_json::Value::as_str);
assert_eq!(default_profile, std::option::Option::Some(resolved.profile_id()));
assert_eq!(resolved.selection_source(), ksp_config_lib::ConfigProfileSelectionSource::DefaultProfile);
assert_eq!(resolved.origin("logs_directory"), std::option::Option::Some(ksp_config_lib::ConfigValueOrigin::Global));
assert_eq!(resolved.origin("default_filter"), std::option::Option::Some(ksp_config_lib::ConfigValueOrigin::Profile));
assert!(!resolved.effective().contains_key("default_profile"));
assert!(!resolved.effective().contains_key("profiles"));
}
}
}
#[test]
fn composite_schema_and_provenance_contracts_are_available_from_crate_root() {
let registry = ksp_config_lib::ConfigFileRegistry::defaults();
let schema_id = ksp_config_lib::ConfigFileId::new(ksp_config_lib::FILE_ID_SCHEMA_COMPOSITE);
assert!(registry.is_ok(), "public registry should remain constructible: {registry:?}");
assert!(schema_id.is_ok(), "public composite schema file_id should be valid: {schema_id:?}");
if let (std::result::Result::Ok(registry), std::result::Result::Ok(schema_id)) = (registry, schema_id) {
let descriptor = registry.descriptor(&schema_id);
assert!(descriptor.is_ok(), "public composite schema descriptor should exist: {descriptor:?}");
if let std::result::Result::Ok(descriptor) = descriptor {
assert_eq!(descriptor.filename(), std::path::Path::new(ksp_config_lib::DEFAULT_COMPOSITE_SCHEMA_FILENAME));
assert_eq!(descriptor.kind(), ksp_config_lib::ConfigFileKind::Schema);
}
}
assert_eq!(ksp_config_lib::FILE_ID_COMPOSITE_KSP_APP_BACKFILL_DESK, "cfg.composite.ksp-app-backfill-desk");
assert_eq!(ksp_config_lib::DEFAULT_COMPOSITE_KSP_APP_BACKFILL_DESK_FILENAME, "composite.ksp-app-backfill-desk.json");
assert_eq!(ksp_config_lib::FILE_ID_COMPOSITE_KSP_APP_SOLPRICES_DESK, "cfg.composite.ksp-app-solprices-desk");
assert_eq!(ksp_config_lib::DEFAULT_COMPOSITE_KSP_APP_SOLPRICES_DESK_FILENAME, "composite.ksp-app-solprices-desk.json");
assert_eq!(ksp_config_lib::FILE_ID_COMPOSITE_KSP_APP_STORE_DESK, "cfg.composite.ksp-app-store-desk");
assert_eq!(ksp_config_lib::DEFAULT_COMPOSITE_KSP_APP_STORE_DESK_FILENAME, "composite.ksp-app-store-desk.json");
assert_ne!(ksp_config_lib::ConfigProfileSelectionSource::Composite, ksp_config_lib::ConfigProfileSelectionSource::Explicit);
}
#[test]
fn environment_resolution_contract_is_available_from_crate_root() {
let loader: fn() -> ksp_core_lib::Result<ksp_config_lib::ConfigEnvironment> = ksp_config_lib::ConfigEnvironment::load;
let _ = loader;
assert_eq!(ksp_config_lib::DEFAULT_DOTENV_PATH, ".env");
assert_eq!(ksp_config_lib::DEFAULT_DOTENV_EXAMPLE_PATH, ".env.example");
assert_ne!(ksp_config_lib::ConfigEnvironmentSource::Process, ksp_config_lib::ConfigEnvironmentSource::DotEnv);
assert_ne!(ksp_config_lib::ConfigEnvironmentSource::DotEnv, ksp_config_lib::ConfigEnvironmentSource::Fallback);
}
#[test]
fn sensitivity_and_safe_resolution_contracts_are_available_from_crate_root() {
assert_eq!(
ksp_config_lib::ConfigSensitivity::from_variable_name("KSP_PUBLIC_HOST").ok(),
std::option::Option::Some(ksp_config_lib::ConfigSensitivity::Public),
);
assert_eq!(
ksp_config_lib::ConfigSensitivity::from_variable_name("KSP_MODE").ok(),
std::option::Option::Some(ksp_config_lib::ConfigSensitivity::Internal)
);
assert_eq!(
ksp_config_lib::ConfigSensitivity::from_variable_name("KSP_SECRET_TOKEN").ok(),
std::option::Option::Some(ksp_config_lib::ConfigSensitivity::Secret),
);
assert_eq!(ksp_config_lib::REDACTED_CONFIG_VALUE, "********");
let detailed_text: fn(&ksp_config_lib::ConfigEnvironment, &str) -> ksp_core_lib::Result<ksp_config_lib::ResolvedConfigText> =
ksp_config_lib::ConfigEnvironment::resolve_text_detailed;
let detailed_json: fn(&ksp_config_lib::ConfigEnvironment, &serde_json::Value) -> ksp_core_lib::Result<ksp_config_lib::ResolvedConfigJson> =
ksp_config_lib::ConfigEnvironment::resolve_json_detailed;
let _ = (detailed_text, detailed_json);
let provenance = ksp_config_lib::ConfigValueProvenance::EnvironmentFallback { variable_name: "KSP_SECRET_TOKEN".to_owned() };
assert_eq!(provenance.variable_name(), std::option::Option::Some("KSP_SECRET_TOKEN"));
assert_eq!(provenance.environment_source(), std::option::Option::Some(ksp_config_lib::ConfigEnvironmentSource::Fallback));
}
#[test]
fn logging_adapter_contract_is_available_from_crate_root() {
let adapter = ksp_config_lib::ConfigDocumentEngine::load_resolved_logging_config;
let composite_adapter = ksp_config_lib::ConfigDocumentEngine::resolve_logging_config_profile;
let _ = (adapter, composite_adapter);
assert_eq!(ksp_config_lib::ERROR_CODE_EFFECTIVE_CONFIG_INVALID.domain(), "config");
assert_eq!(ksp_config_lib::ERROR_CODE_EFFECTIVE_CONFIG_INVALID.code(), "effective_config_invalid");
assert!(std::mem::size_of::<ksp_config_lib::ResolvedLoggingConfig>() > 0);
}
#[test]
fn store_adapter_contract_is_available_from_crate_root() {
let adapter = ksp_config_lib::ConfigDocumentEngine::load_resolved_store_config;
let composite_adapter = ksp_config_lib::ConfigDocumentEngine::resolve_store_config_profile;
let _ = (adapter, composite_adapter);
assert_eq!(ksp_config_lib::FILE_ID_STD_STORE, "cfg.std.store");
assert_eq!(ksp_config_lib::FILE_ID_SCHEMA_STD_STORE, "schema.std.store");
assert_eq!(ksp_config_lib::DEFAULT_STD_STORE_FILENAME, "std.store.json");
assert_eq!(ksp_config_lib::DEFAULT_STD_STORE_SCHEMA_FILENAME, "std.store.schema.json");
assert!(std::mem::size_of::<ksp_config_lib::ResolvedStoreConfig>() > 0);
}
#[test]
fn management_contracts_are_available_from_crate_root() {
let workspace = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..");
let bootstrap = ksp_config_lib::ConfigBootstrapOptions::from_paths(workspace.join("config"), workspace.join("config/schemas"));
let registry = ksp_config_lib::ConfigFileRegistry::defaults();
assert!(bootstrap.is_ok(), "public bootstrap should accept committed Config roots: {bootstrap:?}");
assert!(registry.is_ok(), "public registry should remain constructible: {registry:?}");
if let (std::result::Result::Ok(bootstrap), std::result::Result::Ok(registry)) = (bootstrap, registry) {
let engine = ksp_config_lib::ConfigDocumentEngine::new(bootstrap, registry);
let management = ksp_config_lib::ConfigManagement::new(engine);
let logging = management.load_logging_document();
assert!(logging.is_ok(), "public typed Logging management contract should load committed source: {logging:?}");
if let std::result::Result::Ok(mut logging) = logging {
assert_eq!(logging.format_version(), 1);
let default_profile = logging.default_profile().to_owned();
assert!(!default_profile.is_empty());
assert!(!logging.profiles().is_empty());
assert!(logging.profiles().iter().any(|profile| return profile.profile_id() == default_profile.as_str()));
logging.set_logs_directory("public-api-management-test");
assert_eq!(logging.logs_directory(), "public-api-management-test");
if let std::option::Option::Some(profile) = logging.profiles_mut().first_mut()
&& let std::option::Option::Some(file) = profile.files_mut().first_mut()
{
file.set_ansi(false);
assert!(!file.ansi());
}
}
}
let save_source_candidate: fn(
&ksp_config_lib::ConfigManagement,
&ksp_config_lib::ConfigFileId,
&str,
) -> ksp_core_lib::Result<ksp_config_lib::ConfigDocumentChangeReport> = ksp_config_lib::ConfigManagement::save_source_candidate;
let reveal_effective: fn(&ksp_config_lib::ConfigManagement, &str) -> ksp_core_lib::Result<std::option::Option<String>> =
ksp_config_lib::ConfigManagement::reveal_effective_environment_value;
let reveal_dotenv: fn(&ksp_config_lib::ConfigManagement, &str) -> ksp_core_lib::Result<std::option::Option<String>> =
ksp_config_lib::ConfigManagement::reveal_dotenv_value;
let _ = (save_source_candidate, reveal_effective, reveal_dotenv);
assert_ne!(ksp_config_lib::ERROR_CODE_MANAGEMENT_OPERATION_INVALID, ksp_config_lib::ERROR_CODE_PERSISTENCE_WRITE_FAILED);
}
#[test]
fn transport_adapter_contract_is_available_from_crate_root() {
let _loader = ksp_config_lib::ConfigDocumentEngine::load_resolved_transport_config;
let _composite_loader = ksp_config_lib::ConfigDocumentEngine::resolve_transport_config_profile;
assert!(std::mem::size_of::<ksp_config_lib::ResolvedTransportConfig>() > 0);
let _http_settings = ksp_config_lib::ResolvedTransportConfig::http_settings;
let _ws_settings = ksp_config_lib::ResolvedTransportConfig::ws_settings;
let _grpc_settings = ksp_config_lib::ResolvedTransportConfig::grpc_settings;
let _into_transport_settings = ksp_config_lib::ResolvedTransportConfig::into_transport_settings;
let _into_all_transport_settings = ksp_config_lib::ResolvedTransportConfig::into_all_transport_settings;
assert_eq!(ksp_config_lib::FILE_ID_STD_TRANSPORT, "cfg.std.transport");
assert_eq!(ksp_config_lib::FILE_ID_SCHEMA_STD_TRANSPORT, "schema.std.transport");
assert_eq!(ksp_config_lib::DEFAULT_STD_TRANSPORT_FILENAME, "std.transport.json");
assert_eq!(ksp_config_lib::DEFAULT_STD_TRANSPORT_SCHEMA_FILENAME, "std.transport.schema.json");
}
#[test]
fn wallet_adapter_contract_is_available_from_crate_root() {
let _loader = ksp_config_lib::ConfigDocumentEngine::load_resolved_wallet_config;
let _composite_loader = ksp_config_lib::ConfigDocumentEngine::resolve_wallet_config_profile;
assert!(std::mem::size_of::<ksp_config_lib::ResolvedWalletConfig>() > 0);
assert_eq!(ksp_config_lib::FILE_ID_COMPOSITE_KSP_APP_WALLET_DESK, "cfg.composite.ksp-app-wallet-desk");
assert_eq!(ksp_config_lib::FILE_ID_STD_WALLET, "cfg.std.wallet");
assert_eq!(ksp_config_lib::FILE_ID_SCHEMA_STD_WALLET, "schema.std.wallet");
assert_eq!(ksp_config_lib::DEFAULT_COMPOSITE_KSP_APP_WALLET_DESK_FILENAME, "composite.ksp-app-wallet-desk.json");
assert_eq!(ksp_config_lib::DEFAULT_STD_WALLET_FILENAME, "std.wallet.json");
assert_eq!(ksp_config_lib::DEFAULT_STD_WALLET_SCHEMA_FILENAME, "std.wallet.schema.json");
}
#[test]
fn offchain_transport_adapter_contract_is_available_from_crate_root() {
let _loader = ksp_config_lib::ConfigDocumentEngine::load_resolved_offchain_transport_config;
let _composite_loader = ksp_config_lib::ConfigDocumentEngine::resolve_offchain_transport_config_profile;
assert!(std::mem::size_of::<ksp_config_lib::ResolvedOffchainTransportConfig>() > 0);
let _service = ksp_config_lib::ResolvedOffchainTransportConfig::service;
assert_eq!(ksp_config_lib::FILE_ID_STD_OFFCHAIN_TRANSPORT, "cfg.std.offchain_transport");
assert_eq!(ksp_config_lib::FILE_ID_SCHEMA_STD_OFFCHAIN_TRANSPORT, "schema.std.offchain_transport");
assert_eq!(ksp_config_lib::DEFAULT_STD_OFFCHAIN_TRANSPORT_FILENAME, "std.offchain_transport.json");
assert_eq!(ksp_config_lib::DEFAULT_STD_OFFCHAIN_TRANSPORT_SCHEMA_FILENAME, "std.offchain_transport.schema.json");
}
#[test]
fn packaged_runtime_layout_contract_is_available_from_crate_root() {
let prepare: fn(&std::path::Path) -> ksp_core_lib::Result<ksp_config_lib::PackagedRuntimeLayout> = ksp_config_lib::prepare_packaged_runtime;
let _ = prepare;
assert_eq!(ksp_config_lib::ERROR_CODE_PACKAGED_RUNTIME_PREPARATION_FAILED.domain(), "config");
assert_eq!(ksp_config_lib::ERROR_CODE_PACKAGED_RUNTIME_PREPARATION_FAILED.code(), "packaged_runtime_preparation_failed");
}