use kanban_cli::CliApp;
use kanban_persistence_json::{JsonBackendFactory, JsonStoreFactory};
#[test]
fn test_cli_app_default_has_no_backends() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test.json").to_string_lossy().to_string();
let app = CliApp::default();
match app.registry().create_store("json", &path) {
Ok(_) => panic!("CliApp::default must not register any backends"),
Err(err) => assert!(
err.to_string().contains("json") || err.to_string().contains("Unsupported"),
"expected unsupported-locator error, got: {err}"
),
}
}
#[test]
fn test_cli_app_with_defaults_creates_json_store() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test.json").to_string_lossy().to_string();
let app = CliApp::with_defaults();
let store = app
.registry()
.create_store("json", &path)
.expect("with_defaults must register the JSON backend");
assert!(store.path().to_str().unwrap().ends_with(".json"));
}
#[test]
fn test_cli_app_register_backend_adds_custom_factory() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test.json").to_string_lossy().to_string();
let app = CliApp::default()
.register_backend(Box::new(JsonStoreFactory), Box::new(JsonBackendFactory));
let store = app
.registry()
.create_store("json", &path)
.expect("registered factory must be dispatchable");
assert!(store.path().to_str().unwrap().ends_with(".json"));
}
#[tokio::test(flavor = "multi_thread")]
async fn test_cli_app_register_backend_reachable_via_make_backend() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("board.json").to_string_lossy().to_string();
CliApp::default()
.register_backend(Box::new(JsonStoreFactory), Box::new(JsonBackendFactory))
.run_with_args(["kanban", &path])
.await
.expect("init run must succeed with a registered backend");
CliApp::default()
.register_backend(Box::new(JsonStoreFactory), Box::new(JsonBackendFactory))
.run_with_args(["kanban", &path, "board", "list"])
.await
.expect(
"a backend registered only through register_backend must be reachable via make_backend",
);
}
#[test]
fn test_cli_app_with_config_stores_override() {
use kanban_core::AppConfig;
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test.json").to_string_lossy().to_string();
let app = CliApp::with_defaults().with_config(AppConfig {
storage_backend: Some("sqlite".into()),
..Default::default()
});
let store = app
.registry()
.create_store("json", &path)
.expect("with_defaults backends must survive a with_config call");
assert!(store.path().to_str().unwrap().ends_with(".json"));
}
#[test]
fn test_cli_app_with_defaults_populates_both_registries() {
let app = CliApp::with_defaults();
assert!(!app.registry().is_empty(), "registry() must be populated");
assert!(!app.backends().is_empty(), "backends() must be populated");
let names = app.backends().names();
#[cfg(feature = "http")]
let expected = vec!["sqlite", "json", "http"];
#[cfg(not(feature = "http"))]
let expected = vec!["sqlite", "json"];
assert_eq!(
names, expected,
"sqlite, then json, then http last so file sniffing keeps priority"
);
}
#[cfg(feature = "http")]
#[test]
fn test_cli_defaults_route_an_http_locator_to_the_http_backend() {
let app = CliApp::with_defaults();
assert_eq!(
app.backends()
.for_locator("http://127.0.0.1:9")
.map(|f| f.name()),
Some("http")
);
assert_eq!(
app.backends()
.for_locator("https://example.com/boards")
.map(|f| f.name()),
Some("http")
);
}
#[test]
fn test_a_json_path_still_routes_to_the_json_backend() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("board.json");
std::fs::write(&path, b"{}").unwrap();
let app = CliApp::with_defaults();
assert_eq!(
app.registry().detect_backend(path.to_str().unwrap()),
Some("json")
);
}
#[test]
fn test_a_sqlite_path_still_routes_to_the_sqlite_backend() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("board.sqlite");
let app = CliApp::with_defaults();
assert_eq!(
app.backends()
.for_locator(path.to_str().unwrap())
.map(|f| f.name()),
Some("sqlite")
);
}
#[test]
fn test_cli_app_with_defaults_detects_json_backend() {
let app = CliApp::with_defaults();
let dir = tempfile::tempdir().unwrap();
let json_path = dir.path().join("board.json");
std::fs::write(&json_path, b"{}").unwrap();
let detected = app
.registry()
.detect_backend(json_path.to_str().unwrap())
.expect("should detect json backend for .json file");
assert_eq!(detected, "json");
}