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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//! T-COV-95 Extended Coverage: cli/mod.rs
//!
//! Targets: format_size, is_local_file_path, validate_suite_name,
//! display_model_info, run_bench_compare, run_bench_regression

use crate::cli::*;

// ============================================================================
// format_size coverage
// ============================================================================

#[test]
fn test_format_size_zero() {
    let result = format_size(0);
    assert!(result.contains("0") || result.contains("B"));
}

#[test]
fn test_format_size_bytes() {
    let result = format_size(500);
    assert!(result.contains("500") || result.contains("B"));
}

#[test]
fn test_format_size_kilobytes() {
    let result = format_size(1024);
    assert!(result.contains("KB") || result.contains("1"));
}

#[test]
fn test_format_size_megabytes() {
    let result = format_size(1024 * 1024);
    assert!(result.contains("MB") || result.contains("1"));
}

#[test]
fn test_format_size_gigabytes() {
    let result = format_size(1024 * 1024 * 1024);
    assert!(result.contains("GB") || result.contains("1"));
}

#[test]
fn test_format_size_terabytes() {
    let result = format_size(1024 * 1024 * 1024 * 1024);
    assert!(result.contains("TB") || result.contains("1"));
}

#[test]
fn test_format_size_fractional_kb() {
    let result = format_size(1536); // 1.5 KB
    assert!(!result.is_empty());
}

#[test]
fn test_format_size_fractional_mb() {
    let result = format_size(1024 * 1024 + 512 * 1024); // 1.5 MB
    assert!(!result.is_empty());
}

#[test]
fn test_format_size_large() {
    let result = format_size(u64::MAX / 2);
    assert!(!result.is_empty());
}

// ============================================================================
// is_local_file_path coverage
// Actual implementation: Returns true if:
// - starts with "./" or "/"
// - ends with ".gguf", ".safetensors", or ".apr"
// ============================================================================

#[test]
fn test_is_local_path_absolute() {
    assert!(is_local_file_path("/path/to/model.gguf"));
}

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

#[test]
fn test_is_local_path_relative_parent() {
    // Starts with "../" NOT "./" - but ends with .gguf, so still local
    assert!(is_local_file_path("../model.gguf"));
}

#[test]
fn test_is_local_path_home() {
    // "~" doesn't start with "./" or "/" but ends with .gguf
    assert!(is_local_file_path("~/models/model.gguf"));
}

#[test]
fn test_is_local_path_http_with_extension() {
    // Implementation checks extensions, so URLs ending in .gguf are considered "local"
    // This is testing actual behavior, not ideal behavior
    assert!(is_local_file_path("http://example.com/model.gguf"));
}

#[test]
fn test_is_local_path_https_with_extension() {
    // Same as above - extension-based detection
    assert!(is_local_file_path("https://example.com/model.gguf"));
}

#[test]
fn test_is_local_path_hf_registry() {
    // No extension and doesn't start with ./ or /
    assert!(!is_local_file_path("hf://username/model"));
}

#[test]
fn test_is_local_path_ollama_registry() {
    // No extension and doesn't start with ./ or /
    assert!(!is_local_file_path("ollama://model:tag"));
}

#[test]
fn test_is_local_path_registry_prefix() {
    // No extension and doesn't start with ./ or /
    assert!(!is_local_file_path("registry://some/model"));
}

#[test]
fn test_is_local_path_bare_name() {
    // Bare names without path separators and no extensions
    let result = is_local_file_path("model-name");
    assert!(!result); // Should be false
}

#[test]
fn test_is_local_path_with_spaces() {
    assert!(is_local_file_path("/path/to/my model.gguf"));
}

#[test]
fn test_is_local_path_empty() {
    assert!(!is_local_file_path(""));
}

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

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

#[test]
fn test_is_local_path_just_slash() {
    assert!(is_local_file_path("/"));
}

#[test]
fn test_is_local_path_just_dotslash() {
    assert!(is_local_file_path("./"));
}

// ============================================================================
// validate_suite_name coverage
// Actual implementation: Checks if suite_name is in BENCHMARK_SUITES constant
// Valid suites: tensor_ops, inference, cache, tokenizer, quantize, lambda, comparative
// ============================================================================

#[test]
fn test_validate_suite_tensor_ops() {
    assert!(validate_suite_name("tensor_ops"));
}

#[test]
fn test_validate_suite_inference() {
    assert!(validate_suite_name("inference"));
}

#[test]
fn test_validate_suite_cache() {
    assert!(validate_suite_name("cache"));
}

#[test]
fn test_validate_suite_tokenizer() {
    assert!(validate_suite_name("tokenizer"));
}

#[test]
fn test_validate_suite_quantize() {
    assert!(validate_suite_name("quantize"));
}

#[test]
fn test_validate_suite_lambda() {
    assert!(validate_suite_name("lambda"));
}

#[test]
fn test_validate_suite_comparative() {
    assert!(validate_suite_name("comparative"));
}

#[test]
fn test_validate_suite_invalid_random() {
    assert!(!validate_suite_name("random_suite"));
}

#[test]
fn test_validate_suite_invalid_space() {
    assert!(!validate_suite_name("my test"));
}

#[test]
fn test_validate_suite_invalid_similar() {
    // Close but not exact match
    assert!(!validate_suite_name("tensor"));
}

#[test]
fn test_validate_suite_empty() {
    assert!(!validate_suite_name(""));
}

