model-rs 0.1.1

A Rust CLI tool for downloading HuggingFace models and running local LLM inference
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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
//! End-to-end (E2E) tests for model-rs
//!
//! These tests cover the complete workflow of the model-rs CLI tool,
//! including CLI commands, model operations, and API server functionality.

use std::io::Read;
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::sync::atomic::{AtomicU16, Ordering};
use std::time::Duration;
use tokio::time::{sleep, timeout};

/// Suggested HF id when no local weights are available (hint text only).
const TEST_MODEL: &str = "TinyLlama/TinyLlama-1.1B-Chat-v1.0";
const TEST_PORT: u16 = 3020; // Use different port to avoid conflicts

/// Path to the `model-rs` binary.
///
/// Prefer the runtime `CARGO_BIN_EXE_*` value Cargo sets when running integration tests (avoids a
/// stale absolute path baked in via `option_env!` after moving/renaming the repo). Fall back to
/// `target/{debug|release}/model-rs` (honours `CARGO_TARGET_DIR` when set).
fn model_rs_bin() -> PathBuf {
    if let Ok(p) = std::env::var("CARGO_BIN_EXE_model_rs") {
        return PathBuf::from(p);
    }
    let target_root = std::env::var("CARGO_TARGET_DIR")
        .map(PathBuf::from)
        .unwrap_or_else(|_| Path::new(env!("CARGO_MANIFEST_DIR")).join("target"));
    let profile = if cfg!(debug_assertions) {
        "debug"
    } else {
        "release"
    };
    let exe = if cfg!(target_os = "windows") {
        "model-rs.exe"
    } else {
        "model-rs"
    };
    target_root.join(profile).join(exe)
}

/// Helper function to run model-rs CLI commands
fn run_model_rs(args: &[&str]) -> std::io::Result<std::process::Output> {
    let binary_path = model_rs_bin();

    if !binary_path.exists() {
        panic!("model-rs binary not found at {}.", binary_path.display());
    }

    Command::new(&binary_path)
        .args(args)
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
}

/// Run `model-rs` with extra environment variables (isolates tests from the parent process).
fn run_model_rs_with_env(
    args: &[&str],
    envs: &[(&str, &str)],
) -> std::io::Result<std::process::Output> {
    let binary_path = model_rs_bin();
    if !binary_path.exists() {
        panic!("model-rs binary not found at {}.", binary_path.display());
    }
    let mut cmd = Command::new(&binary_path);
    for (key, val) in envs {
        cmd.env(key, val);
    }
    cmd.args(args)
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
}

/// Unique port per test that spawns `serve`, so parallel `cargo test` does not collide.
fn alloc_e2e_listen_port() -> u16 {
    static NEXT: AtomicU16 = AtomicU16::new(4_100);
    NEXT.fetch_add(1, Ordering::SeqCst)
}

fn first_model_path_from_list_output(output: &str) -> Option<PathBuf> {
    for raw_line in output.lines() {
        let line = raw_line.trim_start();
        let Some(rest) = line.strip_prefix("- **Path:** `") else {
            continue;
        };
        let Some(end) = rest.find('`') else {
            continue;
        };
        let p = PathBuf::from(&rest[..end]);
        if p.is_dir() {
            return Some(p);
        }
    }
    None
}

/// Local model directory for tests that need a running server.
///
/// Order: `MODEL_RS_E2E_MODEL_PATH` (must exist), else first path from `model-rs list`.
fn resolve_e2e_model_path() -> Option<PathBuf> {
    if let Ok(p) = std::env::var("MODEL_RS_E2E_MODEL_PATH") {
        let pb = PathBuf::from(p);
        if pb.is_dir() {
            return Some(pb);
        }
    }
    let list_output = run_model_rs(&["list"]).ok()?;
    if !list_output.status.success() {
        return None;
    }
    let stdout = String::from_utf8_lossy(&list_output.stdout);
    first_model_path_from_list_output(&stdout)
}

