realizar 0.8.5

Pure Rust ML inference engine built from scratch - model serving for GGUF and safetensors
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
//! T-COV-95 Poisoned Pygmies: CLI Graceful Degradation Tests (PMAT-802)
//!
//! Dr. Popper's directive: "The CLI must not merely run; it must gracefully
//! degrade when fed poisoned artifacts. Valid headers, invalid data."
//!
//! This module tests:
//! 1. Benchmark subcommand with poisoned data
//! 2. Visualize subcommand with edge cases
//! 3. Format/display functions with corrupted inputs
//!
//! Target: 483 missed lines in cli/mod.rs

use super::*;
use std::io::Write;
use tempfile::NamedTempFile;

// ============================================================================
// Poisoned Pygmy: Format Size Edge Cases
// ============================================================================

#[test]
fn test_format_size_zero() {
    let result = format_size(0);
    assert_eq!(result, "0 B");
}

#[test]
fn test_format_size_bytes() {
    assert_eq!(format_size(1), "1 B");
    assert_eq!(format_size(512), "512 B");
    assert_eq!(format_size(1023), "1023 B");
}

#[test]
fn test_format_size_kilobytes() {
    assert_eq!(format_size(1024), "1.0 KB");
    assert_eq!(format_size(1536), "1.5 KB");
    assert_eq!(format_size(1024 * 100), "100.0 KB");
}

#[test]
fn test_format_size_megabytes() {
    assert_eq!(format_size(1024 * 1024), "1.0 MB");
    assert_eq!(format_size(1024 * 1024 * 512), "512.0 MB");
}

#[test]
fn test_format_size_gigabytes() {
    assert_eq!(format_size(1024 * 1024 * 1024), "1.0 GB");
    assert_eq!(format_size(1024 * 1024 * 1024 * 7), "7.0 GB");
}

#[test]
fn test_format_size_boundary_kb() {
    // Just below KB boundary
    assert_eq!(format_size(1023), "1023 B");
    // At KB boundary
    assert_eq!(format_size(1024), "1.0 KB");
}

#[test]
fn test_format_size_boundary_mb() {
    // Just below MB boundary
    let result = format_size(1024 * 1024 - 1);
    assert!(result.contains("KB"));
    // At MB boundary
    assert_eq!(format_size(1024 * 1024), "1.0 MB");
}

#[test]
fn test_format_size_boundary_gb() {
    // Just below GB boundary
    let result = format_size(1024 * 1024 * 1024 - 1);
    assert!(result.contains("MB"));
    // At GB boundary
    assert_eq!(format_size(1024 * 1024 * 1024), "1.0 GB");
}

// ============================================================================
// Poisoned Pygmy: Benchmark Suite Validation
// ============================================================================

#[test]
fn test_benchmark_suites_exist() {
    // Verify BENCHMARK_SUITES has content
    assert!(BENCHMARK_SUITES.len() >= 5);
}

#[test]
fn test_benchmark_suites_have_descriptions() {
    for (name, desc) in BENCHMARK_SUITES {
        assert!(!name.is_empty(), "Suite name should not be empty");
        assert!(!desc.is_empty(), "Suite description should not be empty");
    }
}

#[test]
fn test_benchmark_suite_names_valid() {
    let expected = ["tensor_ops", "inference", "cache", "tokenizer", "quantize"];
    for name in expected {
        assert!(
            BENCHMARK_SUITES.iter().any(|(n, _)| *n == name),
            "Expected suite '{}' not found",
            name
        );
    }
}

// ============================================================================
// Poisoned Pygmy: Display Model Info with Corrupted Files
// ============================================================================

#[test]
fn test_display_model_info_gguf_valid_header() {
    use crate::gguf::{GGUF_MAGIC, GGUF_VERSION_V3};

    // Create minimal valid GGUF
    let mut data = Vec::new();
    data.extend_from_slice(&GGUF_MAGIC.to_le_bytes());
    data.extend_from_slice(&GGUF_VERSION_V3.to_le_bytes());
    data.extend_from_slice(&1u64.to_le_bytes()); // tensor_count
    data.extend_from_slice(&0u64.to_le_bytes()); // metadata_count

    // Should not panic even with minimal data
    let result = display_model_info("test.gguf", &data);
    // May succeed or fail, but shouldn't panic
    let _ = result;
}

