sqry-core 6.0.12

Core library for sqry - semantic code search engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! End-to-end integration tests for the plugin system
//!
//! These tests validate the complete workflow from plugin registration
//! to unified graph builds using real plugins (`RustPlugin`).

use sqry_core::graph::unified::build::{StagingGraph, StagingOp};
use sqry_core::graph::unified::node::NodeKind;
use sqry_core::plugin::{LanguagePlugin, PluginManager};
use sqry_lang_rust::RustPlugin;
use tempfile::TempDir;

use sqry_core::test_support::verbosity;
use std::sync::Once;
use std::{collections::HashMap, path::Path};

// Initialize verbose logging once for all tests in this file
static INIT: Once = Once::new();

fn init_logging() {
    INIT.call_once(|| {
        verbosity::init(env!("CARGO_PKG_NAME"));
    });
}

/// Scenario 1: Full Rust workflow - register plugin and build graph
#[test]
fn test_full_rust_workflow() {
    init_logging();
    log::info!("Testing test_full_rust_workflow");

    // Create PluginManager and register RustPlugin
    let mut manager = PluginManager::new();
    manager.register_builtin(Box::new(RustPlugin::default()));

    // Create a test Rust file
    let dir = TempDir::new().unwrap();
    let file_path = dir.path().join("test.rs");
    std::fs::write(
        &file_path,
        r#"
fn main() {
    println!("Hello, world!");
}

struct Point {
    x: i32,
    y: i32,
}

impl Point {
    fn new(x: i32, y: i32) -> Self {
        Point { x, y }
    }

    fn distance(&self) -> f64 {
        ((self.x * self.x + self.y * self.y) as f64).sqrt()
    }
}

enum Color {
    Red,
    Green,
    Blue,
}

trait Drawable {
    fn draw(&self);
}

const PI: f64 = 3.14159;
static COUNTER: i32 = 0;
type Result<T> = std::result::Result<T, Error>;
"#,
    )
    .unwrap();

    // Get plugin and build graph
    let plugin = manager
        .plugin_for_extension("rs")
        .expect("Rust plugin should be registered");

    let content = std::fs::read_to_string(&file_path).unwrap();
    let staging = build_staging_graph(plugin, &file_path, &content);

    assert!(has_node(&staging, NodeKind::Function, "main"));
    assert!(has_node(&staging, NodeKind::Struct, "Point"));
    assert!(has_node(&staging, NodeKind::Method, "new"));
    assert!(has_node(&staging, NodeKind::Method, "distance"));
    assert!(has_node(&staging, NodeKind::Enum, "Color"));
    assert!(has_node(&staging, NodeKind::Interface, "Drawable"));
    assert!(has_node(&staging, NodeKind::Constant, "PI"));
    assert!(has_node(&staging, NodeKind::Constant, "COUNTER"));
    assert!(has_node(&staging, NodeKind::Type, "Result"));
}

/// Scenario 2: Multi-file extraction
#[test]
fn test_multi_file_extraction() {
    let mut manager = PluginManager::new();
    manager.register_builtin(Box::new(RustPlugin::default()));

    let dir = TempDir::new().unwrap();

    // Create multiple Rust files
    let file1 = dir.path().join("module1.rs");
    std::fs::write(&file1, "fn foo() {} fn bar() {}").unwrap();

    let file2 = dir.path().join("module2.rs");
    std::fs::write(&file2, "struct Baz {} enum Qux { A, B }").unwrap();

    let file3 = dir.path().join("module3.rs");
    std::fs::write(&file3, "trait Trait1 {} const C: i32 = 42;").unwrap();

    // Extract from all files
    let plugin = manager.plugin_for_extension("rs").unwrap();

    let content1 = std::fs::read_to_string(&file1).unwrap();
    let staging1 = build_staging_graph(plugin, &file1, &content1);

    let content2 = std::fs::read_to_string(&file2).unwrap();
    let staging2 = build_staging_graph(plugin, &file2, &content2);

    let content3 = std::fs::read_to_string(&file3).unwrap();
    let staging3 = build_staging_graph(plugin, &file3, &content3);

    assert!(has_node(&staging1, NodeKind::Function, "foo"));
    assert!(has_node(&staging1, NodeKind::Function, "bar"));
    assert!(has_node(&staging2, NodeKind::Struct, "Baz"));
    assert!(has_node(&staging2, NodeKind::Enum, "Qux"));
    assert!(has_node(&staging3, NodeKind::Interface, "Trait1"));
    assert!(has_node(&staging3, NodeKind::Constant, "C"));
}

