1
0

watch-policy.test.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * Watch Policy Tests
  3. *
  4. * Covers the decision of whether the live file watcher runs, including the
  5. * WSL2 /mnt auto-detect and the env-var escape hatches (issue #199), plus
  6. * that FileWatcher.start() honors the decision.
  7. */
  8. import { describe, it, expect, afterEach, vi } from 'vitest';
  9. import * as fs from 'fs';
  10. import * as path from 'path';
  11. import * as os from 'os';
  12. import { watchDisabledReason } from '../src/sync/watch-policy';
  13. import { FileWatcher } from '../src/sync/watcher';
  14. describe('watchDisabledReason', () => {
  15. it('returns a reason when CODEGRAPH_NO_WATCH=1', () => {
  16. const reason = watchDisabledReason('/home/me/project', {
  17. env: { CODEGRAPH_NO_WATCH: '1' },
  18. isWsl: false,
  19. });
  20. expect(reason).toBeTruthy();
  21. expect(reason).toMatch(/CODEGRAPH_NO_WATCH/);
  22. });
  23. it('auto-disables on a WSL2 /mnt drive', () => {
  24. const reason = watchDisabledReason('/mnt/d/code/project', { env: {}, isWsl: true });
  25. expect(reason).toBeTruthy();
  26. expect(reason).toMatch(/mnt/);
  27. });
  28. it('does NOT disable on a native WSL home path', () => {
  29. expect(watchDisabledReason('/home/me/project', { env: {}, isWsl: true })).toBeNull();
  30. });
  31. it('does NOT disable on /mnt when not running under WSL', () => {
  32. // A real Linux box may legitimately have a fast /mnt mount.
  33. expect(watchDisabledReason('/mnt/d/code/project', { env: {}, isWsl: false })).toBeNull();
  34. });
  35. it('does NOT treat /mnt/wsl (fast Linux mount) as a Windows drive', () => {
  36. expect(watchDisabledReason('/mnt/wsl/project', { env: {}, isWsl: true })).toBeNull();
  37. });
  38. it('CODEGRAPH_FORCE_WATCH=1 overrides WSL auto-detect', () => {
  39. const reason = watchDisabledReason('/mnt/d/code/project', {
  40. env: { CODEGRAPH_FORCE_WATCH: '1' },
  41. isWsl: true,
  42. });
  43. expect(reason).toBeNull();
  44. });
  45. it('CODEGRAPH_NO_WATCH wins over CODEGRAPH_FORCE_WATCH', () => {
  46. const reason = watchDisabledReason('/home/me/project', {
  47. env: { CODEGRAPH_NO_WATCH: '1', CODEGRAPH_FORCE_WATCH: '1' },
  48. isWsl: false,
  49. });
  50. expect(reason).toBeTruthy();
  51. });
  52. });
  53. describe('FileWatcher honors the watch policy', () => {
  54. let testDir: string;
  55. afterEach(() => {
  56. delete process.env.CODEGRAPH_NO_WATCH;
  57. if (testDir && fs.existsSync(testDir)) {
  58. fs.rmSync(testDir, { recursive: true, force: true });
  59. }
  60. });
  61. it('does not start when CODEGRAPH_NO_WATCH=1', () => {
  62. testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-nowatch-'));
  63. process.env.CODEGRAPH_NO_WATCH = '1';
  64. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  65. const watcher = new FileWatcher(testDir, syncFn);
  66. expect(watcher.start()).toBe(false);
  67. expect(watcher.isActive()).toBe(false);
  68. });
  69. });