periplon 0.2.0

Rust SDK for building multi-agent AI workflows and automation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
//! Comprehensive ratatui backend tests for editor screen rendering
//!
//! Tests editor layout, syntax highlighting, validation panel, and visual elements
//! using the ratatui TestBackend.

#![cfg(feature = "tui")]

use periplon_sdk::tui::state::EditorState;
use periplon_sdk::tui::theme::Theme;
use periplon_sdk::tui::views::editor::{EditorMode, ValidationFeedback};
use ratatui::backend::TestBackend;
use ratatui::Terminal;
use std::path::PathBuf;

// ============================================================================
// Test Utilities
// ============================================================================

/// Create a test terminal with specified dimensions
fn create_test_terminal(width: u16, height: u16) -> Terminal<TestBackend> {
    let backend = TestBackend::new(width, height);
    Terminal::new(backend).unwrap()
}

/// Render editor and return terminal for assertions
fn render_editor(
    state: &EditorState,
    feedback: &ValidationFeedback,
    theme: &Theme,
    width: u16,
    height: u16,
) -> Terminal<TestBackend> {
    let mut terminal = create_test_terminal(width, height);

    terminal
        .draw(|frame| {
            let area = frame.area();
            periplon_sdk::tui::views::editor::render(frame, area, state, feedback, theme);
        })
        .unwrap();

    terminal
}

/// Check if buffer contains text
fn buffer_contains_text(terminal: &Terminal<TestBackend>, text: &str) -> bool {
    let buffer = terminal.backend().buffer();
    let content = buffer.content();

    let visible: String = content
        .iter()
        .map(|cell| cell.symbol())
        .collect::<Vec<_>>()
        .join("");

    visible.contains(text)
}

/// Count visible border characters
fn count_border_chars(terminal: &Terminal<TestBackend>) -> usize {
    let buffer = terminal.backend().buffer();
    buffer
        .content()
        .iter()
        .filter(|cell| {
            let symbol = cell.symbol();
            matches!(
                symbol,
                "" | "" | "" | "" | "" | "" | "" | "" | "" | "" | ""
            )
        })
        .count()
}

// ============================================================================
// Basic Editor Rendering Tests
// ============================================================================

#[test]
fn test_editor_basic_rendering() {
    let mut state = EditorState::new();
    state.content = "name: test\nversion: 1.0.0".to_string();
    state.file_path = Some(PathBuf::from("test.yaml"));

    let feedback = ValidationFeedback::new();
    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should have borders
    assert!(count_border_chars(&terminal) > 0);

    // Should show file name in header
    assert!(buffer_contains_text(&terminal, "test.yaml"));

    // Should show content
    assert!(buffer_contains_text(&terminal, "name"));
}

#[test]
fn test_editor_empty_content() {
    let state = EditorState::new();
    let feedback = ValidationFeedback::new();
    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should render without panicking
    assert!(count_border_chars(&terminal) > 0);

    // Should show new workflow indicator
    assert!(buffer_contains_text(&terminal, "<new workflow>"));
}

#[test]
fn test_editor_with_file_path() {
    let mut state = EditorState::new();
    state.file_path = Some(PathBuf::from("/path/to/my-workflow.yaml"));

    let feedback = ValidationFeedback::new();
    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should show filename
    assert!(buffer_contains_text(&terminal, "my-workflow.yaml"));
}

// ============================================================================
// Header Rendering Tests
// ============================================================================

#[test]
fn test_header_shows_mode() {
    let mut state = EditorState::new();
    state.mode = EditorMode::Text;

    let feedback = ValidationFeedback::new();
    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should show text mode
    assert!(buffer_contains_text(&terminal, "Text"));

    // Test form mode
    state.mode = EditorMode::Form;
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);
    assert!(buffer_contains_text(&terminal, "Form"));
}

#[test]
fn test_header_shows_validation_status_valid() {
    let state = EditorState::new();
    let feedback = ValidationFeedback::new();
    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should show valid checkmark
    assert!(buffer_contains_text(&terminal, ""));
    assert!(buffer_contains_text(&terminal, "Valid"));
}

