terraphim_agent 1.16.34

Terraphim AI Agent CLI - Command-line interface with interactive REPL and ASCII graph visualization
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
//! REPL Integration Tests for terraphim_agent
//!
//! These tests verify the REPL functionality including:
//! - Command parsing and execution
//! - Handler operations in both server and offline modes
//! - Integration between REPL commands and underlying services
//!
//! Uses ratatui's TestBackend for TUI rendering tests without mocks.

#[cfg(feature = "firecracker")]
use terraphim_agent::repl::commands::VmSubcommand;
use terraphim_agent::repl::commands::{
    ConfigSubcommand, ReplCommand, RobotSubcommand, RoleSubcommand, UpdateSubcommand,
};

/// Test that the REPL command parser correctly parses basic commands
#[test]
fn test_repl_command_parsing() {
    // Test help command - use parse_str which uses FromStr
    let cmd: Result<ReplCommand, _> = "/help".parse();
    assert!(matches!(cmd, Ok(ReplCommand::Help { .. })));

    // Test quit command
    let cmd: Result<ReplCommand, _> = "/quit".parse();
    assert!(matches!(cmd, Ok(ReplCommand::Quit)));

    // Test exit command (alias for quit)
    let cmd: Result<ReplCommand, _> = "/exit".parse();
    assert!(matches!(cmd, Ok(ReplCommand::Exit)));

    // Test search command
    let cmd: Result<ReplCommand, _> = "/search rust programming".parse();
    assert!(matches!(cmd, Ok(ReplCommand::Search { .. })));

    // Test empty command (should error)
    let cmd: Result<ReplCommand, _> = "".parse();
    assert!(cmd.is_err());
}

/// Test that the REPL correctly identifies available commands
#[test]
fn test_repl_available_commands() {
    let commands = ReplCommand::available_commands();
    assert!(!commands.is_empty());

    // Check for essential commands - available_commands returns &[&str]
    let has_search = commands.contains(&"search");
    let has_config = commands.contains(&"config");
    let has_role = commands.contains(&"role");
    let has_help = commands.contains(&"help");
    let has_quit = commands.contains(&"quit");

    assert!(has_search, "should have search command");
    assert!(has_config, "should have config command");
    assert!(has_role, "should have role command");
    assert!(has_help, "should have help command");
    assert!(has_quit, "should have quit command");
}

/// Test REPL command string parsing for search queries
#[test]
fn test_repl_search_command_parsing() {
    let cmd: Result<ReplCommand, _> = "/search test query".parse();
    match cmd {
        Ok(ReplCommand::Search { query, .. }) => {
            assert_eq!(query, "test query");
        }
        _ => panic!("Expected Search command"),
    }
}

/// Test REPL search with options
#[test]
fn test_repl_search_with_options() {
    let cmd: Result<ReplCommand, _> = "/search rust --role engineer --limit 10".parse();
    match cmd {
        Ok(ReplCommand::Search {
            query, role, limit, ..
        }) => {
            assert_eq!(query, "rust");
            assert_eq!(role, Some("engineer".to_string()));
            assert_eq!(limit, Some(10));
        }
        _ => panic!("Expected Search command with options"),
    }
}

/// Test REPL role command parsing
#[test]
fn test_repl_role_command_parsing() {
    // Test role list
    let cmd: Result<ReplCommand, _> = "/role list".parse();
    match cmd {
        Ok(ReplCommand::Role { subcommand }) => {
            assert!(matches!(subcommand, RoleSubcommand::List));
        }
        _ => panic!("Expected Role command with List subcommand"),
    }

    // Test role select
    let cmd: Result<ReplCommand, _> = "/role select terraphim-engineer".parse();
    match cmd {
        Ok(ReplCommand::Role { subcommand }) => match subcommand {
            RoleSubcommand::Select { name } => {
                assert_eq!(name, "terraphim-engineer");
            }
            _ => panic!("Expected Select subcommand"),
        },
        _ => panic!("Expected Role command with Select subcommand"),
    }
}

