tix-tracker 0.12.0

A terminal issue tracker: markdown + YAML frontmatter issues with a kanban TUI and CLI
Documentation
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
//! WP04 (T022) — the `tix template` CLI and `tix create --template`, driven
//! through the **compiled binary** only (never a library call), the same way
//! `tests/cli.rs` drives the rest of the command surface.
//!
//! What is proven here, requirement by requirement:
//!
//! - FR-001: create / update / duplicate / delete round-trip through the CLI and
//!   land in `tix.toml`;
//! - C-002: a rename changes the display name and nothing else;
//! - C-006/NFR-003: unknown metadata survives a read, an update, and a save;
//! - FR-002/FR-003: `tix create --template` resolves defaults/prompts and writes
//!   exactly one issue through the shared create core;
//! - FR-004/C-005: a missing required prompt or an unknown reference exits
//!   non-zero, prints **every** diagnostic, and leaves the template and the issue
//!   tree byte-identical;
//! - the regression guarantee: `tix create` without `--template` behaves exactly
//!   as before.
//!
//! TTY-free and network-free: every project is a `TempDir`, nothing reads a
//! clock, and stdin is a pipe (so the delete confirmation auto-confirms).

use std::fs;
use std::path::{Path, PathBuf};

use assert_cmd::Command;
use tempfile::TempDir;

fn tix() -> Command {
    Command::cargo_bin("tix").unwrap()
}

/// A project with the default `tix.toml` and starter board.
fn project() -> TempDir {
    let tmp = TempDir::new().unwrap();
    let mut cmd = tix();
    cmd.current_dir(tmp.path()).arg("init").assert().success();
    tmp
}

/// Run a command that must succeed and return its stdout.
fn stdout(dir: &Path, args: &[&str]) -> String {
    let mut cmd = tix();
    cmd.current_dir(dir).args(args);
    let output = cmd.assert().success().get_output().clone();
    String::from_utf8(output.stdout).unwrap()
}

/// Run a command that must fail and return `(code, stdout, stderr)`.
fn failure(dir: &Path, args: &[&str]) -> (i32, String, String) {
    let mut cmd = tix();
    cmd.current_dir(dir).args(args);
    let output = cmd.assert().failure().get_output().clone();
    (
        output.status.code().unwrap_or(-1),
        String::from_utf8(output.stdout).unwrap(),
        String::from_utf8(output.stderr).unwrap(),
    )
}

/// Every `.md` file under `dir`, recursively and sorted.
fn issue_files_under(dir: &Path) -> Vec<PathBuf> {
    let mut out = Vec::new();
    walk(dir, &mut out);
    out.sort();
    out
}

fn walk(dir: &Path, out: &mut Vec<PathBuf>) {
    let Ok(entries) = fs::read_dir(dir) else {
        return;
    };
    for entry in entries.flatten() {
        let path = entry.path();
        let name = path.file_name().unwrap().to_string_lossy().to_string();
        if path.is_dir() {
            if name == "boards" || name.starts_with('.') {
                continue;
            }
            walk(&path, out);
        } else if path.extension().is_some_and(|e| e == "md") {
            out.push(path);
        }
    }
}

/// The issue files `tix create` wrote (`docs/issues/…`), excluding the seeded
/// board and milestone files.
fn issues(dir: &Path) -> Vec<PathBuf> {
    issue_files_under(&dir.join("docs").join("issues"))
}

fn read_issues(dir: &Path) -> String {
    issues(dir)
        .iter()
        .map(|p| fs::read_to_string(p).unwrap())
        .collect::<Vec<_>>()
        .join("\n---\n")
}

/// Create the "bug" template used by the create tests, exactly as the
/// quickstart spells it.
fn create_bug_template(dir: &Path) {
    stdout(
        dir,
        &[
            "template",
            "create",
            "--id",
            "bug",
            "--name",
            "Bug report",
            "--scope",
            "repository",
            "--default",
            "status=todo",
            "--default",
            "priority=high",
            "--prompt",
            "key=title,kind=text,required=true",
            "--prompt",
            "key=labels,kind=multi-select,required=true,options_ref=labels",
            "--reference",
            "kind=label,value=bug,required=true",
        ],
    );
}

// ---------------------------------------------------------------------------
// T018/T019 — the management tree and structured output
// ---------------------------------------------------------------------------