#[test]
fn test_header_shows_validation_status_invalid() {
    let state = EditorState::new();
    let mut feedback = ValidationFeedback::new();
    feedback
        .errors
        .push((1, "Missing required field".to_string()));
    feedback.errors.push((5, "Invalid syntax".to_string()));

    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should show error count
    assert!(buffer_contains_text(&terminal, ""));
    assert!(buffer_contains_text(&terminal, "2"));
    assert!(buffer_contains_text(&terminal, "errors"));
}

#[test]
fn test_header_shows_modified_indicator() {
    let mut state = EditorState::new();
    state.modified = false;

    let feedback = ValidationFeedback::new();
    let theme = Theme::default();

    // Not modified
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);
    let buffer_without_modified = buffer_contains_text(&terminal, "[Modified]");

    // Modified
    state.modified = true;
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);
    let buffer_with_modified = buffer_contains_text(&terminal, "[Modified]");

    assert!(!buffer_without_modified);
    assert!(buffer_with_modified);
}

// ============================================================================
// Text Editor Mode Rendering Tests
// ============================================================================

#[test]
fn test_text_editor_shows_line_numbers() {
    let mut state = EditorState::new();
    state.mode = EditorMode::Text;
    state.content = "line 1\nline 2\nline 3".to_string();

    let feedback = ValidationFeedback::new();
    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should show line numbers with separator
    assert!(buffer_contains_text(&terminal, ""));
}

#[test]
fn test_text_editor_yaml_syntax_highlighting() {
    let mut state = EditorState::new();
    state.mode = EditorMode::Text;
    state.content = r#"# Comment
name: "Test Workflow"
version: "1.0.0"
agents:
  - test_agent
tasks:
  test_task:
    description: "Task"
"#
    .to_string();

    let feedback = ValidationFeedback::new();
    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should show YAML content
    assert!(buffer_contains_text(&terminal, "name"));
    assert!(buffer_contains_text(&terminal, "version"));
    assert!(buffer_contains_text(&terminal, "agents"));
}

#[test]
fn test_text_editor_shows_yaml_title() {
    let mut state = EditorState::new();
    state.mode = EditorMode::Text;

    let feedback = ValidationFeedback::new();
    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should show YAML Editor title
    assert!(buffer_contains_text(&terminal, "YAML Editor"));
}

#[test]
fn test_text_editor_inline_error_markers() {
    let mut state = EditorState::new();
    state.mode = EditorMode::Text;
    state.content = "line 1\nline 2\nline 3".to_string();

    let mut feedback = ValidationFeedback::new();
    feedback.errors.push((2, "Error on line 2".to_string()));

    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should show error marker
    assert!(buffer_contains_text(&terminal, ""));
}

#[test]
fn test_text_editor_inline_warning_markers() {
    let mut state = EditorState::new();
    state.mode = EditorMode::Text;
    state.content = "line 1\nline 2\nline 3".to_string();

    let mut feedback = ValidationFeedback::new();
    feedback.warnings.push((1, "Warning on line 1".to_string()));

    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should show warning marker
    assert!(buffer_contains_text(&terminal, "⚠️") || buffer_contains_text(&terminal, ""));
}

// ============================================================================
// Form Editor Mode Rendering Tests
// ============================================================================

#[test]
fn test_form_editor_shows_title() {
    let mut state = EditorState::new();
    state.mode = EditorMode::Form;

    let feedback = ValidationFeedback::new();
    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should show Form Editor title
    assert!(buffer_contains_text(&terminal, "Form Editor"));
}

#[test]
fn test_form_editor_with_valid_workflow() {
    let mut state = EditorState::new();
    state.mode = EditorMode::Form;
    state.content = r#"
name: "Test Workflow"
version: "1.0.0"
agents:
  researcher:
    description: "Research agent"
    model: "claude-sonnet-4-5"
tasks:
  task1:
    description: "Test task"
    agent: "researcher"
"#
    .to_string();

    let feedback = ValidationFeedback::new();
    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 30);

    // Should show workflow metadata section
    assert!(buffer_contains_text(&terminal, "Workflow Metadata"));

    // Should show agents section
    assert!(buffer_contains_text(&terminal, "Agents"));

    // Note: The exact agent/task names might not be visible if they're scrolled
    // or if the form rendering uses different formatting. We'll just check sections exist.

    // Should show tasks section (if validation passes and content is long enough)
    // The form might show "Tasks" section header
    let _has_tasks = buffer_contains_text(&terminal, "Tasks");
    // Or it might not be visible in limited terminal height
    // We just verify it doesn't crash - test passes if we reach here
}

