Skip to content

Visual Studio Code, rebuilt in Rust

Rust
24.7 KB
Raw Blame
use std::cell::Cell;
use std::path::Path;
use std::rc::Rc;
use std::sync::{Arc, Mutex};

use exthost::scan::{ScannedExtension, scan_extension};
use gpui::TestAppContext;
use serde_json::json;

use crate::command_palette;
use crate::commands::Outcome;
use crate::host_sync::HostSync;
use crate::languages::Languages;
use crate::quick_input::Entry;
use crate::theme::load_theme;
use crate::workbench::Workbench;
use storage::Storage;

fn labels(workbench: &Workbench, _: &gpui::App) -> Vec<String> {
    let input = workbench.quick_inputs.current().expect("a visible quick input");
    input.order.iter().map(|row| &input.rows[*row]).map(|row| format!("{}{}", row.separator.as_ref().map(|label| format!("--{label}--")).unwrap_or_default(), row.item.label)).collect()
}

#[gpui::test]
fn command_palette_runs_an_extension_command_and_remembers_it(cx: &mut TestAppContext) {
    let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
    let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
    let theme = load_theme(&[themes], "Dark Modern").unwrap();
    let dir = std::env::temp_dir().join(format!("code-gpui-palette-{}", std::process::id()));
    std::fs::create_dir_all(&dir).unwrap();
    let extension = ScannedExtension {
        id: "port.hello".into(),
        location: dir.clone(),
        description: json!({ "contributes": { "commands": [{ "command": "port.hello", "title": "Say Hello", "category": "Port" }] } }),
    };
    let runs = Rc::new(Cell::new(0));
    let storage = Arc::new(Mutex::new(Storage::in_memory()));
    storage.lock().unwrap().set("commandPalette.mru.counter", "41".into());
    let profile = storage.clone();
    let (workbench, cx) = cx.add_window_view(|_, cx| {
        let mut workbench = Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), crate::default_settings(), HostSync::new(std::sync::mpsc::channel().0), cx);
        workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("keybindings.json"));
        workbench.quick_access.menu = command_palette::menu(&[extension]);
        workbench.restore_profile_state(profile.clone(), profile);
        let runs = runs.clone();
        workbench.commands.register("port.hello", move |_, _, _, _| {
            runs.set(runs.get() + 1);
            Outcome::done()
        });
        workbench
    });
    cx.run_until_parked();

    cx.simulate_keystrokes(&crate::keybindings::primary_keys("shift-secondary-p"));
    let (value, placeholder) = workbench.read_with(cx, |workbench, _| {
        let input = workbench.quick_inputs.current().unwrap();
        (input.input_box.value.clone(), input.placeholder.clone())
    });
    assert_eq!(value, ">");
    assert_eq!(placeholder.as_deref(), Some("Type the name of a command to run."));
    let all = workbench.read_with(cx, labels);
    assert!(all.contains(&"Port: Say Hello".to_string()), "{all:?}");
    assert!(all.contains(&"Show All Commands".to_string()), "{all:?}");
    // `viewContainer.workbench.view.explorer.enabled` and `workbench.view.search.active` hold.
    assert!(all.contains(&"View: Show Explorer".to_string()) && all.contains(&"View: Show Search".to_string()), "{all:?}");

    cx.simulate_input("say hel");
    assert_eq!(workbench.read_with(cx, labels), ["Port: Say Hello"]);
    let highlights = workbench.read_with(cx, |workbench, _| workbench.quick_inputs.current().unwrap().rows[0].label_highlights.clone());
    assert_eq!(highlights, [6..13]);

    cx.simulate_keystrokes("enter");
    assert_eq!(runs.get(), 1);
    let stored = |key: &str| storage.lock().unwrap().get(key).map(str::to_owned);
    assert_eq!(stored("commandPalette.mru.cache").as_deref(), Some(r#"{"usesLRU":true,"entries":[{"key":"port.hello","value":41}]}"#));
    assert_eq!(stored("commandPalette.mru.counter").as_deref(), Some("42"));
    assert!(workbench.read_with(cx, |workbench, _| workbench.quick_inputs.current().is_none()));

    cx.simulate_keystrokes(&crate::keybindings::primary_keys("shift-secondary-p"));
    let reopened = workbench.read_with(cx, labels);
    assert_eq!(reopened[0], "--recently used--Port: Say Hello");
    assert!(reopened[1].starts_with("--other commands--"), "{reopened:?}");

    cx.simulate_input("color theme");
    let row = workbench.read_with(cx, |workbench, _| {
        let input = workbench.quick_inputs.current().unwrap();
        let item = &input.rows[input.order[0]].item;
        (item.label.clone(), item.keybinding.clone())
    });
    let primary = if matches!(crate::keybindings::OS, crate::keybindings::Os::Macintosh) { "⌘" } else { "Ctrl" };
    assert_eq!(row, ("Preferences: Color Theme".to_string(), vec![vec![primary.to_string(), "K".to_string()], vec![primary.to_string(), "T".to_string()]]));

    cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-a backspace"));
    cx.simulate_input(">zzzz");
    assert_eq!(workbench.read_with(cx, labels), ["No matching commands"]);
    cx.simulate_keystrokes("enter");
    assert!(workbench.read_with(cx, |workbench, _| workbench.quick_inputs.current().is_some()), "no matching commands keeps the picker");
    cx.simulate_keystrokes("escape");
    assert!(workbench.read_with(cx, |workbench, _| workbench.quick_inputs.current().is_none()));
    std::fs::remove_dir_all(dir).unwrap();
}

#[gpui::test]
fn go_to_line_previews_the_line_and_restores_the_view_on_escape(cx: &mut TestAppContext) {
    let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
    let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
    let theme = load_theme(&[themes], "Dark Modern").unwrap();
    let dir = std::env::temp_dir().join(format!("code-gpui-goto-line-{}", std::process::id()));
    std::fs::create_dir_all(&dir).unwrap();
    let file = dir.join("a.txt");
    std::fs::write(&file, (1..=200).map(|line| format!("line {line}\n")).collect::<String>()).unwrap();
    let (workbench, cx) = cx.add_window_view(|window, cx| {
        let mut workbench = Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), crate::default_settings(), HostSync::new(std::sync::mpsc::channel().0), cx);
        workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("keybindings.json"));
        workbench.open_file(file.clone(), true, window, cx);
        workbench
    });
    cx.run_until_parked();
    let editor = |cx: &mut gpui::VisualTestContext| {
        workbench.read_with(cx, |workbench, _| {
            let editor = workbench.editors.active().unwrap();
            let highlight = workbench.quick_access.goto_line.as_ref().and_then(|state| state.highlight(editor.model_id)).map(|(line, _)| line);
            (editor.selection().head.line, editor.selection().head.column, editor.scroll.top > 0., highlight)
        })
    };

    cx.simulate_keystrokes("ctrl-g");
    assert_eq!(workbench.read_with(cx, labels), ["Type a line number to go to (from 1 to 201)."]);
    cx.simulate_input("150");
    assert_eq!(workbench.read_with(cx, labels), ["Press 'Enter' to go to line 150 or enter colon : to add a column number."]);
    assert_eq!(editor(cx), (0, 0, true, Some(149)), "the line is revealed and highlighted, the cursor stays");
    cx.simulate_keystrokes("escape");
    assert_eq!(editor(cx), (0, 0, false, None), "the view state comes back");

    cx.simulate_keystrokes("ctrl-g");
    cx.simulate_input(":5");
    let buttons = workbench.read_with(cx, |workbench, _| workbench.quick_inputs.current().unwrap().buttons.len());
    assert_eq!((workbench.read_with(cx, labels), buttons), (vec!["Press 'Enter' to go to line 1 at column 5.".to_string()], 1), "offset mode shows the toggle");
    cx.simulate_keystrokes("backspace backspace");
    cx.simulate_input("150:3");
    cx.simulate_keystrokes("enter");
    assert_eq!(editor(cx), (149, 2, true, None));
    assert!(workbench.read_with(cx, |workbench, _| workbench.quick_inputs.current().is_none()));
    let status = workbench.read_with(cx, |workbench, _| workbench.accessibility.status.clone().map(|(message, _)| message));
    assert_eq!(status.as_deref(), Some("Line 150, column 3: line 150"), "editorNavigationQuickAccess.ts `gotoLocation` status");
    std::fs::remove_dir_all(dir).unwrap();
}

