| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- /**
- * Foundation Tests
- *
- * Tests for the CodeGraph foundation layer.
- */
- 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 { Node, Edge } from '../src/types';
- import { isInitialized, getCodeGraphDir, validateDirectory } from '../src/directory';
- import { DatabaseConnection, getDatabasePath } from '../src/db';
- // 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('CodeGraph Foundation', () => {
- let tempDir: string;
- beforeEach(() => {
- tempDir = createTempDir();
- });
- afterEach(() => {
- cleanupTempDir(tempDir);
- });
- describe('Initialization', () => {
- it('should initialize a new project', () => {
- const cg = CodeGraph.initSync(tempDir);
- expect(CodeGraph.isInitialized(tempDir)).toBe(true);
- expect(fs.existsSync(getCodeGraphDir(tempDir))).toBe(true);
- expect(fs.existsSync(getDatabasePath(tempDir))).toBe(true);
- cg.close();
- });
- it('should create .gitignore in .CodeGraph directory', () => {
- const cg = CodeGraph.initSync(tempDir);
- const gitignorePath = path.join(getCodeGraphDir(tempDir), '.gitignore');
- expect(fs.existsSync(gitignorePath)).toBe(true);
- const content = fs.readFileSync(gitignorePath, 'utf-8');
- expect(content).toContain('*.db');
- cg.close();
- });
- it('should throw if already initialized', () => {
- const cg = CodeGraph.initSync(tempDir);
- cg.close();
- expect(() => CodeGraph.initSync(tempDir)).toThrow(/already initialized/i);
- });
- });
- describe('Opening Projects', () => {
- it('should open an existing project', () => {
- // First initialize
- const cg1 = CodeGraph.initSync(tempDir);
- cg1.close();
- // Then open
- const cg2 = CodeGraph.openSync(tempDir);
- expect(cg2.getProjectRoot()).toBe(path.resolve(tempDir));
- cg2.close();
- });
- it('should throw if not initialized', () => {
- expect(() => CodeGraph.openSync(tempDir)).toThrow(/not initialized/i);
- });
- });
- describe('Static Methods', () => {
- it('isInitialized should return false for new directory', () => {
- expect(CodeGraph.isInitialized(tempDir)).toBe(false);
- });
- it('isInitialized should return true after init', () => {
- const cg = CodeGraph.initSync(tempDir);
- expect(CodeGraph.isInitialized(tempDir)).toBe(true);
- cg.close();
- });
- });
- describe('Database', () => {
- it('should create database with correct schema', () => {
- const cg = CodeGraph.initSync(tempDir);
- // Check that we can get stats (requires tables to exist)
- const stats = cg.getStats();
- expect(stats.nodeCount).toBe(0);
- expect(stats.edgeCount).toBe(0);
- expect(stats.fileCount).toBe(0);
- cg.close();
- });
- it('should return correct database size', () => {
- const cg = CodeGraph.initSync(tempDir);
- const stats = cg.getStats();
- // Database should have some size (at least the schema)
- expect(stats.dbSizeBytes).toBeGreaterThan(0);
- cg.close();
- });
- it('should support optimize operation', () => {
- const cg = CodeGraph.initSync(tempDir);
- // Should not throw
- expect(() => cg.optimize()).not.toThrow();
- cg.close();
- });
- it('should support clear operation', () => {
- const cg = CodeGraph.initSync(tempDir);
- // Should not throw
- expect(() => cg.clear()).not.toThrow();
- const stats = cg.getStats();
- expect(stats.nodeCount).toBe(0);
- cg.close();
- });
- });
- describe('Directory Management', () => {
- it('should validate directory structure', () => {
- const cg = CodeGraph.initSync(tempDir);
- cg.close();
- const validation = validateDirectory(tempDir);
- expect(validation.valid).toBe(true);
- expect(validation.errors).toHaveLength(0);
- });
- it('should detect invalid directory', () => {
- const validation = validateDirectory(tempDir);
- expect(validation.valid).toBe(false);
- expect(validation.errors.length).toBeGreaterThan(0);
- });
- });
- describe('Uninitialize', () => {
- it('should remove .CodeGraph directory', () => {
- const cg = CodeGraph.initSync(tempDir);
- cg.uninitialize();
- expect(fs.existsSync(getCodeGraphDir(tempDir))).toBe(false);
- expect(CodeGraph.isInitialized(tempDir)).toBe(false);
- });
- });
- describe('Close/Destroy', () => {
- it('should close database but keep .CodeGraph directory', () => {
- const cg = CodeGraph.initSync(tempDir);
- cg.destroy(); // destroy is alias for close
- expect(fs.existsSync(getCodeGraphDir(tempDir))).toBe(true);
- expect(CodeGraph.isInitialized(tempDir)).toBe(true);
- });
- });
- describe('Graph Query Methods', () => {
- it('should throw "Node not found" for non-existent nodes', () => {
- const cg = CodeGraph.initSync(tempDir);
- // getContext throws for non-existent nodes
- expect(() => cg.getContext('non-existent')).toThrow(/not found/i);
- cg.close();
- });
- it('should return empty results for non-existent nodes', () => {
- const cg = CodeGraph.initSync(tempDir);
- // These methods return empty results instead of throwing
- const traverseResult = cg.traverse('non-existent');
- expect(traverseResult.nodes.size).toBe(0);
- const callGraph = cg.getCallGraph('non-existent');
- expect(callGraph.nodes.size).toBe(0);
- const typeHierarchy = cg.getTypeHierarchy('non-existent');
- expect(typeHierarchy.nodes.size).toBe(0);
- const usages = cg.findUsages('non-existent');
- expect(usages.length).toBe(0);
- cg.close();
- });
- });
- });
- describe('Database Connection', () => {
- let tempDir: string;
- beforeEach(() => {
- tempDir = createTempDir();
- });
- afterEach(() => {
- cleanupTempDir(tempDir);
- });
- it('should initialize new database', () => {
- const dbPath = path.join(tempDir, 'test.db');
- const db = DatabaseConnection.initialize(dbPath);
- expect(db.isOpen()).toBe(true);
- expect(fs.existsSync(dbPath)).toBe(true);
- db.close();
- });
- it('should get schema version', () => {
- const dbPath = path.join(tempDir, 'test.db');
- const db = DatabaseConnection.initialize(dbPath);
- const version = db.getSchemaVersion();
- expect(version).not.toBeNull();
- expect(version?.version).toBe(4);
- db.close();
- });
- it('should support transactions', () => {
- const dbPath = path.join(tempDir, 'test.db');
- const db = DatabaseConnection.initialize(dbPath);
- const result = db.transaction(() => {
- return 42;
- });
- expect(result).toBe(42);
- db.close();
- });
- it('should throw when opening non-existent database', () => {
- const dbPath = path.join(tempDir, 'nonexistent.db');
- expect(() => DatabaseConnection.open(dbPath)).toThrow(/not found/i);
- });
- });
- describe('Query Builder', () => {
- let tempDir: string;
- let cg: CodeGraph;
- beforeEach(() => {
- tempDir = createTempDir();
- cg = CodeGraph.initSync(tempDir);
- });
- afterEach(() => {
- cg.close();
- cleanupTempDir(tempDir);
- });
- it('should return null for non-existent node', () => {
- const node = cg.getNode('nonexistent');
- expect(node).toBeNull();
- });
- it('should return empty array for nodes in non-existent file', () => {
- const nodes = cg.getNodesInFile('nonexistent.ts');
- expect(nodes).toEqual([]);
- });
- it('should return empty array for edges from non-existent node', () => {
- const edges = cg.getOutgoingEdges('nonexistent');
- expect(edges).toEqual([]);
- });
- it('should return null for non-existent file', () => {
- const file = cg.getFile('nonexistent.ts');
- expect(file).toBeNull();
- });
- it('should return empty array for files when none tracked', () => {
- const files = cg.getFiles();
- expect(files).toEqual([]);
- });
- });
|