Skip to content

Commit 7357616

Browse files
committed
fix(search): handling length of zero
Fixes the function throwing an error when there are no code search results
1 parent 8903450 commit 7357616

File tree

2 files changed

+154
-186
lines changed

2 files changed

+154
-186
lines changed

src/features/search/search-code/feature.spec.int.ts

+95-182
Original file line numberDiff line numberDiff line change
@@ -31,46 +31,29 @@ describe('searchCode integration', () => {
3131
}
3232

3333
const options: SearchCodeOptions = {
34-
searchText: 'function',
34+
searchText: 'class',
3535
projectId: projectName,
3636
top: 10,
3737
};
3838

39-
try {
40-
// Act - make an actual API call to Azure DevOps
41-
const result = await searchCode(connection, options);
42-
43-
// Assert on the actual response
44-
expect(result).toBeDefined();
45-
expect(typeof result.count).toBe('number');
46-
expect(Array.isArray(result.results)).toBe(true);
47-
48-
// Check structure of returned items (if any)
49-
if (result.results.length > 0) {
50-
const firstResult = result.results[0];
51-
expect(firstResult.fileName).toBeDefined();
52-
expect(firstResult.path).toBeDefined();
53-
expect(firstResult.project).toBeDefined();
54-
expect(firstResult.repository).toBeDefined();
55-
56-
if (firstResult.project) {
57-
expect(firstResult.project.name).toBe(projectName);
58-
}
59-
}
60-
} catch (error) {
61-
// Skip test if the code search extension is not installed
62-
if (
63-
error instanceof Error &&
64-
(error.message.includes('ms.vss-code-search is not installed') ||
65-
error.message.includes('Resource not found') ||
66-
error.message.includes('Failed to search code'))
67-
) {
68-
console.log(
69-
'Skipping test: Code Search extension is not installed or not available in this Azure DevOps organization',
70-
);
71-
return;
72-
}
73-
throw error;
39+
// Act - make an actual API call to Azure DevOps
40+
const result = await searchCode(connection, options);
41+
42+
// Assert on the actual response
43+
expect(result).toBeDefined();
44+
expect(typeof result.count).toBe('number');
45+
expect(Array.isArray(result.results)).toBe(true);
46+
47+
// Check structure of returned items (if any)
48+
49+
const firstResult = result.results[0];
50+
expect(firstResult.fileName).toBeDefined();
51+
expect(firstResult.path).toBeDefined();
52+
expect(firstResult.project).toBeDefined();
53+
expect(firstResult.repository).toBeDefined();
54+
55+
if (firstResult.project) {
56+
expect(firstResult.project.name).toBe(projectName);
7457
}
7558
});
7659

@@ -89,41 +72,22 @@ describe('searchCode integration', () => {
8972
}
9073

9174
const options: SearchCodeOptions = {
92-
searchText: 'function',
75+
searchText: 'class',
9376
projectId: projectName,
9477
top: 5,
9578
includeContent: true,
9679
};
9780

98-
try {
99-
// Act - make an actual API call to Azure DevOps
100-
const result = await searchCode(connection, options);
101-
102-
// Assert on the actual response
103-
expect(result).toBeDefined();
104-
105-
// Check if content is included (if any results)
106-
if (result.results.length > 0) {
107-
// At least some results should have content
108-
// Note: Some files might fail to fetch content, so we don't expect all to have it
109-
const hasContent = result.results.some((r) => r.content !== undefined);
110-
expect(hasContent).toBe(true);
111-
}
112-
} catch (error) {
113-
// Skip test if the code search extension is not installed
114-
if (
115-
error instanceof Error &&
116-
(error.message.includes('ms.vss-code-search is not installed') ||
117-
error.message.includes('Resource not found') ||
118-
error.message.includes('Failed to search code'))
119-
) {
120-
console.log(
121-
'Skipping test: Code Search extension is not installed or not available in this Azure DevOps organization',
122-
);
123-
return;
124-
}
125-
throw error;
126-
}
81+
// Act - make an actual API call to Azure DevOps
82+
const result = await searchCode(connection, options);
83+
84+
// Assert on the actual response
85+
expect(result).toBeDefined();
86+
87+
// At least some results should have content
88+
// Note: Some files might fail to fetch content, so we don't expect all to have it
89+
const hasContent = result.results.some((r) => r.content !== undefined);
90+
expect(hasContent).toBe(true);
12791
});
12892