/// helpQuickAccess.ts: `?` lists the providers by prefix, accepting one shows it, typing its prefix too.
#[gpui::test]
fn help_lists_the_providers_and_shows_the_picked_one(cx: &mut TestAppContext) {
    let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
    let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
    let theme = load_theme(&[themes], "Dark Modern").unwrap();
    let dir = std::env::temp_dir().join(format!("code-gpui-help-{}", std::process::id()));
    let (workbench, cx) = cx.add_window_view(|_, cx| {
        let mut workbench = Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), crate::default_settings(), HostSync::new(std::sync::mpsc::channel().0), cx);
        workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("keybindings.json"));
        workbench
    });
    cx.run_until_parked();
    let picker = |cx: &mut gpui::VisualTestContext| {
        workbench.read_with(cx, |workbench, _| {
            let input = workbench.quick_inputs.current().unwrap();
            let rows = input.order.iter().map(|row| &input.rows[*row]).map(|row| format!("{}|{}|{:?}", row.item.label, row.item.description.clone().unwrap_or_default(), row.item.keybinding)).collect::<Vec<_>>();
            (input.input_box.value.clone(), input.placeholder.clone().unwrap_or_default(), rows)
        })
    };

    cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-p"));
    cx.simulate_input("?");
    let (value, placeholder, rows) = picker(cx);
    assert_eq!((value.as_str(), placeholder.as_str()), ("?", "Type '?' to get help on the actions you can take from here."));
    let keys = if matches!(crate::keybindings::OS, crate::keybindings::Os::Macintosh) {
        [r#"[["⌘", "P"]]"#, r#"[["⌃", "G"]]"#, r#"[["⇧", "⌘", "O"]]"#, r#"[["⌘", "T"]]"#, r#"[["⇧", "⌘", "P"]]"#, r#"[["⌥", "⌘", "Tab"]]"#]
    } else {
        [r#"[["Ctrl", "P"]]"#, r#"[["Ctrl", "G"]]"#, r#"[["Ctrl", "Shift", "O"]]"#, r#"[["Ctrl", "T"]]"#, r#"[["Ctrl", "Shift", "P"]]"#, r#"[["Ctrl", "K"], ["Ctrl", "P"]]"#]
    };
    assert_eq!(rows, [
        format!("…|Go to File|{}", keys[0]),
        format!(":|Go to Line/Column|{}", keys[1]),
        "::|Go to Offset|[]".into(),
        format!("@|Go to Symbol in Editor|{}", keys[2]),
        "@:|Go to Symbol in Editor by Category|[]".into(),
        format!("#|Go to Symbol in Workspace|{}", keys[3]),
        format!(">|Show and Run Commands|{}", keys[4]),
        "debug |Start Debugging|[]".into(),
        format!("edt |Show All Opened Editors By Appearance|{}", keys[5]),
        "edt active |Show Editors in Active Group by Most Recently Used|[]".into(),
        "edt mru |Show All Opened Editors By Most Recently Used|[]".into(),
        "term |Show All Opened Terminals|[]".into(),
        "view |Open View|[]".into(),
    ]);
    cx.simulate_keystrokes("down enter");
    assert_eq!(picker(cx).0, ":", "accept shows the picked provider");

    cx.simulate_keystrokes(&crate::keybindings::primary_keys("escape secondary-p"));
    cx.simulate_input("?>");
    cx.run_until_parked();
    assert_eq!(picker(cx).0, ">", "a typed prefix shows its provider");
}

/// gotoLineQuickAccess.ts `provideWithTextEditor`: relative line numbers show as "on" while the picker is open.
#[gpui::test]
fn go_to_line_shows_relative_line_numbers_as_on_while_open(cx: &mut TestAppContext) {
    use crate::workbench::editor_view::LineNumbers;

    let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
    let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
    let theme = load_theme(&[themes], "Dark Modern").unwrap();
    let dir = std::env::temp_dir().join(format!("code-gpui-goto-line-relative-{}", std::process::id()));
    std::fs::create_dir_all(dir.join("User")).unwrap();
    std::fs::write(dir.join("User/settings.json"), r#"{ "editor.lineNumbers": "relative" }"#).unwrap();
    let file = dir.join("a.txt");
    std::fs::write(&file, "a\nb\nc\n").unwrap();
    let settings = std::sync::Arc::new(std::sync::Mutex::new(config::service::Settings::load(config::registry::Registry::with_extensions(&[]), dir.join("User"), None)));
    let (workbench, cx) = cx.add_window_view(|window, cx| {
        let mut workbench = Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), settings, HostSync::new(std::sync::mpsc::channel().0), cx);
        workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("keybindings.json"));
        workbench.open_file(file.clone(), true, window, cx);
        workbench
    });
    cx.run_until_parked();
    let line_numbers = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.line_numbers(workbench.editors.active().unwrap()));

    assert_eq!(line_numbers(cx), LineNumbers::Relative);
    cx.simulate_keystrokes("ctrl-g");
    assert_eq!(line_numbers(cx), LineNumbers::On);
    cx.simulate_keystrokes("escape");
    assert_eq!(line_numbers(cx), LineNumbers::Relative);
    std::fs::remove_dir_all(dir).unwrap();
}

