apr-cli 0.4.17

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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
use super::*;

// =========================================================================
// execute() error case tests
// =========================================================================

#[test]
fn test_execute_invalid_repo_id_no_slash() {
    let temp_dir = std::env::temp_dir().join("apr_pub_invalid_repo_1");
    let _ = fs::create_dir_all(&temp_dir);

    let result = execute(
        &temp_dir,
        "invalid-repo-name", // No slash
        None,
        "mit",
        "text-generation",
        None,
        &[],
        None,
        true,
        false,
    );

    assert!(result.is_err());
    match result {
        Err(CliError::ValidationFailed(msg)) => {
            assert!(msg.contains("Invalid repo ID"));
            assert!(msg.contains("Expected format: org/repo-name"));
        }
        other => panic!("Expected ValidationFailed, got {:?}", other),
    }

    let _ = fs::remove_dir_all(&temp_dir);
}

#[test]
fn test_execute_invalid_repo_id_too_many_slashes() {
    let temp_dir = std::env::temp_dir().join("apr_pub_invalid_repo_2");
    let _ = fs::create_dir_all(&temp_dir);

    let result = execute(
        &temp_dir,
        "org/repo/extra", // Too many slashes
        None,
        "mit",
        "text-generation",
        None,
        &[],
        None,
        true,
        false,
    );

    assert!(result.is_err());
    match result {
        Err(CliError::ValidationFailed(msg)) => {
            assert!(msg.contains("Invalid repo ID"));
        }
        other => panic!("Expected ValidationFailed, got {:?}", other),
    }

    let _ = fs::remove_dir_all(&temp_dir);
}

#[test]
fn test_execute_directory_not_found() {
    let result = execute(
        Path::new("/nonexistent/directory"),
        "paiml/test-model",
        None,
        "mit",
        "text-generation",
        None,
        &[],
        None,
        true,
        false,
    );

    assert!(result.is_err());
    match result {
        Err(CliError::FileNotFound(_)) => {}
        other => panic!("Expected FileNotFound, got {:?}", other),
    }
}

#[test]
fn test_execute_no_model_files() {
    let temp_dir = std::env::temp_dir().join("apr_pub_no_models");
    let _ = fs::create_dir_all(&temp_dir);
    // Create non-model files
    let txt_file = temp_dir.join("readme.txt");
    let _ = fs::write(&txt_file, "test");

    let result = execute(
        &temp_dir,
        "paiml/test-model",
        None,
        "mit",
        "text-generation",
        None,
        &[],
        None,
        true,
        false,
    );

    assert!(result.is_err());
    match result {
        Err(CliError::ValidationFailed(msg)) => {
            assert!(msg.contains("No model files found"));
        }
        other => panic!("Expected ValidationFailed, got {:?}", other),
    }

    let _ = fs::remove_dir_all(&temp_dir);
}

#[test]
fn test_execute_dry_run_success() {
    let temp_dir = std::env::temp_dir().join("apr_pub_dry_run");
    let _ = fs::create_dir_all(&temp_dir);

    // Create a model file
    let model_file = temp_dir.join("model.apr");
    let _ = fs::write(&model_file, "APR2test");

    let result = execute(
        &temp_dir,
        "paiml/test-model",
        Some("My Test Model"),
        "apache-2.0",
        "text-generation",
        Some("aprender"),
        &["rust".to_string(), "transformer".to_string()],
        Some("Test commit"),
        true, // dry_run
        true, // verbose
    );

    assert!(result.is_ok());

    let _ = fs::remove_dir_all(&temp_dir);
}

// =========================================================================
// find_model_files() tests
// =========================================================================

#[test]
fn test_find_model_files_empty() {
    let temp_dir = std::env::temp_dir().join("apr_publish_test_empty");
    let _ = fs::create_dir_all(&temp_dir);

    let files = find_model_files(&temp_dir).unwrap();
    assert!(files.is_empty());

    let _ = fs::remove_dir_all(&temp_dir);
}

