-
Notifications
You must be signed in to change notification settings - Fork 201
/
editor-context.test.ts
203 lines (167 loc) · 6.14 KB
/
editor-context.test.ts
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
import { afterEach, describe, expect, it, vi } from 'vitest'
import * as vscode from 'vscode'
import { URI } from 'vscode-uri'
import {
type ContextItem,
type ContextItemFile,
type Editor,
MAX_CURRENT_FILE_TOKENS,
ignores,
testFileUri,
uriBasename,
} from '@sourcegraph/cody-shared'
import { CHARS_PER_TOKEN } from '@sourcegraph/cody-shared/src/prompt/constants'
import { fillInContextItemContent, filterLargeFiles, getFileContextFiles } from './editor-context'
vi.mock('lodash/throttle', () => ({ default: vi.fn(fn => fn) }))
afterEach(() => {
vi.clearAllMocks()
})
describe('getFileContextFiles', () => {
function setFiles(relativePaths: string[]) {
vscode.workspace.findFiles = vi
.fn()
.mockResolvedValueOnce(relativePaths.map(f => testFileUri(f)))
for (const rp of relativePaths) {
vscode.workspace.fs.stat = vi.fn().mockResolvedValue({
size: rp.startsWith('large-file.') ? 10000000 : 10,
type: rp === 'symlink' ? vscode.FileType.SymbolicLink : vscode.FileType.File,
uri: testFileUri(rp),
isDirectory: () => false,
isFile: () => true,
isSymbolicLink: () => false,
toString: vi.fn().mockReturnValue(rp),
})
}
}
async function runSearch(query: string, maxResults: number): Promise<(string | undefined)[]> {
const results = await getFileContextFiles(query, maxResults)
return results.map(f => uriBasename(f.uri))
}
it('fuzzy filters results', async () => {
setFiles(['foo/bar/baz/file.go', 'foo/bar/File/go-has-parts', 'foo/bar/baz/FileWontMatch.ts'])
expect(await runSearch('filego', 5)).toMatchInlineSnapshot(`
[
"go-has-parts",
"file.go",
]
`)
expect(vscode.workspace.findFiles).toBeCalledTimes(1)
})
it('ranks bin/ low if "bin" has not been typed', async () => {
setFiles(['bin/main.dart', 'abcdefghijbklmn.dart'])
expect(await runSearch('bi', 5)).toMatchInlineSnapshot(`
[
"abcdefghijbklmn.dart",
"main.dart",
]
`)
expect(vscode.workspace.findFiles).toBeCalledTimes(1)
})
it('ranks bin/ normally if "bin" has been typed', async () => {
setFiles(['bin/main.dart', 'abcdefghijbklmn.dart'])
expect(await runSearch('bin', 5)).toMatchInlineSnapshot(`
[
"main.dart",
"abcdefghijbklmn.dart",
]
`)
expect(vscode.workspace.findFiles).toBeCalledTimes(1)
})
it('do not return non-file (e.g. symlinks) result', async () => {
setFiles(['symlink'])
expect(await runSearch('symlink', 5)).toMatchInlineSnapshot(`
[]
`)
expect(vscode.workspace.findFiles).toBeCalledTimes(1)
})
it('do not return file larger than 1MB', async () => {
setFiles(['large-file.go'])
expect(await runSearch('large', 5)).toMatchInlineSnapshot(`
[]
`)
expect(vscode.workspace.findFiles).toBeCalledTimes(1)
})
it('filters out ignored files', async () => {
ignores.setActiveState(true)
ignores.setIgnoreFiles(testFileUri(''), [
{ uri: testFileUri('.cody/ignore'), content: '*.ignore' },
])
setFiles(['foo.txt', 'foo.ignore'])
// Match the .txt but not the .ignore
expect(await runSearch('foo', 5)).toMatchInlineSnapshot(`
[
"foo.txt",
]
`)
expect(vscode.workspace.findFiles).toBeCalledTimes(1)
})
})
describe('filterLargeFiles', () => {
it('filters out files larger than 1MB', async () => {
const largeFile: ContextItemFile = {
uri: vscode.Uri.file('/large-file.txt'),
type: 'file',
}
vscode.workspace.fs.stat = vi.fn().mockResolvedValueOnce({
size: 1000001,
type: vscode.FileType.File,
} as vscode.FileStat)
const filtered = await filterLargeFiles([largeFile])
expect(filtered).toEqual([])
})
it('filters out non-text files', async () => {
const binaryFile: ContextItemFile = {
uri: vscode.Uri.file('/binary.bin'),
type: 'file',
}
vscode.workspace.fs.stat = vi.fn().mockResolvedValueOnce({
size: 100,
type: vscode.FileType.SymbolicLink,
} as vscode.FileStat)
const filtered = await filterLargeFiles([binaryFile])
expect(filtered).toEqual([])
})
it('sets isTooLarge for files exceeding token limit but under 1MB', async () => {
const largeTextFile: ContextItemFile = {
uri: vscode.Uri.file('/large-text.txt'),
type: 'file',
}
const oneByteOverTokenLimit = MAX_CURRENT_FILE_TOKENS * CHARS_PER_TOKEN + 1
vscode.workspace.fs.stat = vi.fn().mockResolvedValueOnce({
size: oneByteOverTokenLimit,
type: vscode.FileType.File,
} as vscode.FileStat)
const filtered = await filterLargeFiles([largeTextFile])
expect(filtered[0]).toEqual<ContextItem>({
type: 'file',
uri: largeTextFile.uri,
isTooLarge: true,
})
})
})
describe('fillInContextItemContent', () => {
it('omits files that could not be read', async () => {
// Fixes https://github.com/sourcegraph/cody/issues/2390.
const mockEditor: Partial<Editor> = {
getTextEditorContentForFile(uri) {
if (uri.path === '/a.txt') {
return Promise.resolve('a')
}
throw new Error('error')
},
}
const contextItems = await fillInContextItemContent(mockEditor as Editor, [
{
type: 'file',
uri: URI.parse('file:///a.txt'),
},
{
type: 'file',
uri: URI.parse('file:///error.txt'),
},
])
expect(contextItems).toEqual<ContextItem[]>([
{ type: 'file', uri: URI.parse('file:///a.txt'), content: 'a' },
])
})
})