/ tests / e2e / definition_test.rs
definition_test.rs
  1  use crate::harness::{LspTestClient, TempWorkspace};
  2  use std::thread;
  3  use std::time::Duration;
  4  
  5  #[test]
  6  fn test_goto_definition_to_env_file() {
  7      let workspace = TempWorkspace::new();
  8      let client = LspTestClient::spawn(workspace.root.clone()).expect("Failed to spawn LSP");
  9      client.initialize().expect("Initialize failed");
 10  
 11      let uri = workspace.file_uri("test.js");
 12      let content = "process.env.DB_URL";
 13      workspace.create_file("test.js", content);
 14  
 15      client
 16          .open_document(&uri, "javascript", content)
 17          .expect("Failed to open document");
 18      thread::sleep(Duration::from_millis(300));
 19  
 20      let definition = client
 21          .definition(&uri, 0, 15)
 22          .expect("Definition request failed");
 23  
 24      assert!(!definition.is_null(), "Expected definition result");
 25  
 26      let def_uri = definition
 27          .get("uri")
 28          .expect("Should have uri")
 29          .as_str()
 30          .unwrap();
 31      assert!(
 32          def_uri.ends_with(".env"),
 33          "Definition should point to .env file"
 34      );
 35  
 36      let range = definition.get("range").expect("Should have range");
 37      let start = range.get("start").expect("Should have start");
 38      assert_eq!(
 39          start.get("line").unwrap().as_i64(),
 40          Some(0),
 41          "DB_URL is on first line"
 42      );
 43  
 44      client.shutdown().expect("Shutdown failed");
 45  }
 46  
 47  #[test]
 48  fn test_definition_from_binding() {
 49      let workspace = TempWorkspace::new();
 50      let client = LspTestClient::spawn(workspace.root.clone()).expect("Failed to spawn LSP");
 51      client.initialize().expect("Initialize failed");
 52  
 53      let uri = workspace.file_uri("test.js");
 54      let content = "const { PORT } = process.env; console.log(PORT);";
 55      workspace.create_file("test.js", content);
 56  
 57      client
 58          .open_document(&uri, "javascript", content)
 59          .expect("Failed to open document");
 60      thread::sleep(Duration::from_millis(300));
 61  
 62      let definition = client
 63          .definition(&uri, 0, 44)
 64          .expect("Definition request failed");
 65  
 66      assert!(
 67          !definition.is_null(),
 68          "Expected definition result from binding usage"
 69      );
 70  
 71      client.shutdown().expect("Shutdown failed");
 72  }
 73  
 74  #[test]
 75  fn test_definition_disabled_via_config() {
 76      let workspace = TempWorkspace::new();
 77      workspace.create_config(
 78          r#"
 79  [features]
 80  definition = false
 81  "#,
 82      );
 83  
 84      let client = LspTestClient::spawn(workspace.root.clone()).expect("Failed to spawn LSP");
 85      client.initialize().expect("Initialize failed");
 86  
 87      let uri = workspace.file_uri("test.js");
 88      let content = "process.env.DB_URL";
 89      workspace.create_file("test.js", content);
 90  
 91      client
 92          .open_document(&uri, "javascript", content)
 93          .expect("Failed to open document");
 94      thread::sleep(Duration::from_millis(300));
 95  
 96      let definition = client
 97          .definition(&uri, 0, 15)
 98          .expect("Definition request failed");
 99  
100      assert!(
101          definition.is_null(),
102          "Definition should be null when disabled"
103      );
104  
105      client.shutdown().expect("Shutdown failed");
106  }
107  
108  #[test]
109  fn test_definition_undefined_var() {
110      let workspace = TempWorkspace::new();
111      let client = LspTestClient::spawn(workspace.root.clone()).expect("Failed to spawn LSP");
112      client.initialize().expect("Initialize failed");
113  
114      let uri = workspace.file_uri("test.js");
115      let content = "process.env.UNDEFINED_VAR";
116      workspace.create_file("test.js", content);
117  
118      client
119          .open_document(&uri, "javascript", content)
120          .expect("Failed to open document");
121      thread::sleep(Duration::from_millis(300));
122  
123      let definition = client
124          .definition(&uri, 0, 15)
125          .expect("Definition request failed");
126  
127      assert!(
128          definition.is_null(),
129          "Undefined var should have no definition"
130      );
131  
132      client.shutdown().expect("Shutdown failed");
133  }
134  
135  #[test]
136  fn test_definition_python() {
137      let workspace = TempWorkspace::new();
138      let client = LspTestClient::spawn(workspace.root.clone()).expect("Failed to spawn LSP");
139      client.initialize().expect("Initialize failed");
140  
141      let uri = workspace.file_uri("test.py");
142      let content = "import os\ndb = os.environ['DB_URL']";
143      workspace.create_file("test.py", content);
144  
145      client
146          .open_document(&uri, "python", content)
147          .expect("Failed to open document");
148      thread::sleep(Duration::from_millis(300));
149  
150      let definition = client
151          .definition(&uri, 1, 18)
152          .expect("Definition request failed");
153  
154      assert!(
155          !definition.is_null(),
156          "Expected definition result for Python"
157      );
158  
159      client.shutdown().expect("Shutdown failed");
160  }
161  
162  #[test]
163  fn test_definition_outside_env_returns_null() {
164      let workspace = TempWorkspace::new();
165      let client = LspTestClient::spawn(workspace.root.clone()).expect("Failed to spawn LSP");
166      client.initialize().expect("Initialize failed");
167  
168      let uri = workspace.file_uri("test.js");
169      let content = "const x = 1;";
170      workspace.create_file("test.js", content);
171  
172      client
173          .open_document(&uri, "javascript", content)
174          .expect("Failed to open document");
175      thread::sleep(Duration::from_millis(300));
176  
177      let definition = client
178          .definition(&uri, 0, 6)
179          .expect("Definition request failed");
180  
181      assert!(
182          definition.is_null(),
183          "Non-env code should have no definition"
184      );
185  
186      client.shutdown().expect("Shutdown failed");
187  }