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
//! WP04 acceptance tests: the `tix view` saved-view CLI surface (#172;
//! mission `saved-issue-views-01M3C926`).
//!
//! Driven through the **compiled binary only**, like `tests/cli.rs`. The pure
//! rules already have their own targets (`tests/views_registry.rs`,
//! `tests/views_plan.rs`, `tests/views_service.rs`); this file is the *surface*
//! half: that the CLI routes to the shared service, that a refusal is inert,
//! that applying writes nothing, that unknown metadata is reported rather than
//! refused, and that the pre-existing `tix view <issue>` behavior is unchanged.

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

use assert_cmd::Command;
use predicates::prelude::*;
use tempfile::TempDir;

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

/// An initialized repository in a fresh temp dir.
fn repo() -> TempDir {
    let tmp = TempDir::new().unwrap();
    tix().current_dir(tmp.path()).arg("init").assert().success();
    tmp
}

/// One issue in the **`todo`** column, assigned to `alice`.
///
/// The column is named rather than inherited: a fresh repository's first column
/// is `triage` (the triage inbox's entry status), and every view in this file
/// filters `status:todo`, so relying on "the first column" would silently create
/// issues the views cannot match.
fn issue(dir: &Path, title: &str) {
    tix()
        .current_dir(dir)
        .args([
            "create",
            "--title",
            title,
            "--assignee",
            "alice",
            "--status",
            "todo",
        ])
        .assert()
        .success();
}

/// Run `tix <args>` and return stdout, asserting success.
fn stdout(dir: &Path, args: &[&str]) -> String {
    let out = tix()
        .current_dir(dir)
        .args(args)
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    String::from_utf8(out).unwrap()
}

/// Run `tix view create --name <name> <extra>` and return the generated id.
fn create_view(dir: &Path, name: &str, extra: &[&str]) -> String {
    let mut args = vec!["view", "create", "--name", name];
    args.extend_from_slice(extra);
    let text = stdout(dir, &args);
    let line = text
        .lines()
        .find_map(|line| line.strip_prefix("Created "))
        .unwrap_or_else(|| panic!("no `Created <id>` line in {text:?}"));
    line.trim().to_string()
}

fn config_path(dir: &Path) -> PathBuf {
    dir.join("tix.toml")
}

fn read_config(dir: &Path) -> String {
    fs::read_to_string(config_path(dir)).unwrap()
}

/// Every issue file under `docs/issues`, recursively, as `(path, bytes)`.
fn issue_snapshot(dir: &Path) -> Vec<(PathBuf, Vec<u8>)> {
    fn walk(dir: &Path, out: &mut Vec<(PathBuf, Vec<u8>)>) {
        for entry in fs::read_dir(dir).unwrap() {
            let path = entry.unwrap().path();
            if path.is_dir() {
                walk(&path, out);
            } else if path.extension().is_some_and(|ext| ext == "md") {
                out.push((path.clone(), fs::read(&path).unwrap()));
            }
        }
    }
    let root = dir.join("docs").join("issues");
    let mut out = Vec::new();
    walk(&root, &mut out);
    out.sort_by(|left, right| left.0.cmp(&right.0));
    assert!(!out.is_empty(), "expected at least one issue file");
    out
}

// -- CRUD round trips -----------------------------------------------------

#[test]
fn create_list_and_show_round_trip_the_normalized_plan() {
    let tmp = repo();
    let dir = tmp.path();
    let id = create_view(
        dir,
        "Active",
        &[
            "--filter",
            "status:todo",
            "--sort",
            "due",
            "--group",
            "cycle",
        ],
    );
    assert!(id.starts_with("v-"), "generated id, got {id:?}");

    let rows = stdout(dir, &["view", "list"]);
    assert_eq!(rows.lines().count(), 1, "one view: {rows:?}");
    let columns: Vec<&str> = rows.lines().next().unwrap().split('\t').collect();
    assert_eq!(
        columns,
        vec![id.as_str(), "Active", "repository", "resolved"]
    );

    let shown = stdout(dir, &["view", "show", &id]);
    assert!(shown.contains(&format!("id: {id}")), "{shown}");
    assert!(shown.contains("name: Active"), "{shown}");
    assert!(shown.contains("scope: repository"), "{shown}");
    assert!(shown.contains("query: status:todo"), "{shown}");
    assert!(shown.contains("sort: due"), "{shown}");
    assert!(shown.contains("group: cycle"), "{shown}");
}

