portfolio_rs/tests/e2e_network.rs
Markus Zoppelt 085d7537c6 test(services): cover quotes, quote cache, atomic writes, loader and HTTP API
Bring services/{quotes,quote_cache,atomic_write,persistence,
portfolio_loader}.rs, api/mod.rs and commands/api.rs to 100% line
coverage, offline and deterministic:

- quotes: historic-close windows against a recording bar source,
  AnyProvider/Base dispatch with a stand-in Yahoo backend, fixture,
  offline and cached providers, flush_cache success and failure.
- quote_cache: invalid file, accessors, flush only when dirty,
  close_before/name/last-known paths, poisoned-lock recovery.
- atomic_write: stat/create/rename failures, temp-name collisions
  (sequence is now injectable), private writes, parent_dir.
- persistence: reopen, symlinks, uncreatable directory.
- portfolio_loader: fake gpg program (decrypts in memory), a scripted
  provider for network status, panicked lookups, cached FX and every
  way an excluded position is sized. The network-only
  create_live_portfolio test moves to tests/e2e_network.rs (ignored).
- api: serve on 127.0.0.1:0 with a token and graceful shutdown, busy
  port, non-loopback warning, bad policy; remaining endpoints and the
  'portfolio_rs api' command in tests/cov_services_api.rs.

Small restructures for testability, behavior unchanged: serve_until
takes a boxed shutdown (run_server passes serve_forever); error
contexts that cannot fail are formatted eagerly; the missing-price
warning loop is flattened.
2026-11-03 09:39:48 +01:00

277 lines
8 KiB
Rust

