fontlift-cli 5.0.10

CLI interface for fontlift
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
use super::*;
use clap_complete::Shell;
use fontlift_core::{FontError, FontManager, FontScope, FontliftFontFaceInfo, FontliftFontSource};
use serde_json::Value;
use std::fs;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use tokio::runtime::Runtime;

#[test]
fn test_cli_parsing() {
    use clap::Parser;

    let cli = Cli::try_parse_from(&["fontlift", "list", "-p"]).unwrap();
    match cli.command {
        Commands::List { path, name, sorted } => {
            assert!(path);
            assert!(!name);
            assert!(!sorted);
        }
        _ => panic!("Expected list command"),
    }
}

fn sample_font(path: &str, postscript: &str) -> FontliftFontFaceInfo {
    FontliftFontFaceInfo::new(
        FontliftFontSource::new(PathBuf::from(path)),
        postscript.to_string(),
        postscript.to_string(),
        "Family".to_string(),
        "Regular".to_string(),
    )
}

#[test]
fn list_renders_json_sorted_and_deduped() {
    let fonts = vec![
        sample_font("/fonts/Zeta.ttf", "Zeta"),
        sample_font("/fonts/Alpha.ttf", "Alpha-Regular"),
        sample_font("/fonts/Alpha.ttf", "Alpha-Regular"), // duplicate
        sample_font("/fonts/Beta.ttf", "Beta-Bold"),
    ];

    let opts = ListRenderOptions {
        show_path: true,
        show_name: true,
        sorted: true,
        json: true,
    };

    let output = render_list_output(fonts, opts).expect("render");

    let json = match output {
        ListRender::Json(s) => s,
        _ => panic!("expected json output"),
    };

    let parsed: Vec<Value> = serde_json::from_str(&json).expect("valid json");
    let names: Vec<&str> = parsed
        .iter()
        .map(|v| v["postscript_name"].as_str().unwrap())
        .collect();

    assert_eq!(
        names,
        vec!["Alpha-Regular", "Beta-Bold", "Zeta"],
        "sorted deterministically with duplicates removed"
    );
}

#[test]
fn list_renders_lines_sorted_and_deduped_by_default() {
    let fonts = vec![
        sample_font("/fonts/Beta.ttf", "Beta-Bold"),
        sample_font("/fonts/Alpha.ttf", "Alpha-Regular"),
        sample_font("/fonts/Alpha.ttf", "Alpha-Regular"),
    ];

    let opts = ListRenderOptions {
        show_path: true,
        show_name: false,
        sorted: false,
        json: false,
    };

    let output = render_list_output(fonts, opts).expect("render");
    let lines = match output {
        ListRender::Lines(lines) => lines,
        _ => panic!("expected line output"),
    };

    assert_eq!(
        lines,
        vec![
            "/fonts/Alpha.ttf".to_string(),
            "/fonts/Beta.ttf".to_string()
        ],
        "dedupes identical paths and sorts deterministically"
    );
}

#[test]
fn list_renders_name_only_sorted_by_default() {
    let fonts = vec![
        sample_font("/fonts/Delta.ttf", "Delta"),
        sample_font("/fonts/Alpha.ttf", "Alpha-Regular"),
        sample_font("/fonts/Beta.ttf", "Beta-Bold"),
    ];

    let opts = ListRenderOptions {
        show_path: false,
        show_name: true,
        sorted: false,
        json: false,
    };

    let output = render_list_output(fonts, opts).expect("render");
    let lines = match output {
        ListRender::Lines(lines) => lines,
        _ => panic!("expected line output"),
    };

    assert_eq!(
        lines,
        vec![
            "Alpha-Regular".to_string(),
            "Beta-Bold".to_string(),
            "Delta".to_string()
        ],
        "sorts names deterministically even without --sorted"
    );
}

#[test]
fn collect_font_inputs_scans_directories_and_dedupes() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let alpha = tmp.path().join("Alpha.ttf");
    let beta = tmp.path().join("Beta.otf");
    fs::write(&alpha, b"test").expect("write alpha");
    fs::write(&beta, b"test").expect("write beta");

    // Provide both a directory and a direct file reference to ensure deduplication
    let inputs = vec![tmp.path().to_path_buf(), beta.clone()];
    let collected = collect_font_inputs(&inputs).expect("collect");

    assert_eq!(collected, vec![alpha.clone(), beta.clone()]);
}

#[derive(Default)]
struct RecordingManager {
    installs: Mutex<Vec<(PathBuf, FontScope)>>,
    prunes: Mutex<Vec<FontScope>>,
    cache_clears: Mutex<Vec<FontScope>>,
}

