tix-tracker 0.2.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
//! Integration tests for `tix export` / `tix import`, driven through the
//! compiled binary only. Each test installs fake `glab`/`linear-cli` shell
//! scripts on `PATH` that record argv and the token env, and answers with
//! canned JSON payloads set via `TIX_FAKE_LIST`.

use std::fs;
use std::path::PathBuf;
use std::process::Output;

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

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

fn bin_dir() -> PathBuf {
    let manifest = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".into());
    PathBuf::from(manifest).join("tests").join("bin")
}

struct Harness {
    tmp: TempDir,
    record: PathBuf,
    existing_path: String,
}

impl Harness {
    fn new() -> Self {
        let tmp = TempDir::new().unwrap();
        let record = tmp.path().join("records.txt");
        let existing_path = std::env::var("PATH").unwrap_or_default();
        Harness {
            tmp,
            record,
            existing_path,
        }
    }

    fn dir(&self) -> &std::path::Path {
        self.tmp.path()
    }

    fn init(&self) {
        tix().current_dir(self.dir()).arg("init").assert().success();
    }

    fn init_with_gitlab(&self) {
        // init writes default tix.toml; append [export.gitlab]
        self.init();
        let toml_path = self.dir().join("tix.toml");
        let mut contents = fs::read_to_string(&toml_path).unwrap();
        contents.push_str("\n[export.gitlab]\nproject = \"acme/tix\"\ntoken = \"gl-file-token\"\n");
        fs::write(&toml_path, contents).unwrap();
    }

    fn init_with_linear(&self) {
        self.init();
        let toml_path = self.dir().join("tix.toml");
        let mut contents = fs::read_to_string(&toml_path).unwrap();
        contents.push_str("\n[export.linear]\nteam = \"Eng\"\ntoken = \"lin-file-token\"\n");
        fs::write(&toml_path, contents).unwrap();
    }

    fn create_issue(&self, title: &str, body: &str) -> Output {
        let out = tix()
            .current_dir(self.dir())
            .arg("create")
            .args(["--title", title])
            .args(["--body", body])
            .output()
            .unwrap();
        assert!(
            out.status.success(),
            "create failed: {}",
            String::from_utf8_lossy(&out.stderr)
        );
        out
    }

    fn export(&self, target: &str, extra_args: &[&str], env_token: Option<(&str, &str)>) -> Output {
        let mut cmd = tix();
        cmd.current_dir(self.dir())
            .arg("export")
            .args(["--target", target])
            .args(extra_args)
            .env(
                "PATH",
                format!("{}:{}", bin_dir().display(), self.existing_path),
            )
            .env("TIX_FAKE_RECORD", &self.record);
        if let Some((key, val)) = env_token {
            cmd.env(key, val);
        } else {
            // Ensure env token is NOT set so file token or native path is exercised
            cmd.env_remove("GITLAB_TOKEN").env_remove("LINEAR_TOKEN");
        }
        cmd.output().unwrap()
    }

    fn import(&self, target: &str, fake_list: &str) -> Output {
        tix()
            .current_dir(self.dir())
            .arg("import")
            .args(["--target", target])
            .env(
                "PATH",
                format!("{}:{}", bin_dir().display(), self.existing_path),
            )
            .env("TIX_FAKE_RECORD", &self.record)
            .env("TIX_FAKE_LIST", fake_list)
            .env_remove("GITLAB_TOKEN")
            .env_remove("LINEAR_TOKEN")
            .output()
            .unwrap()
    }

    fn record_contents(&self) -> String {
        fs::read_to_string(&self.record).unwrap_or_default()
    }

    fn list(&self) -> String {
        let out = tix().current_dir(self.dir()).arg("list").output().unwrap();
        assert!(out.status.success());
        String::from_utf8_lossy(&out.stdout).to_string()
    }

    fn view(&self, selector: &str) -> String {
        let out = tix()
            .current_dir(self.dir())
            .arg("view")
            .arg(selector)
            .output()
            .unwrap();
        assert!(out.status.success());
        String::from_utf8_lossy(&out.stdout).to_string()
    }
}

// -----------------------------------------------------------------------
// GitLab export tests
// -----------------------------------------------------------------------

#[test]
fn export_creates_and_backfills() {
    let h = Harness::new();
    h.init_with_gitlab();
    h.create_issue("Fix login", "users cannot log in");

    let out = h.export("gitlab", &[], Some(("GITLAB_TOKEN", "gl-env-secret")));
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout).to_string();
    assert!(stdout.contains("created gitlab issue"), "stdout: {stdout}");

    let record = h.record_contents();
    assert!(
        record.contains("glab issue create --project acme/tix"),
        "record: {record}"
    );
    assert!(
        record.contains("GITLAB_TOKEN=gl-env-secret"),
        "record: {record}"
    );

    // backfill visible via view
    let view = h.view("fix-login");
    assert!(
        view.contains("gitlab_id:"),
        "backfill missing in view: {view}"
    );
}

