github-code-search.test.ts•5.27 kB
import { describe, it, expect } from 'vitest';
import { GitHubCodeSearcher } from '../src/github-code-search.js';
describe('GitHubCodeSearcher', () => {
let searcher: GitHubCodeSearcher;
beforeEach(() => {
searcher = new GitHubCodeSearcher();
});
describe('constructor', () => {
it('should create with default options', () => {
expect(searcher).toBeInstanceOf(GitHubCodeSearcher);
});
it('should create with custom options', () => {
const customSearcher = new GitHubCodeSearcher({ sort: 'created', order: 'asc' });
expect(customSearcher).toBeInstanceOf(GitHubCodeSearcher);
});
});
describe('query building', () => {
it('should handle simple queries', () => {
// This test verifies the query building logic indirectly
// through the class methods available
expect(searcher.hasNextPage({
results: [],
currentPage: 1,
totalPages: 5,
totalResults: 100,
hasNextPage: true,
hasPreviousPage: false,
query: 'test'
})).toBe(true);
});
it('should handle pagination correctly', () => {
const mockPage = {
results: [],
currentPage: 2,
totalPages: 5,
totalResults: 100,
hasNextPage: true,
hasPreviousPage: true,
query: 'test',
};
expect(searcher.hasNextPage(mockPage)).toBe(true);
expect(searcher.hasPreviousPage(mockPage)).toBe(true);
});
it('should correctly identify last page', () => {
const lastPage = {
results: [],
currentPage: 5,
totalPages: 5,
totalResults: 100,
hasNextPage: false,
hasPreviousPage: true,
query: 'test',
};
expect(searcher.hasNextPage(lastPage)).toBe(false);
expect(searcher.hasPreviousPage(lastPage)).toBe(true);
});
it('should correctly identify first page', () => {
const firstPage = {
results: [],
currentPage: 1,
totalPages: 5,
totalResults: 100,
hasNextPage: true,
hasPreviousPage: false,
query: 'test',
};
expect(searcher.hasNextPage(firstPage)).toBe(true);
expect(searcher.hasPreviousPage(firstPage)).toBe(false);
});
});
describe('sort configuration', () => {
it('should set sort options', () => {
searcher.setSort('created', 'asc');
// Since options are private, we can't directly test them
// but we can verify the method doesn't throw
expect(() => searcher.setSort('updated', 'desc')).not.toThrow();
});
});
describe('query examples', () => {
it('should provide query examples', () => {
const examples = GitHubCodeSearcher.getQueryExamples();
expect(Array.isArray(examples)).toBe(true);
expect(examples.length).toBeGreaterThan(0);
expect(examples[0]).toContain('addClass');
});
});
describe('URL conversion', () => {
it('should convert GitHub HTML URL to raw URL', () => {
// We can't directly test the private method, but we can verify
// that the formatCodeResult method produces the expected URL format
const mockCodeResult = {
name: 'test.ts',
path: 'src/test.ts',
sha: 'abc123',
url: 'https://api.github.com/repos/owner/repo/contents/src/test.ts',
git_url: 'https://api.github.com/repos/owner/repo/git/blobs/abc123',
html_url: 'https://github.com/owner/repo/blob/main/src/test.ts',
score: 1.0,
repository: {
id: 123,
node_id: 'test',
name: 'repo',
full_name: 'owner/repo',
owner: {
login: 'owner',
id: 456,
avatar_url: 'https://avatars.githubusercontent.com/u/456',
html_url: 'https://github.com/owner'
},
private: false,
html_url: 'https://github.com/owner/repo',
description: 'Test repository',
language: 'TypeScript',
stargazers_count: 10,
watchers_count: 5,
forks_count: 2
}
};
const result = (searcher as any).formatCodeResult(mockCodeResult, 1);
// Check that the URL is converted to raw format
expect(result.url).toBe('https://raw.githubusercontent.com/owner/repo/main/src/test.ts');
expect(result.title).toBe('owner/repo/src/test.ts');
});
});
describe('error handling for pagination', () => {
it('should throw error when getting next page without availability', async () => {
const lastPage = {
results: [],
currentPage: 5,
totalPages: 5,
totalResults: 100,
hasNextPage: false,
hasPreviousPage: true,
query: 'test',
};
await expect(searcher.getNextPage(lastPage)).rejects.toThrow('No next page available');
});
it('should throw error when getting previous page without availability', async () => {
const firstPage = {
results: [],
currentPage: 1,
totalPages: 5,
totalResults: 100,
hasNextPage: true,
hasPreviousPage: false,
query: 'test',
};
await expect(searcher.getPreviousPage(firstPage)).rejects.toThrow('No previous page available');
});
});
});