#[test]
fn test_validate_suite_case_sensitive() {
    // Suite names are case-sensitive
    assert!(!validate_suite_name("TENSOR_OPS"));
}

#[test]
fn test_validate_suite_with_extra_chars() {
    assert!(!validate_suite_name("tensor_ops!"));
}

#[test]
fn test_validate_suite_unicode() {
    assert!(!validate_suite_name("тест"));
}

// ============================================================================
// display_model_info error paths
// ============================================================================

#[test]
fn test_display_model_info_empty_data() {
    let result = display_model_info("test.gguf", &[]);
    assert!(result.is_err());
}

#[test]
fn test_display_model_info_invalid_magic() {
    let data = vec![0u8; 100];
    let result = display_model_info("test.gguf", &data);
    assert!(result.is_err());
}

#[test]
fn test_display_model_info_truncated() {
    let data = vec![0x47, 0x47, 0x55, 0x46]; // "GGUF" magic but truncated
    let result = display_model_info("test.gguf", &data);
    assert!(result.is_err());
}

#[test]
fn test_display_model_info_unknown_extension() {
    let data = vec![0u8; 100];
    let result = display_model_info("test.unknown", &data);
    // Should handle unknown extension gracefully
    assert!(result.is_err() || result.is_ok());
}

// ============================================================================
// load_gguf_model error paths
// ============================================================================

#[test]
fn test_load_gguf_empty() {
    let result = load_gguf_model(&[]);
    assert!(result.is_err());
}

#[test]
fn test_load_gguf_invalid_magic() {
    let data = vec![0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
    let result = load_gguf_model(&data);
    assert!(result.is_err());
}

#[test]
fn test_load_gguf_truncated_header() {
    // Valid GGUF magic but truncated
    let data = vec![0x47, 0x47, 0x55, 0x46, 0x03, 0x00, 0x00, 0x00];
    let result = load_gguf_model(&data);
    assert!(result.is_err());
}

// ============================================================================
// load_safetensors_model error paths
// ============================================================================

#[test]
fn test_load_safetensors_empty() {
    let result = load_safetensors_model(&[]);
    assert!(result.is_err());
}

#[test]
fn test_load_safetensors_invalid_json() {
    // Header says 16 bytes of metadata, but data is invalid JSON
    let mut data = vec![0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; // 16-byte metadata
    data.extend(b"not valid json!!"); // 16 bytes of invalid JSON
    let result = load_safetensors_model(&data);
    assert!(result.is_err());
}

#[test]
fn test_load_safetensors_truncated() {
    // Header size but no actual header
    let data = vec![0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
    let result = load_safetensors_model(&data);
    assert!(result.is_err());
}

// ============================================================================
// load_apr_model error paths
// ============================================================================

#[test]
fn test_load_apr_empty() {
    let result = load_apr_model(&[]);
    assert!(result.is_err());
}

#[test]
fn test_load_apr_invalid_magic() {
    let data = vec![0x00, 0x00, 0x00, 0x00];
    let result = load_apr_model(&data);
    assert!(result.is_err());
}

#[test]
fn test_load_apr_truncated() {
    // APR magic but truncated
    let data = b"APR\x00".to_vec();
    let result = load_apr_model(&data);
    assert!(result.is_err());
}

// ============================================================================
// run_bench_compare error paths
// ============================================================================

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

#[test]
fn test_bench_compare_invalid_threshold_low() {
    // Even with nonexistent files, should fail gracefully
    let result = run_bench_compare("/nonexistent/a.json", "/nonexistent/b.json", -1.0);
    assert!(result.is_err());
}

#[test]
fn test_bench_compare_invalid_threshold_high() {
    let result = run_bench_compare("/nonexistent/a.json", "/nonexistent/b.json", 1000.0);
    assert!(result.is_err());
}

// ============================================================================
// run_bench_regression error paths
// ============================================================================

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

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

// ============================================================================
// home_dir coverage
// ============================================================================

#[test]
fn test_home_dir_returns_some() {
    // On most systems, home dir should be available
    let result = home_dir();
    // Just verify it doesn't panic
    let _ = result;
}

// ============================================================================
// print_info coverage
// ============================================================================

#[test]
fn test_print_info_no_panic() {
    // Just verify it doesn't panic
    print_info();
}

// ============================================================================
// Cli struct coverage
// ============================================================================

#[test]
fn test_cli_default_verbose() {
    use clap::Parser;
    // Use try_parse_from to avoid exit on --help
    let result = Cli::try_parse_from(["realizar", "--help"]);
    // --help returns an error (display help), which is expected
    assert!(result.is_err());
}

// ============================================================================
// Additional edge cases
// ============================================================================

#[test]
fn test_format_size_exact_boundaries() {
    // Exactly 1 KB
    let kb = format_size(1024);
    assert!(!kb.is_empty());

    // Exactly 1 MB
    let mb = format_size(1024 * 1024);
    assert!(!mb.is_empty());

    // Exactly 1 GB
    let gb = format_size(1024 * 1024 * 1024);
    assert!(!gb.is_empty());
}

#[test]
fn test_is_local_path_windows_style() {
    // Windows-style paths (should still work on Linux for path detection)
    let result = is_local_file_path("C:\\path\\to\\model.gguf");
    // Just verify it handles it
    let _ = result;
}

#[test]
fn test_validate_suite_long_name() {
    let long_name = "a".repeat(256);
    let result = validate_suite_name(&long_name);
    // Should handle long names
    let _ = result;
}