#[test]
fn export_dry_run_does_not_invoke_tool() {
    let h = Harness::new();
    h.init_with_gitlab();
    h.create_issue("Fix login", "users cannot log in");

    let out = h.export(
        "gitlab",
        &["--dry-run"],
        Some(("GITLAB_TOKEN", "gl-env-secret")),
    );
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout).to_string();
    assert!(
        stdout.contains("would create gitlab issue"),
        "stdout: {stdout}"
    );
    assert!(!h.record.exists(), "dry run must not create a record file");
}

#[test]
fn reexport_issues_update_not_duplicate() {
    let h = Harness::new();
    h.init_with_gitlab();
    h.create_issue("Fix login", "users cannot log in");

    // first export → creates + backfills
    h.export("gitlab", &[], Some(("GITLAB_TOKEN", "gl-env-secret")));
    let record_after_first = h.record_contents();
    assert!(record_after_first.contains("issue create"));

    // second export → updates only
    let out = h.export("gitlab", &[], Some(("GITLAB_TOKEN", "gl-env-secret")));
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout).to_string();
    assert!(stdout.contains("updated gitlab issue"), "stdout: {stdout}");

    let record = h.record_contents();
    let create_count = record.matches("issue create").count();
    let update_count = record.matches("issue update").count();
    assert_eq!(create_count, 1, "expected exactly 1 create in:\n{record}");
    assert!(
        update_count >= 1,
        "expected at least 1 update in:\n{record}"
    );
}

// -----------------------------------------------------------------------
// GitLab import tests
// -----------------------------------------------------------------------

#[test]
fn import_creates_new_local_issues() {
    let h = Harness::new();
    h.init_with_gitlab();
    let fake_list = r#"[{"iid":10,"title":"Remote bug","description":"from remote","labels":[{"name":"bug"}],"state":"opened","updated_at":"2026-05-12T08:00:00Z","assignees":[{"username":"alice"}]}]"#;
    let out = h.import("gitlab", fake_list);
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout).to_string();
    assert!(
        stdout.contains("imported gitlab issue 10"),
        "stdout: {stdout}"
    );

    let list = h.list();
    assert!(list.contains("Remote bug"), "list: {list}");

    let view = h.view("remote");
    assert!(view.contains("gitlab_id: '10'"), "backfill missing: {view}");
    assert!(view.contains("- bug"), "labels missing: {view}");
    assert!(view.contains("@alice"), "assignee missing: {view}");
    assert!(view.contains("title: Remote bug"), "title missing: {view}");
}

#[test]
fn import_filters_closed_issues() {
    let h = Harness::new();
    h.init_with_gitlab();
    let fake_list = r#"[{"iid":10,"title":"Open","state":"opened"},{"iid":20,"title":"Closed","state":"closed"}]"#;
    let out = h.import("gitlab", fake_list);
    assert!(out.status.success());
    let list = h.list();
    assert!(list.contains("Open"), "list: {list}");
    assert!(
        !list.contains("Closed"),
        "list must not contain closed: {list}"
    );
}

/// Import routes remote issues into assignee segments via `store::create`:
/// a GitLab `assignees` entry lands in its derived segment, an unassigned
/// remote issue lands in `unassigned/` (IC-06, WP03).
#[test]
fn import_routes_assignee_into_derived_segment() {
    let h = Harness::new();
    h.init_with_gitlab();
    let fake_list = r#"[
{"iid":10,"title":"Assigned remote","description":"with an owner","labels":[],"state":"opened","updated_at":"2026-05-12T08:00:00Z","assignees":[{"username":"alice"}]},
{"iid":20,"title":"Unassigned remote","description":"orphaned","labels":[],"state":"opened","updated_at":"2026-05-12T08:00:00Z"}
]"#;
    let out = h.import("gitlab", fake_list);
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout).to_string();
    assert!(
        stdout.contains("imported gitlab issue 10"),
        "stdout: {stdout}"
    );
    assert!(
        stdout.contains("imported gitlab issue 20"),
        "stdout: {stdout}"
    );

    let issues = h.dir().join("docs").join("issues");
    let md_in = |rel: &str| -> Vec<String> {
        let root = issues.join(rel);
        fs::read_dir(&root)
            .unwrap()
            .filter_map(|e| e.ok().map(|e| e.path()))
            .filter(|p| p.extension().map(|e| e == "md").unwrap_or(false))
            .map(|p| p.file_name().unwrap().to_string_lossy().to_string())
            .collect()
    };

    let assigned = md_in("alice");
    assert_eq!(assigned.len(), 1, "expected one file under alice/");
    let assigned_file = issues.join("alice").join(&assigned[0]);
    let assigned_raw = fs::read_to_string(&assigned_file).unwrap();
    assert!(
        assigned_raw.contains("assignee:") && assigned_raw.contains("alice"),
        "assigned frontmatter must carry @alice: {assigned_raw}"
    );

    let unassigned = md_in("unassigned");
    assert_eq!(
        unassigned.len(),
        1,
        "expected one file under unassigned/ (got {unassigned:?})"
    );
    let unassigned_raw =
        fs::read_to_string(issues.join("unassigned").join(&unassigned[0])).unwrap();
    assert!(
        !unassigned_raw.contains("assignee"),
        "unassigned frontmatter must omit assignee: {unassigned_raw}"
    );
}