impl FontManager for RecordingManager {
    fn install_font(&self, source: &FontliftFontSource) -> fontlift_core::FontResult<()> {
        let scope = source.scope.unwrap_or(FontScope::User);
        self.installs
            .lock()
            .expect("lock")
            .push((source.path.clone(), scope));
        Ok(())
    }

    fn uninstall_font(&self, _source: &FontliftFontSource) -> fontlift_core::FontResult<()> {
        Ok(())
    }

    fn remove_font(&self, _source: &FontliftFontSource) -> fontlift_core::FontResult<()> {
        Ok(())
    }

    fn is_font_installed(&self, _source: &FontliftFontSource) -> fontlift_core::FontResult<bool> {
        Ok(false)
    }

    fn list_installed_fonts(&self) -> fontlift_core::FontResult<Vec<FontliftFontFaceInfo>> {
        Ok(Vec::new())
    }

    fn clear_font_caches(&self, _scope: FontScope) -> fontlift_core::FontResult<()> {
        self.cache_clears.lock().expect("lock").push(_scope);
        Ok(())
    }

    fn prune_missing_fonts(&self, scope: FontScope) -> fontlift_core::FontResult<usize> {
        self.prunes.lock().expect("lock").push(scope);
        Ok(0)
    }
}

#[derive(Default)]
struct ScopedUninstallManager {
    uninstall_scopes: Mutex<Vec<FontScope>>,
}

impl ScopedUninstallManager {
    fn scopes_called(&self) -> Vec<FontScope> {
        self.uninstall_scopes.lock().expect("lock").clone()
    }
}

impl FontManager for ScopedUninstallManager {
    fn install_font(&self, _source: &FontliftFontSource) -> fontlift_core::FontResult<()> {
        Ok(())
    }

    fn uninstall_font(&self, source: &FontliftFontSource) -> fontlift_core::FontResult<()> {
        let scope = source.scope.unwrap_or(FontScope::User);
        self.uninstall_scopes.lock().expect("lock").push(scope);

        match scope {
            FontScope::System => Ok(()),
            FontScope::User => Err(FontError::RegistrationFailed(
                "not installed in user scope".to_string(),
            )),
        }
    }

    fn remove_font(&self, _source: &FontliftFontSource) -> fontlift_core::FontResult<()> {
        Ok(())
    }

    fn is_font_installed(&self, _source: &FontliftFontSource) -> fontlift_core::FontResult<bool> {
        Ok(true)
    }

    fn list_installed_fonts(&self) -> fontlift_core::FontResult<Vec<FontliftFontFaceInfo>> {
        Ok(vec![FontliftFontFaceInfo::new(
            FontliftFontSource::new(PathBuf::from("/Library/Fonts/ScopedUninstall.ttf"))
                .with_scope(None),
            "ScopedUninstall".to_string(),
            "Scoped Uninstall".to_string(),
            "Scoped".to_string(),
            "Regular".to_string(),
        )])
    }

    fn clear_font_caches(&self, _scope: FontScope) -> fontlift_core::FontResult<()> {
        Ok(())
    }

    fn prune_missing_fonts(&self, _scope: FontScope) -> fontlift_core::FontResult<usize> {
        Ok(0)
    }
}

#[derive(Default)]
struct DenyCacheManager {
    prunes: Mutex<usize>,
    cache_attempts: Mutex<usize>,
}

impl FontManager for DenyCacheManager {
    fn install_font(&self, _source: &FontliftFontSource) -> fontlift_core::FontResult<()> {
        Err(FontError::UnsupportedOperation(
            "install not used in test".into(),
        ))
    }

    fn uninstall_font(&self, _source: &FontliftFontSource) -> fontlift_core::FontResult<()> {
        Err(FontError::UnsupportedOperation(
            "uninstall not used in test".into(),
        ))
    }

    fn remove_font(&self, _source: &FontliftFontSource) -> fontlift_core::FontResult<()> {
        Err(FontError::UnsupportedOperation(
            "remove not used in test".into(),
        ))
    }

    fn is_font_installed(&self, _source: &FontliftFontSource) -> fontlift_core::FontResult<bool> {
        Ok(false)
    }

    fn list_installed_fonts(&self) -> fontlift_core::FontResult<Vec<FontliftFontFaceInfo>> {
        Ok(vec![])
    }

    fn clear_font_caches(&self, _scope: FontScope) -> fontlift_core::FontResult<()> {
        *self.cache_attempts.lock().expect("lock") += 1;
        Err(FontError::PermissionDenied(
            "cache clearing requires admin".to_string(),
        ))
    }

