| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- /**
- * Sync Module Tests
- *
- * Tests for sync functionality (incremental updates).
- * Note: Git hooks functionality has been removed in favor of codegraph's
- * Claude Code hooks integration.
- */
- 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/index';
- describe('Sync Module', () => {
- describe('Sync Functionality', () => {
- let testDir: string;
- let cg: CodeGraph;
- beforeEach(async () => {
- testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-sync-func-'));
- // Create initial source files
- const srcDir = path.join(testDir, 'src');
- fs.mkdirSync(srcDir);
- fs.writeFileSync(
- path.join(srcDir, 'index.ts'),
- `export function hello() { return 'world'; }`
- );
- // Initialize and index
- cg = CodeGraph.initSync(testDir, {
- config: {
- include: ['**/*.ts'],
- exclude: [],
- },
- });
- await cg.indexAll();
- });
- afterEach(() => {
- if (cg) {
- cg.destroy();
- }
- if (fs.existsSync(testDir)) {
- fs.rmSync(testDir, { recursive: true, force: true });
- }
- });
- describe('getChangedFiles()', () => {
- it('should detect added files', () => {
- // Add a new file
- fs.writeFileSync(
- path.join(testDir, 'src', 'new.ts'),
- `export function newFunc() { return 42; }`
- );
- const changes = cg.getChangedFiles();
- expect(changes.added).toContain('src/new.ts');
- expect(changes.modified).toHaveLength(0);
- expect(changes.removed).toHaveLength(0);
- });
- it('should detect modified files', () => {
- // Modify existing file
- fs.writeFileSync(
- path.join(testDir, 'src', 'index.ts'),
- `export function hello() { return 'modified'; }`
- );
- const changes = cg.getChangedFiles();
- expect(changes.added).toHaveLength(0);
- expect(changes.modified).toContain('src/index.ts');
- expect(changes.removed).toHaveLength(0);
- });
- it('should detect removed files', () => {
- // Remove file
- fs.unlinkSync(path.join(testDir, 'src', 'index.ts'));
- const changes = cg.getChangedFiles();
- expect(changes.added).toHaveLength(0);
- expect(changes.modified).toHaveLength(0);
- expect(changes.removed).toContain('src/index.ts');
- });
- });
- describe('sync()', () => {
- it('should reindex added files', async () => {
- // Add a new file
- fs.writeFileSync(
- path.join(testDir, 'src', 'new.ts'),
- `export function newFunc() { return 42; }`
- );
- const result = await cg.sync();
- expect(result.filesAdded).toBe(1);
- expect(result.filesModified).toBe(0);
- expect(result.filesRemoved).toBe(0);
- // Verify new function is in the graph
- const nodes = cg.searchNodes('newFunc');
- expect(nodes.length).toBeGreaterThan(0);
- });
- it('should reindex modified files', async () => {
- // Modify existing file
- fs.writeFileSync(
- path.join(testDir, 'src', 'index.ts'),
- `export function goodbye() { return 'farewell'; }`
- );
- const result = await cg.sync();
- expect(result.filesModified).toBe(1);
- // Verify new function is in the graph
- const nodes = cg.searchNodes('goodbye');
- expect(nodes.length).toBeGreaterThan(0);
- // Verify old function is gone
- const oldNodes = cg.searchNodes('hello');
- expect(oldNodes.length).toBe(0);
- });
- it('should remove nodes from deleted files', async () => {
- // Remove file
- fs.unlinkSync(path.join(testDir, 'src', 'index.ts'));
- const result = await cg.sync();
- expect(result.filesRemoved).toBe(1);
- // Verify function is gone
- const nodes = cg.searchNodes('hello');
- expect(nodes.length).toBe(0);
- });
- it('should report no changes when nothing changed', async () => {
- const result = await cg.sync();
- expect(result.filesAdded).toBe(0);
- expect(result.filesModified).toBe(0);
- expect(result.filesRemoved).toBe(0);
- expect(result.filesChecked).toBeGreaterThan(0);
- });
- });
- });
- });
|