octocode 0.16.0

AI-powered code intelligence with semantic search, knowledge graphs, and built-in MCP server. Transform your codebase into a queryable knowledge graph for AI assistants.
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
// Copyright 2026 Muvon Un Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#[cfg(test)]
mod swift_tests {
	use crate::indexer::code_region_extractor::extract_meaningful_regions;
	use crate::indexer::languages::{self, Language};
	use tree_sitter::Parser;

	fn make_parser() -> (Parser, Box<dyn Language>) {
		let lang = languages::get_language("swift").expect("Swift language not registered");
		let mut parser = Parser::new();
		parser.set_language(&lang.get_ts_language()).unwrap();
		(parser, lang)
	}

	// ── region extraction ────────────────────────────────────────────────────

	#[test]
	fn test_region_extraction_class_and_methods() {
		let code = r#"
import Foundation
import UIKit

public class Animal {
    public var name: String
    private var age: Int

    public init(name: String, age: Int) {
        self.name = name
        self.age = age
    }

    public func speak() -> String {
        return "..."
    }

    deinit {
        print("Animal freed")
    }
}
"#;

		let (mut parser, lang) = make_parser();
		let tree = parser.parse(code, None).unwrap();
		let mut regions = Vec::new();
		extract_meaningful_regions(tree.root_node(), code, lang.as_ref(), &mut regions);

		assert!(
			!regions.is_empty(),
			"Should extract regions from class file"
		);

		let has_import = regions.iter().any(|r| r.node_kind == "import_declaration");
		assert!(has_import, "Should extract import declarations");

		let has_class = regions.iter().any(|r| r.node_kind == "class_declaration");
		assert!(has_class, "Should extract class declaration");
		let has_func = regions
			.iter()
			.any(|r| r.node_kind == "function_declaration" || r.node_kind == "class_declaration");
		assert!(
			has_func,
			"Should extract function or class declaration (speak or Animal)"
		);
	}

	#[test]
	fn test_region_extraction_struct_protocol_enum() {
		let code = r#"
protocol Drawable {
    func draw()
}

struct Point: Drawable {
    var x: Double
    var y: Double

    func draw() {
        print("Point at (\(x), \(y))")
    }
}

enum Direction {
    case north, south, east, west

    func opposite() -> Direction {
        switch self {
        case .north: return .south
        case .south: return .north
        case .east: return .west
        case .west: return .east
        }
    }
}
"#;

		let (mut parser, lang) = make_parser();
		let tree = parser.parse(code, None).unwrap();
		let mut regions = Vec::new();
		extract_meaningful_regions(tree.root_node(), code, lang.as_ref(), &mut regions);

		assert!(!regions.is_empty(), "Should extract regions");

		let has_protocol = regions
			.iter()
			.any(|r| r.node_kind == "protocol_declaration");
		assert!(has_protocol, "Should extract protocol declaration");

		// struct and enum parse as class_declaration in tree-sitter-swift
		let has_type_decl = regions.iter().any(|r| r.node_kind == "class_declaration");
		assert!(
			has_type_decl,
			"Should extract struct/enum as class_declaration"
		);
	}

	#[test]
	fn test_region_extraction_extension_typealias() {
		let code = r#"
typealias Completion = () -> Void
typealias Handler<T> = (T) -> Void

extension String {
    func trimmed() -> String {
        return self.trimmingCharacters(in: .whitespaces)
    }

    var isEmpty: Bool {
        return self.count == 0
    }
}
"#;

		let (mut parser, lang) = make_parser();
		let tree = parser.parse(code, None).unwrap();
		let mut regions = Vec::new();
		extract_meaningful_regions(tree.root_node(), code, lang.as_ref(), &mut regions);

		assert!(
			!regions.is_empty(),
			"Should extract regions from extension file"
		);

		let typealias_regions: Vec<_> = regions
			.iter()
			.filter(|r| r.node_kind == "typealias_declaration")
			.collect();
		assert!(
			!typealias_regions.is_empty(),
			"Should extract typealias declarations"
		);

		let has_extension = regions.iter().any(|r| r.node_kind == "class_declaration");
		assert!(
			has_extension,
			"Should extract extension as class_declaration"
		);
	}

