summaryrefslogtreecommitdiff
path: root/packages/core/src/tools/grep.test.ts
diff options
context:
space:
mode:
authorYuki Okita <[email protected]>2025-07-31 05:38:20 +0900
committerGitHub <[email protected]>2025-07-30 20:38:20 +0000
commitc1fe6889569610878c45216556fb99424b5bcba4 (patch)
treeb96f5f66bc00426fcd3e4b87402067342abbce12 /packages/core/src/tools/grep.test.ts
parent21965f986c8aa99da5a0f8e52ae823bb2f040d7a (diff)
feat: Multi-Directory Workspace Support (part1: add `--include-directories` option) (#4605)
Co-authored-by: Allen Hutchison <[email protected]>
Diffstat (limited to 'packages/core/src/tools/grep.test.ts')
-rw-r--r--packages/core/src/tools/grep.test.ts119
1 files changed, 114 insertions, 5 deletions
diff --git a/packages/core/src/tools/grep.test.ts b/packages/core/src/tools/grep.test.ts
index 01295083..aadc93ae 100644
--- a/packages/core/src/tools/grep.test.ts
+++ b/packages/core/src/tools/grep.test.ts
@@ -10,6 +10,7 @@ import path from 'path';
import fs from 'fs/promises';
import os from 'os';
import { Config } from '../config/config.js';
+import { createMockWorkspaceContext } from '../test-utils/mockWorkspaceContext.js';
// Mock the child_process module to control grep/git grep behavior
vi.mock('child_process', () => ({
@@ -33,6 +34,7 @@ describe('GrepTool', () => {
const mockConfig = {
getTargetDir: () => tempRootDir,
+ getWorkspaceContext: () => createMockWorkspaceContext(tempRootDir),
} as unknown as Config;
beforeEach(async () => {
@@ -120,7 +122,7 @@ describe('GrepTool', () => {
const params: GrepToolParams = { pattern: 'world' };
const result = await grepTool.execute(params, abortSignal);
expect(result.llmContent).toContain(
- 'Found 3 matches for pattern "world" in path "."',
+ 'Found 3 matches for pattern "world" in the workspace directory',
);
expect(result.llmContent).toContain('File: fileA.txt');
expect(result.llmContent).toContain('L1: hello world');
@@ -147,7 +149,7 @@ describe('GrepTool', () => {
const params: GrepToolParams = { pattern: 'hello', include: '*.js' };
const result = await grepTool.execute(params, abortSignal);
expect(result.llmContent).toContain(
- 'Found 1 match for pattern "hello" in path "." (filter: "*.js")',
+ 'Found 1 match for pattern "hello" in the workspace directory (filter: "*.js"):',
);
expect(result.llmContent).toContain('File: fileB.js');
expect(result.llmContent).toContain(
@@ -179,7 +181,7 @@ describe('GrepTool', () => {
const params: GrepToolParams = { pattern: 'nonexistentpattern' };
const result = await grepTool.execute(params, abortSignal);
expect(result.llmContent).toContain(
- 'No matches found for pattern "nonexistentpattern" in path "."',
+ 'No matches found for pattern "nonexistentpattern" in the workspace directory.',
);
expect(result.returnDisplay).toBe('No matches found');
});
@@ -188,7 +190,7 @@ describe('GrepTool', () => {
const params: GrepToolParams = { pattern: 'foo.*bar' }; // Matches 'const foo = "bar";'
const result = await grepTool.execute(params, abortSignal);
expect(result.llmContent).toContain(
- 'Found 1 match for pattern "foo.*bar" in path "."',
+ 'Found 1 match for pattern "foo.*bar" in the workspace directory:',
);
expect(result.llmContent).toContain('File: fileB.js');
expect(result.llmContent).toContain('L1: const foo = "bar";');
@@ -198,7 +200,7 @@ describe('GrepTool', () => {
const params: GrepToolParams = { pattern: 'HELLO' };
const result = await grepTool.execute(params, abortSignal);
expect(result.llmContent).toContain(
- 'Found 2 matches for pattern "HELLO" in path "."',
+ 'Found 2 matches for pattern "HELLO" in the workspace directory:',
);
expect(result.llmContent).toContain('File: fileA.txt');
expect(result.llmContent).toContain('L1: hello world');
@@ -220,6 +222,98 @@ describe('GrepTool', () => {
});
});
+ describe('multi-directory workspace', () => {
+ it('should search across all workspace directories when no path is specified', async () => {
+ // Create additional directory with test files
+ const secondDir = await fs.mkdtemp(
+ path.join(os.tmpdir(), 'grep-tool-second-'),
+ );
+ await fs.writeFile(
+ path.join(secondDir, 'other.txt'),
+ 'hello from second directory\nworld in second',
+ );
+ await fs.writeFile(
+ path.join(secondDir, 'another.js'),
+ 'function world() { return "test"; }',
+ );
+
+ // Create a mock config with multiple directories
+ const multiDirConfig = {
+ getTargetDir: () => tempRootDir,
+ getWorkspaceContext: () =>
+ createMockWorkspaceContext(tempRootDir, [secondDir]),
+ } as unknown as Config;
+
+ const multiDirGrepTool = new GrepTool(multiDirConfig);
+ const params: GrepToolParams = { pattern: 'world' };
+ const result = await multiDirGrepTool.execute(params, abortSignal);
+
+ // Should find matches in both directories
+ expect(result.llmContent).toContain(
+ 'Found 5 matches for pattern "world"',
+ );
+
+ // Matches from first directory
+ expect(result.llmContent).toContain('fileA.txt');
+ expect(result.llmContent).toContain('L1: hello world');
+ expect(result.llmContent).toContain('L2: second line with world');
+ expect(result.llmContent).toContain('fileC.txt');
+ expect(result.llmContent).toContain('L1: another world in sub dir');
+
+ // Matches from second directory (with directory name prefix)
+ const secondDirName = path.basename(secondDir);
+ expect(result.llmContent).toContain(
+ `File: ${path.join(secondDirName, 'other.txt')}`,
+ );
+ expect(result.llmContent).toContain('L2: world in second');
+ expect(result.llmContent).toContain(
+ `File: ${path.join(secondDirName, 'another.js')}`,
+ );
+ expect(result.llmContent).toContain('L1: function world()');
+
+ // Clean up
+ await fs.rm(secondDir, { recursive: true, force: true });
+ });
+
+ it('should search only specified path within workspace directories', async () => {
+ // Create additional directory
+ const secondDir = await fs.mkdtemp(
+ path.join(os.tmpdir(), 'grep-tool-second-'),
+ );
+ await fs.mkdir(path.join(secondDir, 'sub'));
+ await fs.writeFile(
+ path.join(secondDir, 'sub', 'test.txt'),
+ 'hello from second sub directory',
+ );
+
+ // Create a mock config with multiple directories
+ const multiDirConfig = {
+ getTargetDir: () => tempRootDir,
+ getWorkspaceContext: () =>
+ createMockWorkspaceContext(tempRootDir, [secondDir]),
+ } as unknown as Config;
+
+ const multiDirGrepTool = new GrepTool(multiDirConfig);
+
+ // Search only in the 'sub' directory of the first workspace
+ const params: GrepToolParams = { pattern: 'world', path: 'sub' };
+ const result = await multiDirGrepTool.execute(params, abortSignal);
+
+ // Should only find matches in the specified sub directory
+ expect(result.llmContent).toContain(
+ 'Found 1 match for pattern "world" in path "sub"',
+ );
+ expect(result.llmContent).toContain('File: fileC.txt');
+ expect(result.llmContent).toContain('L1: another world in sub dir');
+
+ // Should not contain matches from second directory
+ expect(result.llmContent).not.toContain('test.txt');
+
+ // Clean up
+ await fs.rm(secondDir, { recursive: true, force: true });
+ });
+ });
+
describe('getDescription', () => {
it('should generate correct description with pattern only', () => {
const params: GrepToolParams = { pattern: 'testPattern' };
@@ -246,6 +340,21 @@ describe('GrepTool', () => {
);
});
+ it('should indicate searching across all workspace directories when no path specified', () => {
+ // Create a mock config with multiple directories
+ const multiDirConfig = {
+ getTargetDir: () => tempRootDir,
+ getWorkspaceContext: () =>
+ createMockWorkspaceContext(tempRootDir, ['/another/dir']),
+ } as unknown as Config;
+
+ const multiDirGrepTool = new GrepTool(multiDirConfig);
+ const params: GrepToolParams = { pattern: 'testPattern' };
+ expect(multiDirGrepTool.getDescription(params)).toBe(
+ "'testPattern' across all workspace directories",
+ );
+ });
+
it('should generate correct description with pattern, include, and path', () => {
const params: GrepToolParams = {
pattern: 'testPattern',