#[test]
fn test_find_model_files_apr() {
    let temp_dir = std::env::temp_dir().join("apr_pub_find_apr");
    let _ = fs::create_dir_all(&temp_dir);

    let apr_file = temp_dir.join("model.apr");
    let _ = fs::write(&apr_file, "APR2");

    let files = find_model_files(&temp_dir).unwrap();
    assert_eq!(files.len(), 1);
    assert!(files[0].ends_with("model.apr"));

    let _ = fs::remove_dir_all(&temp_dir);
}

#[test]
fn test_find_model_files_safetensors() {
    let temp_dir = std::env::temp_dir().join("apr_pub_find_st");
    let _ = fs::create_dir_all(&temp_dir);

    let st_file = temp_dir.join("model.safetensors");
    let _ = fs::write(&st_file, "safetensors");

    let files = find_model_files(&temp_dir).unwrap();
    assert_eq!(files.len(), 1);
    assert!(files[0].ends_with("model.safetensors"));

    let _ = fs::remove_dir_all(&temp_dir);
}

#[test]
fn test_find_model_files_gguf() {
    let temp_dir = std::env::temp_dir().join("apr_pub_find_gguf");
    let _ = fs::create_dir_all(&temp_dir);

    let gguf_file = temp_dir.join("model.gguf");
    let _ = fs::write(&gguf_file, "GGUF");

    let files = find_model_files(&temp_dir).unwrap();
    assert_eq!(files.len(), 1);
    assert!(files[0].ends_with("model.gguf"));

    let _ = fs::remove_dir_all(&temp_dir);
}

#[test]
fn test_find_model_files_multiple_formats() {
    let temp_dir = std::env::temp_dir().join("apr_pub_find_multi");
    let _ = fs::create_dir_all(&temp_dir);

    let _ = fs::write(temp_dir.join("model.apr"), "APR2");
    let _ = fs::write(temp_dir.join("model.safetensors"), "st");
    let _ = fs::write(temp_dir.join("model.gguf"), "GGUF");
    let _ = fs::write(temp_dir.join("readme.txt"), "ignored");

    let files = find_model_files(&temp_dir).unwrap();
    assert_eq!(files.len(), 3);
    // Files are sorted alphabetically
    assert!(files[0].ends_with("model.apr"));
    assert!(files[1].ends_with("model.gguf"));
    assert!(files[2].ends_with("model.safetensors"));

    let _ = fs::remove_dir_all(&temp_dir);
}

#[test]
fn test_find_model_files_ignores_non_model_files() {
    let temp_dir = std::env::temp_dir().join("apr_pub_find_ignore");
    let _ = fs::create_dir_all(&temp_dir);

    let _ = fs::write(temp_dir.join("model.txt"), "text");
    let _ = fs::write(temp_dir.join("config.json"), "{}");
    let _ = fs::write(temp_dir.join("tokenizer.json"), "{}");
    let _ = fs::write(temp_dir.join("README.md"), "# Readme");

    let files = find_model_files(&temp_dir).unwrap();
    assert!(files.is_empty());

    let _ = fs::remove_dir_all(&temp_dir);
}

#[test]
fn test_find_model_files_case_insensitive() {
    let temp_dir = std::env::temp_dir().join("apr_pub_find_case");
    let _ = fs::create_dir_all(&temp_dir);

    // Extensions are case-insensitive (APR, GGUF, SAFETENSORS work too)
    let _ = fs::write(temp_dir.join("model.APR"), "APR2");
    let _ = fs::write(temp_dir.join("model.GGUF"), "GGUF");

    let files = find_model_files(&temp_dir).unwrap();
    assert_eq!(files.len(), 2);

    let _ = fs::remove_dir_all(&temp_dir);
}

// =========================================================================
// generate_model_card() tests
// =========================================================================

#[test]
fn test_generate_model_card() {
    let (card, file_names) = generate_model_card(
        "paiml/test-model",
        Some("Test Model"),
        "mit",
        "text-generation",
        None,
        &[],
        &[],
    );

    assert_eq!(card.model_id, "paiml/test-model");
    assert_eq!(card.name, "Test Model");
    assert_eq!(card.license, Some("mit".to_string()));
    assert!(file_names.is_empty());
}

