775 lines
25 KiB
Rust
775 lines
25 KiB
Rust
#![cfg(feature = "lsp")]
|
||
|
||
use std::{fs, ops::Range as ByteRange, path::PathBuf};
|
||
|
||
use futures::{FutureExt, StreamExt};
|
||
use john_slop::{
|
||
Config, ConfigLoader, ConfigPaths, apply_replacements,
|
||
lsp::{DiagnosticFilter, LspBackend},
|
||
plan_replacements,
|
||
};
|
||
use serde_json::{Value, json};
|
||
use tempfile::TempDir;
|
||
use tower::{Service, ServiceExt};
|
||
use tower_lsp_server::{
|
||
LspService,
|
||
jsonrpc::{ErrorCode, Request, Response},
|
||
};
|
||
|
||
struct Harness {
|
||
service: LspService<LspBackend>,
|
||
socket: tower_lsp_server::ClientSocket,
|
||
_directory: TempDir,
|
||
explicit: Option<PathBuf>,
|
||
diagnostics: Option<PathBuf>,
|
||
}
|
||
|
||
impl Harness {
|
||
async fn new(explicit_config: Option<&str>) -> Self {
|
||
Self::new_with_filter(explicit_config, None, DiagnosticFilter::UnicodeOnly).await
|
||
}
|
||
|
||
async fn new_all(explicit_config: Option<&str>) -> Self {
|
||
Self::new_with_filter(explicit_config, None, DiagnosticFilter::All).await
|
||
}
|
||
|
||
async fn new_with_diagnostics(
|
||
explicit_config: Option<&str>,
|
||
diagnostic_catalog: Option<&str>,
|
||
) -> Self {
|
||
Self::new_with_filter(explicit_config, diagnostic_catalog, DiagnosticFilter::All).await
|
||
}
|
||
|
||
async fn new_with_filter(
|
||
explicit_config: Option<&str>,
|
||
diagnostic_catalog: Option<&str>,
|
||
diagnostic_filter: DiagnosticFilter,
|
||
) -> Self {
|
||
let directory = tempfile::tempdir().unwrap();
|
||
let explicit = explicit_config.map(|contents| {
|
||
let path = directory.path().join("lsp.toml");
|
||
fs::write(&path, contents).unwrap();
|
||
path
|
||
});
|
||
let diagnostics = diagnostic_catalog.map(|contents| {
|
||
let path = directory.path().join("runtime-diagnostics.toml");
|
||
fs::write(&path, contents).unwrap();
|
||
path
|
||
});
|
||
let paths = ConfigPaths {
|
||
directory: directory.path().to_owned(),
|
||
base: directory.path().join("missing-base.toml"),
|
||
named: None,
|
||
explicit: explicit.clone(),
|
||
};
|
||
let loader =
|
||
ConfigLoader::from_paths(paths).with_diagnostic_files(diagnostics.iter().cloned());
|
||
let config = loader.load().unwrap();
|
||
let (service, socket) = LspService::new(|client| {
|
||
LspBackend::new(client, loader, config)
|
||
.expect("build test LSP backend")
|
||
.with_diagnostic_filter(diagnostic_filter)
|
||
});
|
||
let mut harness = Self {
|
||
service,
|
||
socket,
|
||
_directory: directory,
|
||
explicit,
|
||
diagnostics,
|
||
};
|
||
let response = harness
|
||
.request("initialize", json!({ "capabilities": {} }), 1)
|
||
.await;
|
||
assert!(response.is_ok());
|
||
assert_eq!(
|
||
response.result().unwrap()["capabilities"]["textDocumentSync"],
|
||
1
|
||
);
|
||
assert_eq!(
|
||
response.result().unwrap()["capabilities"]["documentFormattingProvider"],
|
||
true
|
||
);
|
||
harness.notify("initialized", json!({})).await;
|
||
harness
|
||
}
|
||
|
||
async fn request(&mut self, method: &'static str, params: Value, id: i64) -> Response {
|
||
let request = Request::build(method).params(params).id(id).finish();
|
||
self.service
|
||
.ready()
|
||
.await
|
||
.unwrap()
|
||
.call(request)
|
||
.await
|
||
.unwrap()
|
||
.unwrap()
|
||
}
|
||
|
||
async fn notify(&mut self, method: &'static str, params: Value) {
|
||
let request = Request::build(method).params(params).finish();
|
||
assert!(
|
||
self.service
|
||
.ready()
|
||
.await
|
||
.unwrap()
|
||
.call(request)
|
||
.await
|
||
.unwrap()
|
||
.is_none()
|
||
);
|
||
}
|
||
|
||
async fn notification(&mut self, method: &str) -> Value {
|
||
loop {
|
||
let request = self
|
||
.socket
|
||
.next()
|
||
.now_or_never()
|
||
.expect("server notification should already be queued")
|
||
.unwrap();
|
||
if request.method() == method {
|
||
return request.params().cloned().unwrap_or(Value::Null);
|
||
}
|
||
}
|
||
}
|
||
|
||
fn explicit_path(&self) -> &std::path::Path {
|
||
self.explicit.as_deref().unwrap()
|
||
}
|
||
|
||
fn diagnostics_path(&self) -> &std::path::Path {
|
||
self.diagnostics.as_deref().unwrap()
|
||
}
|
||
}
|
||
|
||
fn custom_catalog(enabled: bool) -> String {
|
||
format!(
|
||
r#"schema_version = 1
|
||
|
||
[[rules]]
|
||
id = "custom-review-marker"
|
||
category = "style"
|
||
severity = "warning"
|
||
matcher = "exact-phrase"
|
||
pattern = "REVIEW BEFORE PUBLISHING"
|
||
weight = 8.0
|
||
message = "a project review marker remains"
|
||
help = "Resolve or remove the marker before publishing."
|
||
occurrence_cap = 1
|
||
enabled = {enabled}
|
||
scope = {{ content = ["prose", "source-code"], extensions = [] }}
|
||
"#
|
||
)
|
||
}
|
||
|
||
fn open_params(uri: &str, version: i32, text: &str) -> Value {
|
||
open_params_with_language(uri, "plaintext", version, text)
|
||
}
|
||
|
||
fn open_params_with_language(uri: &str, language_id: &str, version: i32, text: &str) -> Value {
|
||
json!({
|
||
"textDocument": {
|
||
"uri": uri,
|
||
"languageId": language_id,
|
||
"version": version,
|
||
"text": text,
|
||
}
|
||
})
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn publishes_style_and_source_findings_without_non_fixable_actions() {
|
||
let mut harness = Harness::new_all(None).await;
|
||
let prose_uri = "file:///tmp/article.txt";
|
||
let prose = "Moreover, the first claim.\nAdditionally, the second claim.";
|
||
harness
|
||
.notify("textDocument/didOpen", open_params(prose_uri, 4, prose))
|
||
.await;
|
||
let published = harness
|
||
.notification("textDocument/publishDiagnostics")
|
||
.await;
|
||
let diagnostics = published["diagnostics"].as_array().unwrap();
|
||
assert_eq!(diagnostics.len(), 2);
|
||
assert!(
|
||
diagnostics
|
||
.iter()
|
||
.all(|diagnostic| diagnostic["code"] == "repeated-transition")
|
||
);
|
||
assert!(
|
||
diagnostics
|
||
.iter()
|
||
.all(|diagnostic| diagnostic["severity"] == 3)
|
||
);
|
||
assert!(
|
||
diagnostics[0]["message"]
|
||
.as_str()
|
||
.unwrap()
|
||
.contains("Help:")
|
||
);
|
||
assert_eq!(diagnostics[0]["data"]["fixable"], false);
|
||
|
||
let response = harness
|
||
.request(
|
||
"textDocument/codeAction",
|
||
json!({
|
||
"textDocument": { "uri": prose_uri },
|
||
"range": {
|
||
"start": { "line": 0, "character": 0 },
|
||
"end": { "line": 1, "character": 35 }
|
||
},
|
||
"context": { "diagnostics": diagnostics }
|
||
}),
|
||
20,
|
||
)
|
||
.await;
|
||
assert!(response.result().unwrap().as_array().unwrap().is_empty());
|
||
|
||
harness
|
||
.notify(
|
||
"textDocument/didOpen",
|
||
open_params_with_language(
|
||
"untitled:source",
|
||
"rust",
|
||
2,
|
||
"// First, we declare the value.\n// TODO: implement",
|
||
),
|
||
)
|
||
.await;
|
||
let source_published = harness
|
||
.notification("textDocument/publishDiagnostics")
|
||
.await;
|
||
let source_codes = source_published["diagnostics"]
|
||
.as_array()
|
||
.unwrap()
|
||
.iter()
|
||
.map(|diagnostic| diagnostic["code"].as_str().unwrap())
|
||
.collect::<std::collections::BTreeSet<_>>();
|
||
assert_eq!(
|
||
source_codes,
|
||
["source-placeholder-todo", "source-tutorial-narration"]
|
||
.into_iter()
|
||
.collect()
|
||
);
|
||
|
||
harness
|
||
.notify(
|
||
"textDocument/didOpen",
|
||
open_params(
|
||
"untitled:prose",
|
||
2,
|
||
"// First, we declare the value.\n// TODO: implement",
|
||
),
|
||
)
|
||
.await;
|
||
let prose_published = harness
|
||
.notification("textDocument/publishDiagnostics")
|
||
.await;
|
||
assert!(
|
||
prose_published["diagnostics"]
|
||
.as_array()
|
||
.unwrap()
|
||
.is_empty()
|
||
);
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn default_lsp_publishes_unicode_but_suppresses_other_findings() {
|
||
let mut harness = Harness::new(None).await;
|
||
let uri = "file:///tmp/quiet-default.txt";
|
||
harness
|
||
.notify(
|
||
"textDocument/didOpen",
|
||
open_params(uri, 1, "As an AI language model—I hope this helps."),
|
||
)
|
||
.await;
|
||
|
||
let published = harness
|
||
.notification("textDocument/publishDiagnostics")
|
||
.await;
|
||
let diagnostics = published["diagnostics"].as_array().unwrap();
|
||
assert_eq!(diagnostics.len(), 1);
|
||
assert_eq!(diagnostics[0]["code"], "unicode-ascii-replacement");
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn minimum_weight_keeps_unicode_and_only_heavier_non_unicode_findings() {
|
||
let mut harness =
|
||
Harness::new_with_filter(None, None, DiagnosticFilter::MinimumWeight(10.0)).await;
|
||
harness
|
||
.notify(
|
||
"textDocument/didOpen",
|
||
open_params(
|
||
"file:///tmp/weighted.txt",
|
||
1,
|
||
"As an AI language model—I hope this helps.",
|
||
),
|
||
)
|
||
.await;
|
||
|
||
let published = harness
|
||
.notification("textDocument/publishDiagnostics")
|
||
.await;
|
||
let codes = published["diagnostics"]
|
||
.as_array()
|
||
.unwrap()
|
||
.iter()
|
||
.map(|diagnostic| diagnostic["code"].as_str().unwrap())
|
||
.collect::<std::collections::BTreeSet<_>>();
|
||
assert_eq!(
|
||
codes,
|
||
["assistant-self-identification", "unicode-ascii-replacement"]
|
||
.into_iter()
|
||
.collect()
|
||
);
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn unicode_review_findings_separate_safe_fixes_from_controls() {
|
||
let mut harness = Harness::new(None).await;
|
||
let uri = "file:///tmp/unicode-review.txt";
|
||
let source = "pаss\u{200b}word\u{202e}";
|
||
harness
|
||
.notify("textDocument/didOpen", open_params(uri, 1, source))
|
||
.await;
|
||
let published = harness
|
||
.notification("textDocument/publishDiagnostics")
|
||
.await;
|
||
let diagnostics = published["diagnostics"].as_array().unwrap();
|
||
assert_eq!(diagnostics.len(), 3);
|
||
assert_eq!(diagnostics[0]["code"], "unicode-ascii-replacement");
|
||
assert_eq!(diagnostics[0]["data"]["fixable"], true);
|
||
assert_eq!(diagnostics[1]["code"], "unicode-invisible-control");
|
||
assert_eq!(diagnostics[2]["code"], "unicode-invisible-control");
|
||
|
||
let formatting = harness
|
||
.request(
|
||
"textDocument/formatting",
|
||
json!({
|
||
"textDocument": { "uri": uri },
|
||
"options": { "tabSize": 4, "insertSpaces": true }
|
||
}),
|
||
40,
|
||
)
|
||
.await;
|
||
let formatting = formatting.result().unwrap().as_array().unwrap();
|
||
assert_eq!(formatting.len(), 1);
|
||
assert_eq!(formatting[0]["newText"], "a");
|
||
|
||
let actions = harness
|
||
.request(
|
||
"textDocument/codeAction",
|
||
json!({
|
||
"textDocument": { "uri": uri },
|
||
"range": {
|
||
"start": { "line": 0, "character": 0 },
|
||
"end": { "line": 0, "character": 12 }
|
||
},
|
||
"context": { "diagnostics": diagnostics }
|
||
}),
|
||
41,
|
||
)
|
||
.await;
|
||
let actions = actions.result().unwrap().as_array().unwrap();
|
||
assert_eq!(actions.len(), 1);
|
||
assert_eq!(
|
||
actions[0]["edit"]["documentChanges"][0]["edits"][0]["newText"],
|
||
"a"
|
||
);
|
||
}
|
||
|
||
fn utf16_position_to_byte(source: &str, position: &Value) -> usize {
|
||
let target_line = position["line"].as_u64().unwrap() as usize;
|
||
let target_units = position["character"].as_u64().unwrap() as usize;
|
||
let line_start = if target_line == 0 {
|
||
0
|
||
} else {
|
||
source
|
||
.match_indices('\n')
|
||
.nth(target_line - 1)
|
||
.map(|(offset, _)| offset + 1)
|
||
.unwrap()
|
||
};
|
||
let line = &source[line_start..];
|
||
let mut units = 0;
|
||
for (relative, character) in line.char_indices() {
|
||
if units == target_units {
|
||
return line_start + relative;
|
||
}
|
||
units += character.len_utf16();
|
||
assert!(units <= target_units, "position splits a UTF-16 scalar");
|
||
}
|
||
line_start + line.len()
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn lifecycle_versions_unicode_quick_fixes_and_formatter_equivalence() {
|
||
let mut harness = Harness::new(None).await;
|
||
let uri = "file:///tmp/unicode.txt";
|
||
let source = "😀—one\r\ntwo−";
|
||
harness
|
||
.notify("textDocument/didOpen", open_params(uri, 1, source))
|
||
.await;
|
||
let published = harness
|
||
.notification("textDocument/publishDiagnostics")
|
||
.await;
|
||
assert_eq!(published["version"], 1);
|
||
assert_eq!(published["diagnostics"].as_array().unwrap().len(), 2);
|
||
assert_eq!(
|
||
published["diagnostics"][0]["range"]["start"],
|
||
json!({"line": 0, "character": 2})
|
||
);
|
||
|
||
let formatting = harness
|
||
.request(
|
||
"textDocument/formatting",
|
||
json!({
|
||
"textDocument": { "uri": uri },
|
||
"options": { "tabSize": 4, "insertSpaces": true }
|
||
}),
|
||
2,
|
||
)
|
||
.await;
|
||
let formatting_edits = formatting.result().unwrap().as_array().unwrap();
|
||
assert_eq!(formatting_edits.len(), 2);
|
||
let mut formatted = source.to_owned();
|
||
for edit in formatting_edits.iter().rev() {
|
||
let start = utf16_position_to_byte(source, &edit["range"]["start"]);
|
||
let end = utf16_position_to_byte(source, &edit["range"]["end"]);
|
||
formatted.replace_range(start..end, edit["newText"].as_str().unwrap());
|
||
}
|
||
let formatter_output =
|
||
apply_replacements(source, &plan_replacements(&Config::embedded(), source)).unwrap();
|
||
assert_eq!(formatted, formatter_output);
|
||
|
||
let response = harness
|
||
.request(
|
||
"textDocument/codeAction",
|
||
json!({
|
||
"textDocument": { "uri": uri },
|
||
"range": {
|
||
"start": { "line": 0, "character": 0 },
|
||
"end": { "line": 1, "character": 4 }
|
||
},
|
||
"context": { "diagnostics": published["diagnostics"] }
|
||
}),
|
||
3,
|
||
)
|
||
.await;
|
||
let actions = response.result().unwrap().as_array().unwrap();
|
||
assert_eq!(actions.len(), 2);
|
||
let mut edits = actions
|
||
.iter()
|
||
.map(|action| {
|
||
let edit = &action["edit"]["documentChanges"][0]["edits"][0];
|
||
let start = utf16_position_to_byte(source, &edit["range"]["start"]);
|
||
let end = utf16_position_to_byte(source, &edit["range"]["end"]);
|
||
john_slop::TextEdit {
|
||
byte_span: ByteRange { start, end },
|
||
replacement: edit["newText"].as_str().unwrap().to_owned(),
|
||
}
|
||
})
|
||
.collect::<Vec<_>>();
|
||
edits.sort_by_key(|edit| edit.byte_span.start);
|
||
let lsp_output = apply_replacements(source, &edits).unwrap();
|
||
assert_eq!(lsp_output, formatter_output);
|
||
|
||
harness
|
||
.notify(
|
||
"textDocument/didChange",
|
||
json!({
|
||
"textDocument": { "uri": uri, "version": 2 },
|
||
"contentChanges": [{ "text": "plain ASCII" }]
|
||
}),
|
||
)
|
||
.await;
|
||
let cleared = harness
|
||
.notification("textDocument/publishDiagnostics")
|
||
.await;
|
||
assert_eq!(cleared["version"], 2);
|
||
assert!(cleared["diagnostics"].as_array().unwrap().is_empty());
|
||
|
||
let stale = harness
|
||
.request(
|
||
"textDocument/codeAction",
|
||
json!({
|
||
"textDocument": { "uri": uri },
|
||
"range": { "start": { "line": 0, "character": 0 }, "end": { "line": 0, "character": 1 } },
|
||
"context": { "diagnostics": published["diagnostics"] }
|
||
}),
|
||
4,
|
||
)
|
||
.await;
|
||
assert_eq!(stale.error().unwrap().code, ErrorCode::InvalidParams);
|
||
|
||
harness
|
||
.notify(
|
||
"textDocument/didClose",
|
||
json!({ "textDocument": { "uri": uri } }),
|
||
)
|
||
.await;
|
||
let closed = harness
|
||
.notification("textDocument/publishDiagnostics")
|
||
.await;
|
||
assert!(closed["diagnostics"].as_array().unwrap().is_empty());
|
||
assert!(closed["version"].is_null());
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn formatting_width_folds_the_complete_fullwidth_ascii_block() {
|
||
let mut harness = Harness::new(None).await;
|
||
let uri = "file:///tmp/fullwidth.txt";
|
||
let source = std::iter::once('\u{3000}')
|
||
.chain((b'!'..=b'~').filter_map(|ascii| char::from_u32(u32::from(ascii) + 0xfee0)))
|
||
.collect::<String>();
|
||
let expected = (b' '..=b'~').map(char::from).collect::<String>();
|
||
harness
|
||
.notify("textDocument/didOpen", open_params(uri, 1, &source))
|
||
.await;
|
||
let _published = harness
|
||
.notification("textDocument/publishDiagnostics")
|
||
.await;
|
||
|
||
let formatting = harness
|
||
.request(
|
||
"textDocument/formatting",
|
||
json!({
|
||
"textDocument": { "uri": uri },
|
||
"options": { "tabSize": 4, "insertSpaces": true }
|
||
}),
|
||
2,
|
||
)
|
||
.await;
|
||
let mut formatted = source.clone();
|
||
for edit in formatting
|
||
.result()
|
||
.unwrap()
|
||
.as_array()
|
||
.unwrap()
|
||
.iter()
|
||
.rev()
|
||
{
|
||
let start = utf16_position_to_byte(&source, &edit["range"]["start"]);
|
||
let end = utf16_position_to_byte(&source, &edit["range"]["end"]);
|
||
formatted.replace_range(start..end, edit["newText"].as_str().unwrap());
|
||
}
|
||
|
||
assert_eq!(formatted, expected);
|
||
assert_eq!(
|
||
apply_replacements(&source, &plan_replacements(&Config::embedded(), &source)).unwrap(),
|
||
expected
|
||
);
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn configuration_reload_and_concurrent_documents_remain_independent() {
|
||
let disabled = r#"
|
||
[[replacements]]
|
||
id = "em-dash"
|
||
enabled = false
|
||
"#;
|
||
let mut harness = Harness::new(Some(disabled)).await;
|
||
let first = "file:///tmp/first.txt";
|
||
let second = "untitled:second";
|
||
harness
|
||
.notify("textDocument/didOpen", open_params(first, 1, "first—doc"))
|
||
.await;
|
||
let one = harness
|
||
.notification("textDocument/publishDiagnostics")
|
||
.await;
|
||
harness
|
||
.notify("textDocument/didOpen", open_params(second, 3, "second−doc"))
|
||
.await;
|
||
let two = harness
|
||
.notification("textDocument/publishDiagnostics")
|
||
.await;
|
||
let by_uri = [one, two]
|
||
.into_iter()
|
||
.map(|params| (params["uri"].as_str().unwrap().to_owned(), params))
|
||
.collect::<std::collections::HashMap<_, _>>();
|
||
assert!(by_uri[first]["diagnostics"].as_array().unwrap().is_empty());
|
||
assert_eq!(by_uri[second]["diagnostics"].as_array().unwrap().len(), 1);
|
||
let initially_disabled_format = harness
|
||
.request(
|
||
"textDocument/formatting",
|
||
json!({
|
||
"textDocument": { "uri": first },
|
||
"options": { "tabSize": 4, "insertSpaces": true }
|
||
}),
|
||
30,
|
||
)
|
||
.await;
|
||
assert!(
|
||
initially_disabled_format
|
||
.result()
|
||
.unwrap()
|
||
.as_array()
|
||
.unwrap()
|
||
.is_empty()
|
||
);
|
||
|
||
fs::write(
|
||
harness.explicit_path(),
|
||
r#"
|
||
[[replacements]]
|
||
id = "em-dash"
|
||
enabled = true
|
||
"#,
|
||
)
|
||
.unwrap();
|
||
let reload = Request::build("workspace/didChangeConfiguration")
|
||
.params(json!({ "settings": {} }))
|
||
.finish();
|
||
let service = &mut harness.service;
|
||
let socket = &mut harness.socket;
|
||
let reload_future = async { service.ready().await.unwrap().call(reload).await.unwrap() };
|
||
let notifications = async {
|
||
let first = socket.next().await.unwrap();
|
||
let second = socket.next().await.unwrap();
|
||
[first, second].map(|request| request.params().cloned().unwrap())
|
||
};
|
||
let (reload_response, [refreshed_one, refreshed_two]) =
|
||
tokio::join!(reload_future, notifications);
|
||
assert!(reload_response.is_none());
|
||
let refreshed = [refreshed_one, refreshed_two]
|
||
.into_iter()
|
||
.map(|params| (params["uri"].as_str().unwrap().to_owned(), params))
|
||
.collect::<std::collections::HashMap<_, _>>();
|
||
assert_eq!(refreshed[first]["diagnostics"].as_array().unwrap().len(), 1);
|
||
assert_eq!(
|
||
refreshed[second]["diagnostics"].as_array().unwrap().len(),
|
||
1
|
||
);
|
||
let reloaded_format = harness
|
||
.request(
|
||
"textDocument/formatting",
|
||
json!({
|
||
"textDocument": { "uri": first },
|
||
"options": { "tabSize": 4, "insertSpaces": true }
|
||
}),
|
||
31,
|
||
)
|
||
.await;
|
||
assert_eq!(
|
||
reloaded_format.result().unwrap()[0]["newText"],
|
||
"-",
|
||
"formatting must use the reloaded replacement configuration"
|
||
);
|
||
|
||
fs::write(harness.explicit_path(), "schema_version = 999\n").unwrap();
|
||
let failed_reload = Request::build("workspace/didChangeConfiguration")
|
||
.params(json!({ "settings": {} }))
|
||
.finish();
|
||
let service = &mut harness.service;
|
||
let socket = &mut harness.socket;
|
||
let failed_reload_future = async {
|
||
service
|
||
.ready()
|
||
.await
|
||
.unwrap()
|
||
.call(failed_reload)
|
||
.await
|
||
.unwrap()
|
||
};
|
||
let error_message_future = async { socket.next().await.unwrap() };
|
||
let (failed_reload_response, error_message) =
|
||
tokio::join!(failed_reload_future, error_message_future);
|
||
assert!(failed_reload_response.is_none());
|
||
assert_eq!(error_message.method(), "window/showMessage");
|
||
harness
|
||
.notify(
|
||
"textDocument/didChange",
|
||
json!({
|
||
"textDocument": { "uri": first, "version": 2 },
|
||
"contentChanges": [{ "text": "still—enabled" }]
|
||
}),
|
||
)
|
||
.await;
|
||
let after_failed_reload = harness
|
||
.notification("textDocument/publishDiagnostics")
|
||
.await;
|
||
assert_eq!(after_failed_reload["version"], 2);
|
||
assert_eq!(
|
||
after_failed_reload["diagnostics"].as_array().unwrap().len(),
|
||
1
|
||
);
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn disabled_category_suppresses_lsp_diagnostics() {
|
||
let mut harness =
|
||
Harness::new_all(Some("[diagnostics.categories]\nprompt-leakage = false\n")).await;
|
||
harness
|
||
.notify(
|
||
"textDocument/didOpen",
|
||
open_params(
|
||
"file:///tmp/category.md",
|
||
1,
|
||
"As an AI language model, I can help.",
|
||
),
|
||
)
|
||
.await;
|
||
|
||
let published = harness
|
||
.notification("textDocument/publishDiagnostics")
|
||
.await;
|
||
|
||
assert!(published["diagnostics"].as_array().unwrap().is_empty());
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn runtime_diagnostic_catalog_loads_and_reloads_without_recompilation() {
|
||
let enabled = custom_catalog(true);
|
||
let mut harness = Harness::new_with_diagnostics(None, Some(&enabled)).await;
|
||
let uri = "file:///tmp/catalog.md";
|
||
harness
|
||
.notify(
|
||
"textDocument/didOpen",
|
||
open_params(uri, 1, "REVIEW BEFORE PUBLISHING"),
|
||
)
|
||
.await;
|
||
let published = harness
|
||
.notification("textDocument/publishDiagnostics")
|
||
.await;
|
||
let diagnostics = published["diagnostics"].as_array().unwrap();
|
||
assert_eq!(diagnostics.len(), 1);
|
||
assert_eq!(diagnostics[0]["code"], "custom-review-marker");
|
||
|
||
fs::write(harness.diagnostics_path(), custom_catalog(false)).unwrap();
|
||
let reload = Request::build("workspace/didChangeConfiguration")
|
||
.params(json!({ "settings": {} }))
|
||
.finish();
|
||
let service = &mut harness.service;
|
||
let socket = &mut harness.socket;
|
||
let reload_future = async { service.ready().await.unwrap().call(reload).await.unwrap() };
|
||
let notification_future = async { socket.next().await.unwrap() };
|
||
let (reload_response, notification) = tokio::join!(reload_future, notification_future);
|
||
|
||
assert!(reload_response.is_none());
|
||
let refreshed = notification.params().cloned().unwrap();
|
||
assert_eq!(refreshed["uri"], uri);
|
||
assert!(refreshed["diagnostics"].as_array().unwrap().is_empty());
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn malformed_messages_and_invalid_uris_are_rejected_without_panics() {
|
||
assert!("not JSON".parse::<Request>().is_err());
|
||
let mut harness = Harness::new(None).await;
|
||
let malformed = harness
|
||
.request("textDocument/codeAction", json!({ "textDocument": {} }), 7)
|
||
.await;
|
||
assert_eq!(malformed.error().unwrap().code, ErrorCode::InvalidParams);
|
||
|
||
let invalid_uri = harness
|
||
.request(
|
||
"textDocument/codeAction",
|
||
json!({
|
||
"textDocument": { "uri": "http://[invalid" },
|
||
"range": { "start": { "line": 0, "character": 0 }, "end": { "line": 0, "character": 0 } },
|
||
"context": { "diagnostics": [] }
|
||
}),
|
||
8,
|
||
)
|
||
.await;
|
||
assert_eq!(invalid_uri.error().unwrap().code, ErrorCode::InvalidParams);
|
||
}
|