Файли
openidea/crates/ide-host/tests/agent_mcp.rs
T
Mr0Bread b60b743843 refactor(ide-host): shared test HostConfig constructors replace ~100 copied literals (#5501)
`HostConfig` (9 fields) was written out as a full struct literal in about 100 tests of `ide-host`, almost all of them the same boilerplate, so a new field meant editing ~100 places. This adds one base constructor for tests and migrates every literal that is a plain variant of it. Tests that differ keep struct-update overrides (`HostConfig { sink_for: .., ..HostConfig::for_test(&base) }`). No builder.
- Unit tests (`src/`): `#[cfg(test)] pub(crate) fn HostConfig::for_test(base: &Path)` in `lib.rs`.
- Integration tests (`tests/`): `tests/common/mod.rs` with `host_config(base: &Path)`, pulled in with `mod common;`. It builds the same value as `for_test`.
Both build: `config`, `data`, and `data` again as the machine-local dir (the layout most tests used), `LanguageRegistry::load(None)`, empty syntax, a sink that drops events, identity owner, `Handle::current()`, `project_environment: false`.
Why not one constructor for both: integration tests compile against the crate without `cfg(test)`, so a shared one needs a new `testing` cargo feature plus a dev-dependency on the crate itself (nothing in `ide-host` does that today). Two small definitions need no feature, no `Cargo.toml` change and no public item.

Co-authored-by: Mr0Bread <[email protected]>
2026-11-24 07:02:21 +08:00

72 рядки
2.9 KiB
Rust
Неформатований Анотація Історія

Цей файл містить неоднозначні символи Юнікоду
Цей файл містить символи Юнікоду, які можна сплутати з іншими символами. Якщо ви вважаєте, що це зроблено навмисно, можете сміливо ігнорувати це попередження. Щоб показати їх, скористайтеся кнопкою Escape.
//! External MCP servers in Settings › Agents, as the host stores and lists them.
mod common;
use ide_agent::{McpScope, McpServer, McpServerInput};
use ide_core::settings::McpTransport;
use ide_host::Host;
use ide_host::ops::agent::{
agent_mcp_import as mcp_import, agent_mcp_list as mcp_list, agent_mcp_save as mcp_save,
agent_mcp_set_enabled as mcp_set_enabled,
};
use std::path::{Path, PathBuf};
use std::sync::Arc;
fn open(base: &Path) -> (Arc<Host>, PathBuf) {
let root = dunce::canonicalize(base).unwrap().join("project");
std::fs::create_dir_all(root.join(".openidea")).unwrap();
let host = Arc::new(Host::new(common::host_config(base)));
host.trust.decide(&root, true, false).unwrap();
host.open_project("main", &root).unwrap();
(host, root)
}
fn enabled(list: &[McpServer], id: &str) -> bool {
list.iter().find(|s| s.id == id).unwrap_or_else(|| panic!("{id} not listed: {list:?}")).enabled
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn workspace_servers_from_the_project_file_stay_off_until_the_user_turns_them_on() {
let tmp = tempfile::tempdir().unwrap();
let (host, root) = open(tmp.path());
let settings_file = root.join(".openidea/settings.toml");
std::fs::write(&settings_file, "[agents.mcpServers.helper]\ncommand = \"node\"\nargs = [\"helper.js\"]\n").unwrap();
host.project("main").unwrap().settings.reload();
assert!(!enabled(&mcp_list(&host, "main").unwrap(), "workspace::helper"));
assert!(enabled(&mcp_set_enabled(&host, "main", "workspace::helper".into(), true).unwrap(), "workspace::helper"));
assert!(enabled(&mcp_list(&host, "main").unwrap(), "workspace::helper"));
// A changed definition is off again until turned back on.
std::fs::write(&settings_file, "[agents.mcpServers.helper]\ncommand = \"node\"\nargs = [\"other.js\"]\n").unwrap();
host.project("main").unwrap().settings.reload();
assert!(!enabled(&mcp_list(&host, "main").unwrap(), "workspace::helper"));
// What the user adds in Settings is on.
let saved = mcp_save(
&host,
"main",
McpServerInput {
id: None,
name: "mine".into(),
scope: McpScope::Workspace,
transport: McpTransport::Stdio,
enabled: true,
command: Some("uvx".into()),
args: vec!["mcp-server-git".into()],
env: vec![],
url: None,
headers: vec![],
agents: vec![],
},
)
.unwrap();
assert!(enabled(&saved, "workspace::mine"));
// Imported servers come from the project's own files: off until turned on.
std::fs::write(root.join(".mcp.json"), r#"{"mcpServers":{"imported":{"command":"uvx","args":["x"]}}}"#).unwrap();
let imported = mcp_import(&host, "main").unwrap();
assert!(!enabled(&imported, "workspace::imported"));
assert!(enabled(&imported, "workspace::mine"));
}