/// Test REPL config command parsing
#[test]
fn test_repl_config_command_parsing() {
    // Test config show
    let cmd: Result<ReplCommand, _> = "/config show".parse();
    match cmd {
        Ok(ReplCommand::Config { subcommand }) => {
            assert!(matches!(subcommand, ConfigSubcommand::Show));
        }
        _ => panic!("Expected Config command with Show subcommand"),
    }

    // Test config set
    let cmd: Result<ReplCommand, _> = "/config set selected_role terraphim-engineer".parse();
    match cmd {
        Ok(ReplCommand::Config { subcommand }) => match subcommand {
            ConfigSubcommand::Set { key, value } => {
                assert_eq!(key, "selected_role");
                assert_eq!(value, "terraphim-engineer");
            }
            _ => panic!("Expected Set subcommand"),
        },
        _ => panic!("Expected Config command with Set subcommand"),
    }
}

/// Test REPL graph command parsing
#[test]
fn test_repl_graph_command_parsing() {
    let cmd: Result<ReplCommand, _> = "/graph --top-k 20".parse();
    match cmd {
        Ok(ReplCommand::Graph { top_k }) => {
            assert_eq!(top_k, Some(20));
        }
        _ => panic!("Expected Graph command"),
    }
}

/// Test REPL help command with optional command argument
#[test]
fn test_repl_help_command() {
    // Help without argument
    let cmd: Result<ReplCommand, _> = "/help".parse();
    match cmd {
        Ok(ReplCommand::Help { command }) => {
            assert!(command.is_none());
        }
        _ => panic!("Expected Help command"),
    }

    // Help with specific command
    let cmd: Result<ReplCommand, _> = "/help search".parse();
    match cmd {
        Ok(ReplCommand::Help { command }) => {
            assert_eq!(command, Some("search".to_string()));
        }
        _ => panic!("Expected Help command with search argument"),
    }
}

/// Test REPL handler initialization in offline mode
#[tokio::test]
async fn test_repl_handler_offline_mode() {
    use terraphim_agent::repl::handler::ReplHandler;
    use terraphim_agent::service::TuiService;

    // Create TuiService (may fail if config is missing, which is OK for this test)
    match TuiService::new(None).await {
        Ok(service) => {
            let _handler = ReplHandler::new_offline(service);
            // Handler should be created successfully
            // We can't test the actual run() method as it requires TTY
        }
        Err(_) => {
            // Service creation may fail in test environment without config
            // This is acceptable for this integration test
        }
    }
}

/// Test REPL handler initialization in server mode
#[tokio::test]
async fn test_repl_handler_server_mode() {
    use terraphim_agent::client::ApiClient;
    use terraphim_agent::repl::handler::ReplHandler;

    // Create API client for a test server
    let api_client = ApiClient::new("http://localhost:8000".to_string());
    let _handler = ReplHandler::new_server(api_client);

    // Handler should be created successfully
    // Server mode starts with "Terraphim Engineer" role by default
}

/// Test that REPL commands handle edge cases properly
#[test]
fn test_repl_command_edge_cases() {
    // Test command with extra spaces
    let cmd: Result<ReplCommand, _> = "/search   multiple   spaces   ".parse();
    assert!(cmd.is_ok());

    // Test command with special characters in search
    let cmd: Result<ReplCommand, _> = "/search rust&cargo".parse();
    assert!(cmd.is_ok());

    // Test unknown command
    let cmd: Result<ReplCommand, _> = "/unknowncommand".parse();
    assert!(cmd.is_err());

    // Test command without leading slash (should still work)
    let cmd: Result<ReplCommand, _> = "search test".parse();
    assert!(matches!(cmd, Ok(ReplCommand::Search { .. })));
}

