mirror of
https://github.com/QwenLM/qwen-code.git
synced 2026-05-11 04:50:11 +00:00
Some checks are pending
Qwen Code CI / Lint (push) Waiting to run
Qwen Code CI / Test (push) Blocked by required conditions
Qwen Code CI / Test-1 (push) Blocked by required conditions
Qwen Code CI / Test-2 (push) Blocked by required conditions
Qwen Code CI / Test-3 (push) Blocked by required conditions
Qwen Code CI / Test-4 (push) Blocked by required conditions
Qwen Code CI / Test-5 (push) Blocked by required conditions
Qwen Code CI / Test-6 (push) Blocked by required conditions
Qwen Code CI / Test-7 (push) Blocked by required conditions
Qwen Code CI / Test-8 (push) Blocked by required conditions
Qwen Code CI / Post Coverage Comment (push) Blocked by required conditions
Qwen Code CI / CodeQL (push) Waiting to run
E2E Tests / E2E Test (Linux) - sandbox:docker (push) Waiting to run
E2E Tests / E2E Test (Linux) - sandbox:none (push) Waiting to run
E2E Tests / E2E Test (Linux) - sandbox:docker-1 (push) Waiting to run
E2E Tests / E2E Test (Linux) - sandbox:none-1 (push) Waiting to run
E2E Tests / E2E Test (Linux) - sandbox:docker-2 (push) Waiting to run
E2E Tests / E2E Test (Linux) - sandbox:none-2 (push) Waiting to run
E2E Tests / E2E Test - macOS (push) Waiting to run
727 lines
26 KiB
TypeScript
727 lines
26 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2025 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
import {
|
|
describe,
|
|
it,
|
|
expect,
|
|
beforeEach,
|
|
afterEach,
|
|
vi,
|
|
type Mock,
|
|
} from 'vitest';
|
|
import type { RipGrepToolParams } from './ripGrep.js';
|
|
import { RipGrepTool } from './ripGrep.js';
|
|
import path from 'node:path';
|
|
import fs from 'node:fs/promises';
|
|
import os, { EOL } from 'node:os';
|
|
import type { Config } from '../config/config.js';
|
|
import { createMockWorkspaceContext } from '../test-utils/mockWorkspaceContext.js';
|
|
import { spawn } from 'node:child_process';
|
|
import { runRipgrep } from '../utils/ripgrepUtils.js';
|
|
import { DEFAULT_FILE_FILTERING_OPTIONS } from '../config/constants.js';
|
|
|
|
// Mock ripgrepUtils
|
|
vi.mock('../utils/ripgrepUtils.js', () => ({
|
|
runRipgrep: vi.fn(),
|
|
}));
|
|
|
|
// Mock child_process for ripgrep calls
|
|
vi.mock('child_process', () => ({
|
|
spawn: vi.fn(),
|
|
}));
|
|
|
|
const mockSpawn = vi.mocked(spawn);
|
|
|
|
describe('RipGrepTool', () => {
|
|
let tempRootDir: string;
|
|
let grepTool: RipGrepTool;
|
|
let fileExclusionsMock: { getGlobExcludes: () => string[] };
|
|
const abortSignal = new AbortController().signal;
|
|
|
|
const mockConfig = {
|
|
getTargetDir: () => tempRootDir,
|
|
getWorkspaceContext: () => createMockWorkspaceContext(tempRootDir),
|
|
getWorkingDir: () => tempRootDir,
|
|
getDebugMode: () => false,
|
|
getUseBuiltinRipgrep: () => true,
|
|
getTruncateToolOutputThreshold: () => 25000,
|
|
getTruncateToolOutputLines: () => 1000,
|
|
} as unknown as Config;
|
|
|
|
beforeEach(async () => {
|
|
vi.clearAllMocks();
|
|
mockSpawn.mockReset();
|
|
tempRootDir = await fs.mkdtemp(path.join(os.tmpdir(), 'grep-tool-root-'));
|
|
fileExclusionsMock = {
|
|
getGlobExcludes: vi.fn().mockReturnValue([]),
|
|
};
|
|
Object.assign(mockConfig, {
|
|
getFileExclusions: () => fileExclusionsMock,
|
|
getFileFilteringOptions: () => DEFAULT_FILE_FILTERING_OPTIONS,
|
|
});
|
|
grepTool = new RipGrepTool(mockConfig);
|
|
|
|
// Create some test files and directories
|
|
await fs.writeFile(
|
|
path.join(tempRootDir, 'fileA.txt'),
|
|
'hello world\nsecond line with world',
|
|
);
|
|
await fs.writeFile(
|
|
path.join(tempRootDir, 'fileB.js'),
|
|
'const foo = "bar";\nfunction baz() { return "hello"; }',
|
|
);
|
|
await fs.mkdir(path.join(tempRootDir, 'sub'));
|
|
await fs.writeFile(
|
|
path.join(tempRootDir, 'sub', 'fileC.txt'),
|
|
'another world in sub dir',
|
|
);
|
|
await fs.writeFile(
|
|
path.join(tempRootDir, 'sub', 'fileD.md'),
|
|
'# Markdown file\nThis is a test.',
|
|
);
|
|
});
|
|
|
|
afterEach(async () => {
|
|
await fs.rm(tempRootDir, { recursive: true, force: true });
|
|
});
|
|
|
|
describe('validateToolParams', () => {
|
|
it('should return null for valid params (pattern only)', () => {
|
|
const params: RipGrepToolParams = { pattern: 'hello' };
|
|
expect(grepTool.validateToolParams(params)).toBeNull();
|
|
});
|
|
|
|
it('should return null for valid params (pattern and path)', () => {
|
|
const params: RipGrepToolParams = { pattern: 'hello', path: '.' };
|
|
expect(grepTool.validateToolParams(params)).toBeNull();
|
|
});
|
|
|
|
it('should return null for valid params (pattern, path, and glob)', () => {
|
|
const params: RipGrepToolParams = {
|
|
pattern: 'hello',
|
|
path: '.',
|
|
glob: '*.txt',
|
|
};
|
|
expect(grepTool.validateToolParams(params)).toBeNull();
|
|
});
|
|
|
|
it('should return error if pattern is missing', () => {
|
|
const params = { path: '.' } as unknown as RipGrepToolParams;
|
|
expect(grepTool.validateToolParams(params)).toBe(
|
|
`params must have required property 'pattern'`,
|
|
);
|
|
});
|
|
|
|
it('should surface an error for invalid regex pattern', () => {
|
|
const params: RipGrepToolParams = { pattern: '[[' };
|
|
expect(grepTool.validateToolParams(params)).toContain(
|
|
'Invalid regular expression pattern: [[',
|
|
);
|
|
});
|
|
|
|
it('should return error if path does not exist', () => {
|
|
const params: RipGrepToolParams = {
|
|
pattern: 'hello',
|
|
path: 'nonexistent',
|
|
};
|
|
// Check for the core error message, as the full path might vary
|
|
expect(grepTool.validateToolParams(params)).toContain(
|
|
'Path does not exist:',
|
|
);
|
|
expect(grepTool.validateToolParams(params)).toContain('nonexistent');
|
|
});
|
|
|
|
it('should allow path to be a file', () => {
|
|
const filePath = path.join(tempRootDir, 'fileA.txt');
|
|
const params: RipGrepToolParams = { pattern: 'hello', path: filePath };
|
|
expect(grepTool.validateToolParams(params)).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('execute', () => {
|
|
it('should find matches for a simple pattern in all files', async () => {
|
|
(runRipgrep as Mock).mockResolvedValue({
|
|
stdout: `fileA.txt:1:hello world${EOL}fileA.txt:2:second line with world${EOL}sub/fileC.txt:1:another world in sub dir${EOL}`,
|
|
truncated: false,
|
|
error: undefined,
|
|
});
|
|
|
|
const params: RipGrepToolParams = { pattern: 'world' };
|
|
const invocation = grepTool.build(params);
|
|
const result = await invocation.execute(abortSignal);
|
|
expect(result.llmContent).toContain(
|
|
'Found 3 matches for pattern "world" in the workspace directory',
|
|
);
|
|
expect(result.llmContent).toContain('fileA.txt:1:hello world');
|
|
expect(result.llmContent).toContain('fileA.txt:2:second line with world');
|
|
expect(result.llmContent).toContain(
|
|
'sub/fileC.txt:1:another world in sub dir',
|
|
);
|
|
expect(result.returnDisplay).toBe('Found 3 matches');
|
|
});
|
|
|
|
it('should find matches in a specific path', async () => {
|
|
// Setup specific mock for this test - searching in 'sub' should only return matches from that directory
|
|
(runRipgrep as Mock).mockResolvedValue({
|
|
stdout: `fileC.txt:1:another world in sub dir${EOL}`,
|
|
truncated: false,
|
|
error: undefined,
|
|
});
|
|
|
|
const params: RipGrepToolParams = { pattern: 'world', path: 'sub' };
|
|
const invocation = grepTool.build(params);
|
|
const result = await invocation.execute(abortSignal);
|
|
expect(result.llmContent).toContain(
|
|
'Found 1 match for pattern "world" in path "sub"',
|
|
);
|
|
expect(result.llmContent).toContain(
|
|
'fileC.txt:1:another world in sub dir',
|
|
);
|
|
expect(result.returnDisplay).toBe('Found 1 match');
|
|
});
|
|
|
|
it('should use target directory when path is not provided', async () => {
|
|
(runRipgrep as Mock).mockResolvedValue({
|
|
stdout: `fileA.txt:1:hello world${EOL}`,
|
|
truncated: false,
|
|
error: undefined,
|
|
});
|
|
|
|
const params: RipGrepToolParams = { pattern: 'world' };
|
|
const invocation = grepTool.build(params);
|
|
const result = await invocation.execute(abortSignal);
|
|
expect(result.llmContent).toContain(
|
|
'Found 1 match for pattern "world" in the workspace directory',
|
|
);
|
|
});
|
|
|
|
it('should find matches with a glob filter', async () => {
|
|
// Setup specific mock for this test
|
|
(runRipgrep as Mock).mockResolvedValue({
|
|
stdout: `fileB.js:2:function baz() { return "hello"; }${EOL}`,
|
|
truncated: false,
|
|
error: undefined,
|
|
});
|
|
|
|
const params: RipGrepToolParams = { pattern: 'hello', glob: '*.js' };
|
|
const invocation = grepTool.build(params);
|
|
const result = await invocation.execute(abortSignal);
|
|
expect(result.llmContent).toContain(
|
|
'Found 1 match for pattern "hello" in the workspace directory (filter: "*.js"):',
|
|
);
|
|
expect(result.llmContent).toContain(
|
|
'fileB.js:2:function baz() { return "hello"; }',
|
|
);
|
|
expect(result.returnDisplay).toBe('Found 1 match');
|
|
});
|
|
|
|
it('should find matches with a glob filter and path', async () => {
|
|
await fs.writeFile(
|
|
path.join(tempRootDir, 'sub', 'another.js'),
|
|
'const greeting = "hello";',
|
|
);
|
|
|
|
// Setup specific mock for this test - searching for 'hello' in 'sub' with '*.js' filter
|
|
(runRipgrep as Mock).mockResolvedValue({
|
|
stdout: `another.js:1:const greeting = "hello";${EOL}`,
|
|
truncated: false,
|
|
error: undefined,
|
|
});
|
|
|
|
const params: RipGrepToolParams = {
|
|
pattern: 'hello',
|
|
path: 'sub',
|
|
glob: '*.js',
|
|
};
|
|
const invocation = grepTool.build(params);
|
|
const result = await invocation.execute(abortSignal);
|
|
expect(result.llmContent).toContain(
|
|
'Found 1 match for pattern "hello" in path "sub" (filter: "*.js")',
|
|
);
|
|
expect(result.llmContent).toContain(
|
|
'another.js:1:const greeting = "hello";',
|
|
);
|
|
expect(result.returnDisplay).toBe('Found 1 match');
|
|
});
|
|
|
|
it('should pass .qwenignore to ripgrep when respected', async () => {
|
|
await fs.writeFile(
|
|
path.join(tempRootDir, '.qwenignore'),
|
|
'ignored.txt\n',
|
|
);
|
|
(runRipgrep as Mock).mockResolvedValue({
|
|
stdout: '',
|
|
truncated: false,
|
|
error: undefined,
|
|
});
|
|
|
|
const params: RipGrepToolParams = { pattern: 'secret' };
|
|
const invocation = grepTool.build(params);
|
|
const result = await invocation.execute(abortSignal);
|
|
expect(result.llmContent).toContain(
|
|
'No matches found for pattern "secret" in the workspace directory.',
|
|
);
|
|
expect(result.returnDisplay).toBe('No matches found');
|
|
});
|
|
|
|
it('should include .qwenignore matches when disabled in config', async () => {
|
|
await fs.writeFile(path.join(tempRootDir, '.qwenignore'), 'kept.txt\n');
|
|
await fs.writeFile(path.join(tempRootDir, 'kept.txt'), 'keep me');
|
|
Object.assign(mockConfig, {
|
|
getFileFilteringOptions: () => ({
|
|
respectGitIgnore: true,
|
|
respectQwenIgnore: false,
|
|
}),
|
|
});
|
|
|
|
(runRipgrep as Mock).mockResolvedValue({
|
|
stdout: `kept.txt:1:keep me${EOL}`,
|
|
truncated: false,
|
|
error: undefined,
|
|
});
|
|
|
|
const params: RipGrepToolParams = { pattern: 'keep' };
|
|
const invocation = grepTool.build(params);
|
|
const result = await invocation.execute(abortSignal);
|
|
expect(result.llmContent).toContain(
|
|
'Found 1 match for pattern "keep" in the workspace directory:',
|
|
);
|
|
expect(result.llmContent).toContain('kept.txt:1:keep me');
|
|
expect(result.returnDisplay).toBe('Found 1 match');
|
|
});
|
|
|
|
it('should disable gitignore when configured', async () => {
|
|
Object.assign(mockConfig, {
|
|
getFileFilteringOptions: () => ({
|
|
respectGitIgnore: false,
|
|
respectQwenIgnore: true,
|
|
}),
|
|
});
|
|
|
|
(runRipgrep as Mock).mockResolvedValue({
|
|
stdout: '',
|
|
truncated: false,
|
|
error: undefined,
|
|
});
|
|
|
|
const params: RipGrepToolParams = { pattern: 'ignored' };
|
|
const invocation = grepTool.build(params);
|
|
await invocation.execute(abortSignal);
|
|
});
|
|
|
|
it('should truncate llm content when exceeding maximum length', async () => {
|
|
const longMatch = 'fileA.txt:1:' + 'a'.repeat(30_000);
|
|
|
|
(runRipgrep as Mock).mockResolvedValue({
|
|
stdout: `${longMatch}${EOL}`,
|
|
truncated: false,
|
|
error: undefined,
|
|
});
|
|
|
|
const params: RipGrepToolParams = { pattern: 'a+' };
|
|
const invocation = grepTool.build(params);
|
|
const result = await invocation.execute(abortSignal);
|
|
|
|
expect(String(result.llmContent).length).toBeLessThanOrEqual(26_000);
|
|
expect(result.llmContent).toMatch(/\[\d+ lines? truncated\] \.\.\./);
|
|
expect(result.returnDisplay).toContain('truncated');
|
|
});
|
|
|
|
it('should return "No matches found" when pattern does not exist', async () => {
|
|
// Setup specific mock for no matches
|
|
(runRipgrep as Mock).mockResolvedValue({
|
|
stdout: '',
|
|
truncated: false,
|
|
error: undefined,
|
|
});
|
|
|
|
const params: RipGrepToolParams = { pattern: 'nonexistentpattern' };
|
|
const invocation = grepTool.build(params);
|
|
const result = await invocation.execute(abortSignal);
|
|
expect(result.llmContent).toContain(
|
|
'No matches found for pattern "nonexistentpattern" in the workspace directory.',
|
|
);
|
|
expect(result.returnDisplay).toBe('No matches found');
|
|
});
|
|
|
|
it('should throw validation error for invalid regex pattern', async () => {
|
|
const params: RipGrepToolParams = { pattern: '[[' };
|
|
expect(() => grepTool.build(params)).toThrow(
|
|
'Invalid regular expression pattern: [[',
|
|
);
|
|
});
|
|
|
|
it('should handle regex special characters correctly', async () => {
|
|
// Setup specific mock for this test - regex pattern 'foo.*bar' should match 'const foo = "bar";'
|
|
(runRipgrep as Mock).mockResolvedValue({
|
|
stdout: `fileB.js:1:const foo = "bar";${EOL}`,
|
|
truncated: false,
|
|
error: undefined,
|
|
});
|
|
|
|
const params: RipGrepToolParams = { pattern: 'foo.*bar' }; // Matches 'const foo = "bar";'
|
|
const invocation = grepTool.build(params);
|
|
const result = await invocation.execute(abortSignal);
|
|
expect(result.llmContent).toContain(
|
|
'Found 1 match for pattern "foo.*bar" in the workspace directory:',
|
|
);
|
|
expect(result.llmContent).toContain('fileB.js:1:const foo = "bar";');
|
|
});
|
|
|
|
it('should be case-insensitive by default (JS fallback)', async () => {
|
|
// Setup specific mock for this test - case insensitive search for 'HELLO'
|
|
(runRipgrep as Mock).mockResolvedValue({
|
|
stdout: `fileA.txt:1:hello world${EOL}fileB.js:2:function baz() { return "hello"; }${EOL}`,
|
|
truncated: false,
|
|
error: undefined,
|
|
});
|
|
|
|
const params: RipGrepToolParams = { pattern: 'HELLO' };
|
|
const invocation = grepTool.build(params);
|
|
const result = await invocation.execute(abortSignal);
|
|
expect(result.llmContent).toContain(
|
|
'Found 2 matches for pattern "HELLO" in the workspace directory:',
|
|
);
|
|
expect(result.llmContent).toContain('fileA.txt:1:hello world');
|
|
expect(result.llmContent).toContain(
|
|
'fileB.js:2:function baz() { return "hello"; }',
|
|
);
|
|
});
|
|
|
|
it('should throw an error if params are invalid', async () => {
|
|
const params = { path: '.' } as unknown as RipGrepToolParams; // Invalid: pattern missing
|
|
expect(() => grepTool.build(params)).toThrow(
|
|
/params must have required property 'pattern'/,
|
|
);
|
|
});
|
|
|
|
it('should search within a single file when path is a file', async () => {
|
|
(runRipgrep as Mock).mockResolvedValue({
|
|
stdout: `fileA.txt:1:hello world${EOL}fileA.txt:2:second line with world${EOL}`,
|
|
truncated: false,
|
|
error: undefined,
|
|
});
|
|
|
|
const params: RipGrepToolParams = {
|
|
pattern: 'world',
|
|
path: path.join(tempRootDir, 'fileA.txt'),
|
|
};
|
|
const invocation = grepTool.build(params);
|
|
const result = await invocation.execute(abortSignal);
|
|
expect(result.llmContent).toContain('Found 2 matches');
|
|
expect(result.llmContent).toContain('fileA.txt:1:hello world');
|
|
expect(result.llmContent).toContain('fileA.txt:2:second line with world');
|
|
expect(result.returnDisplay).toBe('Found 2 matches');
|
|
});
|
|
|
|
it('should throw an error if ripgrep is not available', async () => {
|
|
(runRipgrep as Mock).mockResolvedValue({
|
|
stdout: '',
|
|
truncated: false,
|
|
error: new Error('ripgrep binary not found.'),
|
|
});
|
|
|
|
const params: RipGrepToolParams = { pattern: 'world' };
|
|
const invocation = grepTool.build(params);
|
|
|
|
expect(await invocation.execute(abortSignal)).toStrictEqual({
|
|
llmContent:
|
|
'Error during grep search operation: ripgrep binary not found.',
|
|
returnDisplay: 'Error: ripgrep binary not found.',
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('abort signal handling', () => {
|
|
it('should handle AbortSignal during search', async () => {
|
|
const controller = new AbortController();
|
|
const params: RipGrepToolParams = { pattern: 'world' };
|
|
const invocation = grepTool.build(params);
|
|
|
|
controller.abort();
|
|
|
|
const result = await invocation.execute(controller.signal);
|
|
expect(result).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe('error handling and edge cases', () => {
|
|
it('should handle workspace boundary violations', () => {
|
|
const params: RipGrepToolParams = { pattern: 'test', path: '../outside' };
|
|
expect(() => grepTool.build(params)).toThrow(
|
|
/Path is not within workspace/,
|
|
);
|
|
});
|
|
|
|
it('should handle empty directories gracefully', async () => {
|
|
const emptyDir = path.join(tempRootDir, 'empty');
|
|
await fs.mkdir(emptyDir);
|
|
|
|
// Setup specific mock for this test - searching in empty directory should return no matches
|
|
(runRipgrep as Mock).mockResolvedValue({
|
|
stdout: '',
|
|
truncated: false,
|
|
error: undefined,
|
|
});
|
|
|
|
const params: RipGrepToolParams = { pattern: 'test', path: 'empty' };
|
|
const invocation = grepTool.build(params);
|
|
const result = await invocation.execute(abortSignal);
|
|
|
|
expect(result.llmContent).toContain('No matches found');
|
|
expect(result.returnDisplay).toBe('No matches found');
|
|
});
|
|
|
|
it('should handle empty files correctly', async () => {
|
|
await fs.writeFile(path.join(tempRootDir, 'empty.txt'), '');
|
|
|
|
// Setup specific mock for this test - searching for anything in empty files should return no matches
|
|
(runRipgrep as Mock).mockResolvedValue({
|
|
stdout: '',
|
|
truncated: false,
|
|
error: undefined,
|
|
});
|
|
|
|
const params: RipGrepToolParams = { pattern: 'anything' };
|
|
const invocation = grepTool.build(params);
|
|
const result = await invocation.execute(abortSignal);
|
|
|
|
expect(result.llmContent).toContain('No matches found');
|
|
});
|
|
|
|
it('should handle special characters in file names', async () => {
|
|
const specialFileName = 'file with spaces & symbols!.txt';
|
|
await fs.writeFile(
|
|
path.join(tempRootDir, specialFileName),
|
|
'hello world with special chars',
|
|
);
|
|
|
|
// Setup specific mock for this test - searching for 'world' should find the file with special characters
|
|
(runRipgrep as Mock).mockResolvedValue({
|
|
stdout: `file with spaces & symbols!.txt:1:hello world with special chars${EOL}`,
|
|
truncated: false,
|
|
error: undefined,
|
|
});
|
|
|
|
const params: RipGrepToolParams = { pattern: 'world' };
|
|
const invocation = grepTool.build(params);
|
|
const result = await invocation.execute(abortSignal);
|
|
|
|
expect(result.llmContent).toContain(specialFileName);
|
|
expect(result.llmContent).toContain('hello world with special chars');
|
|
});
|
|
|
|
it('should handle deeply nested directories', async () => {
|
|
const deepPath = path.join(tempRootDir, 'a', 'b', 'c', 'd', 'e');
|
|
await fs.mkdir(deepPath, { recursive: true });
|
|
await fs.writeFile(
|
|
path.join(deepPath, 'deep.txt'),
|
|
'content in deep directory',
|
|
);
|
|
|
|
// Setup specific mock for this test - searching for 'deep' should find the deeply nested file
|
|
(runRipgrep as Mock).mockResolvedValue({
|
|
stdout: `a/b/c/d/e/deep.txt:1:content in deep directory${EOL}`,
|
|
truncated: false,
|
|
error: undefined,
|
|
});
|
|
|
|
const params: RipGrepToolParams = { pattern: 'deep' };
|
|
const invocation = grepTool.build(params);
|
|
const result = await invocation.execute(abortSignal);
|
|
|
|
expect(result.llmContent).toContain('deep.txt');
|
|
expect(result.llmContent).toContain('content in deep directory');
|
|
});
|
|
});
|
|
|
|
describe('regex pattern validation', () => {
|
|
it('should handle complex regex patterns', async () => {
|
|
await fs.writeFile(
|
|
path.join(tempRootDir, 'code.js'),
|
|
'function getName() { return "test"; }\nconst getValue = () => "value";',
|
|
);
|
|
|
|
// Setup specific mock for this test - regex pattern should match function declarations
|
|
(runRipgrep as Mock).mockResolvedValue({
|
|
stdout: `code.js:1:function getName() { return "test"; }${EOL}`,
|
|
truncated: false,
|
|
error: undefined,
|
|
});
|
|
|
|
const params: RipGrepToolParams = { pattern: 'function\\s+\\w+\\s*\\(' };
|
|
const invocation = grepTool.build(params);
|
|
const result = await invocation.execute(abortSignal);
|
|
|
|
expect(result.llmContent).toContain('function getName()');
|
|
expect(result.llmContent).not.toContain('const getValue');
|
|
});
|
|
|
|
it('should handle case sensitivity correctly in JS fallback', async () => {
|
|
await fs.writeFile(
|
|
path.join(tempRootDir, 'case.txt'),
|
|
'Hello World\nhello world\nHELLO WORLD',
|
|
);
|
|
|
|
// Setup specific mock for this test - case insensitive search should match all variants
|
|
(runRipgrep as Mock).mockResolvedValue({
|
|
stdout: `case.txt:1:Hello World${EOL}case.txt:2:hello world${EOL}case.txt:3:HELLO WORLD${EOL}`,
|
|
truncated: false,
|
|
error: undefined,
|
|
});
|
|
|
|
const params: RipGrepToolParams = { pattern: 'hello' };
|
|
const invocation = grepTool.build(params);
|
|
const result = await invocation.execute(abortSignal);
|
|
|
|
expect(result.llmContent).toContain('Hello World');
|
|
expect(result.llmContent).toContain('hello world');
|
|
expect(result.llmContent).toContain('HELLO WORLD');
|
|
});
|
|
|
|
it('should handle escaped regex special characters', async () => {
|
|
await fs.writeFile(
|
|
path.join(tempRootDir, 'special.txt'),
|
|
'Price: $19.99\nRegex: [a-z]+ pattern\nEmail: test@example.com',
|
|
);
|
|
|
|
// Setup specific mock for this test - escaped regex pattern should match price format
|
|
(runRipgrep as Mock).mockResolvedValue({
|
|
stdout: `special.txt:1:Price: $19.99${EOL}`,
|
|
truncated: false,
|
|
error: undefined,
|
|
});
|
|
|
|
const params: RipGrepToolParams = { pattern: '\\$\\d+\\.\\d+' };
|
|
const invocation = grepTool.build(params);
|
|
const result = await invocation.execute(abortSignal);
|
|
|
|
expect(result.llmContent).toContain('Price: $19.99');
|
|
expect(result.llmContent).not.toContain('Email: test@example.com');
|
|
});
|
|
});
|
|
|
|
describe('glob pattern filtering', () => {
|
|
it('should handle multiple file extensions in glob pattern', async () => {
|
|
await fs.writeFile(
|
|
path.join(tempRootDir, 'test.ts'),
|
|
'typescript content',
|
|
);
|
|
await fs.writeFile(path.join(tempRootDir, 'test.tsx'), 'tsx content');
|
|
await fs.writeFile(
|
|
path.join(tempRootDir, 'test.js'),
|
|
'javascript content',
|
|
);
|
|
await fs.writeFile(path.join(tempRootDir, 'test.txt'), 'text content');
|
|
|
|
// Setup specific mock for this test - glob pattern should filter to only ts/tsx files
|
|
(runRipgrep as Mock).mockResolvedValue({
|
|
stdout: `test.ts:1:typescript content${EOL}test.tsx:1:tsx content${EOL}`,
|
|
truncated: false,
|
|
error: undefined,
|
|
});
|
|
|
|
const params: RipGrepToolParams = {
|
|
pattern: 'content',
|
|
glob: '*.{ts,tsx}',
|
|
};
|
|
const invocation = grepTool.build(params);
|
|
const result = await invocation.execute(abortSignal);
|
|
|
|
expect(result.llmContent).toContain('test.ts');
|
|
expect(result.llmContent).toContain('test.tsx');
|
|
expect(result.llmContent).not.toContain('test.js');
|
|
expect(result.llmContent).not.toContain('test.txt');
|
|
});
|
|
|
|
it('should handle directory patterns in glob', async () => {
|
|
await fs.mkdir(path.join(tempRootDir, 'src'), { recursive: true });
|
|
await fs.writeFile(
|
|
path.join(tempRootDir, 'src', 'main.ts'),
|
|
'source code',
|
|
);
|
|
await fs.writeFile(path.join(tempRootDir, 'other.ts'), 'other code');
|
|
|
|
// Setup specific mock for this test - glob pattern should filter to only src/** files
|
|
(runRipgrep as Mock).mockResolvedValue({
|
|
stdout: `src/main.ts:1:source code${EOL}`,
|
|
truncated: false,
|
|
error: undefined,
|
|
});
|
|
|
|
const params: RipGrepToolParams = {
|
|
pattern: 'code',
|
|
glob: 'src/**',
|
|
};
|
|
const invocation = grepTool.build(params);
|
|
const result = await invocation.execute(abortSignal);
|
|
|
|
expect(result.llmContent).toContain('main.ts');
|
|
expect(result.llmContent).not.toContain('other.ts');
|
|
});
|
|
});
|
|
|
|
describe('getDescription', () => {
|
|
it('should generate correct description with pattern only', () => {
|
|
const params: RipGrepToolParams = { pattern: 'testPattern' };
|
|
const invocation = grepTool.build(params);
|
|
expect(invocation.getDescription()).toBe("'testPattern'");
|
|
});
|
|
|
|
it('should generate correct description with pattern and glob', () => {
|
|
const params: RipGrepToolParams = {
|
|
pattern: 'testPattern',
|
|
glob: '*.ts',
|
|
};
|
|
const invocation = grepTool.build(params);
|
|
expect(invocation.getDescription()).toBe(
|
|
"'testPattern' (filter: '*.ts')",
|
|
);
|
|
});
|
|
|
|
it('should generate correct description with pattern and path', async () => {
|
|
const dirPath = path.join(tempRootDir, 'src', 'app');
|
|
await fs.mkdir(dirPath, { recursive: true });
|
|
const params: RipGrepToolParams = {
|
|
pattern: 'testPattern',
|
|
path: path.join('src', 'app'),
|
|
};
|
|
const invocation = grepTool.build(params);
|
|
expect(invocation.getDescription()).toContain(
|
|
"'testPattern' in path 'src",
|
|
);
|
|
expect(invocation.getDescription()).toContain("app'");
|
|
});
|
|
|
|
it('should generate correct description with default search path', () => {
|
|
const params: RipGrepToolParams = { pattern: 'testPattern' };
|
|
const invocation = grepTool.build(params);
|
|
expect(invocation.getDescription()).toBe("'testPattern'");
|
|
});
|
|
|
|
it('should generate correct description with pattern, glob, and path', async () => {
|
|
const dirPath = path.join(tempRootDir, 'src', 'app');
|
|
await fs.mkdir(dirPath, { recursive: true });
|
|
const params: RipGrepToolParams = {
|
|
pattern: 'testPattern',
|
|
glob: '*.ts',
|
|
path: path.join('src', 'app'),
|
|
};
|
|
const invocation = grepTool.build(params);
|
|
expect(invocation.getDescription()).toContain(
|
|
"'testPattern' in path 'src",
|
|
);
|
|
expect(invocation.getDescription()).toContain("(filter: '*.ts')");
|
|
});
|
|
|
|
it('should use path when specified in description', () => {
|
|
const params: RipGrepToolParams = { pattern: 'testPattern', path: '.' };
|
|
const invocation = grepTool.build(params);
|
|
expect(invocation.getDescription()).toBe("'testPattern' in path '.'");
|
|
});
|
|
});
|
|
});
|