diff --git a/src/__tests__/codeReview.test.ts b/src/__tests__/codeReview.test.ts new file mode 100644 index 0000000..a047ad4 --- /dev/null +++ b/src/__tests__/codeReview.test.ts @@ -0,0 +1,51 @@ +import { test, describe, expect } from '@jest/globals'; +import { handleCodeReview } from '../services/github'; + +// Mock data for testing +const mockPullRequestData = { + id: 1, + title: 'Add new feature', + comments: [ + { + id: 101, + body: 'Please update the README.', + user: { + login: 'reviewer1' + } + }, + { + id: 102, + body: 'LGTM', + user: { + login: 'reviewer2' + } + } + ] +}; + +const mockReviewHandlerResponse = { + updated: true, + messages: ['Reviewed: Please update the README.'] +}; + +// Mock the actual function that will handle the business logic +jest.mock('../services/github', () => ({ + handleCodeReview: jest.fn(() => mockReviewHandlerResponse) +})); + +describe('Code Review Feature', () => { + test('should process code review comments correctly', async () => { + const response = await handleCodeReview(mockPullRequestData); + + expect(response.updated).toBe(true); + expect(response.messages.length).toBeGreaterThan(0); + expect(response.messages[0]).toBe('Reviewed: Please update the README.'); + }); + + test('should handle empty comments', async () => { + const response = await handleCodeReview({ ...mockPullRequestData, comments: [] }); + + expect(response.updated).toBe(false); + expect(response.messages.length).toBe(0); + }); +}); diff --git a/src/__tests__/codeReviewFeature.test.ts b/src/__tests__/codeReviewFeature.test.ts new file mode 100644 index 0000000..a1f2379 --- /dev/null +++ b/src/__tests__/codeReviewFeature.test.ts @@ -0,0 +1,45 @@ +import { describe, it, expect } from '@jest/globals'; + +// Mock function or service to simulate code review changes +type MockReviewService = { + addComment: (comment: string) => void; + removeComment: (commentId: string) => void; + listComments: () => string[]; +}; + +// Simulating a basic mock for code review service +const mockReviewService: MockReviewService = { + comments: [], + addComment(comment) { + this.comments.push(comment); + }, + removeComment(commentId) { + this.comments = this.comments.filter(c => c !== commentId); + }, + listComments() { + return this.comments; + } +}; + +// Unit test for code review feature +describe('Code Review Feature', () => { + it('should add a comment', () => { + mockReviewService.addComment('This is a new comment'); + const comments = mockReviewService.listComments(); + expect(comments).toContain('This is a new comment'); + }); + + it('should remove a comment', () => { + mockReviewService.addComment('Comment to be removed'); + mockReviewService.removeComment('Comment to be removed'); + const comments = mockReviewService.listComments(); + expect(comments).not.toContain('Comment to be removed'); + }); + + it('should list comments correctly', () => { + mockReviewService.addComment('First comment'); + mockReviewService.addComment('Second comment'); + const comments = mockReviewService.listComments(); + expect(comments).toEqual(['First comment', 'Second comment']); + }); +}); diff --git a/src/__tests__/notifications.test.ts b/src/__tests__/notifications.test.ts new file mode 100644 index 0000000..ba44f49 --- /dev/null +++ b/src/__tests__/notifications.test.ts @@ -0,0 +1,34 @@ +import { sendNotification } from '../services/notifications'; + +jest.mock('../services/notifications', () => ({ + sendNotification: jest.fn(), +})); + +describe('Notifications Service', () => { + it('should send notification with correct parameters', async () => { + const notification = { + userId: 'user-1', + type: 'TASK_COMPLETED', + data: { taskId: 'task-123' } + }; + + await sendNotification(notification); + + expect(sendNotification).toHaveBeenCalledWith(notification); + expect(sendNotification).toHaveBeenCalledTimes(1); + }); + + it('should handle errors without throwing', async () => { + const errorMock = jest.fn().mockRejectedValue(new Error('Network error')); + const notification = { + userId: 'user-1', + type: 'TASK_COMPLETED', + data: { taskId: 'task-123' } + }; + + sendNotification.mockImplementationOnce(errorMock); + + await expect(sendNotification(notification)).resolves.not.toThrow(); + expect(sendNotification).toHaveBeenCalledWith(notification); + }); +}); \ No newline at end of file diff --git a/tests/test_code_review_feature.py b/tests/test_code_review_feature.py new file mode 100644 index 0000000..f0d0c19 --- /dev/null +++ b/tests/test_code_review_feature.py @@ -0,0 +1,59 @@ + +import unittest +from code_review import CodeReview + +class TestCodeReviewFeature(unittest.TestCase): + def setUp(self): + self.review = CodeReview() + + def test_add_comment(self): + result = self.review.add_comment('Great job!') + self.assertTrue(result) + + def test_add_duplicate_comment(self): + self.review.add_comment('Great work!') + result = self.review.add_comment('Great work!') + self.assertFalse(result) + + def test_remove_comment(self): + self.review.add_comment('Needs improvement.') + result = self.review.remove_comment('Needs improvement.') + self.assertTrue(result) + + def test_remove_non_existent_comment(self): + result = self.review.remove_comment('This is wrong!') + self.assertFalse(result) + + def test_get_all_comments(self): + self.review.add_comment('Well done!') + comments = self.review.get_all_comments() + self.assertIn('Well done!', comments) + + def test_get_comments_count(self): + self.review.add_comment('Consider refactoring.') + self.review.add_comment('Looks good.') + count = self.review.get_comments_count() + self.assertEqual(count, 2) + + def test_clear_comments(self): + self.review.add_comment('Clean code!') + self.review.clear_comments() + comments = self.review.get_all_comments() + self.assertEqual(len(comments), 0) + + def test_update_comment(self): + self.review.add_comment('Typo here.') + result = self.review.update_comment('Typo here.', 'Spelling mistake.') + self.assertTrue(result) + + def test_update_non_existent_comment(self): + result = self.review.update_comment('Invalid comment', 'New suggestion') + self.assertFalse(result) + + def test_contains_comment(self): + self.review.add_comment('Review this part.') + contains = self.review.contains_comment('Review this part.') + self.assertTrue(contains) + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/tests/test_latest_changes.py b/tests/test_latest_changes.py new file mode 100644 index 0000000..50b54f0 --- /dev/null +++ b/tests/test_latest_changes.py @@ -0,0 +1,12 @@ +import unittest +from my_module import latest_changes_function + +class TestLatestChanges(unittest.TestCase): + def test_latest_changes_function(self): + # Assuming latest_changes_function should return True for valid input + input_data = 'some valid input' + expected_output = True + self.assertEqual(latest_changes_function(input_data), expected_output) + +if __name__ == '__main__': + unittest.main()