async fn wait_for_health_port(port: u16, max_wait: Duration) -> bool {
    let client = reqwest::Client::new();
    let url = format!("http://127.0.0.1:{port}/health");
    let deadline = tokio::time::Instant::now() + max_wait;
    while tokio::time::Instant::now() < deadline {
        if let Ok(resp) = timeout(Duration::from_secs(2), client.get(&url).send()).await {
            if let Ok(resp) = resp {
                if resp.status().is_success() {
                    return true;
                }
            }
        }
        sleep(Duration::from_millis(200)).await;
    }
    false
}

fn spawn_serve_child(model_path: &Path, port: u16) -> std::io::Result<Child> {
    Command::new(model_rs_bin())
        .args([
            "serve",
            "--model-path",
            &model_path.display().to_string(),
            "--port",
            &port.to_string(),
            "--device",
            "cpu",
        ])
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
}

/// Test 1: Verify CLI binary exists and is executable
#[test]
fn test_cli_binary_exists() {
    let path = model_rs_bin();
    assert!(path.exists(), "model-rs binary should exist");
    
    // Check if file is executable (Unix)
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let metadata = path.metadata().expect("Should read file metadata");
        let permissions = metadata.permissions();
        let mode = permissions.mode();
        assert!(mode & 0o111 != 0, "Binary should be executable");
    }
}

/// Test 2: CLI version command
#[test]
fn test_cli_version() {
    let output = run_model_rs(&["--version"])
        .expect("Should execute version command");

    assert!(output.status.success(), "Version command should succeed");
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("model-rs"),
        "Version output should contain 'model-rs'"
    );
}

/// Test 3: CLI help command
#[test]
fn test_cli_help() {
    let output = run_model_rs(&["--help"])
        .expect("Should execute help command");

    assert!(output.status.success(), "Help command should succeed");
    let stdout = String::from_utf8_lossy(&output.stdout);
    
    // Verify main commands are documented
    assert!(
        stdout.contains("download"),
        "Help should document download command"
    );
    assert!(
        stdout.contains("generate"),
        "Help should document generate command"
    );
    assert!(
        stdout.contains("serve"),
        "Help should document serve command"
    );
    assert!(
        stdout.contains("list"),
        "Help should document list command"
    );
}

/// Test 4: List models command (empty or with models)
#[test]
fn test_list_models() {
    let output = run_model_rs(&["list"])
        .expect("Should execute list command");

    assert!(output.status.success(), "List command should succeed");
    let stdout = String::from_utf8_lossy(&output.stdout);
    
    // Should either show "No models found" or list models
    let is_empty = stdout.contains("No models found") || stdout.contains("**To download a model:**");
    let has_content = stdout.contains("#") || stdout.contains("├─") || stdout.contains("|");
    
    assert!(
        is_empty || has_content,
        "List output should either be empty or show model information"
    );
}

/// Test 5: Config command
#[test]
fn test_config_command() {
    let output = run_model_rs(&["config"])
        .expect("Should execute config command");

    assert!(output.status.success(), "Config command should succeed");
    let stdout = String::from_utf8_lossy(&output.stdout);
    
    // Verify config sections are shown
    assert!(
        stdout.contains("Configuration Settings") || stdout.contains("Model Settings"),
        "Config should show settings sections"
    );
}

/// Test 6: Cache command (stats)
#[test]
fn test_cache_stats() {
    let output = run_model_rs(&["cache", "--stats"])
        .expect("Should execute cache stats command");

    assert!(output.status.success(), "Cache stats command should succeed");
    let stdout = String::from_utf8_lossy(&output.stdout);
    
    // Should show cache status
    assert!(
        stdout.contains("Cache Status") || stdout.contains("Status:"),
        "Cache stats should show status"
    );
}

