dv-review 0.2.0

A terminal diff viewer for reviewing GitHub PRs: Helix keys, rust-analyzer, inline review comments, and Claude Code reviews
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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
use super::*;

#[test]
fn keys_and_yank() {
    let mut app = App::with("base".into(), diff::parse(diff::SAMPLE));
    let press = |app: &mut App, s: &str| s.chars().for_each(|c| app.key(KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE)));
    press(&mut app, "]g"); // first change: the deleted line
    assert_eq!(app.rows[app.head.row].line.kind, Kind::Del);
    press(&mut app, "jww");
    assert_eq!(app.selection_text(false), "new_call");
    press(&mut app, "x");
    assert_eq!(app.selection_text(false), "    new_call(x);\n");
    press(&mut app, "kx");
    assert_eq!(app.selection_text(true), "-    old();\n");
    let enter = KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE);
    press(&mut app, "/call");
    app.key(enter);
    assert_eq!((app.anchor, app.head), (Pos { row: 4, col: 8 }, Pos { row: 4, col: 11 }));
    press(&mut app, "N"); // n / N repeat the search, as in Helix (one match: stays)
    assert_eq!(app.head, Pos { row: 4, col: 11 });
    press(&mut app, "/CALL");
    app.key(enter);
    assert!(app.msg.starts_with("no match")); // smart case: uppercase query is case-sensitive
    assert_eq!(base64(b"hello"), "aGVsbG8=");
    let h = app.hover_lines("```rust\nfn x()\n```\n\ndocs");
    assert_eq!(h.len(), 3); // fences dropped
    assert_eq!(h[0].spans[1].style.fg, Some(Color::Red)); // `fn` highlighted as Rust (after the block's 1-col padding)
    let back = KeyEvent::new(KeyCode::BackTab, KeyModifiers::SHIFT);
    app.key(back);
    assert_eq!(app.rows.len(), 3); // only the three file headers
    app.key(back);
    assert!(app.rows.len() > 3);
    app.alphabet = vec!['a', 's'];
    app.height = 20;
    app.goto(4, 0);
    press(&mut app, "gw"); // nearest word after the cursor ("new_call") gets "aa"
    assert_eq!(app.labels[0].2, ['a', 'a']);
    press(&mut app, "aa");
    assert_eq!(app.selection_text(false), "new_call");
}

#[test]
fn suggestion_block() {
    let mut app = App::with("base".into(), diff::parse(diff::SAMPLE));
    // rows: 0 file, 1 hunk, 2 ctx "fn main() {", 3 del, 4 add "    new_call(x);", 5 ctx "}"
    app.goto(4, 0);
    app.select = true;
    app.goto(5, 0);
    let (a, prefill) = app.suggestion().unwrap();
    assert_eq!((a.path.as_str(), a.line, a.left, a.start), ("src/a.rs", 3, false, Some((2, false))));
    assert_eq!(prefill, "\n```suggestion\n    new_call(x);\n}\n```\n");
    // Removed lines can't be part of a suggestion.
    app.goto(3, 0);
    assert!(app.suggestion().err().unwrap().to_string().contains("removed lines"));
    // Code containing ``` gets a longer fence.
    app.rows[4].line.text = "let s = \"```\";".into();
    app.select = false;
    app.goto(4, 0);
    assert!(app.suggestion().unwrap().1.contains("````suggestion\n"));
}

#[test]
fn viewed_files() {
    let mut app = App::with("base".into(), diff::parse(diff::SAMPLE));
    app.pr = Some(gh::Pr { number: 1, node_id: "PR".into(), title: "t".into(), base: "main".into(), head_oid: "abc".into(), url: String::new() });
    let viewed = |p: &str, s: &str| HashMap::from([(p.to_owned(), s.to_owned())]);
    app.apply_viewed(viewed("src/a.rs", "VIEWED"));
    app.build();
    assert_eq!(app.viewed_tag(0).unwrap().0, "✓ Viewed ");
    let header = app.rows.iter().find(|r| r.line.kind == Kind::File && r.file == 0).unwrap();
    assert!(!header.line.text.contains("Viewed")); // drawn at the right edge, not part of the text
    assert!(app.collapsed.contains("src/a.rs")); // viewed → folded
    app.collapsed.clear(); // you unfold it…
    app.apply_viewed(viewed("src/a.rs", "VIEWED"));
    assert!(app.collapsed.is_empty()); // …and a refresh leaves it unfolded
    app.apply_viewed(viewed("src/a.rs", "DISMISSED"));
    app.build();
    assert_eq!(app.viewed_tag(0).unwrap().0, "↻ changed since viewed ");
    let h = app.rows.iter().position(|r| r.line.kind == Kind::File && r.file == 0).unwrap();
    app.goto(h, 0);
    app.key(KeyEvent::new(KeyCode::Char(' '), KeyModifiers::NONE));
    app.key(KeyEvent::new(KeyCode::Char('v'), KeyModifiers::NONE));
    assert!(matches!(app.job, Some(Job::Viewed(ref p, true)) if p == "src/a.rs")); // dismissed → mark again
}

#[test]
fn help_search() {
    let mut app = App::with("base".into(), diff::parse(diff::SAMPLE));
    let k = |app: &mut App, code| app.key(KeyEvent::new(code, KeyModifiers::NONE));
    let text = |app: &App| -> Vec<String> {
        app.popup.as_ref().unwrap().1.iter().map(|l| l.spans.iter().map(|s| s.content.as_ref()).collect()).collect()
    };
    k(&mut app, KeyCode::Char('?'));
    let all = text(&app).len();
    k(&mut app, KeyCode::Char('/'));
    "bracket".chars().for_each(|c| k(&mut app, KeyCode::Char(c)));
    assert_eq!(text(&app), [" MOVE", "  mm                   matching bracket"]);
    k(&mut app, KeyCode::Enter); // keep the filter; j/k scroll again, popup stays
    k(&mut app, KeyCode::Char('j'));
    assert!(app.popup.is_some() && text(&app).len() == 2);
    k(&mut app, KeyCode::Char('/'));
    k(&mut app, KeyCode::Esc); // clear
    assert_eq!(text(&app).len(), all);
    k(&mut app, KeyCode::Char('/'));
    "zzzz".chars().for_each(|c| k(&mut app, KeyCode::Char(c)));
    assert!(text(&app)[0].contains("no keys match"));
    k(&mut app, KeyCode::Enter);
    k(&mut app, KeyCode::Char('x')); // any other key closes
    assert!(app.popup.is_none() && app.help.is_none());
}