#[test]
fn template_create_list_and_show_expose_defaults_prompts_and_references() {
    let tmp = project();
    let dir = tmp.path();
    create_bug_template(dir);

    // `list` is one tab-separated row: id, display name, explicit scope.
    let listed = stdout(dir, &["template", "list"]);
    assert_eq!(
        listed.lines().collect::<Vec<_>>(),
        vec!["bug\tBug report\trepository"],
        "{listed:?}"
    );

    // `show` renders every stored part as a stable `key: value` block.
    let shown = stdout(dir, &["template", "show", "bug"]);
    for expected in [
        "id: bug",
        "name: Bug report",
        "scope: repository",
        "defaults.status: todo",
        "defaults.priority: high",
        "prompt[0].key: title",
        "prompt[0].kind: text",
        "prompt[0].required: true",
        "prompt[1].key: labels",
        "prompt[1].kind: multi-select",
        "prompt[1].options_ref: labels",
        "reference[0].kind: label",
        "reference[0].value: bug",
        "reference[0].required: true",
    ] {
        assert!(
            shown.contains(expected),
            "missing {expected:?} in:\n{shown}"
        );
    }
}

#[test]
fn template_list_on_a_project_with_no_templates_prints_nothing() {
    let tmp = project();
    assert_eq!(stdout(tmp.path(), &["template", "list"]), "");
}

#[test]
fn template_show_of_an_unknown_reference_is_not_found() {
    let tmp = project();
    let (code, out, err) = failure(tmp.path(), &["template", "show", "nope"]);
    assert_eq!(code, 2, "stderr: {err}");
    assert_eq!(out, "");
    assert!(err.contains("no template with id or name `nope`"), "{err}");
}

#[test]
fn template_create_refuses_an_unknown_create_field_and_writes_nothing() {
    let tmp = project();
    let dir = tmp.path();
    let before = fs::read(dir.join("tix.toml")).unwrap();

    let (code, _, err) = failure(
        dir,
        &["template", "create", "--id", "bad", "--default", "nope=1"],
    );
    assert_eq!(code, 1, "stderr: {err}");
    assert!(err.contains("`nope` is not a create field"), "{err}");
    assert_eq!(fs::read(dir.join("tix.toml")).unwrap(), before);
}

// ---------------------------------------------------------------------------
// FR-001 / C-002 — rename, duplicate, delete
// ---------------------------------------------------------------------------

#[test]
fn template_update_renames_without_touching_the_id_scope_or_data() {
    let tmp = project();
    let dir = tmp.path();
    create_bug_template(dir);

    let renamed = stdout(
        dir,
        &["template", "update", "bug", "--name", "Incident report"],
    );
    assert!(renamed.contains("updated template `bug`"), "{renamed}");

    // The ID is still the handle; only the label changed.
    let shown = stdout(dir, &["template", "show", "bug"]);
    assert!(shown.contains("id: bug"), "{shown}");
    assert!(shown.contains("name: Incident report"), "{shown}");
    assert!(shown.contains("scope: repository"), "{shown}");
    assert!(shown.contains("defaults.priority: high"), "{shown}");
    assert!(shown.contains("prompt[1].key: labels"), "{shown}");
    assert!(stdout(dir, &["template", "list"]).contains("bug\tIncident report\trepository"));
}

#[test]
fn template_duplicate_allocates_a_new_id_and_leaves_the_source_intact() {
    let tmp = project();
    let dir = tmp.path();
    create_bug_template(dir);

    let duplicated = stdout(dir, &["template", "duplicate", "bug", "--id", "bug-copy"]);
    assert!(
        duplicated.contains("duplicated template `bug` as `bug-copy`"),
        "{duplicated}"
    );

    let listed = stdout(dir, &["template", "list"]);
    assert!(listed.contains("bug\tBug report\trepository"), "{listed}");
    assert!(
        listed.contains("bug-copy\tBug report\trepository"),
        "{listed}"
    );
    // The copy carries the source's defaults, verbatim.
    let copy = stdout(dir, &["template", "show", "bug-copy"]);
    assert!(copy.contains("id: bug-copy"), "{copy}");
    assert!(copy.contains("defaults.priority: high"), "{copy}");
}

#[test]
fn template_duplicate_without_an_id_allocates_the_documented_copy_id() {
    let tmp = project();
    let dir = tmp.path();
    create_bug_template(dir);

    let duplicated = stdout(dir, &["template", "duplicate", "bug"]);
    assert!(
        duplicated.contains("duplicated template `bug` as `bug-copy`"),
        "{duplicated}"
    );
    assert!(stdout(dir, &["template", "list"]).contains("bug-copy"));
}

#[test]
fn template_delete_removes_only_the_definition_and_never_an_issue() {
    let tmp = project();
    let dir = tmp.path();
    create_bug_template(dir);

    // An issue created from the template is a file in the issue tree.
    stdout(
        dir,
        &[
            "create",
            "--template",
            "bug",
            "--title",
            "Crash on login",
            "--labels",
            "bug",
        ],
    );
    assert_eq!(issues(dir).len(), 1);

    // stdin is a pipe, so the confirmation auto-confirms like `cycle delete`.
    let deleted = stdout(dir, &["template", "delete", "bug"]);
    assert!(deleted.contains("deleted template `bug`"), "{deleted}");
    assert_eq!(stdout(dir, &["template", "list"]), "");
    assert_eq!(
        issues(dir).len(),
        1,
        "delete must never touch an issue file"
    );

    let (code, _, err) = failure(dir, &["template", "show", "bug"]);
    assert_eq!(code, 2, "stderr: {err}");
}