/// Test 7: Search command (basic connectivity test)
#[test]
fn test_search_command() {
    let output = run_model_rs(&["search", "llama", "--limit", "5"])
        .expect("Should execute search command");

    // Search might fail due to network, so we just verify it doesn't crash
    let stderr = String::from_utf8_lossy(&output.stderr);
    let stdout = String::from_utf8_lossy(&output.stdout);
    
    // Either succeeds or shows a network error
    let has_output = output.status.success() || 
                    stderr.contains("error") || 
                    stderr.contains("failed") ||
                    stdout.contains("No results") ||
                    stdout.contains("Search results");
    
    assert!(has_output, "Search should produce output");
}

/// Test 8: Invalid model path should fail gracefully
#[test]
fn test_generate_invalid_model() {
    let output = run_model_rs(&[
        "generate",
        "Hello",
        "--model-path",
        "/nonexistent/model/path"
    ]);

    let output = output.unwrap();
    assert!(!output.status.success(), "Should fail with invalid model path");
    let stderr = String::from_utf8_lossy(&output.stderr);
    
    assert!(
        stderr.contains("error") || stderr.contains("not found") || stderr.contains("Failed"),
        "Should show error message"
    );
}

/// Test 9: Verify model download command format
#[test]
fn test_download_command_format() {
    let output = run_model_rs(&["download", "--help"])
        .expect("Should execute download help");

    assert!(output.status.success(), "Download help should succeed");
    let stdout = String::from_utf8_lossy(&output.stdout);
    
    assert!(
        stdout.contains("--model") || stdout.contains("-m"),
        "Download help should show model option"
    );
    assert!(
        stdout.contains("--mirror") || stdout.contains("-r"),
        "Download help should show mirror option"
    );
}

/// Test 10: Generate command help
#[test]
fn test_generate_command_help() {
    let output = run_model_rs(&["generate", "--help"])
        .expect("Should execute generate help");

    assert!(output.status.success(), "Generate help should succeed");
    let stdout = String::from_utf8_lossy(&output.stdout);
    
    assert!(
        stdout.contains("--temperature"),
        "Generate help should show temperature option"
    );
    assert!(
        stdout.contains("--max-tokens"),
        "Generate help should show max-tokens option"
    );
    assert!(
        stdout.contains("--top-p") || stdout.contains("--top-k"),
        "Generate help should show sampling options"
    );
}

/// Test 11: Serve command help
#[test]
fn test_serve_command_help() {
    let output = run_model_rs(&["serve", "--help"])
        .expect("Should execute serve help");

    assert!(output.status.success(), "Serve help should succeed");
    let stdout = String::from_utf8_lossy(&output.stdout);
    
    assert!(
        stdout.contains("--port"),
        "Serve help should show port option"
    );
    assert!(
        stdout.contains("--device"),
        "Serve help should show device option"
    );
}

/// Test 12: Chat command help
#[test]
fn test_chat_command_help() {
    let output = run_model_rs(&["chat", "--help"])
        .expect("Should execute chat help");

    assert!(output.status.success(), "Chat help should succeed");
    let stdout = String::from_utf8_lossy(&output.stdout);
    
    assert!(
        stdout.contains("--system"),
        "Chat help should show system option"
    );
    assert!(
        stdout.contains("--session"),
        "Chat help should show session option"
    );
}

/// Test 13: Show command help
#[test]
fn test_show_command_help() {
    let output = run_model_rs(&["show", "--help"])
        .expect("Should execute show help");

    assert!(output.status.success(), "Show help should succeed");
    let stdout = String::from_utf8_lossy(&output.stdout);
    
    assert!(
        stdout.contains("<MODEL>"),
        "Show help should show model argument"
    );
}

/// Test 14: Remove command help
#[test]
fn test_remove_command_help() {
    let output = run_model_rs(&["remove", "--help"])
        .expect("Should execute remove help");

    assert!(output.status.success(), "Remove help should succeed");
    let stdout = String::from_utf8_lossy(&output.stdout);
    
    assert!(
        stdout.contains("--force") || stdout.contains("-f"),
        "Remove help should show force option"
    );
}

