//! Pull diagnostics (LSP 3.17): one client is never served both models. use super::*; // ------------------------------------------------------------------ // pull diagnostics // ------------------------------------------------------------------ /// A buffer with one parse error and one lint finding in it. const BROKEN: &str = "module m;\n\twire ;\nendmodule\n"; #[test] fn a_pulling_client_is_never_pushed_diagnostics() { // Never both models for one client: the second set of squiggles would // arrive on a schedule the client did not choose. let tree = Tree::new("pullnopush"); let mut session = Session::with(vec![tree.path.clone()], pulling()); let path = tree.path.join("m.sv"); let uri = session.open(&path, BROKEN); assert_eq!(session.publishes(), 0, "`didOpen` pushed nothing"); session.notify( DidChangeTextDocument::METHOD, serde_json::json!({ "textDocument": { "uri": uri, "version": 2 }, "contentChanges": [{ "text": BROKEN }], }), ); assert_eq!(session.publishes(), 0, "nor did a change"); session .notify(DidSaveTextDocument::METHOD, serde_json::json!({ "textDocument": { "uri": uri } })); assert_eq!(session.publishes(), 0, "nor a save's republish"); session.notify( DidCloseTextDocument::METHOD, serde_json::json!({ "textDocument": { "uri": uri } }), ); assert_eq!(session.publishes(), 0, "and the close clears nothing it never drew"); } #[test] fn a_pushing_client_keeps_being_pushed() { let tree = Tree::new("pushstays"); let mut session = Session::new(vec![tree.path.clone()], false); session.open(&tree.path.join("m.sv"), BROKEN); assert_eq!(session.publishes(), 1, "unchanged from before this feature"); } #[test] fn a_pull_is_full_once_and_unchanged_until_something_moves() { let tree = Tree::new("pullunchanged"); let mut session = Session::with(vec![tree.path.clone()], pulling()); let uri = session.open(&tree.path.join("m.sv"), BROKEN); let first = document_pull(&mut session, &uri, None); assert_eq!(first["kind"], "full"); assert!(!first["items"].as_array().expect("items").is_empty(), "{first:#?}"); let result_id = first["resultId"].as_str().expect("a result id").to_string(); let again = document_pull(&mut session, &uri, Some(&result_id)); assert_eq!(again["kind"], "unchanged", "nothing moved: {again:#?}"); assert_eq!(again["resultId"], serde_json::json!(result_id), "and it stays namable"); // A buffer change moves it. session.notify( DidChangeTextDocument::METHOD, serde_json::json!({ "textDocument": { "uri": uri, "version": 2 }, "contentChanges": [{ "text": "module m;\n wire a;\nendmodule\n" }], }), ); let after = document_pull(&mut session, &uri, Some(&result_id)); assert_eq!(after["kind"], "full", "the buffer changed: {after:#?}"); assert!(after["items"].as_array().expect("items").is_empty(), "and it is clean now"); assert_ne!(after["resultId"], serde_json::json!(result_id), "a new stamp"); } #[test] fn an_index_rebuild_moves_the_result_id_too() { // `inter_file_dependencies: true` is a promise: a save rebuilds the // index, and what the index says can change what a *different* buffer // reports. A stale "unchanged" there would be a wrong squiggle. let tree = Tree::new("pullreindex"); let mut session = Session::with(vec![tree.path.clone()], pulling()); let uri = session.open(&tree.path.join("m.sv"), BROKEN); let stamp = document_pull(&mut session, &uri, None)["resultId"] .as_str() .expect("a result id") .to_string(); session .notify(DidSaveTextDocument::METHOD, serde_json::json!({ "textDocument": { "uri": uri } })); let after = document_pull(&mut session, &uri, Some(&stamp)); assert_eq!(after["kind"], "full", "the index moved: {after:#?}"); } #[test] fn a_pull_for_a_buffer_that_is_not_open_is_empty_rather_than_an_error() { let tree = Tree::new("pullclosed"); let mut session = Session::with(vec![tree.path.clone()], pulling()); let uri = path_to_uri(&tree.path.join("never-opened.sv")).expect("uri"); let report = document_pull(&mut session, &uri, None); assert_eq!(report["kind"], "full"); assert!(report["items"].as_array().expect("items").is_empty()); } #[test] fn the_workspace_pull_reports_every_open_buffer_and_honours_its_stamps() { let tree = Tree::new("workspacepull"); let mut session = Session::with(vec![tree.path.clone()], pulling()); let first = session.open(&tree.path.join("a.sv"), BROKEN); let second = session.open(&tree.path.join("b.sv"), "module b;\nendmodule\n"); let report = session.result( WorkspaceDiagnosticRequest::METHOD, serde_json::json!({ "identifier": diagnostics::SOURCE, "previousResultIds": [] }), ); let items = report["items"].as_array().expect("items").clone(); assert_eq!(items.len(), 2, "{report:#?}"); assert!(items.iter().all(|item| item["kind"] == "full"), "{report:#?}"); let uris: Vec<&str> = items.iter().map(|item| item["uri"].as_str().unwrap_or("")).collect(); assert!(uris.contains(&first.as_str()) && uris.contains(&second.as_str()), "{uris:?}"); let mut sorted = uris.clone(); sorted.sort_unstable(); assert_eq!(uris, sorted, "URI order, so two pulls of one state read alike"); let stamp = items[0]["resultId"].as_str().expect("a result id").to_string(); // Re-asked with what the client already holds, both come back unchanged. let report = session.result( WorkspaceDiagnosticRequest::METHOD, serde_json::json!({ "identifier": diagnostics::SOURCE, "previousResultIds": [ { "uri": first, "value": stamp }, { "uri": second, "value": stamp }, ], }), ); let items = report["items"].as_array().expect("items"); assert_eq!(items.len(), 2); assert!(items.iter().all(|item| item["kind"] == "unchanged"), "{items:#?}"); // A stamp from another generation is not the one we hold. let report = session.result( WorkspaceDiagnosticRequest::METHOD, serde_json::json!({ "identifier": diagnostics::SOURCE, "previousResultIds": [{ "uri": first, "value": "0" }], }), ); let items = report["items"].as_array().expect("items"); assert!(items.iter().all(|item| item["kind"] == "full"), "{items:#?}"); } #[test] fn an_empty_workspace_answers_a_well_formed_empty_report() { // QA attack: no open buffers at all — does `workspace/diagnostic` // still answer a well-formed (if empty) report, rather than an error // or a missing field? let tree = Tree::new("workspaceemptypull"); let mut session = Session::with(vec![tree.path.clone()], pulling()); let report = session.result( WorkspaceDiagnosticRequest::METHOD, serde_json::json!({ "identifier": diagnostics::SOURCE, "previousResultIds": [] }), ); assert_eq!(report["items"], serde_json::json!([]), "{report:#?}"); } #[test] fn a_previous_result_id_for_a_uri_that_is_since_closed_is_harmless() { // QA attack: the client's `previousResultIds` can name a document it // has since closed (it does not have to prune its own cache eagerly). // That URI is no longer among the open buffers `workspace_report` // walks, so it must simply be absent from the answer — not an error, // not a phantom entry. let tree = Tree::new("workspaceclosedstamp"); let mut session = Session::with(vec![tree.path.clone()], pulling()); let first = session.open(&tree.path.join("a.sv"), BROKEN); let closed = session.open(&tree.path.join("b.sv"), "module b;\nendmodule\n"); let stamp = session.result( WorkspaceDiagnosticRequest::METHOD, serde_json::json!({ "identifier": diagnostics::SOURCE, "previousResultIds": [] }), )["items"][0]["resultId"] .as_str() .expect("a result id") .to_string(); session.notify( DidCloseTextDocument::METHOD, serde_json::json!({ "textDocument": { "uri": closed } }), ); let report = session.result( WorkspaceDiagnosticRequest::METHOD, serde_json::json!({ "identifier": diagnostics::SOURCE, "previousResultIds": [ { "uri": first, "value": stamp }, { "uri": closed, "value": stamp }, ], }), ); let items = report["items"].as_array().expect("items"); assert_eq!(items.len(), 1, "the closed uri is gone, not phantom-reported: {items:#?}"); assert_eq!(items[0]["uri"], first.as_str(), "{items:#?}"); } #[test] fn a_previous_result_id_for_a_uri_never_seen_is_ignored() { // QA attack: a `previousResultIds` entry naming a document this // server never opened (a client warm-starting from a stale cache, or // simply a typo'd uri) must not confuse the match-up for the uris // that *are* open. let tree = Tree::new("workspaceneverseenstamp"); let mut session = Session::with(vec![tree.path.clone()], pulling()); let opened = session.open(&tree.path.join("a.sv"), BROKEN); let never_seen = path_to_uri(&tree.path.join("never-opened.sv")).expect("uri"); let report = session.result( WorkspaceDiagnosticRequest::METHOD, serde_json::json!({ "identifier": diagnostics::SOURCE, "previousResultIds": [{ "uri": never_seen, "value": "0" }], }), ); let items = report["items"].as_array().expect("items"); assert_eq!(items.len(), 1, "{items:#?}"); assert_eq!(items[0]["uri"], opened.as_str(), "{items:#?}"); assert_eq!(items[0]["kind"], "full"); } #[test] fn both_models_say_exactly_the_same_thing_about_a_buffer() { // One computation, two deliveries. A second conversion path would be // a second set of off-by-one bugs. let tree = Tree::new("samediagnostics"); let mut pushed = Session::new(vec![tree.path.clone()], false); pushed.open(&tree.path.join("m.sv"), BROKEN); let published = pushed.published(); assert_eq!(published.len(), 1); let mut pulled = Session::with(vec![tree.path.clone()], pulling()); let uri = pulled.open(&tree.path.join("m.sv"), BROKEN); let report = document_pull(&mut pulled, &uri, None); assert_eq!( report["items"], serde_json::to_value(&published[0].diagnostics).expect("diagnostics"), ); }