apr-cli 0.4.15

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 verbose inheritance end-to-end with -v after the subcommand.
    /// Because clap uses `global = true` + `short = 'v'` on both Cli and
    /// Run, -v placed after the subcommand sets the global verbose field.
    #[test]
    fn test_verbose_inheritance_run_local() {
        let args = vec!["apr", "run", "model.gguf", "--prompt", "test", "-v"];
        let cli = parse_cli(args).expect("Failed to parse");
        // -v after the subcommand still sets the global flag due to global = true
        match *cli.command {
            Commands::Run { verbose, .. } => {
                let effective = verbose || cli.verbose;
                assert!(effective, "effective verbose should be true");
            }
            _ => panic!("Expected Run command"),
        }
    }

    // =========================================================================
    // Edge case: conflicting flags (--gpu vs --no-gpu)
    // =========================================================================

    /// Test that --gpu and --no-gpu conflict (Run command)
    #[test]
    fn test_parse_run_gpu_nogpu_conflict() {
        let args = vec![
            "apr",
            "run",
            "model.gguf",
            "--prompt",
            "test",
            "--gpu",
            "--no-gpu",
        ];
        let result = parse_cli(args);
        assert!(result.is_err(), "--gpu and --no-gpu should conflict in Run");
    }

    /// Test parsing 'apr run' with --gpu flag alone
    #[test]
    fn test_parse_run_gpu_only() {
        let args = vec!["apr", "run", "model.gguf", "--prompt", "test", "--gpu"];
        let cli = parse_cli(args).expect("Failed to parse");
        match *cli.command {
            Commands::Run { gpu, no_gpu, .. } => {
                assert!(gpu);
                assert!(!no_gpu);
            }
            _ => panic!("Expected Run command"),
        }
    }

    // =========================================================================
    // Missing required args error tests
    // =========================================================================

    /// Test that 'apr serve' without FILE fails
    #[test]
    fn test_missing_serve_file() {
        let args = vec!["apr", "serve"];
        let result = parse_cli(args);
        assert!(result.is_err(), "serve requires FILE");
    }

    /// Test that 'apr diff' with only one file fails
    #[test]
    fn test_missing_diff_second_file() {
        let args = vec!["apr", "diff", "model1.apr"];
        let result = parse_cli(args);
        assert!(result.is_err(), "diff requires two files");
    }

    /// Test that 'apr export' without output fails
    #[test]
    fn test_missing_export_output() {
        // Output is now optional at parse level (validated at runtime)
        // so parse succeeds, but execution should fail with ValidationFailed
        let args = vec!["apr", "export", "model.apr"];
        let result = parse_cli(args);
        assert!(result.is_ok(), "export parses without -o (validated at runtime)");
    }

    /// Test that 'apr convert' without output fails
    #[test]
    fn test_missing_convert_output() {
        let args = vec!["apr", "convert", "model.apr"];
        let result = parse_cli(args);
        assert!(result.is_err(), "convert requires -o/--output");
    }

    /// Test that 'apr merge' with fewer than 2 files fails
    #[test]
    fn test_missing_merge_files() {
        let args = vec!["apr", "merge", "model1.apr", "-o", "out.apr"];
        let result = parse_cli(args);
        assert!(result.is_err(), "merge requires at least 2 files");
    }

    /// Test that 'apr publish' without repo_id fails
    #[test]
    fn test_missing_publish_repo_id() {
        let args = vec!["apr", "publish", "/tmp/models"];
        let result = parse_cli(args);
        assert!(result.is_err(), "publish requires REPO_ID");
    }

    /// Test that 'apr pull' without model_ref fails
    #[test]
    fn test_missing_pull_model_ref() {
        let args = vec!["apr", "pull"];
        let result = parse_cli(args);
        assert!(result.is_err(), "pull requires MODEL");
    }

    /// Test that 'apr rm' without model_ref fails
    #[test]
    fn test_missing_rm_model_ref() {
        let args = vec!["apr", "rm"];
        let result = parse_cli(args);
        assert!(result.is_err(), "rm requires MODEL");
    }

    /// Test that 'apr compare-hf' without --hf fails
    #[test]
    fn test_missing_compare_hf_hf_arg() {
        let args = vec!["apr", "compare-hf", "model.apr"];
        let result = parse_cli(args);
        assert!(result.is_err(), "compare-hf requires --hf");
    }

    /// Test that 'apr canary create' without --input fails
    #[test]
    fn test_missing_canary_create_input() {
        let args = vec![
            "apr",
            "canary",
            "create",
            "model.apr",
            "--output",
            "canary.json",
        ];
        let result = parse_cli(args);
        assert!(result.is_err(), "canary create requires --input");
    }

    /// Test that 'apr canary check' without --canary fails
    #[test]
    fn test_missing_canary_check_canary() {
        let args = vec!["apr", "canary", "check", "model.apr"];
        let result = parse_cli(args);
        assert!(result.is_err(), "canary check requires --canary");
    }

    // =========================================================================
    // execute_command: contract gate integration
    // =========================================================================

    /// Test that execute_command with skip_contract=false and non-existent paths
    /// still works because non-existent paths are skipped in validate_model_contract
    #[test]
    fn test_execute_with_contract_gate_nonexistent() {
        let cli = Cli {
            command: Box::new(Commands::Inspect {
                file: PathBuf::from("/tmp/nonexistent_contract_test.apr"),
                vocab: false,
                filters: false,
                weights: false,
                json: false,
            }),
            json: false,
            verbose: false,
            quiet: false,
            offline: false,
            skip_contract: false, // Contract enabled, but paths don't exist
        };
        // The contract gate should pass (non-existent paths are skipped),
        // but the command itself should fail (file not found)
        let result = execute_command(&cli);
        assert!(
            result.is_err(),
            "Should still fail from command execution, not contract"
        );
    }

    /// Test that execute_command dispatches List even with contract enabled
    #[test]
    fn test_execute_list_with_contract_enabled() {
        let cli = Cli {
            command: Box::new(Commands::List),
            json: false,
            verbose: false,
            quiet: false,
            offline: false,
            skip_contract: false, // Contract enabled
        };
        let result = execute_command(&cli);
        assert!(result.is_ok(), "List should succeed with contract enabled");
    }

    // =========================================================================
    // Rosetta command execution error paths
    // =========================================================================

    /// Test execute_command: Rosetta inspect with non-existent file returns error
    #[test]
    fn test_execute_rosetta_inspect_file_not_found() {
        let cli = make_cli(Commands::Extended(ExtendedCommands::Tools(ToolCommands::Rosetta {
            action: RosettaCommands::Inspect {
                file: PathBuf::from("/tmp/nonexistent_rosetta_inspect.gguf"),
                hexdump: false,
                json: false,
            },
        })));
        let result = execute_command(&cli);
        assert!(
            result.is_err(),
            "Rosetta inspect should fail with non-existent file"
        );
    }

    /// Test execute_command: Rosetta convert with non-existent source returns error
    #[test]
    fn test_execute_rosetta_convert_file_not_found() {
        let cli = make_cli(Commands::Extended(ExtendedCommands::Tools(ToolCommands::Rosetta {
            action: RosettaCommands::Convert {
                source: PathBuf::from("/tmp/nonexistent_rosetta_convert.gguf"),
                target: PathBuf::from("/tmp/out.safetensors"),
                quantize: None,
                verify: false,
                json: false,
                tokenizer: None,
            },
        })));
        let result = execute_command(&cli);
        assert!(
            result.is_err(),
            "Rosetta convert should fail with non-existent source"
        );
    }

    /// Test execute_command: Rosetta fingerprint with non-existent file returns error
    #[test]
    fn test_execute_rosetta_fingerprint_file_not_found() {
        let cli = make_cli(Commands::Extended(ExtendedCommands::Tools(ToolCommands::Rosetta {
            action: RosettaCommands::Fingerprint {
                model: PathBuf::from("/tmp/nonexistent_rosetta_fingerprint.gguf"),
                model_b: None,
                output: None,
                filter: None,
                verbose: false,
                json: false,
            },
        })));
        let result = execute_command(&cli);
        assert!(
            result.is_err(),
            "Rosetta fingerprint should fail with non-existent file"
        );
    }

    /// Test execute_command: Bench with non-existent file returns error
    #[test]
    fn test_execute_bench_file_not_found() {
        let cli = make_cli(Commands::Extended(ExtendedCommands::Bench {
            file: PathBuf::from("/tmp/nonexistent_model_bench_test.gguf"),
            warmup: 1,
            iterations: 1,
            max_tokens: 1,
            prompt: None,
            fast: false,
            brick: None,
        }));
        let result = execute_command(&cli);
        assert!(result.is_err(), "Bench should fail with non-existent file");
    }

    /// Test execute_command: Eval with non-existent file returns error
    #[test]
    fn test_execute_eval_file_not_found() {
        let cli = make_cli(Commands::Extended(ExtendedCommands::Eval {
            file: PathBuf::from("/tmp/nonexistent_model_eval_test.gguf"),
            dataset: "wikitext-2".to_string(),
            text: None,
            max_tokens: 32,
            threshold: 20.0,
            task: None,
            data: None,
            model_size: None,
            num_classes: 5,
            generate_card: false,
            device: "cpu".to_string(),
            samples: 1,
            temperature: 0.0,
        }));
        let result = execute_command(&cli);
        assert!(result.is_err(), "Eval should fail with non-existent file");
    }

    /// Test execute_command: Profile with non-existent file returns error
    #[test]
    fn test_execute_profile_file_not_found() {
        let cli = make_cli(Commands::Extended(ExtendedCommands::Profile {
            file: PathBuf::from("/tmp/nonexistent_model_profile_test.apr"),
            granular: false,
            format: "human".to_string(),
            focus: None,
            detect_naive: false,
            threshold: 10.0,
            compare_hf: None,
            energy: false,
            perf_grade: false,
            callgraph: false,
            fail_on_naive: false,
            output: None,
            ci: false,
            assert_throughput: None,
            assert_p99: None,
            assert_p50: None,
            warmup: 3,
            measure: 10,
            tokens: 32,
            ollama: false,
            no_gpu: false,
            compare: None,
        }));
        let result = execute_command(&cli);
        assert!(
            result.is_err(),
            "Profile should fail with non-existent file"
        );
    }

    /// Test execute_command: CompareHf with non-existent file returns error
    #[test]
    fn test_execute_compare_hf_file_not_found() {
        let cli = make_cli(Commands::Extended(ExtendedCommands::CompareHf {
            file: PathBuf::from("/tmp/nonexistent_model_compare_hf_test.apr"),
            hf: "openai/whisper-tiny".to_string(),
            tensor: None,
            threshold: 1e-5,
            json: false,
        }));
        let result = execute_command(&cli);
        assert!(
            result.is_err(),
            "CompareHf should fail with non-existent file"
        );
    }

    /// Test execute_command: Canary check with non-existent file returns error
    #[test]
    fn test_execute_canary_check_file_not_found() {
        let cli = make_cli(Commands::Canary {
            command: CanaryCommands::Check {
                file: PathBuf::from("/tmp/nonexistent_canary_check.apr"),
                canary: PathBuf::from("/tmp/nonexistent_canary.json"),
            },
        });
        let result = execute_command(&cli);
        assert!(
            result.is_err(),
            "Canary check should fail with non-existent file"
        );
    }

    /// Test execute_command: Publish with non-existent directory returns error
    #[test]
    fn test_execute_publish_dir_not_found() {
        let cli = make_cli(Commands::Extended(ExtendedCommands::Tools(ToolCommands::Publish {
            directory: PathBuf::from("/tmp/nonexistent_publish_dir_test"),
            repo_id: "test/test".to_string(),
            model_name: None,
            license: "mit".to_string(),
            pipeline_tag: "text-generation".to_string(),
            library_name: None,
            tags: None,
            message: None,
            dry_run: true, // Use dry_run to avoid actual upload
            plan: false,
        })));
        let result = execute_command(&cli);
        assert!(
            result.is_err(),
            "Publish should fail with non-existent directory"
        );
    }

    // =========================================================================
    // Default value verification tests
    // =========================================================================

    /// Test Run command defaults
    #[test]
    fn test_parse_run_defaults() {
        let args = vec!["apr", "run", "model.gguf"];
        let cli = parse_cli(args).expect("Failed to parse");
        match *cli.command {
            Commands::Run {
                max_tokens,
                stream,
                format,
                no_gpu,
                gpu,
                offline,
                benchmark,
                trace,
                trace_payload,
                trace_verbose,
                trace_level,
                profile,
                chat,
                verbose,
                prompt,
                input,
                language,
                task,
                trace_steps,
                trace_output,
                ..
            } => {
                assert_eq!(max_tokens, 32);
                assert!(!stream);
                assert_eq!(format, "text");
                assert!(!no_gpu);
                assert!(!gpu);
                assert!(!offline);
                assert!(!benchmark);
                assert!(!trace);
                assert!(!trace_payload);
                assert!(!trace_verbose);
                assert_eq!(trace_level, "basic");
                assert!(!profile);
                assert!(!chat);
                assert!(!verbose);
                assert!(prompt.is_none());
                assert!(input.is_none());
                assert!(language.is_none());
                assert!(task.is_none());
                assert!(trace_steps.is_none());
                assert!(trace_output.is_none());
            }
            _ => panic!("Expected Run command"),
        }
    }