qqq-cli 0.1.0

Local-first task queue for coding agent sessions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#![cfg(unix)]

use serde_json::Value;
use std::{fs, path::Path, process::Command};
use tempfile::TempDir;

fn command(dir: &Path) -> Command {
    let mut command = Command::new(env!("CARGO_BIN_EXE_qqq"));
    command
        .arg("--json")
        .current_dir(dir)
        .env("HOME", dir)
        .env_remove("QQQ_SESSION")
        .env_remove("HERDR_ENV")
        .env_remove("HERDR_PANE_ID")
        .env_remove("CODEX_THREAD_ID")
        .env_remove("CODEX_SESSION_ID")
        .env_remove("EDITOR");
    command
}

fn project() -> TempDir {
    let dir = TempDir::new().unwrap();
    assert!(
        command(dir.path())
            .arg("init")
            .output()
            .unwrap()
            .status
            .success()
    );
    dir
}

fn editor(dir: &Path, body: &str) -> String {
    let path = dir.join("test editor.sh");
    fs::write(&path, format!("set -eu\n{body}\n")).unwrap();
    format!("sh '{}' --wait", path.display())
}

#[test]
fn add_uses_editor_with_arguments_and_keeps_stdout_json() {
    let dir = project();
    let editor = editor(
        dir.path(),
        r#"
test "$1" = --wait
printf '%s' "$2" > edited-path
printf '  Edited title  \r\n\r\nDetails\r\nSecond line\r\n' > "$2"
echo 'editor output'
"#,
    );
    let output = command(dir.path())
        .arg("add")
        .env("EDITOR", editor)
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
    let task: Value = serde_json::from_slice(&output.stdout).unwrap();
    assert_eq!(
        task["description"],
        "  Edited title  \r\n\r\nDetails\r\nSecond line\r\n"
    );
    assert!(String::from_utf8_lossy(&output.stderr).contains("editor output"));
    let path = fs::read_to_string(dir.path().join("edited-path")).unwrap();
    assert!(!Path::new(&path).exists());
    let saved = command(dir.path()).args(["show", "1"]).output().unwrap();
    let saved: Value = serde_json::from_slice(&saved.stdout).unwrap();
    assert_eq!(saved["task"], task);
}

#[test]
fn edit_flag_prefills_content_and_supports_atomic_editor_saves() {
    let dir = project();
    let editor = editor(
        dir.path(),
        r#"
cp "$2" initial-content
printf 'New title\n\nNew details\n' > "$2.new"
mv "$2.new" "$2"
"#,
    );
    let output = command(dir.path())
        .args(["add", "Old title\n\nOld details", "--edit"])
        .env("EDITOR", editor)
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert_eq!(
        fs::read_to_string(dir.path().join("initial-content")).unwrap(),
        "Old title\n\nOld details"
    );
    let task: Value = serde_json::from_slice(&output.stdout).unwrap();
    assert_eq!(task["description"], "New title\n\nNew details\n");
}

#[test]
fn editor_errors_leave_queue_empty_and_clean_temp_files() {
    for (body, expected) in [
        ("exit 7", "Editor exited unsuccessfully"),
        (
            "printf '   \\n\\t' > \"$2\"",
            "Task description cannot be empty",
        ),
        ("rm \"$2\"", "Failed to read edited task"),
    ] {
        let dir = project();
        let editor = editor(
            dir.path(),
            &format!("printf '%s' \"$2\" > edited-path\n{body}"),
        );
        let output = command(dir.path())
            .arg("add")
            .env("EDITOR", editor)
            .output()
            .unwrap();
        assert_eq!(output.status.code(), Some(1));
        assert!(
            String::from_utf8_lossy(&output.stderr).contains(expected),
            "{}",
            String::from_utf8_lossy(&output.stderr)
        );
        assert!(output.stdout.is_empty());
        let path = fs::read_to_string(dir.path().join("edited-path")).unwrap();
        assert!(!Path::new(&path).exists());
        let tasks = command(dir.path()).arg("list").output().unwrap();
        assert_eq!(
            serde_json::from_slice::<Value>(&tasks.stdout).unwrap(),
            serde_json::json!([])
        );
    }
}

#[test]
fn editor_requires_configuration_but_inline_add_does_not() {
    let dir = project();
    for value in [None, Some(""), Some("  ")] {
        let mut cmd = command(dir.path());
        if let Some(value) = value {
            cmd.env("EDITOR", value);
        }
        let output = cmd.arg("add").output().unwrap();
        assert_eq!(output.status.code(), Some(1));
        assert!(String::from_utf8_lossy(&output.stderr).contains("Set EDITOR"));
    }
    let output = command(dir.path())
        .args(["add", "Inline\n\nDetails"])
        .env("EDITOR", "does-not-exist-qqq")
        .output()
        .unwrap();
    assert!(output.status.success());
    let task: Value = serde_json::from_slice(&output.stdout).unwrap();
    assert_eq!(task["description"], "Inline\n\nDetails");
}