/// viewQuickAccess.ts: Ctrl+Q lists the containers by location, then each container's views under `Parent / Child`,
/// then the output channels; a filter matches the label or the container's label, a pick opens its view.
#[gpui::test]
fn the_view_picker_groups_views_by_container_and_opens_the_pick(cx: &mut TestAppContext) {
    let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
    let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
    let theme = load_theme(&[themes], "Dark Modern").unwrap();
    let dir = std::env::temp_dir().join(format!("code-gpui-view-picker-{}", std::process::id()));
    let (workbench, cx) = cx.add_window_view(|_, cx| {
        let mut workbench = Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), crate::default_settings(), HostSync::new(std::sync::mpsc::channel().0), cx);
        workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("keybindings.json"));
        workbench
    });
    cx.run_until_parked();

    cx.simulate_keystrokes("ctrl-q");
    assert_eq!(workbench.read_with(cx, |workbench, _| workbench.quick_inputs.current().unwrap().placeholder.clone()).as_deref(), Some("Type the name of a view, output channel or terminal to open."));
    assert_eq!(workbench.read_with(cx, labels), [
        "--Side Bar--Explorer",
        "Search",
        "Source Control",
        "Run and Debug",
        "Extensions",
        "--Panel--Problems",
        "Output",
        "Debug Console",
        "Terminal",
        "--Side Bar / Explorer--Open Editors",
        "No Folder Opened",
        "Outline",
        "Timeline",
        "--Side Bar / Search--Search",
        "--Side Bar / Source Control--Changes",
        "--Side Bar / Run and Debug--Run",
        "--Panel / Problems--Problems",
        "--Panel / Output--Output",
        "--Panel / Debug Console--Debug Console",
        "--Panel / Terminal--Terminal",
    ]);
    cx.simulate_input("explorer");
    assert_eq!(workbench.read_with(cx, labels), ["--Side Bar--Explorer", "--Side Bar / Explorer--Open Editors", "No Folder Opened", "Outline", "Timeline"], "the container's label matches its views");
    cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-a backspace"));
    cx.simulate_input("view zzzz");
    assert_eq!(workbench.read_with(cx, labels), ["No matching views"]);

    cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-a backspace"));
    cx.simulate_input("view proble");
    cx.simulate_keystrokes("enter");
    cx.run_until_parked();
    assert_eq!(workbench.read_with(cx, |workbench, _| (workbench.quick_inputs.current().is_none(), workbench.is_view_visible("workbench.panel.markers.view"))), (true, true), "the pick opens its view");

    // `quickNavigateConfiguration`: Ctrl+Q again moves to the next pick, releasing Ctrl opens it.
    cx.simulate_modifiers_change(gpui::Modifiers::control());
    cx.simulate_keystrokes("ctrl-q ctrl-q");
    assert!(workbench.read_with(cx, |workbench, _| workbench.quick_inputs.current().is_some()));
    cx.simulate_modifiers_change(gpui::Modifiers::none());
    cx.run_until_parked();
    assert_eq!(workbench.read_with(cx, |workbench, _| (workbench.quick_inputs.current().is_none(), workbench.is_view_visible("workbench.view.search"))), (true, true), "the second pick, Search, opens");
}