#[test]
fn test_form_editor_with_invalid_yaml() {
    let mut state = EditorState::new();
    state.mode = EditorMode::Form;
    state.content = "invalid: yaml: syntax: here".to_string();

    let feedback = ValidationFeedback::new();
    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should show parse error message
    assert!(buffer_contains_text(&terminal, "Unable to parse"));
    assert!(buffer_contains_text(&terminal, "Text mode"));
}

// ============================================================================
// Validation Status Bar Tests
// ============================================================================

#[test]
fn test_validation_status_bar_no_errors() {
    let state = EditorState::new();
    let feedback = ValidationFeedback::new();
    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should show valid status in status bar
    assert!(buffer_contains_text(&terminal, ""));
    assert!(buffer_contains_text(&terminal, "Valid"));
}

#[test]
fn test_validation_status_bar_with_errors() {
    let state = EditorState::new();
    let mut feedback = ValidationFeedback::new();
    feedback.errors.push((1, "Missing name field".to_string()));
    feedback
        .errors
        .push((5, "Invalid agent reference".to_string()));

    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should show error count in status bar
    assert!(buffer_contains_text(&terminal, ""));
    assert!(buffer_contains_text(&terminal, "2"));
    assert!(buffer_contains_text(&terminal, "error"));
}

#[test]
fn test_validation_status_bar_with_warnings() {
    let state = EditorState::new();
    let mut feedback = ValidationFeedback::new();
    feedback
        .warnings
        .push((3, "Consider adding description".to_string()));
    feedback.warnings.push((10, "Unused variable".to_string()));

    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should show warnings count in status bar
    assert!(buffer_contains_text(&terminal, "2"));
    assert!(buffer_contains_text(&terminal, "warning"));
}

#[test]
fn test_validation_status_bar_mixed_errors_and_warnings() {
    let state = EditorState::new();
    let mut feedback = ValidationFeedback::new();
    feedback.errors.push((1, "Error message".to_string()));
    feedback.warnings.push((2, "Warning message".to_string()));

    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should show both error and warning counts
    assert!(buffer_contains_text(&terminal, "error"));
    assert!(buffer_contains_text(&terminal, "warning"));
}

#[test]
fn test_validation_status_bar_keybinding() {
    let state = EditorState::new();
    let feedback = ValidationFeedback::new();
    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should show validation keybinding with abbreviated label
    assert!(buffer_contains_text(&terminal, "v:"));
    assert!(buffer_contains_text(&terminal, "Valid"));
}

#[test]
fn test_validation_modal_not_shown_when_collapsed() {
    let state = EditorState::new();
    let mut feedback = ValidationFeedback::new();
    feedback.errors.push((1, "Error message".to_string()));

    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should not show modal title when not expanded
    let has_details = buffer_contains_text(&terminal, "Validation Details");
    assert!(!has_details); // Modal should not be shown when not expanded
}

#[test]
fn test_validation_modal_shown_when_expanded() {
    let mut state = EditorState::new();
    state.validation_expanded = true;
    let mut feedback = ValidationFeedback::new();
    feedback.errors.push((1, "Missing name field".to_string()));
    feedback
        .errors
        .push((5, "Invalid agent reference".to_string()));

    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 30);

    // Should show modal with detailed errors
    assert!(buffer_contains_text(&terminal, "Validation Details"));
    assert!(buffer_contains_text(&terminal, "Missing name"));
    assert!(buffer_contains_text(&terminal, "Invalid agent"));
}

// ============================================================================
// Status Bar Tests
// ============================================================================

