Skip to content

fix(search): handling length of zero #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
277 changes: 95 additions & 182 deletions src/features/search/search-code/feature.spec.int.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,46 +31,29 @@ describe('searchCode integration', () => {
}

const options: SearchCodeOptions = {
searchText: 'function',
searchText: 'class',
projectId: projectName,
top: 10,
};

try {
// Act - make an actual API call to Azure DevOps
const result = await searchCode(connection, options);

// Assert on the actual response
expect(result).toBeDefined();
expect(typeof result.count).toBe('number');
expect(Array.isArray(result.results)).toBe(true);

// Check structure of returned items (if any)
if (result.results.length > 0) {
const firstResult = result.results[0];
expect(firstResult.fileName).toBeDefined();
expect(firstResult.path).toBeDefined();
expect(firstResult.project).toBeDefined();
expect(firstResult.repository).toBeDefined();

if (firstResult.project) {
expect(firstResult.project.name).toBe(projectName);
}
}
} catch (error) {
// Skip test if the code search extension is not installed
if (
error instanceof Error &&
(error.message.includes('ms.vss-code-search is not installed') ||
error.message.includes('Resource not found') ||
error.message.includes('Failed to search code'))
) {
console.log(
'Skipping test: Code Search extension is not installed or not available in this Azure DevOps organization',
);
return;
}
throw error;
// Act - make an actual API call to Azure DevOps
const result = await searchCode(connection, options);

// Assert on the actual response
expect(result).toBeDefined();
expect(typeof result.count).toBe('number');
expect(Array.isArray(result.results)).toBe(true);

// Check structure of returned items (if any)

const firstResult = result.results[0];
expect(firstResult.fileName).toBeDefined();
expect(firstResult.path).toBeDefined();
expect(firstResult.project).toBeDefined();
expect(firstResult.repository).toBeDefined();

if (firstResult.project) {
expect(firstResult.project.name).toBe(projectName);
}
});

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

const options: SearchCodeOptions = {
searchText: 'function',
searchText: 'class',
projectId: projectName,
top: 5,
includeContent: true,
};

try {
// Act - make an actual API call to Azure DevOps
const result = await searchCode(connection, options);

// Assert on the actual response
expect(result).toBeDefined();

// Check if content is included (if any results)
if (result.results.length > 0) {
// At least some results should have content
// Note: Some files might fail to fetch content, so we don't expect all to have it
const hasContent = result.results.some((r) => r.content !== undefined);
expect(hasContent).toBe(true);
}
} catch (error) {
// Skip test if the code search extension is not installed
if (
error instanceof Error &&
(error.message.includes('ms.vss-code-search is not installed') ||
error.message.includes('Resource not found') ||
error.message.includes('Failed to search code'))
) {
console.log(
'Skipping test: Code Search extension is not installed or not available in this Azure DevOps organization',
);
return;
}
throw error;
}
// Act - make an actual API call to Azure DevOps
const result = await searchCode(connection, options);

// Assert on the actual response
expect(result).toBeDefined();

// At least some results should have content
// Note: Some files might fail to fetch content, so we don't expect all to have it
const hasContent = result.results.some((r) => r.content !== undefined);
expect(hasContent).toBe(true);
});

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