/// Test 15: PS command (list running processes)
#[test]
fn test_ps_command() {
    let output = run_model_rs(&["ps"])
        .expect("Should execute ps command");

    assert!(output.status.success(), "PS command should succeed");
    // PS should work even with no processes
}

// === API Server Tests ===

/// Helper function to check if server is running
async fn is_server_running(port: u16) -> bool {
    let client = reqwest::Client::new();
    let url = format!("http://localhost:{}/health", port);
    
    let result = timeout(Duration::from_secs(2), client.get(&url).send()).await;
    matches!(result, Ok(Ok(resp)) if resp.status().is_success())
}

/// Test 16: Health check endpoint
#[tokio::test]
async fn test_health_endpoint() {
    if !is_server_running(TEST_PORT).await {
        eprintln!("Server not running on port {}. Skipping API tests.", TEST_PORT);
        eprintln!(
            "Start with: {} serve --port {} --model-path <model-path>",
            model_rs_bin().display(),
            TEST_PORT
        );
        return;
    }

    let client = reqwest::Client::new();
    let url = format!("http://localhost:{}/health", TEST_PORT);
    
    let response = client
        .get(&url)
        .send()
        .await
        .expect("Health request should complete");

    assert_eq!(response.status(), 200, "Health endpoint should return 200");
    
    let body: serde_json::Value = response
        .json()
        .await
        .expect("Should parse JSON response");
    
    assert!(
        body.get("status").is_some(),
        "Health response should contain status"
    );
}

/// Test 17: V1 generate endpoint (non-streaming)
#[tokio::test]
async fn test_v1_generate_endpoint() {
    if !is_server_running(TEST_PORT).await {
        eprintln!("Server not running. Skipping test_v1_generate_endpoint");
        return;
    }

    let client = reqwest::Client::new();
    let url = format!("http://localhost:{}/v1/generate", TEST_PORT);
    
    let response = client
        .post(&url)
        .json(&serde_json::json!({
            "prompt": "What is 2+2?",
            "max_tokens": 20,
            "temperature": 0.7
        }))
        .send()
        .await;

    match response {
        Ok(resp) => {
            assert_eq!(resp.status(), 200, "Generate should return 200");
            
            let body = resp.json::<serde_json::Value>().await
                .expect("Should parse response JSON");
            
            assert!(
                body.get("text").is_some(),
                "Response should contain 'text' field"
            );
            
            let text = body["text"].as_str().expect("Text should be a string");
            assert!(!text.is_empty(), "Generated text should not be empty");
        }
        Err(e) if e.is_connect() => {
            eprintln!("Connection failed. Server might not be running.");
        }
        Err(e) => {
            panic!("Request failed: {}", e);
        }
    }
}

/// Test 18: V1 generate with streaming
#[tokio::test]
async fn test_v1_generate_stream() {
    if !is_server_running(TEST_PORT).await {
        eprintln!("Server not running. Skipping test_v1_generate_stream");
        return;
    }

    let client = reqwest::Client::new();
    let url = format!(
        "http://localhost:{}/v1/generate_stream?prompt=Hello&max_tokens=10",
        TEST_PORT
    );
    
    let response = client
        .get(&url)
        .send()
        .await;

    match response {
        Ok(resp) => {
            assert_eq!(resp.status(), 200, "Stream should return 200");
            
            let content_type = resp.headers()
                .get("content-type")
                .expect("Should have content-type header");
            
            assert!(
                content_type.to_str().unwrap().contains("text/event-stream"),
                "Should return SSE content type"
            );
            
            let mut stream = resp.bytes_stream();
            let mut received_data = false;
            
            let result = timeout(Duration::from_secs(10), async {
                use futures_util::StreamExt;
                
                while let Some(chunk) = stream.next().await {
                    if let Ok(bytes) = chunk {
                        let text = String::from_utf8_lossy(&bytes);
                        if text.contains("data:") {
                            received_data = true;
                            break;
                        }
                    }
                }
            }).await;

            assert!(result.is_ok(), "Stream should complete within timeout");
            assert!(received_data, "Should receive SSE data");
        }
        Err(e) if e.is_connect() => {
            eprintln!("Connection failed. Server might not be running.");
        }
        Err(e) => {
            panic!("Request failed: {}", e);
        }
    }
}