// ---------------------------------------------------------------------------
// C-006 / NFR-003 — unknown metadata round-trips
// ---------------------------------------------------------------------------

#[test]
fn template_show_and_update_preserve_unknown_metadata() {
    let tmp = TempDir::new().unwrap();
    let dir = tmp.path();
    fs::write(
        dir.join("tix.toml"),
        "issues_dir = \"docs/issues\"\n\n\
         [issue_templates.legacy]\n\
         name = \"Legacy\"\n\
         owner = \"team-x\"\n\
         retention_days = 30\n",
    )
    .unwrap();

    let before = stdout(dir, &["template", "show", "legacy"]);
    assert!(before.contains("unknown.owner:"), "{before}");
    assert!(before.contains("team-x"), "{before}");
    assert!(before.contains("unknown.retention_days:"), "{before}");

    stdout(
        dir,
        &["template", "update", "legacy", "--name", "Legacy v2"],
    );

    // The update changed the name and left the unknown keys alone.
    let after = stdout(dir, &["template", "show", "legacy"]);
    assert!(after.contains("name: Legacy v2"), "{after}");
    assert!(after.contains("unknown.owner:"), "{after}");
    assert!(after.contains("team-x"), "{after}");
    assert!(after.contains("retention_days"), "{after}");
}

// ---------------------------------------------------------------------------
// T020/FR-002/FR-003 — template-aware creation through the shared core
// ---------------------------------------------------------------------------

#[test]
fn create_from_template_merges_defaults_and_writes_one_issue() {
    let tmp = project();
    let dir = tmp.path();
    create_bug_template(dir);

    let mut cmd = tix();
    let output = cmd
        .current_dir(dir)
        .args([
            "create",
            "--template",
            "bug",
            "--title",
            "Crash on login",
            "--labels",
            "bug",
        ])
        .assert()
        .success()
        .get_output()
        .clone();
    let out = String::from_utf8(output.stdout).unwrap();
    let err = String::from_utf8(output.stderr).unwrap();
    assert!(out.starts_with("Created "), "{out}");
    // Provenance is visible without polluting the parseable stdout shape.
    assert!(
        err.contains("using template `bug` at scope `repository`"),
        "{err}"
    );

    assert_eq!(issues(dir).len(), 1);
    let contents = read_issues(dir);
    assert!(contents.contains("priority: high"), "{contents}");
    assert!(contents.contains("status: todo"), "{contents}");
    assert!(contents.contains("Crash on login"), "{contents}");
}

#[test]
fn a_user_flag_overrides_a_template_default() {
    let tmp = project();
    let dir = tmp.path();
    create_bug_template(dir);

    stdout(
        dir,
        &[
            "create",
            "--template",
            "bug",
            "--title",
            "Crash on login",
            "--labels",
            "bug",
            "--priority",
            "low",
        ],
    );
    let contents = read_issues(dir);
    assert!(contents.contains("priority: low"), "{contents}");
    assert!(!contents.contains("priority: high"), "{contents}");
}

#[test]
fn a_board_scoped_template_is_reachable_by_its_explicit_scope() {
    let tmp = project();
    let dir = tmp.path();
    stdout(
        dir,
        &[
            "template",
            "create",
            "--id",
            "board-bug",
            "--name",
            "Board bug",
            "--scope",
            "board:default",
            "--default",
            "status=todo",
        ],
    );

    let mut cmd = tix();
    let output = cmd
        .current_dir(dir)
        .args([
            "create",
            "--template",
            "board-bug",
            "--scope",
            "board:default",
            "--title",
            "Board scoped",
        ])
        .assert()
        .success()
        .get_output()
        .clone();
    let err = String::from_utf8(output.stderr).unwrap();
    assert!(
        err.contains("using template `board-bug` at scope `board:default`"),
        "{err}"
    );
    assert_eq!(issues(dir).len(), 1);
}

// ---------------------------------------------------------------------------
// T021/FR-004/C-005 — all diagnostics, no partial write
// ---------------------------------------------------------------------------

