review-copilot
Version:
ReviewCopilot - AI-powered code review assistant with customizable prompts
111 lines (110 loc) • 4.29 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const file_content_provider_1 = require("../services/file-content-provider");
describe('LocalFileContentProvider', () => {
let provider;
let fsSpy;
let errorSpy;
beforeEach(() => {
provider = new file_content_provider_1.LocalFileContentProvider();
fsSpy = jest.spyOn(fs_1.default, 'readFileSync');
errorSpy = jest.spyOn(console, 'error').mockImplementation(() => { });
});
afterEach(() => {
jest.clearAllMocks();
errorSpy.mockRestore();
fsSpy.mockRestore();
provider = null;
});
afterAll(() => {
jest.restoreAllMocks();
});
it('should return file content if file exists', async () => {
fsSpy.mockReturnValue('file content');
const content = await provider.getFileContent('file.txt');
expect(content).toBe('file content');
expect(fsSpy).toHaveBeenCalledWith('file.txt', 'utf8');
});
it('should return null and log error if file does not exist', async () => {
fsSpy.mockImplementation(() => {
throw new Error('not found');
});
const content = await provider.getFileContent('missing.txt');
expect(content).toBeNull();
expect(errorSpy).toHaveBeenCalled();
});
});
describe('GitPlatformFileContentProvider', () => {
let gitServiceMock;
let provider;
beforeEach(() => {
gitServiceMock = {
getFileContent: jest.fn(),
};
provider = new file_content_provider_1.GitPlatformFileContentProvider(gitServiceMock);
});
afterEach(() => {
// Clean up mocks after each test
jest.clearAllMocks();
});
afterAll(() => {
// Restore all mocks after all tests
jest.restoreAllMocks();
});
it('should call gitService.getFileContent with correct args', async () => {
const mockContent = 'remote content';
gitServiceMock.getFileContent.mockResolvedValue(mockContent);
const context = {
owner: 'alex',
repo: 'repo',
pullNumber: 1,
platform: 'github',
commitId: 'abc123',
path: 'main',
};
const content = await provider.getFileContent('file.txt', context);
expect(gitServiceMock.getFileContent).toHaveBeenCalledWith({
owner: context.owner,
repo: context.repo,
filePath: 'file.txt',
pullNumber: context.pullNumber,
});
expect(content).toBe(mockContent);
});
it('should throw if context is missing', async () => {
const invalidContext = {};
await expect(provider.getFileContent('file.txt', invalidContext)).rejects.toThrow('Missing required context for Git platform file content retrieval');
});
});
describe('FileContentProviderFactory', () => {
let gitServiceMock;
let originalEnv;
beforeEach(() => {
originalEnv = { ...process.env };
gitServiceMock = {
getFileContent: jest.fn(),
};
});
afterEach(() => {
process.env = { ...originalEnv };
});
it('should return GitPlatformFileContentProvider in CI', () => {
process.env.GITHUB_ACTIONS = 'true';
const provider = file_content_provider_1.FileContentProviderFactory.createProvider(gitServiceMock);
expect(provider).toBeInstanceOf(file_content_provider_1.GitPlatformFileContentProvider);
});
it('should throw if gitService is missing in CI', () => {
process.env.GITHUB_ACTIONS = 'true';
expect(() => file_content_provider_1.FileContentProviderFactory.createProvider()).toThrow('Git service is required for CI environment');
});
it('should return LocalFileContentProvider outside CI', () => {
delete process.env.GITHUB_ACTIONS;
delete process.env.GITLAB_CI;
const provider = file_content_provider_1.FileContentProviderFactory.createProvider();
expect(provider).toBeInstanceOf(file_content_provider_1.LocalFileContentProvider);
});
});