#[test]
fn test_status_bar_text_mode() {
    let mut state = EditorState::new();
    state.mode = EditorMode::Text;

    let feedback = ValidationFeedback::new();
    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should show text mode shortcuts with abbreviated keys
    assert!(buffer_contains_text(&terminal, "^S"));
    assert!(buffer_contains_text(&terminal, "Save"));
    assert!(buffer_contains_text(&terminal, "^R"));
    assert!(buffer_contains_text(&terminal, "Run"));
    assert!(buffer_contains_text(&terminal, "v:"));
    assert!(buffer_contains_text(&terminal, "Valid"));
    assert!(buffer_contains_text(&terminal, "Tab"));
    assert!(buffer_contains_text(&terminal, "Form"));
    assert!(buffer_contains_text(&terminal, "Esc"));
    assert!(buffer_contains_text(&terminal, "Back"));
}

#[test]
fn test_status_bar_form_mode() {
    let mut state = EditorState::new();
    state.mode = EditorMode::Form;

    let feedback = ValidationFeedback::new();
    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should show form mode shortcuts with abbreviated labels
    assert!(buffer_contains_text(&terminal, "Tab"));
    assert!(buffer_contains_text(&terminal, "Text"));
    assert!(buffer_contains_text(&terminal, "^S"));
    assert!(buffer_contains_text(&terminal, "Save"));
    assert!(buffer_contains_text(&terminal, "v:"));
    assert!(buffer_contains_text(&terminal, "Valid"));
    assert!(buffer_contains_text(&terminal, "Esc"));
    assert!(buffer_contains_text(&terminal, "Back"));
}

#[test]
fn test_status_bar_modified_indicator() {
    let mut state = EditorState::new();
    state.modified = true;

    let feedback = ValidationFeedback::new();
    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should show modified indicator in status bar
    assert!(buffer_contains_text(&terminal, "[Modified]"));
}

// ============================================================================
// Layout Tests
// ============================================================================

#[test]
fn test_editor_layout_structure() {
    let state = EditorState::new();
    let feedback = ValidationFeedback::new();
    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 30);

    // Should have all four sections visible
    // Header, Editor content, Validation panel, Status bar
    assert!(count_border_chars(&terminal) > 0);
}

#[test]
fn test_editor_minimum_size() {
    let state = EditorState::new();
    let feedback = ValidationFeedback::new();
    let theme = Theme::default();

    // Test with small terminal
    let terminal = render_editor(&state, &feedback, &theme, 60, 20);

    // Should render without panicking
    assert!(count_border_chars(&terminal) > 0);
}

#[test]
fn test_editor_large_terminal() {
    let state = EditorState::new();
    let feedback = ValidationFeedback::new();
    let theme = Theme::default();

    // Test with large terminal
    let terminal = render_editor(&state, &feedback, &theme, 200, 60);

    // Should render without panicking
    assert!(count_border_chars(&terminal) > 0);
}

// ============================================================================
// Content Rendering Tests
// ============================================================================

#[test]
fn test_editor_multiline_content() {
    let mut state = EditorState::new();
    state.content = "line 1\nline 2\nline 3\nline 4\nline 5".to_string();

    let feedback = ValidationFeedback::new();
    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should show content (line numbers indicate multiline)
    assert!(buffer_contains_text(&terminal, ""));
}

#[test]
fn test_editor_long_lines() {
    let mut state = EditorState::new();
    state.content = "a".repeat(200);

    let feedback = ValidationFeedback::new();
    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should render without panicking
    assert!(count_border_chars(&terminal) > 0);
}

#[test]
fn test_editor_special_characters() {
    let mut state = EditorState::new();
    state.content = "name: 日本語\ntitle: \"Test 🎉\"\nversion: ñ-ü-ö".to_string();

    let feedback = ValidationFeedback::new();
    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should handle Unicode without panicking
    assert!(count_border_chars(&terminal) > 0);
}

#[test]
fn test_editor_empty_lines() {
    let mut state = EditorState::new();
    state.content = "line 1\n\n\nline 4".to_string();

    let feedback = ValidationFeedback::new();
    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should render empty lines
    assert!(count_border_chars(&terminal) > 0);
}

// ============================================================================
// Theme Tests
// ============================================================================

#[test]
fn test_editor_with_different_themes() {
    let state = EditorState::new();
    let feedback = ValidationFeedback::new();

    let themes = vec![
        Theme::default(),
        Theme::light(),
        Theme::monokai(),
        Theme::solarized(),
    ];

    for theme in themes {
        let terminal = render_editor(&state, &feedback, &theme, 80, 24);

        // Should render with all themes
        assert!(count_border_chars(&terminal) > 0);
    }
}