// -----------------------------------------------------------------------
// GitLab conflict resolution
// -----------------------------------------------------------------------

fn write_issue_file(
    dir: &std::path::Path,
    filename: &str,
    gitlab_id: &str,
    title: &str,
    updated: &str,
) {
    let body = format!(
        "---\nnumber: 1\ntitle: \"{title}\"\nstatus: \"todo\"\nlabels: []\npriority: \"medium\"\ngitlab_id: \"{gitlab_id}\"\ncreated: \"2026-01-01\"\nupdated: \"{updated}\"\n---\n\nOriginal body."
    );
    fs::write(dir.join("docs").join("issues").join(filename), body).unwrap();
}

#[test]
fn import_keeps_local_when_older_remote() {
    let h = Harness::new();
    h.init_with_gitlab();
    // local issue gitlab_id=10, updated 2026-01-02 (far in the future)
    let issues_dir = h.dir().join("docs").join("issues");
    fs::create_dir_all(&issues_dir).unwrap();
    write_issue_file(
        h.dir(),
        "2026-01-01-existing.md",
        "10",
        "Existing Title",
        "2026-01-02",
    );

    let fake_list = r#"[{"iid":10,"title":"Remote Title","description":"remote body","labels":[],"state":"opened","updated_at":"2020-01-01T00:00:00Z"}]"#;
    let out = h.import("gitlab", fake_list);
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout).to_string();
    assert!(stdout.contains("kept gitlab issue 10"), "stdout: {stdout}");

    let view = h.view("existing");
    assert!(
        view.contains("title: Existing Title"),
        "title should be unchanged: {view}"
    );
    assert!(
        !view.contains("Remote Title"),
        "should not contain remote title: {view}"
    );
}

#[test]
fn import_overwrites_when_remote_newer_and_creates_bak() {
    let h = Harness::new();
    h.init_with_gitlab();
    let issues_dir = h.dir().join("docs").join("issues");
    fs::create_dir_all(&issues_dir).unwrap();
    write_issue_file(
        h.dir(),
        "2026-01-01-old.md",
        "10",
        "Old Title",
        "2020-01-01",
    );

    let fake_list = r#"[{"iid":10,"title":"New Title","description":"new body","labels":["new-label"],"state":"opened","updated_at":"2026-05-12T00:00:00Z","assignees":[{"username":"bob"}]}]"#;
    let out = h.import("gitlab", fake_list);
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout).to_string();
    assert!(stdout.contains("overwrote"), "stdout: {stdout}");

    let view = h.view("old");
    assert!(
        view.contains("title: New Title"),
        "title should be updated: {view}"
    );
    assert!(
        view.contains("gitlab_id: '10'"),
        "gitlab_id preserved: {view}"
    );
    assert!(
        view.contains("created: 2026-01-01"),
        "created must be preserved: {view}"
    );

    // .bak exists
    let bak = issues_dir.join("2026-01-01-old.md.bak");
    assert!(bak.is_file(), ".bak must exist at {}", bak.display());
    let bak_contents = fs::read_to_string(&bak).unwrap();
    assert!(
        bak_contents.contains("Old Title"),
        ".bak must hold original: {bak_contents}"
    );
}

// -----------------------------------------------------------------------
// Missing binary (exit 3)
// -----------------------------------------------------------------------

#[test]
fn export_missing_binary_exits_3() {
    let h = Harness::new();
    h.init_with_gitlab();
    h.create_issue("Fix login", "users cannot log in");

    let mut cmd = tix();
    cmd.current_dir(h.dir())
        .arg("export")
        .args(["--target", "gitlab"])
        // PATH points to a real but non-glab-producing dir: just /usr/bin
        // where no "glab" binary exists
        .env("PATH", "/usr/bin")
        .env_remove("GITLAB_TOKEN");
    let out = cmd.output().unwrap();
    assert_eq!(
        out.status.code(),
        Some(3),
        "expected exit 3, got: {:?}",
        out.status
    );
    let stderr = String::from_utf8_lossy(&out.stderr).to_string();
    assert!(
        stderr.contains(
            "required tool not found: glab — install from https://gitlab.com/gitlab-org/cli"
        ),
        "stderr: {stderr}"
    );
}