fn run_json(dir: &Path, args: &[&str]) -> Value {
    let output = command(dir).args(args).output().unwrap();
    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
    serde_json::from_slice(&output.stdout).unwrap()
}

#[test]
fn edit_arguments_replace_body_and_preserve_task_metadata() {
    let dir = project();
    let p = dir.path();
    run_json(p, &["add", "Parent"]);
    run_json(p, &["add", "Old\n\nDetails", "--parent", "1"]);
    let before = run_json(p, &["show", "2"]);
    let changed = run_json(p, &["edit", "2", "--description", "New"]);
    assert_eq!(changed["description"], "New");
    for field in [
        "id",
        "parent_id",
        "created_at",
        "status",
        "harness_name",
        "harness_session",
        "orchestrator_name",
        "orchestrator_session",
    ] {
        assert_eq!(changed[field], before["task"][field]);
    }
    let changed = run_json(p, &["edit", "2", "--description", "Both\n\nUpdated"]);
    assert_eq!(changed["description"], "Both\n\nUpdated");
    let output = command(p)
        .args(["edit", "2", "--description", "  \n\n\t"])
        .output()
        .unwrap();
    assert_eq!(output.status.code(), Some(1));
    assert_eq!(run_json(p, &["show", "2"])["task"], changed);
    let output = command(p)
        .args(["describe", "2", "Removed"])
        .output()
        .unwrap();
    assert_eq!(output.status.code(), Some(2));
}

#[test]
fn edit_opens_prefilled_editor_and_persists_whole_body() {
    let dir = project();
    run_json(dir.path(), &["add", "Old\n\nDetails"]);
    let editor = editor(
        dir.path(),
        r#"
cp "$2" initial-content
printf 'New\n\nUpdated\n' > "$2.new"
mv "$2.new" "$2"
echo editor-output
"#,
    );
    let output = command(dir.path())
        .args(["edit", "1"])
        .env("EDITOR", editor)
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert_eq!(
        fs::read_to_string(dir.path().join("initial-content")).unwrap(),
        "Old\n\nDetails"
    );
    let task: Value = serde_json::from_slice(&output.stdout).unwrap();
    assert_eq!(task["description"], "New\n\nUpdated\n");
    assert_eq!(run_json(dir.path(), &["show", "1"])["task"], task);
}

#[test]
fn edit_failures_preserve_existing_task_and_clean_draft() {
    for body in ["exit 7", "printf '\\n\\t' > \"$2\"", "rm \"$2\""] {
        let dir = project();
        let original = run_json(dir.path(), &["add", "Old\n\nDetails"]);
        let editor = editor(
            dir.path(),
            &format!("printf '%s' \"$2\" > edited-path\n{body}"),
        );
        let output = command(dir.path())
            .args(["edit", "1"])
            .env("EDITOR", editor)
            .output()
            .unwrap();
        assert_eq!(output.status.code(), Some(1));
        assert!(output.stdout.is_empty());
        assert_eq!(run_json(dir.path(), &["show", "1"])["task"], original);
        let path = fs::read_to_string(dir.path().join("edited-path")).unwrap();
        assert!(!Path::new(&path).exists());
    }
}

#[test]
fn edit_missing_task_skips_editor_and_existing_task_requires_editor() {
    let dir = project();
    let editor = editor(dir.path(), "touch editor-started");
    let output = command(dir.path())
        .args(["edit", "42"])
        .env("EDITOR", editor)
        .output()
        .unwrap();
    assert_eq!(output.status.code(), Some(1));
    assert!(String::from_utf8_lossy(&output.stderr).contains("Task 42 not found"));
    assert!(!dir.path().join("editor-started").exists());
    let output = command(dir.path())
        .args(["edit", "42", "-d", "Missing"])
        .output()
        .unwrap();
    assert_eq!(output.status.code(), Some(1));
    run_json(dir.path(), &["add", "Old"]);
    let output = command(dir.path()).args(["edit", "1"]).output().unwrap();
    assert_eq!(output.status.code(), Some(1));
    assert!(String::from_utf8_lossy(&output.stderr).contains("Set EDITOR"));
}

#[test]
fn interactive_edit_round_trips_multiline_body_without_splitting() {
    for body in ["First\nSecond", "First\rSecond"] {
        let dir = project();
        run_json(dir.path(), &["add", body]);
        let editor = editor(dir.path(), "touch editor-started\ncp \"$2\" prefilled");
        let output = command(dir.path())
            .args(["edit", "1"])
            .env("EDITOR", editor)
            .output()
            .unwrap();
        assert_eq!(output.status.code(), Some(0));
        assert!(dir.path().join("editor-started").exists());
        assert_eq!(
            fs::read_to_string(dir.path().join("prefilled")).unwrap(),
            body
        );
        assert_eq!(
            run_json(dir.path(), &["show", "1"])["task"]["description"],
            body
        );
        let updated = run_json(dir.path(), &["edit", "1", "-d", "Updated"]);
        assert_eq!(updated["description"], "Updated");
    }
}

