Repository: nushell/nushell
File: crates/nu-cli/tests/completions/mod.rs (Rust)

Code before:
    match_suggestions(&vec!["bar", "baz", "foo"], &suggestions);
}

#[test]
fn custom_completions_strip_ansi_from_record_values() {
    let (_, _, mut engine, mut stack) = new_engine();
    // Record-style completions with ANSI codes in the value field should be stripped
    let command = r#"
        def comp [] {
            {
                completions: [
                    { value: $"\u{1b}[35mmagenta_dir", description: "a zoxide dir" },
                    { value: "plain_dir", description: "a normal dir" },
                ],
                options: { sort: false }
            }
        }
        def my-command [arg: string@comp] {}"#;
    assert!(support::merge_input(command.as_bytes(), &mut engine, &mut stack).is_ok());

    let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack));
    let completion_str = "my-command ";
    let suggestions = completer.complete(completion_str, completion_str.len());
    // ANSI codes should be stripped, leaving clean values
    assert_eq!(suggestions[0].value, "magenta_dir");
    assert_eq!(suggestions[1].value, "plain_dir");
}

#[rstest]
/// Fallback to file completions if custom completer returns null
#[case::fallback(r#"
    def comp [] { null }
    def my-command [arg: string@comp] {}"#,
    "my-command test", None,
    vec![folder("test_a"), file("test_a_symlink"), folder("test_b")],
    4
)]
/// Custom function arguments mixed with subcommands
#[case::arguments_and_subcommands(r#"
    def foo [i: directory] {}
    def "foo test bar" [] {}"#,
    "foo test", None,
    vec![folder("test_a"), file("test_a_symlink"), folder("test_b"), "foo test bar".into()],
    8
)]
/// If argument type is something like int/string, complete only subcommands
#[case::arguments_vs_subcommands(r#"
    def foo [i: string] {}
    def "foo test bar" [] {}"#,
    "foo test", None,
    vec!["foo test bar".into()],

Review comment (suggestion):
Is there a reason you didn't use `match_suggestions` here like you did on the other test? Because that's the way to go, unless there happens to be a problem with that here

Code after:
}

#[test]
fn custom_completions_strip_ansi_from_record_values() {
    let (_, _, mut engine, mut stack) = new_engine();
    let command = r#"
        def comp [] {
            [{ value: $"\u{1b}[35mmagenta_dir" }, { value: "plain_dir" }]
        }
        def my-command [arg: string@comp] {}"#;
    assert!(support::merge_input(command.as_bytes(), &mut engine, &mut stack).is_ok());

    let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack));
    let completion_str = "my-command ";
    let suggestions = completer.complete(completion_str, completion_str.len());
    match_suggestions(&vec!["magenta_dir", "plain_dir"], &suggestions);
}

#[rstest]
/// Fallback to file completions if custom completer returns null
#[case::fallback(r#"
    def comp [] { null }
    def my-command [arg: string@comp] {}"#,
    "my-command test", None,
    vec![folder("test_a"), file("test_a_symlink"), folder("test_b")],
    4
)]
/// Custom function arguments mixed with subcommands
#[case::arguments_and_subcommands(r#"
    def foo [i: directory] {}
    def "foo test bar" [] {}"#,
    "foo test", None,
    vec![folder("test_a"), file("test_a_symlink"), folder("test_b"), "foo test bar".into()],
    8
)]
/// If argument type is something like int/string, complete only subcommands
#[case::arguments_vs_subcommands(r#"
    def foo [i: string] {}
    def "foo test bar" [] {}"#,
    "foo test", None,
    vec!["foo test bar".into()],
    8
)]
/// Custom function flags mixed with subcommands
#[case::flags_and_subcommands(r#"
    def foo [--test: directory] {}
    def "foo --test bar" [] {}"#,
    "foo --test", None,
    vec!["--test".into(), "foo --test bar".into()],
    10
)]