#[test]
fn matching_bracket() {
    let mut app = App::with("base".into(), diff::parse(diff::SAMPLE));
    // rows: 2 "fn main() {", 3 del "    old();", 4 add "    new_call(x);", 5 "}"
    let mm = |app: &mut App| {
        app.key(KeyEvent::new(KeyCode::Char('m'), KeyModifiers::NONE));
        app.key(KeyEvent::new(KeyCode::Char('m'), KeyModifiers::NONE));
    };
    app.goto(2, 10); // {
    mm(&mut app);
    assert_eq!(app.head, Pos { row: 5, col: 0 }); // } on the new side, skipping the removed line
    mm(&mut app);
    assert_eq!(app.head, Pos { row: 2, col: 10 });
    app.goto(4, 12); // ( in new_call(x)
    mm(&mut app);
    assert_eq!(app.head, Pos { row: 4, col: 14 });
    app.goto(2, 0);
    mm(&mut app);
    assert!(app.msg.contains("not on a bracket"));

    // The match is hidden between hunks: the hunk grows to show it.
    let body: String = (2..=19).map(|i| if i == 3 { "+    changed();\n".to_string() } else { format!("     line{i}();\n") }).collect();
    let text = format!("diff --git a/b.rs b/b.rs\n--- a/b.rs\n+++ b/b.rs\n@@ -1,19 +1,20 @@\n fn f() {{\n{body} }}\n");
    let mut app = App::with("base".into(), diff::parse(&text));
    assert!(!app.rows.iter().any(|r| r.line.text == "}")); // hidden past the context
    let open = app.rows.iter().position(|r| r.line.text == "fn f() {").unwrap();
    app.goto(open, 7);
    mm(&mut app);
    assert_eq!(app.rows[app.head.row].line.text, "}");
    assert!(app.msg.contains("expanded"));
}

#[test]
fn split_view() {
    let sample = "diff --git a/a.rs b/a.rs\n--- a/a.rs\n+++ b/a.rs\n@@ -1,4 +1,3 @@\n ctx\n-old1\n-old2\n+new1\n end\n";
    let mut app = App::with("base".into(), diff::parse(sample));
    let ctrl = |c| KeyEvent::new(KeyCode::Char(c), KeyModifiers::CONTROL);
    app.key(ctrl('w'));
    app.key(KeyEvent::new(KeyCode::Char('v'), KeyModifiers::NONE));
    assert!(app.split);
    // rows: 0 file, 1 hunk, 2 ctx, 3 old1, 4 old2, 5 new1, 6 end
    assert_eq!(app.drows, [
        DRow::Full(0), DRow::Full(1), DRow::Pair(Some(2), Some(2)),
        DRow::Pair(Some(3), Some(5)), DRow::Pair(Some(4), None), DRow::Pair(Some(6), Some(6)),
    ]);
    app.goto(2, 0); // ctx, cursor prefers the right (new) side
    app.vmove(1);
    assert_eq!(app.head.row, 5); // new1, beside old1
    app.key(ctrl('h'));
    assert_eq!(app.head.row, 3); // old1
    app.vmove(1);
    assert_eq!(app.head.row, 4); // stays on the left: old2
    app.key(ctrl('l'));
    assert_eq!(app.head.row, 4); // nothing on the right there; side switches for the next move
    app.vmove(1);
    assert_eq!(app.head.row, 6);
    // Ctrl-w h / Ctrl-w l do the same, as Helix moves between splits.
    app.goto(5, 0); // new1
    app.key(ctrl('w'));
    app.key(KeyEvent::new(KeyCode::Char('h'), KeyModifiers::NONE));
    assert_eq!(app.head.row, 3); // old1, beside it
    app.key(ctrl('w'));
    app.key(KeyEvent::new(KeyCode::Char('l'), KeyModifiers::NONE));
    assert_eq!(app.head.row, 5);
}

#[test]
fn comments_placed_and_anchored() {
    let mut app = App::with("base".into(), diff::parse(diff::SAMPLE));
    let c = |id, body: &str| gh::Comment {
        id, node_id: format!("C{id}"), can_edit: id == 2, can_delete: id == 2, author: "amy".into(), body: body.into(), created: "2026-10-01".into(), pending: false,
    };
    let t = |left, line, resolved, comments| Thread {
        id: format!("T{line:?}"),
        path: "src/a.rs".into(), left, line, start_line: None, original_line: Some(1), resolved, outdated: line.is_none(),
        hunk: vec![], comments, claude: None,
    };
    app.threads = vec![
        t(true, Some(2), false, vec![c(1, "why remove?"), c(2, "unused")]), // on the deleted line
        t(false, None, true, vec![c(3, "old note")]),                        // outdated + resolved
    ];
    app.build();
    let kinds: Vec<_> = app.rows.iter().take(11).map(|r| (r.line.kind, r.thread)).collect();
    assert_eq!(kinds, [
        (Kind::File, None),
        (Kind::Hunk, None),
        (Kind::Ctx, None),
        (Kind::Thread, Some(1)), // outdated (was L1) + resolved: folded, under line 1
        (Kind::Del, None),
        (Kind::Thread, Some(0)),
        (Kind::Comment, Some(0)),
        (Kind::Thread, Some(0)),
        (Kind::Comment, Some(0)),
        (Kind::Comment, Some(0)), // the box's bottom edge
        (Kind::Add, None),
    ]);
    assert!(app.rows[9].edge);
    let parts: Vec<_> = (3..10).map(|r| app.box_part(r)).collect();
    assert_eq!(parts, [None, None, Some(BoxPart::Top), Some(BoxPart::Body), Some(BoxPart::Divider), Some(BoxPart::Body), Some(BoxPart::Bottom)]);
    // Select from the deleted line down to the added line (across the thread): multi-line, mixed sides.
    app.goto(4, 0);
    app.select = true;
    app.goto(10, 0);
    let a = app.selection_anchor().unwrap();
    assert_eq!(app.file_ref().unwrap(), "src/a.rs:2"); // new-side lines only; the deleted one has none

    // An outdated thread is relocated by the commented line's text.
    let moved = Thread { hunk: vec!["+    new_call(x);".into()], ..t(false, None, false, vec![c(4, "nit")]) };
    let lines = &app.files[0].lines;
    assert_eq!(anchor_line(&moved, lines).map(|i| lines[i].kind), Some(Kind::Add));
    // A trivial line ("}") falls back to the original line number (3 → the closing brace's row).
    let brace = Thread { hunk: vec![" }".into()], original_line: Some(3), ..t(false, None, false, vec![c(5, "x")]) };
    assert_eq!(anchor_line(&brace, lines).map(|i| lines[i].new), Some(Some(3)));
    let far = Thread { original_line: Some(500), ..brace };
    assert_eq!(anchor_line(&far, lines), None); // nothing within 10 lines → under the file header

    // Pending replies are tagged; `c` on a thread queues a reply in the review, `C` posts it now.
    app.threads[0].comments[1].pending = true;
    app.build();
    assert!(app.rows.iter().any(|r| r.line.text.starts_with("[pending] amy")));
    app.pr = Some(gh::Pr { number: 1, node_id: "PR".into(), title: "t".into(), base: "main".into(), head_oid: "abc".into(), url: "https://github.com/o/r/pull/1".into() });
    let tr = app.rows.iter().position(|r| r.thread == Some(0)).unwrap();
    app.goto(tr, 0);
    let press = |app: &mut App, c| app.key(KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE));
    press(&mut app, 'c'); // c comments on code; on a thread it points to r
    assert!(app.action.is_none() && app.msg.starts_with("r replies"));
    press(&mut app, 'r');
    assert!(matches!(app.action.take(), Some(Action::Reply { review: true, ref thread, .. }) if thread == "TSome(2)"));
    press(&mut app, 'C'); // and C replies right away
    assert!(matches!(app.action.take(), Some(Action::Reply { review: false, .. })));
    // On the second comment ("unused"), the reply quotes that comment, not the first.
    let second = app.rows.iter().position(|r| r.line.text == "unused").unwrap();
    app.goto(second, 0);
    press(&mut app, 'r');
    assert!(matches!(app.action.take(), Some(Action::Reply { ref quote, .. }) if quote == "> unused\n\n"));
    // Space e edits the comment under the cursor, if you're allowed to.
    press(&mut app, ' ');
    press(&mut app, 'e');
    assert!(matches!(app.action.take(), Some(Action::Edit { ref comment, ref body, .. }) if comment == "C2" && body == "unused"));
    let first = app.rows.iter().position(|r| r.line.text == "why remove?").unwrap();
    app.goto(first, 0);
    press(&mut app, ' ');
    press(&mut app, 'e');
    assert!(app.action.is_none() && app.msg.contains("can't edit"));
    // Space p lists pending comments and jumps to the chosen one.
    app.threads[0].comments[1].body = "> why remove?\n\nunused".into();
    app.build();
    press(&mut app, ' ');
    press(&mut app, 'p');
    let p = app.picker.take().unwrap();
    assert_eq!(p.items.len(), 1);
    assert_eq!((p.items[0].label.as_str(), p.items[0].detail.as_slice()), ("src/a.rs:2  reply", &["unused".to_owned()][..])); // quote skipped
    let Target::Comment(ti, ci) = p.items[0].target else { panic!() };
    // Ctrl-d there asks to delete it ("No" first), then deletes that comment.
    app.picker = Some(p);
    app.key(KeyEvent::new(KeyCode::Char('d'), KeyModifiers::CONTROL));
    let confirm = app.picker.take().unwrap();
    assert_eq!((confirm.title, confirm.sel), (" delete comment? ", 0));
    assert!(matches!(confirm.items[1].target, Target::DeleteComment(ref id) if id == "C2"));
    app.goto_comment(ti, ci);
    assert!(app.rows[app.head.row].line.text.starts_with("[pending] amy"));
    app.threads[0].comments[1].body = "unused".into();
    app.build();
    app.goto(first, 0);
    // Space t lists every thread with its state.
    press(&mut app, ' ');
    press(&mut app, 't');
    let items = app.picker.take().unwrap().items;
    assert_eq!(items[0].label, "src/a.rs:2  1 reply · [pending] @amy");
    assert_eq!(items[0].detail, ["why remove?"]); // the comment's text on the card's next line
    assert!(items[1].label.contains("[resolved] [outdated] @amy") && items[1].detail == ["old note"]);
    // Space d asks first, defaulting to "No".
    press(&mut app, ' ');
    press(&mut app, 'd');
    assert!(app.msg.contains("can't delete"));
    let second = app.rows.iter().position(|r| r.line.text == "unused").unwrap();
    app.goto(second, 0);
    press(&mut app, ' ');
    press(&mut app, 'd');
    let p = app.picker.as_ref().unwrap();
    assert!(matches!(p.items[0].target, Target::Nothing) && matches!(p.items[1].target, Target::DeleteComment(ref id) if id == "C2"));
    app.picker = None;
    assert_eq!(clean_body("> unused\n\n<!-- dv: x -->\n", "<!-- dv:", "> unused\n\n"), ""); // untouched → cancel
    assert_eq!(clean_body("> unused\n\nagreed\n<!-- dv: x -->", "<!-- dv:", "> unused\n\n"), "> unused\n\nagreed");
    let tr = app.rows.iter().position(|r| r.thread == Some(0)).unwrap(); // rebuilt rows include the PR header now
    app.goto(tr, 0);
    press(&mut app, 'C');
    assert!(matches!(app.action.take(), Some(Action::Reply { review: false, .. })));
    press(&mut app, 'S');
    assert_eq!(app.picker.take().unwrap().items.len(), 3); // no pending review → no discard option
    app.review = Some("R".into());
    press(&mut app, 'S');
    assert_eq!(app.picker.as_ref().unwrap().items[3].label, "Discard pending review (1 comment(s))");
    assert_eq!((a.line, a.left, a.start), (2, false, Some((2, true))));
    assert_eq!(wrap("aaa bbb ccc", 7), ["aaa bbb", "ccc"]);
}

#[test]
fn quote_into_conversation() {
    let mut app = App::with("base".into(), diff::parse(diff::SAMPLE));
    app.pr = Some(gh::Pr { number: 1, node_id: "PR".into(), title: "t".into(), base: "main".into(), head_oid: "abc".into(), url: String::new() });
    let line = |heading, md, text: &str| gh::InfoLine { heading, md, text: text.into(), marks: vec![], url: String::new() };
    app.info = vec![line(true, false, "conversation (1)"), line(true, false, "@bo · 2d ago"), line(false, true, "Looks good\nbut fix CI")];
    app.build();
    let quoted = |app: &mut App, row: usize| {
        app.goto(row, 0);
        app.key(KeyEvent::new(KeyCode::Char('C'), KeyModifiers::NONE));
        match app.action.take() {
            Some(Action::Converse { quote }) => quote,
            _ => panic!("no conversation comment started"),
        }
    };
    let body = app.rows.iter().position(|r| r.line.text == "but fix CI").unwrap();
    assert_eq!(quoted(&mut app, body), "> Looks good\n> but fix CI\n\n");
    let author = app.rows.iter().position(|r| r.line.text == "@bo · 2d ago").unwrap();
    assert_eq!(quoted(&mut app, author), "> Looks good\n> but fix CI\n\n"); // the author line quotes its comment
    let heading = app.rows.iter().position(|r| r.line.text == "conversation (1)").unwrap();
    assert_eq!(quoted(&mut app, heading), ""); // other PR lines: an empty conversation comment
    // `c` has nothing to queue in the PR section, and says so without checking git state.
    app.goto(body, 0);
    app.key(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::NONE));
    assert!(app.action.is_none());
    assert_eq!(app.msg, "select code lines to comment on");
    // A rebuild (as after posting) keeps the cursor on the same PR row.
    let keep = app.cursor_key();
    app.rebuild(keep);
    assert_eq!(app.head.row, body);
    // An empty description shows GitHub's placeholder, which isn't quoted.
    app.info = vec![line(true, false, "description"), line(false, true, "")];
    app.build();
    let ph = app.rows.iter().position(|r| r.line.text == "(no description)").unwrap();
    assert_eq!(quoted(&mut app, ph), "");
    // Outside the PR section, `C` still posts a line comment.
    let code = app.rows.iter().position(|r| r.line.kind == Kind::Add).unwrap();
    app.goto(code, 0);
    app.key(KeyEvent::new(KeyCode::Char('C'), KeyModifiers::NONE));
    assert!(!matches!(app.action, Some(Action::Converse { .. })));
}

#[test]
fn claude_review() {
    let mut app = App::with("base".into(), diff::parse(diff::SAMPLE));
    let p = std::env::temp_dir().join(format!("dv-claude-view-{}.json", std::process::id()));
    std::fs::write(&p, r#"{ "summary": "Looks fine.", "comments": [
        { "path": "src/a.rs", "line": 2, "body": "**nit:** name" },
        { "path": "src/a.rs", "line": 2, "side": "old", "body": "why remove?" } ] }"#).unwrap();
    app.claude_path = Some(p.clone());
    app.load_threads();
    app.build();
    let texts: Vec<&str> = app.rows.iter().map(|r| r.line.text.as_str()).collect();
    assert_eq!(&texts[..3], ["Claude's review", "claude's review", "Looks fine."]); // summary up top, no PR needed
    assert_eq!((app.box_part(1), app.box_part(2), app.box_part(3)), (Some(BoxPart::Top), Some(BoxPart::Body), Some(BoxPart::Bottom)));
    let row = |app: &App, side_left: bool| {
        app.rows.iter().position(|r| r.line.kind == Kind::Thread && r.thread.is_some_and(|t| app.threads[t].left == side_left)).unwrap()
    };
    // Each sits under its line: the old-side one under the deleted line, the new-side one under the added line.
    assert_eq!(app.rows[row(&app, true) - 1].line.kind, Kind::Del);
    assert_eq!(app.rows[row(&app, false) - 1].line.kind, Kind::Add);
    let press = |app: &mut App, s: &str| s.chars().for_each(|c| app.key(KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE)));
    // `r` replies to Claude (saved to the file, not GitHub).
    app.goto(row(&app, false), 0);
    press(&mut app, "r");
    assert!(matches!(app.action.take(), Some(Action::ClaudeReply(k)) if k == "c1")); // ids given on load
    // Space r resolves it in the file: it folds, and the cursor stays on it.
    press(&mut app, " r");
    assert!(std::fs::read_to_string(&p).unwrap().contains("\"resolved\": true"));
    let t = app.rows[app.head.row].thread.unwrap();
    assert!(app.threads[t].resolved && !app.thread_open(t));
    // Space d dismisses one; Space e explains instead of editing.
    app.goto(row(&app, true), 0);
    press(&mut app, " e");
    assert!(app.action.is_none() && app.msg.contains("r replies to Claude"));
    press(&mut app, " d");
    assert_eq!(app.threads.iter().filter(|t| t.claude.is_some()).count(), 1);
    // Claude writing the file shows up without a refresh.
    std::fs::write(&p, r#"{ "comments": [] }"#).unwrap();
    app.claude_mtime = None;
    app.watch_claude();
    assert!(app.threads.is_empty() && app.msg.contains("0 comment(s)"));
    _ = std::fs::remove_file(&p);
}

#[test]
fn claude_outdated() {
    let mut app = App::with("base".into(), diff::parse(diff::SAMPLE));
    let p = std::env::temp_dir().join(format!("dv-claude-outdated-{}.json", std::process::id()));
    // One whose code moved (recorded on line 1, now on 2) and one whose code is gone.
    std::fs::write(&p, r#"{ "comments": [
        { "path": "src/a.rs", "line": 1, "body": "moved", "code": ["    new_call(x);"] },
        { "path": "src/a.rs", "line": 2, "body": "gone", "code": ["    other();"] } ] }"#).unwrap();
    app.claude_path = Some(p.clone());
    app.load_threads();
    app.build();
    _ = std::fs::remove_file(&p);
    let (moved, gone) = (&app.threads[0], &app.threads[1]);
    assert_eq!((moved.line, moved.outdated), (Some(2), false));
    assert!(gone.outdated && !app.thread_open(1)); // folded, like GitHub's outdated threads
    let head = app.rows.iter().position(|r| r.thread == Some(1)).unwrap();
    assert!(app.rows[head].line.text.starts_with("[outdated] claude"));
    // Under the line it moved to: the added line (new side 2).
    let m = app.rows.iter().position(|r| r.thread == Some(0)).unwrap();
    assert_eq!(app.rows[m - 1].line.new, Some(2));
    app.goto(head, 0);
    app.key(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::NONE));
    assert!(app.action.is_none() && app.msg.contains("outdated"));
}

#[test]
fn claude_menu() {
    let mut app = App::with("base".into(), diff::parse(diff::SAMPLE));
    let p = std::env::temp_dir().join(format!("dv-claude-menu-{}.json", std::process::id()));
    std::fs::write(&p, r#"{ "comments": [
        { "path": "src/a.rs", "line": 2, "body": "a", "replies": [ { "author": "user", "body": "why?" } ] },
        { "path": "src/a.rs", "line": 2, "body": "b", "resolved": true, "replies": [ { "author": "user", "body": "ok" } ] },
        { "path": "src/a.rs", "line": 2, "body": "**nit:** use `x` ([docs](https://x.y))", "replies": [ { "author": "claude", "body": "done" } ] } ] }"#).unwrap();
    app.claude_path = Some(p.clone());
    app.load_threads();
    _ = std::fs::remove_file(&p);
    assert_eq!(app.waiting_replies(), 1); // resolved and answered ones don't count
    app.key(KeyEvent::new(KeyCode::Char(' '), KeyModifiers::NONE));
    app.key(KeyEvent::new(KeyCode::Char('a'), KeyModifiers::NONE));
    let p = app.picker.as_ref().unwrap();
    assert!(p.items[0].label.starts_with("Update Claude's review"));
    assert!(p.items[1].label.contains("(1 waiting)") && p.sel == 1); // answering preselected
    assert!(p.items[2].label.contains("make the changes (2 open comments")); // the resolved one is skipped
    // Picking it asks for your own instructions first (an editor), then runs Claude.
    app.picker = None;
    app.pick_claude(agent::Ask::Fix);
    assert!(matches!(app.action.take(), Some(Action::ClaudeWith(agent::Ask::Fix))));
    app.pick_claude(agent::Ask::Review); // reviewing asks too
    assert!(matches!(app.action.take(), Some(Action::ClaudeWith(agent::Ask::Review))));
    assert!(app.claude_prompt(agent::Ask::Fix, "keep the API").ends_with("The user adds:\nkeep the API"));
    assert!(!app.claude_prompt(agent::Ask::Fix, "  ").contains("The user adds"));
    app.picker = None;
    // Space A lists Claude's comments, the unanswered one tagged.
    app.key(KeyEvent::new(KeyCode::Char(' '), KeyModifiers::NONE));
    app.key(KeyEvent::new(KeyCode::Char('A'), KeyModifiers::NONE));
    let labels: Vec<&str> = app.picker.as_ref().unwrap().items.iter().map(|i| i.label.as_str()).collect();
    assert_eq!(labels.len(), 3);
    assert!(labels[0].contains("[waiting]") && !labels[1].contains("[waiting]") && !labels[2].contains("[waiting]"));
    assert_eq!(labels[0], "src/a.rs:2  1 reply · [waiting] @claude");
    let p = app.picker.as_ref().unwrap();
    assert_eq!(p.items[2].detail, ["nit: use x (docs)"]); // rendered: no ** or link URL
    // The filter matches the comment's text too.
    app.key(KeyEvent::new(KeyCode::Char('d'), KeyModifiers::NONE));
    app.key(KeyEvent::new(KeyCode::Char('o'), KeyModifiers::NONE));
    app.key(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::NONE));
    assert_eq!(app.matches(), [2]);
    app.picker = None;
    app.threads.clear();
    app.run_claude(agent::Ask::Answer, "");
    assert!(app.msg.contains("no replies waiting") && app.claude_job.is_none());
}

#[test]
fn posting_shows_progress() {
    let mut app = App::with("base".into(), diff::parse(diff::SAMPLE));
    // Written text is queued as a job, so a "posting…" frame shows before the network call…
    let msg = app.post_later(Post::Converse("hi".into())).unwrap();
    assert_eq!(msg, "posting…");
    assert!(matches!(app.job, Some(Job::Post(Post::Converse(_)))));
    // …and a failure still reports (with no PR here, before anything is sent).
    let job = app.job.take().unwrap();
    app.run_job(job);
    assert!(app.msg.contains("no PR"));
}

#[test]
fn help_two_columns() {
    let mut app = App::with("base".into(), diff::parse(diff::SAMPLE));
    let rows = |app: &mut App, w: u16| {
        app.width = w;
        app.key(KeyEvent::new(KeyCode::Char('?'), KeyModifiers::NONE));
        let lines = app.popup.take().unwrap().1;
        app.help = None;
        lines
    };
    let one = rows(&mut app, 100);
    let two = rows(&mut app, 200);
    assert!(two.len() <= one.len() / 2 + 4, "{} vs {}", two.len(), one.len()); // about half as tall (sections stay whole)
    let text: String = two[0].spans.iter().map(|s| s.content.as_ref()).collect();
    assert!(text.starts_with(" MOVE") && text.trim_end().ends_with(text.split_whitespace().last().unwrap())); // a heading on each side
    assert!(two.iter().all(|l| l.width() <= 197));
}

#[test]
fn copy_refs() {
    let mut app = App::with("base".into(), diff::parse(diff::SAMPLE));
    let url = "https://github.com/o/r/pull/1";
    app.pr = Some(gh::Pr { number: 1, node_id: "PR".into(), title: "t".into(), base: "main".into(), head_oid: "abc".into(), url: url.into() });
    let c = |id| gh::Comment { id, node_id: String::new(), can_edit: false, can_delete: false, author: "amy".into(), body: "hi".into(), created: String::new(), pending: false };
    app.threads = vec![gh::Thread {
        id: "T".into(), path: "src/a.rs".into(), left: false, line: Some(2), start_line: None, original_line: Some(2),
        resolved: false, outdated: false, hunk: vec![], comments: vec![c(11), c(12)], claude: None,
    }];
    let heading = |text: &str, url: &str| gh::InfoLine { heading: true, md: false, text: text.into(), marks: vec![], url: url.into() };
    let body = |text: &str| gh::InfoLine { heading: false, md: true, text: text.into(), marks: vec![], url: String::new() };
    app.info = vec![heading("@bo · 1d ago", "https://github.com/o/r/pull/1#issuecomment-5"), body("Looks good")];
    app.build();
    let at = |app: &mut App, text: &str| {
        let r = app.rows.iter().position(|r| r.line.text == text).unwrap();
        app.goto(r, 0);
        app.cursor_ref().unwrap()
    };
    assert_eq!(at(&mut app, "Looks good"), "https://github.com/o/r/pull/1#issuecomment-5"); // conversation comment
    assert_eq!(at(&mut app, "PR #1 · t"), url); // the PR header
    let second = app.rows.iter().enumerate().filter(|(_, r)| r.thread == Some(0) && r.line.text == "hi").nth(1).unwrap().0;
    app.goto(second, 0);
    assert_eq!(app.cursor_ref().unwrap(), "https://github.com/o/r/pull/1#discussion_r12"); // the reply under the cursor
    assert_eq!(app.context_keys().unwrap().0, "review thread"); // the bottom-right key popup
    assert_eq!(app.context_keys().unwrap().1[0], ("r", "reply (review)"));
    app.threads[0].claude = Some("c1".into());
    app.threads[0].comments[0].author = crate::claude::AUTHOR.into();
    app.threads[0].start_line = Some(1);
    app.build();
    app.goto(app.rows.iter().position(|r| r.thread == Some(0)).unwrap(), 0);
    assert_eq!(app.cursor_ref().unwrap(), "src/a.rs:1-2"); // Claude's: where it is
    assert_eq!(app.context_keys().unwrap().0, "Claude's comment");
    let code = app.rows.iter().position(|r| r.line.kind == Kind::Add).unwrap();
    app.goto(code, 0);
    assert_eq!(app.cursor_ref().unwrap(), "src/a.rs:2");
    assert!(app.context_keys().is_none()); // plain code: no popup
}

#[test]
fn cycle_claude_comments() {
    let mut app = App::with("base".into(), diff::parse(diff::SAMPLE));
    let p = std::env::temp_dir().join(format!("dv-claude-cycle-{}.json", std::process::id()));
    std::fs::write(&p, r#"{ "comments": [
        { "path": "src/a.rs", "line": 3, "body": "second" },
        { "path": "src/a.rs", "line": 1, "body": "first" } ] }"#).unwrap();
    app.claude_path = Some(p.clone());
    app.load_threads();
    _ = std::fs::remove_file(&p);
    app.collapsed.insert("src/a.rs".into()); // folded: ]a opens it
    app.build();
    let press = |app: &mut App, s: &str| s.chars().for_each(|c| app.key(KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE)));
    let on = |app: &App| app.rows[app.head.row].thread.map(|t| app.threads[t].comments[0].body.clone());
    press(&mut app, "]a");
    assert_eq!(on(&app).as_deref(), Some("first")); // by line, not file order
    press(&mut app, "]a");
    assert_eq!(on(&app).as_deref(), Some("second"));
    press(&mut app, "]a");
    assert_eq!(on(&app).as_deref(), Some("first")); // wraps around
    press(&mut app, "[a");
    assert_eq!(on(&app).as_deref(), Some("second")); // and back
    // From a code line, forward reaches the comment shown under it.
    let l3 = app.rows.iter().position(|r| r.line.new == Some(3) && r.thread.is_none()).unwrap();
    app.goto(l3, 0);
    press(&mut app, "]a");
    assert_eq!(on(&app).as_deref(), Some("second"));
}

#[test]
fn replies_while_claude_works_wait_for_it() {
    let mut app = App::with("base".into(), diff::parse(diff::SAMPLE));
    // Claude busy with a run: a new reply waits for one more run instead of starting a second.
    let child = std::process::Command::new("sleep").arg("5").spawn().unwrap();
    let log = std::env::temp_dir().join("dv-test-none.log");
    app.claude_job = Some(agent::ClaudeJob { child, log, label: "answering", forked: false });
    let msg = app.answer_now();
    assert!(app.claude_again && msg.contains("when it's done"));
    if let Some(mut job) = app.claude_job.take() {
        _ = job.child.kill();
    }
}

#[test]
fn claude_session_per_pr() {
    let mut app = App::with("base".into(), diff::parse(diff::SAMPLE));
    let dir = std::env::temp_dir().join(format!("dv-session-{}", std::process::id()));
    let review = dir.join("pr-7.json");
    app.claude_path = Some(review.clone());
    // The first run starts the PR's session under a new id, saved next to the review file
    // for this checkout (Claude Code keeps sessions per directory)…
    app.set_session(Some("11111111-2222-4333-8444-555555555555".into()));
    let saved = std::fs::read_to_string(crate::claude::session_path(&review)).unwrap();
    assert_eq!(saved, format!("{}\t11111111-2222-4333-8444-555555555555\n", app.root.display()));
    // Claude may touch dv's directory for the repo, where the notes (and reviews) are.
    let notes = crate::claude::notes_path().unwrap();
    assert_eq!(app.claude_dirs(), [std::ffi::OsString::from("--add-dir"), notes.parent().unwrap().as_os_str().to_owned()]);
    // …and forgetting it (a failed run) removes it, so the next run starts afresh.
    app.set_session(None);
    assert!(!crate::claude::session_path(&review).exists());
    _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn approve_says_ack() {
    let pr = gh::Pr { number: 1, node_id: "PR".into(), title: "t".into(), base: "main".into(), head_oid: "cb1cec09".into(), url: String::new() };
    assert_eq!(ack("APPROVE", Some(&pr), "cb1cec09"), "ACK cb1cec09");
    // You reviewed another commit: still the PR's head, with a hint line (dropped when posted).
    let diverged = ack("APPROVE", Some(&pr), "698eec92");
    assert!(diverged.starts_with("<!-- dv: your checkout is at 698eec92") && diverged.ends_with("\nACK cb1cec09"));
    assert_eq!(clean_body(&diverged, HINT, ""), "ACK cb1cec09");
    assert_eq!(ack("COMMENT", Some(&pr), "cb1cec09"), "");
    assert_eq!(ack("APPROVE", None, "cb1cec09"), "");
}

#[test]
fn ask_claude_on_lines() {
    let mut app = App::with("base".into(), diff::parse(diff::SAMPLE));
    let dir = std::env::temp_dir().join(format!("dv-ask-{}", std::process::id()));
    app.claude_path = Some(dir.join("pr-1.json"));
    let add = app.rows.iter().position(|r| r.line.kind == Kind::Add).unwrap();
    app.goto(add, 0);
    app.key(KeyEvent::new(KeyCode::Char('a'), KeyModifiers::NONE));
    let Some(Action::AskClaude { anchor, code }) = app.action.take() else { panic!("no comment for Claude started") };
    assert_eq!((anchor.path.as_str(), anchor.line, anchor.left), ("src/a.rs", 2, false));
    assert_eq!(code, ["    new_call(x);"]);
    // Once written, it's waiting for Claude (the user had the last word).
    crate::claude::add_comment(app.claude_path.as_ref().unwrap(), &anchor, code, "why?").unwrap();
    app.load_claude();
    _ = std::fs::remove_dir_all(&dir);
    assert_eq!(app.waiting_replies(), 1);
}

#[test]
fn cycle_pending_comments() {
    let mut app = App::with("base".into(), diff::parse(diff::SAMPLE));
    let c = |id, pending| gh::Comment { id, node_id: String::new(), can_edit: true, can_delete: true, author: "me".into(), body: format!("c{id}"), created: String::new(), pending };
    let t = |line, comments| gh::Thread {
        id: String::new(), path: "src/a.rs".into(), left: false, line: Some(line), start_line: None, original_line: Some(line),
        resolved: false, outdated: false, hunk: vec![], comments, claude: None,
    };
    // A pending reply on line 3's thread, a pending comment on line 1, a posted one between.
    app.threads = vec![t(3, vec![c(1, false), c(2, true)]), t(1, vec![c(3, true)]), t(2, vec![c(4, false)])];
    app.build();
    let press = |app: &mut App, s: &str| s.chars().for_each(|ch| app.key(KeyEvent::new(KeyCode::Char(ch), KeyModifiers::NONE)));
    let on = |app: &App| app.rows[app.head.row + 1].line.text.clone(); // the body under the author line
    press(&mut app, "]p");
    assert_eq!(on(&app), "c3"); // line 1 first
    press(&mut app, "]p");
    assert_eq!(on(&app), "c2"); // the reply itself, not its thread's first comment
    press(&mut app, "]p");
    assert_eq!(on(&app), "c3"); // wraps
    press(&mut app, "[p");
    assert_eq!(on(&app), "c2");
}

#[test]
fn your_comments_for_claude_stay_private() {
    let mut app = App::with("base".into(), diff::parse(diff::SAMPLE));
    let dir = std::env::temp_dir().join(format!("dv-private-{}", std::process::id()));
    let p = dir.join("pr-1.json");
    let a = gh::Anchor { path: "src/a.rs".into(), line: 2, left: false, start: None };
    crate::claude::add_comment(&p, &a, vec!["    new_call(x);".into()], "is this right?").unwrap();
    app.claude_path = Some(p);
    app.pr = Some(gh::Pr { number: 1, node_id: "PR".into(), title: "t".into(), base: "main".into(), head_oid: "abc".into(), url: String::new() });
    app.load_threads();
    app.build();
    _ = std::fs::remove_dir_all(&dir);
    // Not Claude's: no copying it to GitHub, and it doesn't count as Claude's anywhere.
    app.goto(app.rows.iter().position(|r| r.thread == Some(0)).unwrap(), 0);
    assert_eq!(app.context_keys().unwrap().0, "your comment for Claude");
    app.key(KeyEvent::new(KeyCode::Char('C'), KeyModifiers::NONE));
    assert!(app.action.is_none() && app.msg.contains("private"));
    assert!(!agent::claude_wrote(&app.threads[0]));
    assert_eq!(app.change_requests(), 1); // but it's a request "make the changes" acts on
    // `a` across removed and added lines is refused: line numbers are per side.
    let del = app.rows.iter().position(|r| r.line.kind == Kind::Del).unwrap();
    let add = app.rows.iter().position(|r| r.line.kind == Kind::Add).unwrap();
    app.goto(del, 0);
    app.select = true;
    app.goto(add, 0);
    app.key(KeyEvent::new(KeyCode::Char('a'), KeyModifiers::NONE));
    assert!(app.action.is_none() && app.msg.contains("one side"));
}

#[test]
fn dismiss_outdated_claude_comments() {
    let mut app = App::with("base".into(), diff::parse(diff::SAMPLE));
    let dir = std::env::temp_dir().join(format!("dv-outdated-{}", std::process::id()));
    let p = dir.join("pr-1.json");
    std::fs::create_dir_all(&dir).unwrap();
    // Two of Claude's whose code is gone, one still current, and one of yours that's outdated.
    std::fs::write(&p, r#"{ "comments": [
        { "id": "c1", "path": "src/a.rs", "line": 2, "body": "gone 1", "code": ["    nope();"] },
        { "id": "c2", "path": "src/a.rs", "line": 2, "body": "current", "code": ["    new_call(x);"] },
        { "id": "c3", "path": "src/a.rs", "line": 1, "body": "gone 2", "code": ["fn nope() {"] },
        { "id": "c4", "path": "src/a.rs", "line": 1, "author": "user", "body": "mine", "code": ["fn other() {"] } ] }"#).unwrap();
    app.claude_path = Some(p.clone());
    app.load_threads();
    app.build();
    assert_eq!(app.outdated_claude_comments(), ["c1", "c3"]);
    app.dismiss_outdated(false);
    assert_eq!(app.picker.take().unwrap().sel, 0); // asks first, "No" selected
    app.dismiss_outdated(true);
    let left: Vec<_> = app.threads.iter().filter_map(|t| t.claude.clone()).collect();
    _ = std::fs::remove_dir_all(&dir);
    assert_eq!(left, ["c2", "c4"]); // the current one and yours stay
}