#[test]
fn a_rename_preserves_the_stable_id_and_the_plan() {
    let tmp = repo();
    let dir = tmp.path();
    let id = create_view(dir, "Active", &["--filter", "status:todo", "--sort", "due"]);

    let updated = stdout(dir, &["view", "update", &id, "--name", "Renamed"]);
    assert!(updated.contains(&format!("Updated {id}")), "{updated}");

    let shown = stdout(dir, &["view", "show", &id]);
    assert!(shown.contains("name: Renamed"), "{shown}");
    // The id is the identity and the plan was not named, so both survive.
    assert!(shown.contains("query: status:todo"), "{shown}");
    assert!(shown.contains("sort: due"), "{shown}");
    assert_eq!(stdout(dir, &["view", "list"]).lines().count(), 1);
}

#[test]
fn a_partial_plan_update_carries_over_unnamed_fields() {
    let tmp = repo();
    let dir = tmp.path();
    let id = create_view(
        dir,
        "Triaged",
        &[
            "--filter",
            "status:todo",
            "--sort",
            "due",
            "--group",
            "cycle",
        ],
    );

    // Naming only `--sort` must replace the sort and carry over the stored
    // filter and group (WP04 review: `update` used to rebuild the plan from the
    // named flags and silently drop the rest).
    stdout(dir, &["view", "update", &id, "--sort", "created"]);

    let shown = stdout(dir, &["view", "show", &id]);
    assert!(
        shown.contains("sort: created"),
        "the named sort is applied: {shown}"
    );
    assert!(
        shown.contains("query: status:todo"),
        "the stored filter survives: {shown}"
    );
    assert!(
        shown.contains("group: cycle"),
        "the stored group survives: {shown}"
    );
}

#[test]
fn an_update_that_changes_nothing_reports_unchanged() {
    let tmp = repo();
    let dir = tmp.path();
    let id = create_view(dir, "Active", &["--filter", "status:todo"]);
    let before = read_config(dir);

    let again = stdout(dir, &["view", "update", &id, "--name", "Active"]);
    assert!(again.contains(&format!("Unchanged {id}")), "{again}");
    assert_eq!(
        read_config(dir),
        before,
        "an idempotent update writes nothing"
    );
}

#[test]
fn duplicate_allocates_a_new_id_and_copies_the_plan() {
    let tmp = repo();
    let dir = tmp.path();
    let id = create_view(dir, "Active", &["--filter", "status:todo", "--sort", "due"]);

    let duplicated = stdout(dir, &["view", "duplicate", &id, "--name", "Copy"]);
    let copy = duplicated
        .lines()
        .find_map(|line| line.strip_prefix("Duplicated "))
        .unwrap()
        .trim()
        .to_string();
    assert_ne!(copy, id, "a duplicate gets a new stable id");

    let shown = stdout(dir, &["view", "show", &copy]);
    assert!(shown.contains("name: Copy"), "{shown}");
    assert!(shown.contains("query: status:todo"), "{shown}");
    assert!(shown.contains("sort: due"), "{shown}");
    assert_eq!(stdout(dir, &["view", "list"]).lines().count(), 2);

    // The stable ids are never reused: the source is still selectable.
    assert!(stdout(dir, &["view", "show", &id]).contains("name: Active"));
}

#[test]
fn share_replaces_only_the_scope_metadata() {
    let tmp = repo();
    let dir = tmp.path();
    let id = create_view(dir, "Active", &["--filter", "status:todo"]);

    let shared = stdout(
        dir,
        &[
            "view",
            "share",
            &id,
            "--scope",
            "user",
            "--owner",
            "alice",
            "--shared-with",
            "bob",
        ],
    );
    assert!(shared.contains(&format!("Shared {id}")), "{shared}");

    let shown = stdout(dir, &["view", "show", &id]);
    assert!(
        shown.contains("scope: user owner=alice shared_with=bob"),
        "{shown}"
    );
    assert!(
        shown.contains("query: status:todo"),
        "the plan survived: {shown}"
    );

    // The scope filter reads the same metadata.
    let user_rows = stdout(dir, &["view", "list", "--scope", "user"]);
    assert!(user_rows.contains(&id), "{user_rows}");
    assert!(stdout(dir, &["view", "list", "--scope", "project"]).is_empty());
}

// -- scope conflicts ------------------------------------------------------

#[test]
fn a_scope_conflict_is_refused_and_writes_no_view() {
    let tmp = repo();
    let dir = tmp.path();
    let before = read_config(dir);

    let conflicts: &[&[&str]] = &[
        // repository (the default) must not name a project
        &["view", "create", "--name", "R", "--project", "p1"],
        // project scope requires a project id
        &["view", "create", "--name", "P", "--scope", "project"],
        // user scope requires an owner or a shared-with identity
        &["view", "create", "--name", "U", "--scope", "user"],
    ];
    for args in conflicts {
        tix()
            .current_dir(dir)
            .args(*args)
            .assert()
            .failure()
            .code(1)
            .stderr(predicate::str::contains("scope_conflict"));
    }
    assert_eq!(read_config(dir), before, "a refusal writes no view");
    assert!(stdout(dir, &["view", "list"]).is_empty());
}

