apr-cli 0.31.1

CLI tool for APR model inspection, debugging, and operations
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

    #[test]
    fn test_clean_chat_response_im_end_alone() {
        let raw = "Some text<|im_end|>";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Some text");
    }

    #[test]
    fn test_clean_chat_response_endoftext_midstream() {
        let raw = "Hello<|endoftext|> world";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Hello world");
    }

    #[test]
    fn test_clean_chat_response_assistant_without_newline() {
        let raw = "<|im_start|>assistantHello";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Hello");
    }

    #[test]
    fn test_clean_chat_response_multiple_endoftext() {
        let raw = "Text<|endoftext|><|endoftext|>more";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Textmore");
    }

    #[test]
    fn test_clean_chat_response_all_markers_combined() {
        let raw = "<|im_start|>assistant\n<|im_start|>Hello <|endoftext|>world<|im_end|>";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Hello world");
    }

    // =========================================================================
    // clean_chat_response: whitespace normalization
    // =========================================================================

    #[test]
    fn test_clean_chat_response_many_spaces() {
        let raw = "Hello     world     test";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Hello world test");
    }

    #[test]
    fn test_clean_chat_response_only_whitespace() {
        let raw = "   \t   \n   ";
        let cleaned = clean_chat_response(raw);
        assert!(cleaned.is_empty() || cleaned.trim().is_empty());
    }

    #[test]
    fn test_clean_chat_response_newlines_preserved() {
        let raw = "Line1\nLine2\nLine3";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Line1\nLine2\nLine3");
    }

    #[test]
    fn test_clean_chat_response_leading_newline_trimmed() {
        let raw = "\nHello";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Hello");
    }

    #[test]
    fn test_clean_chat_response_trailing_newline_trimmed() {
        let raw = "Hello\n";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Hello");
    }

    // =========================================================================
    // detect_format: additional edge cases
    // =========================================================================

    #[test]
    fn test_detect_format_empty_filename() {
        let path = Path::new("");
        assert_eq!(detect_format(path), ModelFormat::Demo);
    }

    #[test]
    fn test_detect_format_just_extension_apr() {
        // Path is literally ".apr"
        let path = Path::new(".apr");
        // file_stem is "" for ".apr", extension is "apr" on some platforms
        // but actually Path::new(".apr").extension() returns None on Unix
        // because ".apr" is treated as a hidden file with no extension
        assert_eq!(detect_format(path), ModelFormat::Demo);
    }

    #[test]
    fn test_detect_format_mixed_case_gguf() {
        let path = Path::new("/models/test.GGUF");
        // Case-sensitive: "GGUF" != "gguf"
        assert_eq!(detect_format(path), ModelFormat::Demo);
    }

    #[test]
    fn test_detect_format_mixed_case_safetensors() {
        let path = Path::new("/models/test.SafeTensors");
        assert_eq!(detect_format(path), ModelFormat::Demo);
    }

    #[test]
    fn test_detect_format_with_spaces_in_path() {
        let path = Path::new("/my models/test model.gguf");
        assert_eq!(detect_format(path), ModelFormat::Gguf);
    }

    #[test]
    fn test_detect_format_unicode_path() {
        let path = Path::new("/modelos/modelo.apr");
        assert_eq!(detect_format(path), ModelFormat::Apr);
    }

    #[test]
    fn test_detect_format_root_path() {
        let path = Path::new("/model.safetensors");
        assert_eq!(detect_format(path), ModelFormat::SafeTensors);
    }

    #[test]
    fn test_detect_format_current_dir() {
        let path = Path::new("model.apr");
        assert_eq!(detect_format(path), ModelFormat::Apr);
    }

    // =========================================================================
    // detect_format_from_bytes: additional edge cases (inference only)
    // =========================================================================

    #[cfg(feature = "inference")]
    mod inference_edge_cases {
        use super::*;

        #[test]
        fn test_detect_format_from_bytes_exactly_8_bytes() {
            // Exactly 8 bytes, not matching any known magic
            let data = [0u8; 8];
            // header_size = 0, so SafeTensors check fails (header_size > 0)
            assert_eq!(detect_format_from_bytes(&data), ModelFormat::Demo);
        }

        #[test]
        fn test_detect_format_from_bytes_safetensors_header_zero() {
            // SafeTensors with header_size = 0 should NOT match
            let mut data = vec![0u8; 16];
            data[0..8].copy_from_slice(&0u64.to_le_bytes());
            assert_eq!(detect_format_from_bytes(&data), ModelFormat::Demo);
        }

        #[test]
        fn test_detect_format_from_bytes_safetensors_header_huge() {
            // SafeTensors with header_size >= 100_000_000 should NOT match
            let mut data = vec![0u8; 16];
            data[0..8].copy_from_slice(&100_000_000u64.to_le_bytes());
            assert_eq!(detect_format_from_bytes(&data), ModelFormat::Demo);
        }

        #[test]
        fn test_detect_format_from_bytes_safetensors_header_boundary() {
            // SafeTensors with header_size = 99_999_999 should match
            let mut data = vec![0u8; 16];
            data[0..8].copy_from_slice(&99_999_999u64.to_le_bytes());
            assert_eq!(detect_format_from_bytes(&data), ModelFormat::SafeTensors);
        }

        #[test]
        fn test_detect_format_from_bytes_safetensors_header_one() {
            // SafeTensors with header_size = 1 should match
            let mut data = vec![0u8; 16];
            data[0..8].copy_from_slice(&1u64.to_le_bytes());
            assert_eq!(detect_format_from_bytes(&data), ModelFormat::SafeTensors);
        }

        #[test]
        fn test_detect_format_from_bytes_7_bytes() {
            // 7 bytes (less than 8) should return Demo
            let data = b"APR2xxx";
            assert_eq!(detect_format_from_bytes(data), ModelFormat::Demo);
        }

        #[test]
        fn test_detect_format_from_bytes_apr_takes_priority_over_safetensors() {
            // "APRN" starts with bytes that could also be a valid u64 header size
            // APR magic should be detected first
            let data = b"APRNxxxxxxxx0000";
            assert_eq!(detect_format_from_bytes(data), ModelFormat::Apr);
        }

        #[test]
        fn test_detect_format_from_bytes_gguf_takes_priority_over_safetensors() {
            // "GGUF" starts with bytes that could also be a valid u64 header size
            // GGUF magic should be detected first
            let data = b"GGUFxxxxxxxx0000";
            assert_eq!(detect_format_from_bytes(data), ModelFormat::Gguf);
        }

        #[test]
        fn test_detect_format_from_bytes_all_0xff() {
            // All 0xFF bytes: header_size would be u64::MAX which is > 100_000_000
            let data = [0xFFu8; 16];
            assert_eq!(detect_format_from_bytes(&data), ModelFormat::Demo);
        }

        #[test]
        fn test_detect_format_from_bytes_safetensors_typical_sizes() {
            // Typical SafeTensors header sizes
            for size in [256u64, 1024, 8192, 65536, 1_000_000, 50_000_000] {
                let mut data = vec![0u8; 16];
                data[0..8].copy_from_slice(&size.to_le_bytes());
                assert_eq!(
                    detect_format_from_bytes(&data),
                    ModelFormat::SafeTensors,
                    "Expected SafeTensors for header_size={}",
                    size
                );
            }
        }
    }

    // =========================================================================
    // ChatConfig: comprehensive field combination tests
    // =========================================================================

    #[test]
    fn test_chat_config_default_trace_is_false() {
        let config = ChatConfig::default();
        assert!(!config.trace);
    }

    #[test]
    fn test_chat_config_default_trace_output_is_none() {
        let config = ChatConfig::default();
        assert!(config.trace_output.is_none());
    }

    #[test]
    fn test_chat_config_all_fields_set() {
        let config = ChatConfig {
            temperature: 1.5,
            top_p: 0.95,
            max_tokens: 2048,
            system: Some("Expert mode".to_string()),
            inspect: true,
            force_cpu: true,
            trace: true,
            trace_output: Some(PathBuf::from("/tmp/all_fields.json")),
        };
        assert!((config.temperature - 1.5).abs() < f32::EPSILON);
        assert!((config.top_p - 0.95).abs() < f32::EPSILON);
        assert_eq!(config.max_tokens, 2048);
        assert_eq!(config.system.as_deref(), Some("Expert mode"));
        assert!(config.inspect);
        assert!(config.force_cpu);
        assert!(config.trace);
        assert_eq!(
            config.trace_output.as_ref().map(|p| p.to_str().expect("to_str(")),
            Some("/tmp/all_fields.json")
        );
    }

    #[test]
    fn test_chat_config_zero_temperature() {
        // Greedy decoding
        let config = ChatConfig {
            temperature: 0.0,
            ..Default::default()
        };
        assert_eq!(config.temperature, 0.0);
    }

    #[test]
    fn test_chat_config_max_tokens_zero() {
        let config = ChatConfig {
            max_tokens: 0,
            ..Default::default()
        };
        assert_eq!(config.max_tokens, 0);
    }

    #[test]
    fn test_chat_config_max_tokens_large() {
        let config = ChatConfig {
            max_tokens: usize::MAX,
            ..Default::default()
        };
        assert_eq!(config.max_tokens, usize::MAX);
    }

    #[test]
    fn test_chat_config_empty_system_prompt() {
        let config = ChatConfig {
            system: Some(String::new()),
            ..Default::default()
        };
        assert_eq!(config.system.as_deref(), Some(""));
    }

    #[test]
    fn test_chat_config_long_system_prompt() {
        let long_prompt = "a".repeat(10_000);
        let config = ChatConfig {
            system: Some(long_prompt.clone()),
            ..Default::default()
        };
        assert_eq!(config.system.as_deref(), Some(long_prompt.as_str()));
    }

    #[test]
    fn test_chat_config_trace_output_relative_path() {
        let config = ChatConfig {
            trace: true,
            trace_output: Some(PathBuf::from("relative/trace.json")),
            ..Default::default()
        };
        assert!(!config.trace_output.as_ref().expect("as_ref(").is_absolute());
    }

    // =========================================================================
    // run() additional error path tests
    // =========================================================================

    #[test]
    fn test_run_nonexistent_path_without_trace() {
        let path = Path::new("/definitely/not/a/real/path/model.apr");
        let result = run(
            path, 0.7, 0.9, 512, None, false, false, false, None, false, None, "info", false,
        );
        assert!(result.is_err());
        match result.unwrap_err() {
            CliError::FileNotFound(p) => {
                assert_eq!(p, PathBuf::from("/definitely/not/a/real/path/model.apr"));
            }
            other => panic!("Expected FileNotFound, got: {:?}", other),
        }
    }

    #[test]
    fn test_run_nonexistent_safetensors() {
        let path = Path::new("/no/such/model.safetensors");
        let result = run(
            path, 0.5, 0.8, 256, None, false, false, false, None, false, None, "info", false,
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_run_nonexistent_apr() {
        let path = Path::new("/no/such/model.apr");
        let result = run(
            path, 1.0, 1.0, 1024, None, true, true, false, None, false, None, "warn", false,
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_run_nonexistent_with_all_trace_options() {
        let path = Path::new("/no/such/model.gguf");
        let steps = vec![
            "tokenize".to_string(),
            "embed".to_string(),
            "attention".to_string(),
            "ffn".to_string(),
            "sample".to_string(),
            "decode".to_string(),
        ];
        let result = run(
            path,
            0.3,
            0.95,
            128,
            Some("System prompt"),
            true,
            false,
            true,
            Some(&steps),
            true,
            Some(PathBuf::from("/tmp/full_trace.json")),
            "debug",
            true,
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_run_nonexistent_no_system_inspect_off() {
        let path = Path::new("/no/model.bin");
        let result = run(
            path, 0.7, 0.9, 512, None, false, false, false, None, false, None, "info", false,
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_run_nonexistent_with_empty_trace_steps() {
        let path = Path::new("/no/model.gguf");
        let steps: Vec<String> = vec![];
        let result = run(
            path,
            0.7,
            0.9,
            512,
            None,
            false,
            false,
            true,
            Some(&steps),
            false,
            None,
            "info",
            false,
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_run_nonexistent_trace_without_output() {
        let path = Path::new("/no/model.apr");
        let result = run(
            path, 0.7, 0.9, 512, None, false, false, true,  // trace enabled
            None,  // no trace steps
            false, // not verbose
            None,  // no trace output
            "info", false, // no profile
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_run_nonexistent_with_profile_only() {
        let path = Path::new("/no/model.gguf");
        let result = run(
            path, 0.7, 0.9, 512, None, false, false,
            true, // trace must be on for profile to print
            None, false, None, "info", true, // profile enabled
        );
        assert!(result.is_err());
    }