	#[test]
	fn test_region_extraction_free_functions() {
		let code = r#"
func add(_ a: Int, _ b: Int) -> Int {
    return a + b
}

func greet(name: String) -> String {
    return "Hello, \(name)!"
}
"#;

		let (mut parser, lang) = make_parser();
		let tree = parser.parse(code, None).unwrap();
		let mut regions = Vec::new();
		extract_meaningful_regions(tree.root_node(), code, lang.as_ref(), &mut regions);

		assert!(
			!regions.is_empty(),
			"Should extract free functions (may be merged into one region)"
		);
	}

	// ── extract_symbols ──────────────────────────────────────────────────────

	#[test]
	fn test_extract_symbols_function() {
		let code = r#"func computeSum(_ values: [Int]) -> Int { return 0 }"#;
		let (mut parser, lang) = make_parser();
		let tree = parser.parse(code, None).unwrap();

		// The root is source_file; first named child should be function_declaration
		let func_node = tree
			.root_node()
			.named_children(&mut tree.root_node().walk())
			.find(|n| n.kind() == "function_declaration")
			.expect("Should have function_declaration");

		let symbols = lang.extract_symbols(func_node, code);
		assert!(
			symbols.contains(&"computeSum".to_string()),
			"Should extract function name: got {:?}",
			symbols
		);
	}

	#[test]
	fn test_extract_symbols_class_declaration() {
		let code = r#"class MyViewController: UIViewController { }"#;
		let (mut parser, lang) = make_parser();
		let tree = parser.parse(code, None).unwrap();

		let class_node = tree
			.root_node()
			.named_children(&mut tree.root_node().walk())
			.find(|n| n.kind() == "class_declaration")
			.expect("Should have class_declaration");

		let symbols = lang.extract_symbols(class_node, code);
		assert!(
			symbols.contains(&"MyViewController".to_string()),
			"Should extract class name: got {:?}",
			symbols
		);
	}

	#[test]
	fn test_extract_symbols_protocol() {
		let code = r#"protocol Serializable { func serialize() -> Data }"#;
		let (mut parser, lang) = make_parser();
		let tree = parser.parse(code, None).unwrap();

		let proto_node = tree
			.root_node()
			.named_children(&mut tree.root_node().walk())
			.find(|n| n.kind() == "protocol_declaration")
			.expect("Should have protocol_declaration");

		let symbols = lang.extract_symbols(proto_node, code);
		assert!(
			symbols.contains(&"Serializable".to_string()),
			"Should extract protocol name: got {:?}",
			symbols
		);
	}

	#[test]
	fn test_extract_symbols_typealias() {
		let code = r#"typealias CompletionHandler = (Bool, Error?) -> Void"#;
		let (mut parser, lang) = make_parser();
		let tree = parser.parse(code, None).unwrap();

		let alias_node = tree
			.root_node()
			.named_children(&mut tree.root_node().walk())
			.find(|n| n.kind() == "typealias_declaration")
			.expect("Should have typealias_declaration");

		let symbols = lang.extract_symbols(alias_node, code);
		assert!(
			symbols.contains(&"CompletionHandler".to_string()),
			"Should extract typealias name: got {:?}",
			symbols
		);
	}

	// ── extract_imports_exports ──────────────────────────────────────────────

	#[test]
	fn test_extract_imports_foundation() {
		let code = r#"import Foundation"#;
		let (mut parser, lang) = make_parser();
		let tree = parser.parse(code, None).unwrap();

		let import_node = tree
			.root_node()
			.named_children(&mut tree.root_node().walk())
			.find(|n| n.kind() == "import_declaration")
			.expect("Should have import_declaration");

		let (imports, exports) = lang.extract_imports_exports(import_node, code);
		assert!(
			imports.iter().any(|i| i.contains("Foundation")),
			"Should extract Foundation import: got {:?}",
			imports
		);
		assert!(exports.is_empty(), "Import node should have no exports");
	}

