Repository navigation
test(node): cover resolve_config three-layer merge and invariants - #365
Conversation
Add 31 unit tests inside `crates/node/src/config/mod.rs::tests` covering the previously untested merge precedence, default-fallback, tilde expansion, required-field, and cross-section invariant paths exercised by `resolve_config` and its per-section helpers. Cases added: - CLI > file precedence for every helper (identity.region, network.bind_port, blockchain.rpc_url, cache.cache_dir [+tilde], cache size knobs, payment.rate_per_mb, observability.log_level/ metrics_port). - File-only fallback for each helper. - Default constants (`DEFAULT_BIND_PORT`, `DEFAULT_CACHE_SIZE_MB`, `DEFAULT_MAX_BLOB_SIZE_MB`, `DEFAULT_RATE_PER_MB`, `DEFAULT_METRICS_PORT`, `DEFAULT_ADMIN_PORT`, `DEFAULT_ANNOUNCE_INTERVAL_SEC`, `DEFAULT_PEER_TTL_SEC`). - `cache.cache_dir` tilde expansion fires through CLI override. - `load_file_config` happy path, explicit-missing-error, bad-TOML-error, and the implicit-None branch when no default config exists. - Required-field-missing diagnostics for `rpc_url`, `payment_channel_address`, and `staking_registry_address`. - End-to-end `resolve_config` runs covering the global/region invariant, port-collision invariant, and origin_url/origin_path mutual exclusion. The integration test at `crates/node/tests/config_validate.rs` is left untouched. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> Closes #217
There was a problem hiding this comment.
Code Review
This pull request introduces a comprehensive suite of unit tests for the configuration resolution logic in crates/node/src/config/mod.rs, covering the merging of CLI arguments, file-based configurations, and default values across various modules. Feedback was provided regarding a test that relies on the local filesystem state (the user's home directory), which may lead to non-deterministic results across different environments; isolating these tests using temporary directories is recommended.
There was a problem hiding this comment.
Pull request overview
Adds unit-test coverage for the node config resolver (resolve_config) and its per-section resolve_* helpers, focusing on three-layer precedence (CLI > file > defaults) and key invariants/errors in the startup config path (issue #217).
Changes:
- Introduces helper constructors for “empty” CLI args and full
RunArgsassembly insideconfig::tests. - Adds extensive unit tests covering CLI-vs-file precedence, default fallbacks, required-field errors, and end-to-end
resolve_configinvariants. - Adds targeted tests for
load_file_configexplicit-path parsing/error branches and the implicit default-path fallback behavior.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Resolves conflicts with PRs #363/#364/#365/#366 that landed on main while this branch was in review. Conflict resolution: - crates/node/Cargo.toml: keep both new dev-deps (nix from this branch for SIGHUP integration tests, wiremock from #366 for the rpc-watchdog test). - crates/node/src/runtime/mod.rs: union the test-module clippy allows ([unwrap_used, expect_used, panic, indexing_slicing]); keep both new test bodies (rpc_watchdog_tracks_endpoint_transitions from #366 plus shutdown_streams_recv_resolves_on_sigterm from this branch). - Test fixtures across config/mod.rs, runtime/reload.rs, and tests/sighup_signal.rs gained the rpc_watchdog_interval_sec field (Option::None for FileConfig literals; literal 30u64 for ResolvedBlockchain). Verified post-merge: cargo fmt --check, cargo clippy --workspace --all-targets -- -D warnings, cargo nextest run -p decdn-node (179/179), RUSTDOCFLAGS=-D warnings cargo doc --workspace --no-deps --document-private-items, all clean. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Summary
Add 31 unit tests inside
crates/node/src/config/mod.rs::testscoveringthe previously untested paths through
resolve_configand its per-sectionhelpers (
resolve_identity,resolve_network,resolve_blockchain,resolve_cache,resolve_payment,resolve_observability,resolve_gossip). The integration test atcrates/node/tests/config_validate.rsis left untouched.Closes #217.
Cases covered
resolve_identity_cli_region_overrides_fileresolve_network_cli_bind_port_overrides_fileresolve_blockchain_cli_rpc_url_overrides_fileresolve_cache_cli_cache_dir_overrides_file_and_expands_tilde(alsoasserts tilde expansion fires)
resolve_cache_cli_size_overrides_file_sizeresolve_payment_cli_rate_overrides_file_rateresolve_observability_cli_log_level_overrides_fileresolve_observability_cli_metrics_port_overrides_fileresolve_identity_falls_back_to_file_when_cli_absentresolve_network_uses_file_when_cli_absentresolve_blockchain_uses_file_rpc_url_when_cli_absentresolve_cache_uses_file_cache_dir_when_cli_absent/resolve_cache_uses_file_size_when_cli_absentresolve_payment_uses_file_when_cli_absentresolve_observability_uses_file_log_level_when_cli_absentconstant (
DEFAULT_BIND_PORT,DEFAULT_CACHE_SIZE_MB,DEFAULT_MAX_BLOB_SIZE_MB,DEFAULT_RATE_PER_MB,DEFAULT_METRICS_PORT,DEFAULT_ADMIN_PORT,DEFAULT_ANNOUNCE_INTERVAL_SEC,DEFAULT_PEER_TTL_SEC).cache.cache_dir(inresolve_cache_cli_cache_dir_overrides_file_and_expands_tilde).load_file_config_returns_default_when_path_is_none_and_default_absentasserts
FileConfig::default()is returned when the documenteddefault location does not exist. Mutating
$HOMEis not safe underedition 2024 + workspace
unsafe_codelints, so the test eprintln-skips with a clear message if the local default file happens to
exist.
load_file_config_errors_when_explicit_path_missing,load_file_config_reads_and_parses_explicit_path, andload_file_config_errors_on_bad_tomlcover the explicit-path branches.resolve_blockchain_errors_when_rpc_url_missing,_when_payment_channel_address_missing,_when_staking_registry_address_missing, plus_treats_empty_string_rpc_url_as_missingto lock the.filter(|s| !s.is_empty())guard.resolve_config_errors_when_subscribe_global_set_without_regiondrives
ensure_region_when_publishing_globalend-to-end.resolve_config_errors_when_bind_port_equals_metrics_portdrivesvalidate_port_layoutend-to-end. The full per-port matrix(bind=metrics, bind=admin, metrics=admin, both-zero, etc.) is
already covered at the helper level by pre-existing tests.
resolve_config_errors_when_origin_url_and_path_both_setconfirmsthe resolver enforces the
(Some, Some)rejection at startup, notonly later in
runtime/mod.rs.End-to-end happy path is exercised by
resolve_config_end_to_end_three_layer_merge, which writes a TOMLfixture, sets
--bind-porton the CLI, and asserts the merged resultshows CLI > file > default for representative fields across all seven
sections.
Test plan
cargo fmt -- --checkcargo clippy --workspace --all-targets -- -D warningscargo nextest run -p decdn-node config::tests— 103 passedcargo nextest run -p decdn-node— 165 passed, 0 skippedpre-commithooks (rustfmt + clippy + cargo-deny) all pass