/// Test the TUI render functions using ratatui's TestBackend
#[test]
fn test_tui_render_search_view() {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;

    let backend = TestBackend::new(80, 24);
    let mut terminal = Terminal::new(backend).unwrap();

    // Test rendering with empty state
    terminal
        .draw(|f| {
            use ratatui::layout::{Constraint, Direction, Layout};
            use ratatui::text::Line;
            use ratatui::widgets::{Block, Borders, List, ListItem, Paragraph};

            let chunks = Layout::default()
                .direction(Direction::Vertical)
                .constraints([
                    Constraint::Length(3),
                    Constraint::Length(5),
                    Constraint::Min(3),
                    Constraint::Length(3),
                ])
                .split(f.area());

            // Render input section
            let input = Paragraph::new(Line::from("test query"))
                .block(Block::default().title("Search").borders(Borders::ALL));
            f.render_widget(input, chunks[0]);

            // Render results section
            let items: Vec<ListItem> = vec![ListItem::new("Result 1"), ListItem::new("Result 2")];
            let list =
                List::new(items).block(Block::default().title("Results").borders(Borders::ALL));
            f.render_widget(list, chunks[2]);
        })
        .unwrap();

    // Verify the buffer was written to
    let buffer = terminal.backend().buffer();
    assert!(!buffer.content.is_empty());

    // Check that the buffer contains expected content
    let buffer_text: String = buffer.content.iter().map(|c| c.symbol()).collect();
    assert!(buffer_text.contains("Search"));
    assert!(buffer_text.contains("Results"));
}

/// Test TUI render with detail view
#[test]
fn test_tui_render_detail_view() {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;
    use terraphim_types::Document;

    let backend = TestBackend::new(80, 24);
    let mut terminal = Terminal::new(backend).unwrap();

    let doc = Document {
        id: "test-doc-1".to_string(),
        title: "Test Document Title".to_string(),
        url: "https://example.com/test".to_string(),
        body: "This is the body of the test document. It contains content.".to_string(),
        description: None,
        summarization: None,
        stub: None,
        rank: Some(1),
        tags: None,
        source_haystack: None,
        doc_type: terraphim_types::DocumentType::Document,
        synonyms: None,
        route: None,
        priority: None,
    };

    terminal
        .draw(|f| {
            use ratatui::layout::{Constraint, Direction, Layout};
            use ratatui::text::Line;
            use ratatui::widgets::{Block, Borders, Paragraph};

            let chunks = Layout::default()
                .direction(Direction::Vertical)
                .constraints([
                    Constraint::Length(3),
                    Constraint::Min(5),
                    Constraint::Length(3),
                ])
                .split(f.area());

            // Render title
            let title = Paragraph::new(Line::from(doc.title.as_str()))
                .block(Block::default().title("Document").borders(Borders::ALL));
            f.render_widget(title, chunks[0]);

            // Render body
            let body = Paragraph::new(doc.body.as_str())
                .block(Block::default().title("Content").borders(Borders::ALL));
            f.render_widget(body, chunks[1]);
        })
        .unwrap();

    let buffer = terminal.backend().buffer();
    let buffer_text: String = buffer.content.iter().map(|c| c.symbol()).collect();

    assert!(buffer_text.contains("Test Document Title"));
    assert!(buffer_text.contains("This is the body"));
}

/// Test TUI render with suggestions
#[test]
fn test_tui_render_with_suggestions() {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;

    let backend = TestBackend::new(80, 24);
    let mut terminal = Terminal::new(backend).unwrap();

    let suggestions = ["rust".to_string(), "react".to_string(), "ruby".to_string()];

    terminal
        .draw(|f| {
            use ratatui::layout::{Constraint, Direction, Layout};
            use ratatui::widgets::{Block, Borders, List, ListItem};

            let chunks = Layout::default()
                .direction(Direction::Vertical)
                .constraints([Constraint::Length(5), Constraint::Min(3)])
                .split(f.area());

            // Render suggestions
            let items: Vec<ListItem> = suggestions
                .iter()
                .map(|s| ListItem::new(s.as_str()))
                .collect();
            let list =
                List::new(items).block(Block::default().title("Suggestions").borders(Borders::ALL));
            f.render_widget(list, chunks[0]);
        })
        .unwrap();

    let buffer = terminal.backend().buffer();
    let buffer_text: String = buffer.content.iter().map(|c| c.symbol()).collect();

    assert!(buffer_text.contains("Suggestions"));
    assert!(buffer_text.contains("rust"));
    assert!(buffer_text.contains("react"));
    assert!(buffer_text.contains("ruby"));
}

