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
mod test_helpers;
use std::process::Command;
use test_helpers::{jj_available, jjj_binary, run_jjj};

/// Helper to setup a test repo with jj, jjj initialized, and a default problem
fn setup_test_repo() -> tempfile::TempDir {
    let temp_dir = test_helpers::setup_test_repo();
    // Create a problem to work with
    run_jjj(temp_dir.path(), &["problem", "new", "Workflow Problem"]);
    temp_dir
}

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

    // 1. Create a new solution via solution new (replaces start)
    let output = run_jjj(
        dir,
        &[
            "solution",
            "new",
            "New Solution",
            "--problem",
            "Workflow Problem",
        ],
    );
    assert!(
        output.status.success(),
        "solution new failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // Verify solution created
    let list = run_jjj(dir, &["solution", "list"]);
    let stdout = String::from_utf8_lossy(&list.stdout);
    assert!(stdout.contains("New Solution"));

    // Verify current change has description
    let jj_log = Command::new("jj")
        .current_dir(dir)
        .args(&["log", "--no-graph", "-r", "@", "-T", "description"])
        .output()
        .unwrap();
    if !jj_log.status.success() {
        println!(
            "DEBUG: jj log failed: {}",
            String::from_utf8_lossy(&jj_log.stderr)
        );
    }
    let desc = String::from_utf8_lossy(&jj_log.stdout);
    println!("DEBUG: Current Change Description: '{}'", desc);
    assert!(
        desc.contains("New Solution"),
        "Description mismatch. Got: '{}'",
        desc
    );
}

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

    // 1. Create solution via solution new (auto-attaches change)
    run_jjj(
        dir,
        &[
            "solution",
            "new",
            "Resume Me",
            "--problem",
            "Workflow Problem",
        ],
    );

    // 2. Resume by title using solution resume
    let output = run_jjj(dir, &["solution", "resume", "Resume Me"]);
    assert!(
        output.status.success(),
        "Resume failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // Verify change is for the solution
    let jj_log = Command::new("jj")
        .current_dir(dir)
        .args(&["log", "--no-graph", "-r", "@", "-T", "description"])
        .output()
        .unwrap();
    let desc = String::from_utf8_lossy(&jj_log.stdout);
    println!("DEBUG: Resume Description: '{}'", desc);
    assert!(
        desc.contains("Resume Me"),
        "Resume desc mismatch. Got: '{}'",
        desc
    );
}

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

    // 1. Create solution via solution new (replaces start)
    let output = run_jjj(
        dir,
        &[
            "solution",
            "new",
            "Solution to Submit",
            "--problem",
            "Workflow Problem",
        ],
    );
    assert!(
        output.status.success(),
        "solution new 1 failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    std::fs::write(dir.join("file.txt"), "content").unwrap();

    // Create 'main' manually
    let status = Command::new("jj")
        .current_dir(dir)
        .args(&["new", "root()", "-m", "initial"])
        .status()
        .unwrap();
    assert!(status.success(), "Failed to create initial commit");
    let status = Command::new("jj")
        .current_dir(dir)
        .args(&["bookmark", "create", "main"])
        .status()
        .unwrap();
    assert!(status.success());

    let log = Command::new("jj")
        .current_dir(dir)
        .args(&["log", "--no-graph", "-r", "all"])
        .output()
        .unwrap();
    println!(
        "DEBUG: Repo Graph before start 2:\nSTDOUT:{}\nSTDERR:{}",
        String::from_utf8_lossy(&log.stdout),
        String::from_utf8_lossy(&log.stderr)
    );

    // Create solution 2
    let output = run_jjj(
        dir,
        &[
            "solution",
            "new",
            "Solution to Submit 2",
            "--problem",
            "Workflow Problem",
        ],
    );
    assert!(
        output.status.success(),
        "solution new 2 failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    std::fs::write(dir.join("file2.txt"), "content").unwrap();

    let log = Command::new("jj")
        .current_dir(dir)
        .args(&["log", "--no-graph", "-r", "all"])
        .output()
        .unwrap();
    println!(
        "DEBUG: Repo Graph before submit:\nSTDOUT:{}\nSTDERR:{}",
        String::from_utf8_lossy(&log.stdout),
        String::from_utf8_lossy(&log.stderr)
    );

    // Move to review before submitting (--force bypasses open critiques, not review requirement)
    run_jjj(dir, &["solution", "submit", "Solution to Submit 2"]);

    let output = run_jjj(dir, &["solution", "approve", "--force"]);

    if !output.status.success() {
        println!("Submit failed: {}", String::from_utf8_lossy(&output.stderr));
    }
    assert!(output.status.success());

    // Verify solution is Accepted
    let list = run_jjj(dir, &["solution", "list", "--status", "approved"]);
    let stdout = String::from_utf8_lossy(&list.stdout);
    assert!(stdout.contains("Solution to Submit 2") || stdout.contains("approved"));
}

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

    // Create a solution (auto-attaches change but stays Proposed)
    run_jjj(
        dir,
        &[
            "solution",
            "new",
            "Test Solution",
            "--problem",
            "Workflow Problem",
        ],
    );

    // Check initial status is proposed (solution new stays Proposed now)
    let show = run_jjj(dir, &["solution", "show", "Test Solution"]);
    let stdout = String::from_utf8_lossy(&show.stdout);
    assert!(
        stdout.contains("Proposed") || stdout.contains("proposed"),
        "Expected proposed status after solution new. Got: {}",
        stdout
    );

    // Advance to review, then submit
    run_jjj(dir, &["solution", "submit", "Test Solution"]);
    let output = run_jjj(dir, &["solution", "approve", "Test Solution"]);
    assert!(output.status.success());

    let show = run_jjj(dir, &["solution", "show", "Test Solution"]);
    let stdout = String::from_utf8_lossy(&show.stdout);
    assert!(stdout.contains("Approved") || stdout.contains("approved"));
}

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

    // Create a solution and move to review
    run_jjj(
        dir,
        &[
            "solution",
            "new",
            "Test Solution",
            "--problem",
            "Workflow Problem",
        ],
    );
    run_jjj(dir, &["solution", "submit", "Test Solution"]);

    // Add a critique
    run_jjj(
        dir,
        &[
            "critique",
            "new",
            "Test Solution",
            "Major flaw in approach",
            "--severity",
            "high",
        ],
    );

    // Try to submit - should fail due to open critique
    let output = run_jjj(dir, &["solution", "approve", "Test Solution"]);
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    let combined = format!("{}{}", stdout, stderr);
    assert!(
        !output.status.success() && (combined.contains("critique") || combined.contains("open")),
        "Expected failure due to open critique. Got: {}",
        combined
    );
}

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

    // Create a main bookmark so submit has something to rebase onto
    Command::new("jj")
        .current_dir(dir)
        .args(&["new", "root()", "-m", "initial"])
        .status()
        .unwrap();
    Command::new("jj")
        .current_dir(dir)
        .args(&["bookmark", "create", "main"])
        .status()
        .unwrap();

    // Create solution (creates change, auto-attaches; stays Proposed)
    run_jjj(
        dir,
        &[
            "solution",
            "new",
            "Token refresh",
            "--problem",
            "Workflow Problem",
        ],
    );

    // Move to review so submit can reach the critique check
    run_jjj(dir, &["solution", "submit", "Token refresh"]);

    // Add a critique
    run_jjj(
        dir,
        &[
            "critique",
            "new",
            "Token refresh",
            "Not thread safe",
            "--severity",
            "high",
        ],
    );

    // Submit without --force should fail
    let output = run_jjj(dir, &["solution", "approve"]);
    assert!(
        !output.status.success(),
        "Expected submit to fail with open critiques"
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("critique") || stderr.contains("Cannot"),
        "Expected critique blocking message in stderr: {}",
        stderr
    );
}