//! End-to-end tests that require network access to Yahoo Finance.
//!
//! All tests are `#[ignore]`d; run them explicitly with:
//! `cargo test --test e2e_network -- --ignored`
use assert_cmd::Command;
use predicates::str;
use serde_json::Value;
use tempfile::TempDir;
fn bin() -> Command {
Command::cargo_bin("portfolio_rs").unwrap()
}
fn example_data() -> String {
concat!(env!("CARGO_MANIFEST_DIR"), "/example_data.json").to_string()
}
/// Write a broad test policy into a fresh temp dir and return (guard, path).
fn test_policy() -> (TempDir, String) {
let tmp = TempDir::new().unwrap();
let policy_toml = r#"
version = "1.0"
name = "E2E Test Policy"
base_currency = "EUR"
time_horizon_years = 10
risk_profile = "moderate"
[constraints]
minimum_cash_amount = 1000.0
single_position_limit_percent = 80.0
[[allocations]]
asset_class = "Stocks"
target_percent = 40.0
tolerance_percent = 10.0
[[allocations]]
asset_class = "Crypto"
target_percent = 20.0
tolerance_percent = 10.0
[[allocations]]
asset_class = "Cash"
target_percent = 20.0
tolerance_percent = 10.0
[[allocations]]
asset_class = "Bonds"
target_percent = 10.0
tolerance_percent = 5.0
[[allocations]]
asset_class = "Gold"
target_percent = 5.0
tolerance_percent = 3.0
[[allocations]]
asset_class = "Commodities"
target_percent = 5.0
tolerance_percent = 3.0
"#;
let path = tmp.path().join("policy.toml");
std::fs::write(&path, policy_toml).unwrap();
let path = path.to_str().unwrap().to_string();
(tmp, path)
}
#[test]
#[ignore = "requires network access to Yahoo Finance"]
fn test_context_json_output_structure() {
let mut cmd = bin();
cmd.args(["context", &example_data(), "--format", "json"]);
let assert = cmd.assert().success();
let stdout = String::from_utf8_lossy(&assert.get_output().stdout);
let parsed: Value = serde_json::from_str(&stdout).unwrap();
assert!(parsed.get("generatedAt").is_some());
assert!(parsed.get("currency").is_some());
assert!(parsed.get("networkStatus").is_some());
assert!(parsed.get("summary").is_some());
assert!(parsed.get("allocation").is_some());
assert!(parsed.get("positions").is_some());
assert!(parsed.get("riskFlags").is_some());
assert!(parsed.get("dataQualityFlags").is_some());
assert!(parsed.get("followUpCommands").is_some());
let summary = parsed.get("summary").unwrap();
assert!(summary.get("totalValue").is_some());
assert!(summary.get("cashValue").is_some());
assert!(summary.get("positionCount").is_some());
let positions = parsed.get("positions").unwrap().as_array().unwrap();
assert!(!positions.is_empty());
let first = &positions[0];
assert!(first.get("name").is_some());
assert!(first.get("value").is_some());
assert!(first.get("weightPercent").is_some());
}
#[test]
#[ignore = "requires network access to Yahoo Finance"]
fn test_context_markdown_output() {
let mut cmd = bin();
cmd.args(["context", &example_data(), "--format", "markdown"]);
cmd.assert()
.success()
.stdout(str::contains("# Portfolio Context"))
.stdout(str::contains("## Summary"))
.stdout(str::contains("## Allocation"))
.stdout(str::contains("## Positions"))
.stdout(str::contains("## Risk Flags"))
.stdout(str::contains("## Data Quality Flags"))
.stdout(str::contains("## Useful Follow-Up Commands"));
}
#[test]
#[ignore = "requires network access to Yahoo Finance"]
fn test_review_json_output_structure() {
let (_tmp, policy_path) = test_policy();
let mut cmd = bin();
cmd.args([
"review",
&example_data(),
"--policy",
&policy_path,
"--format",
"json",
]);
let assert = cmd.assert().success();
let stdout = String::from_utf8_lossy(&assert.get_output().stdout);
let parsed: Value = serde_json::from_str(&stdout).unwrap();
assert!(parsed.get("portfolioValue").is_some());
assert!(parsed.get("currency").is_some());
assert!(parsed.get("policyName").is_some());
assert!(parsed.get("findings").is_some());
assert!(parsed.get("allocations").is_some());
assert!(parsed.get("constraintChecks").is_some());
assert!(parsed.get("suggestedActions").is_some());
let findings = parsed.get("findings").unwrap().as_array().unwrap();
assert!(!findings.is_empty());
let allocations = parsed.get("allocations").unwrap().as_array().unwrap();
assert!(!allocations.is_empty());
let first_alloc = &allocations[0];
assert!(first_alloc.get("assetClass").is_some());
assert!(first_alloc.get("targetPercent").is_some());
assert!(first_alloc.get("actualPercent").is_some());
assert!(first_alloc.get("driftPercent").is_some());
assert!(first_alloc.get("withinTolerance").is_some());
}
#[test]
#[ignore = "requires network access to Yahoo Finance"]
fn test_review_markdown_output() {
let (_tmp, policy_path) = test_policy();
let mut cmd = bin();
cmd.args([
"review",
&example_data(),
"--policy",
&policy_path,
"--format",
"markdown",
]);
cmd.assert()
.success()
.stdout(str::contains("# Portfolio Review"))
.stdout(str::contains("## Findings"))
.stdout(str::contains("## Allocation Review"))
.stdout(str::contains("## Constraint Checks"))
.stdout(str::contains("## Suggested Actions"));
}
#[test]
#[ignore = "requires network access to Yahoo Finance"]
fn test_simulate_json_output_structure() {
let (_tmp, policy_path) = test_policy();
let mut cmd = bin();
cmd.args([
"simulate",
&example_data(),
"--policy",
&policy_path,
"--format",
"json",
]);
let assert = cmd.assert().success();
let stdout = String::from_utf8_lossy(&assert.get_output().stdout);
let parsed: Value = serde_json::from_str(&stdout).unwrap();
assert!(parsed.get("portfolioValue").is_some());
assert!(parsed.get("currency").is_some());
assert!(parsed.get("policyName").is_some());
assert!(parsed.get("scenarios").is_some());
let scenarios = parsed.get("scenarios").unwrap().as_array().unwrap();
assert!(!scenarios.is_empty());
let first = &scenarios[0];
assert!(first.get("name").is_some());
assert!(first.get("description").is_some());
assert!(first.get("trades").is_some());
}
#[test]
#[ignore = "requires network access to Yahoo Finance"]
fn test_simulate_markdown_output() {
let (_tmp, policy_path) = test_policy();
let mut cmd = bin();
cmd.args([
"simulate",
&example_data(),
"--policy",
&policy_path,
"--format",
"markdown",
]);
cmd.assert()
.success()
.stdout(str::contains("# Rebalance Simulation"))
.stdout(str::contains("Scenario 1"))
.stdout(str::contains("Disclaimer"));
}
#[test]
#[ignore = "requires network access to Yahoo Finance"]
fn test_balances_output() {
// Run in a temp dir so the sled database side effect stays out of the repo.
let tmp = TempDir::new().unwrap();
let mut cmd = bin();
cmd.current_dir(tmp.path());
cmd.args(["balances", &example_data()]);
cmd.assert()
.success()
.stdout(str::contains("Name"))
.stdout(str::contains("Class"))
.stdout(str::contains("Value"));
}
#[test]
#[ignore = "requires network access to Yahoo Finance"]
fn test_allocation_output() {
let mut cmd = bin();
cmd.args(["allocation", &example_data()]);
cmd.assert().success();
}
#[test]
#[ignore = "requires network access to Yahoo Finance"]
fn test_sort_output() {
let mut cmd = bin();
cmd.args(["sort", &example_data()]);
cmd.assert()
.success()
.stdout(str::contains("sorted by current value"));
}
#[tokio::test]
#[ignore = "requires network access to Yahoo Finance"]
async fn test_create_live_portfolio() {
let positions_str = std::fs::read_to_string(example_data()).unwrap();
let (portfolio, _network_status) =
portfolio_rs::services::portfolio_loader::create_live_portfolio(positions_str).await;
assert!(!portfolio.positions.is_empty());
}