/// Test 19: Ollama-compatible generate endpoint
#[tokio::test]
async fn test_ollama_generate_endpoint() {
    if !is_server_running(TEST_PORT).await {
        eprintln!("Server not running. Skipping test_ollama_generate_endpoint");
        return;
    }

    let client = reqwest::Client::new();
    let url = format!("http://localhost:{}/api/generate", TEST_PORT);
    
    let response = client
        .post(&url)
        .json(&serde_json::json!({
            "prompt": "Hi",
            "stream": false,
            "options": {
                "num_predict": 10
            }
        }))
        .send()
        .await;

    match response {
        Ok(resp) => {
            assert_eq!(resp.status(), 200, "Ollama generate should return 200");
            
            let body = resp.json::<serde_json::Value>().await
                .expect("Should parse response JSON");
            
            assert!(
                body.get("response").is_some(),
                "Response should contain 'response' field"
            );
            assert!(
                body.get("done").is_some(),
                "Response should contain 'done' field"
            );
        }
        Err(e) if e.is_connect() => {
            eprintln!("Connection failed. Server might not be running.");
        }
        Err(e) => {
            panic!("Request failed: {}", e);
        }
    }
}

/// Test 20: Ollama tags endpoint
#[tokio::test]
async fn test_ollama_tags_endpoint() {
    if !is_server_running(TEST_PORT).await {
        eprintln!("Server not running. Skipping test_ollama_tags_endpoint");
        return;
    }

    let client = reqwest::Client::new();
    let url = format!("http://localhost:{}/api/tags", TEST_PORT);
    
    let response = client
        .get(&url)
        .send()
        .await;

    match response {
        Ok(resp) => {
            assert_eq!(resp.status(), 200, "Tags should return 200");
            
            let body = resp.json::<serde_json::Value>().await
                .expect("Should parse response JSON");
            
            assert!(
                body.get("models").is_some(),
                "Response should contain 'models' field"
            );
        }
        Err(e) if e.is_connect() => {
            eprintln!("Connection failed. Server might not be running.");
        }
        Err(e) => {
            panic!("Request failed: {}", e);
        }
    }
}

/// Test 21: Invalid request should return 400
#[tokio::test]
async fn test_invalid_generate_request() {
    if !is_server_running(TEST_PORT).await {
        eprintln!("Server not running. Skipping test_invalid_generate_request");
        return;
    }

    let client = reqwest::Client::new();
    let url = format!("http://localhost:{}/v1/generate", TEST_PORT);
    
    let response = client
        .post(&url)
        .json(&serde_json::json!({
            "max_tokens": 50  // Missing required 'prompt' field
        }))
        .send()
        .await;

    match response {
        Ok(resp) => {
            let status = resp.status();
            assert!(
                status == 400 || status == 422,
                "Invalid request should return 400 or 422, got {}",
                status
            );
        }
        Err(e) if e.is_connect() => {
            eprintln!("Connection failed. Server might not be running.");
        }
        Err(e) => {
            panic!("Request failed: {}", e);
        }
    }
}