	#[test]
	fn test_extract_imports_uikit_and_swiftui() {
		let code = r#"
import UIKit
import SwiftUI
"#;
		let (mut parser, lang) = make_parser();
		let tree = parser.parse(code, None).unwrap();

		let mut all_imports = Vec::new();
		for node in tree
			.root_node()
			.named_children(&mut tree.root_node().walk())
		{
			if node.kind() == "import_declaration" {
				let (imports, _) = lang.extract_imports_exports(node, code);
				all_imports.extend(imports);
			}
		}

		assert!(
			all_imports.iter().any(|i| i.contains("UIKit")),
			"Should extract UIKit import"
		);
		assert!(
			all_imports.iter().any(|i| i.contains("SwiftUI")),
			"Should extract SwiftUI import"
		);
	}

	#[test]
	fn test_extract_exports_public_func() {
		let code = r#"public func doSomething() {}"#;
		let (mut parser, lang) = make_parser();
		let tree = parser.parse(code, None).unwrap();

		let func_node = tree
			.root_node()
			.named_children(&mut tree.root_node().walk())
			.find(|n| n.kind() == "function_declaration")
			.expect("Should have function_declaration");

		let (imports, exports) = lang.extract_imports_exports(func_node, code);
		assert!(imports.is_empty(), "Function node should have no imports");
		assert!(
			exports.contains(&"doSomething".to_string()),
			"Should export public function: got {:?}",
			exports
		);
	}

	#[test]
	fn test_no_export_for_internal_func() {
		let code = r#"func internalHelper() {}"#;
		let (mut parser, lang) = make_parser();
		let tree = parser.parse(code, None).unwrap();

		let func_node = tree
			.root_node()
			.named_children(&mut tree.root_node().walk())
			.find(|n| n.kind() == "function_declaration")
			.expect("Should have function_declaration");

		let (_, exports) = lang.extract_imports_exports(func_node, code);
		assert!(
			exports.is_empty(),
			"Internal function should not be exported: got {:?}",
			exports
		);
	}

	// ── extract_function_calls ───────────────────────────────────────────────

	#[test]
	fn test_extract_function_calls() {
		// We need to find call_expression nodes inside a function body
		let code = r#"
func test() {
    print("hello")
    doWork()
    someObject.process()
}
"#;
		let (mut parser, lang) = make_parser();
		let tree = parser.parse(code, None).unwrap();

		let mut calls = Vec::new();
		collect_calls(tree.root_node(), code, lang.as_ref(), &mut calls);

		// Should capture at least "print" and "doWork"
		assert!(
			!calls.is_empty(),
			"Should extract function calls: got {:?}",
			calls
		);
		assert!(
			calls.iter().any(|c| c == "print"),
			"Should contain 'print' call: got {:?}",
			calls
		);
	}

	fn collect_calls(
		node: tree_sitter::Node,
		code: &str,
		lang: &dyn Language,
		calls: &mut Vec<String>,
	) {
		calls.extend(lang.extract_function_calls(node, code));
		let mut cursor = node.walk();
		for child in node.children(&mut cursor) {
			collect_calls(child, code, lang, calls);
		}
	}

	// ── extract_type_relations ───────────────────────────────────────────────

	#[test]
	fn test_type_relations_class_inheritance() {
		use crate::indexer::languages::TypeRelationKind;

		let code = r#"class Dog: Animal, Trainable { }"#;
		let (mut parser, lang) = make_parser();
		let tree = parser.parse(code, None).unwrap();

		let class_node = tree
			.root_node()
			.named_children(&mut tree.root_node().walk())
			.find(|n| n.kind() == "class_declaration")
			.expect("Should have class_declaration");

		let relations = lang.extract_type_relations(class_node, code);
		assert!(
			!relations.is_empty(),
			"Should extract type relations: got {:?}",
			relations
		);

		// All should be Implements (we conservatively treat all specifiers as Implements)
		assert!(
			relations
				.iter()
				.all(|(k, _)| *k == TypeRelationKind::Implements),
			"Class specifiers should produce Implements relations"
		);

		let names: Vec<&String> = relations.iter().map(|(_, n)| n).collect();
		assert!(
			names.iter().any(|n| n.as_str() == "Animal"),
			"Should reference Animal: got {:?}",
			names
		);
	}

