-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsearch-snippets.test.ts
More file actions
220 lines (186 loc) · 5.51 KB
/
search-snippets.test.ts
File metadata and controls
220 lines (186 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { promises as fs } from 'fs';
import os from 'os';
import path from 'path';
import { CodebaseIndexer } from '../src/core/indexer.js';
import { rmWithRetries } from './test-helpers.js';
describe('Search Snippets with Scope Headers', () => {
let tempRoot: string | null = null;
beforeEach(async () => {
vi.resetModules();
tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'search-snippets-test-'));
process.env.CODEBASE_ROOT = tempRoot;
process.argv[2] = tempRoot;
const srcDir = path.join(tempRoot, 'src');
await fs.mkdir(srcDir, { recursive: true });
// File with class and methods
await fs.writeFile(
path.join(srcDir, 'auth.service.ts'),
`
export class AuthService {
/**
* Get authentication token
*/
getToken(): string {
const token = localStorage.getItem('auth_token');
return token || '';
}
/**
* Refresh token from server
*/
refreshToken(): Promise<string> {
return fetch('/api/refresh')
.then(res => res.json())
.then(data => data.token);
}
/**
* Validate token format
*/
validateToken(token: string): boolean {
return token && token.length > 0;
}
/**
* Clear stored token
*/
clearToken(): void {
localStorage.removeItem('auth_token');
}
}
`
);
// File with standalone functions
await fs.writeFile(
path.join(srcDir, 'utils.ts'),
`
export function formatDate(date: Date): string {
return date.toISOString();
}
export function parseJSON(str: string): any {
return JSON.parse(str);
}
export class DataProcessor {
process(data: any): void {
console.log(data);
}
}
`
);
// File with no meaningful structure
await fs.writeFile(
path.join(srcDir, 'constants.ts'),
`
export const API_URL = 'https://api.example.com';
export const TIMEOUT = 5000;
export const VERSION = '1.0.0';
`
);
// Index the project
const indexer = new CodebaseIndexer({
rootPath: tempRoot,
config: { skipEmbedding: true }
});
await indexer.index();
}, 30000);
afterEach(async () => {
if (tempRoot) {
await rmWithRetries(tempRoot);
tempRoot = null;
}
delete process.env.CODEBASE_ROOT;
}, 30000);
it('returns snippets when includeSnippets=true', async () => {
if (!tempRoot) throw new Error('tempRoot not initialized');
const { server } = await import('../src/index.js');
const handler = (server as any)._requestHandlers.get('tools/call');
const response = await handler({
jsonrpc: '2.0',
id: 1,
method: 'tools/call',
params: {
name: 'search_codebase',
arguments: {
query: 'getToken',
includeSnippets: true
}
}
});
const content = response.content[0];
const parsed = JSON.parse(content.text);
expect(parsed.results).toBeDefined();
expect(parsed.results.length).toBeGreaterThan(0);
const withSnippets = parsed.results.filter((r: any) => r.snippet);
expect(withSnippets.length).toBeGreaterThan(0);
});
it('scope header is a comment line starting with //', async () => {
if (!tempRoot) throw new Error('tempRoot not initialized');
const { server } = await import('../src/index.js');
const handler = (server as any)._requestHandlers.get('tools/call');
const response = await handler({
jsonrpc: '2.0',
id: 1,
method: 'tools/call',
params: {
name: 'search_codebase',
arguments: {
query: 'getToken',
includeSnippets: true
}
}
});
const content = response.content[0];
const parsed = JSON.parse(content.text);
const withSnippet = parsed.results.find((r: any) => r.snippet);
if (withSnippet && withSnippet.snippet) {
const firstLine = withSnippet.snippet.split('\n')[0];
// Scope header should be a comment line
expect(firstLine).toMatch(/^\/\//);
}
});
it('does not include snippet when includeSnippets=false', async () => {
if (!tempRoot) throw new Error('tempRoot not initialized');
const { server } = await import('../src/index.js');
const handler = (server as any)._requestHandlers.get('tools/call');
const response = await handler({
jsonrpc: '2.0',
id: 1,
method: 'tools/call',
params: {
name: 'search_codebase',
arguments: {
query: 'getToken',
includeSnippets: false
}
}
});
const content = response.content[0];
const parsed = JSON.parse(content.text);
// No results should have snippet field
parsed.results.forEach((r: any) => {
expect(r.snippet).toBeUndefined();
});
});
it('snippet is a string starting with code or comment', async () => {
if (!tempRoot) throw new Error('tempRoot not initialized');
const { server } = await import('../src/index.js');
const handler = (server as any)._requestHandlers.get('tools/call');
const response = await handler({
jsonrpc: '2.0',
id: 1,
method: 'tools/call',
params: {
name: 'search_codebase',
arguments: {
query: 'formatDate',
includeSnippets: true
}
}
});
const content = response.content[0];
const parsed = JSON.parse(content.text);
const withSnippet = parsed.results.find((r: any) => r.snippet);
if (withSnippet && withSnippet.snippet) {
expect(typeof withSnippet.snippet).toBe('string');
expect(withSnippet.snippet.length).toBeGreaterThan(0);
}
});
});