/// Test 22: Generate with custom parameters
#[tokio::test]
async fn test_generate_with_custom_params() {
    if !is_server_running(TEST_PORT).await {
        eprintln!("Server not running. Skipping test_generate_with_custom_params");
        return;
    }

    let client = reqwest::Client::new();
    let url = format!("http://localhost:{}/v1/generate", TEST_PORT);
    
    let response = client
        .post(&url)
        .json(&serde_json::json!({
            "prompt": "Count to 3:",
            "max_tokens": 20,
            "temperature": 0.5,
            "top_p": 0.9,
            "top_k": 40
        }))
        .send()
        .await;

    match response {
        Ok(resp) => {
            assert_eq!(resp.status(), 200, "Generate with params should return 200");
            
            let body = resp.json::<serde_json::Value>().await
                .expect("Should parse response JSON");
            
            assert!(
                body.get("text").is_some(),
                "Response should contain 'text' field"
            );
        }
        Err(e) if e.is_connect() => {
            eprintln!("Connection failed. Server might not be running.");
        }
        Err(e) => {
            panic!("Request failed: {}", e);
        }
    }
}

/// Test 23: Embeddings endpoint
#[tokio::test]
async fn test_embeddings_endpoint() {
    if !is_server_running(TEST_PORT).await {
        eprintln!("Server not running. Skipping test_embeddings_endpoint");
        return;
    }

    let client = reqwest::Client::new();
    let url = format!("http://localhost:{}/v1/embeddings", TEST_PORT);
    
    let response = client
        .post(&url)
        .json(&serde_json::json!({
            "input": "Hello world"
        }))
        .send()
        .await;

    match response {
        Ok(resp) => {
            // Might return 200 or 501 (not implemented for this model)
            let status = resp.status();
            assert!(
                status == 200 || status == 501,
                "Embeddings should return 200 or 501, got {}",
                status
            );
            
            if status == 200 {
                let body = resp.json::<serde_json::Value>().await
                    .expect("Should parse response JSON");
                
                assert!(
                    body.get("data").is_some(),
                    "Response should contain 'data' field"
                );
            }
        }
        Err(e) if e.is_connect() => {
            eprintln!("Connection failed. Server might not be running.");
        }
        Err(e) => {
            panic!("Request failed: {}", e);
        }
    }
}

// === Integration Test ===

/// Test 24: Full workflow test (requires a local model directory)
///
/// Uses `MODEL_RS_E2E_MODEL_PATH` or the first model from `model-rs list`. If neither is
/// available, the test returns early so `cargo test` stays green on clean checkouts.
#[tokio::test]
async fn test_full_workflow() {
    let Some(model_path) = resolve_e2e_model_path() else {
        eprintln!(
            "No local model for test_full_workflow: set MODEL_RS_E2E_MODEL_PATH or run `model-rs download {TEST_MODEL}`."
        );
        return;
    };

    let port = alloc_e2e_listen_port();
    let mut server_cmd = spawn_serve_child(&model_path, port).expect("Should start server");

    if !wait_for_health_port(port, Duration::from_secs(120)).await {
        let mut err = String::new();
        if let Some(stderr) = server_cmd.stderr.as_mut() {
            let _ = Read::read_to_string(stderr, &mut err);
        }
        let _ = server_cmd.kill();
        let _ = server_cmd.wait();
        panic!(
            "Server did not become healthy on port {port}. stderr (if any): {err}"
        );
    }

    let client = reqwest::Client::new();
    let url = format!("http://127.0.0.1:{port}/v1/generate");
    let response = client
        .post(&url)
        .json(&serde_json::json!({
            "prompt": "Hello",
            "max_tokens": 10
        }))
        .send()
        .await
        .expect("request should complete");

    assert_eq!(
        response.status(),
        200,
        "Full workflow: generate should succeed"
    );

    let body = response
        .json::<serde_json::Value>()
        .await
        .expect("Should parse response");
    assert!(body.get("text").is_some(), "Should have text response");

    let _ = server_cmd.kill();
    let _ = server_cmd.wait();
}