	#[test]
	fn test_type_relations_protocol_inheritance() {
		use crate::indexer::languages::TypeRelationKind;

		let code = r#"protocol Flyable: Movable { }"#;
		let (mut parser, lang) = make_parser();
		let tree = parser.parse(code, None).unwrap();

		let proto_node = tree
			.root_node()
			.named_children(&mut tree.root_node().walk())
			.find(|n| n.kind() == "protocol_declaration")
			.expect("Should have protocol_declaration");

		let relations = lang.extract_type_relations(proto_node, code);
		assert!(
			!relations.is_empty(),
			"Protocol should emit inheritance relations: got {:?}",
			relations
		);

		assert!(
			relations
				.iter()
				.any(|(k, _)| *k == TypeRelationKind::Extends),
			"Protocol inheritance should use Extends"
		);

		let names: Vec<&String> = relations.iter().map(|(_, n)| n).collect();
		assert!(
			names.iter().any(|n| n.as_str() == "Movable"),
			"Should reference Movable: got {:?}",
			names
		);
	}

	#[test]
	fn test_no_type_relations_for_plain_function() {
		let code = r#"func standalone() {}"#;
		let (mut parser, lang) = make_parser();
		let tree = parser.parse(code, None).unwrap();

		let func_node = tree
			.root_node()
			.named_children(&mut tree.root_node().walk())
			.find(|n| n.kind() == "function_declaration")
			.expect("Should have function_declaration");

		let relations = lang.extract_type_relations(func_node, code);
		assert!(
			relations.is_empty(),
			"Plain function should have no type relations"
		);
	}

	// ── resolve_import ───────────────────────────────────────────────────────

	#[test]
	fn test_resolve_import_relative() {
		let lang = languages::get_language("swift").unwrap();

		let all_files = vec![
			"Sources/App/Models/User.swift".to_string(),
			"Sources/App/Services/UserService.swift".to_string(),
			"Sources/App/Views/UserView.swift".to_string(),
			// Path that ./Models/User resolves to from Services/
			"Sources/App/Services/Models/User.swift".to_string(),
		];

		// Relative import from UserService.swift
		let result = lang.resolve_import(
			"./Models/User",
			"Sources/App/Services/UserService.swift",
			&all_files,
		);
		assert!(
			result.is_some(),
			"Should resolve relative import to User.swift"
		);
	}

	#[test]
	fn test_resolve_import_module_maps_to_directory() {
		let lang = languages::get_language("swift").unwrap();

		let all_files = vec![
			"Sources/Models/User.swift".to_string(),
			"Sources/Models/Post.swift".to_string(),
			"Sources/Services/AuthService.swift".to_string(),
		];

		// Module-level import "Models" — should find a file in Models directory
		let result = lang.resolve_import("Models", "Sources/main.swift", &all_files);
		// May or may not resolve depending on implementation — just check no panic
		let _ = result;
	}

	// ── metadata ────────────────────────────────────────────────────────────

	#[test]
	fn test_language_name_and_extensions() {
		let lang = languages::get_language("swift").unwrap();
		assert_eq!(lang.name(), "swift");
		assert!(
			lang.get_file_extensions().contains(&"swift"),
			"Should support .swift extension"
		);
	}

	#[test]
	fn test_are_node_types_equivalent() {
		let lang = languages::get_language("swift").unwrap();

		// Same type
		assert!(lang.are_node_types_equivalent("function_declaration", "function_declaration"));

		// Function and init are grouped
		assert!(lang.are_node_types_equivalent("function_declaration", "init_declaration"));

		// Property and subscript are grouped
		assert!(lang.are_node_types_equivalent("property_declaration", "subscript_declaration"));

		// Different groups should not be equivalent
		assert!(!lang.are_node_types_equivalent("function_declaration", "class_declaration"));
		assert!(!lang.are_node_types_equivalent("import_declaration", "function_declaration"));
	}

	#[test]
	fn test_get_node_type_description() {
		let lang = languages::get_language("swift").unwrap();

		assert_eq!(
			lang.get_node_type_description("function_declaration"),
			"function declarations"
		);
		assert!(
			lang.get_node_type_description("class_declaration")
				.contains("class"),
			"class_declaration description should mention class"
		);
		assert_eq!(
			lang.get_node_type_description("protocol_declaration"),
			"protocol declarations"
		);
		assert_eq!(
			lang.get_node_type_description("import_declaration"),
			"import statements"
		);
		assert_eq!(
			lang.get_node_type_description("typealias_declaration"),
			"type alias declarations"
		);
	}
}