mod common;
use common::{criterion, fixture, need, stderr, stdout};
#[test]
fn edit_changes_title_and_free_attributes() {
let fx = fixture();
fx.seed(&[need("0001", "a need")]);
let out = fx.run(&[
"edit",
"n-0001",
"--reason",
"clarify",
"--title",
"new title",
"--set",
"owner=piko",
"--append",
"note=one",
"--append",
"note=two",
]);
assert!(out.status.success(), "{}", stderr(&out));
assert!(stdout(&out).contains("changed: title, free:owner, free:note"));
assert!(stdout(&out).contains("missing: \nnext: \n"));
let text = stdout(&fx.run(&["show", "--full", "n-0001"]));
assert!(text.contains("new title"));
assert!(text.contains("owner: piko"));
assert!(text.contains("note: one\ntwo"));
}
#[test]
fn a_writer_can_follow_show_to_read_free_attributes() {
let fx = fixture();
fx.seed(&[need("0001", "a need")]);
for change in [["--append", "k=v"], ["--set", "k2=v2"]] {
let out = fx.run(&["edit", "n-0001", "--reason", "record", change[0], change[1]]);
assert!(out.status.success(), "{}", stderr(&out));
assert!(stdout(&out).contains("missing: \nnext: \n"));
}
let out = fx.run(&["show", "n-0001"]);
assert!(out.status.success(), "{}", stderr(&out));
let text = stdout(&out);
assert!(!text.contains("k: v"), "{text}");
assert!(!text.contains("k2: v2"), "{text}");
let command = text
.lines()
.find_map(|line| {
line.strip_prefix("To read all free attributes, run `gy ")
.and_then(|command| command.strip_suffix("`."))
})
.unwrap_or_else(|| panic!("no reading command: {text}"));
let mut args: Vec<&str> = command.split_whitespace().collect();
assert_eq!(args, ["show", "n-0001", "--full"]);
let out = fx.run(&args);
assert!(out.status.success(), "{}", stderr(&out));
let full = stdout(&out);
assert!(full.contains("k: v"), "{full}");
assert!(full.contains("k2: v2"), "{full}");
assert!(!full.contains("To read all free attributes"), "{full}");
args.push("--json");
let out = fx.run(&args);
assert!(out.status.success(), "{}", stderr(&out));
let full: serde_json::Value = serde_json::from_str(&stdout(&out)).unwrap();
assert_eq!(
full[0]["attributes"],
serde_json::json!({"k": "v", "k2": "v2"})
);
let out = fx.run(&["show", "n-0001", "--json"]);
assert!(out.status.success(), "{}", stderr(&out));
let shown: serde_json::Value = serde_json::from_str(&stdout(&out)).unwrap();
assert!(shown[0].get("attributes").is_none(), "{shown}");
let out = fx.run(&[
"edit", "n-0001", "--reason", "clarify", "--title", "renamed",
]);
assert!(out.status.success(), "{}", stderr(&out));
let text = stdout(&fx.run(&["show", "n-0001"]));
assert!(text.contains(command), "{text}");
let out = fx.run(&[
"edit", "n-0001", "--reason", "remove", "--set", "k=", "--set", "k2=",
]);
assert!(out.status.success(), "{}", stderr(&out));
let text = stdout(&fx.run(&["show", "n-0001"]));
assert!(!text.contains(command), "{text}");
let out = fx.run(&["cheat"]);
assert!(out.status.success(), "{}", stderr(&out));
let text = stdout(&out);
assert!(
text.contains("Read free attributes with gy show <ID> --full (also with --json)."),
"{text}"
);
}
#[test]
fn edit_moves_a_node_to_a_declared_scope() {
let fx = fixture();
std::fs::write(fx.root.join("gy.toml"), "[scopes.a]\n[scopes.b]\n").unwrap();
fx.seed(&[need("0001", "a need")]);
let out = fx.run(&["edit", "n-0001", "--reason", "move", "--set", "scope=b"]);
assert!(out.status.success(), "{}", stderr(&out));
assert!(stdout(&out).contains("changed: scope"), "{}", stdout(&out));
let text = stdout(&fx.run(&["show", "--full", "n-0001"]));
assert!(text.contains("scope: b"), "{text}");
let out = fx.run(&["edit", "n-0001", "--reason", "move", "--set", "scope=c"]);
assert_eq!(out.status.code(), Some(2));
}
#[test]
fn edit_names_the_declared_scopes() {
let fx = fixture();
std::fs::write(fx.root.join("gy.toml"), "[scopes.a]\n[scopes.b]\n").unwrap();
fx.seed(&[need("0001", "a need")]);
let out = fx.run(&["edit", "n-0001", "--reason", "move", "--set", "scope=nope"]);
assert_eq!(out.status.code(), Some(2));
let message = stderr(&out);
assert!(message.contains("expected one of a, b"), "{message}");
}
#[test]
fn edit_help_shows_the_repeated_attribute_forms() {
let fx = fixture();
let help = fx.run(&["edit", "--help"]);
assert!(help.status.success(), "{}", stderr(&help));
let text = stdout(&help);
assert!(text.contains("--set A=B --set C=D"), "{text}");
assert!(text.contains("--append A=B --append C=D"), "{text}");
}
#[test]
fn edit_prints_the_fifth_line_and_the_json_field() {
let fx = fixture();
fx.seed(&[need("0001", "a need")]);
let out = fx.run(&[
"edit", "n-0001", "--reason", "clarify", "--title", "renamed",
]);
assert!(out.status.success(), "{}", stderr(&out));
let text = stdout(&out);
assert!(text.contains("missing: \nnext: \n"), "{text}");
assert!(text.ends_with("unresolved: \n"), "{text}");
let json = fx.run(&[
"--json", "edit", "n-0001", "--reason", "clarify", "--title", "again",
]);
assert!(json.status.success(), "{}", stderr(&json));
let value: serde_json::Value = serde_json::from_str(stdout(&json).trim()).unwrap();
assert_eq!(value["unresolved"], serde_json::json!([]));
}
#[test]
fn edit_errors() {
let fx = fixture();
fx.seed(&[need("0002", "a need")]);
let out = fx.run(&["edit", "n-0002", "--reason", " ", "--title", "x"]);
assert_eq!(out.status.code(), Some(2));
let out = fx.run(&["edit", "n-0002", "--reason", "x", "--set", "status=done"]);
assert_eq!(out.status.code(), Some(2));
let out = fx.run(&["edit", "n-0002", "--reason", "x"]);
assert_eq!(out.status.code(), Some(2));
let out = fx.run(&["edit", "n-9999", "--reason", "x", "--title", "y"]);
assert_eq!(out.status.code(), Some(2));
let out = fx.run(&["edit", "n-0002", "--reason", "x", "--set", "novalue"]);
assert_eq!(out.status.code(), Some(2));
}
#[test]
fn edit_refusals_name_the_operation_that_changes_the_attribute() {
let fx = fixture();
fx.seed(&[need("0001", "a need"), criterion("0002", "a criterion")]);
let refusal = |key: &str| -> String {
let out = fx.run(&[
"edit",
"n-0001",
"--reason",
"shortcut",
"--set",
&format!("{key}=x"),
]);
assert_eq!(out.status.code(), Some(2), "{key}");
stderr(&out)
};
let state = refusal("state");
assert!(state.contains("gy req approve"), "{state}");
assert!(state.contains("gy need close"), "{state}");
assert!(state.contains("gy criterion satisfy"), "{state}");
let criterion_state = fx.run(&[
"edit", "ac-0002", "--reason", "shortcut", "--set", "state=x",
]);
assert_eq!(criterion_state.status.code(), Some(2));
let message = stderr(&criterion_state);
assert!(message.contains("gy criterion satisfy"), "{message}");
assert!(
!message.contains("its own operation"),
"the old refusal survives: {message}"
);
let reference = refusal("reference");
assert!(reference.contains("gy req add --ref"), "{reference}");
assert!(reference.contains("cannot be changed later"), "{reference}");
let decider = refusal("decider");
assert!(decider.contains("gy question add --decider"), "{decider}");
assert!(decider.contains("does not change"), "{decider}");
let title = refusal("title");
assert!(title.contains("gy edit --title"), "{title}");
for message in [state, reference, decider, title] {
assert!(
!message.contains("its own operation"),
"the old refusal survives: {message}"
);
}
}