/// Test 25: Server startup and health (no curl; portable)
#[tokio::test]
async fn test_server_lifecycle() {
    let Some(model_path) = resolve_e2e_model_path() else {
        eprintln!(
            "No local model for test_server_lifecycle: set MODEL_RS_E2E_MODEL_PATH or run `model-rs download {TEST_MODEL}`."
        );
        return;
    };

    let port = alloc_e2e_listen_port();
    let mut server = spawn_serve_child(&model_path, port).expect("Should start server");

    assert!(
        wait_for_health_port(port, Duration::from_secs(120)).await,
        "server health check failed on port {port}"
    );

    let _ = server.kill();
    let _ = server.wait();
}

/// Test 26: Config command with environment variables (child process env only)
#[test]
fn test_config_with_env() {
    let output = run_model_rs_with_env(
        &["config"],
        &[
            ("MODEL_RS_TEMPERATURE", "0.5"),
            ("MODEL_RS_MAX_TOKENS", "100"),
        ],
    )
    .expect("Should execute config command");

    assert!(output.status.success(), "Config should succeed");

    let stdout = String::from_utf8_lossy(&output.stdout);
    // `model-rs config` renders via termimad/crossterm; captured stdout includes ANSI SGR codes,
    // so match the Generation Parameters slice instead of a literal `- Temperature: 0.5` line.
    let generation = stdout
        .split("Generation Parameters")
        .nth(1)
        .and_then(|s| s.split("Device Settings").next())
        .unwrap_or("");
    assert!(
        generation.contains("Temperature") && generation.contains("0.5"),
        "config output should reflect MODEL_RS_TEMPERATURE=0.5 in Generation Parameters, got: {stdout}"
    );
    assert!(
        generation.contains("Max Tokens") && generation.contains("100"),
        "config output should reflect MODEL_RS_MAX_TOKENS=100 in Generation Parameters, got: {stdout}"
    );
}

/// Test 27: Cache clear command
#[test]
fn test_cache_clear() {
    let output = run_model_rs(&["cache", "--clear"]).expect("Should execute cache clear");

    assert!(output.status.success(), "Cache clear should succeed");

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("Cache cleared") || stdout.contains("cleared"),
        "Should confirm cache was cleared"
    );
}

/// Test 28: Verify error handling for non-existent model operations
#[test]
fn test_nonexistent_model_operations() {
    let fake_model = "NonExistent/Model-123";
    
    // Test show command
    let output = run_model_rs(&["show", fake_model]);
    assert!(!output.unwrap().status.success(), "Show should fail for non-existent model");
    
    // Test remove command (should fail or prompt)
    let output = run_model_rs(&["remove", fake_model]);
    assert!(!output.unwrap().status.success(), "Remove should fail for non-existent model");
    
    // Test verify command
    let output = run_model_rs(&["verify", fake_model]);
    assert!(!output.unwrap().status.success(), "Verify should fail for non-existent model");
    
    // Test info command
    let output = run_model_rs(&["info", fake_model]);
    assert!(!output.unwrap().status.success(), "Info should fail for non-existent model");
}

/// Test 29: Concurrent generation requests
#[tokio::test]
async fn test_concurrent_requests() {
    let Some(model_path) = resolve_e2e_model_path() else {
        eprintln!(
            "No local model for test_concurrent_requests: set MODEL_RS_E2E_MODEL_PATH or run `model-rs download {TEST_MODEL}`."
        );
        return;
    };

    let port = alloc_e2e_listen_port();
    let mut server = spawn_serve_child(&model_path, port).expect("Should start server");
    if !wait_for_health_port(port, Duration::from_secs(120)).await {
        let _ = server.kill();
        let _ = server.wait();
        panic!("Server did not become healthy on port {port}");
    }

    let client = reqwest::Client::new();
    let url = format!("http://127.0.0.1:{port}/v1/generate");

    let mut handles = vec![];
    for i in 0..5 {
        let client = client.clone();
        let url = url.clone();
        let handle = tokio::spawn(async move {
            client
                .post(&url)
                .json(&serde_json::json!({
                    "prompt": format!("Test {}", i),
                    "max_tokens": 5
                }))
                .send()
                .await
        });
        handles.push(handle);
    }

    let mut successful = 0;
    for handle in handles {
        match handle.await {
            Ok(Ok(resp)) if resp.status() == 200 => successful += 1,
            Ok(Ok(resp)) => eprintln!("Request failed with status: {}", resp.status()),
            Ok(Err(e)) => eprintln!("Request error: {}", e),
            Err(e) => eprintln!("Task error: {}", e),
        }
    }

    let _ = server.kill();
    let _ = server.wait();

    assert!(
        successful > 0,
        "At least some concurrent requests should succeed"
    );
}