#[test]
fn a_project_scope_that_does_not_resolve_is_refused() {
    let tmp = repo();
    let dir = tmp.path();
    // This build ships no first-class project registry, so a project scope is
    // refused by the registry rather than silently accepted.
    tix()
        .current_dir(dir)
        .args([
            "view",
            "create",
            "--name",
            "P",
            "--scope",
            "project",
            "--project",
            "p-platform",
        ])
        .assert()
        .failure()
        .code(1)
        .stderr(predicate::str::contains("not in the project registry"));
    assert!(stdout(dir, &["view", "list"]).is_empty());
}

#[test]
fn an_unknown_view_is_a_not_found_exit() {
    let tmp = repo();
    let dir = tmp.path();
    for args in [
        vec!["view", "show", "nope"],
        vec!["view", "apply", "nope"],
        vec!["view", "delete", "nope", "--confirm"],
    ] {
        tix()
            .current_dir(dir)
            .args(&args)
            .assert()
            .failure()
            .code(2)
            .stderr(predicate::str::contains("no saved view matches"));
    }
}

// -- deletion safety ------------------------------------------------------

#[test]
fn delete_without_confirmation_is_refused_and_writes_nothing() {
    let tmp = repo();
    let dir = tmp.path();
    let id = create_view(dir, "Active", &["--filter", "status:todo"]);
    let before = read_config(dir);

    tix()
        .current_dir(dir)
        .args(["view", "delete", &id])
        .assert()
        .failure()
        .code(1)
        .stderr(predicate::str::contains("--confirm"));

    assert_eq!(
        read_config(dir),
        before,
        "an unconfirmed delete writes nothing"
    );
    assert!(stdout(dir, &["view", "show", &id]).contains("name: Active"));
}

#[test]
fn delete_removes_only_the_definition() {
    let tmp = repo();
    let dir = tmp.path();
    issue(dir, "Fix login");
    let id = create_view(dir, "Active", &["--filter", "status:todo"]);
    let issues_before = issue_snapshot(dir);

    let deleted = stdout(dir, &["view", "delete", &id, "--confirm"]);
    assert!(deleted.contains(&format!("Deleted {id}")), "{deleted}");

    assert!(!read_config(dir).contains(&id), "the definition is gone");
    assert!(stdout(dir, &["view", "list"]).is_empty());
    // Every issue file is byte-identical: only the definition was removed.
    assert_eq!(issue_snapshot(dir), issues_before);
}

// -- apply / divergence ---------------------------------------------------

#[test]
fn apply_is_read_only_and_reports_the_result_and_active_marker() {
    let tmp = repo();
    let dir = tmp.path();
    issue(dir, "Fix login");
    issue(dir, "Ship board");
    let id = create_view(dir, "Active", &["--filter", "status:todo"]);

    let config_before = read_config(dir);
    let issues_before = issue_snapshot(dir);

    let applied = stdout(dir, &["view", "apply", &id]);
    assert!(applied.contains(&format!("id: {id}")), "{applied}");
    assert!(applied.contains("query: status:todo"), "{applied}");
    assert!(applied.contains(&format!("active: {id}")), "{applied}");
    // The two issues as a **set**: the apply lists them in the board's order,
    // which ties on `created` and falls back to the filename — and a filename now
    // carries the random issue id (tix #1587), so the order within one day is not
    // a property to pin. Both issues matching is.
    let mut listed: Vec<&str> = applied
        .lines()
        .find_map(|line| line.strip_prefix("issues: "))
        .expect("the apply names the matching issues")
        .split(',')
        .collect();
    listed.sort_unstable();
    assert_eq!(listed, vec!["1", "2"], "{applied}");
    assert!(applied.contains("count: 2"), "{applied}");

    // C-005: applying writes no issue, no comment, and no view configuration.
    assert_eq!(read_config(dir), config_before, "apply wrote tix.toml");
    assert_eq!(issue_snapshot(dir), issues_before, "apply wrote an issue");
}