/// debugQuickAccess.ts: `debug ` lists the launch configurations, the selected one under Most Recent, a separator at
/// each presentation group, then Add Configuration; a pick starts its configuration.
#[gpui::test]
fn the_debug_picker_lists_and_starts_the_launch_configurations(cx: &mut TestAppContext) {
    let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
    let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
    let theme = load_theme(&[themes], "Dark Modern").unwrap();
    let dir = std::env::temp_dir().join(format!("code-gpui-debug-picker-{}", std::process::id()));
    let folder = dir.join("folder");
    std::fs::create_dir_all(folder.join(".vscode")).unwrap();
    std::fs::create_dir_all(dir.join("User")).unwrap();
    let launch = json!({ "configurations": [
        { "name": "Launch app.js", "type": "node", "request": "launch" },
        { "name": "Launch lazy.js", "type": "node", "request": "launch" },
        { "name": "Grouped", "type": "node", "request": "launch", "presentation": { "group": "a" } },
        { "name": "Hidden", "type": "node", "request": "launch", "presentation": { "hidden": true } },
    ] });
    std::fs::write(folder.join(".vscode/launch.json"), launch.to_string()).unwrap();
    let workspace = config::workspace::Workspace::folder(folder.clone());
    let settings = Arc::new(Mutex::new(config::service::Settings::load(config::registry::Registry::with_extensions(&[]), dir.join("User"), Some(workspace.clone()))));
    let (workbench, cx) = cx.add_window_view(|_, cx| {
        let mut workbench = Workbench::new(theme, app_root.clone(), Some(workspace), Languages::new(&[]), settings, HostSync::new(std::sync::mpsc::channel().0), cx);
        workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("User/keybindings.json"));
        workbench.debug.debuggers.push(crate::debug::configuration::Debugger { debug_type: "node".into(), contribution: json!({ "label": "Node.js" }), extension_id: "a.node".into() });
        let provider = crate::workbench::debug_service::ConfigurationProvider { handle: 1, debug_type: "node".into(), trigger_kind: 2, has_provide: true, has_resolve: false, has_resolve2: false };
        workbench.debug.providers.push(provider);
        workbench.workspace_storage.lock().unwrap().set("debug.recentdynamicconfigurations", json!([{ "name": "Run Script: test", "type": "node" }]).to_string());
        workbench.debug.selected = Some((Some(folder.clone()), "Launch app.js".into()));
        workbench
    });
    cx.run_until_parked();

    workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("workbench.action.debug.selectandstart", Vec::new(), window, cx)));
    assert_eq!(workbench.read_with(cx, |workbench, _| workbench.quick_inputs.current().unwrap().placeholder.clone()).as_deref(), Some("Type the name of a launch configuration to run."));
    assert_eq!(workbench.read_with(cx, labels), ["--Most Recent--Launch app.js", "----Grouped", "----Launch lazy.js", "--contributed--Run Script: test", "$(folder) Node.js...", "--configure--Add Configuration..."]);
    cx.simulate_input("lazy");
    assert_eq!(workbench.read_with(cx, labels), ["--launch.json--Launch lazy.js", "--contributed--$(folder) Node.js...", "--configure--Add Configuration..."]);
    cx.simulate_keystrokes("enter");
    cx.run_until_parked();
    assert_eq!(workbench.read_with(cx, |workbench, _| (workbench.quick_inputs.current().is_none(), workbench.debug.selected.clone())), (true, Some((Some(folder.clone()), "Launch lazy.js".into()))), "the pick is selected and started");
    // debugStatus.ts: `getLaunches()` always holds the user settings launch too, so the folder's name follows.
    let status = workbench.update(cx, |workbench, _| {
        workbench.debug.first_session_started = true;
        workbench.update_debug_status();
        workbench.status_entries.iter().find(|entry| entry.entry_id == "status.debug").map(|entry| entry.text.clone())
    });
    assert_eq!(status.as_deref(), Some("$(debug-alt-small) Launch lazy.js (folder)"));
    std::fs::remove_dir_all(dir).unwrap();
}