/// Scenario 3: Error handling - invalid file extension
#[test]
fn test_error_invalid_extension() {
    let manager = PluginManager::new();

    // Try to get plugin for unsupported extension
    let plugin = manager.plugin_for_extension("txt");

    assert!(plugin.is_none(), "Should not find plugin for .txt files");
}

/// Scenario 4: Error handling - malformed Rust syntax
#[test]
fn test_error_malformed_syntax() {
    let mut manager = PluginManager::new();
    manager.register_builtin(Box::new(RustPlugin::default()));

    let dir = TempDir::new().unwrap();
    let file_path = dir.path().join("malformed.rs");
    std::fs::write(&file_path, "fn main() { // Missing closing brace").unwrap();

    let plugin = manager.plugin_for_extension("rs").unwrap();
    let content = std::fs::read_to_string(&file_path).unwrap();

    let result = plugin.parse_ast(content.as_bytes());
    assert!(result.is_ok(), "Should handle malformed syntax gracefully");

    let staging = build_staging_graph(plugin, &file_path, &content);
    assert!(count_nodes(&staging) <= 1);
}

/// Scenario 5: Error handling - empty file
#[test]
fn test_error_empty_file() {
    let mut manager = PluginManager::new();
    manager.register_builtin(Box::new(RustPlugin::default()));

    let dir = TempDir::new().unwrap();
    let file_path = dir.path().join("empty.rs");
    std::fs::write(&file_path, "").unwrap();

    let plugin = manager.plugin_for_extension("rs").unwrap();
    let content = std::fs::read_to_string(&file_path).unwrap();
    let staging = build_staging_graph(plugin, &file_path, &content);

    assert_eq!(count_nodes(&staging), 0, "Empty file should stage no nodes");
}

/// Scenario 6: Plugin lookup patterns
#[test]
fn test_plugin_lookup_patterns() {
    let mut manager = PluginManager::new();
    manager.register_builtin(Box::new(RustPlugin::default()));

    // Lookup by extension
    let plugin_ext = manager.plugin_for_extension("rs");
    assert!(plugin_ext.is_some(), "Should find plugin by extension");
    assert_eq!(plugin_ext.unwrap().metadata().id, "rust");

    // Lookup by language ID
    let plugin_id = manager.plugin_by_id("rust");
    assert!(plugin_id.is_some(), "Should find plugin by ID");
    assert_eq!(plugin_id.unwrap().metadata().name, "Rust");

    // Lookup non-existent extension
    assert!(manager.plugin_for_extension("xyz").is_none());

    // Lookup non-existent ID
    assert!(manager.plugin_by_id("nonexistent").is_none());

    // Enumerate all plugins
    let all_plugins = manager.plugins();
    assert_eq!(all_plugins.len(), 1);
    assert_eq!(all_plugins[0].metadata().id, "rust");
}

/// Scenario 7: Plugin metadata validation
#[test]
fn test_plugin_metadata_validation() {
    let plugin = RustPlugin::default();
    let metadata = plugin.metadata();

    // Verify all metadata fields are populated
    assert!(!metadata.id.is_empty(), "ID should not be empty");
    assert!(!metadata.name.is_empty(), "Name should not be empty");
    assert!(!metadata.version.is_empty(), "Version should not be empty");
    assert!(!metadata.author.is_empty(), "Author should not be empty");
    assert!(
        !metadata.description.is_empty(),
        "Description should not be empty"
    );
    assert!(
        !metadata.tree_sitter_version.is_empty(),
        "Tree-sitter version should not be empty"
    );

    // Verify ID follows conventions
    assert!(
        metadata.id.chars().all(|c| c.is_lowercase() || c == '-'),
        "ID should be lowercase with hyphens"
    );

    // Verify extensions are populated
    let extensions = plugin.extensions();
    assert!(!extensions.is_empty(), "Extensions should not be empty");
    assert!(
        extensions.iter().all(|ext| !ext.is_empty()),
        "No empty extensions"
    );
    assert!(
        extensions.iter().all(|ext| !ext.starts_with('.')),
        "Extensions should not start with dot"
    );
}

