|
| 1 | +use std::env; |
| 2 | +use std::path::PathBuf; |
| 3 | +use std::cell::RefCell; |
| 4 | +use std::rc::Rc; |
| 5 | + |
| 6 | +use odoo_ls_server::core::odoo::SyncOdoo; |
| 7 | +use odoo_ls_server::core::symbols::symbol::Symbol; |
| 8 | +use odoo_ls_server::core::file_mgr::FileInfo; |
| 9 | +use odoo_ls_server::utils::PathSanitizer; |
| 10 | +use odoo_ls_server::threads::SessionInfo; |
| 11 | + |
| 12 | +mod setup; |
| 13 | +mod test_utils; |
| 14 | + |
| 15 | +/// Test that odoo.http.request and request.env hooks work correctly. |
| 16 | +/// Tests hover and definition. |
| 17 | +#[test] |
| 18 | +fn test_request_hooks() { |
| 19 | + let (mut odoo, config) = setup::setup::setup_server(true); |
| 20 | + let mut session = setup::setup::create_init_session(&mut odoo, config); |
| 21 | + |
| 22 | + // Check Odoo version (request hooks require 15.3+) |
| 23 | + if session.sync_odoo.version_major < 15 || (session.sync_odoo.version_major == 15 && session.sync_odoo.version_minor < 3) { |
| 24 | + panic!("Skipping test_request_hooks: Odoo version < 15.3"); |
| 25 | + } |
| 26 | + |
| 27 | + let test_file = PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 28 | + .join("tests/data/addons/module_1/controllers/test_http.py") |
| 29 | + .sanitize(); |
| 30 | + |
| 31 | + let Some(file_symbol) = SyncOdoo::get_symbol_of_opened_file(&mut session, &PathBuf::from(&test_file)) else { |
| 32 | + panic!("Failed to get file symbol for {}", test_file); |
| 33 | + }; |
| 34 | + |
| 35 | + let file_mgr = session.sync_odoo.get_file_mgr(); |
| 36 | + let file_info = file_mgr.borrow().get_file_info(&test_file).unwrap(); |
| 37 | + |
| 38 | + test_request_type_hover(&mut session, &file_symbol, &file_info); |
| 39 | + test_request_env_definition(&mut session, &file_symbol, &file_info); |
| 40 | +} |
| 41 | + |
| 42 | +/// Test that hovering over 'request' shows Request class |
| 43 | +/// and that hovering over 'request.env' shows Environment type |
| 44 | +fn test_request_type_hover( |
| 45 | + session: &mut SessionInfo, |
| 46 | + file_symbol: &Rc<RefCell<Symbol>>, |
| 47 | + file_info: &Rc<RefCell<FileInfo>> |
| 48 | +) { |
| 49 | + // Test 1: Hover over 'request' variable (line 11: req = request) |
| 50 | + // Should show Request class |
| 51 | + let hover_text = test_utils::get_hover_markdown(session, file_symbol, file_info, 11, 14) |
| 52 | + .expect("Should get hover text for request"); |
| 53 | + |
| 54 | + assert!( |
| 55 | + hover_text.contains("Request"), |
| 56 | + "Hover over 'request' should show Request class. Got: {}", |
| 57 | + hover_text |
| 58 | + ); |
| 59 | + |
| 60 | + // Test 2: Hover over 'request.env' (line 14: env = request.env) |
| 61 | + // Should show Environment | None |
| 62 | + let hover_text = test_utils::get_hover_markdown(session, file_symbol, file_info, 14, 21) |
| 63 | + .expect("Should get hover text for request.env"); |
| 64 | + |
| 65 | + assert!( |
| 66 | + hover_text.contains("Environment"), |
| 67 | + "Hover over 'request.env' should show Environment type. Got: {}", |
| 68 | + hover_text |
| 69 | + ); |
| 70 | + assert!( |
| 71 | + hover_text.contains("None"), |
| 72 | + "Hover over 'request.env' should show None. Got: {}", |
| 73 | + hover_text |
| 74 | + ); |
| 75 | +} |
| 76 | + |
| 77 | +/// Test that request.env provides correct definitions |
| 78 | +fn test_request_env_definition( |
| 79 | + session: &mut SessionInfo, |
| 80 | + file_symbol: &Rc<RefCell<Symbol>>, |
| 81 | + file_info: &Rc<RefCell<FileInfo>> |
| 82 | +) { |
| 83 | + // Test 1: Go-to-definition on 'request' import (line 3: from odoo.http import request) |
| 84 | + // Should navigate to request variable in odoo.http |
| 85 | + let definitions = test_utils::get_definition_locs(session, file_symbol, file_info, 2, 22); |
| 86 | + |
| 87 | + assert!( |
| 88 | + !definitions.is_empty(), |
| 89 | + "Should find definition for 'request' import" |
| 90 | + ); |
| 91 | + |
| 92 | + // Verify it points to odoo/http.py or similar |
| 93 | + let target_uri = &definitions[0].target_uri.to_string(); |
| 94 | + assert!( |
| 95 | + target_uri.contains("odoo/http.py"), |
| 96 | + "Definition should point to http module. Got: {:?}", |
| 97 | + target_uri |
| 98 | + ); |
| 99 | + |
| 100 | + // Test 2: Go-to-definition on 'request.env' (line 15: env = request.env) |
| 101 | + // Should navigate to Environment class or env attribute |
| 102 | + let definitions = test_utils::get_definition_locs(session, file_symbol, file_info, 14, 21); |
| 103 | + |
| 104 | + assert!( |
| 105 | + !definitions.is_empty(), |
| 106 | + "Should find definition for 'request.env'" |
| 107 | + ); |
| 108 | + |
| 109 | + // Test 3: Hover on request.env.user (line 34: current_user = request.env.user) |
| 110 | + // Should show proper type for user |
| 111 | + let hover_text = test_utils::get_hover_markdown(session, file_symbol, file_info, 33, 35); |
| 112 | + |
| 113 | + if let Some(text) = hover_text { |
| 114 | + // env.user should have a type (typically res.users or User) |
| 115 | + assert!( |
| 116 | + !text.is_empty(), |
| 117 | + "request.env.user should have hover information" |
| 118 | + ); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | + |
0 commit comments