1
use crate::pane::PaneId;
2

            
3
use super::*;
4

            
5
22
fn console() -> ConsoleState {
6
22
    ConsoleState::new()
7
22
}
8

            
9
1020
fn type_line(state: &mut ConsoleState, text: &str) {
10
1020
    for c in text.chars() {
11
5984
        state.input.insert_char(c);
12
    }
13
1020
}
14

            
15
#[test]
16
1
fn incomplete_form_buffers_and_yields_nothing() {
17
1
    let mut state = console();
18
1
    type_line(&mut state, "(list");
19
1
    assert_eq!(state.take_complete_form(), None);
20
1
    assert_eq!(state.pending, "(list");
21
1
    assert!(state.input.buffer().is_empty());
22
1
    assert!(state.history.is_empty());
23
1
}
24

            
25
#[test]
26
1
fn balanced_form_yields_and_clears_buffer() {
27
1
    let mut state = console();
28
1
    type_line(&mut state, "(+ 1 2)");
29
1
    let form = state.take_complete_form();
30
1
    assert_eq!(form.as_deref(), Some("(+ 1 2)"));
31
1
    assert!(state.pending.is_empty());
32
1
    assert!(state.input.buffer().is_empty());
33
1
}
34

            
35
#[test]
36
1
fn multiline_form_assembles_then_completes() {
37
1
    let mut state = console();
38
1
    type_line(&mut state, "(list");
39
1
    assert_eq!(state.take_complete_form(), None);
40
1
    type_line(&mut state, " 1 2)");
41
1
    let form = state.take_complete_form();
42
1
    assert_eq!(form.as_deref(), Some("(list\n 1 2)"));
43
1
    assert!(state.pending.is_empty());
44
1
}
45

            
46
#[test]
47
1
fn submitted_form_is_pushed_to_history() {
48
1
    let mut state = console();
49
1
    type_line(&mut state, "(foo)");
50
1
    let _ = state.take_complete_form();
51
1
    assert_eq!(state.history, vec!["(foo)".to_string()]);
52
1
    assert_eq!(state.history_cursor, None);
53
1
}
54

            
55
#[test]
56
1
fn incomplete_form_not_pushed_to_history() {
57
1
    let mut state = console();
58
1
    type_line(&mut state, "(open");
59
1
    let _ = state.take_complete_form();
60
1
    assert!(state.history.is_empty());
61
1
}
62

            
63
#[test]
64
1
fn history_prev_loads_latest_then_older_entries() {
65
1
    let mut state = console();
66
1
    for form in ["(a)", "(b)", "(c)"] {
67
3
        type_line(&mut state, form);
68
3
        let _ = state.take_complete_form();
69
    }
70
1
    state.history_prev();
71
1
    assert_eq!(state.input.buffer(), "(c)");
72
1
    state.history_prev();
73
1
    assert_eq!(state.input.buffer(), "(b)");
74
1
    state.history_prev();
75
1
    assert_eq!(state.input.buffer(), "(a)");
76
1
    state.history_prev();
77
1
    assert_eq!(state.input.buffer(), "(a)");
78
1
}
79

            
80
#[test]
81
1
fn history_next_walks_forward_then_clears_to_fresh_line() {
82
1
    let mut state = console();
83
1
    for form in ["(a)", "(b)"] {
84
2
        type_line(&mut state, form);
85
2
        let _ = state.take_complete_form();
86
    }
87
1
    state.history_prev();
88
1
    state.history_prev();
89
1
    assert_eq!(state.input.buffer(), "(a)");
90
1
    state.history_next();
91
1
    assert_eq!(state.input.buffer(), "(b)");
92
1
    state.history_next();
93
1
    assert!(state.input.buffer().is_empty());
94
1
    assert_eq!(state.history_cursor, None);
95
1
}
96

            
97
#[test]
98
1
fn history_navigation_is_noop_without_history() {
99
1
    let mut state = console();
100
1
    state.history_prev();
101
1
    assert!(state.input.buffer().is_empty());
102
1
    state.history_next();
103
1
    assert!(state.input.buffer().is_empty());
104
1
    assert_eq!(state.history_cursor, None);
105
1
}
106

            
107
#[test]
108
1
fn blank_enter_yields_nothing_and_records_no_history() {
109
1
    let mut state = console();
110
1
    assert_eq!(state.take_complete_form(), None);
111
1
    assert!(state.history.is_empty());
112
1
    assert!(state.pending.is_empty());
113
1
    type_line(&mut state, "   ");
114
1
    assert_eq!(state.take_complete_form(), None);
115
1
    assert!(state.history.is_empty());
116
1
}
117

            
118
#[test]
119
1
fn duplicate_consecutive_submissions_both_recorded() {
120
1
    let mut state = console();
121
1
    for _ in 0..2 {
122
2
        type_line(&mut state, "(a)");
123
2
        assert_eq!(state.take_complete_form().as_deref(), Some("(a)"));
124
    }
125
1
    assert_eq!(state.history, vec!["(a)".to_string(), "(a)".to_string()]);
126
1
}
127

            
128
#[test]
129
1
fn history_next_at_bottom_is_noop() {
130
1
    let mut state = console();
131
1
    type_line(&mut state, "(a)");
132
1
    let _ = state.take_complete_form();
133
1
    assert_eq!(state.history_cursor, None);
134
1
    state.history_next();
135
1
    assert_eq!(state.history_cursor, None);
136
1
    assert!(state.input.buffer().is_empty());
137
1
}
138

            
139
#[test]
140
1
fn history_caps_at_max_dropping_oldest() {
141
1
    let mut state = console();
142
1
    for i in 0..(MAX_HISTORY_ENTRIES + 5) {
143
1005
        type_line(&mut state, &format!("(f{i})"));
144
1005
        let _ = state.take_complete_form();
145
    }
146
1
    assert_eq!(state.history.len(), MAX_HISTORY_ENTRIES);
147
1
    assert_eq!(state.history.first().unwrap(), "(f5)");
148
1
    assert_eq!(
149
1
        state.history.last().unwrap(),
150
1
        &format!("(f{})", MAX_HISTORY_ENTRIES + 4)
151
    );
152
1
    state.history_prev();
153
1
    assert_eq!(
154
1
        state.input.buffer(),
155
1
        format!("(f{})", MAX_HISTORY_ENTRIES + 4)
156
    );
157
1
}
158

            
159
#[test]
160
1
fn format_result_scalar_number_renders_value() {
161
1
    assert_eq!(format_result("(:id 1 :value 2)"), vec!["2".to_string()]);
162
1
}
163

            
164
#[test]
165
1
fn format_result_scalar_string_renders_value() {
166
1
    assert_eq!(
167
1
        format_result(r#"(:id 1 :value "hello")"#),
168
1
        vec!["hello".to_string()]
169
    );
170
1
}
171

            
172
#[test]
173
1
fn format_result_list_response_aligned_table() {
174
    // A list of two plists: each entity has :name and :symbol fields
175
1
    let envelope = r#"(:id 1 :value ((:name "Alice" :symbol "USD") (:name "Bob" :symbol "EUR")))"#;
176
1
    let lines = format_result(envelope);
177
1
    assert_eq!(lines.len(), 2, "two rows: {lines:?}");
178
1
    assert!(lines[0].contains("Alice"), "{lines:?}");
179
1
    assert!(lines[1].contains("Bob"), "{lines:?}");
180
    // Aligned rows must have equal total length (padding equalises them)
181
1
    assert_eq!(
182
1
        lines[0].len(),
183
1
        lines[1].len(),
184
        "rows not same length: {lines:?}"
185
    );
186
1
}
187

            
188
#[test]
189
1
fn format_result_empty_list_verbatim() {
190
    // An empty list is not tabulated — verbatim fallback.
191
1
    assert_eq!(format_result("(:id 1 :value nil)"), vec!["nil".to_string()]);
192
1
}
193

            
194
#[test]
195
1
fn format_result_non_list_string_verbatim() {
196
    // A string that does not read as a list renders verbatim.
197
1
    let envelope = r#"(:id 1 :value "hello world")"#;
198
1
    assert_eq!(format_result(envelope), vec!["hello world".to_string()]);
199
1
}
200

            
201
#[test]
202
1
fn format_result_pair_string_list_tabulated() {
203
    // get-balances returns pair:string — a list whose elements are strings.
204
    // It must still tabulate (one line per element), not fall back.
205
1
    let envelope = r#"(:id 1 :value ("100 USD" "50 EUR"))"#;
206
1
    let lines = format_result(envelope);
207
1
    assert_eq!(lines.len(), 2, "{lines:?}");
208
1
    assert!(lines.iter().any(|l| l.contains("USD")), "{lines:?}");
209
2
    assert!(lines.iter().any(|l| l.contains("EUR")), "{lines:?}");
210
1
}
211

            
212
#[test]
213
1
fn format_result_string_native_printed_list_tabulated() {
214
    let envelope =
215
1
        r#"(:id 1 :value "((:name \"Alice\" :symbol \"USD\") (:name \"Bob\" :symbol \"EUR\"))")"#;
216
1
    let lines = format_result(envelope);
217
1
    assert_eq!(lines.len(), 2, "{lines:?}");
218
1
    assert!(lines[1].contains("Bob"), "{lines:?}");
219
1
}
220

            
221
#[test]
222
1
fn format_result_record_list_with_leading_whitespace_tabulated() {
223
1
    let envelope = r#"(:id 1 :value "  ((:name \"Alice\" :symbol \"USD\"))")"#;
224
1
    let lines = format_result(envelope);
225
1
    assert_eq!(lines.len(), 1, "{lines:?}");
226
1
    assert!(lines[0].contains("Alice"), "{lines:?}");
227
1
}
228

            
229
#[test]
230
1
fn format_result_error_envelope_renders_error_prefix() {
231
1
    let envelope = r#"(:id 1 :error (:code not-found :message "account missing"))"#;
232
1
    let lines = format_result(envelope);
233
1
    assert_eq!(lines.len(), 1, "{lines:?}");
234
1
    assert_eq!(lines[0], "[error] not-found: account missing");
235
1
}
236

            
237
#[test]
238
1
fn format_result_request_envelope_falls_back_verbatim() {
239
    // Request envelopes are not response frames; parse_wire errors → verbatim
240
1
    let envelope = "(:id 0 :form (foo))";
241
1
    assert_eq!(format_result(envelope), vec![envelope.to_string()]);
242
1
}
243

            
244
#[test]
245
1
fn format_result_plain_text_falls_back_verbatim() {
246
1
    assert_eq!(
247
1
        format_result("line one\nline two"),
248
1
        vec!["line one".to_string(), "line two".to_string()]
249
    );
250
1
}
251

            
252
#[test]
253
1
fn format_result_plain_text_is_verbatim() {
254
1
    let notice = "plain notice";
255
1
    assert_eq!(format_result(notice), vec![notice.to_string()]);
256
1
}
257

            
258
#[test]
259
1
fn align_rows_empty() {
260
1
    assert!(align_rows(&[]).is_empty());
261
1
}
262

            
263
#[test]
264
1
fn align_rows_single_column() {
265
1
    let rows = vec![vec!["a".to_string()], vec!["bbb".to_string()]];
266
1
    let out = align_rows(&rows);
267
1
    assert_eq!(out, vec!["a", "bbb"]);
268
1
}
269

            
270
#[test]
271
1
fn align_rows_multiple_columns_pads_to_max_width() {
272
1
    let rows = vec![
273
1
        vec!["hi".to_string(), "there".to_string()],
274
1
        vec!["goodbye".to_string(), "ok".to_string()],
275
    ];
276
1
    let out = align_rows(&rows);
277
1
    assert_eq!(out[0], "hi       there");
278
1
    assert_eq!(out[1], "goodbye  ok");
279
1
}
280

            
281
#[test]
282
1
fn align_rows_ragged_rows() {
283
1
    let rows = vec![
284
1
        vec!["a".to_string(), "b".to_string(), "c".to_string()],
285
1
        vec!["longer".to_string()],
286
    ];
287
1
    let out = align_rows(&rows);
288
1
    assert_eq!(out.len(), 2);
289
    // Second row has only one cell; still renders without panic
290
1
    assert!(out[1].starts_with("longer"));
291
1
}
292

            
293
#[test]
294
1
fn align_rows_trailing_whitespace_trimmed() {
295
1
    let rows = vec![vec!["x".to_string()]];
296
1
    let out = align_rows(&rows);
297
1
    assert_eq!(out[0], "x");
298
1
}
299

            
300
#[test]
301
1
fn format_result_nil_value_renders_nil() {
302
    // nil scalar value → "nil"
303
1
    let lines = format_result("(:id 1 :value nil)");
304
1
    assert_eq!(lines, vec!["nil".to_string()]);
305
1
}
306

            
307
#[test]
308
1
fn scroll_up_increases_offset_clamped_to_len() {
309
1
    let mut state = console();
310
1
    for i in 0..5 {
311
5
        state.push_scrollback(format!("line {i}"));
312
    }
313
1
    state.scroll_up(3);
314
1
    assert_eq!(state.scroll, 3);
315
    // Clamped: cannot exceed scrollback.len()
316
1
    state.scroll_up(100);
317
1
    assert_eq!(state.scroll, state.scrollback.len());
318
1
}
319

            
320
#[test]
321
1
fn scroll_down_decreases_offset_saturating_at_zero() {
322
1
    let mut state = console();
323
1
    for i in 0..5 {
324
5
        state.push_scrollback(format!("line {i}"));
325
    }
326
1
    state.scroll_up(3);
327
1
    state.scroll_down(1);
328
1
    assert_eq!(state.scroll, 2);
329
1
    state.scroll_down(100);
330
1
    assert_eq!(state.scroll, 0);
331
1
}
332

            
333
#[test]
334
1
fn reset_scroll_returns_to_zero() {
335
1
    let mut state = console();
336
1
    for i in 0..5 {
337
5
        state.push_scrollback(format!("line {i}"));
338
    }
339
1
    state.scroll_up(3);
340
1
    state.reset_scroll();
341
1
    assert_eq!(state.scroll, 0);
342
1
}
343

            
344
#[test]
345
1
fn visible_scrollback_at_zero_shows_tail() {
346
1
    let mut state = console();
347
1
    for i in 0..10 {
348
10
        state.push_scrollback(format!("line {i}"));
349
    }
350
1
    let visible = state.visible_scrollback(3);
351
1
    assert_eq!(visible, &["line 7", "line 8", "line 9"]);
352
1
}
353

            
354
#[test]
355
1
fn visible_scrollback_scrolled_up_shows_older_lines() {
356
1
    let mut state = console();
357
1
    for i in 0..10 {
358
10
        state.push_scrollback(format!("line {i}"));
359
    }
360
1
    state.scroll_up(3);
361
    // end = 10 - 3 = 7, start = 7 - 3 = 4
362
1
    let visible = state.visible_scrollback(3);
363
1
    assert_eq!(visible, &["line 4", "line 5", "line 6"]);
364
    // Newest line is not visible
365
1
    assert!(!visible.contains(&"line 9".to_string()));
366
1
}
367

            
368
#[test]
369
1
fn visible_scrollback_over_scrolled_clamps_to_oldest_window() {
370
1
    let mut state = console();
371
1
    for i in 0..5 {
372
5
        state.push_scrollback(format!("line {i}"));
373
    }
374
1
    state.scroll_up(5);
375
1
    assert_eq!(state.scroll, 5);
376
    // Stored offset (5) exceeds len - height (2); effective offset clamps so
377
    // the oldest 3 lines stay visible rather than emptying the window.
378
1
    let visible = state.visible_scrollback(3);
379
1
    assert_eq!(visible, &state.scrollback[0..3]);
380
1
    assert_eq!(visible, &["line 0", "line 1", "line 2"]);
381
1
    assert!(!visible.is_empty());
382
1
}
383

            
384
#[test]
385
1
fn visible_scrollback_height_larger_than_buffer_shows_all() {
386
1
    let mut state = console();
387
1
    state.push_scrollback("only");
388
1
    let visible = state.visible_scrollback(100);
389
1
    assert_eq!(visible, &["only"]);
390
1
}
391

            
392
#[test]
393
1
fn visible_scrollback_empty_scrollback_is_empty() {
394
1
    let state = console();
395
1
    assert!(state.visible_scrollback(10).is_empty());
396
1
}
397

            
398
#[test]
399
1
fn default_pane_is_prompt() {
400
1
    let state = console();
401
1
    assert_eq!(state.panes.focused(), PaneId::Prompt);
402
1
}
403

            
404
#[test]
405
1
fn push_scrollback_caps_at_max_dropping_oldest() {
406
1
    let mut state = console();
407
1
    for i in 0..(MAX_SCROLLBACK_LINES + 5) {
408
1005
        state.push_scrollback(format!("line {i}"));
409
    }
410
1
    assert_eq!(state.scrollback.len(), MAX_SCROLLBACK_LINES);
411
1
    assert_eq!(state.scrollback.first().unwrap(), "line 5");
412
1
    assert_eq!(
413
1
        state.scrollback.last().unwrap(),
414
1
        &format!("line {}", MAX_SCROLLBACK_LINES + 4)
415
    );
416
1
}
417

            
418
#[test]
419
1
fn console_lines_hint_only_for_the_too_many_streams_code() {
420
    let refused =
421
1
        r#"(:id 1 :stream 9 :error (:code too-many-streams :message "too many streams (max 8)"))"#;
422
1
    assert_eq!(console_lines(refused).len(), 2);
423
1
    let text = r#"(:id 1 :value "[error] too-many-streams: just text")"#;
424
1
    assert_eq!(console_lines(text), format_result(text));
425
1
}