Initial public release
Luxe 0.1.0: a coding agent harness for the terminal. See README.md to get started and CHANGELOG.md for releases.
This commit is contained in:
134
crates/luxe-lsp/tests/integration_real_lsp/main.rs
Normal file
134
crates/luxe-lsp/tests/integration_real_lsp/main.rs
Normal file
@@ -0,0 +1,134 @@
|
||||
//! Real-server LSP validation (validation-backlog §A) against an installed `rust-analyzer`.
|
||||
//!
|
||||
//! These `#[ignore]` tests spawn the real server over a tiny Cargo project and confirm the paths
|
||||
//! that were previously only stub-tested: diagnostics settle logic, `LocationLink` definitions,
|
||||
//! hierarchical `documentSymbol` flattening, and call hierarchy. Run explicitly:
|
||||
//!
|
||||
//! ```text
|
||||
//! cargo nextest run -p luxe-lsp --run-ignored all -E 'test(real_ra)' --no-capture
|
||||
//! ```
|
||||
|
||||
use luxe_core::{ContentBlock, Tool, ToolContext, ToolResultData};
|
||||
use luxe_lsp::{LspDiagnosticsTool, LspNavigationTool, WorkspaceLspClient};
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
use tokio::sync::{mpsc, watch};
|
||||
|
||||
const CARGO_TOML: &str =
|
||||
"[package]\nname = \"real_ra_fixture\"\nversion = \"0.1.0\"\nedition = \"2021\"\n";
|
||||
const LIB_RS: &str = "pub mod bad;\npub mod good;\npub mod nav;\n";
|
||||
const BAD_RS: &str = "pub fn bad() -> u32 {\n \"not a number\"\n}\n";
|
||||
const GOOD_RS: &str = "pub fn good() -> u32 {\n 42\n}\n";
|
||||
// A definition + two call sites, plus a struct with a method (for documentSymbol nesting).
|
||||
const NAV_RS: &str = "pub fn helper() -> u32 {\n 7\n}\n\npub fn caller() -> u32 {\n helper() + helper()\n}\n\npub struct Widget {\n pub size: u32,\n}\n\nimpl Widget {\n pub fn area(&self) -> u32 {\n self.size * self.size\n }\n}\n";
|
||||
|
||||
fn ctx(cwd: &Path) -> ToolContext {
|
||||
let (event_tx, _rx) = mpsc::channel(8);
|
||||
let (_c, cancel_rx) = watch::channel(false);
|
||||
ToolContext {
|
||||
cwd: cwd.to_path_buf(),
|
||||
session_id: "real-ra".to_string(),
|
||||
cancelled: Arc::new(cancel_rx),
|
||||
event_tx,
|
||||
hook_ctx: None,
|
||||
question_broker: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn text(result: &ToolResultData) -> String {
|
||||
match &result.content[0] {
|
||||
ContentBlock::Text(t) => t.clone(),
|
||||
_ => panic!("expected text"),
|
||||
}
|
||||
}
|
||||
|
||||
/// A tool result as the model receives it: its text, and whether Luxe called the call a failure.
|
||||
fn answer(result: &ToolResultData) -> (bool, String) {
|
||||
(result.is_error, text(result))
|
||||
}
|
||||
|
||||
/// Whether Luxe declined the request by naming the server that would not run.
|
||||
///
|
||||
/// A host is entitled to have no *working* server for a language even though one resolves on
|
||||
/// `PATH` — a version-manager shim with no version set is exactly that, and [`have`] cannot see
|
||||
/// it. Luxe failing the call and naming the server, in the server's own words, is this contract
|
||||
/// holding rather than breaking. What must never pass is the silent empty answer, which a reader
|
||||
/// cannot tell apart from "your file is fine".
|
||||
fn declined(failed: bool, out: &str, server: &str) -> bool {
|
||||
failed && out.contains(server)
|
||||
}
|
||||
|
||||
/// Accept this answer, or hand it back so the failure can quote what actually arrived.
|
||||
fn accept(good: bool, out: String) -> Result<String, String> {
|
||||
if good {
|
||||
Ok(out)
|
||||
} else {
|
||||
Err(out)
|
||||
}
|
||||
}
|
||||
|
||||
fn have_rust_analyzer() -> bool {
|
||||
Path::new("/usr/bin/rust-analyzer").exists()
|
||||
|| std::env::var_os("PATH").is_some_and(|paths| {
|
||||
std::env::split_paths(&paths).any(|dir| dir.join("rust-analyzer").exists())
|
||||
})
|
||||
}
|
||||
|
||||
/// A directory of this test's own, deleted when it is dropped; it reads as the path it holds.
|
||||
struct Scratch(tempfile::TempDir);
|
||||
|
||||
impl std::ops::Deref for Scratch {
|
||||
type Target = std::path::Path;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.0.path()
|
||||
}
|
||||
}
|
||||
|
||||
/// Write the fixture Cargo project into a fresh temp dir and return its root.
|
||||
fn fixture(tag: &str) -> Scratch {
|
||||
let scratch = Scratch(
|
||||
tempfile::Builder::new()
|
||||
.prefix(&format!("luxe-real-ra-{tag}-"))
|
||||
.tempdir()
|
||||
.expect("temp dir"),
|
||||
);
|
||||
let dir: &Path = &scratch;
|
||||
std::fs::create_dir_all(dir.join("src")).unwrap();
|
||||
std::fs::write(dir.join("Cargo.toml"), CARGO_TOML).unwrap();
|
||||
std::fs::write(dir.join("src/lib.rs"), LIB_RS).unwrap();
|
||||
std::fs::write(dir.join("src/bad.rs"), BAD_RS).unwrap();
|
||||
std::fs::write(dir.join("src/good.rs"), GOOD_RS).unwrap();
|
||||
std::fs::write(dir.join("src/nav.rs"), NAV_RS).unwrap();
|
||||
scratch
|
||||
}
|
||||
|
||||
/// Poll `f` until it accepts an answer, or the deadline passes.
|
||||
///
|
||||
/// The rejected answers are not thrown away — the last one comes back with the failure. These
|
||||
/// tests spend up to ninety seconds before they give up, and "must surface a type error" on its
|
||||
/// own says nothing about *what* arrived instead, which is the whole of the diagnosis.
|
||||
async fn poll_until<F, Fut>(deadline: Instant, mut f: F) -> Result<String, String>
|
||||
where
|
||||
F: FnMut() -> Fut,
|
||||
Fut: std::future::Future<Output = Result<String, String>>,
|
||||
{
|
||||
let mut last = String::from("(the tool was never reached)");
|
||||
while Instant::now() < deadline {
|
||||
match f().await {
|
||||
Ok(value) => return Ok(value),
|
||||
Err(rejected) => last = rejected,
|
||||
}
|
||||
tokio::time::sleep(Duration::from_secs(2)).await;
|
||||
}
|
||||
Err(last)
|
||||
}
|
||||
|
||||
fn have(cmd: &str) -> bool {
|
||||
std::env::var_os("PATH")
|
||||
.is_some_and(|paths| std::env::split_paths(&paths).any(|dir| dir.join(cmd).exists()))
|
||||
}
|
||||
|
||||
mod other_servers;
|
||||
mod rust_analyzer;
|
||||
488
crates/luxe-lsp/tests/integration_real_lsp/other_servers.rs
Normal file
488
crates/luxe-lsp/tests/integration_real_lsp/other_servers.rs
Normal file
@@ -0,0 +1,488 @@
|
||||
//! Other servers — TypeScript, Python, Go and the rest answer the same navigation questions.
|
||||
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "spawns real typescript-language-server; run with --run-ignored"]
|
||||
async fn real_ts_diagnostics_and_symbols() {
|
||||
if !have("typescript-language-server") {
|
||||
eprintln!("typescript-language-server not found; skipping");
|
||||
return;
|
||||
}
|
||||
let dir_guard = tempfile::tempdir().expect("temp dir");
|
||||
let dir = dir_guard.path().to_path_buf();
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
std::fs::write(
|
||||
dir.join("package.json"),
|
||||
"{\n \"name\": \"t\",\n \"version\": \"1.0.0\"\n}\n",
|
||||
)
|
||||
.unwrap();
|
||||
std::fs::write(
|
||||
dir.join("tsconfig.json"),
|
||||
"{\n \"compilerOptions\": { \"strict\": true }\n}\n",
|
||||
)
|
||||
.unwrap();
|
||||
std::fs::write(
|
||||
dir.join("index.ts"),
|
||||
"export function bad(): number {\n return \"not a number\";\n}\n\nexport class Widget {\n size = 1;\n area(): number { return this.size; }\n}\n",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let service = Arc::new(WorkspaceLspClient::new());
|
||||
let diag = LspDiagnosticsTool::new(service);
|
||||
let cx = ctx(&dir);
|
||||
|
||||
// Which server answers depends on the host, and all three outcomes are Luxe working. A
|
||||
// TypeScript 5.x tsserver pushes the type error; TypeScript 7's native LSP pushes nothing at
|
||||
// all and has to be *asked* for it; a host with neither gets a refusal naming the servers that
|
||||
// would not run. The one result that is a defect is a bare "No diagnostics", which says the
|
||||
// file is fine — and that is what shipped for every `.ts` file against TypeScript 7.
|
||||
let deadline = Instant::now() + Duration::from_secs(40);
|
||||
let out = poll_until(deadline, || async {
|
||||
let (failed, out) = answer(
|
||||
&diag.run(serde_json::json!({ "file": "index.ts", "waitMs": 12000 }), &cx).await,
|
||||
);
|
||||
let explained = out.contains("note: language server reported")
|
||||
|| declined(failed, &out, "typescript-language-server");
|
||||
accept(out.contains("ERROR") || explained, out)
|
||||
})
|
||||
.await
|
||||
.unwrap_or_else(|last| {
|
||||
panic!("luxe must surface the type error, or say why it cannot — got:\n{last}")
|
||||
});
|
||||
eprintln!("TS DIAG:\n{out}");
|
||||
if !out.contains("ERROR") {
|
||||
eprintln!("no usable TypeScript server on this host, and Luxe said so — nothing to check");
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "spawns real pyright-langserver; run with --run-ignored"]
|
||||
async fn real_pyright_diagnostics_and_symbols() {
|
||||
if !have("pyright-langserver") {
|
||||
eprintln!("pyright-langserver not found; skipping");
|
||||
return;
|
||||
}
|
||||
let dir_guard = tempfile::tempdir().expect("temp dir");
|
||||
let dir = dir_guard.path().to_path_buf();
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
std::fs::write(dir.join("pyproject.toml"), "[tool.pyright]\ntypeCheckingMode = \"basic\"\n")
|
||||
.unwrap();
|
||||
std::fs::write(
|
||||
dir.join("app.py"),
|
||||
"def bad() -> int:\n return \"not a number\"\n\n\nclass Widget:\n def area(self) -> int:\n return 1\n",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let service = Arc::new(WorkspaceLspClient::new());
|
||||
let diag = LspDiagnosticsTool::new(service.clone());
|
||||
let nav = LspNavigationTool::new(service);
|
||||
let cx = ctx(&dir);
|
||||
|
||||
let deadline = Instant::now() + Duration::from_secs(60);
|
||||
let reported = poll_until(deadline, || async {
|
||||
let (failed, out) =
|
||||
answer(&diag.run(serde_json::json!({ "file": "app.py", "waitMs": 12000 }), &cx).await);
|
||||
let declined = declined(failed, &out, "pyright-langserver");
|
||||
accept(out.contains("ERROR") || declined, out)
|
||||
})
|
||||
.await
|
||||
.unwrap_or_else(|last| {
|
||||
panic!("pyright should report the return-type error, or say why it cannot — got:\n{last}")
|
||||
});
|
||||
eprintln!("PY DIAG:\n{reported}");
|
||||
if !reported.contains("ERROR") {
|
||||
eprintln!("pyright cannot run on this host, and Luxe said so — skipping the rest");
|
||||
return;
|
||||
}
|
||||
|
||||
let symbols = text(
|
||||
&nav.run(serde_json::json!({ "action": "document_symbols", "file": "app.py" }), &cx).await,
|
||||
);
|
||||
eprintln!("PY SYMBOLS:\n{symbols}");
|
||||
assert!(symbols.contains("bad") && symbols.contains("Widget"), "symbols listed: {symbols}");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "spawns real gopls; run with --run-ignored"]
|
||||
async fn real_gopls_diagnostics_and_symbols() {
|
||||
if !have("gopls") {
|
||||
eprintln!("gopls not found; skipping");
|
||||
return;
|
||||
}
|
||||
let dir_guard = tempfile::tempdir().expect("temp dir");
|
||||
let dir = dir_guard.path().to_path_buf();
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
std::fs::write(dir.join("go.mod"), "module gofixture\n\ngo 1.21\n").unwrap();
|
||||
std::fs::write(
|
||||
dir.join("main.go"),
|
||||
"package main\n\nfunc bad() int {\n\treturn \"not a number\"\n}\n\nfunc Area(size int) int {\n\treturn size * size\n}\n\nfunc main() {\n\t_ = bad()\n\t_ = Area(2)\n}\n",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let service = Arc::new(WorkspaceLspClient::new());
|
||||
let diag = LspDiagnosticsTool::new(service.clone());
|
||||
let nav = LspNavigationTool::new(service);
|
||||
let cx = ctx(&dir);
|
||||
|
||||
let deadline = Instant::now() + Duration::from_secs(90);
|
||||
let reported = poll_until(deadline, || async {
|
||||
let (failed, out) =
|
||||
answer(&diag.run(serde_json::json!({ "file": "main.go", "waitMs": 15000 }), &cx).await);
|
||||
let declined = declined(failed, &out, "gopls");
|
||||
accept(out.contains("ERROR") || declined, out)
|
||||
})
|
||||
.await
|
||||
.unwrap_or_else(|last| {
|
||||
panic!("gopls should report the return-type error, or say why it cannot — got:\n{last}")
|
||||
});
|
||||
eprintln!("GO DIAG:\n{reported}");
|
||||
if !reported.contains("ERROR") {
|
||||
eprintln!("gopls cannot run on this host, and Luxe said so — skipping the rest");
|
||||
return;
|
||||
}
|
||||
|
||||
let symbols = text(
|
||||
&nav.run(serde_json::json!({ "action": "document_symbols", "file": "main.go" }), &cx).await,
|
||||
);
|
||||
eprintln!("GO SYMBOLS:\n{symbols}");
|
||||
assert!(symbols.contains("bad") && symbols.contains("Area"), "symbols listed: {symbols}");
|
||||
}
|
||||
|
||||
/// Navigation, not just diagnostics, on the servers that are not rust-analyzer.
|
||||
///
|
||||
/// Every other language was covered by "does it report an error and list symbols", which exercises
|
||||
/// `publishDiagnostics` and `documentSymbol` and nothing else. Definition, references and hover go
|
||||
/// through different requests, different response shapes, and the position translation that turns
|
||||
/// a 1-based line into a UTF-16 offset — so a server that disagreed about any of those went
|
||||
/// unnoticed on every language but Rust.
|
||||
///
|
||||
/// One test per language, deliberately: five servers sharing one test share its deadline, and the
|
||||
/// last of them starves. A failure should name the language rather than the batch.
|
||||
struct Case {
|
||||
server: &'static str,
|
||||
files: &'static [(&'static str, &'static str)],
|
||||
file: &'static str,
|
||||
/// A call site of the symbol, 1-based, as `(line, character)`.
|
||||
///
|
||||
/// Definition is asked from here rather than from the symbol's own name: servers disagree
|
||||
/// about answering "go to definition" when you are already standing on it (clangd returns
|
||||
/// nothing, tsserver returns the definition), and that disagreement is not Luxe's to assert.
|
||||
/// A call site is the question every server answers the same way — and it exercises the
|
||||
/// line/character path, which the `symbol` shorthand does not.
|
||||
call_site: (u32, u32),
|
||||
/// A word the hover text must contain, in that language's own vocabulary.
|
||||
hover_says: &'static str,
|
||||
}
|
||||
|
||||
/// Drive one language's server through definition, references and hover.
|
||||
///
|
||||
/// The contract asserted is Luxe's, not the server's: **navigate, or say why not.** A server that
|
||||
/// cannot answer is a real situation — a `typescript-language-server` with no usable `typescript`
|
||||
/// to drive answers everything with nothing, and a version-manager shim resolves on `PATH` and
|
||||
/// then refuses to run at all — and the failure that matters is Luxe passing either off as "no
|
||||
/// definition exists". Any of the three is a pass; a bare empty answer is not.
|
||||
async fn navigates(case: Case) {
|
||||
if !have(case.server) {
|
||||
eprintln!("{} not found; skipping", case.server);
|
||||
return;
|
||||
}
|
||||
let dir_guard = tempfile::tempdir().expect("temp dir");
|
||||
let dir = dir_guard.path().to_path_buf();
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
for (name, body) in case.files {
|
||||
std::fs::write(dir.join(name), body).unwrap();
|
||||
}
|
||||
let service = Arc::new(WorkspaceLspClient::new());
|
||||
let nav = LspNavigationTool::new(service);
|
||||
let cx = ctx(&dir);
|
||||
let deadline = Instant::now() + Duration::from_secs(45);
|
||||
let at = serde_json::json!({
|
||||
"file": case.file, "line": case.call_site.0, "character": case.call_site.1
|
||||
});
|
||||
let ask = |action: &'static str| {
|
||||
let (nav, cx, at) = (&nav, &cx, at.clone());
|
||||
async move {
|
||||
let mut args = at;
|
||||
args["action"] = serde_json::Value::String(action.to_string());
|
||||
answer(&nav.run(args, cx).await)
|
||||
}
|
||||
};
|
||||
|
||||
let definition = poll_until(deadline, || async {
|
||||
let (failed, out) = ask("go_to_definition").await;
|
||||
// `Definition:` and nothing else — the note quotes the file's own URI, so matching the
|
||||
// filename would match the failure too.
|
||||
let answered = out.starts_with("Definition:");
|
||||
let explained =
|
||||
out.contains("note: language server reported") || declined(failed, &out, case.server);
|
||||
accept(answered || explained, out)
|
||||
})
|
||||
.await
|
||||
.unwrap_or_else(|last| {
|
||||
panic!("{}: go_to_definition must resolve or say why not — got:\n{last}", case.server)
|
||||
});
|
||||
eprintln!("{} DEFINITION: {}", case.server, definition.trim());
|
||||
if !definition.starts_with("Definition:") {
|
||||
eprintln!("{}: cannot navigate on this host, and said so — skipping the rest", case.server);
|
||||
return;
|
||||
}
|
||||
|
||||
let (_, references) = ask("find_references").await;
|
||||
eprintln!("{} REFERENCES:\n{}", case.server, references.trim());
|
||||
assert!(!references.starts_with("Error:"), "{}: {references}", case.server);
|
||||
assert!(
|
||||
references.matches(case.file).count() >= 2,
|
||||
"{}: two call sites plus the definition: {references}",
|
||||
case.server
|
||||
);
|
||||
|
||||
let (_, hover) = ask("hover").await;
|
||||
eprintln!("{} HOVER: {}", case.server, hover.trim());
|
||||
assert!(hover.contains(case.hover_says), "{}: {hover}", case.server);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "spawns a real typescript-language-server; run with --run-ignored"]
|
||||
async fn real_ts_navigates() {
|
||||
navigates(Case {
|
||||
server: "typescript-language-server",
|
||||
files: &[
|
||||
("package.json", "{\"name\":\"t\",\"version\":\"1.0.0\"}\n"),
|
||||
("tsconfig.json", "{\"compilerOptions\":{\"strict\":true}}\n"),
|
||||
("index.ts", "export function helper(): number {\n return 7;\n}\n\nexport function caller(): number {\n return helper() + helper();\n}\n"),
|
||||
],
|
||||
file: "index.ts",
|
||||
call_site: (6, 10),
|
||||
hover_says: "number",
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "spawns a real pyright; run with --run-ignored"]
|
||||
async fn real_pyright_navigates() {
|
||||
navigates(Case {
|
||||
server: "pyright-langserver",
|
||||
files: &[
|
||||
("pyproject.toml", "[tool.pyright]\ntypeCheckingMode = \"basic\"\n"),
|
||||
("app.py", "def helper() -> int:\n return 7\n\n\ndef caller() -> int:\n return helper() + helper()\n"),
|
||||
],
|
||||
file: "app.py",
|
||||
call_site: (6, 12),
|
||||
hover_says: "int",
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "spawns a real gopls; run with --run-ignored"]
|
||||
async fn real_gopls_navigates() {
|
||||
navigates(Case {
|
||||
server: "gopls",
|
||||
files: &[
|
||||
("go.mod", "module nav\n\ngo 1.21\n"),
|
||||
("main.go", "package main\n\nfunc helper() int {\n\treturn 7\n}\n\nfunc caller() int {\n\treturn helper() + helper()\n}\n"),
|
||||
],
|
||||
file: "main.go",
|
||||
call_site: (8, 10),
|
||||
hover_says: "int",
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "spawns a real bash-language-server; run with --run-ignored"]
|
||||
async fn real_bash_navigates() {
|
||||
navigates(Case {
|
||||
server: "bash-language-server",
|
||||
files: &[
|
||||
(".git", ""),
|
||||
("script.sh", "#!/usr/bin/env bash\n\nhelper() {\n echo 7\n}\n\ncaller() {\n helper\n helper\n}\n"),
|
||||
],
|
||||
file: "script.sh",
|
||||
call_site: (8, 4),
|
||||
hover_says: "helper",
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "spawns a real clangd; run with --run-ignored"]
|
||||
async fn real_clangd_navigates() {
|
||||
navigates(Case {
|
||||
server: "clangd",
|
||||
files: &[
|
||||
("compile_flags.txt", "-std=c11\n"),
|
||||
("main.c", "int helper(void) {\n return 7;\n}\n\nint caller(void) {\n return helper() + helper();\n}\n"),
|
||||
],
|
||||
file: "main.c",
|
||||
call_site: (6, 12),
|
||||
hover_says: "int",
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
/// The config languages: a malformed file has to be reported like any other error.
|
||||
///
|
||||
/// JSON and YAML carry the settings an agent edits most often and gets subtly wrong — a trailing
|
||||
/// comma, a tab where the format forbids one. Both servers are npm packages and neither is
|
||||
/// interesting beyond "does the registry entry name it correctly and does the error arrive", which
|
||||
/// is exactly what an unverified entry cannot promise.
|
||||
#[tokio::test]
|
||||
#[ignore = "spawns real json/yaml language servers; run with --run-ignored"]
|
||||
async fn real_config_language_servers_report_malformed_files() {
|
||||
let cases: &[(&str, &str, &str)] = &[
|
||||
("vscode-json-language-server", "settings.json", "{\n \"a\": 1,\n}\n"),
|
||||
("yaml-language-server", "config.yaml", "root:\n good: 1\n\tbad: 2\n"),
|
||||
];
|
||||
for (server, file, body) in cases {
|
||||
if !have(server) {
|
||||
eprintln!("{server} not found; skipping");
|
||||
continue;
|
||||
}
|
||||
let dir_guard = tempfile::tempdir().expect("temp dir");
|
||||
let dir = dir_guard.path().to_path_buf();
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
std::fs::write(dir.join(".git"), "").unwrap();
|
||||
std::fs::write(dir.join(file), body).unwrap();
|
||||
|
||||
let service = Arc::new(WorkspaceLspClient::new());
|
||||
let diag = LspDiagnosticsTool::new(service);
|
||||
let cx = ctx(&dir);
|
||||
let deadline = Instant::now() + Duration::from_secs(45);
|
||||
let reported = poll_until(deadline, || async {
|
||||
let (failed, out) =
|
||||
answer(&diag.run(serde_json::json!({ "file": file, "waitMs": 8000 }), &cx).await);
|
||||
let found = out.contains("ERROR") || out.contains("WARN");
|
||||
accept(found || declined(failed, &out, server), out)
|
||||
})
|
||||
.await
|
||||
.unwrap_or_else(|last| {
|
||||
panic!(
|
||||
"{server} should report the malformed {file}, or say why it cannot — got:\n{last}"
|
||||
)
|
||||
});
|
||||
eprintln!("{server} DIAG:\n{}", reported.trim());
|
||||
}
|
||||
}
|
||||
|
||||
/// Ruby and Lua: diagnostics and navigation, like every other entry in the registry.
|
||||
///
|
||||
/// Added last and verified the same way as the rest — a registry entry nobody has watched answer a
|
||||
/// real request is a claim, not support.
|
||||
#[tokio::test]
|
||||
#[ignore = "spawns a real ruby-lsp; run with --run-ignored"]
|
||||
async fn real_ruby_navigates() {
|
||||
navigates(Case {
|
||||
server: "ruby-lsp",
|
||||
files: &[
|
||||
("Gemfile", "source 'https://rubygems.org'\n"),
|
||||
("app.rb", "def helper\n 7\nend\n\ndef caller_method\n helper + helper\nend\n"),
|
||||
],
|
||||
file: "app.rb",
|
||||
call_site: (6, 3),
|
||||
hover_says: "helper",
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "spawns a real lua-language-server; run with --run-ignored"]
|
||||
async fn real_lua_navigates() {
|
||||
navigates(Case {
|
||||
server: "lua-language-server",
|
||||
files: &[
|
||||
(".luarc.json", "{\n \"runtime.version\": \"Lua 5.4\"\n}\n"),
|
||||
(
|
||||
"main.lua",
|
||||
"local function helper()\n return 7\nend\n\nlocal function caller()\n return helper() + helper()\nend\n\nreturn caller\n",
|
||||
),
|
||||
],
|
||||
file: "main.lua",
|
||||
call_site: (6, 11),
|
||||
hover_says: "helper",
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
/// Svelte: a component is two languages in one file, and only the server knows which is which.
|
||||
///
|
||||
/// The reason this needs its own case rather than riding on TypeScript's: `svelteserver` does not
|
||||
/// answer about the file on disk. It runs the component through `svelte2tsx` and asks a TypeScript
|
||||
/// service about the *generated* module, then maps every position back — so a definition, a
|
||||
/// reference and a hover each make a round trip through a source map that the `.ts` path never
|
||||
/// touches. A registry entry that merely names the binary proves none of that survived.
|
||||
#[tokio::test]
|
||||
#[ignore = "spawns a real svelteserver; run with --run-ignored"]
|
||||
async fn real_svelte_navigates() {
|
||||
navigates(Case {
|
||||
server: "svelteserver",
|
||||
files: &[
|
||||
("package.json", "{\"name\":\"s\",\"version\":\"1.0.0\",\"type\":\"module\"}\n"),
|
||||
("svelte.config.js", "export default {};\n"),
|
||||
(
|
||||
"App.svelte",
|
||||
"<script lang=\"ts\">\n function helper(): number {\n return 7;\n }\n\n function caller(): number {\n return helper() + helper();\n }\n</script>\n\n<p>{caller()}</p>\n",
|
||||
),
|
||||
],
|
||||
file: "App.svelte",
|
||||
call_site: (7, 12),
|
||||
hover_says: "number",
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
/// The type error inside a component's `<script>` reaches the model.
|
||||
///
|
||||
/// Separate from navigation because it is the failure that matters most and the one a source map
|
||||
/// most easily loses: a diagnostic reported against the generated module lands on the wrong line of
|
||||
/// the component, or on no line at all. A wrong line is worse than none — the model edits what the
|
||||
/// number pointed at.
|
||||
#[tokio::test]
|
||||
#[ignore = "spawns a real svelteserver; run with --run-ignored"]
|
||||
async fn real_svelte_diagnostics_land_on_the_component_line() {
|
||||
if !have("svelteserver") {
|
||||
eprintln!("svelteserver not found; skipping");
|
||||
return;
|
||||
}
|
||||
let dir_guard = tempfile::tempdir().expect("temp dir");
|
||||
let dir = dir_guard.path().to_path_buf();
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
std::fs::write(dir.join("package.json"), "{\"name\":\"s\",\"version\":\"1.0.0\"}\n").unwrap();
|
||||
std::fs::write(dir.join("svelte.config.js"), "export default {};\n").unwrap();
|
||||
// The bad return is on line 3 of the component, and nowhere near line 3 of what `svelte2tsx`
|
||||
// hands the TypeScript service.
|
||||
std::fs::write(
|
||||
dir.join("Bad.svelte"),
|
||||
"<script lang=\"ts\">\n function bad(): number {\n return \"not a number\";\n }\n</script>\n\n<p>{bad()}</p>\n",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let service = Arc::new(WorkspaceLspClient::new());
|
||||
let diag = LspDiagnosticsTool::new(service);
|
||||
let cx = ctx(&dir);
|
||||
|
||||
let deadline = Instant::now() + Duration::from_secs(60);
|
||||
let out = poll_until(deadline, || async {
|
||||
let (failed, out) = answer(
|
||||
&diag.run(serde_json::json!({ "file": "Bad.svelte", "waitMs": 15000 }), &cx).await,
|
||||
);
|
||||
let explained = out.contains("note: language server reported")
|
||||
|| declined(failed, &out, "svelteserver");
|
||||
accept(out.contains("ERROR") || explained, out)
|
||||
})
|
||||
.await
|
||||
.unwrap_or_else(|last| {
|
||||
panic!("luxe must surface the component's type error, or say why it cannot — got:\n{last}")
|
||||
});
|
||||
eprintln!("SVELTE DIAG:\n{out}");
|
||||
if out.contains("ERROR") {
|
||||
assert!(
|
||||
out.contains("] 3:"),
|
||||
"the error belongs to line 3 of the component, not of the generated module:\n{out}"
|
||||
);
|
||||
} else {
|
||||
eprintln!("no usable Svelte server on this host, and Luxe said so — nothing to check");
|
||||
}
|
||||
}
|
||||
442
crates/luxe-lsp/tests/integration_real_lsp/rust_analyzer.rs
Normal file
442
crates/luxe-lsp/tests/integration_real_lsp/rust_analyzer.rs
Normal file
@@ -0,0 +1,442 @@
|
||||
//! Rust-analyzer — diagnostics, navigation, rename and code actions against the real server.
|
||||
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "spawns real rust-analyzer; run with --run-ignored"]
|
||||
async fn real_ra_diagnostics_report_error_and_clean_is_empty() {
|
||||
if !have_rust_analyzer() {
|
||||
eprintln!("rust-analyzer not found; skipping");
|
||||
return;
|
||||
}
|
||||
let dir = fixture("diag");
|
||||
let service = Arc::new(WorkspaceLspClient::new());
|
||||
let tool = LspDiagnosticsTool::new(service);
|
||||
let cx = ctx(&dir);
|
||||
|
||||
// The type-error file must eventually report a real error (not an empty pre-index snapshot).
|
||||
let deadline = Instant::now() + Duration::from_secs(90);
|
||||
let reported = poll_until(deadline, || async {
|
||||
let out = text(
|
||||
&tool.run(serde_json::json!({ "file": "src/bad.rs", "waitMs": 15000 }), &cx).await,
|
||||
);
|
||||
accept(out.contains("ERROR"), out)
|
||||
})
|
||||
.await
|
||||
.unwrap_or_else(|last| {
|
||||
panic!("rust-analyzer should report the type error in bad.rs — got:\n{last}")
|
||||
});
|
||||
eprintln!("DIAG(bad.rs):\n{reported}");
|
||||
assert!(reported.contains("ERROR"), "real error surfaced");
|
||||
assert!(
|
||||
reported.to_lowercase().contains("mismatch")
|
||||
|| reported.to_lowercase().contains("expected"),
|
||||
"error mentions the type mismatch: {reported}"
|
||||
);
|
||||
|
||||
// A clean file returns "No diagnostics" quickly — no 10s hang (server is warm now).
|
||||
let started = Instant::now();
|
||||
let clean =
|
||||
text(&tool.run(serde_json::json!({ "file": "src/good.rs", "waitMs": 10000 }), &cx).await);
|
||||
let elapsed = started.elapsed();
|
||||
eprintln!("DIAG(good.rs) in {elapsed:?}:\n{clean}");
|
||||
assert!(clean.contains("No diagnostics"), "clean file reports none: {clean}");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "spawns real rust-analyzer; run with --run-ignored"]
|
||||
async fn real_ra_go_to_definition_resolves_location_link() {
|
||||
if !have_rust_analyzer() {
|
||||
eprintln!("rust-analyzer not found; skipping");
|
||||
return;
|
||||
}
|
||||
let dir = fixture("def");
|
||||
let service = Arc::new(WorkspaceLspClient::new());
|
||||
let nav = LspNavigationTool::new(service);
|
||||
let cx = ctx(&dir);
|
||||
|
||||
// `helper()` is called on line 6 (0-based 5); go to its definition on line 1 (0-based 0).
|
||||
let deadline = Instant::now() + Duration::from_secs(90);
|
||||
let resolved = poll_until(deadline, || async {
|
||||
let out = text(
|
||||
&nav.run(
|
||||
serde_json::json!({
|
||||
"action": "go_to_definition",
|
||||
"file": "src/nav.rs",
|
||||
"line": 6,
|
||||
"character": 5
|
||||
}),
|
||||
&cx,
|
||||
)
|
||||
.await,
|
||||
);
|
||||
// Resolves to nav.rs (the definition site), not an error/empty.
|
||||
accept(out.contains("nav.rs") && !out.to_lowercase().contains("no "), out)
|
||||
})
|
||||
.await
|
||||
.unwrap_or_else(|last| panic!("go_to_definition should resolve helper() — got:\n{last}"));
|
||||
eprintln!("DEF:\n{resolved}");
|
||||
assert!(resolved.contains("nav.rs"), "definition points into nav.rs: {resolved}");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "spawns real rust-analyzer; run with --run-ignored"]
|
||||
async fn real_ra_document_symbols_flatten_hierarchy() {
|
||||
if !have_rust_analyzer() {
|
||||
eprintln!("rust-analyzer not found; skipping");
|
||||
return;
|
||||
}
|
||||
let dir = fixture("docsym");
|
||||
let service = Arc::new(WorkspaceLspClient::new());
|
||||
let nav = LspNavigationTool::new(service);
|
||||
let cx = ctx(&dir);
|
||||
|
||||
let deadline = Instant::now() + Duration::from_secs(90);
|
||||
let symbols = poll_until(deadline, || async {
|
||||
let out = text(
|
||||
&nav.run(
|
||||
serde_json::json!({ "action": "document_symbols", "file": "src/nav.rs" }),
|
||||
&cx,
|
||||
)
|
||||
.await,
|
||||
);
|
||||
// rust-analyzer returns a hierarchical DocumentSymbol; flattened output should carry the
|
||||
// struct, its method, and the free functions.
|
||||
accept(out.contains("Widget") && out.contains("area") && out.contains("helper"), out)
|
||||
})
|
||||
.await
|
||||
.unwrap_or_else(|last| {
|
||||
panic!("document_symbols should list Widget/area/helper — got:\n{last}")
|
||||
});
|
||||
eprintln!("DOCSYM:\n{symbols}");
|
||||
assert!(symbols.contains("Widget") && symbols.contains("area"), "nested method flattened");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "spawns real rust-analyzer; run with --run-ignored"]
|
||||
async fn real_ra_references_and_incoming_call_hierarchy() {
|
||||
if !have_rust_analyzer() {
|
||||
eprintln!("rust-analyzer not found; skipping");
|
||||
return;
|
||||
}
|
||||
let dir = fixture("refs");
|
||||
let service = Arc::new(WorkspaceLspClient::new());
|
||||
let nav = LspNavigationTool::new(service);
|
||||
let cx = ctx(&dir);
|
||||
|
||||
// helper() is called twice by caller(); references should find both call sites.
|
||||
let deadline = Instant::now() + Duration::from_secs(90);
|
||||
let refs = poll_until(deadline, || async {
|
||||
let out = text(
|
||||
&nav.run(
|
||||
serde_json::json!({ "action": "find_references", "file": "src/nav.rs", "symbol": "helper" }),
|
||||
&cx,
|
||||
)
|
||||
.await,
|
||||
);
|
||||
// Two call sites in nav.rs (declaration excluded).
|
||||
accept(out.contains("nav.rs") && out.contains("2 "), out)
|
||||
})
|
||||
.await
|
||||
.unwrap_or_else(|last| {
|
||||
panic!("find_references should locate both helper() call sites — got:\n{last}")
|
||||
});
|
||||
eprintln!("REFS:\n{refs}");
|
||||
assert!(refs.contains("nav.rs"), "references in nav.rs: {refs}");
|
||||
|
||||
// Incoming call hierarchy: helper is called by caller.
|
||||
let deadline = Instant::now() + Duration::from_secs(60);
|
||||
let calls = poll_until(deadline, || async {
|
||||
let out = text(
|
||||
&nav.run(
|
||||
serde_json::json!({ "action": "call_hierarchy", "file": "src/nav.rs", "symbol": "helper" }),
|
||||
&cx,
|
||||
)
|
||||
.await,
|
||||
);
|
||||
accept(out.contains("caller"), out)
|
||||
})
|
||||
.await
|
||||
.unwrap_or_else(|last| {
|
||||
panic!("call_hierarchy should report helper is called by caller — got:\n{last}")
|
||||
});
|
||||
eprintln!("CALLH:\n{calls}");
|
||||
assert!(calls.to_lowercase().contains("called by"), "incoming direction: {calls}");
|
||||
assert!(calls.contains("caller"), "caller listed: {calls}");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "spawns real rust-analyzer; run with --run-ignored"]
|
||||
async fn real_ra_rename_applies_across_files_with_utf16_offsets() {
|
||||
if !have_rust_analyzer() {
|
||||
eprintln!("rust-analyzer not found; skipping");
|
||||
return;
|
||||
}
|
||||
// Two-file rename with an astral emoji (surrogate pair) on the same line as a use site, to
|
||||
// stress UTF-16 -> byte offset conversion in apply_text_edits.
|
||||
let dir_guard = tempfile::tempdir().expect("temp dir");
|
||||
let dir = dir_guard.path().to_path_buf();
|
||||
std::fs::create_dir_all(dir.join("src")).unwrap();
|
||||
std::fs::write(dir.join("Cargo.toml"), CARGO_TOML).unwrap();
|
||||
std::fs::write(dir.join("src/lib.rs"), "pub mod a;\npub mod b;\n").unwrap();
|
||||
std::fs::write(dir.join("src/a.rs"), "pub fn greet() -> &'static str {\n \"hi\"\n}\n")
|
||||
.unwrap();
|
||||
let b_src = "use crate::a::greet;\n\npub fn run() -> String {\n format!(\"\u{1F600} {}\", greet())\n}\n";
|
||||
std::fs::write(dir.join("src/b.rs"), b_src).unwrap();
|
||||
|
||||
let service = Arc::new(WorkspaceLspClient::new());
|
||||
let nav = LspNavigationTool::new(service);
|
||||
let cx = ctx(&dir);
|
||||
|
||||
// Poll a preview until rust-analyzer resolves the cross-file usages (edits in both files).
|
||||
let deadline = Instant::now() + Duration::from_secs(90);
|
||||
let preview_ok = poll_until(deadline, || async {
|
||||
let out = text(
|
||||
&nav.run(
|
||||
serde_json::json!({ "action": "rename", "file": "src/a.rs", "symbol": "greet", "newName": "salutation" }),
|
||||
&cx,
|
||||
)
|
||||
.await,
|
||||
);
|
||||
accept(out.contains("a.rs") && out.contains("b.rs"), out)
|
||||
})
|
||||
.await
|
||||
.unwrap_or_else(|last| panic!("rename preview should span a.rs and b.rs — got:\n{last}"));
|
||||
eprintln!("RENAME PREVIEW:\n{preview_ok}");
|
||||
|
||||
// Apply it.
|
||||
let applied = text(
|
||||
&nav.run(
|
||||
serde_json::json!({ "action": "rename", "file": "src/a.rs", "symbol": "greet", "newName": "salutation", "apply": true }),
|
||||
&cx,
|
||||
)
|
||||
.await,
|
||||
);
|
||||
eprintln!("RENAME APPLIED:\n{applied}");
|
||||
assert!(applied.contains("Applied"), "apply reported: {applied}");
|
||||
|
||||
let a = std::fs::read_to_string(dir.join("src/a.rs")).unwrap();
|
||||
let b = std::fs::read_to_string(dir.join("src/b.rs")).unwrap();
|
||||
eprintln!("A:\n{a}\nB:\n{b}");
|
||||
|
||||
assert!(a.contains("pub fn salutation"), "definition renamed: {a}");
|
||||
assert!(!a.contains("greet"), "no stale greet in a.rs: {a}");
|
||||
assert!(b.contains("use crate::a::salutation"), "import renamed: {b}");
|
||||
assert!(b.contains("salutation()"), "call site renamed: {b}");
|
||||
assert!(!b.contains("greet"), "no stale greet in b.rs: {b}");
|
||||
// The emoji must survive intact — a UTF-16 offset bug would corrupt this line.
|
||||
assert!(b.contains("\u{1F600} {}"), "emoji line intact (UTF-16 offsets correct): {b}");
|
||||
}
|
||||
|
||||
const TRAIT_RS: &str = "pub trait Greeter {\n fn greet(&self) -> u32;\n}\n\npub struct En;\n\nimpl Greeter for En {\n fn greet(&self) -> u32 {\n 1\n }\n}\n\npub fn use_it() -> u32 {\n let g: En = En;\n g.greet()\n}\n";
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "spawns real rust-analyzer; run with --run-ignored"]
|
||||
async fn real_ra_implementation_and_type_definition() {
|
||||
if !have_rust_analyzer() {
|
||||
eprintln!("rust-analyzer not found; skipping");
|
||||
return;
|
||||
}
|
||||
let dir_guard = tempfile::tempdir().expect("temp dir");
|
||||
let dir = dir_guard.path().to_path_buf();
|
||||
std::fs::create_dir_all(dir.join("src")).unwrap();
|
||||
std::fs::write(dir.join("Cargo.toml"), CARGO_TOML).unwrap();
|
||||
std::fs::write(dir.join("src/lib.rs"), "pub mod t;\n").unwrap();
|
||||
std::fs::write(dir.join("src/t.rs"), TRAIT_RS).unwrap();
|
||||
|
||||
let service = Arc::new(WorkspaceLspClient::new());
|
||||
let nav = LspNavigationTool::new(service);
|
||||
let cx = ctx(&dir);
|
||||
|
||||
// implementation on the `Greeter` trait (line 1) resolves to the `impl ... for En` in t.rs.
|
||||
let deadline = Instant::now() + Duration::from_secs(90);
|
||||
let impls = poll_until(deadline, || async {
|
||||
let out = text(
|
||||
&nav.run(
|
||||
serde_json::json!({ "action": "implementation", "file": "src/t.rs", "line": 1, "character": 11 }),
|
||||
&cx,
|
||||
)
|
||||
.await,
|
||||
);
|
||||
accept(out.contains("t.rs") && !out.to_lowercase().contains("no "), out)
|
||||
})
|
||||
.await
|
||||
.unwrap_or_else(|last| {
|
||||
panic!("implementation should resolve to the impl block — got:\n{last}")
|
||||
});
|
||||
eprintln!("IMPL:\n{impls}");
|
||||
assert!(impls.contains("t.rs"), "impl located in t.rs: {impls}");
|
||||
|
||||
// type_definition on `g` (line 14: ` let g: En = En;`) resolves to the `En` struct.
|
||||
let deadline = Instant::now() + Duration::from_secs(60);
|
||||
let type_def = poll_until(deadline, || async {
|
||||
let out = text(
|
||||
&nav.run(
|
||||
serde_json::json!({ "action": "type_definition", "file": "src/t.rs", "line": 14, "character": 9 }),
|
||||
&cx,
|
||||
)
|
||||
.await,
|
||||
);
|
||||
accept(out.contains("t.rs") && !out.to_lowercase().contains("no "), out)
|
||||
})
|
||||
.await
|
||||
.unwrap_or_else(|last| panic!("type_definition of g should resolve to En — got:\n{last}"));
|
||||
eprintln!("TYPEDEF:\n{type_def}");
|
||||
assert!(type_def.contains("t.rs"), "type def located in t.rs: {type_def}");
|
||||
}
|
||||
|
||||
/// The four navigation actions nothing exercised, against a real server.
|
||||
///
|
||||
/// `hover`, `signature_help`, `workspace_symbol` and `code_action` were declared in the schema,
|
||||
/// reachable by the model on every request, and covered by no test at any level — a third of this
|
||||
/// tool's surface resting on the assumption that the same code path works for all twelve actions.
|
||||
#[tokio::test]
|
||||
#[ignore = "spawns real rust-analyzer; run with --run-ignored"]
|
||||
async fn real_ra_hover_signature_help_and_workspace_symbol() {
|
||||
if !have_rust_analyzer() {
|
||||
eprintln!("rust-analyzer not found; skipping");
|
||||
return;
|
||||
}
|
||||
let root = fixture("readonly-actions");
|
||||
let cx = ctx(&root);
|
||||
let service = Arc::new(WorkspaceLspClient::new());
|
||||
let nav = LspNavigationTool::new(service);
|
||||
let deadline = Instant::now() + Duration::from_secs(90);
|
||||
|
||||
let hover = poll_until(deadline, || async {
|
||||
let out = text(
|
||||
&nav.run(
|
||||
serde_json::json!({ "action": "hover", "file": "src/nav.rs", "symbol": "helper" }),
|
||||
&cx,
|
||||
)
|
||||
.await,
|
||||
);
|
||||
accept(out.contains("u32"), out)
|
||||
})
|
||||
.await
|
||||
.unwrap_or_else(|last| panic!("hover should describe helper's signature — got:\n{last}"));
|
||||
eprintln!("HOVER:\n{hover}");
|
||||
|
||||
let symbols = poll_until(deadline, || async {
|
||||
let out = text(
|
||||
&nav.run(
|
||||
serde_json::json!({
|
||||
"action": "workspace_symbol", "file": "src/nav.rs", "query": "helper"
|
||||
}),
|
||||
&cx,
|
||||
)
|
||||
.await,
|
||||
);
|
||||
accept(out.contains("helper"), out)
|
||||
})
|
||||
.await
|
||||
.unwrap_or_else(|last| panic!("workspace_symbol should find helper by name — got:\n{last}"));
|
||||
eprintln!("WORKSPACE SYMBOL:\n{symbols}");
|
||||
|
||||
// Inside `helper() + helper()`, between the parens of the first call.
|
||||
let signature = text(
|
||||
&nav.run(
|
||||
serde_json::json!({
|
||||
"action": "signature_help", "file": "src/nav.rs", "line": 6, "character": 12
|
||||
}),
|
||||
&cx,
|
||||
)
|
||||
.await,
|
||||
);
|
||||
eprintln!("SIGNATURE HELP:\n{signature}");
|
||||
assert!(!signature.starts_with("Error:"), "signature_help must not error: {signature}");
|
||||
|
||||
// Outgoing is the direction nothing covered; incoming is tested elsewhere.
|
||||
let outgoing = text(
|
||||
&nav.run(
|
||||
serde_json::json!({
|
||||
"action": "call_hierarchy", "file": "src/nav.rs", "symbol": "caller",
|
||||
"direction": "outgoing"
|
||||
}),
|
||||
&cx,
|
||||
)
|
||||
.await,
|
||||
);
|
||||
eprintln!("OUTGOING CALLS:\n{outgoing}");
|
||||
assert!(outgoing.contains("helper"), "caller's outgoing calls must include helper: {outgoing}");
|
||||
}
|
||||
|
||||
/// `code_action` previews by default and writes only when asked, and `title` picks which one.
|
||||
///
|
||||
/// The write path of a tool that edits files on the model's behalf, and it had no test at all.
|
||||
#[tokio::test]
|
||||
#[ignore = "spawns real rust-analyzer; run with --run-ignored"]
|
||||
async fn real_ra_code_action_previews_then_applies_the_one_named() {
|
||||
if !have_rust_analyzer() {
|
||||
eprintln!("rust-analyzer not found; skipping");
|
||||
return;
|
||||
}
|
||||
let root = fixture("code-action");
|
||||
// An unused import: every Rust server offers to remove it, under a stable title.
|
||||
std::fs::write(
|
||||
root.join("src/fix.rs"),
|
||||
"use std::collections::HashMap;\n\npub fn f() -> u32 {\n 1\n}\n",
|
||||
)
|
||||
.unwrap();
|
||||
std::fs::write(
|
||||
root.join("src/lib.rs"),
|
||||
"pub mod bad;\npub mod good;\npub mod nav;\npub mod fix;\n",
|
||||
)
|
||||
.unwrap();
|
||||
let cx = ctx(&root);
|
||||
let service = Arc::new(WorkspaceLspClient::new());
|
||||
let nav = LspNavigationTool::new(service);
|
||||
let deadline = Instant::now() + Duration::from_secs(90);
|
||||
let before = std::fs::read_to_string(root.join("src/fix.rs")).unwrap();
|
||||
|
||||
let listed = poll_until(deadline, || async {
|
||||
let out = text(
|
||||
&nav.run(
|
||||
serde_json::json!({
|
||||
"action": "code_action", "file": "src/fix.rs", "line": 1, "character": 5
|
||||
}),
|
||||
&cx,
|
||||
)
|
||||
.await,
|
||||
);
|
||||
accept(!out.starts_with("Error:") && out.to_lowercase().contains("remove"), out)
|
||||
})
|
||||
.await
|
||||
// No early-out: an empty list is exactly the failure this test exists for. Undeclared
|
||||
// `codeActionLiteralSupport` made rust-analyzer answer every request with zero actions, and a
|
||||
// "skip when nothing is offered" branch reported that as a pass — for 91 seconds, once per run.
|
||||
.unwrap_or_else(|last| {
|
||||
panic!(
|
||||
"rust-analyzer must offer a quickfix for an unused import; zero actions means the \
|
||||
client failed to declare codeActionLiteralSupport — got:\n{last}"
|
||||
)
|
||||
});
|
||||
eprintln!("CODE ACTIONS:\n{listed}");
|
||||
assert_eq!(
|
||||
std::fs::read_to_string(root.join("src/fix.rs")).unwrap(),
|
||||
before,
|
||||
"listing actions must not touch the file"
|
||||
);
|
||||
|
||||
// Pick by title, which is the parameter that had no coverage at all.
|
||||
let title = listed
|
||||
.lines()
|
||||
.map(str::trim)
|
||||
.find(|line| line.to_lowercase().contains("remove"))
|
||||
.map(|line| line.split(" [").next().unwrap_or(line).trim().to_string())
|
||||
.expect("a titled action");
|
||||
let applied = text(
|
||||
&nav.run(
|
||||
serde_json::json!({
|
||||
"action": "code_action", "file": "src/fix.rs", "line": 1, "character": 5,
|
||||
"apply": true, "title": title
|
||||
}),
|
||||
&cx,
|
||||
)
|
||||
.await,
|
||||
);
|
||||
eprintln!("APPLIED ({title}):\n{applied}");
|
||||
let after = std::fs::read_to_string(root.join("src/fix.rs")).unwrap();
|
||||
assert_ne!(after, before, "apply:true must write the edit: {applied}");
|
||||
assert!(!after.contains("HashMap"), "the unused import should be gone:\n{after}");
|
||||
}
|
||||
Reference in New Issue
Block a user