/// Test that terminal size constraints are handled
#[test]
fn test_tui_render_small_terminal() {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;

    // Test with a very small terminal (20x10)
    let backend = TestBackend::new(20, 10);
    let mut terminal = Terminal::new(backend).unwrap();

    terminal
        .draw(|f| {
            use ratatui::layout::{Constraint, Direction, Layout};
            use ratatui::text::Line;
            use ratatui::widgets::{Block, Borders, Paragraph};

            let chunks = Layout::default()
                .direction(Direction::Vertical)
                .constraints([Constraint::Min(1)])
                .split(f.area());

            let widget =
                Paragraph::new(Line::from("Test")).block(Block::default().borders(Borders::ALL));
            f.render_widget(widget, chunks[0]);
        })
        .unwrap();

    let buffer = terminal.backend().buffer();
    assert!(!buffer.content.is_empty());
}

/// Test REPL command error handling for invalid inputs
#[test]
fn test_repl_command_error_handling() {
    // Test various invalid inputs
    let invalid_inputs = vec![
        "/", "/ ", "/search", // search without query
        "/role",   // role without subcommand
        "/config", // config without subcommand
    ];

    for input in invalid_inputs {
        let result: Result<ReplCommand, _> = input.parse();
        assert!(result.is_err(), "'{}' should fail to parse", input);
    }
}

/// Integration test: verify REPL components work together
#[tokio::test]
async fn test_repl_integration_components() {
    // This test verifies that all REPL components can be instantiated
    // and that their interfaces are compatible

    // Test command availability
    let commands = ReplCommand::available_commands();
    assert!(!commands.is_empty(), "REPL should have available commands");

    // Test that command parsing doesn't panic
    let test_inputs = vec![
        "/help",
        "/quit",
        "/search test",
        "/config show",
        "/role list",
        "/graph",
    ];

    for input in test_inputs {
        let _result: Result<ReplCommand, _> = input.parse();
        // Just verify no panic occurs
    }
}

/// Test that render functions handle empty results gracefully
#[test]
fn test_render_empty_results() {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;

    let backend = TestBackend::new(80, 24);
    let mut terminal = Terminal::new(backend).unwrap();

    terminal
        .draw(|f| {
            use ratatui::layout::{Constraint, Direction, Layout};
            use ratatui::widgets::{Block, Borders, List};

            let chunks = Layout::default()
                .direction(Direction::Vertical)
                .constraints([Constraint::Min(3)])
                .split(f.area());

            // Empty results
            let empty_items: Vec<ratatui::widgets::ListItem> = vec![];
            let list = List::new(empty_items)
                .block(Block::default().title("No Results").borders(Borders::ALL));
            f.render_widget(list, chunks[0]);
        })
        .unwrap();

    let buffer = terminal.backend().buffer();
    assert!(!buffer.content.is_empty());
}

/// Test TUI rendering with long content
#[test]
fn test_render_long_content() {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;

    let backend = TestBackend::new(80, 24);
    let mut terminal = Terminal::new(backend).unwrap();

    let long_content = "a".repeat(1000);

    terminal
        .draw(|f| {
            use ratatui::layout::{Constraint, Direction, Layout};
            use ratatui::widgets::{Block, Borders, Paragraph, Wrap};

            let chunks = Layout::default()
                .direction(Direction::Vertical)
                .constraints([Constraint::Min(5)])
                .split(f.area());

            let paragraph = Paragraph::new(long_content.as_str())
                .block(Block::default().borders(Borders::ALL))
                .wrap(Wrap { trim: true });
            f.render_widget(paragraph, chunks[0]);
        })
        .unwrap();

    // Just verify no panic occurs with long content
    let buffer = terminal.backend().buffer();
    assert!(!buffer.content.is_empty());
}