/// Scenario 8: Edge cases - unicode, whitespace, special characters
#[test]
fn test_edge_cases() {
    let mut manager = PluginManager::new();
    manager.register_builtin(Box::new(RustPlugin::default()));

    let dir = TempDir::new().unwrap();
    let file_path = dir.path().join("edge_cases.rs");
    std::fs::write(
        &file_path,
        r"
// Unicode identifiers (if Rust supports them in the future)
fn hello_世界() {}

// Lots of whitespace
fn   spaced_out  (  )   {   }

// Comment only
// This is a comment

// Empty impl block
impl Foo {}

// Multiple items on one line
fn a() {} fn b() {} fn c() {}
",
    )
    .unwrap();

    let plugin = manager.plugin_for_extension("rs").unwrap();
    let content = std::fs::read_to_string(&file_path).unwrap();
    let staging = build_staging_graph(plugin, &file_path, &content);

    assert!(count_nodes(&staging) >= 4, "Should stage function nodes");
}

/// Integration test: Concurrent access (thread-safety)
#[test]
fn test_concurrent_access() {
    use std::sync::Arc;
    use std::thread;

    let mut manager = PluginManager::new();
    manager.register_builtin(Box::new(RustPlugin::default()));
    let manager = Arc::new(manager);

    let dir = TempDir::new().unwrap();
    let file_path = Arc::new(dir.path().join("concurrent.rs"));
    std::fs::write(
        file_path.as_ref(),
        "fn test1() {}\nfn test2() {}\nfn test3() {}",
    )
    .unwrap();

    // Spawn multiple threads that all use the same PluginManager
    let mut handles = vec![];
    for i in 0..10 {
        let manager_clone = Arc::clone(&manager);
        let file_clone = Arc::clone(&file_path);

        let handle = thread::spawn(move || {
            let plugin = manager_clone.plugin_for_extension("rs").unwrap();
            let content = std::fs::read_to_string(file_clone.as_ref()).unwrap();
            let staging = build_staging_graph(plugin, file_clone.as_ref(), &content);

            let function_count = count_nodes_by_kind(&staging, NodeKind::Function);
            assert_eq!(function_count, 3, "Thread {i} got wrong function count");
            function_count
        });

        handles.push(handle);
    }

    // Wait for all threads and verify they all succeeded
    for handle in handles {
        let function_count = handle.join().expect("Thread panicked");
        assert_eq!(function_count, 3);
    }
}

fn build_staging_graph(
    plugin: &dyn LanguagePlugin,
    file_path: &Path,
    content: &str,
) -> StagingGraph {
    let tree = plugin
        .parse_ast(content.as_bytes())
        .expect("Failed to parse AST");
    let builder = plugin
        .graph_builder()
        .expect("Rust plugin should provide GraphBuilder");
    let mut staging = StagingGraph::new();
    builder
        .build_graph(&tree, content.as_bytes(), file_path, &mut staging)
        .expect("Graph build failed");
    staging
}

fn build_string_lookup(staging: &StagingGraph) -> HashMap<u32, String> {
    staging
        .operations()
        .iter()
        .filter_map(|op| {
            if let StagingOp::InternString { local_id, value } = op {
                Some((local_id.index(), value.clone()))
            } else {
                None
            }
        })
        .collect()
}

fn count_nodes(staging: &StagingGraph) -> usize {
    staging
        .operations()
        .iter()
        .filter(|op| matches!(op, StagingOp::AddNode { .. }))
        .count()
}

fn count_nodes_by_kind(staging: &StagingGraph, kind: NodeKind) -> usize {
    staging
        .operations()
        .iter()
        .filter(|op| {
            if let StagingOp::AddNode { entry, .. } = op {
                entry.kind == kind
            } else {
                false
            }
        })
        .count()
}

fn has_node(staging: &StagingGraph, kind: NodeKind, name: &str) -> bool {
    let strings = build_string_lookup(staging);

    staging.operations().iter().any(|op| {
        if let StagingOp::AddNode { entry, .. } = op {
            if entry.kind != kind {
                return false;
            }
            let name_idx = entry.qualified_name.unwrap_or(entry.name).index();
            return strings.get(&name_idx).is_some_and(|node_name| {
                node_name == name || short_name(node_name).as_str() == name
            });
        }
        false
    })
}

fn short_name(name: &str) -> String {
    if let Some(pos) = name.rfind("::") {
        return name[pos + 2..].to_string();
    }
    if let Some(pos) = name.rfind('.') {
        return name[pos + 1..].to_string();
    }
    name.to_string()
}