#[test]
fn test_display_model_info_gguf_poisoned_truncated() {
    use crate::gguf::GGUF_MAGIC;

    // Valid magic but truncated - only 8 bytes
    let mut data = Vec::new();
    data.extend_from_slice(&GGUF_MAGIC.to_le_bytes());
    data.extend_from_slice(&3u32.to_le_bytes()); // Partial version

    let result = display_model_info("test.gguf", &data);
    // Should fail gracefully, not panic
    assert!(result.is_err());
}

#[test]
fn test_display_model_info_safetensors_poisoned() {
    // Valid SafeTensors structure but invalid JSON
    let mut data = Vec::new();
    let header = b"{ not valid json }";
    data.extend_from_slice(&(header.len() as u64).to_le_bytes());
    data.extend_from_slice(header);

    let result = display_model_info("test.safetensors", &data);
    // Should fail gracefully
    assert!(result.is_err());
}

#[test]
fn test_display_model_info_apr_poisoned() {
    use crate::format::APR_MAGIC;

    // Valid APR magic but corrupted metadata
    let mut data = Vec::new();
    data.extend_from_slice(APR_MAGIC);
    data.extend_from_slice(&[0xFF; 100]); // Garbage

    let result = display_model_info("test.apr", &data);
    // Should handle gracefully (may succeed with "Unknown" or fail)
    let _ = result;
}

#[test]
fn test_display_model_info_unknown_format() {
    // Completely unknown format
    let data = b"UNKNOWN_FORMAT_HEADER_DATA_HERE";

    let result = display_model_info("test.bin", data);
    // Should succeed and report as unknown
    assert!(result.is_ok());
}

#[test]
fn test_display_model_info_empty_file() {
    let data: &[u8] = &[];

    let result = display_model_info("empty.gguf", data);
    // Should fail gracefully with empty data
    assert!(result.is_err() || result.is_ok()); // Either way, no panic
}

// ============================================================================
// Poisoned Pygmy: Visualization with Edge Cases
// ============================================================================

#[test]
fn test_run_visualization_zero_samples() {
    // Zero samples - should not panic
    run_visualization(false, 0);
}

#[test]
fn test_run_visualization_one_sample() {
    // Single sample - edge case
    run_visualization(false, 1);
}

#[test]
fn test_run_visualization_large_samples() {
    // Large sample count
    run_visualization(false, 1000);
}

#[test]
fn test_run_visualization_with_color() {
    // Color enabled
    run_visualization(true, 100);
}

#[test]
fn test_run_visualization_without_color() {
    // Color disabled
    run_visualization(false, 100);
}

// ============================================================================
// Poisoned Pygmy: is_local_file_path Edge Cases
// ============================================================================

#[test]
fn test_is_local_file_path_absolute() {
    assert!(is_local_file_path("/home/user/model.gguf"));
    assert!(is_local_file_path("/tmp/test.apr"));
}

#[test]
fn test_is_local_file_path_relative() {
    assert!(is_local_file_path("./model.gguf"));
    assert!(is_local_file_path("models/test.apr"));
}

#[test]
fn test_is_local_file_path_registry_uri() {
    assert!(!is_local_file_path("pacha://model/name"));
    assert!(!is_local_file_path("hf://org/model"));
}

#[test]
fn test_is_local_file_path_empty() {
    // Empty string may or may not be treated as local - implementation dependent
    let result = is_local_file_path("");
    // Just verify it doesn't panic
    let _ = result;
}

#[test]
fn test_is_local_file_path_just_filename() {
    assert!(is_local_file_path("model.gguf"));
}

// ============================================================================
// Poisoned Pygmy: Benchmark File Operations
// ============================================================================

#[test]
fn test_run_bench_compare_nonexistent_files() {
    let result = run_bench_compare("/nonexistent/file1.json", "/nonexistent/file2.json", 0.1);
    assert!(result.is_err());
}

#[test]
fn test_run_bench_compare_empty_file() {
    let mut temp = NamedTempFile::new().unwrap();
    temp.write_all(b"").unwrap();
    temp.flush().unwrap();

    let result = run_bench_compare(
        temp.path().to_str().unwrap(),
        temp.path().to_str().unwrap(),
        0.1,
    );
    // Should fail gracefully with empty JSON
    assert!(result.is_err());
}

#[test]
fn test_run_bench_compare_invalid_json() {
    let mut temp = NamedTempFile::new().unwrap();
    temp.write_all(b"{ not valid json }").unwrap();
    temp.flush().unwrap();

    let result = run_bench_compare(
        temp.path().to_str().unwrap(),
        temp.path().to_str().unwrap(),
        0.1,
    );
    assert!(result.is_err());
}

