use std::process::Command;
use tempfile::tempdir;
#[test]
fn test_cli_new_with_obs() {
let dir = tempdir().unwrap();
let db_path = dir.path().join("test.db");
let db_path_str = db_path.to_str().unwrap();
let mut bin_path = std::env::current_exe().unwrap();
bin_path.pop(); if bin_path.ends_with("deps") {
bin_path.pop(); }
bin_path.push("asobi");
assert!(
bin_path.exists(),
"Asobi binary not found at {:?}",
bin_path
);
let output = Command::new(&bin_path)
.arg("new")
.arg("foo")
.arg("concept")
.arg("--obs")
.arg("first observation")
.arg("--obs")
.arg("second observation")
.env("ASOBI_DATABASE_URL", db_path_str)
.output()
.expect("failed to execute asobi");
assert!(
output.status.success(),
"Expected new command with --obs to succeed, but it failed.\nstdout:\n{}\nstderr:\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
let output = Command::new(&bin_path)
.arg("show")
.arg("foo")
.env("ASOBI_DATABASE_URL", db_path_str)
.output()
.expect("failed to execute asobi");
assert!(output.status.success());
let stdout_str = String::from_utf8(output.stdout).unwrap();
let graph: serde_json::Value = serde_json::from_str(&stdout_str).unwrap();
let entities = graph["entities"]
.as_array()
.expect("entities field missing or not an array");
assert_eq!(entities.len(), 1);
let entity = &entities[0];
assert_eq!(entity["name"], "foo");
let observations = entity["observations"]
.as_array()
.expect("observations field missing or not an array");
let mut obs_strs: Vec<&str> = observations.iter().map(|v| v.as_str().unwrap()).collect();
obs_strs.sort();
assert_eq!(obs_strs, vec!["first observation", "second observation"]);
}