foundation.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /**
  2. * Foundation Tests
  3. *
  4. * Tests for the CodeGraph foundation layer.
  5. */
  6. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  7. import * as fs from 'fs';
  8. import * as path from 'path';
  9. import * as os from 'os';
  10. import { CodeGraph } from '../src';
  11. import { DEFAULT_CONFIG, Node, Edge } from '../src/types';
  12. import { loadConfig, saveConfig } from '../src/config';
  13. import { isInitialized, getCodeGraphDir, validateDirectory } from '../src/directory';
  14. import { DatabaseConnection, getDatabasePath } from '../src/db';
  15. // Create a temporary directory for each test
  16. function createTempDir(): string {
  17. return fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-test-'));
  18. }
  19. // Clean up temporary directory
  20. function cleanupTempDir(dir: string): void {
  21. if (fs.existsSync(dir)) {
  22. fs.rmSync(dir, { recursive: true, force: true });
  23. }
  24. }
  25. describe('CodeGraph Foundation', () => {
  26. let tempDir: string;
  27. beforeEach(() => {
  28. tempDir = createTempDir();
  29. });
  30. afterEach(() => {
  31. cleanupTempDir(tempDir);
  32. });
  33. describe('Initialization', () => {
  34. it('should initialize a new project', () => {
  35. const cg = CodeGraph.initSync(tempDir);
  36. expect(CodeGraph.isInitialized(tempDir)).toBe(true);
  37. expect(fs.existsSync(getCodeGraphDir(tempDir))).toBe(true);
  38. expect(fs.existsSync(getDatabasePath(tempDir))).toBe(true);
  39. cg.close();
  40. });
  41. it('should create .gitignore in .CodeGraph directory', () => {
  42. const cg = CodeGraph.initSync(tempDir);
  43. const gitignorePath = path.join(getCodeGraphDir(tempDir), '.gitignore');
  44. expect(fs.existsSync(gitignorePath)).toBe(true);
  45. const content = fs.readFileSync(gitignorePath, 'utf-8');
  46. expect(content).toContain('*.db');
  47. cg.close();
  48. });
  49. it('should create config.json with defaults', () => {
  50. const cg = CodeGraph.initSync(tempDir);
  51. const configPath = path.join(getCodeGraphDir(tempDir), 'config.json');
  52. expect(fs.existsSync(configPath)).toBe(true);
  53. const config = cg.getConfig();
  54. expect(config.version).toBe(DEFAULT_CONFIG.version);
  55. expect(config.include).toEqual(DEFAULT_CONFIG.include);
  56. expect(config.exclude).toEqual(DEFAULT_CONFIG.exclude);
  57. cg.close();
  58. });
  59. it('should throw if already initialized', () => {
  60. const cg = CodeGraph.initSync(tempDir);
  61. cg.close();
  62. expect(() => CodeGraph.initSync(tempDir)).toThrow(/already initialized/i);
  63. });
  64. it('should accept custom config options', () => {
  65. const cg = CodeGraph.initSync(tempDir, {
  66. config: {
  67. maxFileSize: 500000,
  68. extractDocstrings: false,
  69. },
  70. });
  71. const config = cg.getConfig();
  72. expect(config.maxFileSize).toBe(500000);
  73. expect(config.extractDocstrings).toBe(false);
  74. cg.close();
  75. });
  76. });
  77. describe('Opening Projects', () => {
  78. it('should open an existing project', () => {
  79. // First initialize
  80. const cg1 = CodeGraph.initSync(tempDir);
  81. cg1.close();
  82. // Then open
  83. const cg2 = CodeGraph.openSync(tempDir);
  84. expect(cg2.getProjectRoot()).toBe(path.resolve(tempDir));
  85. cg2.close();
  86. });
  87. it('should throw if not initialized', () => {
  88. expect(() => CodeGraph.openSync(tempDir)).toThrow(/not initialized/i);
  89. });
  90. it('should preserve configuration across open/close', () => {
  91. const cg1 = CodeGraph.initSync(tempDir, {
  92. config: { maxFileSize: 123456 },
  93. });
  94. cg1.close();
  95. const cg2 = CodeGraph.openSync(tempDir);
  96. expect(cg2.getConfig().maxFileSize).toBe(123456);
  97. cg2.close();
  98. });
  99. });
  100. describe('Static Methods', () => {
  101. it('isInitialized should return false for new directory', () => {
  102. expect(CodeGraph.isInitialized(tempDir)).toBe(false);
  103. });
  104. it('isInitialized should return true after init', () => {
  105. const cg = CodeGraph.initSync(tempDir);
  106. expect(CodeGraph.isInitialized(tempDir)).toBe(true);
  107. cg.close();
  108. });
  109. });
  110. describe('Database', () => {
  111. it('should create database with correct schema', () => {
  112. const cg = CodeGraph.initSync(tempDir);
  113. // Check that we can get stats (requires tables to exist)
  114. const stats = cg.getStats();
  115. expect(stats.nodeCount).toBe(0);
  116. expect(stats.edgeCount).toBe(0);
  117. expect(stats.fileCount).toBe(0);
  118. cg.close();
  119. });
  120. it('should return correct database size', () => {
  121. const cg = CodeGraph.initSync(tempDir);
  122. const stats = cg.getStats();
  123. // Database should have some size (at least the schema)
  124. expect(stats.dbSizeBytes).toBeGreaterThan(0);
  125. cg.close();
  126. });
  127. it('should support optimize operation', () => {
  128. const cg = CodeGraph.initSync(tempDir);
  129. // Should not throw
  130. expect(() => cg.optimize()).not.toThrow();
  131. cg.close();
  132. });
  133. it('should support clear operation', () => {
  134. const cg = CodeGraph.initSync(tempDir);
  135. // Should not throw
  136. expect(() => cg.clear()).not.toThrow();
  137. const stats = cg.getStats();
  138. expect(stats.nodeCount).toBe(0);
  139. cg.close();
  140. });
  141. });
  142. describe('Configuration', () => {
  143. it('should load and merge config with defaults', () => {
  144. const cg = CodeGraph.initSync(tempDir);
  145. cg.close();
  146. const config = loadConfig(tempDir);
  147. expect(config.version).toBe(DEFAULT_CONFIG.version);
  148. expect(config.rootDir).toBe(path.resolve(tempDir));
  149. });
  150. it('should update configuration', () => {
  151. const cg = CodeGraph.initSync(tempDir);
  152. cg.updateConfig({ maxFileSize: 999999 });
  153. expect(cg.getConfig().maxFileSize).toBe(999999);
  154. cg.close();
  155. // Verify persistence
  156. const config = loadConfig(tempDir);
  157. expect(config.maxFileSize).toBe(999999);
  158. });
  159. });
  160. describe('Directory Management', () => {
  161. it('should validate directory structure', () => {
  162. const cg = CodeGraph.initSync(tempDir);
  163. cg.close();
  164. const validation = validateDirectory(tempDir);
  165. expect(validation.valid).toBe(true);
  166. expect(validation.errors).toHaveLength(0);
  167. });
  168. it('should detect invalid directory', () => {
  169. const validation = validateDirectory(tempDir);
  170. expect(validation.valid).toBe(false);
  171. expect(validation.errors.length).toBeGreaterThan(0);
  172. });
  173. });
  174. describe('Uninitialize', () => {
  175. it('should remove .CodeGraph directory', () => {
  176. const cg = CodeGraph.initSync(tempDir);
  177. cg.uninitialize();
  178. expect(fs.existsSync(getCodeGraphDir(tempDir))).toBe(false);
  179. expect(CodeGraph.isInitialized(tempDir)).toBe(false);
  180. });
  181. });
  182. describe('Close/Destroy', () => {
  183. it('should close database but keep .CodeGraph directory', () => {
  184. const cg = CodeGraph.initSync(tempDir);
  185. cg.destroy(); // destroy is alias for close
  186. expect(fs.existsSync(getCodeGraphDir(tempDir))).toBe(true);
  187. expect(CodeGraph.isInitialized(tempDir)).toBe(true);
  188. });
  189. });
  190. describe('Graph Query Methods', () => {
  191. it('should throw "Node not found" for non-existent nodes', () => {
  192. const cg = CodeGraph.initSync(tempDir);
  193. // getContext throws for non-existent nodes
  194. expect(() => cg.getContext('non-existent')).toThrow(/not found/i);
  195. cg.close();
  196. });
  197. it('should return empty results for non-existent nodes', () => {
  198. const cg = CodeGraph.initSync(tempDir);
  199. // These methods return empty results instead of throwing
  200. const traverseResult = cg.traverse('non-existent');
  201. expect(traverseResult.nodes.size).toBe(0);
  202. const callGraph = cg.getCallGraph('non-existent');
  203. expect(callGraph.nodes.size).toBe(0);
  204. const typeHierarchy = cg.getTypeHierarchy('non-existent');
  205. expect(typeHierarchy.nodes.size).toBe(0);
  206. const usages = cg.findUsages('non-existent');
  207. expect(usages.length).toBe(0);
  208. cg.close();
  209. });
  210. it('should require embedding initialization for semantic search', async () => {
  211. const cg = CodeGraph.initSync(tempDir);
  212. // Semantic search requires embeddings to be initialized first
  213. await expect(cg.semanticSearch('test')).rejects.toThrow(/not initialized/i);
  214. await expect(cg.findSimilar('test')).rejects.toThrow(/not initialized/i);
  215. // Check embedding status
  216. expect(cg.isEmbeddingsInitialized()).toBe(false);
  217. cg.close();
  218. });
  219. });
  220. });
  221. describe('Database Connection', () => {
  222. let tempDir: string;
  223. beforeEach(() => {
  224. tempDir = createTempDir();
  225. });
  226. afterEach(() => {
  227. cleanupTempDir(tempDir);
  228. });
  229. it('should initialize new database', () => {
  230. const dbPath = path.join(tempDir, 'test.db');
  231. const db = DatabaseConnection.initialize(dbPath);
  232. expect(db.isOpen()).toBe(true);
  233. expect(fs.existsSync(dbPath)).toBe(true);
  234. db.close();
  235. });
  236. it('should get schema version', () => {
  237. const dbPath = path.join(tempDir, 'test.db');
  238. const db = DatabaseConnection.initialize(dbPath);
  239. const version = db.getSchemaVersion();
  240. expect(version).not.toBeNull();
  241. expect(version?.version).toBe(2);
  242. db.close();
  243. });
  244. it('should support transactions', () => {
  245. const dbPath = path.join(tempDir, 'test.db');
  246. const db = DatabaseConnection.initialize(dbPath);
  247. const result = db.transaction(() => {
  248. return 42;
  249. });
  250. expect(result).toBe(42);
  251. db.close();
  252. });
  253. it('should throw when opening non-existent database', () => {
  254. const dbPath = path.join(tempDir, 'nonexistent.db');
  255. expect(() => DatabaseConnection.open(dbPath)).toThrow(/not found/i);
  256. });
  257. });
  258. describe('Query Builder', () => {
  259. let tempDir: string;
  260. let cg: CodeGraph;
  261. beforeEach(() => {
  262. tempDir = createTempDir();
  263. cg = CodeGraph.initSync(tempDir);
  264. });
  265. afterEach(() => {
  266. cg.close();
  267. cleanupTempDir(tempDir);
  268. });
  269. it('should return null for non-existent node', () => {
  270. const node = cg.getNode('nonexistent');
  271. expect(node).toBeNull();
  272. });
  273. it('should return empty array for nodes in non-existent file', () => {
  274. const nodes = cg.getNodesInFile('nonexistent.ts');
  275. expect(nodes).toEqual([]);
  276. });
  277. it('should return empty array for edges from non-existent node', () => {
  278. const edges = cg.getOutgoingEdges('nonexistent');
  279. expect(edges).toEqual([]);
  280. });
  281. it('should return null for non-existent file', () => {
  282. const file = cg.getFile('nonexistent.ts');
  283. expect(file).toBeNull();
  284. });
  285. it('should return empty array for files when none tracked', () => {
  286. const files = cg.getFiles();
  287. expect(files).toEqual([]);
  288. });
  289. });