itack 0.1.2

Git-backed issue tracker for multi-agent coordination
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
//! End-to-end CLI tests.

use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use std::path::Path;
use tempfile::TempDir;

/// Read an issue file from the data/itack branch.
/// Returns the content of the issue file.
fn read_issue_from_data_branch(repo_path: &Path, id: u32) -> Option<String> {
    use std::process::Command;

    let suffix = format!("-issue-{:03}.md", id);

    // List files in .itack/ on data/itack branch
    let output = Command::new("git")
        .args(["ls-tree", "--name-only", "data/itack", ".itack/"])
        .current_dir(repo_path)
        .output()
        .ok()?;

    if !output.status.success() {
        return None;
    }

    let files = String::from_utf8_lossy(&output.stdout);
    let matching_file = files.lines().find(|f| f.ends_with(&suffix))?;

    // Read the file content from the branch
    let output = Command::new("git")
        .args(["show", &format!("data/itack:{}", matching_file)])
        .current_dir(repo_path)
        .output()
        .ok()?;

    if output.status.success() {
        Some(String::from_utf8_lossy(&output.stdout).to_string())
    } else {
        None
    }
}

/// Test environment with isolated git repo and database directory.
struct TestEnv {
    /// Temporary git repository.
    repo: TempDir,
    /// Temporary directory for ITACK_HOME (database storage).
    itack_home: TempDir,
}

impl TestEnv {
    fn path(&self) -> &Path {
        self.repo.path()
    }

    fn itack_home_str(&self) -> &str {
        self.itack_home.path().to_str().unwrap()
    }
}

fn itack(env: &TestEnv) -> Command {
    let mut cmd = Command::cargo_bin("itack").unwrap();
    cmd.env("ITACK_HOME", env.itack_home_str());
    cmd
}

