|
| 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_controller() { |
| 19 | + let (mut odoo, config) = setup::setup::setup_server(true); |
| 20 | + let mut session = setup::setup::create_init_session(&mut odoo, config); |
| 21 | + |
| 22 | + let test_file = PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 23 | + .join("tests/data/addons/module_1/controllers/main.py") |
| 24 | + .sanitize(); |
| 25 | + |
| 26 | + let Some(file_symbol) = SyncOdoo::get_symbol_of_opened_file(&mut session, &PathBuf::from(&test_file)) else { |
| 27 | + panic!("Failed to get file symbol for {}", test_file); |
| 28 | + }; |
| 29 | + |
| 30 | + let file_mgr = session.sync_odoo.get_file_mgr(); |
| 31 | + let file_info = file_mgr.borrow().get_file_info(&test_file).unwrap(); |
| 32 | + |
| 33 | + test_request_type_hover(&mut session, &file_symbol, &file_info); |
| 34 | + test_request_env_definition(&mut session, &file_symbol, &file_info); |
| 35 | + test_request_env_subscript(&mut session, &file_symbol, &file_info); |
| 36 | +} |
| 37 | + |
| 38 | +/// Test that hovering over 'request' shows Request class |
| 39 | +/// and that hovering over 'request.env' shows Environment type |
| 40 | +fn test_request_type_hover( |
| 41 | + session: &mut SessionInfo, |
| 42 | + file_symbol: &Rc<RefCell<Symbol>>, |
| 43 | + file_info: &Rc<RefCell<FileInfo>> |
| 44 | +) { |
| 45 | + // Test 1: Hover over 'request' variable (line 11: req = request) |
| 46 | + // Should show Request class |
| 47 | + let hover_text = test_utils::get_hover_markdown(session, file_symbol, file_info, 11, 14) |
| 48 | + .expect("Should get hover text for request"); |
| 49 | + |
| 50 | + assert!( |
| 51 | + hover_text.contains("Request"), |
| 52 | + "Hover over 'request' should show Request class. Got: {}", |
| 53 | + hover_text |
| 54 | + ); |
| 55 | + |
| 56 | + // Test 2: Hover over 'request.env' (line 14: env = request.env) |
| 57 | + // Should show Environment | None |
| 58 | + let hover_text = test_utils::get_hover_markdown(session, file_symbol, file_info, 14, 21) |
| 59 | + .expect("Should get hover text for request.env"); |
| 60 | + |
| 61 | + assert!( |
| 62 | + hover_text.contains("Environment"), |
| 63 | + "Hover over 'request.env' should show Environment type. Got: {}", |
| 64 | + hover_text |
| 65 | + ); |
| 66 | + assert!( |
| 67 | + hover_text.contains("None"), |
| 68 | + "Hover over 'request.env' should show None. Got: {}", |
| 69 | + hover_text |
| 70 | + ); |
| 71 | +} |
| 72 | + |
| 73 | +/// Test that request.env provides correct definitions |
| 74 | +fn test_request_env_definition( |
| 75 | + session: &mut SessionInfo, |
| 76 | + file_symbol: &Rc<RefCell<Symbol>>, |
| 77 | + file_info: &Rc<RefCell<FileInfo>> |
| 78 | +) { |
| 79 | + // Test 1: Go-to-definition on 'request' import (line 3: from odoo.http import request) |
| 80 | + // Should navigate to request variable in odoo.http |
| 81 | + let definitions = test_utils::get_definition_locs(session, file_symbol, file_info, 2, 22); |
| 82 | + |
| 83 | + assert!( |
| 84 | + !definitions.is_empty(), |
| 85 | + "Should find definition for 'request' import" |
| 86 | + ); |
| 87 | + |
| 88 | + // Verify it points to the correct file |
| 89 | + let target_uri = &definitions[0].target_uri.to_string(); |
| 90 | + assert!( |
| 91 | + target_uri.contains("odoo/http"), |
| 92 | + "Definition should point to http or requestlib. Got: {:?}", |
| 93 | + target_uri |
| 94 | + ); |
| 95 | + |
| 96 | + // Test 2: Go-to-definition on 'request.env' (line 15: env = request.env) |
| 97 | + let definitions = test_utils::get_definition_locs(session, file_symbol, file_info, 14, 22); |
| 98 | + assert!( |
| 99 | + !definitions.is_empty(), |
| 100 | + "Should find definition for 'request.env'" |
| 101 | + ); |
| 102 | +} |
| 103 | + |
| 104 | +/// Test that request.env["model_name"] resolves to Model instance |
| 105 | +fn test_request_env_subscript( |
| 106 | + session: &mut SessionInfo, |
| 107 | + file_symbol: &Rc<RefCell<Symbol>>, |
| 108 | + file_info: &Rc<RefCell<FileInfo>> |
| 109 | +) { |
| 110 | + let partner_class = test_utils::PARTNER_CLASS_NAME(&session.sync_odoo.full_version); |
| 111 | + |
| 112 | + // Test 1: Hover over 'partner' variable (line 18: partner = request.env['res.partner']) |
| 113 | + // Should show Partner/ResPartner class |
| 114 | + let hover_text = test_utils::get_hover_markdown(session, file_symbol, file_info, 17, 8) |
| 115 | + .expect("Should get hover text for 'partner' variable"); |
| 116 | + |
| 117 | + assert!( |
| 118 | + hover_text.contains(partner_class), |
| 119 | + "Hover over 'partner' should show {} class. Got: {}", |
| 120 | + partner_class, |
| 121 | + hover_text |
| 122 | + ); |
| 123 | + |
| 124 | + // Test 2: Hover over .search on request.env['res.partner'].search (line 19) |
| 125 | + // Should display markdown content for the search method |
| 126 | + let hover_text = test_utils::get_hover_markdown(session, file_symbol, file_info, 18, 46) |
| 127 | + .expect("Should get hover text for .search method"); |
| 128 | + |
| 129 | + assert!( |
| 130 | + hover_text.contains("def search"), |
| 131 | + "Hover over .search should show its definition. Got: {}", |
| 132 | + hover_text |
| 133 | + ); |
| 134 | +} |
0 commit comments