Files
codegraph/__tests__/watch-policy.test.ts
T
f6772dac7c feat: zero-config indexing driven by .gitignore (#283) (#285)
Remove .codegraph/config.json and the entire config surface. CodeGraph now
indexes every file whose extension maps to a supported language and respects
.gitignore everywhere — git repos via git itself, non-git projects via the
`ignore` library (root + nested .gitignore files, the same way git does).

- Remove CodeGraphConfig/DEFAULT_CONFIG, src/config.ts, and the public config
  API (the `config` option on init, getConfig/updateConfig/getConfigPath).
- Derive the source-file allowlist from EXTENSION_MAP (isSourceFile); maxFileSize
  is now a constant. Drop the .codegraphignore marker.
- Behavior change: committed, non-gitignored dirs (vendor/, a committed dist/)
  are now indexed — .gitignore is the single source of truth.

Earlier inert fields (languages, frameworks, extractDocstrings, trackCallSites,
customPatterns) and their dead helpers are removed as part of this.

Resolves #283.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-21 18:06:02 -05:00

83 lines
2.7 KiB
TypeScript

/**
* Watch Policy Tests
*
* Covers the decision of whether the live file watcher runs, including the
* WSL2 /mnt auto-detect and the env-var escape hatches (issue #199), plus
* that FileWatcher.start() honors the decision.
*/
import { describe, it, expect, afterEach, vi } from 'vitest';
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import { watchDisabledReason } from '../src/sync/watch-policy';
import { FileWatcher } from '../src/sync/watcher';
describe('watchDisabledReason', () => {
it('returns a reason when CODEGRAPH_NO_WATCH=1', () => {
const reason = watchDisabledReason('/home/me/project', {
env: { CODEGRAPH_NO_WATCH: '1' },
isWsl: false,
});
expect(reason).toBeTruthy();
expect(reason).toMatch(/CODEGRAPH_NO_WATCH/);
});
it('auto-disables on a WSL2 /mnt drive', () => {
const reason = watchDisabledReason('/mnt/d/code/project', { env: {}, isWsl: true });
expect(reason).toBeTruthy();
expect(reason).toMatch(/mnt/);
});
it('does NOT disable on a native WSL home path', () => {
expect(watchDisabledReason('/home/me/project', { env: {}, isWsl: true })).toBeNull();
});
it('does NOT disable on /mnt when not running under WSL', () => {
// A real Linux box may legitimately have a fast /mnt mount.
expect(watchDisabledReason('/mnt/d/code/project', { env: {}, isWsl: false })).toBeNull();
});
it('does NOT treat /mnt/wsl (fast Linux mount) as a Windows drive', () => {
expect(watchDisabledReason('/mnt/wsl/project', { env: {}, isWsl: true })).toBeNull();
});
it('CODEGRAPH_FORCE_WATCH=1 overrides WSL auto-detect', () => {
const reason = watchDisabledReason('/mnt/d/code/project', {
env: { CODEGRAPH_FORCE_WATCH: '1' },
isWsl: true,
});
expect(reason).toBeNull();
});
it('CODEGRAPH_NO_WATCH wins over CODEGRAPH_FORCE_WATCH', () => {
const reason = watchDisabledReason('/home/me/project', {
env: { CODEGRAPH_NO_WATCH: '1', CODEGRAPH_FORCE_WATCH: '1' },
isWsl: false,
});
expect(reason).toBeTruthy();
});
});
describe('FileWatcher honors the watch policy', () => {
let testDir: string;
afterEach(() => {
delete process.env.CODEGRAPH_NO_WATCH;
if (testDir && fs.existsSync(testDir)) {
fs.rmSync(testDir, { recursive: true, force: true });
}
});
it('does not start when CODEGRAPH_NO_WATCH=1', () => {
testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-nowatch-'));
process.env.CODEGRAPH_NO_WATCH = '1';
const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
const watcher = new FileWatcher(testDir, syncFn);
expect(watcher.start()).toBe(false);
expect(watcher.isActive()).toBe(false);
});
});