/// Test 30: Longer generation request against a spawned server
#[tokio::test]
async fn test_long_generation() {
    let Some(model_path) = resolve_e2e_model_path() else {
        eprintln!(
            "No local model for test_long_generation: set MODEL_RS_E2E_MODEL_PATH or run `model-rs download {TEST_MODEL}`."
        );
        return;
    };

    let port = alloc_e2e_listen_port();
    let mut server = spawn_serve_child(&model_path, port).expect("Should start server");
    if !wait_for_health_port(port, Duration::from_secs(120)).await {
        let _ = server.kill();
        let _ = server.wait();
        panic!("Server did not become healthy on port {port}");
    }

    let client = reqwest::Client::new();
    let url = format!("http://127.0.0.1:{port}/v1/generate");

    let response = client
        .post(&url)
        .json(&serde_json::json!({
            "prompt": "Write a short story about a robot:",
            "max_tokens": 80,
            "temperature": 0.8
        }))
        .send()
        .await
        .expect("request should complete");

    assert_eq!(response.status(), 200, "Long generation should succeed");

    let body = response
        .json::<serde_json::Value>()
        .await
        .expect("Should parse response");
    let text = body["text"].as_str().expect("Should have text");
    assert!(
        text.len() > 10,
        "Should generate reasonable amount of text"
    );

    let _ = server.kill();
    let _ = server.wait();
}

/// Test 31: Verify CLI output format consistency
#[test]
fn test_output_format_consistency() {
    let commands = vec![
        vec!["--help"],
        vec!["list"],
        vec!["config"],
        vec!["cache", "--stats"],
    ];

    for args in commands {
        let output = run_model_rs(&args)
            .expect("Should execute command");
        
        assert!(
            output.status.success(),
            "Command {:?} should succeed",
            args
        );
        
        // Verify output is valid UTF-8
        let _ = String::from_utf8(output.stdout)
            .expect("Output should be valid UTF-8");
    }
}

/// Test 32: Test all command help outputs
#[test]
fn test_all_command_helps() {
    let commands = vec![
        "download",
        "generate",
        "run",
        "stop",
        "chat",
        "serve",
        "list",
        "search",
        "embed",
        "show",
        "remove",
        "ps",
        "info",
        "verify",
        "cache",
        "copy",
        "config",
        "deploy",
    ];

    for cmd in commands {
        let output = run_model_rs(&[cmd, "--help"])
            .expect(&format!("Should get help for {}", cmd));
        
        assert!(
            output.status.success(),
            "Help for {} should succeed",
            cmd
        );
        
        let stdout = String::from_utf8_lossy(&output.stdout);
        assert!(
            !stdout.is_empty(),
            "Help for {} should have output",
            cmd
        );
        
        println!("✓ Help for '{}': OK", cmd);
    }
}

/// Test 33: Verify error messages are user-friendly
#[test]
fn test_error_messages() {
    // Test various error conditions
    let test_cases = vec![
        (vec!["generate"], true), // Missing prompt
        (vec!["download"], true), // Missing model name
        (vec!["search"], true), // Missing query
        (vec!["show"], true), // Missing model name
    ];

    for (args, should_fail) in test_cases {
        let output = run_model_rs(&args);
        
        if should_fail {
            assert!(!output.unwrap().status.success(), "Command {:?} should fail", args);
        }
    }
}