// ============================================================================
// Cursor Position Tests
// ============================================================================

#[test]
fn test_editor_cursor_at_beginning() {
    let mut state = EditorState::new();
    state.content = "test content".to_string();
    state.cursor = (0, 0);

    let feedback = ValidationFeedback::new();
    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should render without panicking
    assert!(count_border_chars(&terminal) > 0);
}

#[test]
fn test_editor_cursor_at_end() {
    let mut state = EditorState::new();
    state.content = "line 1\nline 2".to_string();
    state.cursor = (1, 6); // End of second line

    let feedback = ValidationFeedback::new();
    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should render without panicking
    assert!(count_border_chars(&terminal) > 0);
}

#[test]
fn test_editor_cursor_line_highlighting() {
    let mut state = EditorState::new();
    state.content = "line 1\nline 2\nline 3".to_string();
    state.cursor = (1, 0); // Second line

    let feedback = ValidationFeedback::new();
    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Cursor line should be highlighted (implementation detail - hard to test background color)
    assert!(count_border_chars(&terminal) > 0);
}

// ============================================================================
// Scroll Tests
// ============================================================================

#[test]
fn test_editor_scroll_offset() {
    let mut state = EditorState::new();
    state.content = (0..50)
        .map(|i| format!("line {}", i))
        .collect::<Vec<_>>()
        .join("\n");
    state.scroll = (10, 0); // Scrolled down 10 lines

    let feedback = ValidationFeedback::new();
    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should render scrolled content
    assert!(count_border_chars(&terminal) > 0);
}

#[test]
fn test_editor_scrollbar_with_long_content() {
    let mut state = EditorState::new();
    state.content = (0..100)
        .map(|i| format!("line {}", i))
        .collect::<Vec<_>>()
        .join("\n");

    let feedback = ValidationFeedback::new();
    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should render scrollbar indicators
    assert!(buffer_contains_text(&terminal, "") || buffer_contains_text(&terminal, ""));
}

// ============================================================================
// Validation Feedback Tests
// ============================================================================

#[test]
fn test_validation_feedback_helper_methods() {
    let mut feedback = ValidationFeedback::new();

    assert!(feedback.is_valid());
    assert_eq!(feedback.error_count(), 0);
    assert_eq!(feedback.warning_count(), 0);

    feedback.errors.push((1, "Error".to_string()));
    assert!(!feedback.is_valid());
    assert_eq!(feedback.error_count(), 1);

    feedback.warnings.push((2, "Warning".to_string()));
    assert_eq!(feedback.warning_count(), 1);
}

#[test]
fn test_validation_feedback_timestamp() {
    let mut feedback = ValidationFeedback::new();
    assert!(feedback.validated_at.is_none());

    feedback.validated_at = Some(std::time::Instant::now());
    assert!(feedback.validated_at.is_some());
}

// ============================================================================
// Edge Cases Tests
// ============================================================================

#[test]
fn test_editor_very_long_error_message() {
    let state = EditorState::new();
    let mut feedback = ValidationFeedback::new();
    feedback.errors.push((
        1,
        "This is a very long error message that might exceed the available width and should be handled gracefully by the rendering system".to_string(),
    ));

    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should render without panicking
    assert!(count_border_chars(&terminal) > 0);
}

#[test]
fn test_editor_many_errors() {
    let state = EditorState::new();
    let mut feedback = ValidationFeedback::new();

    // Add many errors
    for i in 1..=50 {
        feedback.errors.push((i, format!("Error {}", i)));
    }

    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should render without panicking and show error count in status bar
    assert!(buffer_contains_text(&terminal, "50"));
    assert!(buffer_contains_text(&terminal, "error"));
}

#[test]
fn test_editor_no_file_path() {
    let state = EditorState::new();
    let feedback = ValidationFeedback::new();
    let theme = Theme::default();
    let terminal = render_editor(&state, &feedback, &theme, 80, 24);

    // Should show default indicator
    assert!(buffer_contains_text(&terminal, "<new workflow>"));
}

#[test]
fn test_editor_mode_enum() {
    // Test EditorMode enum values
    assert_ne!(EditorMode::Text, EditorMode::Form);
}