/// debugConfigurationManager.ts `selectConfiguration(launch, name, undefined, { type })`: a picked dynamic
/// configuration stays selected under its type (`debug.selectedtype`), F5 asks its provider for it again, and a
/// launch.json pick drops the type.
#[gpui::test]
fn a_dynamic_selection_keeps_its_type_for_f5(cx: &mut TestAppContext) {
    let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
    let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
    let theme = load_theme(&[themes], "Dark Modern").unwrap();
    let dir = std::env::temp_dir().join(format!("code-gpui-debug-dynamic-{}", std::process::id()));
    let folder = dir.join("folder");
    std::fs::create_dir_all(folder.join(".vscode")).unwrap();
    std::fs::create_dir_all(dir.join("User")).unwrap();
    std::fs::write(folder.join(".vscode/launch.json"), json!({ "configurations": [{ "name": "Launch app.js", "type": "node", "request": "launch" }] }).to_string()).unwrap();
    let workspace = config::workspace::Workspace::folder(folder.clone());
    let settings = Arc::new(Mutex::new(config::service::Settings::load(config::registry::Registry::with_extensions(&[]), dir.join("User"), Some(workspace.clone()))));
    let (sender, host) = std::sync::mpsc::channel();
    let (workbench, cx) = cx.add_window_view(|_, cx| {
        let mut workbench = Workbench::new(theme, app_root.clone(), Some(workspace), Languages::new(&[]), settings, HostSync::new(sender), cx);
        let provider = crate::workbench::debug_service::ConfigurationProvider { handle: 1, debug_type: "node".into(), trigger_kind: 2, has_provide: true, has_resolve: false, has_resolve2: false };
        workbench.debug.providers.push(provider);
        workbench
    });
    cx.run_until_parked();
    let provided = |cx: &mut gpui::VisualTestContext| {
        let mut provided = 0;
        loop {
            cx.run_until_parked();
            let messages: Vec<_> = host.try_iter().collect();
            if messages.is_empty() {
                return provided;
            }
            for message in messages {
                let answer = match message.method {
                    "$provideDebugConfigurations" => {
                        provided += 1;
                        json!([{ "name": "Run Script: test", "type": "node-terminal", "request": "launch" }])
                    }
                    _ => serde_json::Value::Null,
                };
                if let Some(reply) = message.reply {
                    let _ = reply.send(Ok(answer));
                }
            }
        }
    };
    let selection = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| (workbench.selected_launch().1, workbench.workspace_storage.lock().unwrap().get("debug.selectedtype").map(str::to_owned)));

    workbench.update_in(cx, |workbench, window, cx| workbench.start_recent_dynamic("Run Script: test".into(), "node".into(), window, cx));
    assert_eq!(provided(cx), 1);
    assert_eq!(selection(cx), (Some("Run Script: test".into()), Some("node".into())));
    workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("workbench.action.debug.start", Vec::new(), window, cx)));
    assert_eq!(provided(cx), 1, "F5 starts the dynamic selection");

    workbench.update_in(cx, |workbench, window, cx| workbench.accept_debug_pick(super::start_debug::DebugPick::Launch { folder: folder.clone(), name: "Launch app.js".into() }, window, cx));
    provided(cx);
    assert_eq!(selection(cx), (Some("Launch app.js".into()), None));
    std::fs::remove_dir_all(dir).unwrap();
}

#[test]
fn removing_a_recently_used_pick_keeps_the_others() {
    let mut input = crate::quick_input::QuickInput::new(1, crate::quick_input::Kind::QuickPick);
    input.show();
    let item = |handle: i64, label: &str| Entry::Item(crate::quick_input::Item { handle, label: label.into(), pickable: true, ..Default::default() });
    input.set_items(vec![Entry::Separator(Some("recently used".into())), item(0, "a"), item(1, "b"), Entry::Separator(Some("other commands".into())), item(2, "c")]);
    input.set_active_items(&[1]);
    input.remove_item(0);
    assert_eq!(input.order.iter().map(|row| input.rows[*row].item.label.as_str()).collect::<Vec<_>>(), ["b", "c"]);
    assert_eq!(input.active_handles(), [1]);
}