mirror of
https://github.com/0xrsydn/idx-cli.git
synced 2026-08-07 01:33:52 +00:00
test(api): add yahoo fixtures, mock-provider errors, and CLI integration coverage
This commit is contained in:
parent
c0a3d744b2
commit
639ee4b041
6 changed files with 233 additions and 51 deletions
142
tests/cli.rs
142
tests/cli.rs
|
|
@ -1,10 +1,22 @@
|
|||
use assert_cmd::Command;
|
||||
use std::fs;
|
||||
|
||||
use assert_cmd::{cargo::cargo_bin, Command};
|
||||
use predicates::prelude::*;
|
||||
|
||||
fn bin() -> Command {
|
||||
Command::new(cargo_bin("idx-cli"))
|
||||
}
|
||||
|
||||
fn test_env_dir(name: &str) -> std::path::PathBuf {
|
||||
let dir = std::env::temp_dir().join(format!("idx-cli-it-{name}-{}", std::process::id()));
|
||||
let _ = fs::remove_dir_all(&dir);
|
||||
fs::create_dir_all(&dir).expect("create temp dir");
|
||||
dir
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn help_works() {
|
||||
Command::cargo_bin("idx-cli")
|
||||
.expect("binary exists")
|
||||
bin()
|
||||
.arg("--help")
|
||||
.assert()
|
||||
.success();
|
||||
|
|
@ -12,8 +24,7 @@ fn help_works() {
|
|||
|
||||
#[test]
|
||||
fn version_prints_cargo_version() {
|
||||
Command::cargo_bin("idx-cli")
|
||||
.expect("binary exists")
|
||||
bin()
|
||||
.arg("version")
|
||||
.assert()
|
||||
.success()
|
||||
|
|
@ -21,34 +32,113 @@ fn version_prints_cargo_version() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn quote_with_mock_provider_json() {
|
||||
Command::cargo_bin("idx-cli")
|
||||
.expect("binary exists")
|
||||
fn quote_table_with_mock_contains_expected_columns() {
|
||||
bin()
|
||||
.env("IDX_USE_MOCK_PROVIDER", "1")
|
||||
.args(["-o", "json", "stocks", "quote", "BBCA,BBRI"])
|
||||
.env("IDX_CACHE_QUOTE_TTL", "0")
|
||||
.args(["stocks", "quote", "BBCA"])
|
||||
.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("BBCA.JK"))
|
||||
.stdout(predicate::str::contains("BBRI.JK"));
|
||||
.stdout(predicate::str::contains("SYMBOL"))
|
||||
.stdout(predicate::str::contains("PRICE"))
|
||||
.stdout(predicate::str::contains("CHG%"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn history_with_mock_provider_json() {
|
||||
Command::cargo_bin("idx-cli")
|
||||
.expect("binary exists")
|
||||
fn quote_with_mock_provider_json() {
|
||||
bin()
|
||||
.env("IDX_USE_MOCK_PROVIDER", "1")
|
||||
.args([
|
||||
"-o",
|
||||
"json",
|
||||
"stocks",
|
||||
"history",
|
||||
"BBCA",
|
||||
"--period",
|
||||
"3mo",
|
||||
"--interval",
|
||||
"1d",
|
||||
])
|
||||
.args(["-o", "json", "stocks", "quote", "BBCA"])
|
||||
.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("2026-03-01"));
|
||||
.stdout(predicate::str::contains("symbol"))
|
||||
.stdout(predicate::str::contains("price"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn history_with_mock_provider_table_contains_columns() {
|
||||
bin()
|
||||
.env("IDX_USE_MOCK_PROVIDER", "1")
|
||||
.args(["stocks", "history", "BBCA", "--period", "1mo"])
|
||||
.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("DATE"))
|
||||
.stdout(predicate::str::contains("OPEN"))
|
||||
.stdout(predicate::str::contains("VOLUME"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn config_path_prints_path() {
|
||||
bin()
|
||||
.args(["config", "path"])
|
||||
.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("config.toml"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn config_init_creates_file() {
|
||||
let root = test_env_dir("config-init");
|
||||
let config_home = root.join("cfg");
|
||||
|
||||
bin()
|
||||
.env("XDG_CONFIG_HOME", &config_home)
|
||||
.args(["config", "init"])
|
||||
.assert()
|
||||
.success();
|
||||
|
||||
assert!(config_home.join("idx/config.toml").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cache_info_and_clear_do_not_crash() {
|
||||
let root = test_env_dir("cache");
|
||||
let cache_home = root.join("cache");
|
||||
|
||||
bin()
|
||||
.env("XDG_CACHE_HOME", &cache_home)
|
||||
.args(["cache", "info"])
|
||||
.assert()
|
||||
.success();
|
||||
|
||||
bin()
|
||||
.env("XDG_CACHE_HOME", &cache_home)
|
||||
.args(["cache", "clear"])
|
||||
.assert()
|
||||
.success();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serves_stale_cache_on_provider_failure_with_warning() {
|
||||
let root = test_env_dir("stale");
|
||||
let cache_home = root.join("cache");
|
||||
|
||||
bin()
|
||||
.env("XDG_CACHE_HOME", &cache_home)
|
||||
.env("IDX_USE_MOCK_PROVIDER", "1")
|
||||
.env("IDX_CACHE_QUOTE_TTL", "0")
|
||||
.args(["stocks", "quote", "BBCA"])
|
||||
.assert()
|
||||
.success();
|
||||
|
||||
bin()
|
||||
.env("XDG_CACHE_HOME", &cache_home)
|
||||
.env("IDX_USE_MOCK_PROVIDER", "1")
|
||||
.env("IDX_CACHE_QUOTE_TTL", "0")
|
||||
.env("IDX_MOCK_ERROR", "1")
|
||||
.args(["stocks", "quote", "BBCA"])
|
||||
.assert()
|
||||
.success()
|
||||
.stderr(predicate::str::contains("warning: network failed"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_symbol_returns_non_zero() {
|
||||
bin()
|
||||
.env("IDX_USE_MOCK_PROVIDER", "1")
|
||||
.env("IDX_MOCK_ERROR", "1")
|
||||
.args(["stocks", "quote", "INVALID"])
|
||||
.assert()
|
||||
.failure()
|
||||
.stderr(predicate::str::contains("Error:"));
|
||||
}
|
||||
|
|
|
|||
30
tests/fixtures/chart_bbca_1d.json
vendored
Normal file
30
tests/fixtures/chart_bbca_1d.json
vendored
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
{
|
||||
"chart": {
|
||||
"result": [
|
||||
{
|
||||
"meta": {
|
||||
"symbol": "BBCA.JK",
|
||||
"regularMarketPrice": 9875.0,
|
||||
"previousClose": 9758.0,
|
||||
"regularMarketVolume": 12300000,
|
||||
"marketCap": 1215200000000000,
|
||||
"fiftyTwoWeekHigh": 10250.0,
|
||||
"fiftyTwoWeekLow": 7800.0,
|
||||
"averageDailyVolume3Month": 10000000
|
||||
},
|
||||
"timestamp": [1709251200],
|
||||
"indicators": {
|
||||
"quote": [
|
||||
{
|
||||
"open": [9800.0],
|
||||
"high": [9900.0],
|
||||
"low": [9750.0],
|
||||
"close": [9875.0],
|
||||
"volume": [12300000]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
25
tests/fixtures/chart_bbca_3mo.json
vendored
Normal file
25
tests/fixtures/chart_bbca_3mo.json
vendored
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
{
|
||||
"chart": {
|
||||
"result": [
|
||||
{
|
||||
"meta": {
|
||||
"symbol": "BBCA.JK",
|
||||
"regularMarketPrice": 9875.0,
|
||||
"previousClose": 9758.0
|
||||
},
|
||||
"timestamp": [1709251200, 1709337600, 1709424000],
|
||||
"indicators": {
|
||||
"quote": [
|
||||
{
|
||||
"open": [9800.0, 9850.0, 9860.0],
|
||||
"high": [9900.0, 9920.0, 9950.0],
|
||||
"low": [9750.0, 9800.0, 9820.0],
|
||||
"close": [9875.0, 9880.0, 9925.0],
|
||||
"volume": [12300000, 11000000, 14000000]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue