| 676 |
775
|
|
"the composer stayed on hold:\n{frame}"
|
| 677 |
776
|
|
);
|
| 678 |
777
|
|
}
|
|
778
|
+ |
|
|
779
|
+ |
// ------------------------------------------------------- markdown and code
|
|
780
|
+ |
|
|
781
|
+ |
/// A model writes markdown. Before this the transcript printed the marks.
|
|
782
|
+ |
#[test]
|
|
783
|
+ |
fn a_reply_in_markdown_is_rendered_rather_than_printed_with_its_marks() {
|
|
784
|
+ |
let (mut app, control, _rx) = app();
|
|
785
|
+ |
let mut term = terminal();
|
|
786
|
+ |
type_str(&mut app, &control, "explain");
|
|
787
|
+ |
app.on_key(&key(KeyCode::Enter), WIDTH, &control);
|
|
788
|
+ |
app.on_turn_event(TurnEvent::Chunk(
|
|
789
|
+ |
"Run `mix precommit` and read **the output**.".to_string(),
|
|
790
|
+ |
));
|
|
791
|
+ |
app.on_turn_event(TurnEvent::Done(String::new()));
|
|
792
|
+ |
app.draw(&mut term).unwrap();
|
|
793
|
+ |
|
|
794
|
+ |
let frame = screen(&term);
|
|
795
|
+ |
assert!(
|
|
796
|
+ |
frame.contains("Run mix precommit and read the output."),
|
|
797
|
+ |
"the markdown was not rendered:\n{frame}"
|
|
798
|
+ |
);
|
|
799
|
+ |
assert!(
|
|
800
|
+ |
!frame.contains("**the output**"),
|
|
801
|
+ |
"the marks are still on the screen:\n{frame}"
|
|
802
|
+ |
);
|
|
803
|
+ |
assert!(!frame.contains("`mix"), "{frame}");
|
|
804
|
+ |
}
|
|
805
|
+ |
|
|
806
|
+ |
/// A heading is drawn bold and cyan, which is a claim about the frame's cells
|
|
807
|
+ |
/// rather than about its text.
|
|
808
|
+ |
#[test]
|
|
809
|
+ |
fn a_heading_in_a_reply_is_drawn_bold() {
|
|
810
|
+ |
use ratatui::style::Modifier;
|
|
811
|
+ |
let (mut app, control, _rx) = app();
|
|
812
|
+ |
let mut term = terminal();
|
|
813
|
+ |
type_str(&mut app, &control, "go");
|
|
814
|
+ |
app.on_key(&key(KeyCode::Enter), WIDTH, &control);
|
|
815
|
+ |
app.on_turn_event(TurnEvent::Chunk("## Findings".to_string()));
|
|
816
|
+ |
app.on_turn_event(TurnEvent::Done(String::new()));
|
|
817
|
+ |
app.draw(&mut term).unwrap();
|
|
818
|
+ |
|
|
819
|
+ |
let frame = screen(&term);
|
|
820
|
+ |
assert!(frame.contains("Findings"), "{frame}");
|
|
821
|
+ |
assert!(!frame.contains("## Findings"), "{frame}");
|
|
822
|
+ |
|
|
823
|
+ |
assert!(
|
|
824
|
+ |
cell_of(&term, "Findings").modifier.contains(Modifier::BOLD),
|
|
825
|
+ |
"the heading was not drawn bold:\n{frame}"
|
|
826
|
+ |
);
|
|
827
|
+ |
}
|
|
828
|
+ |
|
|
829
|
+ |
/// A fenced block gets its rail, and the code inside it is highlighted.
|
|
830
|
+ |
#[test]
|
|
831
|
+ |
fn a_fenced_code_block_is_railed_and_highlighted_in_the_transcript() {
|
|
832
|
+ |
use ratatui::style::Color;
|
|
833
|
+ |
let (mut app, control, _rx) = app();
|
|
834
|
+ |
let mut term = terminal();
|
|
835
|
+ |
type_str(&mut app, &control, "show me");
|
|
836
|
+ |
app.on_key(&key(KeyCode::Enter), WIDTH, &control);
|
|
837
|
+ |
app.on_turn_event(TurnEvent::Chunk(
|
|
838
|
+ |
"```rust\nlet answer = 42;\n```".to_string(),
|
|
839
|
+ |
));
|
|
840
|
+ |
app.on_turn_event(TurnEvent::Done(String::new()));
|
|
841
|
+ |
app.draw(&mut term).unwrap();
|
|
842
|
+ |
|
|
843
|
+ |
let frame = screen(&term);
|
|
844
|
+ |
assert!(frame.contains("\u{256d}\u{2500} rust"), "{frame}");
|
|
845
|
+ |
assert!(frame.contains("\u{2502} let answer = 42;"), "{frame}");
|
|
846
|
+ |
assert!(frame.contains("\u{2570}\u{2500}"), "{frame}");
|
|
847
|
+ |
|
|
848
|
+ |
// `let` is a keyword and is coloured as one.
|
|
849
|
+ |
assert_eq!(
|
|
850
|
+ |
cell_of(&term, "let answer").fg,
|
|
851
|
+ |
Color::Magenta,
|
|
852
|
+ |
"the keyword was not highlighted:\n{frame}"
|
|
853
|
+ |
);
|
|
854
|
+ |
}
|
|
855
|
+ |
|
|
856
|
+ |
/// The half-written state every chunk but the last is in.
|
|
857
|
+ |
#[test]
|
|
858
|
+ |
fn a_code_fence_that_is_still_arriving_is_already_drawn_as_code() {
|
|
859
|
+ |
let (mut app, control, _rx) = app();
|
|
860
|
+ |
let mut term = terminal();
|
|
861
|
+ |
type_str(&mut app, &control, "write it");
|
|
862
|
+ |
app.on_key(&key(KeyCode::Enter), WIDTH, &control);
|
|
863
|
+ |
app.on_turn_event(TurnEvent::Chunk("```rust\nfn main() {".to_string()));
|
|
864
|
+ |
app.draw(&mut term).unwrap();
|
|
865
|
+ |
assert!(
|
|
866
|
+ |
screen(&term).contains("\u{2502} fn main() {"),
|
|
867
|
+ |
"an unclosed fence held its contents back:\n{}",
|
|
868
|
+ |
screen(&term)
|
|
869
|
+ |
);
|
|
870
|
+ |
}
|
|
871
|
+ |
|
|
872
|
+ |
/// A notice this program wrote is not markdown and is not treated as any.
|
|
873
|
+ |
#[test]
|
|
874
|
+ |
fn a_notice_is_shown_as_the_text_it_is() {
|
|
875
|
+ |
let (mut app, control, _rx) = app();
|
|
876
|
+ |
let mut term = terminal();
|
|
877
|
+ |
app.on_turn_event(TurnEvent::Notice("Wrote src/some_file_name.rs".to_string()));
|
|
878
|
+ |
app.draw(&mut term).unwrap();
|
|
879
|
+ |
let _ = control;
|
|
880
|
+ |
assert!(
|
|
881
|
+ |
screen(&term).contains("Wrote src/some_file_name.rs"),
|
|
882
|
+ |
"an underscore in a path was eaten as emphasis:\n{}",
|
|
883
|
+ |
screen(&term)
|
|
884
|
+ |
);
|
|
885
|
+ |
}
|
|
886
|
+ |
|
|
887
|
+ |
// ------------------------------------------------------------- status bar
|
|
888
|
+ |
|
|
889
|
+ |
#[test]
|
|
890
|
+ |
fn the_bar_names_the_lane_and_its_tier() {
|
|
891
|
+ |
let mut app = CoderApp::new("openagents coder", &Lane::Flash);
|
|
892
|
+ |
let mut term = terminal_of(120, HEIGHT);
|
|
893
|
+ |
app.draw(&mut term).unwrap();
|
|
894
|
+ |
assert!(
|
|
895
|
+ |
status_row(&term).contains("Lane: Coder Flash (flash)"),
|
|
896
|
+ |
"{}",
|
|
897
|
+ |
status_row(&term)
|
|
898
|
+ |
);
|
|
899
|
+ |
}
|
|
900
|
+ |
|
|
901
|
+ |
/// A lane that belongs to no tier is not given an invented one.
|
|
902
|
+ |
#[test]
|
|
903
|
+ |
fn a_lane_with_no_tier_is_named_without_one() {
|
|
904
|
+ |
let mut app = CoderApp::new("openagents coder", &Lane::Named("some-model".to_string()));
|
|
905
|
+ |
let mut term = terminal_of(120, HEIGHT);
|
|
906
|
+ |
app.draw(&mut term).unwrap();
|
|
907
|
+ |
let row = status_row(&term);
|
|
908
|
+ |
assert!(row.contains("Lane: Coder (some-model)"), "{row}");
|
|
909
|
+ |
assert!(!row.contains("(auto)"), "{row}");
|
|
910
|
+ |
}
|
|
911
|
+ |
|
|
912
|
+ |
/// Nothing is reported until the server has reported something. A zero would
|
|
913
|
+ |
/// read as "this turn was free", which is a different claim from "unknown".
|
|
914
|
+ |
#[test]
|
|
915
|
+ |
fn tokens_are_shown_only_once_the_server_has_said_what_a_turn_cost() {
|
|
916
|
+ |
let (mut app, _control, _rx) = app();
|
|
917
|
+ |
let mut term = terminal_of(140, HEIGHT);
|
|
918
|
+ |
app.draw(&mut term).unwrap();
|
|
919
|
+ |
assert!(
|
|
920
|
+ |
!status_row(&term).contains("Tokens"),
|
|
921
|
+ |
"{}",
|
|
922
|
+ |
status_row(&term)
|
|
923
|
+ |
);
|
|
924
|
+ |
|
|
925
|
+ |
app.on_turn_event(TurnEvent::Usage(TurnUsage {
|
|
926
|
+ |
prompt_tokens: 128,
|
|
927
|
+ |
completion_tokens: 64,
|
|
928
|
+ |
total_tokens: 192,
|
|
929
|
+ |
}));
|
|
930
|
+ |
app.draw(&mut term).unwrap();
|
|
931
|
+ |
assert!(
|
|
932
|
+ |
status_row(&term).contains("Tokens: 128+64=192"),
|
|
933
|
+ |
"{}",
|
|
934
|
+ |
status_row(&term)
|
|
935
|
+ |
);
|
|
936
|
+ |
}
|
|
937
|
+ |
|
|
938
|
+ |
// ------------------------------------------------------------ the history
|
|
939
|
+ |
|
|
940
|
+ |
#[test]
|
|
941
|
+ |
fn up_and_down_walk_the_prompts_that_were_sent() {
|
|
942
|
+ |
let (mut app, control, _rx) = app();
|
|
943
|
+ |
let mut term = terminal();
|
|
944
|
+ |
|
|
945
|
+ |
type_str(&mut app, &control, "the first question");
|
|
946
|
+ |
app.on_key(&key(KeyCode::Enter), WIDTH, &control);
|
|
947
|
+ |
app.on_turn_event(TurnEvent::Done("ok".to_string()));
|
|
948
|
+ |
type_str(&mut app, &control, "the second question");
|
|
949
|
+ |
app.on_key(&key(KeyCode::Enter), WIDTH, &control);
|
|
950
|
+ |
app.on_turn_event(TurnEvent::Done("ok".to_string()));
|
|
951
|
+ |
|
|
952
|
+ |
app.on_key(&key(KeyCode::Up), WIDTH, &control);
|
|
953
|
+ |
app.draw(&mut term).unwrap();
|
|
954
|
+ |
assert!(
|
|
955
|
+ |
screen(&term).contains("\u{203a} the second question"),
|
|
956
|
+ |
"Up did not recall the last prompt:\n{}",
|
|
957
|
+ |
screen(&term)
|
|
958
|
+ |
);
|
|
959
|
+ |
|
|
960
|
+ |
app.on_key(&key(KeyCode::Up), WIDTH, &control);
|
|
961
|
+ |
app.draw(&mut term).unwrap();
|
|
962
|
+ |
assert!(
|
|
963
|
+ |
screen(&term).contains("\u{203a} the first question"),
|
|
964
|
+ |
"{}",
|
|
965
|
+ |
screen(&term)
|
|
966
|
+ |
);
|
|
967
|
+ |
|
|
968
|
+ |
app.on_key(&key(KeyCode::Down), WIDTH, &control);
|
|
969
|
+ |
app.draw(&mut term).unwrap();
|
|
970
|
+ |
assert!(
|
|
971
|
+ |
screen(&term).contains("\u{203a} the second question"),
|
|
972
|
+ |
"{}",
|
|
973
|
+ |
screen(&term)
|
|
974
|
+ |
);
|
|
975
|
+ |
}
|
|
976
|
+ |
|
|
977
|
+ |
/// A half-typed line is not lost by looking back at the history.
|
|
978
|
+ |
#[test]
|
|
979
|
+ |
fn walking_back_and_forward_returns_the_draft() {
|
|
980
|
+ |
let (mut app, control, _rx) = app();
|
|
981
|
+ |
let mut term = terminal();
|
|
982
|
+ |
type_str(&mut app, &control, "sent");
|
|
983
|
+ |
app.on_key(&key(KeyCode::Enter), WIDTH, &control);
|
|
984
|
+ |
app.on_turn_event(TurnEvent::Done("ok".to_string()));
|
|
985
|
+ |
|
|
986
|
+ |
type_str(&mut app, &control, "half typed");
|
|
987
|
+ |
app.on_key(&key(KeyCode::Up), WIDTH, &control);
|
|
988
|
+ |
app.on_key(&key(KeyCode::Down), WIDTH, &control);
|
|
989
|
+ |
app.draw(&mut term).unwrap();
|
|
990
|
+ |
assert!(
|
|
991
|
+ |
screen(&term).contains("\u{203a} half typed"),
|
|
992
|
+ |
"the draft was lost:\n{}",
|
|
993
|
+ |
screen(&term)
|
|
994
|
+ |
);
|
|
995
|
+ |
}
|
|
996
|
+ |
|
|
997
|
+ |
/// A recalled prompt can be edited and sent again, which is the whole point.
|
|
998
|
+ |
#[test]
|
|
999
|
+ |
fn a_recalled_prompt_can_be_changed_and_sent_again() {
|
|
1000
|
+ |
let (mut app, control, mut rx) = app();
|
|
1001
|
+ |
type_str(&mut app, &control, "list the issues");
|
|
1002
|
+ |
app.on_key(&key(KeyCode::Enter), WIDTH, &control);
|
|
1003
|
+ |
let _ = rx.try_recv();
|
|
1004
|
+ |
app.on_turn_event(TurnEvent::Done("ok".to_string()));
|
|
1005
|
+ |
|
|
1006
|
+ |
app.on_key(&key(KeyCode::Up), WIDTH, &control);
|
|
1007
|
+ |
type_str(&mut app, &control, " again");
|
|
1008
|
+ |
app.on_key(&key(KeyCode::Enter), WIDTH, &control);
|
|
1009
|
+ |
assert!(matches!(rx.try_recv(), Ok(Control::Prompt(p)) if p == "list the issues again"));
|
|
1010
|
+ |
}
|
|
1011
|
+ |
|
|
1012
|
+ |
/// Scrolling is PgUp and PgDn, which is what the bar names. Up and Down are
|
|
1013
|
+ |
/// the history, so a reader looking back at what they typed does not have the
|
|
1014
|
+ |
/// transcript slide under them.
|
|
1015
|
+ |
#[test]
|
|
1016
|
+ |
fn up_does_not_scroll_the_transcript() {
|
|
1017
|
+ |
let (mut app, control, _rx) = app();
|
|
1018
|
+ |
let mut term = terminal();
|
|
1019
|
+ |
type_str(&mut app, &control, "a question");
|
|
1020
|
+ |
app.on_key(&key(KeyCode::Enter), WIDTH, &control);
|
|
1021
|
+ |
app.on_turn_event(TurnEvent::Chunk(
|
|
1022
|
+ |
(1..=30)
|
|
1023
|
+ |
.map(|n| format!("line {n}"))
|
|
1024
|
+ |
.collect::<Vec<_>>()
|
|
1025
|
+ |
.join("\n"),
|
|
1026
|
+ |
));
|
|
1027
|
+ |
app.on_turn_event(TurnEvent::Done(String::new()));
|
|
1028
|
+ |
|
|
1029
|
+ |
for _ in 0..10 {
|
|
1030
|
+ |
app.on_key(&key(KeyCode::Up), WIDTH, &control);
|
|
1031
|
+ |
}
|
|
1032
|
+ |
app.draw(&mut term).unwrap();
|
|
1033
|
+ |
assert!(
|
|
1034
|
+ |
screen(&term).contains("line 30"),
|
|
1035
|
+ |
"Up scrolled the transcript:\n{}",
|
|
1036
|
+ |
screen(&term)
|
|
1037
|
+ |
);
|
|
1038
|
+ |
}
|
|
1039
|
+ |
|
|
1040
|
+ |
// --------------------------------------------------------- the completions
|
|
1041
|
+ |
|
|
1042
|
+ |
fn scratch_directory() -> tempfile::TempDir {
|
|
1043
|
+ |
let dir = tempfile::tempdir().expect("a temporary directory");
|
|
1044
|
+ |
std::fs::create_dir(dir.path().join("crates")).expect("crates/");
|
|
1045
|
+ |
std::fs::write(dir.path().join("README.md"), "").expect("README.md");
|
|
1046
|
+ |
dir
|
|
1047
|
+ |
}
|
|
1048
|
+ |
|
|
1049
|
+ |
#[test]
|
|
1050
|
+ |
fn tab_completes_the_only_command_that_matches() {
|
|
1051
|
+ |
let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
|
|
1052
|
+ |
let dir = scratch_directory();
|
|
1053
|
+ |
let mut app = CoderApp::new("openagents coder", &Lane::OxAlpha)
|
|
1054
|
+ |
.with_working_directory(dir.path().to_path_buf());
|
|
1055
|
+ |
let mut term = terminal();
|
|
1056
|
+ |
|
|
1057
|
+ |
type_str(&mut app, &tx, "/exp");
|
|
1058
|
+ |
app.on_key(&key(KeyCode::Tab), WIDTH, &tx);
|
|
1059
|
+ |
app.draw(&mut term).unwrap();
|
|
1060
|
+ |
assert!(
|
|
1061
|
+ |
screen(&term).contains("\u{203a} /export "),
|
|
1062
|
+ |
"{}",
|
|
1063
|
+ |
screen(&term)
|
|
1064
|
+ |
);
|
|
1065
|
+ |
}
|
|
1066
|
+ |
|
|
1067
|
+ |
#[test]
|
|
1068
|
+ |
fn tab_lists_the_candidates_rather_than_choosing_one() {
|
|
1069
|
+ |
let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
|
|
1070
|
+ |
let dir = scratch_directory();
|
|
1071
|
+ |
let mut app = CoderApp::new("openagents coder", &Lane::OxAlpha)
|
|
1072
|
+ |
.with_working_directory(dir.path().to_path_buf());
|
|
1073
|
+ |
let mut term = terminal();
|
|
1074
|
+ |
|
|
1075
|
+ |
type_str(&mut app, &tx, "/");
|
|
1076
|
+ |
app.on_key(&key(KeyCode::Tab), WIDTH, &tx);
|
|
1077
|
+ |
app.draw(&mut term).unwrap();
|
|
1078
|
+ |
|
|
1079
|
+ |
let frame = screen(&term);
|
|
1080
|
+ |
assert!(
|
|
1081
|
+ |
frame.contains("clear diff export help run"),
|
|
1082
|
+ |
"the candidates were not listed:\n{frame}"
|
|
1083
|
+ |
);
|
|
1084
|
+ |
let composer = frame
|
|
1085
|
+ |
.lines()
|
|
1086
|
+ |
.find(|line| line.contains('\u{203a}'))
|
|
1087
|
+ |
.expect("a composer row");
|
|
1088
|
+ |
assert_eq!(
|
|
1089
|
+ |
composer.trim_matches('\u{2502}').trim_end(),
|
|
1090
|
+ |
"\u{203a} /",
|
|
1091
|
+ |
"Tab chose a command when five matched:\n{frame}"
|
|
1092
|
+ |
);
|
|
1093
|
+ |
}
|
|
1094
|
+ |
|
|
1095
|
+ |
#[test]
|
|
1096
|
+ |
fn tab_completes_a_path_in_the_working_directory() {
|
|
1097
|
+ |
let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
|
|
1098
|
+ |
let dir = scratch_directory();
|
|
1099
|
+ |
let mut app = CoderApp::new("openagents coder", &Lane::OxAlpha)
|
|
1100
|
+ |
.with_working_directory(dir.path().to_path_buf());
|
|
1101
|
+ |
let mut term = terminal();
|
|
1102
|
+ |
|
|
1103
|
+ |
type_str(&mut app, &tx, "look at REA");
|
|
1104
|
+ |
app.on_key(&key(KeyCode::Tab), WIDTH, &tx);
|
|
1105
|
+ |
app.draw(&mut term).unwrap();
|
|
1106
|
+ |
assert!(
|
|
1107
|
+ |
screen(&term).contains("look at README.md"),
|
|
1108
|
+ |
"{}",
|
|
1109
|
+ |
screen(&term)
|
|
1110
|
+ |
);
|
|
1111
|
+ |
}
|
|
1112
|
+ |
|
|
1113
|
+ |
/// The list is transient: the next keystroke narrows the set, so leaving the
|
|
1114
|
+ |
/// old candidates up would be showing the answer to the previous question.
|
|
1115
|
+ |
#[test]
|
|
1116
|
+ |
fn the_candidate_list_goes_away_on_the_next_keystroke() {
|
|
1117
|
+ |
let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
|
|
1118
|
+ |
let dir = scratch_directory();
|
|
1119
|
+ |
let mut app = CoderApp::new("openagents coder", &Lane::OxAlpha)
|
|
1120
|
+ |
.with_working_directory(dir.path().to_path_buf());
|
|
1121
|
+ |
let mut term = terminal();
|
|
1122
|
+ |
|
|
1123
|
+ |
type_str(&mut app, &tx, "/");
|
|
1124
|
+ |
app.on_key(&key(KeyCode::Tab), WIDTH, &tx);
|
|
1125
|
+ |
app.draw(&mut term).unwrap();
|
|
1126
|
+ |
assert!(screen(&term).contains("export"), "{}", screen(&term));
|
|
1127
|
+ |
|
|
1128
|
+ |
type_str(&mut app, &tx, "c");
|
|
1129
|
+ |
app.draw(&mut term).unwrap();
|
|
1130
|
+ |
assert!(
|
|
1131
|
+ |
!screen(&term).contains("clear diff export"),
|
|
1132
|
+ |
"the stale candidate list stayed up:\n{}",
|
|
1133
|
+ |
screen(&term)
|
|
1134
|
+ |
);
|
|
1135
|
+ |
}
|
|
1136
|
+ |
|
|
1137
|
+ |
// ------------------------------------------------------------- the commands
|
|
1138
|
+ |
|
|
1139
|
+ |
#[test]
|
|
1140
|
+ |
fn an_unknown_command_is_refused_rather_than_sent_to_the_model() {
|
|
1141
|
+ |
let (mut app, control, mut rx) = app();
|
|
1142
|
+ |
let mut term = terminal();
|
|
1143
|
+ |
type_str(&mut app, &control, "/difff");
|
|
1144
|
+ |
app.on_key(&key(KeyCode::Enter), WIDTH, &control);
|
|
1145
|
+ |
app.draw(&mut term).unwrap();
|
|
1146
|
+ |
|
|
1147
|
+ |
assert!(
|
|
1148
|
+ |
rx.try_recv().is_err(),
|
|
1149
|
+ |
"a mistyped command was sent to the model as a prompt"
|
|
1150
|
+ |
);
|
|
1151
|
+ |
assert!(
|
|
1152
|
+ |
screen(&term).contains("There is no `/difff`"),
|
|
1153
|
+ |
"{}",
|
|
1154
|
+ |
screen(&term)
|
|
1155
|
+ |
);
|
|
1156
|
+ |
}
|
|
1157
|
+ |
|
|
1158
|
+ |
#[test]
|
|
1159
|
+ |
fn slash_help_lists_every_command_the_session_handles() {
|
|
1160
|
+ |
let (mut app, control, _rx) = app();
|
|
1161
|
+ |
let mut term = terminal_of(100, 40);
|
|
1162
|
+ |
type_str(&mut app, &control, "/help");
|
|
1163
|
+ |
app.on_key(&key(KeyCode::Enter), WIDTH, &control);
|
|
1164
|
+ |
app.draw(&mut term).unwrap();
|
|
1165
|
+ |
|
|
1166
|
+ |
let frame = screen(&term);
|
|
1167
|
+ |
for (name, _) in openagents_cli::interactive::COMMANDS {
|
|
1168
|
+ |
assert!(
|
|
1169
|
+ |
frame.contains(&format!("/{name}")),
|
|
1170
|
+ |
"`/{name}` is handled and not listed:\n{frame}"
|
|
1171
|
+ |
);
|
|
1172
|
+ |
}
|
|
1173
|
+ |
}
|
|
1174
|
+ |
|
|
1175
|
+ |
#[test]
|
|
1176
|
+ |
fn slash_clear_empties_the_transcript() {
|
|
1177
|
+ |
let (mut app, control, _rx) = app();
|
|
1178
|
+ |
let mut term = terminal();
|
|
1179
|
+ |
type_str(&mut app, &control, "a question");
|
|
1180
|
+ |
app.on_key(&key(KeyCode::Enter), WIDTH, &control);
|
|
1181
|
+ |
app.on_turn_event(TurnEvent::Done("an answer".to_string()));
|
|
1182
|
+ |
app.draw(&mut term).unwrap();
|
|
1183
|
+ |
assert!(screen(&term).contains("an answer"));
|
|
1184
|
+ |
|
|
1185
|
+ |
type_str(&mut app, &control, "/clear");
|
|
1186
|
+ |
app.on_key(&key(KeyCode::Enter), WIDTH, &control);
|
|
1187
|
+ |
app.draw(&mut term).unwrap();
|
|
1188
|
+ |
let frame = screen(&term);
|
|
1189
|
+ |
assert!(!frame.contains("an answer"), "{frame}");
|
|
1190
|
+ |
assert!(!frame.contains("a question"), "{frame}");
|
|
1191
|
+ |
}
|
|
1192
|
+ |
|
|
1193
|
+ |
#[test]
|
|
1194
|
+ |
fn slash_export_writes_the_transcript_where_it_was_told_to() {
|
|
1195
|
+ |
let dir = tempfile::tempdir().expect("a temporary directory");
|
|
1196
|
+ |
let path = dir.path().join("session.txt");
|
|
1197
|
+ |
let (mut app, control, _rx) = app();
|
|
1198
|
+ |
let mut term = terminal();
|
|
1199
|
+ |
|
|
1200
|
+ |
type_str(&mut app, &control, "what changed");
|
|
1201
|
+ |
app.on_key(&key(KeyCode::Enter), WIDTH, &control);
|
|
1202
|
+ |
app.on_turn_event(TurnEvent::Done("Two files.".to_string()));
|
|
1203
|
+ |
type_str(&mut app, &control, &format!("/export {}", path.display()));
|
|
1204
|
+ |
app.on_key(&key(KeyCode::Enter), WIDTH, &control);
|
|
1205
|
+ |
app.draw(&mut term).unwrap();
|
|
1206
|
+ |
|
|
1207
|
+ |
let written = std::fs::read_to_string(&path).expect("the transcript file");
|
|
1208
|
+ |
assert!(written.contains("[you] what changed"), "{written}");
|
|
1209
|
+ |
assert!(written.contains("[coder] Two files."), "{written}");
|
|
1210
|
+ |
assert!(
|
|
1211
|
+ |
screen(&term).contains("Transcript written to"),
|
|
1212
|
+ |
"{}",
|
|
1213
|
+ |
screen(&term)
|
|
1214
|
+ |
);
|
|
1215
|
+ |
}
|
|
1216
|
+ |
|
|
1217
|
+ |
#[test]
|
|
1218
|
+ |
fn slash_export_without_a_path_says_so_instead_of_guessing_one() {
|
|
1219
|
+ |
let (mut app, control, _rx) = app();
|
|
1220
|
+ |
let mut term = terminal();
|
|
1221
|
+ |
type_str(&mut app, &control, "/export");
|
|
1222
|
+ |
app.on_key(&key(KeyCode::Enter), WIDTH, &control);
|
|
1223
|
+ |
app.draw(&mut term).unwrap();
|
|
1224
|
+ |
assert!(screen(&term).contains("needs a path"), "{}", screen(&term));
|
|
1225
|
+ |
}
|
|
1226
|
+ |
|
|
1227
|
+ |
// --------------------------------------------------------- the diff inspector
|
|
1228
|
+ |
|
|
1229
|
+ |
const TWO_FILE_DIFF: &str = "\
|
|
1230
|
+ |
diff --git a/lib/thing.ex b/lib/thing.ex
|
|
1231
|
+ |
--- a/lib/thing.ex
|
|
1232
|
+ |
+++ b/lib/thing.ex
|
|
1233
|
+ |
@@ -1,3 +1,3 @@
|
|
1234
|
+ |
defmodule Thing do
|
|
1235
|
+ |
- def run, do: :old
|
|
1236
|
+ |
+ def run, do: :new
|
|
1237
|
+ |
end
|
|
1238
|
+ |
diff --git a/README.md b/README.md
|
|
1239
|
+ |
--- a/README.md
|
|
1240
|
+ |
+++ b/README.md
|
|
1241
|
+ |
@@ -1,2 +1,2 @@
|
|
1242
|
+ |
# Title
|
|
1243
|
+ |
-first line
|
|
1244
|
+ |
+second line
|
|
1245
|
+ |
";
|
|
1246
|
+ |
|
|
1247
|
+ |
fn with_a_diff() -> (
|
|
1248
|
+ |
CoderApp,
|
|
1249
|
+ |
UnboundedSender<Control>,
|
|
1250
|
+ |
UnboundedReceiver<Control>,
|
|
1251
|
+ |
) {
|
|
1252
|
+ |
let (mut app, tx, rx) = app();
|
|
1253
|
+ |
app.on_turn_event(TurnEvent::Diff(openagents_cli::diff::parse_unified(
|
|
1254
|
+ |
TWO_FILE_DIFF,
|
|
1255
|
+ |
)));
|
|
1256
|
+ |
(app, tx, rx)
|
|
1257
|
+ |
}
|
|
1258
|
+ |
|
|
1259
|
+ |
#[test]
|
|
1260
|
+ |
fn the_inspector_opens_on_a_diff_and_shows_the_change_unified() {
|
|
1261
|
+ |
let (mut app, _control, _rx) = with_a_diff();
|
|
1262
|
+ |
let mut term = terminal_of(90, HEIGHT);
|
|
1263
|
+ |
app.draw(&mut term).unwrap();
|
|
1264
|
+ |
|
|
1265
|
+ |
let frame = screen(&term);
|
|
1266
|
+ |
assert!(app.inspecting(), "the inspector did not open");
|
|
1267
|
+ |
assert!(frame.contains("Diff \u{b7} lib/thing.ex"), "{frame}");
|
|
1268
|
+ |
assert!(frame.contains("1 of 2"), "{frame}");
|
|
1269
|
+ |
assert!(frame.contains("unified"), "{frame}");
|
|
1270
|
+ |
assert!(frame.contains("@@ -1,3 +1,3 @@"), "{frame}");
|
|
1271
|
+ |
assert!(frame.contains("\u{2212} def run, do: :old"), "{frame}");
|
|
1272
|
+ |
assert!(frame.contains("+ def run, do: :new"), "{frame}");
|
|
1273
|
+ |
// The composer is not drawn: it would be a control that is not live.
|
|
1274
|
+ |
assert!(
|
|
1275
|
+ |
!frame.contains("Message"),
|
|
1276
|
+ |
"the composer was drawn under a pane that takes the keyboard:\n{frame}"
|
|
1277
|
+ |
);
|
|
1278
|
+ |
}
|
|
1279
|
+ |
|
|
1280
|
+ |
/// Additions are green and removals red, asserted on the cells.
|
|
1281
|
+ |
#[test]
|
|
1282
|
+ |
fn the_two_sides_of_a_change_are_coloured_apart() {
|
|
1283
|
+ |
use ratatui::style::Color;
|
|
1284
|
+ |
let (mut app, _control, _rx) = with_a_diff();
|
|
1285
|
+ |
let mut term = terminal_of(90, HEIGHT);
|
|
1286
|
+ |
app.draw(&mut term).unwrap();
|
|
1287
|
+ |
|
|
1288
|
+ |
assert_eq!(cell_of(&term, "def run, do: :old").fg, Color::Red);
|
|
1289
|
+ |
assert_eq!(cell_of(&term, "def run, do: :new").fg, Color::Green);
|
|
1290
|
+ |
}
|
|
1291
|
+ |
|
|
1292
|
+ |
#[test]
|
|
1293
|
+ |
fn v_switches_between_the_unified_and_side_by_side_views() {
|
|
1294
|
+ |
let (mut app, control, _rx) = with_a_diff();
|
|
1295
|
+ |
let mut term = terminal_of(90, HEIGHT);
|
|
1296
|
+ |
app.draw(&mut term).unwrap();
|
|
1297
|
+ |
|
|
1298
|
+ |
app.on_key(&key(KeyCode::Char('v')), 90, &control);
|
|
1299
|
+ |
app.draw(&mut term).unwrap();
|
|
1300
|
+ |
let frame = screen(&term);
|
|
1301
|
+ |
assert!(frame.contains("side by side"), "{frame}");
|
|
1302
|
+ |
// Both texts on one row is what side by side means.
|
|
1303
|
+ |
assert!(
|
|
1304
|
+ |
frame
|
|
1305
|
+ |
.lines()
|
|
1306
|
+ |
.any(|line| line.contains(":old") && line.contains(":new")),
|
|
1307
|
+ |
"the two sides are not opposite each other:\n{frame}"
|
|
1308
|
+ |
);
|
|
1309
|
+ |
|
|
1310
|
+ |
app.on_key(&key(KeyCode::Char('v')), 90, &control);
|
|
1311
|
+ |
app.draw(&mut term).unwrap();
|
|
1312
|
+ |
let frame = screen(&term);
|
|
1313
|
+ |
assert!(frame.contains("unified"), "{frame}");
|
|
1314
|
+ |
assert!(
|
|
1315
|
+ |
!frame
|
|
1316
|
+ |
.lines()
|
|
1317
|
+ |
.any(|line| line.contains(":old") && line.contains(":new")),
|
|
1318
|
+ |
"{frame}"
|
|
1319
|
+ |
);
|
|
1320
|
+ |
}
|
|
1321
|
+ |
|
|
1322
|
+ |
#[test]
|
|
1323
|
+ |
fn tab_moves_to_the_next_file_and_wraps_round() {
|
|
1324
|
+ |
let (mut app, control, _rx) = with_a_diff();
|
|
1325
|
+ |
let mut term = terminal_of(90, HEIGHT);
|
|
1326
|
+ |
app.draw(&mut term).unwrap();
|
|
1327
|
+ |
|
|
1328
|
+ |
app.on_key(&key(KeyCode::Tab), 90, &control);
|
|
1329
|
+ |
app.draw(&mut term).unwrap();
|
|
1330
|
+ |
let frame = screen(&term);
|
|
1331
|
+ |
assert!(frame.contains("Diff \u{b7} README.md"), "{frame}");
|
|
1332
|
+ |
assert!(frame.contains("2 of 2"), "{frame}");
|
|
1333
|
+ |
assert!(frame.contains("second line"), "{frame}");
|
|
1334
|
+ |
|
|
1335
|
+ |
app.on_key(&key(KeyCode::Tab), 90, &control);
|
|
1336
|
+ |
app.draw(&mut term).unwrap();
|
|
1337
|
+ |
assert!(screen(&term).contains("1 of 2"), "{}", screen(&term));
|
|
1338
|
+ |
}
|
|
1339
|
+ |
|
|
1340
|
+ |
#[test]
|
|
1341
|
+ |
fn esc_closes_the_inspector_and_leaves_the_session_open() {
|
|
1342
|
+ |
let (mut app, control, _rx) = with_a_diff();
|
|
1343
|
+ |
let mut term = terminal_of(90, HEIGHT);
|
|
1344
|
+ |
app.draw(&mut term).unwrap();
|
|
1345
|
+ |
|
|
1346
|
+ |
app.on_key(&key(KeyCode::Esc), 90, &control);
|
|
1347
|
+ |
app.draw(&mut term).unwrap();
|
|
1348
|
+ |
assert!(!app.inspecting());
|
|
1349
|
+ |
assert!(!app.should_exit(), "Esc in the inspector ended the session");
|
|
1350
|
+ |
assert!(screen(&term).contains("Message"), "{}", screen(&term));
|
|
1351
|
+ |
}
|
|
1352
|
+ |
|
|
1353
|
+ |
/// While the inspector is up it has the keyboard, so a key that would have
|
|
1354
|
+ |
/// been typed does not land in a composer nobody can see.
|
|
1355
|
+ |
#[test]
|
|
1356
|
+ |
fn the_inspector_takes_the_keyboard_from_the_composer() {
|
|
1357
|
+ |
let (mut app, control, _rx) = with_a_diff();
|
|
1358
|
+ |
let mut term = terminal_of(90, HEIGHT);
|
|
1359
|
+ |
type_str(&mut app, &control, "hello");
|
|
1360
|
+ |
app.on_key(&key(KeyCode::Esc), 90, &control);
|
|
1361
|
+ |
app.draw(&mut term).unwrap();
|
|
1362
|
+ |
assert!(
|
|
1363
|
+ |
!screen(&term).contains("\u{203a} hello"),
|
|
1364
|
+ |
"keys pressed over the inspector reached the composer:\n{}",
|
|
1365
|
+ |
screen(&term)
|
|
1366
|
+ |
);
|
|
1367
|
+ |
}
|
|
1368
|
+ |
|
|
1369
|
+ |
#[test]
|
|
1370
|
+ |
fn a_diff_with_no_changes_says_so_rather_than_opening_an_empty_pane() {
|
|
1371
|
+ |
let (mut app, _control, _rx) = app();
|
|
1372
|
+ |
let mut term = terminal();
|
|
1373
|
+ |
app.on_turn_event(TurnEvent::Diff(Vec::new()));
|
|
1374
|
+ |
app.draw(&mut term).unwrap();
|
|
1375
|
+ |
assert!(!app.inspecting());
|
|
1376
|
+ |
assert!(
|
|
1377
|
+ |
screen(&term).contains("Nothing has changed"),
|
|
1378
|
+ |
"{}",
|
|
1379
|
+ |
screen(&term)
|
|
1380
|
+ |
);
|
|
1381
|
+ |
}
|
|
1382
|
+ |
|
|
1383
|
+ |
/// Scrolling stops at the last row rather than running off into a blank pane.
|
|
1384
|
+ |
#[test]
|
|
1385
|
+ |
fn scrolling_the_inspector_stops_at_the_end() {
|
|
1386
|
+ |
let (mut app, control, _rx) = with_a_diff();
|
|
1387
|
+ |
let mut term = terminal_of(90, HEIGHT);
|
|
1388
|
+ |
app.draw(&mut term).unwrap();
|
|
1389
|
+ |
for _ in 0..200 {
|
|
1390
|
+ |
app.on_key(&key(KeyCode::PageDown), 90, &control);
|
|
1391
|
+ |
}
|
|
1392
|
+ |
app.draw(&mut term).unwrap();
|
|
1393
|
+ |
let frame = screen(&term);
|
|
1394
|
+ |
assert!(
|
|
1395
|
+ |
frame.lines().any(|line| line.contains("end")),
|
|
1396
|
+ |
"the pane scrolled past everything it had:\n{frame}"
|
|
1397
|
+ |
);
|
|
1398
|
+ |
}
|
|
1399
|
+ |
|
|
1400
|
+ |
/// `/diff` against a real repository, through the real actor. This is the
|
|
1401
|
+ |
/// producer half: git is run, its output parsed, and the inspector opened.
|
|
1402
|
+ |
#[tokio::test]
|
|
1403
|
+ |
async fn slash_diff_shows_what_changed_in_a_real_repository() {
|
|
1404
|
+ |
let dir = tempfile::tempdir().expect("a temporary directory");
|
|
1405
|
+ |
let repo = dir.path();
|
|
1406
|
+ |
let git = |args: &[&str]| {
|
|
1407
|
+ |
std::process::Command::new("git")
|
|
1408
|
+ |
.args(args)
|
|
1409
|
+ |
.current_dir(repo)
|
|
1410
|
+ |
.output()
|
|
1411
|
+ |
.expect("git")
|
|
1412
|
+ |
};
|
|
1413
|
+ |
git(&["init", "-q"]);
|
|
1414
|
+ |
git(&["config", "user.email", "t@example.com"]);
|
|
1415
|
+ |
git(&["config", "user.name", "Test"]);
|
|
1416
|
+ |
std::fs::write(repo.join("thing.txt"), "keep\nold line\ntail\n").unwrap();
|
|
1417
|
+ |
git(&["add", "."]);
|
|
1418
|
+ |
git(&["commit", "-qm", "first"]);
|
|
1419
|
+ |
std::fs::write(repo.join("thing.txt"), "keep\nnew line\ntail\n").unwrap();
|
|
1420
|
+ |
|
|
1421
|
+ |
let files = openagents_cli::interactive::collect_diff(&[], repo)
|
|
1422
|
+ |
.await
|
|
1423
|
+ |
.expect("a diff from git");
|
|
1424
|
+ |
assert_eq!(files.len(), 1, "{files:?}");
|
|
1425
|
+ |
assert_eq!(files[0].path, "thing.txt");
|
|
1426
|
+ |
assert_eq!(files[0].stats(), (1, 1));
|
|
1427
|
+ |
|
|
1428
|
+ |
let mut app = CoderApp::new("openagents coder", &Lane::OxAlpha)
|
|
1429
|
+ |
.with_working_directory(repo.to_path_buf());
|
|
1430
|
+ |
let mut term = terminal_of(90, HEIGHT);
|
|
1431
|
+ |
app.on_turn_event(TurnEvent::Diff(files));
|
|
1432
|
+ |
app.draw(&mut term).unwrap();
|
|
1433
|
+ |
let frame = screen(&term);
|
|
1434
|
+ |
assert!(frame.contains("thing.txt"), "{frame}");
|
|
1435
|
+ |
assert!(frame.contains("\u{2212} old line"), "{frame}");
|
|
1436
|
+ |
assert!(frame.contains("+ new line"), "{frame}");
|
|
1437
|
+ |
}
|
|
1438
|
+ |
|
|
1439
|
+ |
/// Two files named directly are compared by this program, not by git, so
|
|
1440
|
+ |
/// `/diff` works on files that are not in a repository at all.
|
|
1441
|
+ |
#[tokio::test]
|
|
1442
|
+ |
async fn slash_diff_with_two_paths_compares_the_two_files() {
|
|
1443
|
+ |
let dir = tempfile::tempdir().expect("a temporary directory");
|
|
1444
|
+ |
std::fs::write(dir.path().join("before.txt"), "one\ntwo\nthree\n").unwrap();
|
|
1445
|
+ |
std::fs::write(dir.path().join("after.txt"), "one\nTWO\nthree\nfour\n").unwrap();
|
|
1446
|
+ |
|
|
1447
|
+ |
let files = openagents_cli::interactive::collect_diff(
|
|
1448
|
+ |
&["before.txt".to_string(), "after.txt".to_string()],
|
|
1449
|
+ |
dir.path(),
|
|
1450
|
+ |
)
|
|
1451
|
+ |
.await
|
|
1452
|
+ |
.expect("a diff of the two files");
|
|
1453
|
+ |
|
|
1454
|
+ |
assert_eq!(files.len(), 1);
|
|
1455
|
+ |
assert_eq!(files[0].stats(), (2, 1));
|
|
1456
|
+ |
assert_eq!(files[0].renamed_from.as_deref(), Some("before.txt"));
|
|
1457
|
+ |
}
|
|
1458
|
+ |
|
|
1459
|
+ |
/// A directory git knows nothing about is a refusal with a reason, not a
|
|
1460
|
+ |
/// silent empty inspector.
|
|
1461
|
+ |
#[tokio::test]
|
|
1462
|
+ |
async fn slash_diff_outside_a_repository_says_why_it_cannot() {
|
|
1463
|
+ |
let dir = tempfile::tempdir().expect("a temporary directory");
|
|
1464
|
+ |
let result = openagents_cli::interactive::collect_diff(&[], dir.path()).await;
|
|
1465
|
+ |
let message = result.expect_err("git should have refused here");
|
|
1466
|
+ |
assert!(
|
|
1467
|
+ |
message.to_lowercase().contains("git"),
|
|
1468
|
+ |
"the refusal does not say what refused: {message}"
|
|
1469
|
+ |
);
|
|
1470
|
+ |
}
|
|
1471
|
+ |
|
|
1472
|
+ |
// ------------------------------------------------- programs under a terminal
|
|
1473
|
+ |
|
|
1474
|
+ |
use openagents_cli::pty::PtyControl;
|
|
1475
|
+ |
use ratatui::layout::Rect;
|
|
1476
|
+ |
use std::sync::{Arc, Mutex};
|
|
1477
|
+ |
use std::time::Duration;
|
|
1478
|
+ |
|
|
1479
|
+ |
/// A session whose runtime actor is real but whose model is unreachable.
|
|
1480
|
+ |
///
|
|
1481
|
+ |
/// `/run` and `/diff` never touch the model, so the actor these tests drive is
|
|
1482
|
+ |
/// the production one and only the inference host is absent.
|
|
1483
|
+ |
fn actor_session() -> CoderRuntimeSession {
|
|
1484
|
+ |
CoderRuntimeSession::new(
|
|
1485
|
+ |
Lane::OxAlpha,
|
|
1486
|
+ |
// Reserved by RFC 6890 as "this host on this network": nothing here
|
|
1487
|
+ |
// reaches it, and a test that accidentally tried would fail loudly.
|
|
1488
|
+ |
Some("http://192.0.2.1:9/api/v1".to_string()),
|
|
1489
|
+ |
None,
|
|
1490
|
+ |
HarnessToolRegistry::new(Some(std::env::temp_dir())),
|
|
1491
|
+ |
)
|
|
1492
|
+ |
}
|
|
1493
|
+ |
|
|
1494
|
+ |
/// Deliver turn events to `app` until `done` holds or the deadline passes.
|
|
1495
|
+ |
async fn pump<F>(
|
|
1496
|
+ |
app: &mut CoderApp,
|
|
1497
|
+ |
turns: &mut UnboundedReceiver<TurnEvent>,
|
|
1498
|
+ |
done: F,
|
|
1499
|
+ |
within: Duration,
|
|
1500
|
+ |
) where
|
|
1501
|
+ |
F: Fn(&CoderApp) -> bool,
|
|
1502
|
+ |
{
|
|
1503
|
+ |
let deadline = tokio::time::Instant::now() + within;
|
|
1504
|
+ |
loop {
|
|
1505
|
+ |
if done(app) {
|
|
1506
|
+ |
return;
|
|
1507
|
+ |
}
|
|
1508
|
+ |
let left = deadline.saturating_duration_since(tokio::time::Instant::now());
|
|
1509
|
+ |
if left.is_zero() {
|
|
1510
|
+ |
return;
|
|
1511
|
+ |
}
|
|
1512
|
+ |
match tokio::time::timeout(left, turns.recv()).await {
|
|
1513
|
+ |
Ok(Some(event)) => app.on_turn_event(event),
|
|
1514
|
+ |
_ => return,
|
|
1515
|
+ |
}
|
|
1516
|
+ |
}
|
|
1517
|
+ |
}
|
|
1518
|
+ |
|
|
1519
|
+ |
/// Start a session, run `command` in it, and pump until `done`.
|
|
1520
|
+ |
async fn run_in_a_pane<F>(
|
|
1521
|
+ |
command: &str,
|
|
1522
|
+ |
size: (u16, u16),
|
|
1523
|
+ |
done: F,
|
|
1524
|
+ |
within: Duration,
|
|
1525
|
+ |
) -> (
|
|
1526
|
+ |
CoderApp,
|
|
1527
|
+ |
Terminal<TestBackend>,
|
|
1528
|
+ |
UnboundedReceiver<TurnEvent>,
|
|
1529
|
+ |
)
|
|
1530
|
+ |
where
|
|
1531
|
+ |
F: Fn(&CoderApp) -> bool,
|
|
1532
|
+ |
{
|
|
1533
|
+ |
let (control_tx, control_rx) = unbounded_channel::<Control>();
|
|
1534
|
+ |
let (turn_tx, mut turn_rx) = unbounded_channel::<TurnEvent>();
|
|
1535
|
+ |
tokio::spawn(runtime_actor(actor_session(), control_rx, turn_tx.clone()));
|
|
1536
|
+ |
|
|
1537
|
+ |
let mut term = terminal_of(size.0, size.1);
|
|
1538
|
+ |
let mut app = CoderApp::new("openagents coder", &Lane::OxAlpha);
|
|
1539
|
+ |
// The frame's size is what the child is told, so it has to be known before
|
|
1540
|
+ |
// the child starts.
|
|
1541
|
+ |
app.draw(&mut term).unwrap();
|
|
1542
|
+ |
app.submit(command.to_string(), &control_tx);
|
|
1543
|
+ |
|
|
1544
|
+ |
pump(&mut app, &mut turn_rx, done, within).await;
|
|
1545
|
+ |
app.draw(&mut term).unwrap();
|
|
1546
|
+ |
(app, term, turn_rx)
|
|
1547
|
+ |
}
|
|
1548
|
+ |
|
|
1549
|
+ |
/// The claim this whole module exists for: the child is on a terminal.
|
|
1550
|
+ |
///
|
|
1551
|
+ |
/// `tty` prints the terminal it is attached to, and prints `not a tty` when it
|
|
1552
|
+ |
/// is attached to a pipe. Under the buffered `Command::output` the crate used
|
|
1553
|
+ |
/// everywhere else, this test would print the second.
|
|
1554
|
+ |
#[tokio::test]
|
|
1555
|
+ |
async fn a_program_run_in_the_frame_is_on_a_real_terminal() {
|
|
1556
|
+ |
let (_app, term, _rx) = run_in_a_pane(
|
|
1557
|
+ |
"/run tty",
|
|
1558
|
+ |
(80, 24),
|
|
1559
|
+ |
|app| app.pty_exit().is_some(),
|
|
1560
|
+ |
Duration::from_secs(10),
|
|
1561
|
+ |
)
|
|
1562
|
+ |
.await;
|
|
1563
|
+ |
|
|
1564
|
+ |
let frame = screen(&term);
|
|
1565
|
+ |
assert!(
|
|
1566
|
+ |
frame.contains("/dev/"),
|
|
1567
|
+ |
"the child did not report a terminal:\n{frame}"
|
|
1568
|
+ |
);
|
|
1569
|
+ |
assert!(
|
|
1570
|
+ |
!frame.contains("not a tty"),
|
|
1571
|
+ |
"the child was given a pipe, not a pseudoterminal:\n{frame}"
|
|
1572
|
+ |
);
|
|
1573
|
+ |
assert!(frame.contains("Run \u{b7} tty"), "{frame}");
|
|
1574
|
+ |
}
|
|
1575
|
+ |
|
|
1576
|
+ |
/// The child is told the size of the pane it is drawn into, and no other size.
|
|
1577
|
+ |
///
|
|
1578
|
+ |
/// `stty size` asks the kernel for the window size of its terminal, which only
|
|
1579
|
+ |
/// exists because there is a terminal. An 80x24 frame leaves 78 columns and 16
|
|
1580
|
+ |
/// rows inside the header, the status bar, and the pane's own rules.
|
|
1581
|
+ |
#[tokio::test]
|
|
1582
|
+ |
async fn the_program_is_told_the_size_of_the_pane_it_is_drawn_into() {
|
|
1583
|
+ |
let (_app, term, _rx) = run_in_a_pane(
|
|
1584
|
+ |
"/run stty size",
|
|
1585
|
+ |
(80, 24),
|
|
1586
|
+ |
|app| app.pty_exit().is_some(),
|
|
1587
|
+ |
Duration::from_secs(10),
|
|
1588
|
+ |
)
|
|
1589
|
+ |
.await;
|
|
1590
|
+ |
|
|
1591
|
+ |
assert!(
|
|
1592
|
+ |
screen(&term).contains("16 78"),
|
|
1593
|
+ |
"the child was told the wrong window size:\n{}",
|
|
1594
|
+ |
screen(&term)
|
|
1595
|
+ |
);
|
|
1596
|
+ |
}
|
|
1597
|
+ |
|
|
1598
|
+ |
/// Colour survives the trip: the child emits an SGR sequence and the frame
|
|
1599
|
+ |
/// draws the cell in that colour. A pipe would have made most programs drop it.
|
|
1600
|
+ |
#[tokio::test]
|
|
1601
|
+ |
async fn colour_the_program_writes_reaches_the_frame() {
|
|
1602
|
+ |
use ratatui::style::Color;
|
|
1603
|
+ |
let (_app, term, _rx) = run_in_a_pane(
|
|
1604
|
+ |
r"/run printf \033[31mRED\033[0m",
|
|
1605
|
+ |
(80, 24),
|
|
1606
|
+ |
|app| app.pty_exit().is_some(),
|
|
1607
|
+ |
Duration::from_secs(10),
|
|
1608
|
+ |
)
|
|
1609
|
+ |
.await;
|
|
1610
|
+ |
|
|
1611
|
+ |
assert_eq!(
|
|
1612
|
+ |
cell_in_the_pane(&term, "RED").fg,
|
|
1613
|
+ |
Color::Red,
|
|
1614
|
+ |
"the colour the child asked for was not drawn:\n{}",
|
|
1615
|
+ |
screen(&term)
|
|
1616
|
+ |
);
|
|
1617
|
+ |
}
|
|
1618
|
+ |
|
|
1619
|
+ |
/// A full-screen program: it clears the screen, moves the cursor, and draws.
|
|
1620
|
+ |
/// Nothing of that works down a pipe.
|
|
1621
|
+ |
#[tokio::test]
|
|
1622
|
+ |
async fn a_program_that_draws_a_screen_is_drawn_where_it_asked_to_be() {
|
|
1623
|
+ |
let (_app, term, _rx) = run_in_a_pane(
|
|
1624
|
+ |
// Cursor addressing without a semicolon in it: `5d` is line-position
|
|
1625
|
+ |
// absolute and `10G` is column absolute. A `;` would send the line to
|
|
1626
|
+ |
// a shell, which would then try to glob `[2J`.
|
|
1627
|
+ |
r"/run printf \033[2J\033[5d\033[10Gmiddle",
|
|
1628
|
+ |
(80, 24),
|
|
1629
|
+ |
|app| app.pty_exit().is_some(),
|
|
1630
|
+ |
Duration::from_secs(10),
|
|
1631
|
+ |
)
|
|
1632
|
+ |
.await;
|
|
1633
|
+ |
|
|
1634
|
+ |
// The pane's inner area starts at column 1 and row 4 of the frame, and the
|
|
1635
|
+ |
// child asked for row 5, column 10 of its own screen.
|
|
1636
|
+ |
assert_eq!(
|
|
1637
|
+ |
position_from(&term, "middle", PANE_TOP),
|
|
1638
|
+ |
(1 + 9, 4 + 4),
|
|
1639
|
+ |
"\n{}",
|
|
1640
|
+ |
screen(&term)
|
|
1641
|
+ |
);
|
|
1642
|
+ |
}
|
|
1643
|
+ |
|
|
1644
|
+ |
/// Keys typed reach the program, and `Ctrl+]` takes the keyboard back.
|
|
1645
|
+ |
#[tokio::test]
|
|
1646
|
+ |
async fn keys_reach_the_program_and_ctrl_bracket_takes_them_back() {
|
|
1647
|
+ |
// Wide enough that the status bar has room for its hint; the narrow case
|
|
1648
|
+ |
// is covered where the dropping rule is.
|
|
1649
|
+ |
const WIDE: u16 = 140;
|
|
1650
|
+ |
let (control_tx, control_rx) = unbounded_channel::<Control>();
|
|
1651
|
+ |
let (turn_tx, mut turn_rx) = unbounded_channel::<TurnEvent>();
|
|
1652
|
+ |
tokio::spawn(runtime_actor(actor_session(), control_rx, turn_tx.clone()));
|
|
1653
|
+ |
|
|
1654
|
+ |
let mut term = terminal_of(WIDE, HEIGHT);
|
|
1655
|
+ |
let mut app = CoderApp::new("openagents coder", &Lane::OxAlpha);
|
|
1656
|
+ |
app.draw(&mut term).unwrap();
|
|
1657
|
+ |
app.submit("/run cat".to_string(), &control_tx);
|
|
1658
|
+ |
pump(
|
|
1659
|
+ |
&mut app,
|
|
1660
|
+ |
&mut turn_rx,
|
|
1661
|
+ |
|app| app.running(),
|
|
1662
|
+ |
Duration::from_secs(10),
|
|
1663
|
+ |
)
|
|
1664
|
+ |
.await;
|
|
1665
|
+ |
|
|
1666
|
+ |
// The bar offers exactly the key that works here, and no other.
|
|
1667
|
+ |
app.draw(&mut term).unwrap();
|
|
1668
|
+ |
let frame = screen(&term);
|
|
1669
|
+ |
assert!(frame.contains("Ctrl+]: stop and go back"), "{frame}");
|
|
1670
|
+ |
assert!(!frame.contains("Enter: send"), "{frame}");
|
|
1671
|
+ |
assert!(frame.contains("Status: running"), "{frame}");
|
|
1672
|
+ |
|
|
1673
|
+ |
// `cat` echoes a line once its terminal has one to give it.
|
|
1674
|
+ |
for ch in "ping".chars() {
|
|
1675
|
+ |
app.on_key(&key(KeyCode::Char(ch)), WIDE, &control_tx);
|
|
1676
|
+ |
}
|
|
1677
|
+ |
app.on_key(&key(KeyCode::Enter), WIDE, &control_tx);
|
|
1678
|
+ |
pump(
|
|
1679
|
+ |
&mut app,
|
|
1680
|
+ |
&mut turn_rx,
|
|
1681
|
+ |
|app| {
|
|
1682
|
+ |
app.pty_text()
|
|
1683
|
+ |
.is_some_and(|text| text.matches("ping").count() >= 2)
|
|
1684
|
+ |
},
|
|
1685
|
+ |
Duration::from_secs(10),
|
|
1686
|
+ |
)
|
|
1687
|
+ |
.await;
|
|
1688
|
+ |
app.draw(&mut term).unwrap();
|
|
1689
|
+ |
assert!(
|
|
1690
|
+ |
screen(&term).matches("ping").count() >= 2,
|
|
1691
|
+ |
"the keys did not reach the program:\n{}",
|
|
1692
|
+ |
screen(&term)
|
|
1693
|
+ |
);
|
|
1694
|
+ |
|
|
1695
|
+ |
// Ctrl+] ends it and hands the keyboard back to the composer.
|
|
1696
|
+ |
app.on_key(
|
|
1697
|
+ |
&KeyEvent::new(KeyCode::Char(']'), KeyModifiers::CONTROL),
|
|
1698
|
+ |
WIDE,
|
|
1699
|
+ |
&control_tx,
|
|
1700
|
+ |
);
|
|
1701
|
+ |
app.draw(&mut term).unwrap();
|
|
1702
|
+ |
let frame = screen(&term);
|
|
1703
|
+ |
assert!(!app.running(), "the program was not stopped");
|
|
1704
|
+ |
assert!(frame.contains("Stopped"), "{frame}");
|
|
1705
|
+ |
assert!(
|
|
1706
|
+ |
frame.contains("Message"),
|
|
1707
|
+ |
"the composer did not come back:\n{frame}"
|
|
1708
|
+ |
);
|
|
1709
|
+ |
assert!(!app.should_exit(), "Ctrl+] ended the session");
|
|
1710
|
+ |
}
|
|
1711
|
+ |
|
|
1712
|
+ |
/// Esc belongs to the program, not to the session. A full-screen program that
|
|
1713
|
+ |
/// could not receive Esc would be unusable, and a session that exited on it
|
|
1714
|
+ |
/// would take the reader out of their editor.
|
|
1715
|
+ |
#[tokio::test]
|
|
1716
|
+ |
async fn esc_goes_to_the_program_rather_than_ending_the_session() {
|
|
1717
|
+ |
let (control_tx, control_rx) = unbounded_channel::<Control>();
|
|
1718
|
+ |
let (turn_tx, mut turn_rx) = unbounded_channel::<TurnEvent>();
|
|
1719
|
+ |
tokio::spawn(runtime_actor(actor_session(), control_rx, turn_tx.clone()));
|
|
1720
|
+ |
|
|
1721
|
+ |
let mut term = terminal();
|
|
1722
|
+ |
let mut app = CoderApp::new("openagents coder", &Lane::OxAlpha);
|
|
1723
|
+ |
app.draw(&mut term).unwrap();
|
|
1724
|
+ |
app.submit("/run cat".to_string(), &control_tx);
|
|
1725
|
+ |
pump(
|
|
1726
|
+ |
&mut app,
|
|
1727
|
+ |
&mut turn_rx,
|
|
1728
|
+ |
|app| app.running(),
|
|
1729
|
+ |
Duration::from_secs(10),
|
|
1730
|
+ |
)
|
|
1731
|
+ |
.await;
|
|
1732
|
+ |
|
|
1733
|
+ |
app.on_key(&key(KeyCode::Esc), WIDTH, &control_tx);
|
|
1734
|
+ |
assert!(!app.should_exit(), "Esc over a running program exited");
|
|
1735
|
+ |
assert!(app.running());
|
|
1736
|
+ |
|
|
1737
|
+ |
app.on_key(
|
|
1738
|
+ |
&KeyEvent::new(KeyCode::Char(']'), KeyModifiers::CONTROL),
|
|
1739
|
+ |
WIDTH,
|
|
1740
|
+ |
&control_tx,
|
|
1741
|
+ |
);
|
|
1742
|
+ |
}
|
|
1743
|
+ |
|
|
1744
|
+ |
/// A program that ends leaves its output up, because that output is usually
|
|
1745
|
+ |
/// the answer, and says how it ended.
|
|
1746
|
+ |
#[tokio::test]
|
|
1747
|
+ |
async fn a_program_that_fails_reports_its_exit_code() {
|
|
1748
|
+ |
let (mut app, term, _rx) = run_in_a_pane(
|
|
1749
|
+ |
"/run sh -c \"exit 3\"",
|
|
1750
|
+ |
(140, 24),
|
|
1751
|
+ |
|app| app.pty_exit().is_some(),
|
|
1752
|
+ |
Duration::from_secs(10),
|
|
1753
|
+ |
)
|
|
1754
|
+ |
.await;
|
|
1755
|
+ |
|
|
1756
|
+ |
assert_eq!(app.pty_exit(), Some(3));
|
|
1757
|
+ |
assert!(screen(&term).contains("exited 3"), "{}", screen(&term));
|
|
1758
|
+ |
// The bar stops offering the key that stops a program that has stopped.
|
|
1759
|
+ |
assert!(
|
|
1760
|
+ |
screen(&term).contains("Enter: go back"),
|
|
1761
|
+ |
"{}",
|
|
1762
|
+ |
screen(&term)
|
|
1763
|
+ |
);
|
|
1764
|
+ |
assert!(!screen(&term).contains("Ctrl+]"), "{}", screen(&term));
|
|
1765
|
+ |
|
|
1766
|
+ |
let (tx, _rx2) = tokio::sync::mpsc::unbounded_channel();
|
|
1767
|
+ |
let mut term = term;
|
|
1768
|
+ |
app.on_key(&key(KeyCode::Enter), 140, &tx);
|
|
1769
|
+ |
app.draw(&mut term).unwrap();
|
|
1770
|
+ |
assert!(
|
|
1771
|
+ |
screen(&term).contains("exited with code 3"),
|
|
1772
|
+ |
"{}",
|
|
1773
|
+ |
screen(&term)
|
|
1774
|
+ |
);
|
|
1775
|
+ |
assert!(screen(&term).contains("Message"), "{}", screen(&term));
|
|
1776
|
+ |
}
|
|
1777
|
+ |
|
|
1778
|
+ |
/// A command that does not exist is reported, and the session stays open.
|
|
1779
|
+ |
#[tokio::test]
|
|
1780
|
+ |
async fn a_command_that_is_not_there_is_reported_rather_than_hanging() {
|
|
1781
|
+ |
let (control_tx, control_rx) = unbounded_channel::<Control>();
|
|
1782
|
+ |
let (turn_tx, mut turn_rx) = unbounded_channel::<TurnEvent>();
|
|
1783
|
+ |
tokio::spawn(runtime_actor(actor_session(), control_rx, turn_tx.clone()));
|
|
1784
|
+ |
|
|
1785
|
+ |
let mut term = terminal_of(100, HEIGHT);
|
|
1786
|
+ |
let mut app = CoderApp::new("openagents coder", &Lane::OxAlpha);
|
|
1787
|
+ |
app.draw(&mut term).unwrap();
|
|
1788
|
+ |
app.submit(
|
|
1789
|
+ |
"/run this-program-does-not-exist-anywhere".to_string(),
|
|
1790
|
+ |
&control_tx,
|
|
1791
|
+ |
);
|
|
1792
|
+ |
pump(
|
|
1793
|
+ |
&mut app,
|
|
1794
|
+ |
&mut turn_rx,
|
|
1795
|
+ |
|app| app.entries().iter().any(|e| e.text.contains("Could not")),
|
|
1796
|
+ |
Duration::from_secs(10),
|
|
1797
|
+ |
)
|
|
1798
|
+ |
.await;
|
|
1799
|
+ |
app.draw(&mut term).unwrap();
|
|
1800
|
+ |
|
|
1801
|
+ |
assert!(!app.running());
|
|
1802
|
+ |
assert!(
|
|
1803
|
+ |
screen(&term).contains("Could not run it"),
|
|
1804
|
+ |
"{}",
|
|
1805
|
+ |
screen(&term)
|
|
1806
|
+ |
);
|
|
1807
|
+ |
}
|
|
1808
|
+ |
|
|
1809
|
+ |
/// What a resize does to the program's side, without depending on a signal
|
|
1810
|
+ |
/// arriving in a test's timing window.
|
|
1811
|
+ |
#[derive(Debug, Default)]
|
|
1812
|
+ |
struct RecordingControl {
|
|
1813
|
+ |
sizes: Mutex<Vec<(u16, u16)>>,
|
|
1814
|
+ |
killed: Mutex<bool>,
|
|
1815
|
+ |
written: Mutex<Vec<u8>>,
|
|
1816
|
+ |
}
|
|
1817
|
+ |
|
|
1818
|
+ |
impl PtyControl for RecordingControl {
|
|
1819
|
+ |
fn write(&self, bytes: &[u8]) {
|
|
1820
|
+ |
self.written.lock().unwrap().extend_from_slice(bytes);
|
|
1821
|
+ |
}
|
|
1822
|
+ |
fn resize(&self, cols: u16, rows: u16) {
|
|
1823
|
+ |
self.sizes.lock().unwrap().push((cols, rows));
|
|
1824
|
+ |
}
|
|
1825
|
+ |
fn kill(&self) {
|
|
1826
|
+ |
*self.killed.lock().unwrap() = true;
|
|
1827
|
+ |
}
|
|
1828
|
+ |
}
|
|
1829
|
+ |
|
|
1830
|
+ |
#[test]
|
|
1831
|
+ |
fn resizing_the_window_resizes_the_program() {
|
|
1832
|
+ |
let (mut app, control, _rx) = app();
|
|
1833
|
+ |
let recorder = Arc::new(RecordingControl::default());
|
|
1834
|
+ |
app.on_turn_event(TurnEvent::PtyOpen {
|
|
1835
|
+ |
label: "cat".to_string(),
|
|
1836
|
+ |
control: recorder.clone(),
|
|
1837
|
+ |
});
|
|
1838
|
+ |
|
|
1839
|
+ |
let mut term = terminal_of(80, 24);
|
|
1840
|
+ |
app.draw(&mut term).unwrap();
|
|
1841
|
+ |
// The first draw sets the pane's size; nothing has changed yet, so nothing
|
|
1842
|
+ |
// is sent — a resize the child did not need is a signal it did not need.
|
|
1843
|
+ |
assert!(recorder.sizes.lock().unwrap().is_empty());
|
|
1844
|
+ |
|
|
1845
|
+ |
app.on_size(Rect::new(0, 0, 100, 30));
|
|
1846
|
+ |
assert_eq!(
|
|
1847
|
+ |
recorder.sizes.lock().unwrap().as_slice(),
|
|
1848
|
+ |
&[(98, 22)],
|
|
1849
|
+ |
"the new window size did not reach the program"
|
|
1850
|
+ |
);
|
|
1851
|
+ |
|
|
1852
|
+ |
// And drawing at that size again does not send it twice.
|
|
1853
|
+ |
app.on_size(Rect::new(0, 0, 100, 30));
|
|
1854
|
+ |
assert_eq!(recorder.sizes.lock().unwrap().len(), 1);
|
|
1855
|
+ |
let _ = control;
|
|
1856
|
+ |
}
|
|
1857
|
+ |
|
|
1858
|
+ |
#[test]
|
|
1859
|
+ |
fn a_key_over_a_running_program_is_sent_as_the_bytes_a_terminal_would_send() {
|
|
1860
|
+ |
let (mut app, control, _rx) = app();
|
|
1861
|
+ |
let recorder = Arc::new(RecordingControl::default());
|
|
1862
|
+ |
app.on_turn_event(TurnEvent::PtyOpen {
|
|
1863
|
+ |
label: "cat".to_string(),
|
|
1864
|
+ |
control: recorder.clone(),
|
|
1865
|
+ |
});
|
|
1866
|
+ |
|
|
1867
|
+ |
app.on_key(&key(KeyCode::Char('h')), WIDTH, &control);
|
|
1868
|
+ |
app.on_key(&key(KeyCode::Enter), WIDTH, &control);
|
|
1869
|
+ |
app.on_key(
|
|
1870
|
+ |
&KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL),
|
|
1871
|
+ |
WIDTH,
|
|
1872
|
+ |
&control,
|
|
1873
|
+ |
);
|
|
1874
|
+ |
assert_eq!(recorder.written.lock().unwrap().as_slice(), b"h\r\x03");
|
|
1875
|
+ |
assert!(!*recorder.killed.lock().unwrap());
|
|
1876
|
+ |
|
|
1877
|
+ |
app.on_key(
|
|
1878
|
+ |
&KeyEvent::new(KeyCode::Char(']'), KeyModifiers::CONTROL),
|
|
1879
|
+ |
WIDTH,
|
|
1880
|
+ |
&control,
|
|
1881
|
+ |
);
|
|
1882
|
+ |
assert!(*recorder.killed.lock().unwrap(), "Ctrl+] did not stop it");
|
|
1883
|
+ |
}
|
|
1884
|
+ |
|
|
1885
|
+ |
/// The whole loop, over a real pseudoterminal: keys in at the top, a program
|
|
1886
|
+ |
/// run, and its ending reported on the transcript.
|
|
1887
|
+ |
#[tokio::test]
|
|
1888
|
+ |
async fn end_to_end_over_the_loop_running_a_program_under_a_terminal() {
|
|
1889
|
+ |
let (control_tx, control_rx) = unbounded_channel::<Control>();
|
|
1890
|
+ |
let (turn_tx, mut turn_rx) = unbounded_channel::<TurnEvent>();
|
|
1891
|
+ |
tokio::spawn(runtime_actor(actor_session(), control_rx, turn_tx.clone()));
|
|
1892
|
+ |
|
|
1893
|
+ |
let mut term = terminal_of(90, HEIGHT);
|
|
1894
|
+ |
let mut app = CoderApp::new("openagents coder", &Lane::OxAlpha);
|
|
1895
|
+ |
let (keys_tx, keys_rx) = unbounded_channel();
|
|
1896
|
+ |
|
|
1897
|
+ |
send_keys(&keys_tx, "/run tty");
|
|
1898
|
+ |
let _ = keys_tx.send(Event::Key(key(KeyCode::Enter)));
|
|
1899
|
+ |
|
|
1900
|
+ |
// Once the program has ended, dismiss its pane and leave.
|
|
1901
|
+ |
let keys_for_exit = keys_tx.clone();
|
|
1902
|
+ |
tokio::spawn(async move {
|
|
1903
|
+ |
tokio::time::sleep(Duration::from_millis(1500)).await;
|
|
1904
|
+ |
let _ = keys_for_exit.send(Event::Key(key(KeyCode::Enter)));
|
|
1905
|
+ |
tokio::time::sleep(Duration::from_millis(200)).await;
|
|
1906
|
+ |
let _ = keys_for_exit.send(Event::Key(key(KeyCode::Esc)));
|
|
1907
|
+ |
});
|
|
1908
|
+ |
|
|
1909
|
+ |
drive(
|
|
1910
|
+ |
&mut app,
|
|
1911
|
+ |
&mut term,
|
|
1912
|
+ |
keys_rx,
|
|
1913
|
+ |
control_tx,
|
|
1914
|
+ |
&mut turn_rx,
|
|
1915
|
+ |
turn_tx,
|
|
1916
|
+ |
)
|
|
1917
|
+ |
.await;
|
|
1918
|
+ |
|
|
1919
|
+ |
let frame = screen(&term);
|
|
1920
|
+ |
assert!(app.should_exit(), "the loop did not exit");
|
|
1921
|
+ |
assert!(
|
|
1922
|
+ |
frame.contains("`tty` finished."),
|
|
1923
|
+ |
"the program did not run to completion through the loop:\n{frame}"
|
|
1924
|
+ |
);
|
|
1925
|
+ |
}
|
|
1926
|
+ |
|
|
1927
|
+ |
/// The resize reaches the running program as a signal, not just as a number.
|
|
1928
|
+ |
///
|
|
1929
|
+ |
/// The shell traps `SIGWINCH` and prints the window size the kernel now
|
|
1930
|
+ |
/// reports. Nothing about that is emulated: the size is set with the same
|
|
1931
|
+ |
/// `TIOCSWINSZ` a terminal emulator uses, and the kernel is what raises the
|
|
1932
|
+ |
/// signal.
|
|
1933
|
+ |
#[tokio::test]
|
|
1934
|
+ |
async fn resizing_the_frame_signals_the_running_program() {
|
|
1935
|
+ |
let (control_tx, control_rx) = unbounded_channel::<Control>();
|
|
1936
|
+ |
let (turn_tx, mut turn_rx) = unbounded_channel::<TurnEvent>();
|
|
1937
|
+ |
tokio::spawn(runtime_actor(actor_session(), control_rx, turn_tx.clone()));
|
|
1938
|
+ |
|
|
1939
|
+ |
let mut term = terminal_of(80, 24);
|
|
1940
|
+ |
let mut app = CoderApp::new("openagents coder", &Lane::OxAlpha);
|
|
1941
|
+ |
app.draw(&mut term).unwrap();
|
|
1942
|
+ |
app.submit(
|
|
1943
|
+ |
"/run trap 'stty size' WINCH; stty size; for i in 1 2 3 4 5 6 7 8 9 10; do sleep 0.3; done"
|
|
1944
|
+ |
.to_string(),
|
|
1945
|
+ |
&control_tx,
|
|
1946
|
+ |
);
|
|
1947
|
+ |
|
|
1948
|
+ |
let says = |needle: &'static str| {
|
|
1949
|
+ |
move |app: &CoderApp| app.pty_text().is_some_and(|text| text.contains(needle))
|
|
1950
|
+ |
};
|
|
1951
|
+ |
pump(
|
|
1952
|
+ |
&mut app,
|
|
1953
|
+ |
&mut turn_rx,
|
|
1954
|
+ |
says("16 78"),
|
|
1955
|
+ |
Duration::from_secs(10),
|
|
1956
|
+ |
)
|
|
1957
|
+ |
.await;
|
|
1958
|
+ |
app.draw(&mut term).unwrap();
|
|
1959
|
+ |
assert!(
|
|
1960
|
+ |
screen(&term).contains("16 78"),
|
|
1961
|
+ |
"the program never reported its starting size:\n{}",
|
|
1962
|
+ |
screen(&term)
|
|
1963
|
+ |
);
|
|
1964
|
+ |
|
|
1965
|
+ |
// Widen the window. `draw` is what notices, and what tells the child.
|
|
1966
|
+ |
term.backend_mut().resize(100, 30);
|
|
1967
|
+ |
app.draw(&mut term).unwrap();
|
|
1968
|
+ |
|
|
1969
|
+ |
pump(
|
|
1970
|
+ |
&mut app,
|
|
1971
|
+ |
&mut turn_rx,
|
|
1972
|
+ |
says("22 98"),
|
|
1973
|
+ |
Duration::from_secs(10),
|
|
1974
|
+ |
)
|
|
1975
|
+ |
.await;
|
|
1976
|
+ |
app.draw(&mut term).unwrap();
|
|
1977
|
+ |
assert!(
|
|
1978
|
+ |
screen(&term).contains("22 98"),
|
|
1979
|
+ |
"the resize did not reach the program as a signal:\n{}",
|
|
1980
|
+ |
screen(&term)
|
|
1981
|
+ |
);
|
|
1982
|
+ |
|
|
1983
|
+ |
app.on_key(
|
|
1984
|
+ |
&KeyEvent::new(KeyCode::Char(']'), KeyModifiers::CONTROL),
|
|
1985
|
+ |
100,
|
|
1986
|
+ |
&control_tx,
|
|
1987
|
+ |
);
|
|
1988
|
+ |
}
|
|
1989
|
+ |
|
|
1990
|
+ |
/// Every key the inspector's own status bar names has to do something too.
|
|
1991
|
+ |
#[test]
|
|
1992
|
+ |
fn every_key_the_inspectors_status_bar_names_does_something() {
|
|
1993
|
+ |
let (mut app, control, _rx) = with_a_diff();
|
|
1994
|
+ |
let mut term = terminal_of(200, HEIGHT);
|
|
1995
|
+ |
app.draw(&mut term).unwrap();
|
|
1996
|
+ |
let frame = screen(&term);
|
|
1997
|
+ |
for hint in [
|
|
1998
|
+ |
"Esc: close",
|
|
1999
|
+ |
"v: change view",
|
|
2000
|
+ |
"Tab: next file",
|
|
2001
|
+ |
"\u{2191}\u{2193} PgUp/PgDn: scroll",
|
|
2002
|
+ |
] {
|
|
2003
|
+ |
assert!(
|
|
2004
|
+ |
frame.contains(hint),
|
|
2005
|
+ |
"the bar does not offer {hint}:\n{frame}"
|
|
2006
|
+ |
);
|
|
2007
|
+ |
}
|
|
2008
|
+ |
|
|
2009
|
+ |
// v changes the view.
|
|
2010
|
+ |
app.on_key(&key(KeyCode::Char('v')), 200, &control);
|
|
2011
|
+ |
app.draw(&mut term).unwrap();
|
|
2012
|
+ |
assert!(screen(&term).contains("side by side"), "{}", screen(&term));
|
|
2013
|
+ |
|
|
2014
|
+ |
// Tab changes the file.
|
|
2015
|
+ |
app.on_key(&key(KeyCode::Tab), 200, &control);
|
|
2016
|
+ |
app.draw(&mut term).unwrap();
|
|
2017
|
+ |
assert!(screen(&term).contains("2 of 2"), "{}", screen(&term));
|
|
2018
|
+ |
|
|
2019
|
+ |
// Down scrolls, and Up comes back.
|
|
2020
|
+ |
app.on_key(&key(KeyCode::Down), 200, &control);
|
|
2021
|
+ |
app.draw(&mut term).unwrap();
|
|
2022
|
+ |
let scrolled = screen(&term);
|
|
2023
|
+ |
assert!(
|
|
2024
|
+ |
!scrolled.contains("README.md +1"),
|
|
2025
|
+ |
"Down did not scroll the header off:\n{scrolled}"
|
|
2026
|
+ |
);
|
|
2027
|
+ |
app.on_key(&key(KeyCode::Up), 200, &control);
|
|
2028
|
+ |
app.draw(&mut term).unwrap();
|
|
2029
|
+ |
assert!(screen(&term).contains("README.md +1"), "{}", screen(&term));
|
|
2030
|
+ |
|
|
2031
|
+ |
// PgDn moves further than Down did.
|
|
2032
|
+ |
app.on_key(&key(KeyCode::PageDown), 200, &control);
|
|
2033
|
+ |
app.on_key(&key(KeyCode::PageUp), 200, &control);
|
|
2034
|
+ |
app.draw(&mut term).unwrap();
|
|
2035
|
+ |
assert!(screen(&term).contains("README.md +1"), "{}", screen(&term));
|
|
2036
|
+ |
|
|
2037
|
+ |
// Esc closes.
|
|
2038
|
+ |
app.on_key(&key(KeyCode::Esc), 200, &control);
|
|
2039
|
+ |
assert!(!app.inspecting());
|
|
2040
|
+ |
assert!(!app.should_exit());
|
|
2041
|
+ |
}
|
|
2042
|
+ |
|
|
2043
|
+ |
/// The status bar's model and token counts, over the real stack: a real socket
|
|
2044
|
+ |
/// speaking real server-sent events, through the real `runtime_actor`.
|
|
2045
|
+ |
///
|
|
2046
|
+ |
/// The model is read from the session's `last_model` rather than from its
|
|
2047
|
+ |
/// grant, which is what makes the local lane — which never opens a grant —
|
|
2048
|
+ |
/// report a model at all.
|
|
2049
|
+ |
#[tokio::test]
|
|
2050
|
+ |
async fn end_to_end_over_real_http_the_bar_reports_the_model_and_the_tokens() {
|
|
2051
|
+ |
let stub = support::start_reporting_usage(vec!["Done."], (128, 64, 192)).await;
|
|
2052
|
+ |
|
|
2053
|
+ |
let session = CoderRuntimeSession::new(
|
|
2054
|
+ |
Lane::OxAlpha,
|
|
2055
|
+ |
Some(stub.base),
|
|
2056
|
+ |
Some("oat_test".to_string()),
|
|
2057
|
+ |
HarnessToolRegistry::new(Some(std::env::temp_dir())),
|
|
2058
|
+ |
);
|
|
2059
|
+ |
|
|
2060
|
+ |
let (control_tx, control_rx) = unbounded_channel::<Control>();
|
|
2061
|
+ |
let (turn_tx, mut turn_rx) = unbounded_channel::<TurnEvent>();
|
|
2062
|
+ |
tokio::spawn(runtime_actor(session, control_rx, turn_tx.clone()));
|
|
2063
|
+ |
|
|
2064
|
+ |
let mut term = terminal_of(140, HEIGHT);
|
|
2065
|
+ |
let mut app = CoderApp::new("openagents coder", &Lane::OxAlpha);
|
|
2066
|
+ |
let (keys_tx, keys_rx) = unbounded_channel();
|
|
2067
|
+ |
send_keys(&keys_tx, "how much");
|
|
2068
|
+ |
let _ = keys_tx.send(Event::Key(key(KeyCode::Enter)));
|
|
2069
|
+ |
|
|
2070
|
+ |
let keys_for_exit = keys_tx.clone();
|
|
2071
|
+ |
tokio::spawn(async move {
|
|
2072
|
+ |
tokio::time::sleep(Duration::from_millis(2000)).await;
|
|
2073
|
+ |
let _ = keys_for_exit.send(Event::Key(key(KeyCode::Esc)));
|
|
2074
|
+ |
});
|
|
2075
|
+ |
|
|
2076
|
+ |
drive(
|
|
2077
|
+ |
&mut app,
|
|
2078
|
+ |
&mut term,
|
|
2079
|
+ |
keys_rx,
|
|
2080
|
+ |
control_tx,
|
|
2081
|
+ |
&mut turn_rx,
|
|
2082
|
+ |
turn_tx,
|
|
2083
|
+ |
)
|
|
2084
|
+ |
.await;
|
|
2085
|
+ |
|
|
2086
|
+ |
let row = status_row(&term);
|
|
2087
|
+ |
assert!(row.contains("Model: ox-alpha"), "{row}");
|
|
2088
|
+ |
assert!(
|
|
2089
|
+ |
row.contains("Tokens: 128+64=192"),
|
|
2090
|
+ |
"the tokens the server reported are not on the bar: {row}"
|
|
2091
|
+ |
);
|
|
2092
|
+ |
assert_eq!(
|
|
2093
|
+ |
app.usage().total_tokens,
|
|
2094
|
+ |
192,
|
|
2095
|
+ |
"the session did not carry the reported usage"
|
|
2096
|
+ |
);
|
|
2097
|
+ |
}
|
|
2098
|
+ |
|
|
2099
|
+ |
/// `/diff` end to end: typed into the composer, run by the real actor, and
|
|
2100
|
+ |
/// opened in the inspector. The producer half of the inspector, over the same
|
|
2101
|
+ |
/// channel the session uses.
|
|
2102
|
+ |
#[tokio::test]
|
|
2103
|
+ |
async fn slash_diff_typed_into_the_composer_opens_the_inspector() {
|
|
2104
|
+ |
let dir = tempfile::tempdir().expect("a temporary directory");
|
|
2105
|
+ |
let repo = dir.path();
|
|
2106
|
+ |
let git = |args: &[&str]| {
|
|
2107
|
+ |
std::process::Command::new("git")
|
|
2108
|
+ |
.args(args)
|
|
2109
|
+ |
.current_dir(repo)
|
|
2110
|
+ |
.output()
|
|
2111
|
+ |
.expect("git")
|
|
2112
|
+ |
};
|
|
2113
|
+ |
git(&["init", "-q"]);
|
|
2114
|
+ |
git(&["config", "user.email", "t@example.com"]);
|
|
2115
|
+ |
git(&["config", "user.name", "Test"]);
|
|
2116
|
+ |
std::fs::write(repo.join("thing.txt"), "keep\nold line\ntail\n").unwrap();
|
|
2117
|
+ |
git(&["add", "."]);
|
|
2118
|
+ |
git(&["commit", "-qm", "first"]);
|
|
2119
|
+ |
std::fs::write(repo.join("thing.txt"), "keep\nnew line\ntail\n").unwrap();
|
|
2120
|
+ |
|
|
2121
|
+ |
// The actor runs git in the process's own working directory, so the test
|
|
2122
|
+ |
// runs from the repository it is asking about.
|
|
2123
|
+ |
let previous = std::env::current_dir().expect("a working directory");
|
|
2124
|
+ |
std::env::set_current_dir(repo).expect("move into the repository");
|
|
2125
|
+ |
|
|
2126
|
+ |
let (control_tx, control_rx) = unbounded_channel::<Control>();
|
|
2127
|
+ |
let (turn_tx, mut turn_rx) = unbounded_channel::<TurnEvent>();
|
|
2128
|
+ |
tokio::spawn(runtime_actor(actor_session(), control_rx, turn_tx.clone()));
|
|
2129
|
+ |
|
|
2130
|
+ |
let mut term = terminal_of(90, HEIGHT);
|
|
2131
|
+ |
let mut app = CoderApp::new("openagents coder", &Lane::OxAlpha)
|
|
2132
|
+ |
.with_working_directory(repo.to_path_buf());
|
|
2133
|
+ |
type_str(&mut app, &control_tx, "/diff");
|
|
2134
|
+ |
app.on_key(&key(KeyCode::Enter), 90, &control_tx);
|
|
2135
|
+ |
|
|
2136
|
+ |
pump(
|
|
2137
|
+ |
&mut app,
|
|
2138
|
+ |
&mut turn_rx,
|
|
2139
|
+ |
|app| app.inspecting(),
|
|
2140
|
+ |
Duration::from_secs(20),
|
|
2141
|
+ |
)
|
|
2142
|
+ |
.await;
|
|
2143
|
+ |
std::env::set_current_dir(previous).expect("go back");
|
|
2144
|
+ |
|
|
2145
|
+ |
app.draw(&mut term).unwrap();
|
|
2146
|
+ |
let frame = screen(&term);
|
|
2147
|
+ |
assert!(
|
|
2148
|
+ |
app.inspecting(),
|
|
2149
|
+ |
"`/diff` never opened the inspector:\n{frame}"
|
|
2150
|
+ |
);
|
|
2151
|
+ |
assert!(frame.contains("thing.txt"), "{frame}");
|
|
2152
|
+ |
assert!(frame.contains("\u{2212} old line"), "{frame}");
|
|
2153
|
+ |
assert!(frame.contains("+ new line"), "{frame}");
|
|
2154
|
+ |
}
|