apr-cli 0.4.13

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_chat_config_default() {
        let config = ChatConfig::default();
        assert!((config.temperature - 0.7).abs() < 0.01);
        assert!((config.top_p - 0.9).abs() < 0.01);
        assert_eq!(config.max_tokens, 512);
        assert!(config.system.is_none());
        assert!(!config.inspect);
        // F-GPU-134b: Default to GPU (force_cpu = false)
        assert!(
            !config.force_cpu,
            "F-GPU-134b: force_cpu should default to false"
        );
    }

    #[test]
    fn test_chat_config_with_system_prompt() {
        let config = ChatConfig {
            system: Some("You are a helpful assistant.".to_string()),
            ..Default::default()
        };
        assert!(config.system.is_some());
        assert_eq!(
            config.system.as_ref().unwrap(),
            "You are a helpful assistant."
        );
    }

    #[test]
    fn test_chat_config_trace_settings() {
        let config = ChatConfig {
            trace: true,
            trace_output: Some(PathBuf::from("/tmp/trace.json")),
            ..Default::default()
        };
        assert!(config.trace);
        assert_eq!(
            config.trace_output.as_ref().unwrap().to_str().unwrap(),
            "/tmp/trace.json"
        );
    }

    #[test]
    fn test_chat_config_force_cpu() {
        let config = ChatConfig {
            force_cpu: true,
            ..Default::default()
        };
        assert!(config.force_cpu);
    }

    // F-PIPE-166b: Test tokenizer artifact cleaning
    #[test]
    fn test_clean_chat_response_removes_chatml_markers() {
        let raw = "<|im_start|>assistant\nHello world<|im_end|>";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Hello world");
    }

    #[test]
    fn test_clean_chat_response_removes_bpe_artifacts() {
        // Ġ (U+0120) is used in GPT-2/BPE tokenizers for space prefix
        let raw = "HelloĠworld";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Hello world");
    }

    #[test]
    fn test_clean_chat_response_normalizes_repeated_punctuation() {
        let raw = "Wow!!!!!";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Wow!!!");

        let raw2 = "Really??????";
        let cleaned2 = clean_chat_response(raw2);
        assert_eq!(cleaned2, "Really???");
    }

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

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

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

    #[test]
    fn test_clean_chat_response_newline_artifact() {
        // Ċ (U+010A) represents newline in some BPE tokenizers
        let raw = "HelloĊworld";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Hello\nworld");
    }

    #[test]
    fn test_clean_chat_response_strips_new_turn() {
        // If response has new question after first line, return first line only
        let raw = "4\nSuggest a fun way to learn Rust";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "4");
    }

    #[test]
    fn test_clean_chat_response_keeps_multiline_answer() {
        // Normal multiline response should be preserved
        let raw = "Here is the answer:\nLine 1\nLine 2";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Here is the answer:\nLine 1\nLine 2");
    }

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

    #[test]
    fn test_clean_chat_response_only_markers() {
        let raw = "<|im_start|>assistant<|im_end|>";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "");
    }

    #[test]
    fn test_clean_chat_response_human_prompt_cutoff() {
        // If "Human:" appears after first line, cut it off
        let raw = "Yes\nHuman: What else?";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Yes");
    }

    // =========================================================================
    // ModelFormat tests
    // =========================================================================

    #[test]
    fn test_model_format_equality() {
        assert_eq!(ModelFormat::Apr, ModelFormat::Apr);
        assert_eq!(ModelFormat::Gguf, ModelFormat::Gguf);
        assert_eq!(ModelFormat::SafeTensors, ModelFormat::SafeTensors);
        assert_eq!(ModelFormat::Demo, ModelFormat::Demo);
    }

    #[test]
    fn test_model_format_inequality() {
        assert_ne!(ModelFormat::Apr, ModelFormat::Gguf);
        assert_ne!(ModelFormat::Gguf, ModelFormat::SafeTensors);
        assert_ne!(ModelFormat::SafeTensors, ModelFormat::Demo);
    }

    #[test]
    fn test_model_format_debug() {
        assert_eq!(format!("{:?}", ModelFormat::Apr), "Apr");
        assert_eq!(format!("{:?}", ModelFormat::Gguf), "Gguf");
        assert_eq!(format!("{:?}", ModelFormat::SafeTensors), "SafeTensors");
        assert_eq!(format!("{:?}", ModelFormat::Demo), "Demo");
    }

    #[test]
    fn test_model_format_clone() {
        let format = ModelFormat::Apr;
        let cloned = format;
        assert_eq!(format, cloned);
    }

    #[test]
    fn test_model_format_copy() {
        let format = ModelFormat::Gguf;
        let copied: ModelFormat = format;
        assert_eq!(format, copied);
    }

    // =========================================================================
    // detect_format tests (Y14: format-agnostic)
    // =========================================================================

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

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

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

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

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

    #[test]
    fn test_detect_format_case_sensitive() {
        // Extensions are case-sensitive in Rust
        let path = Path::new("/models/test.APR");
        assert_eq!(detect_format(path), ModelFormat::Demo);
    }

    #[test]
    fn test_detect_format_nested_path() {
        let path = Path::new("/home/user/.cache/models/qwen2-0.5b.gguf");
        assert_eq!(detect_format(path), ModelFormat::Gguf);
    }

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

    // =========================================================================
    // detect_format_from_bytes tests (inference feature only)
    // =========================================================================

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

        #[test]
        fn test_detect_format_from_bytes_apr_v1() {
            let data = b"APRNxxxx00000000";
            assert_eq!(detect_format_from_bytes(data), ModelFormat::Apr);
        }

        #[test]
        fn test_detect_format_from_bytes_apr_v2() {
            let data = b"APR2xxxx00000000";
            assert_eq!(detect_format_from_bytes(data), ModelFormat::Apr);
        }

        #[test]
        fn test_detect_format_from_bytes_apr_null() {
            let data = b"APR\0xxxx00000000";
            assert_eq!(detect_format_from_bytes(data), ModelFormat::Apr);
        }

        #[test]
        fn test_detect_format_from_bytes_gguf() {
            let data = b"GGUFxxxx00000000";
            assert_eq!(detect_format_from_bytes(data), ModelFormat::Gguf);
        }

        #[test]
        fn test_detect_format_from_bytes_safetensors() {
            // SafeTensors: first 8 bytes are little-endian header size
            // A typical header size might be ~1000-10000 bytes
            let mut data = vec![0u8; 16];
            // Write 1000 as little-endian u64 (reasonable header size)
            data[0..8].copy_from_slice(&1000u64.to_le_bytes());
            assert_eq!(detect_format_from_bytes(&data), ModelFormat::SafeTensors);
        }

        #[test]
        fn test_detect_format_from_bytes_too_short() {
            let data = b"APR";
            assert_eq!(detect_format_from_bytes(data), ModelFormat::Demo);
        }

        #[test]
        fn test_detect_format_from_bytes_empty() {
            let data: &[u8] = &[];
            assert_eq!(detect_format_from_bytes(data), ModelFormat::Demo);
        }

        #[test]
        fn test_detect_format_from_bytes_unknown_magic() {
            let data = b"UNKN0000\x00\x00\x00\x00\x00\x00\x00\x00";
            assert_eq!(detect_format_from_bytes(data), ModelFormat::Demo);
        }
    }

    // =========================================================================
    // run() error case tests
    // =========================================================================

    #[test]
    fn test_run_file_not_found() {
        let path = Path::new("/nonexistent/model.gguf");
        let result = run(
            path, 0.7, 0.9, 512, None, false, false, false, None, false, None, "info", false,
        );
        assert!(result.is_err());
        match result {
            Err(CliError::FileNotFound(p)) => {
                assert_eq!(p, PathBuf::from("/nonexistent/model.gguf"));
            }
            other => panic!("Expected FileNotFound error, got {:?}", other),
        }
    }

    #[test]
    fn test_run_with_trace_config() {
        let path = Path::new("/nonexistent/model.gguf");
        // This should still fail with FileNotFound, but trace config is set
        let result = run(
            path,
            0.7,
            0.9,
            512,
            Some("You are helpful"),
            true,
            true,
            true,
            Some(&["tokenize".to_string(), "sample".to_string()]),
            true,
            Some(PathBuf::from("/tmp/trace.json")),
            "debug",
            true,
        );
        assert!(result.is_err());
    }

    // =========================================================================
    // CommandResult tests
    // =========================================================================

    #[test]
    fn test_command_result_variants() {
        // Ensure CommandResult variants exist and can be matched
        let continue_result = CommandResult::Continue;
        let quit_result = CommandResult::Quit;

        match continue_result {
            CommandResult::Continue => {}
            CommandResult::Quit => panic!("Expected Continue"),
        }

        match quit_result {
            CommandResult::Quit => {}
            CommandResult::Continue => panic!("Expected Quit"),
        }
    }

    // =========================================================================
    // Edge case tests for clean_chat_response
    // =========================================================================

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

    #[test]
    fn test_clean_chat_response_repeated_dots() {
        let raw = "Hmm........ let me think";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Hmm... let me think");
    }

    #[test]
    fn test_clean_chat_response_unicode_preserved() {
        let raw = "こんにちは世界";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "こんにちは世界");
    }

    #[test]
    fn test_clean_chat_response_emoji_preserved() {
        let raw = "Hello 👋 World 🌍";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Hello 👋 World 🌍");
    }

    #[test]
    fn test_clean_chat_response_code_block() {
        let raw = "Here is code:\n```rust\nfn main() {}\n```";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Here is code:\n```rust\nfn main() {}\n```");
    }

    #[test]
    fn test_clean_chat_response_numbered_list() {
        let raw = "Steps:\n1. First\n2. Second\n3. Third";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Steps:\n1. First\n2. Second\n3. Third");
    }

    // =========================================================================
    // format_params tests (non-inference only)
    // =========================================================================

    #[cfg(not(feature = "inference"))]
    #[test]
    fn test_format_params_billions() {
        assert_eq!(format_params(7_000_000_000), "7.0B");
        assert_eq!(format_params(1_500_000_000), "1.5B");
    }

    #[cfg(not(feature = "inference"))]
    #[test]
    fn test_format_params_millions() {
        assert_eq!(format_params(500_000_000), "500.0M");
        assert_eq!(format_params(7_000_000), "7.0M");
        assert_eq!(format_params(1_500_000), "1.5M");
    }

    #[cfg(not(feature = "inference"))]
    #[test]
    fn test_format_params_thousands() {
        assert_eq!(format_params(500_000), "500.0K");
        assert_eq!(format_params(7_000), "7.0K");
        assert_eq!(format_params(1_500), "1.5K");
    }

    #[cfg(not(feature = "inference"))]
    #[test]
    fn test_format_params_small() {
        assert_eq!(format_params(999), "999");
        assert_eq!(format_params(100), "100");
        assert_eq!(format_params(1), "1");
        assert_eq!(format_params(0), "0");
    }