|
1
|
+ |
//! Frame-level proof that the interactive coder takes keys and shows replies.
|
|
2
|
+ |
//!
|
|
3
|
+ |
//! Every assertion here is against what the terminal would actually show. The
|
|
4
|
+ |
//! frames come from ratatui's `TestBackend`, so a test failing means the
|
|
5
|
+ |
//! reader would not have seen the thing, not that some intermediate value was
|
|
6
|
+ |
//! wrong.
|
|
7
|
+ |
//!
|
|
8
|
+ |
//! The end-to-end tests at the bottom run the real `run_loop` and the real
|
|
9
|
+ |
//! `runtime_actor` over a real HTTP server speaking real server-sent events.
|
|
10
|
+ |
//! Only the model behind that server is a stand-in.
|
|
11
|
+ |
|
|
12
|
+ |
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
|
|
13
|
+ |
use futures::Stream;
|
|
14
|
+ |
use openagents_cli::interactive::{run_loop, runtime_actor, CoderApp, Control, TurnEvent};
|
|
15
|
+ |
use openagents_cli::runtime::{CoderRuntimeSession, Lane};
|
|
16
|
+ |
use openagents_cli::tools::HarnessToolRegistry;
|
|
17
|
+ |
|
|
18
|
+ |
mod support;
|
|
19
|
+ |
use ratatui::backend::TestBackend;
|
|
20
|
+ |
use ratatui::Terminal;
|
|
21
|
+ |
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
|
|
22
|
+ |
|
|
23
|
+ |
const WIDTH: u16 = 74;
|
|
24
|
+ |
const HEIGHT: u16 = 22;
|
|
25
|
+ |
|
|
26
|
+ |
fn terminal() -> Terminal<TestBackend> {
|
|
27
|
+ |
terminal_of(WIDTH, HEIGHT)
|
|
28
|
+ |
}
|
|
29
|
+ |
|
|
30
|
+ |
fn terminal_of(width: u16, height: u16) -> Terminal<TestBackend> {
|
|
31
|
+ |
Terminal::new(TestBackend::new(width, height)).expect("test terminal")
|
|
32
|
+ |
}
|
|
33
|
+ |
|
|
34
|
+ |
/// The frame as the reader would see it, one row per line.
|
|
35
|
+ |
fn screen(terminal: &Terminal<TestBackend>) -> String {
|
|
36
|
+ |
let buffer = terminal.backend().buffer();
|
|
37
|
+ |
(0..buffer.area.height)
|
|
38
|
+ |
.map(|y| {
|
|
39
|
+ |
(0..buffer.area.width)
|
|
40
|
+ |
.map(|x| buffer[(x, y)].symbol())
|
|
41
|
+ |
.collect::<String>()
|
|
42
|
+ |
})
|
|
43
|
+ |
.collect::<Vec<_>>()
|
|
44
|
+ |
.join("\n")
|
|
45
|
+ |
}
|
|
46
|
+ |
|
|
47
|
+ |
fn key(code: KeyCode) -> KeyEvent {
|
|
48
|
+ |
KeyEvent::new(code, KeyModifiers::NONE)
|
|
49
|
+ |
}
|
|
50
|
+ |
|
|
51
|
+ |
fn app() -> (
|
|
52
|
+ |
CoderApp,
|
|
53
|
+ |
UnboundedSender<Control>,
|
|
54
|
+ |
UnboundedReceiver<Control>,
|
|
55
|
+ |
) {
|
|
56
|
+ |
let (tx, rx) = unbounded_channel();
|
|
57
|
+ |
(CoderApp::new("openagents coder"), tx, rx)
|
|
58
|
+ |
}
|
|
59
|
+ |
|
|
60
|
+ |
fn type_str(app: &mut CoderApp, control: &UnboundedSender<Control>, text: &str) {
|
|
61
|
+ |
for ch in text.chars() {
|
|
62
|
+ |
app.on_key(&key(KeyCode::Char(ch)), WIDTH, control);
|
|
63
|
+ |
}
|
|
64
|
+ |
}
|
|
65
|
+ |
|
|
66
|
+ |
// ---------------------------------------------------------------- the input
|
|
67
|
+ |
|
|
68
|
+ |
#[test]
|
|
69
|
+ |
fn what_you_type_appears_in_the_composer() {
|
|
70
|
+ |
let (mut app, control, _rx) = app();
|
|
71
|
+ |
let mut term = terminal();
|
|
72
|
+ |
type_str(&mut app, &control, "list the open issues");
|
|
73
|
+ |
app.draw(&mut term).unwrap();
|
|
74
|
+ |
|
|
75
|
+ |
let frame = screen(&term);
|
|
76
|
+ |
assert!(
|
|
77
|
+ |
frame.contains("› list the open issues"),
|
|
78
|
+ |
"the composer did not show what was typed:\n{frame}"
|
|
79
|
+ |
);
|
|
80
|
+ |
}
|
|
81
|
+ |
|
|
82
|
+ |
#[test]
|
|
83
|
+ |
fn backspace_takes_a_character_back_off_the_screen() {
|
|
84
|
+ |
let (mut app, control, _rx) = app();
|
|
85
|
+ |
let mut term = terminal();
|
|
86
|
+ |
type_str(&mut app, &control, "hello");
|
|
87
|
+ |
app.on_key(&key(KeyCode::Backspace), WIDTH, &control);
|
|
88
|
+ |
app.draw(&mut term).unwrap();
|
|
89
|
+ |
|
|
90
|
+ |
let frame = screen(&term);
|
|
91
|
+ |
assert!(frame.contains("› hell"), "{frame}");
|
|
92
|
+ |
assert!(!frame.contains("› hello"), "{frame}");
|
|
93
|
+ |
}
|
|
94
|
+ |
|
|
95
|
+ |
#[test]
|
|
96
|
+ |
fn the_caret_sits_where_the_next_character_will_go() {
|
|
97
|
+ |
let (mut app, control, _rx) = app();
|
|
98
|
+ |
let mut term = terminal();
|
|
99
|
+ |
type_str(&mut app, &control, "abc");
|
|
100
|
+ |
app.on_key(&key(KeyCode::Left), WIDTH, &control);
|
|
101
|
+ |
app.draw(&mut term).unwrap();
|
|
102
|
+ |
|
|
103
|
+ |
// The composer pane's left border is column 0, its inner text starts at
|
|
104
|
+ |
// column 1, and the prompt `› ` takes two more.
|
|
105
|
+ |
let (x, _y) = term.get_cursor_position().unwrap().into();
|
|
106
|
+ |
assert_eq!(x, 1 + 2 + 2, "caret was not left of the last character");
|
|
107
|
+ |
|
|
108
|
+ |
type_str(&mut app, &control, "X");
|
|
109
|
+ |
app.draw(&mut term).unwrap();
|
|
110
|
+ |
assert!(screen(&term).contains("› abXc"), "{}", screen(&term));
|
|
111
|
+ |
}
|
|
112
|
+ |
|
|
113
|
+ |
#[test]
|
|
114
|
+ |
fn alt_enter_opens_a_second_composer_row_and_enter_still_sends() {
|
|
115
|
+ |
let (mut app, control, mut rx) = app();
|
|
116
|
+ |
let mut term = terminal();
|
|
117
|
+ |
type_str(&mut app, &control, "one");
|
|
118
|
+ |
app.on_key(
|
|
119
|
+ |
&KeyEvent::new(KeyCode::Enter, KeyModifiers::ALT),
|
|
120
|
+ |
WIDTH,
|
|
121
|
+ |
&control,
|
|
122
|
+ |
);
|
|
123
|
+ |
type_str(&mut app, &control, "two");
|
|
124
|
+ |
app.draw(&mut term).unwrap();
|
|
125
|
+ |
|
|
126
|
+ |
let frame = screen(&term);
|
|
127
|
+ |
assert!(frame.contains("› one"), "{frame}");
|
|
128
|
+ |
assert!(frame.contains(" two"), "{frame}");
|
|
129
|
+ |
|
|
130
|
+ |
app.on_key(&key(KeyCode::Enter), WIDTH, &control);
|
|
131
|
+ |
match rx.try_recv() {
|
|
132
|
+ |
Ok(Control::Prompt(prompt)) => assert_eq!(prompt, "one\ntwo"),
|
|
133
|
+ |
other => panic!("Enter did not send both rows as one prompt: {other:?}"),
|
|
134
|
+ |
}
|
|
135
|
+ |
}
|
|
136
|
+ |
|
|
137
|
+ |
// ------------------------------------------------------------ the turn cycle
|
|
138
|
+ |
|
|
139
|
+ |
#[test]
|
|
140
|
+ |
fn submitting_puts_the_prompt_on_the_transcript_and_asks_the_runtime() {
|
|
141
|
+ |
let (mut app, control, mut rx) = app();
|
|
142
|
+ |
let mut term = terminal();
|
|
143
|
+ |
type_str(&mut app, &control, "what changed today");
|
|
144
|
+ |
app.on_key(&key(KeyCode::Enter), WIDTH, &control);
|
|
145
|
+ |
app.draw(&mut term).unwrap();
|
|
146
|
+ |
|
|
147
|
+ |
match rx.try_recv() {
|
|
148
|
+ |
Ok(Control::Prompt(prompt)) => assert_eq!(prompt, "what changed today"),
|
|
149
|
+ |
other => panic!("the runtime was not asked for a turn: {other:?}"),
|
|
150
|
+ |
}
|
|
151
|
+ |
|
|
152
|
+ |
let frame = screen(&term);
|
|
153
|
+ |
assert!(
|
|
154
|
+ |
frame.contains("what changed today"),
|
|
155
|
+ |
"the prompt is not on the transcript:\n{frame}"
|
|
156
|
+ |
);
|
|
157
|
+ |
assert!(
|
|
158
|
+ |
frame.contains("waiting for the reply"),
|
|
159
|
+ |
"the composer does not say it is on hold:\n{frame}"
|
|
160
|
+ |
);
|
|
161
|
+ |
assert!(frame.contains("streaming"), "{frame}");
|
|
162
|
+ |
}
|
|
163
|
+ |
|
|
164
|
+ |
#[test]
|
|
165
|
+ |
fn a_reply_shows_up_while_it_is_still_arriving() {
|
|
166
|
+ |
let (mut app, control, _rx) = app();
|
|
167
|
+ |
let mut term = terminal();
|
|
168
|
+ |
type_str(&mut app, &control, "hi");
|
|
169
|
+ |
app.on_key(&key(KeyCode::Enter), WIDTH, &control);
|
|
170
|
+ |
|
|
171
|
+ |
app.on_turn_event(TurnEvent::Chunk("The first ".to_string()));
|
|
172
|
+ |
app.draw(&mut term).unwrap();
|
|
173
|
+ |
let partial = screen(&term);
|
|
174
|
+ |
assert!(
|
|
175
|
+ |
partial.contains("The first"),
|
|
176
|
+ |
"the first chunk was not drawn:\n{partial}"
|
|
177
|
+ |
);
|
|
178
|
+ |
assert!(
|
|
179
|
+ |
app.busy(),
|
|
180
|
+ |
"the turn was treated as finished by its first chunk"
|
|
181
|
+ |
);
|
|
182
|
+ |
|
|
183
|
+ |
app.on_turn_event(TurnEvent::Chunk("half arrived.".to_string()));
|
|
184
|
+ |
app.draw(&mut term).unwrap();
|
|
185
|
+ |
assert!(screen(&term).contains("The first half arrived."));
|
|
186
|
+ |
|
|
187
|
+ |
app.on_turn_event(TurnEvent::Done(String::new()));
|
|
188
|
+ |
app.draw(&mut term).unwrap();
|
|
189
|
+ |
let done = screen(&term);
|
|
190
|
+ |
assert!(!app.busy(), "the composer stayed on hold after Done");
|
|
191
|
+ |
assert!(done.contains("ready"), "{done}");
|
|
192
|
+ |
assert!(!done.contains("waiting for the reply"), "{done}");
|
|
193
|
+ |
}
|
|
194
|
+ |
|
|
195
|
+ |
#[test]
|
|
196
|
+ |
fn keys_typed_during_a_turn_do_not_reach_the_composer() {
|
|
197
|
+ |
let (mut app, control, _rx) = app();
|
|
198
|
+ |
let mut term = terminal();
|
|
199
|
+ |
type_str(&mut app, &control, "go");
|
|
200
|
+ |
app.on_key(&key(KeyCode::Enter), WIDTH, &control);
|
|
201
|
+ |
type_str(&mut app, &control, "ignored");
|
|
202
|
+ |
app.draw(&mut term).unwrap();
|
|
203
|
+ |
|
|
204
|
+ |
assert!(
|
|
205
|
+ |
!screen(&term).contains("ignored"),
|
|
206
|
+ |
"a key typed mid-turn landed in a composer that says it is on hold"
|
|
207
|
+ |
);
|
|
208
|
+ |
}
|
|
209
|
+ |
|
|
210
|
+ |
#[test]
|
|
211
|
+ |
fn a_reply_with_no_chunks_falls_back_to_the_returned_answer() {
|
|
212
|
+ |
let (mut app, control, _rx) = app();
|
|
213
|
+ |
let mut term = terminal();
|
|
214
|
+ |
type_str(&mut app, &control, "go");
|
|
215
|
+ |
app.on_key(&key(KeyCode::Enter), WIDTH, &control);
|
|
216
|
+ |
app.on_turn_event(TurnEvent::Done("the whole answer at once".to_string()));
|
|
217
|
+ |
app.draw(&mut term).unwrap();
|
|
218
|
+ |
assert!(
|
|
219
|
+ |
screen(&term).contains("the whole answer at once"),
|
|
220
|
+ |
"{}",
|
|
221
|
+ |
screen(&term)
|
|
222
|
+ |
);
|
|
223
|
+ |
}
|
|
224
|
+ |
|
|
225
|
+ |
#[test]
|
|
226
|
+ |
fn a_failed_turn_lands_on_the_transcript_and_the_session_stays_open() {
|
|
227
|
+ |
let (mut app, control, mut rx) = app();
|
|
228
|
+ |
let mut term = terminal();
|
|
229
|
+ |
type_str(&mut app, &control, "go");
|
|
230
|
+ |
app.on_key(&key(KeyCode::Enter), WIDTH, &control);
|
|
231
|
+ |
let _ = rx.try_recv();
|
|
232
|
+ |
|
|
233
|
+ |
app.on_turn_event(TurnEvent::Failed("connection reset".to_string()));
|
|
234
|
+ |
app.draw(&mut term).unwrap();
|
|
235
|
+ |
|
|
236
|
+ |
let frame = screen(&term);
|
|
237
|
+ |
assert!(frame.contains("Turn failed"), "{frame}");
|
|
238
|
+ |
assert!(frame.contains("connection reset"), "{frame}");
|
|
239
|
+ |
assert!(!app.should_exit(), "a failed turn ended the session");
|
|
240
|
+ |
assert!(!app.busy(), "a failed turn left the composer on hold");
|
|
241
|
+ |
|
|
242
|
+ |
// And the next prompt still goes out.
|
|
243
|
+ |
type_str(&mut app, &control, "again");
|
|
244
|
+ |
app.on_key(&key(KeyCode::Enter), WIDTH, &control);
|
|
245
|
+ |
assert!(matches!(rx.try_recv(), Ok(Control::Prompt(p)) if p == "again"));
|
|
246
|
+ |
}
|
|
247
|
+ |
|
|
248
|
+ |
// ------------------------------------------------------------- the keybinds
|
|
249
|
+ |
|
|
250
|
+ |
#[test]
|
|
251
|
+ |
fn every_key_the_status_bar_names_does_something() {
|
|
252
|
+ |
const WIDE: u16 = 120;
|
|
253
|
+ |
let (mut app, control, mut rx) = app();
|
|
254
|
+ |
let mut term = terminal_of(WIDE, HEIGHT);
|
|
255
|
+ |
app.draw(&mut term).unwrap();
|
|
256
|
+ |
let frame = screen(&term);
|
|
257
|
+ |
|
|
258
|
+ |
// Whatever the bar claims, claim it here too, so a new label without a
|
|
259
|
+ |
// key behind it fails this test.
|
|
260
|
+ |
assert!(frame.contains("Enter: send"), "{frame}");
|
|
261
|
+ |
assert!(frame.contains("Alt+Enter: newline"), "{frame}");
|
|
262
|
+ |
assert!(frame.contains("PgUp/PgDn: scroll"), "{frame}");
|
|
263
|
+ |
assert!(frame.contains("Esc: exit"), "{frame}");
|
|
264
|
+ |
|
|
265
|
+ |
// Neither of the keys the old bar advertised is here. `Tab: effort` had
|
|
266
|
+ |
// nothing behind it — `execute_turn` sends no effort field. `Shift+Tab:
|
|
267
|
+ |
// lane` could not be given anything behind it: the thread endpoint
|
|
268
|
+ |
// publishes no model parameter and the grant pins the model.
|
|
269
|
+ |
assert!(!frame.contains("Tab: effort"), "{frame}");
|
|
270
|
+ |
assert!(!frame.contains("Shift+Tab"), "{frame}");
|
|
271
|
+ |
|
|
272
|
+ |
// Enter sends.
|
|
273
|
+ |
type_str(&mut app, &control, "x");
|
|
274
|
+ |
app.on_key(&key(KeyCode::Enter), WIDE, &control);
|
|
275
|
+ |
assert!(matches!(rx.try_recv(), Ok(Control::Prompt(_))));
|
|
276
|
+ |
app.on_turn_event(TurnEvent::Done("ok".to_string()));
|
|
277
|
+ |
|
|
278
|
+ |
// Esc exits.
|
|
279
|
+ |
app.on_key(&key(KeyCode::Esc), WIDE, &control);
|
|
280
|
+ |
assert!(app.should_exit());
|
|
281
|
+ |
}
|
|
282
|
+ |
|
|
283
|
+ |
/// Shift+Tab is not bound, so it does nothing rather than pretending to.
|
|
284
|
+ |
#[test]
|
|
285
|
+ |
fn shift_tab_is_not_bound() {
|
|
286
|
+ |
let (mut app, control, mut rx) = app();
|
|
287
|
+ |
app.on_key(&key(KeyCode::BackTab), WIDTH, &control);
|
|
288
|
+ |
assert!(!app.should_exit());
|
|
289
|
+ |
assert!(app.model().is_none());
|
|
290
|
+ |
assert!(
|
|
291
|
+ |
rx.try_recv().is_err(),
|
|
292
|
+ |
"Shift+Tab sent something to the runtime"
|
|
293
|
+ |
);
|
|
294
|
+ |
}
|
|
295
|
+ |
|
|
296
|
+ |
/// The bar names the model the grant chose, and says so honestly before one.
|
|
297
|
+ |
#[test]
|
|
298
|
+ |
fn the_model_shown_is_the_one_the_grant_named() {
|
|
299
|
+ |
let (mut app, control, _rx) = app();
|
|
300
|
+ |
let mut term = terminal_of(120, HEIGHT);
|
|
301
|
+ |
app.draw(&mut term).unwrap();
|
|
302
|
+ |
assert!(
|
|
303
|
+ |
screen(&term).contains("Model: not yet granted"),
|
|
304
|
+ |
"{}",
|
|
305
|
+ |
screen(&term)
|
|
306
|
+ |
);
|
|
307
|
+ |
|
|
308
|
+ |
type_str(&mut app, &control, "go");
|
|
309
|
+ |
app.on_key(&key(KeyCode::Enter), 120, &control);
|
|
310
|
+ |
app.on_turn_event(TurnEvent::Model("ox-alpha-2".to_string()));
|
|
311
|
+ |
app.on_turn_event(TurnEvent::Done("done".to_string()));
|
|
312
|
+ |
app.draw(&mut term).unwrap();
|
|
313
|
+ |
assert_eq!(app.model(), Some("ox-alpha-2"));
|
|
314
|
+ |
assert!(
|
|
315
|
+ |
screen(&term).contains("Model: ox-alpha-2"),
|
|
316
|
+ |
"{}",
|
|
317
|
+ |
screen(&term)
|
|
318
|
+ |
);
|
|
319
|
+ |
}
|
|
320
|
+ |
|
|
321
|
+ |
/// PgUp reaches material the transcript has scrolled past, and PgDn returns.
|
|
322
|
+ |
#[test]
|
|
323
|
+ |
fn paging_up_shows_what_scrolled_off_the_top() {
|
|
324
|
+ |
let (mut app, control, _rx) = app();
|
|
325
|
+ |
let mut term = terminal();
|
|
326
|
+ |
|
|
327
|
+ |
type_str(&mut app, &control, "the first question");
|
|
328
|
+ |
app.on_key(&key(KeyCode::Enter), WIDTH, &control);
|
|
329
|
+ |
app.on_turn_event(TurnEvent::Chunk(
|
|
330
|
+ |
(1..=30)
|
|
331
|
+ |
.map(|n| format!("line {n}"))
|
|
332
|
+ |
.collect::<Vec<_>>()
|
|
333
|
+ |
.join("\n"),
|
|
334
|
+ |
));
|
|
335
|
+ |
app.on_turn_event(TurnEvent::Done(String::new()));
|
|
336
|
+ |
app.draw(&mut term).unwrap();
|
|
337
|
+ |
|
|
338
|
+ |
// The newest rows are what the reader sees, so the prompt is off the top.
|
|
339
|
+ |
let bottom = screen(&term);
|
|
340
|
+ |
assert!(bottom.contains("line 30"), "{bottom}");
|
|
341
|
+ |
assert!(!bottom.contains("the first question"), "{bottom}");
|
|
342
|
+ |
|
|
343
|
+ |
for _ in 0..8 {
|
|
344
|
+ |
app.on_key(&key(KeyCode::PageUp), WIDTH, &control);
|
|
345
|
+ |
}
|
|
346
|
+ |
app.draw(&mut term).unwrap();
|
|
347
|
+ |
let scrolled = screen(&term);
|
|
348
|
+ |
assert!(
|
|
349
|
+ |
scrolled.contains("the first question"),
|
|
350
|
+ |
"PgUp did not reach the prompt:\n{scrolled}"
|
|
351
|
+ |
);
|
|
352
|
+ |
assert!(!scrolled.contains("line 30"), "{scrolled}");
|
|
353
|
+ |
|
|
354
|
+ |
for _ in 0..8 {
|
|
355
|
+ |
app.on_key(&key(KeyCode::PageDown), WIDTH, &control);
|
|
356
|
+ |
}
|
|
357
|
+ |
app.draw(&mut term).unwrap();
|
|
358
|
+ |
assert!(
|
|
359
|
+ |
screen(&term).contains("line 30"),
|
|
360
|
+ |
"PgDn did not come back to the bottom:\n{}",
|
|
361
|
+ |
screen(&term)
|
|
362
|
+ |
);
|
|
363
|
+ |
}
|
|
364
|
+ |
|
|
365
|
+ |
/// The status bar's own row, which is the second from the bottom.
|
|
366
|
+ |
fn status_row(terminal: &Terminal<TestBackend>) -> String {
|
|
367
|
+ |
let frame = screen(terminal);
|
|
368
|
+ |
let rows: Vec<&str> = frame.lines().collect();
|
|
369
|
+ |
rows[rows.len() - 2].to_string()
|
|
370
|
+ |
}
|
|
371
|
+ |
|
|
372
|
+ |
#[test]
|
|
373
|
+ |
fn a_narrow_window_drops_hints_rather_than_showing_half_of_one() {
|
|
374
|
+ |
let (app, _control, _rx) = app();
|
|
375
|
+ |
|
|
376
|
+ |
// Wide enough for two whole hints and no more.
|
|
377
|
+ |
let mut term = terminal_of(74, HEIGHT);
|
|
378
|
+ |
app.draw(&mut term).unwrap();
|
|
379
|
+ |
let row = status_row(&term);
|
|
380
|
+ |
assert!(row.contains("Enter: send \u{b7} Esc: exit"), "{row}");
|
|
381
|
+ |
assert!(!row.contains("PgU"), "a hint was cut in half: {row}");
|
|
382
|
+ |
|
|
383
|
+ |
// Too narrow for even the first: the status and the lane stay, the hints go.
|
|
384
|
+ |
let mut term = terminal_of(46, HEIGHT);
|
|
385
|
+ |
app.draw(&mut term).unwrap();
|
|
386
|
+ |
let row = status_row(&term);
|
|
387
|
+ |
assert!(row.contains("Status: ready"), "{row}");
|
|
388
|
+ |
assert!(row.contains("Model: not yet granted"), "{row}");
|
|
389
|
+ |
assert!(!row.contains("Ent"), "a hint was cut in half: {row}");
|
|
390
|
+ |
for line in screen(&term).lines() {
|
|
391
|
+ |
assert_eq!(
|
|
392
|
+ |
line.chars().count(),
|
|
393
|
+ |
46,
|
|
394
|
+ |
"a row is not exactly the window's width:\n{}",
|
|
395
|
+ |
screen(&term)
|
|
396
|
+ |
);
|
|
397
|
+ |
}
|
|
398
|
+ |
}
|
|
399
|
+ |
|
|
400
|
+ |
#[test]
|
|
401
|
+ |
fn ctrl_c_exits() {
|
|
402
|
+ |
let (mut app, control, _rx) = app();
|
|
403
|
+ |
app.on_key(
|
|
404
|
+ |
&KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL),
|
|
405
|
+ |
WIDTH,
|
|
406
|
+ |
&control,
|
|
407
|
+ |
);
|
|
408
|
+ |
assert!(app.should_exit());
|
|
409
|
+ |
}
|
|
410
|
+ |
|
|
411
|
+ |
#[test]
|
|
412
|
+ |
fn the_welcome_text_promises_only_what_the_screen_does() {
|
|
413
|
+ |
let mut term = terminal();
|
|
414
|
+ |
let (app, _control, _rx) = app();
|
|
415
|
+ |
app.draw(&mut term).unwrap();
|
|
416
|
+ |
let frame = screen(&term);
|
|
417
|
+ |
assert!(frame.contains("Type a prompt"), "{frame}");
|
|
418
|
+ |
// The frame keeps the boxed panes and their titles set into the rule.
|
|
419
|
+ |
assert!(frame.contains("Agent Context"), "{frame}");
|
|
420
|
+ |
assert!(frame.contains("Transcript"), "{frame}");
|
|
421
|
+ |
assert!(frame.contains("Message"), "{frame}");
|
|
422
|
+ |
}
|
|
423
|
+ |
|
|
424
|
+ |
// ------------------------------------------------------------- end-to-end
|
|
425
|
+ |
|
|
426
|
+ |
/// A stream of terminal events the test writes by hand.
|
|
427
|
+ |
fn scripted(rx: UnboundedReceiver<Event>) -> impl Stream<Item = std::io::Result<Event>> + Unpin {
|
|
428
|
+ |
Box::pin(futures::stream::unfold(rx, |mut rx| async move {
|
|
429
|
+ |
rx.recv().await.map(|event| (Ok(event), rx))
|
|
430
|
+ |
}))
|
|
431
|
+ |
}
|
|
432
|
+ |
|
|
433
|
+ |
async fn drive(
|
|
434
|
+ |
app: &mut CoderApp,
|
|
435
|
+ |
term: &mut Terminal<TestBackend>,
|
|
436
|
+ |
keys: UnboundedReceiver<Event>,
|
|
437
|
+ |
control: UnboundedSender<Control>,
|
|
438
|
+ |
turns: &mut UnboundedReceiver<TurnEvent>,
|
|
439
|
+ |
keepalive: UnboundedSender<TurnEvent>,
|
|
440
|
+ |
) {
|
|
441
|
+ |
let mut events = scripted(keys);
|
|
442
|
+ |
run_loop(term, app, &mut events, control, turns, keepalive)
|
|
443
|
+ |
.await
|
|
444
|
+ |
.expect("the loop returned an error");
|
|
445
|
+ |
}
|
|
446
|
+ |
|
|
447
|
+ |
fn send_keys(tx: &UnboundedSender<Event>, text: &str) {
|
|
448
|
+ |
for ch in text.chars() {
|
|
449
|
+ |
let _ = tx.send(Event::Key(key(KeyCode::Char(ch))));
|
|
450
|
+ |
}
|
|
451
|
+ |
}
|
|
452
|
+ |
|
|
453
|
+ |
/// The whole loop, with a stub runtime on the other end of the real channels.
|
|
454
|
+ |
#[tokio::test]
|
|
455
|
+ |
async fn end_to_end_over_the_loop_with_a_stub_runtime() {
|
|
456
|
+ |
let mut term = terminal();
|
|
457
|
+ |
let mut app = CoderApp::new("openagents coder");
|
|
458
|
+ |
let (keys_tx, keys_rx) = unbounded_channel();
|
|
459
|
+ |
let (control_tx, mut control_rx) = unbounded_channel::<Control>();
|
|
460
|
+ |
let (turn_tx, mut turn_rx) = unbounded_channel::<TurnEvent>();
|
|
461
|
+ |
|
|
462
|
+ |
// The stand-in for `runtime_actor`: same channels, same message types.
|
|
463
|
+ |
let stub_sink = turn_tx.clone();
|
|
464
|
+ |
let keys_for_stub = keys_tx.clone();
|
|
465
|
+ |
tokio::spawn(async move {
|
|
466
|
+ |
while let Some(Control::Prompt(prompt)) = control_rx.recv().await {
|
|
467
|
+ |
assert_eq!(prompt, "who are you");
|
|
468
|
+ |
for chunk in ["I am ", "openagents ", "coder."] {
|
|
469
|
+ |
let _ = stub_sink.send(TurnEvent::Chunk(chunk.to_string()));
|
|
470
|
+ |
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
|
|
471
|
+ |
}
|
|
472
|
+ |
let _ = stub_sink.send(TurnEvent::Done(String::new()));
|
|
473
|
+ |
tokio::time::sleep(std::time::Duration::from_millis(120)).await;
|
|
474
|
+ |
let _ = keys_for_stub.send(Event::Key(key(KeyCode::Esc)));
|
|
475
|
+ |
}
|
|
476
|
+ |
});
|
|
477
|
+ |
|
|
478
|
+ |
send_keys(&keys_tx, "who are you");
|
|
479
|
+ |
let _ = keys_tx.send(Event::Key(key(KeyCode::Enter)));
|
|
480
|
+ |
|
|
481
|
+ |
drive(
|
|
482
|
+ |
&mut app,
|
|
483
|
+ |
&mut term,
|
|
484
|
+ |
keys_rx,
|
|
485
|
+ |
control_tx,
|
|
486
|
+ |
&mut turn_rx,
|
|
487
|
+ |
turn_tx,
|
|
488
|
+ |
)
|
|
489
|
+ |
.await;
|
|
490
|
+ |
|
|
491
|
+ |
let frame = screen(&term);
|
|
492
|
+ |
assert!(
|
|
493
|
+ |
frame.contains("who are you"),
|
|
494
|
+ |
"the typed prompt is not on the final frame:\n{frame}"
|
|
495
|
+ |
);
|
|
496
|
+ |
assert!(
|
|
497
|
+ |
frame.contains("I am openagents coder."),
|
|
498
|
+ |
"the streamed reply is not on the final frame:\n{frame}"
|
|
499
|
+ |
);
|
|
500
|
+ |
assert!(app.should_exit(), "the loop did not exit on Esc");
|
|
501
|
+ |
}
|
|
502
|
+ |
|
|
503
|
+ |
// -------------------------------------------- end-to-end over real HTTP/SSE
|
|
504
|
+ |
|
|
505
|
+ |
/// The real loop, the real `runtime_actor`, and the real `CoderRuntimeSession`
|
|
506
|
+ |
/// against a real socket speaking real server-sent events.
|
|
507
|
+ |
///
|
|
508
|
+ |
/// The reader types a prompt, presses Enter, and the reply appears. The turn is
|
|
509
|
+ |
/// interrupted deliberately after its first chunk to prove the transcript is
|
|
510
|
+ |
/// showing text while the turn is still open — not assembling it at the end.
|
|
511
|
+ |
#[tokio::test]
|
|
512
|
+ |
async fn end_to_end_over_real_http_shows_a_chunk_before_the_turn_finishes() {
|
|
513
|
+ |
let (gate_tx, gate_rx) = tokio::sync::oneshot::channel();
|
|
514
|
+ |
let stub = support::start(vec!["Reading the ", "repository now."], Some(gate_rx)).await;
|
|
515
|
+ |
|
|
516
|
+ |
let session = CoderRuntimeSession::new(
|
|
517
|
+ |
Lane::OxAlpha,
|
|
518
|
+ |
Some(stub.base),
|
|
519
|
+ |
Some("oat_test".to_string()),
|
|
520
|
+ |
HarnessToolRegistry::new(Some(std::env::temp_dir())),
|
|
521
|
+ |
);
|
|
522
|
+ |
|
|
523
|
+ |
let (control_tx, control_rx) = unbounded_channel::<Control>();
|
|
524
|
+ |
let (turn_tx, mut turn_rx) = unbounded_channel::<TurnEvent>();
|
|
525
|
+ |
tokio::spawn(runtime_actor(session, control_rx, turn_tx.clone()));
|
|
526
|
+ |
|
|
527
|
+ |
let mut term = terminal();
|
|
528
|
+ |
let mut app = CoderApp::new("openagents coder");
|
|
529
|
+ |
let (keys_tx, keys_rx) = unbounded_channel();
|
|
530
|
+ |
|
|
531
|
+ |
send_keys(&keys_tx, "read the repo");
|
|
532
|
+ |
let _ = keys_tx.send(Event::Key(key(KeyCode::Enter)));
|
|
533
|
+ |
|
|
534
|
+ |
// Once the first chunk is on the transcript, exit — with the second chunk
|
|
535
|
+ |
// still held behind the gate on the server.
|
|
536
|
+ |
let keys_for_exit = keys_tx.clone();
|
|
537
|
+ |
tokio::spawn(async move {
|
|
538
|
+ |
tokio::time::sleep(std::time::Duration::from_millis(1200)).await;
|
|
539
|
+ |
let _ = keys_for_exit.send(Event::Key(key(KeyCode::Esc)));
|
|
540
|
+ |
});
|
|
541
|
+ |
|
|
542
|
+ |
drive(
|
|
543
|
+ |
&mut app,
|
|
544
|
+ |
&mut term,
|
|
545
|
+ |
keys_rx,
|
|
546
|
+ |
control_tx,
|
|
547
|
+ |
&mut turn_rx,
|
|
548
|
+ |
turn_tx,
|
|
549
|
+ |
)
|
|
550
|
+ |
.await;
|
|
551
|
+ |
let _ = gate_tx.send(());
|
|
552
|
+ |
|
|
553
|
+ |
let frame = screen(&term);
|
|
554
|
+ |
assert!(
|
|
555
|
+ |
frame.contains("read the repo"),
|
|
556
|
+ |
"the typed prompt is not on the frame:\n{frame}"
|
|
557
|
+ |
);
|
|
558
|
+ |
assert!(
|
|
559
|
+ |
frame.contains("Reading the"),
|
|
560
|
+ |
"the first streamed chunk never reached the transcript:\n{frame}"
|
|
561
|
+ |
);
|
|
562
|
+ |
assert!(
|
|
563
|
+ |
!frame.contains("repository now."),
|
|
564
|
+ |
"the held-back chunk arrived, so this run proves nothing about streaming:\n{frame}"
|
|
565
|
+ |
);
|
|
566
|
+ |
assert!(
|
|
567
|
+ |
frame.contains("waiting for the reply"),
|
|
568
|
+ |
"the turn was not still open when the frame was taken:\n{frame}"
|
|
569
|
+ |
);
|
|
570
|
+ |
}
|
|
571
|
+ |
|
|
572
|
+ |
/// The same stack, allowed to finish, so the whole reply lands.
|
|
573
|
+ |
#[tokio::test]
|
|
574
|
+ |
async fn end_to_end_over_real_http_streams_a_whole_reply_onto_the_transcript() {
|
|
575
|
+ |
let stub = support::start(vec!["Two files ", "changed today."], None).await;
|
|
576
|
+ |
|
|
577
|
+ |
let session = CoderRuntimeSession::new(
|
|
578
|
+ |
Lane::OxAlpha,
|
|
579
|
+ |
Some(stub.base),
|
|
580
|
+ |
Some("oat_test".to_string()),
|
|
581
|
+ |
HarnessToolRegistry::new(Some(std::env::temp_dir())),
|
|
582
|
+ |
);
|
|
583
|
+ |
|
|
584
|
+ |
let (control_tx, control_rx) = unbounded_channel::<Control>();
|
|
585
|
+ |
let (turn_tx, mut turn_rx) = unbounded_channel::<TurnEvent>();
|
|
586
|
+ |
tokio::spawn(runtime_actor(session, control_rx, turn_tx.clone()));
|
|
587
|
+ |
|
|
588
|
+ |
let mut term = terminal();
|
|
589
|
+ |
let mut app = CoderApp::new("openagents coder");
|
|
590
|
+ |
let (keys_tx, keys_rx) = unbounded_channel();
|
|
591
|
+ |
|
|
592
|
+ |
send_keys(&keys_tx, "what changed");
|
|
593
|
+ |
let _ = keys_tx.send(Event::Key(key(KeyCode::Enter)));
|
|
594
|
+ |
|
|
595
|
+ |
let keys_for_exit = keys_tx.clone();
|
|
596
|
+ |
tokio::spawn(async move {
|
|
597
|
+ |
tokio::time::sleep(std::time::Duration::from_millis(2000)).await;
|
|
598
|
+ |
let _ = keys_for_exit.send(Event::Key(key(KeyCode::Esc)));
|
|
599
|
+ |
});
|
|
600
|
+ |
|
|
601
|
+ |
drive(
|
|
602
|
+ |
&mut app,
|
|
603
|
+ |
&mut term,
|
|
604
|
+ |
keys_rx,
|
|
605
|
+ |
control_tx,
|
|
606
|
+ |
&mut turn_rx,
|
|
607
|
+ |
turn_tx,
|
|
608
|
+ |
)
|
|
609
|
+ |
.await;
|
|
610
|
+ |
|
|
611
|
+ |
let frame = screen(&term);
|
|
612
|
+ |
assert!(frame.contains("what changed"), "{frame}");
|
|
613
|
+ |
assert!(
|
|
614
|
+ |
frame.contains("Two files changed today."),
|
|
615
|
+ |
"the reply did not arrive whole:\n{frame}"
|
|
616
|
+ |
);
|
|
617
|
+ |
assert!(
|
|
618
|
+ |
frame.contains("ready"),
|
|
619
|
+ |
"the composer never came off hold:\n{frame}"
|
|
620
|
+ |
);
|
|
621
|
+ |
}
|
|
622
|
+ |
|
|
623
|
+ |
/// A refused request reaches the reader as a failure, not as a finished turn.
|
|
624
|
+ |
///
|
|
625
|
+ |
/// Before this change `create_thread` answered a 401 by inventing a grant with
|
|
626
|
+ |
/// a placeholder token, and `execute_turn` answered the proxy's rejection by
|
|
627
|
+ |
/// streaming `Completed autonomous reasoning turn (offline fallback).` and
|
|
628
|
+ |
/// returning success. A session with no token therefore looked exactly like a
|
|
629
|
+ |
/// session that had worked.
|
|
630
|
+ |
#[tokio::test]
|
|
631
|
+ |
async fn a_refused_turn_says_so_on_the_transcript() {
|
|
632
|
+ |
let stub = support::start_refusing().await;
|
|
633
|
+ |
|
|
634
|
+ |
let session = CoderRuntimeSession::new(
|
|
635
|
+ |
Lane::OxAlpha,
|
|
636
|
+ |
Some(stub.base),
|
|
637
|
+ |
None,
|
|
638
|
+ |
HarnessToolRegistry::new(Some(std::env::temp_dir())),
|
|
639
|
+ |
);
|
|
640
|
+ |
|
|
641
|
+ |
let (control_tx, control_rx) = unbounded_channel::<Control>();
|
|
642
|
+ |
let (turn_tx, mut turn_rx) = unbounded_channel::<TurnEvent>();
|
|
643
|
+ |
tokio::spawn(runtime_actor(session, control_rx, turn_tx.clone()));
|
|
644
|
+ |
|
|
645
|
+ |
let mut term = terminal_of(100, HEIGHT);
|
|
646
|
+ |
let mut app = CoderApp::new("openagents coder");
|
|
647
|
+ |
let (keys_tx, keys_rx) = unbounded_channel();
|
|
648
|
+ |
send_keys(&keys_tx, "hello");
|
|
649
|
+ |
let _ = keys_tx.send(Event::Key(key(KeyCode::Enter)));
|
|
650
|
+ |
|
|
651
|
+ |
let keys_for_exit = keys_tx.clone();
|
|
652
|
+ |
tokio::spawn(async move {
|
|
653
|
+ |
tokio::time::sleep(std::time::Duration::from_millis(1500)).await;
|
|
654
|
+ |
let _ = keys_for_exit.send(Event::Key(key(KeyCode::Esc)));
|
|
655
|
+ |
});
|
|
656
|
+ |
|
|
657
|
+ |
drive(
|
|
658
|
+ |
&mut app,
|
|
659
|
+ |
&mut term,
|
|
660
|
+ |
keys_rx,
|
|
661
|
+ |
control_tx,
|
|
662
|
+ |
&mut turn_rx,
|
|
663
|
+ |
turn_tx,
|
|
664
|
+ |
)
|
|
665
|
+ |
.await;
|
|
666
|
+ |
|
|
667
|
+ |
let frame = screen(&term);
|
|
668
|
+ |
assert!(frame.contains("Turn failed"), "{frame}");
|
|
669
|
+ |
assert!(frame.contains("401"), "{frame}");
|
|
670
|
+ |
assert!(
|
|
671
|
+ |
!frame.contains("offline fallback"),
|
|
672
|
+ |
"a refused turn still reads as a completed one:\n{frame}"
|
|
673
|
+ |
);
|
|
674
|
+ |
assert!(
|
|
675
|
+ |
frame.contains("ready"),
|
|
676
|
+ |
"the composer stayed on hold:\n{frame}"
|
|
677
|
+ |
);
|
|
678
|
+ |
}
|