cli_update.rs

  1use assert_cmd::cargo::cargo_bin_cmd;
  2use predicates::prelude::*;
  3use tempfile::TempDir;
  4
  5fn td(home: &TempDir) -> assert_cmd::Command {
  6    let mut cmd = cargo_bin_cmd!("td");
  7    cmd.env("HOME", home.path());
  8    cmd
  9}
 10
 11fn init_tmp() -> TempDir {
 12    let tmp = TempDir::new().unwrap();
 13    td(&tmp)
 14        .args(["project", "init", "main"])
 15        .current_dir(&tmp)
 16        .assert()
 17        .success();
 18    tmp
 19}
 20
 21fn create_task(dir: &TempDir, title: &str) -> String {
 22    let out = td(dir)
 23        .args(["--json", "create", title])
 24        .current_dir(dir)
 25        .output()
 26        .unwrap();
 27    let v: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
 28    v["id"].as_str().unwrap().to_string()
 29}
 30
 31fn get_task_json(dir: &TempDir, id: &str) -> serde_json::Value {
 32    let out = td(dir)
 33        .args(["--json", "show", id])
 34        .current_dir(dir)
 35        .output()
 36        .unwrap();
 37    serde_json::from_slice(&out.stdout).unwrap()
 38}
 39
 40// ── update ───────────────────────────────────────────────────────────
 41
 42#[test]
 43fn update_changes_status() {
 44    let tmp = init_tmp();
 45    let id = create_task(&tmp, "In progress");
 46
 47    td(&tmp)
 48        .args(["update", &id, "-s", "in_progress"])
 49        .current_dir(&tmp)
 50        .assert()
 51        .success()
 52        .stdout(predicate::str::contains("updated"));
 53
 54    let t = get_task_json(&tmp, &id);
 55    assert_eq!(t["status"].as_str().unwrap(), "in_progress");
 56}
 57
 58#[test]
 59fn update_changes_priority() {
 60    let tmp = init_tmp();
 61    let id = create_task(&tmp, "Reprioritise");
 62
 63    td(&tmp)
 64        .args(["update", &id, "-p", "high"])
 65        .current_dir(&tmp)
 66        .assert()
 67        .success();
 68
 69    let t = get_task_json(&tmp, &id);
 70    assert_eq!(t["priority"].as_str().unwrap(), "high");
 71}
 72
 73#[test]
 74fn update_changes_title() {
 75    let tmp = init_tmp();
 76    let id = create_task(&tmp, "Old title");
 77
 78    td(&tmp)
 79        .args(["update", &id, "-t", "New title"])
 80        .current_dir(&tmp)
 81        .assert()
 82        .success();
 83
 84    let t = get_task_json(&tmp, &id);
 85    assert_eq!(t["title"].as_str().unwrap(), "New title");
 86}
 87
 88#[test]
 89fn update_changes_description() {
 90    let tmp = init_tmp();
 91    let id = create_task(&tmp, "Describe me");
 92
 93    td(&tmp)
 94        .args(["update", &id, "-d", "Now with details"])
 95        .current_dir(&tmp)
 96        .assert()
 97        .success();
 98
 99    let t = get_task_json(&tmp, &id);
100    assert_eq!(t["description"].as_str().unwrap(), "Now with details");
101}
102
103#[test]
104fn update_json_returns_task() {
105    let tmp = init_tmp();
106    let id = create_task(&tmp, "JSON update");
107
108    let out = td(&tmp)
109        .args(["--json", "update", &id, "-p", "high"])
110        .current_dir(&tmp)
111        .output()
112        .unwrap();
113    let v: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
114    assert_eq!(v["priority"].as_str().unwrap(), "high");
115}
116
117#[test]
118fn update_changes_effort() {
119    let tmp = init_tmp();
120    let id = create_task(&tmp, "Re-estimate");
121
122    td(&tmp)
123        .args(["update", &id, "-e", "high"])
124        .current_dir(&tmp)
125        .assert()
126        .success();
127
128    let t = get_task_json(&tmp, &id);
129    assert_eq!(t["effort"].as_str().unwrap(), "high");
130}
131
132// ── done ─────────────────────────────────────────────────────────────
133
134#[test]
135fn done_closes_task() {
136    let tmp = init_tmp();
137    let id = create_task(&tmp, "Close me");
138
139    td(&tmp)
140        .args(["done", &id])
141        .current_dir(&tmp)
142        .assert()
143        .success()
144        .stdout(predicate::str::contains("closed"));
145
146    let t = get_task_json(&tmp, &id);
147    assert_eq!(t["status"].as_str().unwrap(), "closed");
148}
149
150#[test]
151fn done_closes_multiple_tasks() {
152    let tmp = init_tmp();
153    let id1 = create_task(&tmp, "First");
154    let id2 = create_task(&tmp, "Second");
155
156    td(&tmp)
157        .args(["done", &id1, &id2])
158        .current_dir(&tmp)
159        .assert()
160        .success();
161
162    assert_eq!(get_task_json(&tmp, &id1)["status"], "closed");
163    assert_eq!(get_task_json(&tmp, &id2)["status"], "closed");
164}
165
166// ── reopen ───────────────────────────────────────────────────────────
167
168#[test]
169fn reopen_reopens_closed_task() {
170    let tmp = init_tmp();
171    let id = create_task(&tmp, "Reopen me");
172
173    td(&tmp)
174        .args(["done", &id])
175        .current_dir(&tmp)
176        .assert()
177        .success();
178    assert_eq!(get_task_json(&tmp, &id)["status"], "closed");
179
180    td(&tmp)
181        .args(["reopen", &id])
182        .current_dir(&tmp)
183        .assert()
184        .success()
185        .stdout(predicate::str::contains("reopened"));
186
187    assert_eq!(get_task_json(&tmp, &id)["status"], "open");
188}
189
190// ── editor fallback ───────────────────────────────────────────────────────────
191
192#[test]
193fn update_via_editor_changes_title_and_desc() {
194    // Bare `td update <id>` with TD_FORCE_EDITOR should open the editor
195    // pre-populated and apply whatever the fake editor writes back.
196    let tmp = init_tmp();
197    let id = create_task(&tmp, "Original title");
198
199    let fake_editor = "sh -c 'printf \"New title\\nNew description\" > \"$1\"' sh";
200
201    td(&tmp)
202        .args(["update", &id])
203        .env("TD_FORCE_EDITOR", fake_editor)
204        .current_dir(&tmp)
205        .assert()
206        .success();
207
208    let t = get_task_json(&tmp, &id);
209    assert_eq!(t["title"].as_str().unwrap(), "New title");
210    assert_eq!(t["description"].as_str().unwrap(), "New description");
211}
212
213#[test]
214fn update_via_editor_aborts_on_empty_file() {
215    // If the fake editor leaves only comments, the update should be aborted.
216    let tmp = init_tmp();
217    let id = create_task(&tmp, "Stays the same");
218
219    let fake_editor = "sh -c 'printf \"TD: comment only\\n\" > \"$1\"' sh";
220
221    td(&tmp)
222        .args(["update", &id])
223        .env("TD_FORCE_EDITOR", fake_editor)
224        .current_dir(&tmp)
225        .assert()
226        .failure()
227        .stderr(predicate::str::contains("aborted"));
228
229    // Title must be unchanged.
230    let t = get_task_json(&tmp, &id);
231    assert_eq!(t["title"].as_str().unwrap(), "Stays the same");
232}
233
234#[test]
235fn update_via_editor_preserves_existing_content_as_template() {
236    // The editor should be pre-populated with the task's current title and
237    // description, so the user can edit rather than retype from scratch.
238    let tmp = init_tmp();
239    let id = create_task(&tmp, "Existing title");
240
241    // Set description first.
242    td(&tmp)
243        .args(["update", &id, "-d", "Existing description"])
244        .current_dir(&tmp)
245        .assert()
246        .success();
247
248    // Fake editor: grep out the first non-comment, non-blank line (the title),
249    // then write "title: <that line>" so we can assert it was pre-populated.
250    let fake_editor = concat!(
251        "sh -c '",
252        r#"title=$(grep -v "^TD: " "$1" | grep -v "^[[:space:]]*$" | head -1); "#,
253        r#"printf "title: %s" "$title" > "$1""#,
254        "' sh"
255    );
256
257    td(&tmp)
258        .args(["update", &id])
259        .env("TD_FORCE_EDITOR", fake_editor)
260        .current_dir(&tmp)
261        .assert()
262        .success();
263
264    let t = get_task_json(&tmp, &id);
265    // The fake editor wrote the existing title back with a prefix, proving it
266    // received the pre-populated template.
267    assert_eq!(t["title"].as_str().unwrap(), "title: Existing title");
268}