/// Test TUI with selected item highlighting
#[test]
fn test_render_with_selection() {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;
    use ratatui::style::{Modifier, Style};

    let backend = TestBackend::new(80, 24);
    let mut terminal = Terminal::new(backend).unwrap();

    let results = ["Item 1", "Item 2", "Item 3"];
    let selected_index = 1;

    terminal
        .draw(|f| {
            use ratatui::layout::{Constraint, Direction, Layout};
            use ratatui::widgets::{Block, Borders, List, ListItem};

            let chunks = Layout::default()
                .direction(Direction::Vertical)
                .constraints([Constraint::Min(3)])
                .split(f.area());

            let items: Vec<ListItem> = results
                .iter()
                .enumerate()
                .map(|(i, r)| {
                    let item = ListItem::new(*r);
                    if i == selected_index {
                        item.style(Style::default().add_modifier(Modifier::REVERSED))
                    } else {
                        item
                    }
                })
                .collect();

            let list =
                List::new(items).block(Block::default().title("Results").borders(Borders::ALL));
            f.render_widget(list, chunks[0]);
        })
        .unwrap();

    let buffer = terminal.backend().buffer();
    assert!(!buffer.content.is_empty());
}

/// Test TUI with transparent background option
#[test]
fn test_render_transparent_background() {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;
    use ratatui::style::{Color, Style};

    let backend = TestBackend::new(80, 24);
    let mut terminal = Terminal::new(backend).unwrap();

    terminal
        .draw(|f| {
            use ratatui::layout::{Constraint, Direction, Layout};
            use ratatui::text::Line;
            use ratatui::widgets::{Block, Borders, Paragraph};

            let chunks = Layout::default()
                .direction(Direction::Vertical)
                .constraints([Constraint::Min(3)])
                .split(f.area());

            // Render with transparent background
            let transparent_style = Style::default().bg(Color::Reset);
            let widget = Paragraph::new(Line::from("Transparent Test")).block(
                Block::default()
                    .borders(Borders::ALL)
                    .style(transparent_style),
            );
            f.render_widget(widget, chunks[0]);
        })
        .unwrap();

    let buffer = terminal.backend().buffer();
    assert!(!buffer.content.is_empty());
}

/// Test search command with semantic and concepts flags
#[test]
fn test_repl_search_with_flags() {
    let cmd: Result<ReplCommand, _> = "/search rust programming --semantic --concepts".parse();
    match cmd {
        Ok(ReplCommand::Search {
            query,
            semantic,
            concepts,
            ..
        }) => {
            assert_eq!(query, "rust programming");
            assert!(semantic);
            assert!(concepts);
        }
        _ => panic!("Expected Search command with semantic and concepts flags"),
    }
}

/// Test that available_commands returns the correct set based on features
#[test]
fn test_available_commands_by_feature() {
    let commands = ReplCommand::available_commands();

    // Core commands should always be present
    let has_search = commands.contains(&"search");
    let has_config = commands.contains(&"config");
    let has_role = commands.contains(&"role");
    let has_graph = commands.contains(&"graph");
    let has_vm = commands.contains(&"vm");
    let has_robot = commands.contains(&"robot");
    let has_update = commands.contains(&"update");
    let has_help = commands.contains(&"help");
    let has_quit = commands.contains(&"quit");
    let has_exit = commands.contains(&"exit");
    let has_clear = commands.contains(&"clear");

    assert!(has_search);
    assert!(has_config);
    assert!(has_role);
    assert!(has_graph);
    assert!(has_vm);
    assert!(has_robot);
    assert!(has_update);
    assert!(has_help);
    assert!(has_quit);
    assert!(has_exit);
    assert!(has_clear);
}

