1
0

sync.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * Sync Module Tests
  3. *
  4. * Tests for sync functionality (incremental updates).
  5. * Note: Git hooks functionality has been removed in favor of codegraph's
  6. * Claude Code hooks integration.
  7. */
  8. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  9. import * as fs from 'fs';
  10. import * as path from 'path';
  11. import * as os from 'os';
  12. import CodeGraph from '../src/index';
  13. describe('Sync Module', () => {
  14. describe('Sync Functionality', () => {
  15. let testDir: string;
  16. let cg: CodeGraph;
  17. beforeEach(async () => {
  18. testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-sync-func-'));
  19. // Create initial source files
  20. const srcDir = path.join(testDir, 'src');
  21. fs.mkdirSync(srcDir);
  22. fs.writeFileSync(
  23. path.join(srcDir, 'index.ts'),
  24. `export function hello() { return 'world'; }`
  25. );
  26. // Initialize and index
  27. cg = CodeGraph.initSync(testDir, {
  28. config: {
  29. include: ['**/*.ts'],
  30. exclude: [],
  31. },
  32. });
  33. await cg.indexAll();
  34. });
  35. afterEach(() => {
  36. if (cg) {
  37. cg.destroy();
  38. }
  39. if (fs.existsSync(testDir)) {
  40. fs.rmSync(testDir, { recursive: true, force: true });
  41. }
  42. });
  43. describe('getChangedFiles()', () => {
  44. it('should detect added files', () => {
  45. // Add a new file
  46. fs.writeFileSync(
  47. path.join(testDir, 'src', 'new.ts'),
  48. `export function newFunc() { return 42; }`
  49. );
  50. const changes = cg.getChangedFiles();
  51. expect(changes.added).toContain('src/new.ts');
  52. expect(changes.modified).toHaveLength(0);
  53. expect(changes.removed).toHaveLength(0);
  54. });
  55. it('should detect modified files', () => {
  56. // Modify existing file
  57. fs.writeFileSync(
  58. path.join(testDir, 'src', 'index.ts'),
  59. `export function hello() { return 'modified'; }`
  60. );
  61. const changes = cg.getChangedFiles();
  62. expect(changes.added).toHaveLength(0);
  63. expect(changes.modified).toContain('src/index.ts');
  64. expect(changes.removed).toHaveLength(0);
  65. });
  66. it('should detect removed files', () => {
  67. // Remove file
  68. fs.unlinkSync(path.join(testDir, 'src', 'index.ts'));
  69. const changes = cg.getChangedFiles();
  70. expect(changes.added).toHaveLength(0);
  71. expect(changes.modified).toHaveLength(0);
  72. expect(changes.removed).toContain('src/index.ts');
  73. });
  74. });
  75. describe('sync()', () => {
  76. it('should reindex added files', async () => {
  77. // Add a new file
  78. fs.writeFileSync(
  79. path.join(testDir, 'src', 'new.ts'),
  80. `export function newFunc() { return 42; }`
  81. );
  82. const result = await cg.sync();
  83. expect(result.filesAdded).toBe(1);
  84. expect(result.filesModified).toBe(0);
  85. expect(result.filesRemoved).toBe(0);
  86. // Verify new function is in the graph
  87. const nodes = cg.searchNodes('newFunc');
  88. expect(nodes.length).toBeGreaterThan(0);
  89. });
  90. it('should reindex modified files', async () => {
  91. // Modify existing file
  92. fs.writeFileSync(
  93. path.join(testDir, 'src', 'index.ts'),
  94. `export function goodbye() { return 'farewell'; }`
  95. );
  96. const result = await cg.sync();
  97. expect(result.filesModified).toBe(1);
  98. // Verify new function is in the graph
  99. const nodes = cg.searchNodes('goodbye');
  100. expect(nodes.length).toBeGreaterThan(0);
  101. // Verify old function is gone
  102. const oldNodes = cg.searchNodes('hello');
  103. expect(oldNodes.length).toBe(0);
  104. });
  105. it('should remove nodes from deleted files', async () => {
  106. // Remove file
  107. fs.unlinkSync(path.join(testDir, 'src', 'index.ts'));
  108. const result = await cg.sync();
  109. expect(result.filesRemoved).toBe(1);
  110. // Verify function is gone
  111. const nodes = cg.searchNodes('hello');
  112. expect(nodes.length).toBe(0);
  113. });
  114. it('should report no changes when nothing changed', async () => {
  115. const result = await cg.sync();
  116. expect(result.filesAdded).toBe(0);
  117. expect(result.filesModified).toBe(0);
  118. expect(result.filesRemoved).toBe(0);
  119. expect(result.filesChecked).toBeGreaterThan(0);
  120. });
  121. });
  122. });
  123. });