try {
// First get some results to find a repository name
const initialOptions: SearchCodeOptions = {
searchText: 'function',
projectId: projectName,
top: 1,
};

const initialResult = await searchCode(connection, initialOptions);

// Skip if no results found
if (initialResult.results.length === 0) {
console.log('Skipping filter test: No initial results found');
return;
}

// Use the repository from the first result for filtering
const repoName = initialResult.results[0].repository.name;

const filteredOptions: SearchCodeOptions = {
searchText: 'function',
projectId: projectName,
filters: {
Repository: [repoName],
},
top: 5,
};

// Act - make an actual API call to Azure DevOps with filters
const result = await searchCode(connection, filteredOptions);

// Assert on the actual response
expect(result).toBeDefined();

// All results should be from the specified repository
if (result.results.length > 0) {
const allFromRepo = result.results.every(
(r) => r.repository.name === repoName,
);
expect(allFromRepo).toBe(true);
}
} catch (error) {
// Skip test if the code search extension is not installed
if (
error instanceof Error &&
(error.message.includes('ms.vss-code-search is not installed') ||
error.message.includes('Resource not found') ||
error.message.includes('Failed to search code'))
) {
console.log(
'Skipping test: Code Search extension is not installed or not available in this Azure DevOps organization',
);
return;
}
throw error;
}
// First get some results to find a repository name
const initialOptions: SearchCodeOptions = {
searchText: 'class',
projectId: projectName,
top: 1,
};

const initialResult = await searchCode(connection, initialOptions);

// Use the repository from the first result for filtering
const repoName = initialResult.results[0].repository.name;

const filteredOptions: SearchCodeOptions = {
searchText: 'class',
projectId: projectName,
filters: {
Repository: [repoName],
},
top: 5,
};

// Act - make an actual API call to Azure DevOps with filters
const result = await searchCode(connection, filteredOptions);

// Assert on the actual response
expect(result).toBeDefined();

const allFromRepo = result.results.every(
(r) => r.repository.name === repoName,
);
expect(allFromRepo).toBe(true);
});

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

try {
// Get first page
const firstPageOptions: SearchCodeOptions = {
searchText: 'function',
projectId: projectName,
top: 2,
skip: 0,
};

const firstPageResult = await searchCode(connection, firstPageOptions);

// Skip if not enough results for pagination test
if (firstPageResult.count <= 2) {
console.log('Skipping pagination test: Not enough results');
return;
}

// Get second page
const secondPageOptions: SearchCodeOptions = {
searchText: 'function',
projectId: projectName,
top: 2,
skip: 2,
};

const secondPageResult = await searchCode(connection, secondPageOptions);

// Assert on pagination
expect(secondPageResult).toBeDefined();
expect(secondPageResult.results.length).toBeGreaterThan(0);

// First and second page should have different results
if (
firstPageResult.results.length > 0 &&
secondPageResult.results.length > 0
) {
const firstPagePaths = firstPageResult.results.map((r) => r.path);
const secondPagePaths = secondPageResult.results.map((r) => r.path);

// Check if there's any overlap between pages
const hasOverlap = firstPagePaths.some((path) =>
secondPagePaths.includes(path),
);
expect(hasOverlap).toBe(false);
}
} catch (error) {
// Skip test if the code search extension is not installed
if (
error instanceof Error &&
(error.message.includes('ms.vss-code-search is not installed') ||
error.message.includes('Resource not found') ||
error.message.includes('Failed to search code'))
) {
console.log(
'Skipping test: Code Search extension is not installed or not available in this Azure DevOps organization',
);
return;
}
throw error;
}
// Get first page
const firstPageOptions: SearchCodeOptions = {
searchText: 'class',
projectId: projectName,
top: 2,
skip: 0,
};

const firstPageResult = await searchCode(connection, firstPageOptions);

// Get second page
const secondPageOptions: SearchCodeOptions = {
searchText: 'class',
projectId: projectName,
top: 2,
skip: 2,
};

const secondPageResult = await searchCode(connection, secondPageOptions);

// Assert on pagination
expect(secondPageResult).toBeDefined();
expect(secondPageResult.results.length).toBeGreaterThan(0);

// First and second page should have different results

const firstPagePaths = firstPageResult.results.map((r) => r.path);
const secondPagePaths = secondPageResult.results.map((r) => r.path);

// Check if there's any overlap between pages
const hasOverlap = firstPagePaths.some((path) =>
secondPagePaths.includes(path),
);
expect(hasOverlap).toBe(false);
});
});
Loading
Loading