fn setup_git_repo() -> TestEnv {
    let repo = TempDir::new().unwrap();
    let itack_home = TempDir::new().unwrap();

    // Initialize git repo
    std::process::Command::new("git")
        .args(["init"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to init git repo");

    // Configure git user for the repo
    std::process::Command::new("git")
        .args(["config", "user.email", "test@test.com"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to configure git email");

    std::process::Command::new("git")
        .args(["config", "user.name", "Test User"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to configure git name");

    TestEnv { repo, itack_home }
}

#[test]
fn test_init_creates_itack_directory() {
    let env = setup_git_repo();

    itack(&env)
        .arg("init")
        .current_dir(env.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("Initialized itack project"));

    assert!(env.path().join(".itack").exists());
    assert!(env.path().join(".itack/metadata.toml").exists());
}

#[test]
fn test_init_fails_without_git() {
    let dir = TempDir::new().unwrap();
    let itack_home = TempDir::new().unwrap();

    Command::cargo_bin("itack")
        .unwrap()
        .env("ITACK_HOME", itack_home.path())
        .arg("init")
        .current_dir(dir.path())
        .assert()
        .failure()
        .stderr(predicate::str::contains("Not in a git repository"));
}

#[test]
fn test_init_repairs_if_already_initialized() {
    let env = setup_git_repo();

    itack(&env)
        .arg("init")
        .current_dir(env.path())
        .assert()
        .success();

    // Running init again should repair/succeed (not fail)
    itack(&env)
        .arg("init")
        .current_dir(env.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("Repaired database"));
}

#[test]
fn test_create_and_show_issue() {
    let env = setup_git_repo();

    itack(&env)
        .arg("init")
        .current_dir(env.path())
        .assert()
        .success();

    itack(&env)
        .args(["create", "Test issue", "--epic", "MVP"])
        .current_dir(env.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("Created issue #1"));

    // Verify file was created in data branch
    let content = read_issue_from_data_branch(env.path(), 1);
    assert!(content.is_some(), "Issue file should exist in data branch");

    // Show the issue
    itack(&env)
        .args(["show", "1"])
        .current_dir(env.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("Test issue"))
        .stdout(predicate::str::contains("MVP"));

    // Show as JSON
    itack(&env)
        .args(["show", "1", "--json"])
        .current_dir(env.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("\"title\": \"Test issue\""));
}

#[test]
fn test_show_nonexistent_issue() {
    let env = setup_git_repo();

    itack(&env)
        .arg("init")
        .current_dir(env.path())
        .assert()
        .success();

    itack(&env)
        .args(["show", "999"])
        .current_dir(env.path())
        .assert()
        .failure()
        .stderr(predicate::str::contains("Issue 999 not found"));
}

#[test]
fn test_list_issues() {
    let env = setup_git_repo();

    itack(&env)
        .arg("init")
        .current_dir(env.path())
        .assert()
        .success();

    itack(&env)
        .args(["create", "First issue"])
        .current_dir(env.path())
        .assert()
        .success();

    itack(&env)
        .args(["create", "Second issue", "--epic", "MVP"])
        .current_dir(env.path())
        .assert()
        .success();

    // List all
    itack(&env)
        .arg("list")
        .current_dir(env.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("First issue"))
        .stdout(predicate::str::contains("Second issue"));

    // List with epic filter
    itack(&env)
        .args(["list", "--epic", "MVP"])
        .current_dir(env.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("Second issue"))
        .stdout(predicate::str::contains("First issue").not());

    // List as JSON
    itack(&env)
        .args(["list", "--json"])
        .current_dir(env.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("\"id\": 1"))
        .stdout(predicate::str::contains("\"id\": 2"));
}

#[test]
fn test_done_command() {
    let env = setup_git_repo();

    itack(&env)
        .arg("init")
        .current_dir(env.path())
        .assert()
        .success();

    itack(&env)
        .args(["create", "Test issue"])
        .current_dir(env.path())
        .assert()
        .success();

    // Mark as done
    itack(&env)
        .args(["done", "1"])
        .current_dir(env.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("open -> done"));

    // Verify in list
    itack(&env)
        .args(["list", "--status", "done"])
        .current_dir(env.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("Test issue"));
}

#[test]
fn test_claim_and_release() {
    let env = setup_git_repo();

    itack(&env)
        .arg("init")
        .current_dir(env.path())
        .assert()
        .success();

    itack(&env)
        .args(["create", "Test issue"])
        .current_dir(env.path())
        .assert()
        .success();

    // Claim the issue
    itack(&env)
        .args(["claim", "1", "agent-1"])
        .current_dir(env.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("Claimed issue #1 for agent-1"));

    // Verify status changed to in-progress
    itack(&env)
        .args(["show", "1", "--json"])
        .current_dir(env.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("\"status\": \"in-progress\""))
        .stdout(predicate::str::contains("\"assignee\": \"agent-1\""));

    // Release the claim
    itack(&env)
        .args(["release", "1"])
        .current_dir(env.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("Released issue #1"));

    // Verify assignee is removed
    itack(&env)
        .args(["show", "1", "--json"])
        .current_dir(env.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("\"assignee\": null"));
}

#[test]
fn test_claim_conflict_returns_exit_code_2() {
    let env = setup_git_repo();

    itack(&env)
        .arg("init")
        .current_dir(env.path())
        .assert()
        .success();

    itack(&env)
        .args(["create", "Test issue"])
        .current_dir(env.path())
        .assert()
        .success();

    // First claim succeeds
    itack(&env)
        .args(["claim", "1", "agent-1"])
        .current_dir(env.path())
        .assert()
        .success();

    // Second claim fails with exit code 2
    itack(&env)
        .args(["claim", "1", "agent-2"])
        .current_dir(env.path())
        .assert()
        .code(2)
        .stderr(predicate::str::contains("already claimed by agent-1"));
}

#[test]
fn test_release_unclaimed_issue() {
    let env = setup_git_repo();

    itack(&env)
        .arg("init")
        .current_dir(env.path())
        .assert()
        .success();

    itack(&env)
        .args(["create", "Test issue"])
        .current_dir(env.path())
        .assert()
        .success();

    // Release unclaimed issue fails
    itack(&env)
        .args(["release", "1"])
        .current_dir(env.path())
        .assert()
        .failure()
        .stderr(predicate::str::contains("not claimed"));
}

#[test]
fn test_board_command() {
    let env = setup_git_repo();

    itack(&env)
        .arg("init")
        .current_dir(env.path())
        .assert()
        .success();

    itack(&env)
        .args(["create", "Issue 1"])
        .current_dir(env.path())
        .assert()
        .success();

    itack(&env)
        .args(["create", "Issue 2"])
        .current_dir(env.path())
        .assert()
        .success();

    itack(&env)
        .args(["done", "1"])
        .current_dir(env.path())
        .assert()
        .success();

    // Board shows summary
    itack(&env)
        .arg("board")
        .current_dir(env.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("Open"))
        .stdout(predicate::str::contains("Done"));

    // Board as JSON
    itack(&env)
        .args(["board", "--json"])
        .current_dir(env.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("\"open\": 1"))
        .stdout(predicate::str::contains("\"done\": 1"));
}

#[test]
fn test_done_nonexistent_issue() {
    let env = setup_git_repo();

    itack(&env)
        .arg("init")
        .current_dir(env.path())
        .assert()
        .success();

    itack(&env)
        .args(["done", "999"])
        .current_dir(env.path())
        .assert()
        .failure()
        .stderr(predicate::str::contains("Issue 999 not found"));
}

#[test]
fn test_wontfix_command() {
    let env = setup_git_repo();

    itack(&env)
        .arg("init")
        .current_dir(env.path())
        .assert()
        .success();

    itack(&env)
        .args(["create", "Test issue"])
        .current_dir(env.path())
        .assert()
        .success();

    // Mark as wont-fix
    itack(&env)
        .args(["wont-fix", "1"])
        .current_dir(env.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("open -> wont-fix"));

    // Verify in list
    itack(&env)
        .args(["list", "--status", "wont-fix"])
        .current_dir(env.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("Test issue"));
}

#[test]
fn test_wontfix_already_wontfix() {
    let env = setup_git_repo();

    itack(&env)
        .arg("init")
        .current_dir(env.path())
        .assert()
        .success();

    itack(&env)
        .args(["create", "Test issue"])
        .current_dir(env.path())
        .assert()
        .success();

    itack(&env)
        .args(["wont-fix", "1"])
        .current_dir(env.path())
        .assert()
        .success();

    // Already wont-fix should fail
    itack(&env)
        .args(["wont-fix", "1"])
        .current_dir(env.path())
        .assert()
        .failure()
        .stderr(predicate::str::contains("already wont-fix"));
}

#[test]
fn test_wontfix_nonexistent_issue() {
    let env = setup_git_repo();

    itack(&env)
        .arg("init")
        .current_dir(env.path())
        .assert()
        .success();

    itack(&env)
        .args(["wont-fix", "999"])
        .current_dir(env.path())
        .assert()
        .failure()
        .stderr(predicate::str::contains("Issue 999 not found"));
}

#[test]
fn test_issue_ids_increment() {
    let env = setup_git_repo();

    itack(&env)
        .arg("init")
        .current_dir(env.path())
        .assert()
        .success();

    itack(&env)
        .args(["create", "First"])
        .current_dir(env.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("#1"));

    itack(&env)
        .args(["create", "Second"])
        .current_dir(env.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("#2"));

    itack(&env)
        .args(["create", "Third"])
        .current_dir(env.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("#3"));
}

#[test]
fn test_markdown_file_format() {
    let env = setup_git_repo();

    itack(&env)
        .arg("init")
        .current_dir(env.path())
        .assert()
        .success();

    itack(&env)
        .args(["create", "Test issue", "--epic", "MVP"])
        .current_dir(env.path())
        .assert()
        .success();

    // Read issue from data branch
    let content =
        read_issue_from_data_branch(env.path(), 1).expect("Issue file should exist in data branch");

    // Check YAML front matter format (title is NOT in YAML, it's in markdown body)
    assert!(content.starts_with("---\n"));
    assert!(content.contains("id: 1"));
    assert!(
        !content.contains("title:"),
        "Title should not be in YAML front matter"
    );
    assert!(content.contains("epic: MVP"));
    assert!(content.contains("status: open"));
    // Title should be in markdown body as H1 heading
    assert!(content.contains("# Test issue"));
}