#[test]
fn test_generate_model_card_default_name() {
    let (card, _) = generate_model_card(
        "paiml/my-awesome-model",
        None, // No explicit name
        "apache-2.0",
        "text-generation",
        None,
        &[],
        &[],
    );

    // Should use last part of repo_id as name
    assert_eq!(card.name, "my-awesome-model");
}

#[test]
fn test_generate_model_card_description_generated() {
    let (card, _) = generate_model_card(
        "paiml/whisper-tiny",
        Some("Whisper Tiny"),
        "mit",
        "automatic-speech-recognition",
        Some("whisper"),
        &["speech".to_string()],
        &[],
    );

    assert!(card.description.is_some());
    assert!(card.description.unwrap().contains("Whisper Tiny"));
}

#[test]
fn test_generate_model_card_captures_file_names() {
    let files = vec![
        std::path::PathBuf::from("/tmp/model.gguf"),
        std::path::PathBuf::from("/tmp/model.safetensors"),
    ];
    let (_, file_names) = generate_model_card(
        "paiml/test",
        None,
        "mit",
        "text-generation",
        None,
        &[],
        &files,
    );

    assert_eq!(file_names, vec!["model.gguf", "model.safetensors"]);
}

// =========================================================================
// ModelCardExt::to_huggingface_extended() tests
// =========================================================================

#[test]
fn test_model_card_extended_asr() {
    let card = ModelCard::new("paiml/whisper-test", "1.0.0")
        .with_name("Whisper Test")
        .with_license("MIT");

    let output = card.to_huggingface_extended(
        "automatic-speech-recognition",
        Some("whisper-apr"),
        &["whisper".to_string()],
        &[],
    );

    assert!(output.contains("pipeline_tag: automatic-speech-recognition"));
    assert!(output.contains("library_name: whisper-apr"));
    assert!(output.contains("- speech-recognition"));
    assert!(output.contains("- whisper"));
}

#[test]
fn test_model_card_extended_text_generation() {
    let card = ModelCard::new("paiml/gpt-test", "1.0.0")
        .with_name("GPT Test")
        .with_license("apache-2.0");

    let output = card.to_huggingface_extended(
        "text-generation",
        Some("aprender"),
        &["transformer".to_string(), "causal-lm".to_string()],
        &[],
    );

    assert!(output.contains("pipeline_tag: text-generation"));
    assert!(output.contains("library_name: aprender"));
    assert!(output.contains("- transformer"));
    assert!(output.contains("- causal-lm"));
    assert!(output.contains("- aprender"));
    assert!(output.contains("- rust"));
    // Should NOT have ASR-specific tags
    assert!(!output.contains("- speech-recognition"));
}

#[test]
fn test_model_card_extended_yaml_front_matter() {
    let card = ModelCard::new("paiml/test", "1.0.0")
        .with_name("Test")
        .with_license("mit");

    let output = card.to_huggingface_extended("text-generation", None, &[], &[]);

    // Should start with YAML front matter
    assert!(output.starts_with("---\n"));
    assert!(output.contains("\n---\n\n"));
}

#[test]
fn test_model_card_extended_contains_sections() {
    let card = ModelCard::new("paiml/test", "1.0.0")
        .with_name("Test Model")
        .with_license("mit");

    let output = card.to_huggingface_extended("text-generation", None, &[], &[]);

    // Should contain all expected sections
    assert!(output.contains("# Test Model"));
    assert!(output.contains("## Available Formats"));
    assert!(output.contains("## Usage"));
    assert!(output.contains("## Framework"));
    assert!(output.contains("## Citation"));
}

#[test]
fn test_model_card_extended_code_example() {
    let card = ModelCard::new("paiml/test", "1.0.0").with_name("Test");

    let output = card.to_huggingface_extended("text-generation", None, &[], &[]);

    // Should contain Rust code example
    assert!(output.contains("```rust"));
    assert!(output.contains("use aprender::Model;"));
    assert!(output.contains("Model::load"));
}