#[test]
fn comments_in_the_margin() {
    // Four lines of code, comments on lines 1 and 2: the second box is pushed below the first.
    let sample = "diff --git a/a.rs b/a.rs\nindex 1..2 100644\n--- a/a.rs\n+++ b/a.rs\n@@ -1,4 +1,4 @@\n one\n two\n three\n-four\n+four!\n";
    let mut app = App::with("base".into(), diff::parse(sample));
    let c = |id, body: &str| gh::Comment { id, node_id: String::new(), can_edit: false, can_delete: false, author: "amy".into(), body: body.into(), created: String::new(), pending: false };
    let t = |line, comments| gh::Thread {
        id: String::new(), path: "a.rs".into(), left: false, line: Some(line), start_line: None, original_line: Some(line),
        resolved: false, outdated: false, hunk: vec![], comments, claude: None,
    };
    app.threads = vec![t(1, vec![c(1, "first")]), t(2, vec![c(2, "second")])];
    app.margin_active = true;
    app.build();
    // rows: 0 file, 1 hunk, 2 one, 3-5 thread 0 (top, body, bottom), 6 two, 7-9 thread 1, 10 three,
    // 11 -four, 12 +four!
    let code = |d: usize| match app.drows[d] { DRow::Margin(c, _) => c, _ => None };
    let note = |d: usize| match app.drows[d] { DRow::Margin(_, n) => n, _ => None };
    assert_eq!(app.drows[0], DRow::Full(0)); // the file header stays full width
    // The code runs on, uninterrupted: hunk, one, two, three, -four, +four!, then spacers.
    assert_eq!((1..7).map(code).collect::<Vec<_>>(), [Some(1), Some(2), Some(6), Some(10), Some(11), Some(12)]);
    // Thread 0 starts beside its line ("one"); thread 1 right after it, not beside "two".
    assert_eq!((2..5).map(note).collect::<Vec<_>>(), [Some(3), Some(4), Some(5)]);
    assert_eq!((5..7).map(note).collect::<Vec<_>>(), [Some(7), Some(8)]);
    // It needs more room than the code has: a spacer row (no code) after the last line.
    assert_eq!(app.drows.len(), 8);
    assert_eq!((code(7), note(7)), (None, Some(9)));
    // Moving: Ctrl-w l into the comment beside the line, Ctrl-w h back; j on code skips spacers.
    app.goto(2, 0); // "one"
    let ctrl_w = |app: &mut App, c| {
        app.key(KeyEvent::new(KeyCode::Char('w'), KeyModifiers::CONTROL));
        app.key(KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE));
    };
    ctrl_w(&mut app, 'l');
    assert_eq!(app.head.row, 3); // thread 0's top
    assert_eq!(app.lit_code(), Some(2)); // and its line lit
    ctrl_w(&mut app, 'h');
    assert_eq!(app.head.row, 2);
    assert_eq!(app.lit_thread(), Some(0)); // its box lit from the code
    app.goto(12, 0); // the last line
    app.vmove(1); // only a spacer below: stays
    assert_eq!(app.head.row, 12);
    // A search that lands in a comment leaves the cursor in the margin: j stays there.
    let enter = KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE);
    app.goto(0, 0);
    "/first".chars().for_each(|ch| app.key(KeyEvent::new(KeyCode::Char(ch), KeyModifiers::NONE)));
    app.key(enter);
    assert_eq!(app.head.row, 4); // thread 0's body
    app.key(KeyEvent::new(KeyCode::Char('j'), KeyModifiers::NONE));
    assert_eq!(app.head.row, 5); // its bottom edge, not the code beside it
}