#[test]
fn status_reports_field_level_divergence_and_in_sync() {
    let tmp = repo();
    let dir = tmp.path();
    let id = create_view(dir, "Active", &["--filter", "status:todo", "--sort", "due"]);

    let divergent = stdout(dir, &["view", "status", &id, "--filter", "label:urgent"]);
    assert!(divergent.contains("in-sync: false"), "{divergent}");
    assert!(
        divergent.contains("difference: plan.text_query saved=status:todo live=label:urgent"),
        "{divergent}"
    );
    assert!(
        divergent.contains("difference: plan.sort saved=due live=(unset)"),
        "{divergent}"
    );

    let matching = stdout(
        dir,
        &[
            "view",
            "status",
            &id,
            "--filter",
            "status:todo",
            "--sort",
            "due",
        ],
    );
    assert!(matching.contains("in-sync: true"), "{matching}");
    assert!(!matching.contains("difference:"), "{matching}");
}

#[test]
fn status_without_an_id_reports_the_missing_marker_instead_of_in_sync() {
    let tmp = repo();
    let dir = tmp.path();
    let out = stdout(dir, &["view", "status"]);
    assert!(out.contains("in-sync: false"), "{out}");
    assert!(
        out.contains("no active saved view is selected"),
        "a missing marker is an explicit diagnostic: {out}"
    );
}

// -- unknown metadata -----------------------------------------------------

#[test]
fn unknown_metadata_is_reported_not_refused_and_survives_an_update() {
    let tmp = repo();
    let dir = tmp.path();
    let id = create_view(dir, "Active", &["--filter", "status:todo"]);

    // A newer `tix` wrote a key this build does not interpret.
    let config = read_config(dir);
    let injected = config.replace(
        &format!("[views.{id}]"),
        &format!("[views.{id}]\nx-extra = \"kept\""),
    );
    assert_ne!(config, injected, "the view section must exist to augment");
    fs::write(config_path(dir), &injected).unwrap();

    // Reported, never refused: the read succeeds and names the field.
    let shown = stdout(dir, &["view", "show", &id]);
    assert!(shown.contains("unknown_metadata"), "{shown}");
    assert!(shown.contains("x-extra"), "{shown}");

    // NFR-002: an accepted update preserves it byte-for-byte.
    stdout(dir, &["view", "update", &id, "--name", "Renamed"]);
    assert!(
        read_config(dir).contains("x-extra = \"kept\""),
        "an update must not drop unknown metadata"
    );
}

// -- plan vocabulary ------------------------------------------------------

#[test]
fn visibility_is_an_absolute_snapshot_and_invalid_columns_are_refused() {
    let tmp = repo();
    let dir = tmp.path();
    let id = create_view(
        dir,
        "Board",
        &["--hide-columns", "done", "--show-mismatch", "true"],
    );
    let shown = stdout(dir, &["view", "show", &id]);
    assert!(shown.contains("hide-columns: done"), "{shown}");
    assert!(shown.contains("show-mismatch: true"), "{shown}");

    tix()
        .current_dir(dir)
        .args(["view", "create", "--name", "Bad", "--hide-columns", "nope"])
        .assert()
        .failure()
        .code(1)
        .stderr(predicate::str::contains("does not configure"));
}

#[test]
fn an_invalid_sort_key_is_refused_before_persistence() {
    let tmp = repo();
    let dir = tmp.path();
    let before = read_config(dir);
    tix()
        .current_dir(dir)
        .args(["view", "create", "--name", "S", "--sort", "bogus"])
        .assert()
        .failure()
        .code(1)
        .stderr(predicate::str::contains("not a supported sort key"));
    assert_eq!(read_config(dir), before);
}

// -- the pre-existing surface --------------------------------------------

#[test]
fn printing_an_issue_and_the_existing_query_surfaces_are_unchanged() {
    let tmp = repo();
    let dir = tmp.path();
    issue(dir, "Fix login bug");

    // `tix view <issue>` still prints the issue file.
    let printed = stdout(dir, &["view", "1"]);
    assert!(printed.contains("title: Fix login bug"), "{printed}");

    // `tix show <issue>` (the alias) still works.
    assert!(stdout(dir, &["show", "1"]).contains("title: Fix login bug"));

    // `--show relationships` is still accepted, and an unknown value still fails.
    tix()
        .current_dir(dir)
        .args(["view", "1", "--show", "relationships"])
        .assert()
        .success();
    tix()
        .current_dir(dir)
        .args(["view", "1", "--show", "bogus"])
        .assert()
        .failure()
        .code(1)
        .stderr(predicate::str::contains("unknown --show value"));

    // The existing list/status query surfaces keep working beside the views.
    assert!(stdout(dir, &["list", "--filter", "status:todo"]).contains("Fix login bug"));
    assert!(stdout(dir, &["status"]).contains("Total"));

    // Bare `tix view` is a usage error, not a silent success.
    tix()
        .current_dir(dir)
        .arg("view")
        .assert()
        .failure()
        .code(1)
        .stderr(predicate::str::contains(
            "needs an issue selector or a subcommand",
        ));
}