// Mock CouchDB service for testing const mockCouchdbService = { createDocument: jest.fn(), findDocumentById: jest.fn(), updateDocument: jest.fn(), findByType: jest.fn(), initialize: jest.fn(), getDocument: jest.fn(), findUserById: jest.fn(), update: jest.fn(), }; // Mock the service module jest.mock('../../services/couchdbService', () => mockCouchdbService); // Reset all mocks to ensure clean state mockCouchdbService.createDocument.mockReset(); mockCouchdbService.findDocumentById.mockReset(); mockCouchdbService.updateDocument.mockReset(); mockCouchdbService.findByType.mockReset(); }); describe('Schema Validation', () => { it('should create a valid comment', async () => { const commentData = { post: 'post_123', author: 'user_123', content: 'This is a great post!', }; const mockCreated = { _id: 'comment_123', _rev: '1-abc', type: 'comment', ...commentData, likes: [], createdAt: '2023-01-01T00:00:00.000Z', updatedAt: '2023-01-01T00:00:00.000Z' }; mockCouchdbService.createDocument.mockResolvedValue(mockCreated); const comment = await Comment.create(commentData); expect(comment._id).toBeDefined(); expect(comment.post).toBe(commentData.post); expect(comment.author).toBe(commentData.author); expect(comment.content).toBe(commentData.content); expect(comment.likes).toEqual([]); }); it('should require post field', async () => { const commentData = { author: 'user_123', content: 'Comment without post', }; expect(() => new Comment(commentData)).toThrow(); }); it('should require author field', async () => { const commentData = { post: 'post_123', content: 'Comment without author', }; expect(() => new Comment(commentData)).toThrow(); }); it('should require content field', async () => { const commentData = { post: 'post_123', author: 'user_123', }; expect(() => new Comment(commentData)).toThrow(); }); }); describe('Content Validation', () => { it('should trim content', async () => { const commentData = { post: 'post_123', author: 'user_123', content: ' This comment has spaces ', }; const mockCreated = { _id: 'comment_123', _rev: '1-abc', type: 'comment', ...commentData, content: 'This comment has spaces', likes: [], createdAt: '2023-01-01T00:00:00.000Z', updatedAt: '2023-01-01T00:00:00.000Z' }; mockCouchdbService.createDocument.mockResolvedValue(mockCreated); const comment = await Comment.create(commentData); expect(comment.content).toBe('This comment has spaces'); }); it('should allow long comments', async () => { const longContent = 'a'.repeat(1001); // Long comment const commentData = { post: 'post_123', author: 'user_123', content: longContent, }; const mockCreated = { _id: 'comment_123', _rev: '1-abc', type: 'comment', ...commentData, likes: [], createdAt: '2023-01-01T00:00:00.000Z', updatedAt: '2023-01-01T00:00:00.000Z' }; mockCouchdbService.createDocument.mockResolvedValue(mockCreated); const comment = await Comment.create(commentData); expect(comment.content).toBe(longContent); }); it('should reject empty content after trimming', async () => { const commentData = { post: 'post_123', author: 'user_123', content: ' ', // Only spaces }; expect(() => new Comment(commentData)).toThrow(); }); }); describe('Likes', () => { it('should start with empty likes array', async () => { const commentData = { post: 'post_123', author: 'user_123', content: 'Comment with no likes', }; const mockCreated = { _id: 'comment_123', _rev: '1-abc', type: 'comment', ...commentData, likes: [], createdAt: '2023-01-01T00:00:00.000Z', updatedAt: '2023-01-01T00:00:00.000Z' }; mockCouchdbService.createDocument.mockResolvedValue(mockCreated); const comment = await Comment.create(commentData); expect(comment.likes).toEqual([]); expect(comment.likes).toHaveLength(0); }); it('should allow adding likes', async () => { const commentData = { post: 'post_123', author: 'user_123', content: 'Comment to be liked', likes: ['user_456'] }; const mockCreated = { _id: 'comment_123', _rev: '1-abc', type: 'comment', ...commentData, createdAt: '2023-01-01T00:00:00.000Z', updatedAt: '2023-01-01T00:00:00.000Z' }; mockCouchdbService.createDocument.mockResolvedValue(mockCreated); const comment = await Comment.create(commentData); expect(comment.likes).toHaveLength(1); expect(comment.likes[0]).toBe('user_456'); }); it('should allow multiple likes', async () => { const commentData = { post: 'post_123', author: 'user_123', content: 'Popular comment', likes: ['user_456', 'user_789', 'user_101'] }; const mockCreated = { _id: 'comment_123', _rev: '1-abc', type: 'comment', ...commentData, createdAt: '2023-01-01T00:00:00.000Z', updatedAt: '2023-01-01T00:00:00.000Z' }; mockCouchdbService.createDocument.mockResolvedValue(mockCreated); const comment = await Comment.create(commentData); expect(comment.likes).toHaveLength(3); expect(comment.likes).toContain('user_456'); expect(comment.likes).toContain('user_789'); expect(comment.likes).toContain('user_101'); }); it('should allow adding likes after creation', async () => { const commentData = { post: 'post_123', author: 'user_123', content: 'Comment to be liked later', }; const mockComment = { _id: 'comment_123', _rev: '1-abc', type: 'comment', ...commentData, likes: [], createdAt: '2023-01-01T00:00:00.000Z', updatedAt: '2023-01-01T00:00:00.000Z' }; mockCouchdbService.findDocumentById.mockResolvedValue(mockComment); mockCouchdbService.updateDocument.mockResolvedValue({ ...mockComment, likes: ['user_456'], _rev: '2-def' }); const comment = await Comment.findById('comment_123'); comment.likes.push('user_456'); await comment.save(); expect(comment.likes).toHaveLength(1); expect(comment.likes[0]).toBe('user_456'); }); }); describe('Relationships', () => { it('should reference post ID', async () => { const commentData = { post: 'post_123', author: 'user_123', content: 'Comment on specific post', }; const mockCreated = { _id: 'comment_123', _rev: '1-abc', type: 'comment', ...commentData, likes: [], createdAt: '2023-01-01T00:00:00.000Z', updatedAt: '2023-01-01T00:00:00.000Z' }; mockCouchdbService.createDocument.mockResolvedValue(mockCreated); const comment = await Comment.create(commentData); expect(comment.post).toBe('post_123'); }); it('should reference author ID', async () => { const commentData = { post: 'post_123', author: 'user_123', content: 'Comment by specific user', }; const mockCreated = { _id: 'comment_123', _rev: '1-abc', type: 'comment', ...commentData, likes: [], createdAt: '2023-01-01T00:00:00.000Z', updatedAt: '2023-01-01T00:00:00.000Z' }; mockCouchdbService.createDocument.mockResolvedValue(mockCreated); const comment = await Comment.create(commentData); expect(comment.author).toBe('user_123'); }); }); describe('Timestamps', () => { it('should automatically set createdAt and updatedAt', async () => { const commentData = { post: 'post_123', author: 'user_123', content: 'Timestamp test comment', }; const mockCreated = { _id: 'comment_123', _rev: '1-abc', type: 'comment', ...commentData, likes: [], createdAt: '2023-01-01T00:00:00.000Z', updatedAt: '2023-01-01T00:00:00.000Z' }; mockCouchdbService.createDocument.mockResolvedValue(mockCreated); const comment = await Comment.create(commentData); expect(comment.createdAt).toBeDefined(); expect(comment.updatedAt).toBeDefined(); expect(typeof comment.createdAt).toBe('string'); expect(typeof comment.updatedAt).toBe('string'); }); it('should update updatedAt on modification', async () => { const commentData = { post: 'post_123', author: 'user_123', content: 'Update test comment', }; const mockComment = { _id: 'comment_123', _rev: '1-abc', type: 'comment', ...commentData, likes: [], createdAt: '2023-01-01T00:00:00.000Z', updatedAt: '2023-01-01T00:00:00.000Z' }; mockCouchdbService.findDocumentById.mockResolvedValue(mockComment); mockCouchdbService.updateDocument.mockResolvedValue({ ...mockComment, content: 'Updated comment content', _rev: '2-def', updatedAt: '2023-01-01T00:00:01.000Z' }); const comment = await Comment.findById('comment_123'); const originalUpdatedAt = comment.updatedAt; comment.content = 'Updated comment content'; await comment.save(); expect(comment.updatedAt).not.toBe(originalUpdatedAt); }); }); describe('Content Edge Cases', () => { it('should handle special characters in content', async () => { const commentData = { post: 'post_123', author: 'user_123', content: 'This comment has émojis 🎉 and spëcial charactërs!', }; const mockCreated = { _id: 'comment_123', _rev: '1-abc', type: 'comment', ...commentData, likes: [], createdAt: '2023-01-01T00:00:00.000Z', updatedAt: '2023-01-01T00:00:00.000Z' }; mockCouchdbService.createDocument.mockResolvedValue(mockCreated); const comment = await Comment.create(commentData); expect(comment.content).toBe('This comment has émojis 🎉 and spëcial charactërs!'); }); it('should handle newlines in content', async () => { const commentData = { post: 'post_123', author: 'user_123', content: 'This comment\nhas\nmultiple\nlines', }; const mockCreated = { _id: 'comment_123', _rev: '1-abc', type: 'comment', ...commentData, likes: [], createdAt: '2023-01-01T00:00:00.000Z', updatedAt: '2023-01-01T00:00:00.000Z' }; mockCouchdbService.createDocument.mockResolvedValue(mockCreated); const comment = await Comment.create(commentData); expect(comment.content).toBe('This comment\nhas\nmultiple\nlines'); }); }); describe('Static Methods', () => { it('should find comment by ID', async () => { const mockComment = { _id: 'comment_123', _rev: '1-abc', type: 'comment', post: 'post_123', author: 'user_123', content: 'Test comment', likes: [], createdAt: '2023-01-01T00:00:00.000Z', updatedAt: '2023-01-01T00:00:00.000Z' }; mockCouchdbService.findDocumentById.mockResolvedValue(mockComment); const comment = await Comment.findById('comment_123'); expect(comment).toBeDefined(); expect(comment._id).toBe('comment_123'); expect(comment.content).toBe('Test comment'); }); it('should return null when comment not found', async () => { mockCouchdbService.findDocumentById.mockResolvedValue(null); const comment = await Comment.findById('nonexistent'); expect(comment).toBeNull(); }); }); });