    fn prune_missing_fonts(&self, _scope: FontScope) -> fontlift_core::FontResult<usize> {
        *self.prunes.lock().expect("lock") += 1;
        Ok(1)
    }
}

#[test]
fn dry_run_install_skips_invoking_manager() {
    let runtime = Runtime::new().expect("runtime");
    let tmp = tempfile::tempdir().expect("tempdir");
    let font = tmp.path().join("DryRun.ttf");
    fs::write(&font, b"test").expect("write font");

    let manager = Arc::new(RecordingManager::default());
    let opts = OperationOptions::new(true, true, false);

    runtime
        .block_on(handle_install_command(
            manager.clone(),
            vec![font.clone()],
            false,
            false, // no validation
            ValidationStrictness::Normal,
            false, // inplace (false = copy mode, default)
            opts,
        ))
        .expect("dry run install");

    assert!(
        manager.installs.lock().expect("lock").is_empty(),
        "dry-run should not call install_font"
    );
}

#[test]
fn cleanup_respects_prune_and_cache_flags() {
    let runtime = Runtime::new().expect("runtime");
    let base_opts = OperationOptions::new(false, true, false);

    // default: both prune and cache clear
    let manager = Arc::new(RecordingManager::default());
    runtime
        .block_on(handle_cleanup_command(
            manager.clone(),
            false,
            false,
            false,
            base_opts,
        ))
        .expect("cleanup both");
    assert_eq!(manager.prunes.lock().expect("lock").len(), 1);
    assert_eq!(manager.cache_clears.lock().expect("lock").len(), 1);

    // prune-only
    let manager = Arc::new(RecordingManager::default());
    runtime
        .block_on(handle_cleanup_command(
            manager.clone(),
            false,
            true,
            false,
            base_opts,
        ))
        .expect("prune-only");
    assert_eq!(manager.prunes.lock().expect("lock").len(), 1);
    assert!(
        manager.cache_clears.lock().expect("lock").is_empty(),
        "cache clear should be skipped"
    );

    // cache-only
    let manager = Arc::new(RecordingManager::default());
    runtime
        .block_on(handle_cleanup_command(
            manager.clone(),
            false,
            false,
            true,
            base_opts,
        ))
        .expect("cache-only");
    assert!(
        manager.prunes.lock().expect("lock").is_empty(),
        "prune should be skipped"
    );
    assert_eq!(manager.cache_clears.lock().expect("lock").len(), 1);
}

#[test]
fn cleanup_skips_cache_clear_permission_denied_on_windows_user_scope() {
    let runtime = Runtime::new().expect("runtime");
    let manager = Arc::new(DenyCacheManager::default());
    let base_opts = OperationOptions::new(false, true, false);

    let result = runtime.block_on(handle_cleanup_command(
        manager.clone(),
        false, // admin
        false, // prune_only
        false, // cache_only
        base_opts,
    ));

    assert!(
        result.is_ok(),
        "cleanup should not fail when cache clear needs admin"
    );
    assert_eq!(*manager.prunes.lock().expect("lock"), 1, "prune should run");
    assert_eq!(
        *manager.cache_attempts.lock().expect("lock"),
        1,
        "cache clear should be attempted once"
    );
}

#[test]
fn uninstall_by_name_checks_both_scopes() {
    let runtime = Runtime::new().expect("runtime");
    let manager = Arc::new(ScopedUninstallManager::default());
    let opts = OperationOptions::new(false, true, false);

    runtime
        .block_on(handle_uninstall_command(
            manager.clone(),
            Some("ScopedUninstall".to_string()),
            Vec::new(),
            false,
            opts,
        ))
        .expect("uninstall should succeed after checking both scopes");

    assert_eq!(
        manager.scopes_called(),
        vec![FontScope::User, FontScope::System],
        "should attempt user then system scope"
    );
}

#[test]
fn completions_include_core_commands() {
    let mut buffer = Vec::new();

    write_completions(Shell::Bash, &mut buffer).expect("completions");

    let script = String::from_utf8(buffer).expect("utf8");
    assert!(
        script.contains("list"),
        "expected list command in completions"
    );
    assert!(
        script.contains("install"),
        "expected install command in completions"
    );
}