#[test]
fn test_run_bench_regression_nonexistent() {
    let result = run_bench_regression(
        "/nonexistent/baseline.json",
        "/nonexistent/current.json",
        false, // strict
    );
    assert!(result.is_err());
}

// ============================================================================
// Poisoned Pygmy: Convoy and Saturation Tests (CLI paths)
// ============================================================================

#[test]
fn test_run_convoy_test_no_model() {
    // Runtime None means use default
    let result = run_convoy_test(None, None, None);
    // Should handle gracefully (may succeed with no-op or fail)
    let _ = result;
}

#[test]
fn test_run_saturation_test_no_model() {
    let result = run_saturation_test(None, None, None);
    let _ = result;
}

// ============================================================================
// Poisoned Pygmy: Benchmark List Mode
// ============================================================================

#[test]
fn test_run_benchmarks_list_mode() {
    // list=true should just print suites and return
    let result = run_benchmarks(None, true, None, None, None, None);
    assert!(result.is_ok());
}

#[test]
#[ignore = "calls std::process::exit(1) which kills test process"]
fn test_run_benchmarks_invalid_suite() {
    // Note: This test is ignored because run_benchmarks calls process::exit(1)
    // for invalid suite names, which kills the test harness.
    let result = run_benchmarks(
        Some("nonexistent_suite".to_string()),
        false,
        None,
        None,
        None,
        None,
    );
    let _ = result;
}

#[test]
#[ignore = "requires cargo bench infrastructure"]
fn test_run_benchmarks_valid_suite_no_runtime() {
    // Note: This test is ignored because it tries to run actual cargo bench
    let result = run_benchmarks(
        Some("tensor_ops".to_string()),
        false,
        None,
        None,
        None,
        None,
    );
    let _ = result;
}

// ============================================================================
// Poisoned Pygmy: Chat Template Handling
// ============================================================================

#[test]
fn test_display_model_info_by_extension() {
    // Test extension-based detection
    use crate::gguf::GGUF_MAGIC;

    let mut data = Vec::new();
    data.extend_from_slice(&GGUF_MAGIC.to_le_bytes());
    // Incomplete but has valid magic

    // .gguf extension triggers GGUF path
    let result = display_model_info("model.gguf", &data);
    // Either parses or fails, but uses GGUF path
    let _ = result;
}

#[test]
fn test_display_model_info_safetensors_extension() {
    // Test .safetensors extension path
    let mut data = Vec::new();
    let header = b"{}"; // Valid but empty JSON
    data.extend_from_slice(&(header.len() as u64).to_le_bytes());
    data.extend_from_slice(header);

    let result = display_model_info("model.safetensors", &data);
    // Empty header is valid JSON, may succeed or fail on tensor count
    let _ = result;
}

// ============================================================================
// Poisoned Pygmy: Handlers Module Integration
// ============================================================================

#[test]
fn test_validate_suite_name_valid() {
    for (name, _) in BENCHMARK_SUITES {
        // All defined suites should be valid
        let is_valid = BENCHMARK_SUITES.iter().any(|(n, _)| *n == *name);
        assert!(is_valid);
    }
}

#[test]
fn test_validate_suite_name_invalid() {
    let invalid_names = ["invalid", "nonexistent", "test123", ""];
    for name in invalid_names {
        let is_valid = BENCHMARK_SUITES.iter().any(|(n, _)| *n == name);
        // These should NOT be valid suites
        if !name.is_empty() {
            assert!(!is_valid, "Name '{}' should not be valid", name);
        }
    }
}

// ============================================================================
// Poisoned Pygmy: Error Message Coverage
// ============================================================================

#[test]
fn test_model_not_found_error_path() {
    use crate::error::RealizarError;

    let err = RealizarError::ModelNotFound("nonexistent.gguf".to_string());
    let msg = err.to_string();
    assert!(msg.contains("nonexistent.gguf") || msg.contains("not found"));
}

#[test]
fn test_unsupported_operation_error_path() {
    use crate::error::RealizarError;

    let err = RealizarError::UnsupportedOperation {
        operation: "test_op".to_string(),
        reason: "test reason".to_string(),
    };
    let msg = err.to_string();
    assert!(msg.contains("test_op") || msg.contains("test reason") || msg.contains("Unsupported"));
}