// -----------------------------------------------------------------------
// Token scrub
// -----------------------------------------------------------------------

#[test]
fn export_token_scrubbed_on_tool_failure() {
    let h = Harness::new();
    h.init_with_gitlab();
    h.create_issue("Fix login", "users cannot log in");
    // token comes from the env var; the fake leaks what it saw into stderr.
    let mut cmd = tix();
    cmd.current_dir(h.dir())
        .arg("export")
        .args(["--target", "gitlab"])
        .env("PATH", format!("{}:{}", bin_dir().display(), "/usr/bin"))
        .env("TIX_FAKE_RECORD", &h.record)
        .env("TIX_FAKE_FAIL", "1")
        .env("GITLAB_TOKEN", "gl-env-secret");
    let out = cmd.output().unwrap();
    assert_eq!(
        out.status.code(),
        Some(3),
        "expected exit 3, got: {:?}",
        out.status
    );
    let stderr = String::from_utf8_lossy(&out.stderr).to_string();
    assert!(
        !stderr.contains("gl-env-secret"),
        "token leaked to stderr: {stderr}"
    );
    assert!(
        stderr.contains("<token>"),
        "expected redaction, stderr: {stderr}"
    );
}

// -----------------------------------------------------------------------
// Linear export + import
// -----------------------------------------------------------------------

#[test]
fn linear_export_import_roundtrip() {
    let h = Harness::new();
    h.init_with_linear();
    h.create_issue("Fix login", "linear body");

    // export
    let out = h.export("linear", &[], Some(("LINEAR_TOKEN", "lin-env")));
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout).to_string();
    assert!(stdout.contains("created linear issue"), "stdout: {stdout}");
    let view = h.view("fix-login");
    assert!(view.contains("linear_id:"), "backfill missing: {view}");
    let record = h.record_contents();
    assert!(record.contains("linear-cli issue create --team Eng"));
    assert!(record.contains("LINEAR_TOKEN=lin-env"));

    // import
    let fake_list = r#"[{"identifier":"TIX-200","title":"Linear task","description":"desc","labels":["feature"],"status":{"name":"Todo"},"updated_at":"2026-05-12T00:00:00Z"}]"#;
    let out = h.import("linear", fake_list);
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout).to_string();
    assert!(
        stdout.contains("imported linear issue TIX-200"),
        "stdout: {stdout}"
    );
    let list = h.list();
    assert!(list.contains("Linear task"), "list: {list}");
}

#[test]
fn linear_export_dry_run() {
    let h = Harness::new();
    h.init_with_linear();
    h.create_issue("Task", "body");

    let out = h.export("linear", &["--dry-run"], Some(("LINEAR_TOKEN", "lin-env")));
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout).to_string();
    assert!(
        stdout.contains("would create linear issue"),
        "stdout: {stdout}"
    );
    assert!(!h.record.exists());
}

#[test]
fn linear_reexport_updates_not_duplicates() {
    let h = Harness::new();
    h.init_with_linear();
    h.create_issue("Task", "body");

    h.export("linear", &[], Some(("LINEAR_TOKEN", "lin-env")));
    let out = h.export("linear", &[], Some(("LINEAR_TOKEN", "lin-env")));
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout).to_string();
    assert!(stdout.contains("updated linear issue"), "stdout: {stdout}");
    let record = h.record_contents();
    assert_eq!(
        record.matches("issue create").count(),
        1,
        "create must happen once: {record}"
    );
    assert!(
        record.matches("issue update").count() >= 1,
        "expected at least 1 update: {record}"
    );
}

// -----------------------------------------------------------------------
// No-issues paths
// -----------------------------------------------------------------------

#[test]
fn export_nothing_to_export_prints_message() {
    let h = Harness::new();
    h.init_with_gitlab();
    let out = h.export("gitlab", &[], Some(("GITLAB_TOKEN", "gl-env")));
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout).to_string();
    assert!(stdout.contains("nothing to export"), "stdout: {stdout}");
}

#[test]
fn import_nothing_to_import_prints_message() {
    let h = Harness::new();
    h.init_with_gitlab();
    let out = h.import("gitlab", "[]");
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout).to_string();
    assert!(stdout.contains("nothing to import"), "stdout: {stdout}");
}

// -----------------------------------------------------------------------
// Missing config targets
// -----------------------------------------------------------------------

#[test]
fn export_missing_gitlab_config_fails() {
    let h = Harness::new();
    h.init(); // no [export.gitlab] section
    let out = h.export("gitlab", &[], Some(("GITLAB_TOKEN", "x")));
    assert!(!out.status.success());
    let stderr = String::from_utf8_lossy(&out.stderr).to_string();
    assert!(
        stderr.contains("[export.gitlab]") || stderr.contains("tix.toml"),
        "stderr: {stderr}"
    );
}