#[test]
fn subcommand_aliases_match_legacy() {
    // list alias
    let cli = Cli::try_parse_from(["fontlift", "l"]).expect("alias l");
    assert!(matches!(cli.command, Commands::List { .. }));

    // install alias
    let cli = Cli::try_parse_from(["fontlift", "i", "font.ttf"]).expect("alias i");
    assert!(matches!(cli.command, Commands::Install { .. }));

    // uninstall alias
    let cli = Cli::try_parse_from(["fontlift", "u", "-n", "FontName"]).expect("alias u");
    assert!(matches!(cli.command, Commands::Uninstall { .. }));

    // remove alias
    let cli = Cli::try_parse_from(["fontlift", "rm", "-n", "FontName"]).expect("alias rm");
    assert!(matches!(cli.command, Commands::Remove { .. }));

    // cleanup alias
    let cli = Cli::try_parse_from(["fontlift", "c"]).expect("alias c");
    assert!(matches!(cli.command, Commands::Cleanup { .. }));

    // doctor alias
    let cli = Cli::try_parse_from(["fontlift", "d"]).expect("alias d");
    assert!(matches!(cli.command, Commands::Doctor { .. }));
}

#[test]
fn clap_error_exit_codes_match_legacy() {
    use clap::error::ErrorKind;

    assert_eq!(exit_code_for_clap_error(ErrorKind::DisplayHelp), 0);
    assert_eq!(exit_code_for_clap_error(ErrorKind::DisplayVersion), 0);
    assert_eq!(exit_code_for_clap_error(ErrorKind::UnknownArgument), 1);
    assert_eq!(
        exit_code_for_clap_error(ErrorKind::MissingRequiredArgument),
        1
    );
}

#[test]
fn validation_strictness_presets_parse() {
    // Default is Normal
    let cli = Cli::try_parse_from(["fontlift", "install", "font.ttf"]).expect("default strictness");
    let Commands::Install {
        validation_strictness,
        ..
    } = cli.command
    else {
        panic!("expected Install");
    };
    assert!(matches!(
        validation_strictness,
        ValidationStrictness::Normal
    ));

    // Explicit lenient
    let cli = Cli::try_parse_from([
        "fontlift",
        "install",
        "font.ttf",
        "--validation-strictness",
        "lenient",
    ])
    .expect("lenient");
    let Commands::Install {
        validation_strictness,
        ..
    } = cli.command
    else {
        panic!("expected Install");
    };
    assert!(matches!(
        validation_strictness,
        ValidationStrictness::Lenient
    ));

    // Explicit paranoid
    let cli = Cli::try_parse_from([
        "fontlift",
        "install",
        "font.ttf",
        "--validation-strictness",
        "paranoid",
    ])
    .expect("paranoid");
    let Commands::Install {
        validation_strictness,
        ..
    } = cli.command
    else {
        panic!("expected Install");
    };
    assert!(matches!(
        validation_strictness,
        ValidationStrictness::Paranoid
    ));
}

#[test]
fn no_validate_flag_parses() {
    let cli =
        Cli::try_parse_from(["fontlift", "install", "font.ttf", "--no-validate"]).expect("parse");
    let Commands::Install { no_validate, .. } = cli.command else {
        panic!("expected Install");
    };
    assert!(no_validate, "--no-validate should set flag to true");
}

#[test]
fn help_text_includes_all_commands() {
    use clap::CommandFactory;

    let mut cmd = Cli::command();
    let help = cmd.render_help().to_string();

    // Verify all main commands are listed in help
    assert!(help.contains("list"), "help should mention list command");
    assert!(
        help.contains("install"),
        "help should mention install command"
    );
    assert!(
        help.contains("uninstall"),
        "help should mention uninstall command"
    );
    assert!(
        help.contains("remove"),
        "help should mention remove command"
    );
    assert!(
        help.contains("cleanup"),
        "help should mention cleanup command"
    );
    assert!(
        help.contains("doctor"),
        "help should mention doctor command"
    );
    assert!(
        help.contains("completions"),
        "help should mention completions command"
    );
}

#[test]
fn shell_completions_generate_for_all_shells() {
    use clap_complete::Shell;

    // Test all supported shells generate valid completions
    for shell in [
        Shell::Bash,
        Shell::Zsh,
        Shell::Fish,
        Shell::PowerShell,
        Shell::Elvish,
    ] {
        let mut buffer = Vec::new();
        write_completions(shell, &mut buffer).expect(&format!("{:?} completions", shell));
        let script = String::from_utf8(buffer).expect("utf8");
        assert!(
            !script.is_empty(),
            "{:?} completions should not be empty",
            shell
        );
        // All shells should include the binary name
        assert!(
            script.contains("fontlift"),
            "{:?} completions should reference fontlift",
            shell
        );
    }
}