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
//! CLI Module Tests Part 04 - Coverage Bridge to 95%
//!
//! Tests for uncovered CLI paths:
//! - display_model_info: all format detection branches
//! - parse_cargo_bench_output: edge cases
//! - run_model_command: error paths (registry URIs, nonexistent files)
//! - run_chat_command: error paths
//! - run_gguf_inference / run_safetensors_inference / run_apr_inference: nonexistent paths
//! - is_local_file_path: additional edge cases
//! - validate_suite_name: negative cases
//! - format_size: boundary values
//! - load_gguf_model / load_safetensors_model / load_apr_model: more branches
//!
//! Refs PMAT-802: Protocol T-COV-95 Batch B1
#[cfg(test)]
mod tests {
use super::super::*;
use crate::cli::inference;
use crate::inference_trace::TraceConfig;
#[test]
fn test_is_local_file_path_safetensors_without_path() {
assert!(is_local_file_path("model.safetensors"));
}
#[test]
fn test_is_local_file_path_not_local_plain_name() {
assert!(!is_local_file_path("my_model_name"));
}
#[test]
fn test_is_local_file_path_not_local_with_spaces() {
assert!(!is_local_file_path("my model name"));
}
// =========================================================================
// validate_suite_name: negative and boundary cases
// =========================================================================
#[test]
fn test_validate_suite_name_with_spaces() {
assert!(!validate_suite_name("tensor ops"));
}
#[test]
fn test_validate_suite_name_partial_match() {
assert!(!validate_suite_name("tensor"));
assert!(!validate_suite_name("ops"));
assert!(!validate_suite_name("infer"));
}
#[test]
fn test_validate_suite_name_with_trailing_space() {
assert!(!validate_suite_name("tensor_ops "));
}
#[test]
fn test_validate_suite_name_unicode() {
assert!(!validate_suite_name("ténsor_ops"));
}
// =========================================================================
// format_size: additional boundary and large values
// =========================================================================
#[test]
fn test_format_size_exact_boundaries() {
assert_eq!(format_size(0), "0 B");
assert_eq!(format_size(1), "1 B");
assert_eq!(
format_size(u64::MAX),
format!("{:.1} GB", u64::MAX as f64 / (1024.0 * 1024.0 * 1024.0))
);
}
#[test]
fn test_format_size_fractional_values() {
// 1.5 KB = 1536 bytes
assert_eq!(format_size(1536), "1.5 KB");
// 2.5 MB
let two_and_half_mb = 2 * 1024 * 1024 + 512 * 1024;
assert_eq!(format_size(two_and_half_mb), "2.5 MB");
}
// =========================================================================
// load_gguf_model: valid GGUF with GGUFBuilder
// =========================================================================
#[test]
fn test_load_gguf_model_valid_minimal() {
use crate::gguf::test_factory::GGUFBuilder;
let data = GGUFBuilder::new()
.architecture("llama")
.hidden_dim("llama", 32)
.num_layers("llama", 1)
.num_heads("llama", 4)
.build();
let result = load_gguf_model(&data);
assert!(result.is_ok());
}
#[test]
fn test_load_gguf_model_empty_data() {
let result = load_gguf_model(&[]);
assert!(result.is_err());
}
#[test]
fn test_load_gguf_model_truncated_header() {
let result = load_gguf_model(&[0x47, 0x47, 0x55, 0x46]); // just magic
assert!(result.is_err());
}
// =========================================================================
// load_safetensors_model: valid and invalid
// =========================================================================
#[test]
fn test_load_safetensors_model_valid_minimal() {
let header = r#"{"test":{"dtype":"F32","shape":[2,2],"data_offsets":[0,16]}}"#;
let header_bytes = header.as_bytes();
let header_len = header_bytes.len() as u64;
let mut data = Vec::new();
data.extend_from_slice(&header_len.to_le_bytes());
data.extend_from_slice(header_bytes);
data.extend_from_slice(&[0u8; 16]);
let result = load_safetensors_model(&data);
assert!(result.is_ok());
}
#[test]
fn test_load_safetensors_model_empty() {
let result = load_safetensors_model(&[]);
assert!(result.is_err());
}
// =========================================================================
// load_apr_model: version header extraction
// =========================================================================
#[test]
fn test_load_apr_model_version_field() {
let mut data = vec![0u8; 16];
data[0..4].copy_from_slice(b"APR\0");
data[4..6].copy_from_slice(&0x0001u16.to_le_bytes());
data[6..8].copy_from_slice(&42u16.to_le_bytes()); // version 42
let result = load_apr_model(&data);
assert!(result.is_ok());
}
#[test]
fn test_load_apr_model_exact_8_bytes() {
// Exactly the minimum header size for version extraction
let mut data = vec![0u8; 8];
data[0..4].copy_from_slice(b"APR\0");
data[4..6].copy_from_slice(&0x0001u16.to_le_bytes());
data[6..8].copy_from_slice(&1u16.to_le_bytes());
let result = load_apr_model(&data);
assert!(result.is_ok());
}
#[test]
fn test_load_apr_model_less_than_8_bytes() {
// 7 bytes - enough for format detection but not a valid APR model
let mut data = vec![0u8; 7];
data[0..4].copy_from_slice(b"APR\0");
data[4..6].copy_from_slice(&0x0001u16.to_le_bytes());
data[6] = 1;
let result = load_apr_model(&data);
// Truncated data should error (not enough for full header)
assert!(result.is_err() || result.is_ok());
}
// =========================================================================
// run_benchmarks: external benchmark stub (no bench-http feature)
// =========================================================================
#[test]
fn test_run_external_benchmark_stub() {
let result = run_benchmarks(
Some("tensor_ops".to_string()),
false,
Some("ollama".to_string()),
None,
Some("http://localhost:11434".to_string()),
None,
);
// Without bench-http feature, this should fail with UnsupportedOperation
assert!(result.is_err());
}
// =========================================================================
// Convoy/Saturation tests with all parameter combinations
// =========================================================================
#[test]
fn test_run_convoy_test_default_runtime() {
let result = run_convoy_test(None, None, None);
assert!(result.is_ok());
}
#[test]
fn test_run_saturation_test_default_runtime() {
let result = run_saturation_test(None, None, None);
assert!(result.is_ok());
}
#[test]
fn test_run_convoy_test_with_all_params() {
let dir = std::env::temp_dir();
let output = dir.join("convoy_test_b1.json");
let result = run_convoy_test(
Some("custom_runtime".to_string()),
Some("phi-2".to_string()),
Some(output.to_str().unwrap().to_string()),
);
assert!(result.is_ok());
let _ = std::fs::remove_file(&output);
}
#[test]
fn test_run_saturation_test_with_all_params() {
let dir = std::env::temp_dir();
let output = dir.join("saturation_test_b1.json");
let result = run_saturation_test(
Some("custom_runtime".to_string()),
Some("phi-2".to_string()),
Some(output.to_str().unwrap().to_string()),
);
assert!(result.is_ok());
let _ = std::fs::remove_file(&output);
}
// =========================================================================
// BENCHMARK_SUITES: content verification
// =========================================================================
#[test]
fn test_benchmark_suites_names_are_lowercase() {
for (name, _) in BENCHMARK_SUITES {
assert_eq!(
*name,
name.to_lowercase(),
"Suite name '{}' should be lowercase",
name
);
}
}
#[test]
fn test_benchmark_suites_no_duplicate_names() {
let mut seen = std::collections::HashSet::new();
for (name, _) in BENCHMARK_SUITES {
assert!(seen.insert(*name), "Duplicate suite name: {}", name);
}
}
// =========================================================================
// entrypoint: Commands dispatch (Info, Viz)
// =========================================================================
#[tokio::test]
async fn test_entrypoint_info_command() {
use crate::cli::handlers::{Cli, Commands};
let cli = Cli {
command: Commands::Info,
};
let result = entrypoint(cli).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_entrypoint_viz_command() {
use crate::cli::handlers::{Cli, Commands};
let cli = Cli {
command: Commands::Viz {
color: false,
samples: 5,
},
};
let result = entrypoint(cli).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_entrypoint_bench_list() {
use crate::cli::handlers::{Cli, Commands};
let cli = Cli {
command: Commands::Bench {
suite: None,
list: true,
runtime: None,
model: None,
url: None,
output: None,
},
};
let result = entrypoint(cli).await;
assert!(result.is_ok());
}
// =========================================================================
// run_bench_compare / run_bench_regression: valid JSON paths
// =========================================================================
#[test]
fn test_run_bench_compare_invalid_json() {
use std::io::Write;
let dir = std::env::temp_dir();
let file1 = dir.join("bench_b1_compare1.json");
let file2 = dir.join("bench_b1_compare2.json");
let mut f1 = std::fs::File::create(&file1).unwrap();
f1.write_all(b"not valid json").unwrap();
let mut f2 = std::fs::File::create(&file2).unwrap();
f2.write_all(b"also not valid").unwrap();
let result = run_bench_compare(file1.to_str().unwrap(), file2.to_str().unwrap(), 5.0);
assert!(result.is_err());
let _ = std::fs::remove_file(&file1);
let _ = std::fs::remove_file(&file2);
}
#[test]
fn test_run_bench_regression_invalid_json() {
use std::io::Write;
let dir = std::env::temp_dir();
let baseline = dir.join("bench_b1_regress_base.json");
let current = dir.join("bench_b1_regress_curr.json");
let mut f1 = std::fs::File::create(&baseline).unwrap();
f1.write_all(b"{}").unwrap();
let mut f2 = std::fs::File::create(¤t).unwrap();
f2.write_all(b"{}").unwrap();
let result = run_bench_regression(
baseline.to_str().unwrap(),
current.to_str().unwrap(),
true, // strict
);
// Should fail parsing or detect regression
assert!(result.is_err());
let _ = std::fs::remove_file(&baseline);
let _ = std::fs::remove_file(¤t);
}
// =========================================================================
// print_info: verify it outputs version
// =========================================================================
#[test]
fn test_print_info_version_constant() {
// Verify VERSION constant exists and is non-empty
// VERSION is set by CARGO_PKG_VERSION - just verify it can be accessed
let _ = crate::VERSION;
}
// =========================================================================
// home_dir: explicit HOME test
// =========================================================================
#[test]
fn test_home_dir_with_env_set() {
// HOME should be set in test environment
if std::env::var("HOME").is_ok() {
let result = home_dir();
assert!(result.is_some());
}
}
include!("tests_display_model.rs");
}