#[test]
fn conversation_beside_the_description() {
    let mut app = App::with("base".into(), diff::parse(diff::SAMPLE));
    app.pr = Some(gh::Pr { number: 1, node_id: "PR".into(), title: "t".into(), base: "main".into(), head_oid: "abc".into(), url: String::new() });
    let heading = |t: &str| gh::InfoLine { heading: true, md: false, text: t.into(), marks: vec![], url: String::new() };
    let md = |t: &str| gh::InfoLine { heading: false, md: true, text: t.into(), marks: vec![], url: String::new() };
    app.info = vec![
        heading("description"), md("line 1\nline 2\nline 3\nline 4\nline 5"),
        heading("conversation (3)"),
        heading("@bo · 2d ago"), md("short"),
        heading("@cy · 1d ago"), md("also short"),
        heading("@dee · 1h ago"), md("late"),
    ];
    app.margin_active = true;
    app.build();
    let by_text = |t: &str| app.rows.iter().position(|r| r.line.text == t).unwrap();
    // The description is the left column; the first comments sit in the margin beside it.
    assert!(app.rows[by_text("line 3")].lead);
    assert!(app.rows[by_text("short")].aside && app.rows[by_text("also short")].aside);
    let d = app.d_of[by_text("conversation (3)")];
    assert_eq!(d, app.d_of[by_text("description")]); // beside the top of the description
    assert!(matches!(app.drows[d], DRow::Margin(Some(_), Some(_))));
    // Once there's no description left beside it, the rest is full width, as without the margin.
    let late = by_text("late");
    assert!(!app.rows[late].aside && matches!(app.drows[app.d_of[late]], DRow::Full(_)));
}

#[test]
fn margin_tags_on_their_own_line() {
    let mut app = App::with("base".into(), diff::parse(diff::SAMPLE));
    let c = gh::Comment { id: 1, node_id: String::new(), can_edit: false, can_delete: false, author: "amy".into(), body: "old".into(), created: String::new(), pending: false };
    app.threads = vec![gh::Thread {
        id: String::new(), path: "src/a.rs".into(), left: false, line: Some(2), start_line: None, original_line: Some(2),
        resolved: true, outdated: false, hunk: vec![], comments: vec![c], claude: None,
    }];
    let rows = |app: &App| app.rows.iter().filter(|r| r.thread == Some(0)).map(|r| r.line.text.clone()).collect::<Vec<_>>();
    app.build();
    assert!(rows(&app)[0].starts_with("[resolved] amy")); // inline: one line
    app.margin_active = true;
    app.build();
    let r = rows(&app);
    assert!(r[0].starts_with("amy") && r[1] == "[resolved]", "{r:?}"); // margin: tags below, even folded
}

#[test]
fn what_claude_was_refused() {
    use agent::refusal;
    let v = |d: serde_json::Value| serde_json::json!({ "permission_denials": d });
    assert_eq!(refusal(&v(serde_json::json!([]))), None);
    let edit = serde_json::json!({ "tool_name": "Edit", "tool_input": { "file_path": "/r/notes.md" } });
    assert_eq!(refusal(&v(serde_json::json!([edit]))).unwrap(), "Edit /r/notes.md");
    // A long multi-line command: its first line, short; others counted.
    let bash = serde_json::json!({ "tool_name": "Bash", "tool_input": { "command": format!("cat <<EOF > x\n{}\nEOF", "y".repeat(200)) } });
    let grep = serde_json::json!({ "tool_name": "Grep", "tool_input": { "pattern": "fn main", "path": "/r/src" } });
    assert_eq!(refusal(&v(serde_json::json!([bash, grep]))).unwrap(), "Bash cat <<EOF > x (and 1 more)");
    assert_eq!(refusal(&v(serde_json::json!([grep]))).unwrap(), "Grep /r/src"); // its path, not a bare name
}

#[test]
fn mouse() {
    use ratatui::crossterm::event::{MouseButton, MouseEvent, MouseEventKind};
    let mut app = App::with("base".into(), diff::parse(diff::SAMPLE));
    app.build();
    app.height = 20;
    app.geom = mouse::Geom { relw: 0, gw: 10, ghw: 6, half: 40, ctw: 0, picker: None };
    let at = |kind, x: u16, y: u16| MouseEvent { kind, column: x, row: y, modifiers: KeyModifiers::NONE };
    // rows: 0 file header, 1 hunk, 2 `fn main() {`, 3 `-    old();`, 4 `+    new_call(x);`
    app.mouse(at(MouseEventKind::Down(MouseButton::Left), 10 + 4, 4)); // text column 4 of row 4
    assert_eq!(app.head, Pos { row: 4, col: 4 });
    // A drag selects from there.
    app.mouse(at(MouseEventKind::Drag(MouseButton::Left), 10 + 11, 4));
    assert_eq!(app.selection_text(false), "new_call");
    // A click on the file header's fold arrow folds the file.
    app.mouse(at(MouseEventKind::Down(MouseButton::Left), 1, 0));
    assert!(app.collapsed.contains("src/a.rs"));
    // The wheel scrolls a picker's selection; a click picks, again opens.
    app.key(KeyEvent::new(KeyCode::Char(' '), KeyModifiers::NONE));
    app.key(KeyEvent::new(KeyCode::Char('f'), KeyModifiers::NONE));
    app.mouse(at(MouseEventKind::ScrollDown, 30, 5));
    assert_eq!(app.picker.as_ref().unwrap().sel, 1);
    app.geom.picker = Some(mouse::PickerGeom { list: Rect { x: 10, y: 4, width: 60, height: 5 }, item_h: 1, off: 0 });
    app.mouse(at(MouseEventKind::Down(MouseButton::Left), 20, 6)); // the third file
    assert_eq!(app.picker.as_ref().unwrap().sel, 2);
    app.mouse(at(MouseEventKind::Down(MouseButton::Left), 20, 6)); // again: opens it
    assert!(app.picker.is_none() && app.rows[app.head.row].line.text.starts_with("img.png"));
}

#[test]
fn make_changes_without_comments() {
    let mut app = App::with("base".into(), diff::parse(diff::SAMPLE));
    app.claude_path = Some(std::env::temp_dir().join(format!("dv-fix-{}", std::process::id())).join("pr-1.json"));
    // No open comments: still asks for instructions; with none written, nothing to do.
    app.pick_claude(agent::Ask::Fix);
    assert!(matches!(app.action.take(), Some(Action::ClaudeWith(agent::Ask::Fix))));
    assert!(!app.run_claude(agent::Ask::Fix, "  "));
    assert!(app.msg.starts_with("nothing to change") && app.claude_job.is_none());
    // While Claude is busy, it says so before the editor opens (so nothing written is lost).
    let child = std::process::Command::new("sleep").arg("5").spawn().unwrap();
    app.claude_job = Some(agent::ClaudeJob { child, log: std::env::temp_dir().join("dv-test-busy.log"), label: "reviewing", forked: false });
    app.pick_claude(agent::Ask::Fix);
    assert!(app.action.is_none() && app.msg.contains("still working"));
    if let Some(mut job) = app.claude_job.take() {
        _ = job.child.kill();
    }
    // With instructions, those are the request.
    let prompt = app.claude_prompt(agent::Ask::Fix, "rename x to y");
    assert!(prompt.starts_with("Make the change the user asks for below") && prompt.ends_with("rename x to y"));
}