#[test]
fn test_model_card_extended_bibtex_citation() {
    let card = ModelCard::new("paiml/test", "1.0.0").with_name("Test");

    let output = card.to_huggingface_extended("text-generation", None, &[], &[]);

    assert!(output.contains("```bibtex"));
    assert!(output.contains("@software{aprender,"));
    assert!(output.contains("title = {aprender: Rust ML Library}"));
}

#[test]
fn test_model_card_extended_model_index() {
    let card = ModelCard::new("paiml/test-model", "1.0.0").with_name("Test Model");

    let output = card.to_huggingface_extended("text-generation", None, &[], &[]);

    assert!(output.contains("model-index:"));
    assert!(output.contains("- name: paiml/test-model"));
    assert!(output.contains("type: text-generation"));
}

#[test]
fn test_model_card_extended_no_library_name() {
    let card = ModelCard::new("paiml/test", "1.0.0").with_name("Test");

    let output = card.to_huggingface_extended(
        "text-generation",
        None, // No library name
        &[],
        &[],
    );

    // Should NOT contain library_name field
    assert!(!output.contains("library_name:"));
}

#[test]
fn test_model_card_extended_deduplicated_tags() {
    let card = ModelCard::new("paiml/test", "1.0.0").with_name("Test");

    let output = card.to_huggingface_extended(
        "text-generation",
        None,
        &[
            "rust".to_string(),     // Already added by default
            "aprender".to_string(), // Already added by default
            "custom".to_string(),   // New tag
        ],
        &[],
    );

    // Count occurrences of "- rust" (should be exactly 1)
    let rust_count = output.matches("  - rust\n").count();
    assert_eq!(rust_count, 1, "rust tag should appear exactly once");

    let aprender_count = output.matches("  - aprender\n").count();
    assert_eq!(aprender_count, 1, "aprender tag should appear exactly once");

    assert!(output.contains("  - custom\n"));
}

#[test]
fn test_model_card_extended_multilingual_asr() {
    let card = ModelCard::new("paiml/whisper", "1.0.0").with_name("Whisper");

    let output = card.to_huggingface_extended("automatic-speech-recognition", None, &[], &[]);

    // ASR models should have language specification
    assert!(output.contains("language:"));
    assert!(output.contains("  - en"));
    assert!(output.contains("  - multilingual"));
}

#[test]
fn test_model_card_extended_with_architecture() {
    let card = ModelCard::new("paiml/test", "1.0.0")
        .with_name("Test")
        .with_architecture("transformer");

    let output = card.to_huggingface_extended("text-generation", None, &[], &[]);

    // Architecture should appear in tags
    assert!(output.contains("  - transformer\n"));
}

// GH-511: Test that Available Formats section uses actual file names
#[test]
fn test_model_card_extended_dynamic_formats() {
    let card = ModelCard::new("paiml/test", "1.0.0").with_name("Test");
    let file_names = vec!["model.gguf".to_string(), "weights.safetensors".to_string()];

    let output = card.to_huggingface_extended("text-generation", None, &[], &file_names);

    // Should contain actual file names, not hardcoded defaults
    assert!(output.contains("| `model.gguf` | GGUF format (llama.cpp compatible) |"));
    assert!(output.contains("| `weights.safetensors` | HuggingFace SafeTensors format |"));
    // Available Formats table should NOT contain hardcoded model.apr when not in file list
    // (Note: model.apr still appears in the Usage code example section, so check table specifically)
    let formats_section = output.split("## Available Formats").nth(1).unwrap();
    let formats_table = formats_section.split("## Usage").next().unwrap();
    assert!(!formats_table.contains("model.apr"));
}

#[test]
fn test_model_card_extended_empty_files_fallback() {
    let card = ModelCard::new("paiml/test", "1.0.0").with_name("Test");

    let output = card.to_huggingface_extended("text-generation", None, &[], &[]);

    // Empty file list should show fallback
    assert!(output.contains("model.apr"));
}