Arrow functions and function expressions assigned to variables
(e.g. `export const useAuth = () => { ... }`) were not being indexed
because the arrow_function AST node has no `name` field — the name
lives on the parent variable_declarator node.
Additionally, `isExported()` for TypeScript and JavaScript extractors
only checked 10 characters back from the node's start position, which
missed `export` for deeply nested nodes like arrow functions inside
variable declarations inside export statements.
Changes:
- extractFunction(): When an arrow_function or function_expression
resolves to '<anonymous>', look up the parent variable_declarator
for the name before skipping.
- isExported() (TS + JS): Walk the parent chain to find an
export_statement ancestor instead of substring matching.
- Add 6 test cases covering arrow function exports, function
expression exports, non-exported arrow functions, anonymous
arrow functions, multiple exports, and JavaScript files.
Tested on a real monorepo (238 files): node count increased from
779 to 958 (+23%), with 94 new nodes in packages/ that previously
had 0 coverage.
766 lines
21 KiB
TypeScript
766 lines
21 KiB
TypeScript
/**
|
|
* Extraction Tests
|
|
*
|
|
* Tests for the tree-sitter extraction system.
|
|
*/
|
|
|
|
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
import * as os from 'os';
|
|
import { CodeGraph } from '../src';
|
|
import { extractFromSource } from '../src/extraction';
|
|
import { detectLanguage, isLanguageSupported, getSupportedLanguages } from '../src/extraction/grammars';
|
|
|
|
// Create a temporary directory for each test
|
|
function createTempDir(): string {
|
|
return fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-test-'));
|
|
}
|
|
|
|
// Clean up temporary directory
|
|
function cleanupTempDir(dir: string): void {
|
|
if (fs.existsSync(dir)) {
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
}
|
|
|
|
describe('Language Detection', () => {
|
|
it('should detect TypeScript files', () => {
|
|
expect(detectLanguage('src/index.ts')).toBe('typescript');
|
|
expect(detectLanguage('components/Button.tsx')).toBe('tsx');
|
|
});
|
|
|
|
it('should detect JavaScript files', () => {
|
|
expect(detectLanguage('index.js')).toBe('javascript');
|
|
expect(detectLanguage('App.jsx')).toBe('jsx');
|
|
expect(detectLanguage('config.mjs')).toBe('javascript');
|
|
});
|
|
|
|
it('should detect Python files', () => {
|
|
expect(detectLanguage('main.py')).toBe('python');
|
|
});
|
|
|
|
it('should detect Go files', () => {
|
|
expect(detectLanguage('main.go')).toBe('go');
|
|
});
|
|
|
|
it('should detect Rust files', () => {
|
|
expect(detectLanguage('lib.rs')).toBe('rust');
|
|
});
|
|
|
|
it('should detect Java files', () => {
|
|
expect(detectLanguage('Main.java')).toBe('java');
|
|
});
|
|
|
|
it('should detect C files', () => {
|
|
expect(detectLanguage('main.c')).toBe('c');
|
|
expect(detectLanguage('utils.h')).toBe('c');
|
|
});
|
|
|
|
it('should detect C++ files', () => {
|
|
expect(detectLanguage('main.cpp')).toBe('cpp');
|
|
expect(detectLanguage('class.hpp')).toBe('cpp');
|
|
});
|
|
|
|
it('should detect C# files', () => {
|
|
expect(detectLanguage('Program.cs')).toBe('csharp');
|
|
});
|
|
|
|
it('should detect PHP files', () => {
|
|
expect(detectLanguage('index.php')).toBe('php');
|
|
});
|
|
|
|
it('should detect Ruby files', () => {
|
|
expect(detectLanguage('app.rb')).toBe('ruby');
|
|
});
|
|
|
|
it('should detect Swift files', () => {
|
|
expect(detectLanguage('ViewController.swift')).toBe('swift');
|
|
});
|
|
|
|
it('should detect Kotlin files', () => {
|
|
expect(detectLanguage('MainActivity.kt')).toBe('kotlin');
|
|
expect(detectLanguage('build.gradle.kts')).toBe('kotlin');
|
|
});
|
|
|
|
it('should return unknown for unsupported extensions', () => {
|
|
expect(detectLanguage('styles.css')).toBe('unknown');
|
|
expect(detectLanguage('data.json')).toBe('unknown');
|
|
});
|
|
});
|
|
|
|
describe('Language Support', () => {
|
|
it('should report supported languages', () => {
|
|
expect(isLanguageSupported('typescript')).toBe(true);
|
|
expect(isLanguageSupported('python')).toBe(true);
|
|
expect(isLanguageSupported('go')).toBe(true);
|
|
expect(isLanguageSupported('unknown')).toBe(false);
|
|
});
|
|
|
|
it('should list all supported languages', () => {
|
|
const languages = getSupportedLanguages();
|
|
expect(languages).toContain('typescript');
|
|
expect(languages).toContain('javascript');
|
|
expect(languages).toContain('python');
|
|
expect(languages).toContain('go');
|
|
expect(languages).toContain('rust');
|
|
expect(languages).toContain('java');
|
|
expect(languages).toContain('csharp');
|
|
expect(languages).toContain('php');
|
|
expect(languages).toContain('ruby');
|
|
expect(languages).toContain('swift');
|
|
expect(languages).toContain('kotlin');
|
|
});
|
|
});
|
|
|
|
describe('TypeScript Extraction', () => {
|
|
it('should extract function declarations', () => {
|
|
const code = `
|
|
export function processPayment(amount: number): Promise<Receipt> {
|
|
return stripe.charge(amount);
|
|
}
|
|
`;
|
|
const result = extractFromSource('payment.ts', code);
|
|
|
|
expect(result.nodes).toHaveLength(1);
|
|
expect(result.nodes[0]).toMatchObject({
|
|
kind: 'function',
|
|
name: 'processPayment',
|
|
language: 'typescript',
|
|
isExported: true,
|
|
});
|
|
expect(result.nodes[0]?.signature).toContain('amount: number');
|
|
});
|
|
|
|
it('should extract class declarations', () => {
|
|
const code = `
|
|
export class PaymentService {
|
|
private stripe: StripeClient;
|
|
|
|
constructor(apiKey: string) {
|
|
this.stripe = new StripeClient(apiKey);
|
|
}
|
|
|
|
async charge(amount: number): Promise<Receipt> {
|
|
return this.stripe.charge(amount);
|
|
}
|
|
}
|
|
`;
|
|
const result = extractFromSource('service.ts', code);
|
|
|
|
const classNode = result.nodes.find((n) => n.kind === 'class');
|
|
const methodNodes = result.nodes.filter((n) => n.kind === 'method');
|
|
|
|
expect(classNode).toBeDefined();
|
|
expect(classNode?.name).toBe('PaymentService');
|
|
expect(classNode?.isExported).toBe(true);
|
|
|
|
expect(methodNodes.length).toBeGreaterThanOrEqual(1);
|
|
const chargeMethod = methodNodes.find((m) => m.name === 'charge');
|
|
expect(chargeMethod).toBeDefined();
|
|
});
|
|
|
|
it('should extract interfaces', () => {
|
|
const code = `
|
|
export interface User {
|
|
id: string;
|
|
name: string;
|
|
email: string;
|
|
}
|
|
`;
|
|
const result = extractFromSource('types.ts', code);
|
|
|
|
expect(result.nodes).toHaveLength(1);
|
|
expect(result.nodes[0]).toMatchObject({
|
|
kind: 'interface',
|
|
name: 'User',
|
|
isExported: true,
|
|
});
|
|
});
|
|
|
|
it('should track function calls', () => {
|
|
const code = `
|
|
function main() {
|
|
const result = processData();
|
|
console.log(result);
|
|
}
|
|
`;
|
|
const result = extractFromSource('main.ts', code);
|
|
|
|
expect(result.unresolvedReferences.length).toBeGreaterThan(0);
|
|
const calls = result.unresolvedReferences.filter((r) => r.referenceKind === 'calls');
|
|
expect(calls.some((c) => c.referenceName === 'processData')).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('Arrow Function Export Extraction', () => {
|
|
it('should extract exported arrow functions assigned to const', () => {
|
|
const code = `
|
|
export const useAuth = (): AuthContextValue => {
|
|
return useContext(AuthContext);
|
|
};
|
|
`;
|
|
const result = extractFromSource('hooks.ts', code);
|
|
|
|
expect(result.nodes).toHaveLength(1);
|
|
expect(result.nodes[0]).toMatchObject({
|
|
kind: 'function',
|
|
name: 'useAuth',
|
|
isExported: true,
|
|
});
|
|
});
|
|
|
|
it('should extract exported function expressions assigned to const', () => {
|
|
const code = `
|
|
export const processData = function(input: string): string {
|
|
return input.trim();
|
|
};
|
|
`;
|
|
const result = extractFromSource('utils.ts', code);
|
|
|
|
expect(result.nodes).toHaveLength(1);
|
|
expect(result.nodes[0]).toMatchObject({
|
|
kind: 'function',
|
|
name: 'processData',
|
|
isExported: true,
|
|
});
|
|
});
|
|
|
|
it('should not extract non-exported arrow functions as exported', () => {
|
|
const code = `
|
|
const internalHelper = () => {
|
|
return 42;
|
|
};
|
|
`;
|
|
const result = extractFromSource('internal.ts', code);
|
|
|
|
const helperNode = result.nodes.find((n) => n.name === 'internalHelper');
|
|
expect(helperNode).toBeDefined();
|
|
expect(helperNode?.isExported).toBeFalsy();
|
|
});
|
|
|
|
it('should still skip truly anonymous arrow functions', () => {
|
|
const code = `
|
|
const items = [1, 2, 3].map((x) => x * 2);
|
|
`;
|
|
const result = extractFromSource('anon.ts', code);
|
|
|
|
// The inline arrow function passed to .map() has no variable_declarator parent
|
|
// and should remain anonymous (skipped)
|
|
const anonFunctions = result.nodes.filter(
|
|
(n) => n.kind === 'function' && n.name === '<anonymous>'
|
|
);
|
|
expect(anonFunctions).toHaveLength(0);
|
|
});
|
|
|
|
it('should extract multiple exported arrow functions from the same file', () => {
|
|
const code = `
|
|
export const add = (a: number, b: number): number => a + b;
|
|
|
|
export const subtract = (a: number, b: number): number => a - b;
|
|
|
|
const internal = () => 'not exported';
|
|
`;
|
|
const result = extractFromSource('math.ts', code);
|
|
|
|
const exported = result.nodes.filter((n) => n.kind === 'function' && n.isExported);
|
|
expect(exported).toHaveLength(2);
|
|
expect(exported.map((n) => n.name).sort()).toEqual(['add', 'subtract']);
|
|
|
|
const internalNode = result.nodes.find((n) => n.name === 'internal');
|
|
expect(internalNode).toBeDefined();
|
|
expect(internalNode?.isExported).toBeFalsy();
|
|
});
|
|
|
|
it('should extract arrow functions in JavaScript files', () => {
|
|
const code = `
|
|
export const fetchData = async () => {
|
|
const response = await fetch('/api/data');
|
|
return response.json();
|
|
};
|
|
`;
|
|
const result = extractFromSource('api.js', code);
|
|
|
|
expect(result.nodes).toHaveLength(1);
|
|
expect(result.nodes[0]).toMatchObject({
|
|
kind: 'function',
|
|
name: 'fetchData',
|
|
isExported: true,
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('Python Extraction', () => {
|
|
it('should extract function definitions', () => {
|
|
const code = `
|
|
def calculate_total(items: list, tax_rate: float) -> float:
|
|
"""Calculate total with tax."""
|
|
subtotal = sum(item.price for item in items)
|
|
return subtotal * (1 + tax_rate)
|
|
`;
|
|
const result = extractFromSource('calc.py', code);
|
|
|
|
expect(result.nodes).toHaveLength(1);
|
|
expect(result.nodes[0]).toMatchObject({
|
|
kind: 'function',
|
|
name: 'calculate_total',
|
|
language: 'python',
|
|
});
|
|
});
|
|
|
|
it('should extract class definitions', () => {
|
|
const code = `
|
|
class UserService:
|
|
"""Service for managing users."""
|
|
|
|
def __init__(self, db):
|
|
self.db = db
|
|
|
|
def get_user(self, user_id: str) -> User:
|
|
return self.db.find_user(user_id)
|
|
`;
|
|
const result = extractFromSource('service.py', code);
|
|
|
|
const classNode = result.nodes.find((n) => n.kind === 'class');
|
|
expect(classNode).toBeDefined();
|
|
expect(classNode?.name).toBe('UserService');
|
|
});
|
|
});
|
|
|
|
describe('Go Extraction', () => {
|
|
it('should extract function declarations', () => {
|
|
const code = `
|
|
package main
|
|
|
|
func ProcessOrder(order Order) (Receipt, error) {
|
|
// Process the order
|
|
return Receipt{}, nil
|
|
}
|
|
`;
|
|
const result = extractFromSource('main.go', code);
|
|
|
|
const funcNode = result.nodes.find((n) => n.kind === 'function');
|
|
expect(funcNode).toBeDefined();
|
|
expect(funcNode?.name).toBe('ProcessOrder');
|
|
});
|
|
|
|
it('should extract method declarations', () => {
|
|
const code = `
|
|
package main
|
|
|
|
type Service struct {
|
|
db *Database
|
|
}
|
|
|
|
func (s *Service) GetUser(id string) (*User, error) {
|
|
return s.db.FindUser(id)
|
|
}
|
|
`;
|
|
const result = extractFromSource('service.go', code);
|
|
|
|
const methodNode = result.nodes.find((n) => n.kind === 'method');
|
|
expect(methodNode).toBeDefined();
|
|
expect(methodNode?.name).toBe('GetUser');
|
|
});
|
|
});
|
|
|
|
describe('Rust Extraction', () => {
|
|
it('should extract function declarations', () => {
|
|
const code = `
|
|
pub fn process_data(input: &str) -> Result<Output, Error> {
|
|
// Process data
|
|
Ok(Output::new())
|
|
}
|
|
`;
|
|
const result = extractFromSource('lib.rs', code);
|
|
|
|
const funcNode = result.nodes.find((n) => n.kind === 'function');
|
|
expect(funcNode).toBeDefined();
|
|
expect(funcNode?.name).toBe('process_data');
|
|
expect(funcNode?.visibility).toBe('public');
|
|
});
|
|
|
|
it('should extract struct declarations', () => {
|
|
const code = `
|
|
pub struct User {
|
|
pub id: String,
|
|
pub name: String,
|
|
email: String,
|
|
}
|
|
`;
|
|
const result = extractFromSource('models.rs', code);
|
|
|
|
const structNode = result.nodes.find((n) => n.kind === 'struct');
|
|
expect(structNode).toBeDefined();
|
|
expect(structNode?.name).toBe('User');
|
|
});
|
|
|
|
it('should extract trait declarations', () => {
|
|
const code = `
|
|
pub trait Repository {
|
|
fn find(&self, id: &str) -> Option<Entity>;
|
|
fn save(&mut self, entity: Entity) -> Result<(), Error>;
|
|
}
|
|
`;
|
|
const result = extractFromSource('traits.rs', code);
|
|
|
|
const traitNode = result.nodes.find((n) => n.kind === 'trait');
|
|
expect(traitNode).toBeDefined();
|
|
expect(traitNode?.name).toBe('Repository');
|
|
});
|
|
});
|
|
|
|
describe('Java Extraction', () => {
|
|
it('should extract class declarations', () => {
|
|
const code = `
|
|
public class UserService {
|
|
private final UserRepository repository;
|
|
|
|
public UserService(UserRepository repository) {
|
|
this.repository = repository;
|
|
}
|
|
|
|
public User getUser(String id) {
|
|
return repository.findById(id);
|
|
}
|
|
}
|
|
`;
|
|
const result = extractFromSource('UserService.java', code);
|
|
|
|
const classNode = result.nodes.find((n) => n.kind === 'class');
|
|
expect(classNode).toBeDefined();
|
|
expect(classNode?.name).toBe('UserService');
|
|
expect(classNode?.visibility).toBe('public');
|
|
});
|
|
|
|
it('should extract method declarations', () => {
|
|
const code = `
|
|
public class Calculator {
|
|
public static int add(int a, int b) {
|
|
return a + b;
|
|
}
|
|
}
|
|
`;
|
|
const result = extractFromSource('Calculator.java', code);
|
|
|
|
const methodNode = result.nodes.find((n) => n.kind === 'method' && n.name === 'add');
|
|
expect(methodNode).toBeDefined();
|
|
expect(methodNode?.isStatic).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('C# Extraction', () => {
|
|
it('should extract class declarations', () => {
|
|
const code = `
|
|
public class OrderService
|
|
{
|
|
private readonly IOrderRepository _repository;
|
|
|
|
public OrderService(IOrderRepository repository)
|
|
{
|
|
_repository = repository;
|
|
}
|
|
|
|
public async Task<Order> GetOrderAsync(string id)
|
|
{
|
|
return await _repository.FindByIdAsync(id);
|
|
}
|
|
}
|
|
`;
|
|
const result = extractFromSource('OrderService.cs', code);
|
|
|
|
const classNode = result.nodes.find((n) => n.kind === 'class');
|
|
expect(classNode).toBeDefined();
|
|
expect(classNode?.name).toBe('OrderService');
|
|
expect(classNode?.visibility).toBe('public');
|
|
});
|
|
});
|
|
|
|
describe('PHP Extraction', () => {
|
|
it('should extract class declarations', () => {
|
|
const code = `<?php
|
|
|
|
class UserController
|
|
{
|
|
private UserService $userService;
|
|
|
|
public function __construct(UserService $userService)
|
|
{
|
|
$this->userService = $userService;
|
|
}
|
|
|
|
public function show(string $id): User
|
|
{
|
|
return $this->userService->find($id);
|
|
}
|
|
}
|
|
`;
|
|
const result = extractFromSource('UserController.php', code);
|
|
|
|
const classNode = result.nodes.find((n) => n.kind === 'class');
|
|
expect(classNode).toBeDefined();
|
|
expect(classNode?.name).toBe('UserController');
|
|
});
|
|
});
|
|
|
|
describe('Swift Extraction', () => {
|
|
it('should extract class declarations', () => {
|
|
const code = `
|
|
public class NetworkManager {
|
|
private let session: URLSession
|
|
|
|
public init(session: URLSession = .shared) {
|
|
self.session = session
|
|
}
|
|
|
|
public func fetchData(from url: URL) async throws -> Data {
|
|
let (data, _) = try await session.data(from: url)
|
|
return data
|
|
}
|
|
}
|
|
`;
|
|
const result = extractFromSource('NetworkManager.swift', code);
|
|
|
|
const classNode = result.nodes.find((n) => n.kind === 'class');
|
|
expect(classNode).toBeDefined();
|
|
expect(classNode?.name).toBe('NetworkManager');
|
|
});
|
|
|
|
it('should extract function declarations', () => {
|
|
const code = `
|
|
func calculateSum(_ numbers: [Int]) -> Int {
|
|
return numbers.reduce(0, +)
|
|
}
|
|
|
|
public func formatCurrency(amount: Double) -> String {
|
|
return String(format: "$%.2f", amount)
|
|
}
|
|
`;
|
|
const result = extractFromSource('utils.swift', code);
|
|
|
|
const functions = result.nodes.filter((n) => n.kind === 'function');
|
|
expect(functions.length).toBeGreaterThanOrEqual(1);
|
|
});
|
|
|
|
it('should extract struct declarations', () => {
|
|
const code = `
|
|
public struct User {
|
|
let id: UUID
|
|
var name: String
|
|
var email: String
|
|
|
|
func displayName() -> String {
|
|
return name
|
|
}
|
|
}
|
|
`;
|
|
const result = extractFromSource('User.swift', code);
|
|
|
|
const structNode = result.nodes.find((n) => n.kind === 'struct');
|
|
expect(structNode).toBeDefined();
|
|
expect(structNode?.name).toBe('User');
|
|
});
|
|
|
|
it('should extract protocol declarations', () => {
|
|
const code = `
|
|
public protocol Repository {
|
|
associatedtype Entity
|
|
|
|
func find(id: String) async throws -> Entity?
|
|
func save(_ entity: Entity) async throws
|
|
}
|
|
`;
|
|
const result = extractFromSource('Repository.swift', code);
|
|
|
|
const protocolNode = result.nodes.find((n) => n.kind === 'interface');
|
|
expect(protocolNode).toBeDefined();
|
|
expect(protocolNode?.name).toBe('Repository');
|
|
});
|
|
});
|
|
|
|
describe('Kotlin Extraction', () => {
|
|
it('should extract class declarations', () => {
|
|
const code = `
|
|
class UserRepository(private val database: Database) {
|
|
fun findById(id: String): User? {
|
|
return database.query("SELECT * FROM users WHERE id = ?", id)
|
|
}
|
|
|
|
suspend fun save(user: User) {
|
|
database.insert(user)
|
|
}
|
|
}
|
|
`;
|
|
const result = extractFromSource('UserRepository.kt', code);
|
|
|
|
const classNode = result.nodes.find((n) => n.kind === 'class');
|
|
expect(classNode).toBeDefined();
|
|
expect(classNode?.name).toBe('UserRepository');
|
|
});
|
|
|
|
it('should extract function declarations', () => {
|
|
const code = `
|
|
fun calculateTotal(items: List<Item>): Double {
|
|
return items.sumOf { it.price }
|
|
}
|
|
|
|
suspend fun fetchUserData(userId: String): User {
|
|
return api.getUser(userId)
|
|
}
|
|
`;
|
|
const result = extractFromSource('utils.kt', code);
|
|
|
|
const functions = result.nodes.filter((n) => n.kind === 'function');
|
|
expect(functions.length).toBeGreaterThanOrEqual(1);
|
|
});
|
|
|
|
it('should detect suspend functions as async', () => {
|
|
const code = `
|
|
suspend fun loadData(): List<String> {
|
|
delay(1000)
|
|
return listOf("a", "b", "c")
|
|
}
|
|
`;
|
|
const result = extractFromSource('loader.kt', code);
|
|
|
|
const funcNode = result.nodes.find((n) => n.kind === 'function');
|
|
expect(funcNode).toBeDefined();
|
|
expect(funcNode?.isAsync).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('Full Indexing', () => {
|
|
let tempDir: string;
|
|
|
|
beforeEach(() => {
|
|
tempDir = createTempDir();
|
|
});
|
|
|
|
afterEach(() => {
|
|
cleanupTempDir(tempDir);
|
|
});
|
|
|
|
it('should index a TypeScript file', async () => {
|
|
// Create test file
|
|
const srcDir = path.join(tempDir, 'src');
|
|
fs.mkdirSync(srcDir);
|
|
fs.writeFileSync(
|
|
path.join(srcDir, 'utils.ts'),
|
|
`
|
|
export function add(a: number, b: number): number {
|
|
return a + b;
|
|
}
|
|
|
|
export function multiply(a: number, b: number): number {
|
|
return a * b;
|
|
}
|
|
`
|
|
);
|
|
|
|
// Initialize and index
|
|
const cg = CodeGraph.initSync(tempDir);
|
|
const result = await cg.indexAll();
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(result.filesIndexed).toBe(1);
|
|
expect(result.nodesCreated).toBeGreaterThanOrEqual(2);
|
|
|
|
// Check nodes were stored
|
|
const nodes = cg.getNodesInFile('src/utils.ts');
|
|
expect(nodes.length).toBeGreaterThanOrEqual(2);
|
|
|
|
const addFunc = nodes.find((n) => n.name === 'add');
|
|
expect(addFunc).toBeDefined();
|
|
expect(addFunc?.kind).toBe('function');
|
|
|
|
cg.close();
|
|
});
|
|
|
|
it('should index multiple files', async () => {
|
|
// Create test files
|
|
const srcDir = path.join(tempDir, 'src');
|
|
fs.mkdirSync(srcDir);
|
|
|
|
fs.writeFileSync(
|
|
path.join(srcDir, 'math.ts'),
|
|
`export function add(a: number, b: number) { return a + b; }`
|
|
);
|
|
|
|
fs.writeFileSync(
|
|
path.join(srcDir, 'string.ts'),
|
|
`export function capitalize(s: string) { return s.toUpperCase(); }`
|
|
);
|
|
|
|
// Initialize and index
|
|
const cg = CodeGraph.initSync(tempDir);
|
|
const result = await cg.indexAll();
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(result.filesIndexed).toBe(2);
|
|
|
|
const files = cg.getFiles();
|
|
expect(files.length).toBe(2);
|
|
|
|
cg.close();
|
|
});
|
|
|
|
it('should track file hashes for incremental updates', async () => {
|
|
// Create initial file
|
|
const srcDir = path.join(tempDir, 'src');
|
|
fs.mkdirSync(srcDir);
|
|
fs.writeFileSync(path.join(srcDir, 'main.ts'), `export const x = 1;`);
|
|
|
|
// Initialize and index
|
|
const cg = CodeGraph.initSync(tempDir);
|
|
await cg.indexAll();
|
|
|
|
// Check file is tracked
|
|
const file = cg.getFile('src/main.ts');
|
|
expect(file).toBeDefined();
|
|
expect(file?.contentHash).toBeDefined();
|
|
|
|
// Modify file
|
|
fs.writeFileSync(path.join(srcDir, 'main.ts'), `export const x = 2;`);
|
|
|
|
// Check for changes
|
|
const changes = cg.getChangedFiles();
|
|
expect(changes.modified).toContain('src/main.ts');
|
|
|
|
cg.close();
|
|
});
|
|
|
|
it('should sync and detect changes', async () => {
|
|
// Create initial file
|
|
const srcDir = path.join(tempDir, 'src');
|
|
fs.mkdirSync(srcDir);
|
|
fs.writeFileSync(
|
|
path.join(srcDir, 'main.ts'),
|
|
`export function original() { return 1; }`
|
|
);
|
|
|
|
// Initialize and index
|
|
const cg = CodeGraph.initSync(tempDir);
|
|
await cg.indexAll();
|
|
|
|
const initialNodes = cg.getNodesInFile('src/main.ts');
|
|
expect(initialNodes.some((n) => n.name === 'original')).toBe(true);
|
|
|
|
// Modify file
|
|
fs.writeFileSync(
|
|
path.join(srcDir, 'main.ts'),
|
|
`export function updated() { return 2; }`
|
|
);
|
|
|
|
// Sync
|
|
const syncResult = await cg.sync();
|
|
expect(syncResult.filesModified).toBe(1);
|
|
|
|
// Check nodes were updated
|
|
const updatedNodes = cg.getNodesInFile('src/main.ts');
|
|
expect(updatedNodes.some((n) => n.name === 'updated')).toBe(true);
|
|
expect(updatedNodes.some((n) => n.name === 'original')).toBe(false);
|
|
|
|
cg.close();
|
|
});
|
|
});
|