jjj 0.4.1

Distributed project management and code review for Jujutsu
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
mod test_helpers;
use std::process::Command;
use test_helpers::{jj_available, jjj_binary, run_jjj, setup_test_repo};

/// Regression test for https://github.com/doug/jjj/issues/1
/// `jjj init` must create the metadata branch from root(), not on top of @.
#[test]
fn test_init_creates_orphan_from_root() {
    if !jj_available() {
        return;
    }

    let dir = tempfile::TempDir::new().expect("Failed to create temp dir");

    // Initialize a jj repo and make a real commit so @ is not at root
    Command::new("jj")
        .args(["git", "init"])
        .current_dir(dir.path())
        .output()
        .expect("jj git init");
    Command::new("jj")
        .args(["config", "set", "--repo", "user.name", "Test User"])
        .current_dir(dir.path())
        .status()
        .ok();
    Command::new("jj")
        .args(["config", "set", "--repo", "user.email", "test@example.com"])
        .current_dir(dir.path())
        .status()
        .ok();

    // Create a file and describe the change so @ has content
    std::fs::write(dir.path().join("hello.txt"), "hello").unwrap();
    Command::new("jj")
        .args(["describe", "-m", "user commit"])
        .current_dir(dir.path())
        .output()
        .expect("jj describe");

    // Now run jjj init
    let output = Command::new(jjj_binary())
        .arg("init")
        .current_dir(dir.path())
        .output()
        .expect("jjj init");
    assert!(
        output.status.success(),
        "jjj init failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // Verify: the jjj bookmark's ancestor chain should include root() but
    // should NOT include the "user commit" change.
    let log_output = Command::new("jj")
        .args([
            "log",
            "--no-graph",
            "-r",
            "ancestors(jjj)",
            "-T",
            r#"description ++ "\n""#,
        ])
        .current_dir(dir.path())
        .output()
        .expect("jj log");
    let log_stdout = String::from_utf8_lossy(&log_output.stdout);
    assert!(
        !log_stdout.contains("user commit"),
        "jjj bookmark should NOT descend from @; got ancestors:\n{}",
        log_stdout
    );
}

#[test]
fn test_init_and_create_problem_solution() {
    // Skip if jj is not installed
    if !jj_available() {
        return;
    }

    let temp_dir = setup_test_repo();
    let dir_path = temp_dir.path();

    // 1. Create a problem
    let output = run_jjj(dir_path, &["problem", "new", "Integration Problem"]);
    assert!(
        output.status.success(),
        "problem new failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("Integration Problem"));

    // 3. Create a solution associated with the problem
    let output = run_jjj(
        dir_path,
        &[
            "solution",
            "new",
            "Test Solution",
            "--problem",
            "Integration Problem",
        ],
    );
    assert!(
        output.status.success(),
        "solution new failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // 4. List solutions and verify
    let output = run_jjj(dir_path, &["solution", "list"]);
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("Test Solution"));

    // 5. Show solution details to verify problem link
    let output = run_jjj(dir_path, &["solution", "show", "Test Solution"]);
    assert!(output.status.success(), "solution show failed");
    let stdout = String::from_utf8_lossy(&output.stdout);
    // Problem reference should be present (title or "Addresses:" field with UUID)
    assert!(stdout.contains("Integration Problem") || stdout.contains("Addresses:"));
}

#[test]
fn test_critique_workflow() {
    // Skip if jj is not installed
    if !jj_available() {
        return;
    }

    let temp_dir = setup_test_repo();
    let dir_path = temp_dir.path();

    // 1. Create problem and solution
    run_jjj(dir_path, &["problem", "new", "Test Problem"]);
    run_jjj(
        dir_path,
        &[
            "solution",
            "new",
            "Test Solution",
            "--problem",
            "Test Problem",
        ],
    );

    // 3. Add a critique
    let output = run_jjj(
        dir_path,
        &[
            "critique",
            "new",
            "Test Solution",
            "This has a flaw",
            "--severity",
            "high",
        ],
    );
    assert!(
        output.status.success(),
        "critique new failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // 4. List critiques
    let output = run_jjj(dir_path, &["critique", "list"]);
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("flaw") || stdout.contains("This has a flaw"));
}

#[test]
fn test_problem_hierarchy() {
    // Skip if jj is not installed
    if !jj_available() {
        return;
    }

    let temp_dir = setup_test_repo();
    let dir_path = temp_dir.path();

    // 1. Create parent problem
    let output = run_jjj(dir_path, &["problem", "new", "Parent Problem"]);
    assert!(output.status.success());

    // 3. Create child problem
    let output = run_jjj(
        dir_path,
        &[
            "problem",
            "new",
            "Child Problem",
            "--parent",
            "Parent Problem",
        ],
    );
    assert!(
        output.status.success(),
        "child problem failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // 4. Show parent should reference child
    let output = run_jjj(dir_path, &["problem", "show", "Parent Problem"]);
    let stdout = String::from_utf8_lossy(&output.stdout);
    // Should mention it has sub-problems
    assert!(
        stdout.contains("Child Problem")
            || stdout.contains("Sub-problems")
            || stdout.contains("Child")
    );
}

#[test]
fn test_problem_priority() {
    if !jj_available() {
        return;
    }

    let temp_dir = setup_test_repo();
    let dir_path = temp_dir.path();

    // Create with P0 priority
    let output = run_jjj(
        dir_path,
        &["problem", "new", "Critical bug", "--priority", "critical"],
    );
    assert!(
        output.status.success(),
        "problem new with priority failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // Verify in show output (text mode should show priority)
    let output = run_jjj(dir_path, &["problem", "show", "Critical bug"]);
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("critical"),
        "Priority not shown in output: {}",
        stdout
    );

    // Create with default priority
    let output = run_jjj(dir_path, &["problem", "new", "Normal bug"]);
    assert!(output.status.success());
    let output = run_jjj(dir_path, &["problem", "show", "Normal bug"]);
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("medium"),
        "Default priority not shown: {}",
        stdout
    );
}

#[test]
fn test_problem_dissolve_reason() {
    if !jj_available() {
        return;
    }

    let temp_dir = setup_test_repo();
    let dir_path = temp_dir.path();

    let output = run_jjj(dir_path, &["problem", "new", "Ghost bug"]);
    assert!(output.status.success());

    let output = run_jjj(
        dir_path,
        &[
            "problem",
            "dissolve",
            "Ghost bug",
            "--reason",
            "Test data was stale",
        ],
    );
    assert!(
        output.status.success(),
        "dissolve with reason failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let output = run_jjj(dir_path, &["problem", "show", "Ghost bug"]);
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("dissolved"),
        "Status not dissolved: {}",
        stdout
    );
    assert!(
        stdout.contains("Test data was stale"),
        "Dissolved reason not shown: {}",
        stdout
    );
}

#[test]
fn test_solution_supersedes() {
    if !jj_available() {
        return;
    }

    let temp_dir = setup_test_repo();
    let dir_path = temp_dir.path();

    run_jjj(dir_path, &["problem", "new", "Slow queries"]);
    run_jjj(
        dir_path,
        &["solution", "new", "Add index", "--problem", "Slow queries"],
    );
    run_jjj(dir_path, &["solution", "withdraw", "Add index"]);

    let output = run_jjj(
        dir_path,
        &[
            "solution",
            "new",
            "Use connection pool",
            "--problem",
            "Slow queries",
            "--supersedes",
            "Add index",
        ],
    );
    assert!(
        output.status.success(),
        "solution new with supersedes failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("Supersedes") || stdout.contains("Add index"),
        "Supersedes not shown in creation output: {}",
        stdout
    );

    let output = run_jjj(dir_path, &["solution", "show", "Use connection pool"]);
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("Supersedes") || stdout.contains("Add index"),
        "Supersedes not shown in show: {}",
        stdout
    );
}

#[test]
fn test_solve_warns_active_solutions() {
    if !jj_available() {
        return;
    }

    let temp_dir = setup_test_repo();
    let dir_path = temp_dir.path();

    run_jjj(dir_path, &["problem", "new", "Fix auth"]);
    run_jjj(
        dir_path,
        &["solution", "new", "Approach A", "--problem", "Fix auth"],
    );
    run_jjj(dir_path, &["solution", "test", "Approach A"]);

    // Solving with active testing solution should still succeed but warn
    let output = run_jjj(dir_path, &["problem", "solve", "Fix auth"]);
    // Note: solve may or may not succeed depending on can_solve_problem logic.
    // The key thing is: if it runs far enough to check, it should warn.
    // Let's verify the warning appears in stderr.
    let stderr = String::from_utf8_lossy(&output.stderr);
    // If solve succeeded or even if it didn't, check for warning text
    if output.status.success() {
        assert!(
            stderr.contains("active") || stderr.contains("Warning") || stderr.contains("review"),
            "Expected warning about active solutions: stderr={}",
            stderr
        );
    }
    // If solve failed (e.g., no accepted solution), that's OK too —
    // the warning may not appear if can_solve_problem fails first
}

#[test]
fn test_next_priority_sorting() {
    if !jj_available() {
        return;
    }

    let temp_dir = setup_test_repo();
    let dir_path = temp_dir.path();

    // Create problems with different priorities
    run_jjj(
        dir_path,
        &["problem", "new", "Low priority task", "--priority", "low"],
    );
    run_jjj(
        dir_path,
        &["problem", "new", "Critical issue", "--priority", "critical"],
    );
    run_jjj(
        dir_path,
        &["problem", "new", "High priority work", "--priority", "high"],
    );

    // All should appear as TODO (no solutions)
    let output = run_jjj(dir_path, &["status", "--json", "--all"]);
    assert!(
        output.status.success(),
        "status failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    let json: serde_json::Value = serde_json::from_str(&stdout).expect("Failed to parse JSON");
    let items = json["items"].as_array().expect("items not array");

    // Should have at least 3 items
    assert!(
        items.len() >= 3,
        "Expected at least 3 items, got {}",
        items.len()
    );

    // Find the TODO items and verify Critical is before High is before Low
    let todo_items: Vec<_> = items
        .iter()
        .filter(|i| i["category"].as_str() == Some("todo"))
        .collect();

    assert!(todo_items.len() >= 3, "Expected at least 3 TODO items");

    // Critical should be first (check by title since IDs are UUIDs)
    let first_title = todo_items[0]["title"].as_str().unwrap_or("");
    assert!(
        first_title.contains("Critical"),
        "Expected Critical first, got {}",
        first_title
    );
    // High should be second
    let second_title = todo_items[1]["title"].as_str().unwrap_or("");
    assert!(
        second_title.contains("High"),
        "Expected High second, got {}",
        second_title
    );
    // Low should be last
    let third_title = todo_items[2]["title"].as_str().unwrap_or("");
    assert!(
        third_title.contains("Low"),
        "Expected Low third, got {}",
        third_title
    );
}

#[test]
fn test_critique_new_with_reviewer() {
    if !jj_available() {
        return;
    }
    let temp_dir = setup_test_repo();
    let dir = temp_dir.path();

    run_jjj(dir, &["problem", "new", "Test problem"]);
    run_jjj(
        dir,
        &[
            "solution",
            "new",
            "Test solution",
            "--problem",
            "Test problem",
        ],
    );
    let output = run_jjj(
        dir,
        &[
            "critique",
            "new",
            "Test solution",
            "Review needed",
            "--reviewer",
            "bob",
        ],
    );

    assert!(
        output.status.success(),
        "critique new with reviewer failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("Review needed"),
        "Expected title in output: {}",
        stdout
    );

    let show_output = run_jjj(dir, &["critique", "show", "Review needed", "--json"]);
    assert!(
        show_output.status.success(),
        "critique show --json failed: {}",
        String::from_utf8_lossy(&show_output.stderr)
    );
    let show_stdout = String::from_utf8_lossy(&show_output.stdout);
    assert!(
        show_stdout.contains("\"reviewer\": \"bob\""),
        "Expected reviewer: bob in output: {}",
        show_stdout
    );
}

#[test]
fn test_critique_list_filter_by_reviewer() {
    if !jj_available() {
        return;
    }
    let temp_dir = setup_test_repo();
    let dir = temp_dir.path();

    run_jjj(dir, &["problem", "new", "Test problem"]);
    run_jjj(
        dir,
        &[
            "solution",
            "new",
            "Test solution",
            "--problem",
            "Test problem",
        ],
    );
    run_jjj(
        dir,
        &[
            "critique",
            "new",
            "Test solution",
            "For alice",
            "--reviewer",
            "alice",
        ],
    );
    run_jjj(
        dir,
        &[
            "critique",
            "new",
            "Test solution",
            "For bob",
            "--reviewer",
            "bob",
        ],
    );
    run_jjj(dir, &["critique", "new", "Test solution", "No reviewer"]);

    let output = run_jjj(dir, &["critique", "list", "--reviewer", "alice"]);
    assert!(
        output.status.success(),
        "critique list --reviewer failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("For alice"),
        "Expected 'For alice' in output: {}",
        stdout
    );
    assert!(
        !stdout.contains("For bob"),
        "Should not contain 'For bob' in output: {}",
        stdout
    );
    assert!(
        !stdout.contains("No reviewer"),
        "Should not contain 'No reviewer' in output: {}",
        stdout
    );
}

#[test]
fn test_solution_new_with_reviewer() {
    if !jj_available() {
        return;
    }
    let temp_dir = setup_test_repo();
    let dir = temp_dir.path();

    run_jjj(dir, &["problem", "new", "Test problem"]);
    let output = run_jjj(
        dir,
        &[
            "solution",
            "new",
            "Test solution",
            "--problem",
            "Test problem",
            "--reviewer",
            "bob",
        ],
    );

    assert!(
        output.status.success(),
        "solution new with --reviewer failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // Should have created an awaiting review critique
    let critiques = run_jjj(dir, &["critique", "list", "--json"]);
    assert!(
        critiques.status.success(),
        "critique list --json failed: {}",
        String::from_utf8_lossy(&critiques.stderr)
    );
    let stdout = String::from_utf8_lossy(&critiques.stdout);
    assert!(
        stdout.contains("Awaiting review from @bob"),
        "Expected 'Awaiting review from @bob' in output: {}",
        stdout
    );
    assert!(
        stdout.contains("\"reviewer\": \"bob\""),
        "Expected '\"reviewer\": \"bob\"' in output: {}",
        stdout
    );
}

#[test]
fn test_solution_new_with_multiple_reviewers() {
    if !jj_available() {
        return;
    }
    let temp_dir = setup_test_repo();
    let dir = temp_dir.path();

    run_jjj(dir, &["problem", "new", "Test problem"]);
    let output = run_jjj(
        dir,
        &[
            "solution",
            "new",
            "Test solution",
            "--problem",
            "Test problem",
            "--reviewer",
            "@alice",
            "--reviewer",
            "bob:high",
        ],
    );

    assert!(
        output.status.success(),
        "solution new with multiple --reviewer failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // Should have created awaiting review critiques for both
    let critiques = run_jjj(dir, &["critique", "list", "--json"]);
    assert!(
        critiques.status.success(),
        "critique list --json failed: {}",
        String::from_utf8_lossy(&critiques.stderr)
    );
    let stdout = String::from_utf8_lossy(&critiques.stdout);
    assert!(
        stdout.contains("Awaiting review from @alice"),
        "Expected 'Awaiting review from @alice' in output: {}",
        stdout
    );
    assert!(
        stdout.contains("Awaiting review from @bob"),
        "Expected 'Awaiting review from @bob' in output: {}",
        stdout
    );
    assert!(
        stdout.contains("\"reviewer\": \"alice\""),
        "Expected '\"reviewer\": \"alice\"' in output: {}",
        stdout
    );
    assert!(
        stdout.contains("\"reviewer\": \"bob\""),
        "Expected '\"reviewer\": \"bob\"' in output: {}",
        stdout
    );
    // Bob's critique should have high severity
    assert!(
        stdout.contains("\"severity\": \"high\""),
        "Expected '\"severity\": \"high\"' for bob in output: {}",
        stdout
    );
}

#[test]
fn test_status_shows_review_needed() {
    if !jj_available() {
        return;
    }
    let temp_dir = setup_test_repo();
    let dir = temp_dir.path();

    // Configure git user as "bob" for this test
    Command::new("git")
        .args(["config", "user.name", "bob"])
        .current_dir(dir)
        .output()
        .expect("Failed to set git user");

    run_jjj(dir, &["problem", "new", "Test problem"]);
    run_jjj(
        dir,
        &[
            "solution",
            "new",
            "Test solution",
            "--problem",
            "Test problem",
            "--reviewer",
            "bob",
        ],
    );

    let output = run_jjj(dir, &["status"]);
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should show REVIEW category for bob's awaiting review
    assert!(
        stdout.contains("REVIEW") || stdout.contains("review"),
        "Expected REVIEW in status output: {}",
        stdout
    );
    assert!(
        stdout.contains("Awaiting review from @bob") || stdout.contains("Review requested"),
        "Expected awaiting review info: {}",
        stdout
    );
}