use super::*;
#[test]
fn the_sidecar_gate_reads_the_session_consent_set() {
use crate::widgets::input_prompt::InputPurpose;
const EXT: &str = r#"
id = "tconsent"
name = "tconsent"
api_version = 1
[[mcp_servers]]
id = "srv"
command = "/bin/false"
[[commands]]
id = "tconsent.go"
title = "tconsent: go"
server = "srv"
tool = "go"
arg = "q"
prompt = "Query"
"#;
let (_scratch, croft) = scratch_config(&[("tconsent", EXT)]);
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.config_dir = croft.clone();
app.consented_extensions = consent_on_disk(&croft);
app.consented_extensions.insert("tconsent".into());
app.run_extension_command("tconsent.go");
assert!(
matches!(
app.input_prompt.as_ref().map(|p| &p.purpose),
Some(InputPurpose::McpArg { .. })
),
"a consented extension goes straight to its argument prompt, not consent: {:?}",
app.input_prompt.as_ref().map(|p| p.title.clone())
);
app.close_input_prompt();
app.consented_extensions.remove("tconsent");
std::fs::create_dir_all(croft.join("config.json")).unwrap();
app.run_extension_command("tconsent.go");
assert!(
matches!(
app.input_prompt.as_ref().map(|p| &p.purpose),
Some(InputPurpose::McpConsent { .. })
),
"fixture: an unconsented extension is asked first"
);
app.submit_input_prompt();
assert!(
app.status.contains("could not be saved"),
"a failed prefs write is reported: {:?}",
app.status
);
assert!(
matches!(
app.input_prompt.as_ref().map(|p| &p.purpose),
Some(InputPurpose::McpArg { .. })
),
"and the command went on to its argument prompt"
);
assert!(
app.consented_extensions.contains("tconsent"),
"the session grant holds"
);
}
#[test]
fn a_reload_follows_the_extension_consents_in_the_settings_file() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.consented_extensions.insert("unsaved".into());
let mut file = app.applied_settings.clone();
file.mcp_consented.insert("listed".into());
app.apply_merged_settings(&file);
assert!(
app.consented_extensions.contains("listed"),
"a consent put in the file applies: {:?}",
app.consented_extensions
);
file.mcp_consented.remove("listed");
app.apply_merged_settings(&file);
assert!(
!app.consented_extensions.contains("listed"),
"a consent taken out of the file is gone: {:?}",
app.consented_extensions
);
assert!(
app.consented_extensions.contains("unsaved"),
"a session grant the file never held stays"
);
}
const CONSENT_EXT: &str = r#"
id = "tconsent"
name = "tconsent"
api_version = 1
[[mcp_servers]]
id = "srv"
command = "/bin/false"
[[commands]]
id = "tconsent.go"
title = "tconsent: go"
server = "srv"
tool = "go"
arg = "q"
prompt = "Query"
"#;
fn asks_for_consent(app: &App) -> bool {
matches!(
app.input_prompt.as_ref().map(|p| &p.purpose),
Some(crate::widgets::input_prompt::InputPurpose::McpConsent { .. })
)
}
#[test]
fn a_consent_taken_out_of_the_settings_file_asks_again_after_the_reload() {
let (_scratch, croft) = scratch_config(&[("tconsent", CONSENT_EXT)]);
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.config_dir = croft.clone();
let asked = asks_for_consent;
app.run_extension_command("tconsent.go");
assert!(asked(&app), "fixture: a new extension is asked first");
app.submit_input_prompt();
app.close_input_prompt();
assert!(
consent_on_disk(&croft).contains("tconsent"),
"fixture: allowing saves the consent"
);
let mut file = app.applied_settings.clone();
file.mcp_consented.remove("tconsent");
app.apply_merged_settings(&file);
app.run_extension_command("tconsent.go");
assert!(
asked(&app),
"the command asks for consent again, not for its argument: {:?}",
app.input_prompt.as_ref().map(|p| p.title.clone())
);
}
#[test]
fn a_consent_allowed_in_croft_survives_a_reload_beside_a_local_list() {
let (_scratch, croft) = scratch_config(&[("tconsent", CONSENT_EXT)]);
std::fs::write(
croft.join("config.local.json"),
r#"{ "mcp_consented": ["another"] }"#,
)
.unwrap();
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.config_dir = croft.clone();
app.run_extension_command("tconsent.go");
assert!(
asks_for_consent(&app),
"fixture: a new extension is asked first"
);
app.submit_input_prompt();
app.close_input_prompt();
let merged = crate::config_layers::load_merged_from(
&croft,
None,
crate::config_layers::current_platform(),
);
app.apply_merged_settings(&merged.prefs);
app.run_extension_command("tconsent.go");
assert!(
!asks_for_consent(&app),
"the consent just given still holds after the reload: {:?}",
app.input_prompt.as_ref().map(|p| p.title.clone())
);
}
#[test]
fn uninstalling_an_extension_forgets_its_consent() {
let (_scratch, croft) = scratch_config(&[]);
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.config_dir = croft.clone();
app.consented_extensions = consent_on_disk(&croft);
crate::prefs::save_mcp_consent_in(&croft, "csvlens").unwrap();
app.consented_extensions.insert("csvlens".into());
app.disabled_extensions.insert("csvlens".into());
crate::prefs::save_disabled_extensions_in(&croft, &app.disabled_extensions).unwrap();
app.perform_extension_uninstall("csvlens");
let disabled_on_disk = crate::prefs::Prefs::load(&croft.join("config.json"))
.unwrap_or_default()
.disabled_extensions;
assert!(
!disabled_on_disk.contains("csvlens"),
"the disabled state is cleared in the app's own config file: {disabled_on_disk:?}"
);
assert!(
app.status.starts_with("Uninstalled"),
"fixture: the catalog entry uninstalls cleanly: {:?}",
app.status
);
assert!(
!app.consented_extensions.contains("csvlens"),
"the session grant is gone"
);
let saved = consent_on_disk(&croft);
assert!(
!saved.contains("csvlens"),
"and so is the one on disk: {saved:?}"
);
}
#[test]
fn uninstalling_an_extension_forgets_its_consent_in_the_local_file_too() {
let (_scratch, croft) = scratch_config(&[]);
std::fs::create_dir_all(&croft).unwrap();
std::fs::write(
croft.join("config.local.json"),
"{\n // this box only\n \"theme\": \"nord\",\n \"mcp_consented\": [\"csvlens\", \"other\"]\n}\n",
)
.unwrap();
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.config_dir = croft.clone();
crate::prefs::save_mcp_consent_in(&croft, "csvlens").unwrap();
app.consented_extensions.insert("csvlens".into());
app.disabled_extensions.insert("csvlens".into());
crate::prefs::save_disabled_extensions_in(&croft, &app.disabled_extensions).unwrap();
app.perform_extension_uninstall("csvlens");
assert!(
app.status.starts_with("Uninstalled"),
"fixture: the catalog entry uninstalls cleanly: {:?}",
app.status
);
let local = std::fs::read_to_string(croft.join("config.local.json")).unwrap();
assert!(
!local.contains("\"csvlens\""),
"the local consent is gone: {local}"
);
assert!(
local.contains("\"other\"") && local.contains("// this box only") && local.contains("nord"),
"and the rest of the file stays: {local}"
);
let merged = crate::config_layers::load_merged_from(
&croft,
None,
crate::config_layers::current_platform(),
);
assert!(
!merged.prefs.mcp_consented.contains("csvlens"),
"no layer consents to it any more: {:?}",
merged.prefs.mcp_consented
);
}
#[test]
fn uninstalling_an_extension_forgets_its_consent_in_platform_blocks_and_extended_files() {
let (_scratch, croft) = scratch_config(&[]);
std::fs::create_dir_all(&croft).unwrap();
let platform = crate::config_layers::current_platform();
std::fs::write(
croft.join("team.json"),
"{ \"mcp_consented\": [\"csvlens\", \"team-tool\"] }\n",
)
.unwrap();
std::fs::write(
croft.join("config.json"),
format!(
"{{\n \"extends\": \"team.json\",\n \"{platform}\": {{ \"mcp_consented\": [\"csvlens\"] }}\n}}\n"
),
)
.unwrap();
std::fs::write(
croft.join("config.local.json"),
format!(
"{{\n // this box only\n \"{platform}\": {{ \"mcp_consented\": [\"csvlens\", \"other\"] }}\n}}\n"
),
)
.unwrap();
let consented = || {
crate::config_layers::load_merged_from(&croft, None, platform)
.prefs
.mcp_consented
};
assert!(
consented().contains("csvlens"),
"fixture: the user layers consent to it"
);
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.config_dir = croft.clone();
app.consented_extensions.insert("csvlens".into());
app.perform_extension_uninstall("csvlens");
assert!(
app.status.starts_with("Uninstalled"),
"the uninstall and its settings writes succeed: {:?}",
app.status
);
let after = consented();
assert!(
!after.contains("csvlens"),
"no user settings file consents to it any more: {after:?}"
);
assert!(
after.contains("team-tool") && after.contains("other"),
"the other consents stay: {after:?}"
);
let local = std::fs::read_to_string(croft.join("config.local.json")).unwrap();
assert!(local.contains("// this box only"), "{local}");
}
#[cfg(unix)]
#[test]
fn forgetting_a_consent_skips_an_extends_the_settings_loader_refuses() {
use std::os::unix::ffi::OsStrExt as _;
let (_scratch, croft) = scratch_config(&[]);
std::fs::create_dir_all(&croft).unwrap();
let pipe = croft.join("pipe.json");
let name = std::ffi::CString::new(pipe.as_os_str().as_bytes()).unwrap();
assert_eq!(
unsafe { libc::mkfifo(name.as_ptr(), 0o600) },
0,
"fixture: a named pipe"
);
std::fs::write(
croft.join("config.json"),
"{ \"extends\": \"pipe.json\", \"mcp_consented\": [\"csvlens\"] }\n",
)
.unwrap();
let (tx, rx) = std::sync::mpsc::channel();
let dir = croft.clone();
std::thread::spawn(move || {
let forgot = crate::prefs::forget_mcp_consent_in(&dir, "csvlens");
let _ = tx.send(forgot.map_err(|e| e.to_string()));
});
let forgot = rx.recv_timeout(std::time::Duration::from_secs(10));
if forgot.is_err() {
let _ = std::fs::OpenOptions::new().write(true).open(&pipe);
}
assert!(
matches!(forgot, Ok(Ok(()))),
"the forget step finishes without reading the pipe: {forgot:?}"
);
let config = std::fs::read_to_string(croft.join("config.json")).unwrap();
assert!(!config.contains("csvlens"), "{config}");
}
#[test]
fn adding_an_extension_clears_a_stale_disabled_state_and_says_when_that_cannot_save() {
let (_scratch, croft) = scratch_config(&[]);
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.config_dir = croft.clone();
let add_time = |app: &mut App| {
app.disabled_extensions.insert(String::from("mcp-time"));
app.extensions
.set_items(crate::widgets::extensions::items_from_available(
crate::lsp::manifest::summaries(crate::lsp::manifest::CATALOG_MANIFESTS)
.into_iter()
.filter(|s| s.id == "mcp-time")
.collect(),
));
let pos = app
.extensions
.visible_indices()
.iter()
.position(|&i| app.extensions.items()[i].id == "mcp-time")
.expect("the Time row is visible");
app.extensions.select(pos);
app.toggle_selected_extension();
};
add_time(&mut app);
assert!(app.status.starts_with("Added 'mcp-time'"), "{}", app.status);
assert!(croft.join("extensions/mcp-time/extension.toml").is_file());
assert!(!app.disabled_extensions.contains("mcp-time"));
let saved = crate::prefs::Prefs::load(&croft.join("config.json"))
.unwrap_or_default()
.disabled_extensions;
assert!(!saved.contains("mcp-time"), "{saved:?}");
std::fs::remove_dir_all(croft.join("extensions/mcp-time")).unwrap();
std::fs::remove_file(croft.join("config.json")).unwrap();
std::fs::create_dir_all(croft.join("config.json")).unwrap();
add_time(&mut app);
assert!(
app.status.starts_with("Setting not saved"),
"{}",
app.status
);
}
#[test]
fn an_edit_between_refusal_and_consent_forces_a_fresh_prompt() {
let tmp = tempfile::tempdir().unwrap();
let mut app = app_with_open_file(tmp.path(), "a.txt", "plain");
app.editor
.reopen_with_encoding(encoding_rs::WINDOWS_1252)
.unwrap();
app.editor.lines = vec![String::from("æ—¥")];
app.editor.dirty = true;
app.save();
assert_eq!(app.pending_lossy_save.as_ref().unwrap().chars, ['æ—¥']);
app.handle_key(key(KeyCode::Esc, KeyModifiers::NONE))
.unwrap();
app.editor.insert_char('語');
app.save();
assert_eq!(
std::fs::read_to_string(tmp.path().join("a.txt")).unwrap(),
"plain",
"nothing was consented to, so this press asks again"
);
assert_eq!(
app.pending_lossy_save.as_ref().unwrap().chars,
['語', '日'],
"the fresh prompt names the newly typed character"
);
app.handle_key(key(KeyCode::Char('s'), KeyModifiers::NONE))
.unwrap();
assert_eq!(
std::fs::read(tmp.path().join("a.txt")).unwrap(),
b"語日",
"consent given after the prompt that named both characters writes"
);
}
#[test]
fn workspace_extension_recommendations_show_under_from_vscode() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir(tmp.path().join(".vscode")).unwrap();
std::fs::write(
tmp.path().join(".vscode").join("extensions.json"),
r#"{ "recommendations": ["rust-lang.rust-analyzer", "nobody.nothing"] }"#,
)
.unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.refresh_extensions();
let ids: Vec<String> = app
.extensions
.items()
.iter()
.filter(|i| i.vscode)
.map(|i| i.id.clone())
.collect();
assert_eq!(ids, ["rust-lang.rust-analyzer", "nobody.nothing"]);
let visible = app.extensions.visible_indices();
let pos = visible
.iter()
.position(|&i| app.extensions.items()[i].id == "rust-lang.rust-analyzer")
.expect("the row is visible");
app.extensions.select(pos);
let before = app.disabled_extensions.clone();
app.toggle_selected_extension();
assert!(
app.status
.starts_with("rust-lang.rust-analyzer: built in: lsp-rust"),
"the status restates the row: {}",
app.status
);
assert_eq!(app.disabled_extensions, before, "nothing was toggled");
}
#[test]
fn toggling_an_extension_off_forgets_its_tool_fingerprints() {
const EXT: &str = r#"
id = "tfp"
name = "tfp"
api_version = 1
[[mcp_servers]]
id = "srv"
command = "/bin/false"
[[commands]]
id = "tfp.go"
title = "tfp: go"
server = "srv"
tool = "go"
"#;
let other = EXT.replace("tfp", "oth");
let (_scratch, croft) = scratch_config(&[("tfp", EXT), ("oth", &other)]);
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.config_dir = croft.clone();
let toggle_tfp = |app: &mut App| {
app.extensions
.set_items(crate::widgets::extensions::items_from_summaries(
crate::lsp::manifest::summaries(&[EXT]),
&app.disabled_extensions,
));
let visible = app.extensions.visible_indices();
let pos = visible
.iter()
.position(|&i| app.extensions.items()[i].id == "tfp")
.expect("the tfp row is visible");
app.extensions.select(pos);
app.toggle_selected_extension();
};
assert!(crate::prefs::trust_mcp_tool_in(&croft, "tfp.go", "fp1"));
assert!(crate::prefs::trust_mcp_tool_in(&croft, "oth.go", "o1"));
toggle_tfp(&mut app);
assert!(app.disabled_extensions.contains("tfp"), "{}", app.status);
assert!(
crate::prefs::trust_mcp_tool_in(&croft, "tfp.go", "fp2"),
"the changed tool is approved afresh"
);
assert!(
!crate::prefs::trust_mcp_tool_in(&croft, "oth.go", "o2"),
"another extension's record stays"
);
toggle_tfp(&mut app);
assert!(!app.disabled_extensions.contains("tfp"), "{}", app.status);
assert!(!crate::prefs::trust_mcp_tool_in(&croft, "tfp.go", "fp3"));
}
#[test]
fn uninstalling_an_extension_forgets_its_tool_fingerprints() {
const TIME: &str = include_str!("../../../assets/catalog/mcp-time/extension.toml");
const OTH: &str = r#"
id = "oth"
name = "oth"
api_version = 1
[[mcp_servers]]
id = "srv"
command = "/bin/false"
[[commands]]
id = "oth.go"
title = "oth: go"
server = "srv"
tool = "go"
"#;
let (_scratch, croft) = scratch_config(&[("mcp-time", TIME), ("oth", OTH)]);
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.config_dir = croft.clone();
assert!(crate::prefs::trust_mcp_tool_in(&croft, "time.now", "fp1"));
assert!(crate::prefs::trust_mcp_tool_in(&croft, "oth.go", "o1"));
app.perform_extension_uninstall("mcp-time");
assert!(app.status.starts_with("Uninstalled"), "{}", app.status);
assert!(
crate::prefs::trust_mcp_tool_in(&croft, "time.now", "fp2"),
"the re-added tool is approved afresh"
);
assert!(
!crate::prefs::trust_mcp_tool_in(&croft, "oth.go", "o2"),
"another extension's record stays"
);
}