// Note: test_submit_blocked_by_review was removed as the reviewer/sign-off system
// has been replaced with critique-based reviews. The test_submit_blocked_by_critiques
// test covers the blocking behavior via the unified critique system.

/// Regression test for the "working copy is stale" bug.
///
/// `commit_changes()` in storage/mod.rs coordinates two jj workspaces:
///  1. meta workspace: `jj workspace update-stale` then `jj new` (snapshots
///     metadata files into a commit, advances shared op log to op N)
///  2. main workspace: `jj bookmark set --ignore-working-copy` (points the
///     jjj bookmark at the new meta commit; --ignore-working-copy bypasses the
///     stale check that would otherwise fail because op N > main's last sync)
///
/// Without `workspace update-stale` before `jj new` in the meta workspace, a
/// prior `bookmark set` in the main workspace (op N-1) would leave the meta
/// workspace stale, causing `jj new` to fail on the next call.
///
/// This test exercises the full call path through multiple metadata writes and
/// verifies that `jj status` in the main workspace succeeds after each one.
#[test]
fn test_no_stale_working_copy_after_metadata_writes() {
    if !jj_available() {
        return;
    }
    let temp_dir = setup_test_repo();
    let dir = temp_dir.path();

    let assert_not_stale = |label: &str| {
        let out = Command::new("jj")
            .current_dir(dir)
            .args(&["status"])
            .output()
            .expect("Failed to run jj status");
        let stderr = String::from_utf8_lossy(&out.stderr);
        assert!(
            !stderr.contains("stale"),
            "{}: jj status reported stale working copy:\n{}",
            label,
            stderr
        );
        assert!(
            out.status.success(),
            "{}: jj status exited non-zero:\n{}",
            label,
            stderr
        );
    };

    // setup already ran `jjj init` + `problem new` (two commit_changes calls)
    assert_not_stale("after setup");

    // Each of these triggers another commit_changes()
    run_jjj(
        dir,
        &[
            "solution",
            "new",
            "Stale Regression Solution",
            "--problem",
            "Workflow Problem",
        ],
    );
    assert_not_stale("after solution new");

    run_jjj(
        dir,
        &[
            "critique",
            "new",
            "Stale Regression Solution",
            "Needs more tests",
            "--severity",
            "low",
        ],
    );
    assert_not_stale("after critique new");

    run_jjj(dir, &["solution", "submit", "Stale Regression Solution"]);
    assert_not_stale("after solution review");

    run_jjj(
        dir,
        &[
            "solution",
            "approve",
            "Stale Regression Solution",
            "--force",
        ],
    );
    assert_not_stale("after submit");
}

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

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

    // Move to review — submit now requires Review state; the awaiting-review critique blocks it
    run_jjj(dir, &["solution", "submit", "Test solution"]);

    // Submit should fail because awaiting review critique is open
    let output = Command::new(jjj_binary())
        .args(["solution", "approve"])
        .current_dir(dir)
        .output()
        .expect("Failed to execute jjj solution approve");

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(!output.status.success(), "Approve should have failed");
    assert!(
        stderr.contains("open critique") || stderr.contains("Awaiting review"),
        "Expected open critique error, got: {}",
        stderr
    );
}

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

    // Create and submit a solution (force to bypass review requirement)
    run_jjj(
        dir,
        &[
            "solution",
            "new",
            "Test Solution",
            "--problem",
            "Workflow Problem",
        ],
    );
    run_jjj(dir, &["solution", "submit", "Test Solution"]);
    run_jjj(dir, &["solution", "approve", "Test Solution", "--force"]);

    // Check events
    let output = run_jjj(dir, &["events", "--json"]);
    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("solution_created"),
        "Missing solution_created event"
    );
    assert!(
        stdout.contains("solution_approved"),
        "Missing solution_approved event"
    );
}