#[test]
fn edit_recent_selects_creation_order_across_statuses_and_id_gaps() {
    let dir = project();
    let p = dir.path();
    for title in ["First", "Removed", "Newest"] {
        run_json(p, &["add", title]);
    }
    let conn = rusqlite::Connection::open(p.join("qqq.db")).unwrap();
    conn.execute("DELETE FROM tasks WHERE id=2", []).unwrap();
    conn.execute("UPDATE tasks SET created_at='same timestamp'", [])
        .unwrap();
    for id in ["1", "3"] {
        run_json(p, &["next", "--session", "recent-edit"]);
        run_json(p, &["complete", id, "--session", "recent-edit"]);
    }
    assert_eq!(
        run_json(p, &["edit", "-1", "--description", "Latest"])["id"],
        3
    );
    let older = run_json(p, &["edit", "-2", "-d", "Changed older"]);
    assert_eq!(older["id"], 1);
    assert_eq!(older["status"], "completed");
    assert_eq!(run_json(p, &["edit", "-1", "-d", "Still latest"])["id"], 3);
    assert_eq!(
        run_json(p, &["edit", "1", "--description", "By ID"])["id"],
        1
    );
}

#[test]
fn edit_recent_invalid_references_skip_editor_and_preserve_tasks() {
    let dir = project();
    let p = dir.path();
    let editor = editor(p, "touch editor-started");
    for populated in [false, true] {
        if populated {
            run_json(p, &["add", "Unchanged"]);
        }
        let before = run_json(p, &["list"]);
        for reference in ["0", "-2", "-9223372036854775808", "99"] {
            for direct in [false, true] {
                let mut cmd = command(p);
                cmd.args(["edit", reference]).env("EDITOR", &editor);
                if direct {
                    cmd.args(["--description", "Lost"]);
                }
                let out = cmd.output().unwrap();
                assert_eq!(
                    out.status.code(),
                    Some(1),
                    "{}",
                    String::from_utf8_lossy(&out.stderr)
                );
                assert!(out.stdout.is_empty());
                assert!(!p.join("editor-started").exists());
                assert_eq!(run_json(p, &["list"]), before);
            }
        }
    }
}

#[test]
fn edit_recent_pins_target_before_editor_creates_another_task() {
    let dir = project();
    let p = dir.path();
    run_json(p, &["add", "Original\n\nOriginal details"]);
    let editor = editor(
        p,
        r#"
cp "$2" initial-content
"$QQQ_TEST_BIN" add "Created during edit" >/dev/null
printf 'Edited original\n\nNew details\n' > "$2"
"#,
    );
    let out = command(p)
        .args(["edit", "-1"])
        .env("EDITOR", editor)
        .env("QQQ_TEST_BIN", env!("CARGO_BIN_EXE_qqq"))
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "{}",
        String::from_utf8_lossy(&out.stderr)
    );
    let edited: Value = serde_json::from_slice(&out.stdout).unwrap();
    assert_eq!(edited["id"], 1);
    assert_eq!(edited["description"], "Edited original\n\nNew details\n");
    assert_eq!(
        fs::read_to_string(p.join("initial-content")).unwrap(),
        "Original\n\nOriginal details"
    );
    assert_eq!(
        run_json(p, &["show", "2"])["task"]["description"],
        "Created during edit"
    );
}

#[test]
fn explicit_edit_flag_opens_external_editor_with_field_prefills() {
    let dir = project();
    run_json(dir.path(), &["add", "Original\n\nOriginal details"]);
    let editor = editor(
        dir.path(),
        "cat \"$2\" > prefilled\nprintf 'Edited\\n\\nSaved\\n' > \"$2\"",
    );
    let output = command(dir.path())
        .args([
            "edit",
            "1",
            "--edit",
            "--description",
            "Prefill\n\nPrefilled details",
        ])
        .env("EDITOR", editor)
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert_eq!(
        fs::read_to_string(dir.path().join("prefilled")).unwrap(),
        "Prefill\n\nPrefilled details"
    );
    let task: Value = serde_json::from_slice(&output.stdout).unwrap();
    assert_eq!(task["description"], "Edited\n\nSaved\n");
}

#[test]
fn external_editor_flag_conflicts_with_status_update() {
    let dir = project();
    let output = command(dir.path())
        .args(["edit", "1", "--edit", "--set-status", "new"])
        .output()
        .unwrap();
    assert_eq!(output.status.code(), Some(2));
    assert!(String::from_utf8_lossy(&output.stderr).contains("cannot be used with"));
}