12993
test('should filter results when filters are provided', async () => {
@@ -140,62 +104,37 @@ describe('searchCode integration', () => {
140104
);
141105
}
142106

143-
try {
144-
// First get some results to find a repository name
145-
const initialOptions: SearchCodeOptions = {
146-
searchText: 'function',
147-
projectId: projectName,
148-
top: 1,
149-
};
150-
151-
const initialResult = await searchCode(connection, initialOptions);
152-
153-
// Skip if no results found
154-
if (initialResult.results.length === 0) {
155-
console.log('Skipping filter test: No initial results found');
156-
return;
157-
}
158-
159-
// Use the repository from the first result for filtering
160-
const repoName = initialResult.results[0].repository.name;
161-
162-
const filteredOptions: SearchCodeOptions = {
163-
searchText: 'function',
164-
projectId: projectName,
165-
filters: {
166-
Repository: [repoName],
167-
},
168-
top: 5,
169-
};
170-
171-
// Act - make an actual API call to Azure DevOps with filters
172-
const result = await searchCode(connection, filteredOptions);
173-
174-
// Assert on the actual response
175-
expect(result).toBeDefined();
176-
177-
// All results should be from the specified repository
178-
if (result.results.length > 0) {
179-
const allFromRepo = result.results.every(
180-
(r) => r.repository.name === repoName,
181-
);
182-
expect(allFromRepo).toBe(true);
183-
}
184-
} catch (error) {
185-
// Skip test if the code search extension is not installed
186-
if (
187-
error instanceof Error &&
188-
(error.message.includes('ms.vss-code-search is not installed') ||
189-
error.message.includes('Resource not found') ||
190-
error.message.includes('Failed to search code'))
191-
) {
192-
console.log(
193-
'Skipping test: Code Search extension is not installed or not available in this Azure DevOps organization',
194-
);
195-
return;
196-
}
197-
throw error;
198-
}
107+
// First get some results to find a repository name
108+
const initialOptions: SearchCodeOptions = {
109+
searchText: 'class',
110+
projectId: projectName,
111+
top: 1,
112+
};
113+
114+
const initialResult = await searchCode(connection, initialOptions);
115+
116+
// Use the repository from the first result for filtering
117+
const repoName = initialResult.results[0].repository.name;
118+
119+
const filteredOptions: SearchCodeOptions = {
120+
searchText: 'class',
121+
projectId: projectName,
122+
filters: {
123+
Repository: [repoName],
124+
},
125+
top: 5,
126+
};
127+
128+
// Act - make an actual API call to Azure DevOps with filters
129+
const result = await searchCode(connection, filteredOptions);
130+
131+
// Assert on the actual response
132+
expect(result).toBeDefined();
133+
134+
const allFromRepo = result.results.every(
135+
(r) => r.repository.name === repoName,
136+
);
137+
expect(allFromRepo).toBe(true);
199138
});
200139

201140
test('should handle pagination', async () => {
@@ -212,65 +151,39 @@ describe('searchCode integration', () => {
212151
);
213152
}
214153

215-
try {
216-
// Get first page
217-
const firstPageOptions: SearchCodeOptions = {
218-
searchText: 'function',
219-
projectId: projectName,
220-
top: 2,
221-
skip: 0,
222-
};
223-
224-
const firstPageResult = await searchCode(connection, firstPageOptions);
225-
226-
// Skip if not enough results for pagination test
227-
if (firstPageResult.count <= 2) {
228-
console.log('Skipping pagination test: Not enough results');
229-
return;
230-
}
231-
232-
// Get second page
233-
const secondPageOptions: SearchCodeOptions = {
234-
searchText: 'function',
235-
projectId: projectName,
236-
top: 2,
237-
skip: 2,
238-
};
239-
240-
const secondPageResult = await searchCode(connection, secondPageOptions);
241-
242-
// Assert on pagination
243-
expect(secondPageResult).toBeDefined();
244-
expect(secondPageResult.results.length).toBeGreaterThan(0);
245-
246-
// First and second page should have different results
247-
if (
248-
firstPageResult.results.length > 0 &&
249-
secondPageResult.results.length > 0
250-
) {
251-
const firstPagePaths = firstPageResult.results.map((r) => r.path);
252-
const secondPagePaths = secondPageResult.results.map((r) => r.path);
253-
254-
// Check if there's any overlap between pages
255-
const hasOverlap = firstPagePaths.some((path) =>
256-
secondPagePaths.includes(path),
257-
);
258-
expect(hasOverlap).toBe(false);
259-
}
260-
} catch (error) {
261-
// Skip test if the code search extension is not installed
262-
if (
263-
error instanceof Error &&
264-
(error.message.includes('ms.vss-code-search is not installed') ||
265-
error.message.includes('Resource not found') ||
266-
error.message.includes('Failed to search code'))
267-
) {
268-
console.log(
269-
'Skipping test: Code Search extension is not installed or not available in this Azure DevOps organization',
270-
);
271-
return;
272-
}
273-
throw error;
274-
}
154+
// Get first page
155+
const firstPageOptions: SearchCodeOptions = {
156+
searchText: 'class',
157+
projectId: projectName,
158+
top: 2,
159+
skip: 0,
160+
};
161+
162+
const firstPageResult = await searchCode(connection, firstPageOptions);
163+
164+
// Get second page
165+
const secondPageOptions: SearchCodeOptions = {
166+
searchText: 'class',
167+
projectId: projectName,
168+
top: 2,
169+
skip: 2,
170+
};
171+
172+
const secondPageResult = await searchCode(connection, secondPageOptions);
173+
174+
// Assert on pagination
175+
expect(secondPageResult).toBeDefined();
176+
expect(secondPageResult.results.length).toBeGreaterThan(0);
177+
178+
// First and second page should have different results
179+
180+
const firstPagePaths = firstPageResult.results.map((r) => r.path);
181+
const secondPagePaths = secondPageResult.results.map((r) => r.path);
182+
183+
// Check if there's any overlap between pages
184+
const hasOverlap = firstPagePaths.some((path) =>
185+
secondPagePaths.includes(path),
186+
);
187+
expect(hasOverlap).toBe(false);
275188
});
276189
});

0 commit comments

Comments
 (0)