/// Test command help retrieval
#[test]
fn test_command_help_retrieval() {
    let help = ReplCommand::get_command_help("search");
    assert!(help.is_some());
    assert!(help.unwrap().contains("Search"));

    let help = ReplCommand::get_command_help("quit");
    assert!(help.is_some());

    let help = ReplCommand::get_command_help("nonexistent");
    assert!(help.is_none());
}

/// Test VM command parsing
#[cfg(feature = "firecracker")]
#[test]
fn test_repl_vm_command_parsing() {
    let cmd: Result<ReplCommand, _> = "/vm list".parse();
    match cmd {
        Ok(ReplCommand::Vm { subcommand }) => {
            assert!(matches!(subcommand, VmSubcommand::List));
        }
        _ => panic!("Expected Vm command with List subcommand"),
    }
}

/// Test update command parsing
#[test]
fn test_repl_update_command_parsing() {
    let cmd: Result<ReplCommand, _> = "/update check".parse();
    match cmd {
        Ok(ReplCommand::Update { subcommand }) => {
            assert!(matches!(subcommand, UpdateSubcommand::Check));
        }
        _ => panic!("Expected Update command with Check subcommand"),
    }

    let cmd: Result<ReplCommand, _> = "/update install".parse();
    match cmd {
        Ok(ReplCommand::Update { subcommand }) => {
            assert!(matches!(subcommand, UpdateSubcommand::Install));
        }
        _ => panic!("Expected Update command with Install subcommand"),
    }
}

/// Test robot command parsing
#[test]
fn test_repl_robot_command_parsing() {
    let cmd: Result<ReplCommand, _> = "/robot capabilities".parse();
    match cmd {
        Ok(ReplCommand::Robot { subcommand }) => {
            assert!(matches!(subcommand, RobotSubcommand::Capabilities));
        }
        _ => panic!("Expected Robot command with Capabilities subcommand"),
    }

    let cmd: Result<ReplCommand, _> = "/robot schemas search".parse();
    match cmd {
        Ok(ReplCommand::Robot { subcommand }) => match subcommand {
            RobotSubcommand::Schemas { command } => {
                assert_eq!(command, Some("search".to_string()));
            }
            _ => panic!("Expected Schemas subcommand"),
        },
        _ => panic!("Expected Robot command with Schemas subcommand"),
    }
}

/// Test complex search query with multiple terms and options
#[test]
fn test_repl_complex_search_query() {
    let cmd: Result<ReplCommand, _> =
        "/search rust async programming --role engineer --limit 50 --semantic".parse();
    match cmd {
        Ok(ReplCommand::Search {
            query,
            role,
            limit,
            semantic,
            concepts,
        }) => {
            assert_eq!(query, "rust async programming");
            assert_eq!(role, Some("engineer".to_string()));
            assert_eq!(limit, Some(50));
            assert!(semantic);
            assert!(!concepts);
        }
        _ => panic!("Expected complex Search command"),
    }
}

/// Test REPL command parsing preserves query order
#[test]
fn test_repl_search_query_order() {
    let cmd: Result<ReplCommand, _> = "/search first second third".parse();
    match cmd {
        Ok(ReplCommand::Search { query, .. }) => {
            assert_eq!(query, "first second third");
        }
        _ => panic!("Expected Search command with ordered query"),
    }
}

/// Test that parsing is case-sensitive for commands
#[test]
fn test_repl_command_case_sensitivity() {
    // Commands should be lowercase
    let cmd: Result<ReplCommand, _> = "/SEARCH test".parse();
    assert!(cmd.is_err());

    let cmd: Result<ReplCommand, _> = "/Search test".parse();
    assert!(cmd.is_err());

    // But queries should preserve case
    let cmd: Result<ReplCommand, _> = "/search TestQuery".parse();
    match cmd {
        Ok(ReplCommand::Search { query, .. }) => {
            assert_eq!(query, "TestQuery");
        }
        _ => panic!("Expected Search command with case-preserved query"),
    }
}