#[test]
fn create_from_template_with_a_missing_required_prompt_writes_nothing() {
    let tmp = project();
    let dir = tmp.path();
    create_bug_template(dir);
    let config_before = fs::read(dir.join("tix.toml")).unwrap();

    // `--labels` is a required prompt with no value: the resolver reports it
    // and no issue is written.
    let (code, out, err) = failure(
        dir,
        &["create", "--template", "bug", "--title", "Crash on login"],
    );
    assert_eq!(code, 1, "stderr: {err}");
    assert_eq!(out, "", "a failed resolution must not print `Created`");
    assert!(
        err.contains("template `bug` at scope `repository` did not resolve"),
        "{err}"
    );
    assert!(
        err.contains("missing-required") && err.contains("labels"),
        "the missing prompt must be named: {err}"
    );

    assert!(issues(dir).is_empty(), "no partial issue may be written");
    assert_eq!(fs::read(dir.join("tix.toml")).unwrap(), config_before);
}

#[test]
fn create_from_template_with_an_unknown_reference_writes_nothing() {
    let tmp = project();
    let dir = tmp.path();
    stdout(
        dir,
        &[
            "template",
            "create",
            "--id",
            "planned",
            "--name",
            "Planned",
            "--default",
            "status=todo",
            "--reference",
            "kind=cycle,value=no-such-cycle,required=true",
        ],
    );

    let (code, out, err) = failure(dir, &["create", "--template", "planned", "--title", "X"]);
    assert_eq!(code, 1, "stderr: {err}");
    assert_eq!(out, "");
    assert!(
        err.contains("unknown-reference") && err.contains("no-such-cycle"),
        "{err}"
    );
    assert!(issues(dir).is_empty());
}

#[test]
fn create_from_template_lists_every_diagnostic_at_once() {
    let tmp = project();
    let dir = tmp.path();
    // A template requiring both a title and labels: neither supplied.
    stdout(
        dir,
        &[
            "template",
            "create",
            "--id",
            "strict",
            "--name",
            "Strict",
            "--default",
            "status=todo",
            "--prompt",
            "key=title,kind=text,required=true",
            "--prompt",
            "key=labels,kind=multi-select,required=true,options_ref=labels",
        ],
    );

    let (code, _, err) = failure(dir, &["create", "--template", "strict"]);
    assert_eq!(code, 1, "stderr: {err}");
    assert!(err.contains("title"), "title diagnostic missing: {err}");
    assert!(err.contains("labels"), "labels diagnostic missing: {err}");
    assert!(issues(dir).is_empty());
}

#[test]
fn create_from_template_with_an_unknown_id_is_reported() {
    let tmp = project();
    let dir = tmp.path();
    let (code, out, err) = failure(dir, &["create", "--template", "ghost", "--title", "X"]);
    assert_eq!(code, 1, "stderr: {err}");
    assert_eq!(out, "");
    assert!(err.contains("unknown-template"), "{err}");
    assert!(issues(dir).is_empty());
}

#[test]
fn create_from_template_with_a_mismatched_scope_writes_nothing() {
    let tmp = project();
    let dir = tmp.path();
    create_bug_template(dir);

    let (code, _, err) = failure(
        dir,
        &[
            "create",
            "--template",
            "bug",
            "--scope",
            "board:default",
            "--title",
            "Crash",
            "--labels",
            "bug",
        ],
    );
    assert_eq!(code, 1, "stderr: {err}");
    assert!(err.contains("scope-mismatch"), "{err}");
    assert!(issues(dir).is_empty());
}

// ---------------------------------------------------------------------------
// Regression — the no-template create path is untouched
// ---------------------------------------------------------------------------

#[test]
fn create_without_a_template_behaves_exactly_as_before() {
    let tmp = project();
    let dir = tmp.path();
    // A registry is present, yet the normal path must ignore it entirely.
    create_bug_template(dir);

    let mut cmd = tix();
    let output = cmd
        .current_dir(dir)
        .args(["create", "--title", "Plain issue", "--priority", "high"])
        .assert()
        .success()
        .get_output()
        .clone();
    let out = String::from_utf8(output.stdout).unwrap();
    let err = String::from_utf8(output.stderr).unwrap();
    assert_eq!(out.lines().count(), 1, "stdout: {out:?}");
    assert!(out.starts_with("Created "), "{out:?}");
    assert!(
        !err.contains("using template"),
        "the no-template path must not mention a template: {err}"
    );

    assert_eq!(issues(dir).len(), 1);
    let contents = read_issues(dir);
    assert!(contents.contains("priority: high"), "{contents}");
    assert!(contents.contains("Plain issue"), "{contents}");
}

#[test]
fn scope_without_a_template_is_a_usage_error() {
    let tmp = project();
    let dir = tmp.path();
    let (code, _, err) = failure(
        dir,
        &["create", "--scope", "repository", "--title", "Plain"],
    );
    // clap's usage error code, matching the rest of the CLI surface.
    assert_eq!(code, 2, "stderr: {err}");
    assert!(err.contains("--template"), "{err}");
    assert!(issues(dir).is_empty());
}