|
| 1 | +// |
| 2 | +// Copyright © 2025 Hardcore Engineering Inc. |
| 3 | +// |
| 4 | + |
| 5 | +import { createMarkupOperations } from '../markup/client' |
| 6 | +import { getClient } from '@hcengineering/collaborator-client' |
| 7 | +import { makeCollabId } from '@hcengineering/core' |
| 8 | + |
| 9 | +// Mock dependencies |
| 10 | +jest.mock('@hcengineering/collaborator-client') |
| 11 | +jest.mock('@hcengineering/text', () => ({ |
| 12 | + htmlToJSON: jest.fn((html) => ({ type: 'doc', content: [{ type: 'text', text: html }] })), |
| 13 | + jsonToHTML: jest.fn((json) => json.content?.[0]?.text ?? ''), |
| 14 | + jsonToMarkup: jest.fn((json) => json.content?.[0]?.text ?? ''), |
| 15 | + markupToJSON: jest.fn((markup) => ({ type: 'doc', content: [{ type: 'text', text: markup }] })) |
| 16 | +})) |
| 17 | +jest.mock('@hcengineering/text-markdown', () => ({ |
| 18 | + markdownToMarkup: jest.fn((md) => md), |
| 19 | + markupToMarkdown: jest.fn((json) => json.content?.[0]?.text ?? '') |
| 20 | +})) |
| 21 | + |
| 22 | +describe('MarkupOperations', () => { |
| 23 | + const mockConfig = { |
| 24 | + ACCOUNTS_URL: 'https://accounts.example.com', |
| 25 | + COLLABORATOR_URL: 'https://collaborator.example.com', |
| 26 | + FILES_URL: 'https://files.example.com', |
| 27 | + UPLOAD_URL: 'https://upload.example.com' |
| 28 | + } |
| 29 | + |
| 30 | + const workspace = 'test-workspace' as any |
| 31 | + const token = 'test-token' |
| 32 | + const url = 'https://api.example.com' |
| 33 | + |
| 34 | + let mockCollaborator: any |
| 35 | + let operations: any |
| 36 | + |
| 37 | + beforeEach(() => { |
| 38 | + jest.clearAllMocks() |
| 39 | + |
| 40 | + mockCollaborator = { |
| 41 | + getMarkup: jest.fn(), |
| 42 | + createMarkup: jest.fn() |
| 43 | + } |
| 44 | + ;(getClient as jest.Mock).mockReturnValue(mockCollaborator) |
| 45 | + |
| 46 | + operations = createMarkupOperations(url, workspace, token, mockConfig) |
| 47 | + }) |
| 48 | + |
| 49 | + describe('fetchMarkup', () => { |
| 50 | + const objectClass = 'class:test.Doc' as any |
| 51 | + const objectId = 'doc-id-123' as any |
| 52 | + const objectAttr = 'content' |
| 53 | + const markupRef = 'markup-ref-456' as any |
| 54 | + |
| 55 | + it('should fetch markup in markup format', async () => { |
| 56 | + const mockMarkup = 'Test markup content' |
| 57 | + mockCollaborator.getMarkup.mockResolvedValue(mockMarkup) |
| 58 | + |
| 59 | + const result = await operations.fetchMarkup(objectClass, objectId, objectAttr, markupRef, 'markup') |
| 60 | + |
| 61 | + const collabId = makeCollabId(objectClass, objectId, objectAttr) |
| 62 | + expect(mockCollaborator.getMarkup).toHaveBeenCalledWith(collabId, markupRef) |
| 63 | + expect(result).toBe(mockMarkup) |
| 64 | + }) |
| 65 | + |
| 66 | + it('should fetch markup in HTML format', async () => { |
| 67 | + const mockMarkup = '<p>Test content</p>' |
| 68 | + mockCollaborator.getMarkup.mockResolvedValue(mockMarkup) |
| 69 | + |
| 70 | + const result = await operations.fetchMarkup(objectClass, objectId, objectAttr, markupRef, 'html') |
| 71 | + |
| 72 | + expect(mockCollaborator.getMarkup).toHaveBeenCalled() |
| 73 | + expect(result).toBeDefined() |
| 74 | + }) |
| 75 | + |
| 76 | + it('should fetch markup in markdown format', async () => { |
| 77 | + const mockMarkup = '# Test heading' |
| 78 | + mockCollaborator.getMarkup.mockResolvedValue(mockMarkup) |
| 79 | + |
| 80 | + const result = await operations.fetchMarkup(objectClass, objectId, objectAttr, markupRef, 'markdown') |
| 81 | + |
| 82 | + expect(mockCollaborator.getMarkup).toHaveBeenCalled() |
| 83 | + expect(result).toBeDefined() |
| 84 | + }) |
| 85 | + |
| 86 | + it('should throw error for unknown format', async () => { |
| 87 | + mockCollaborator.getMarkup.mockResolvedValue('content') |
| 88 | + |
| 89 | + await expect( |
| 90 | + operations.fetchMarkup(objectClass, objectId, objectAttr, markupRef, 'unknown-format' as any) |
| 91 | + ).rejects.toThrow('Unknown content format') |
| 92 | + }) |
| 93 | + |
| 94 | + it('should handle collaborator errors', async () => { |
| 95 | + mockCollaborator.getMarkup.mockRejectedValue(new Error('Collaborator error')) |
| 96 | + |
| 97 | + await expect(operations.fetchMarkup(objectClass, objectId, objectAttr, markupRef, 'markup')).rejects.toThrow( |
| 98 | + 'Collaborator error' |
| 99 | + ) |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
| 103 | + describe('uploadMarkup', () => { |
| 104 | + const objectClass = 'class:test.Doc' as any |
| 105 | + const objectId = 'doc-id-123' as any |
| 106 | + const objectAttr = 'content' |
| 107 | + const mockMarkupRef = 'new-markup-ref-789' as any |
| 108 | + |
| 109 | + beforeEach(() => { |
| 110 | + mockCollaborator.createMarkup.mockResolvedValue(mockMarkupRef) |
| 111 | + }) |
| 112 | + |
| 113 | + it('should upload markup in markup format', async () => { |
| 114 | + const content = 'Test markup content' |
| 115 | + |
| 116 | + const result = await operations.uploadMarkup(objectClass, objectId, objectAttr, content, 'markup') |
| 117 | + |
| 118 | + const collabId = makeCollabId(objectClass, objectId, objectAttr) |
| 119 | + expect(mockCollaborator.createMarkup).toHaveBeenCalledWith(collabId, content) |
| 120 | + expect(result).toBe(mockMarkupRef) |
| 121 | + }) |
| 122 | + |
| 123 | + it('should upload markup in HTML format', async () => { |
| 124 | + const content = '<p>Test HTML content</p>' |
| 125 | + |
| 126 | + const result = await operations.uploadMarkup(objectClass, objectId, objectAttr, content, 'html') |
| 127 | + |
| 128 | + expect(mockCollaborator.createMarkup).toHaveBeenCalled() |
| 129 | + expect(result).toBe(mockMarkupRef) |
| 130 | + }) |
| 131 | + |
| 132 | + it('should upload markup in markdown format', async () => { |
| 133 | + const content = '# Test markdown' |
| 134 | + |
| 135 | + const result = await operations.uploadMarkup(objectClass, objectId, objectAttr, content, 'markdown') |
| 136 | + |
| 137 | + expect(mockCollaborator.createMarkup).toHaveBeenCalled() |
| 138 | + expect(result).toBe(mockMarkupRef) |
| 139 | + }) |
| 140 | + |
| 141 | + it('should throw error for unknown format', async () => { |
| 142 | + await expect( |
| 143 | + operations.uploadMarkup(objectClass, objectId, objectAttr, 'content', 'unknown-format' as any) |
| 144 | + ).rejects.toThrow('Unknown content format') |
| 145 | + }) |
| 146 | + |
| 147 | + it('should handle empty content', async () => { |
| 148 | + const result = await operations.uploadMarkup(objectClass, objectId, objectAttr, '', 'markup') |
| 149 | + |
| 150 | + const collabId = makeCollabId(objectClass, objectId, objectAttr) |
| 151 | + expect(mockCollaborator.createMarkup).toHaveBeenCalledWith(collabId, '') |
| 152 | + expect(result).toBe(mockMarkupRef) |
| 153 | + }) |
| 154 | + |
| 155 | + it('should handle collaborator errors', async () => { |
| 156 | + mockCollaborator.createMarkup.mockRejectedValue(new Error('Upload failed')) |
| 157 | + |
| 158 | + await expect(operations.uploadMarkup(objectClass, objectId, objectAttr, 'content', 'markup')).rejects.toThrow( |
| 159 | + 'Upload failed' |
| 160 | + ) |
| 161 | + }) |
| 162 | + }) |
| 163 | + |
| 164 | + describe('initialization', () => { |
| 165 | + it('should initialize collaborator client with correct parameters', () => { |
| 166 | + expect(getClient).toHaveBeenCalledWith(workspace, token, mockConfig.COLLABORATOR_URL) |
| 167 | + }) |
| 168 | + |
| 169 | + it('should handle different workspace IDs', () => { |
| 170 | + const differentWorkspace = 'different-workspace' as any |
| 171 | + createMarkupOperations(url, differentWorkspace, token, mockConfig) |
| 172 | + |
| 173 | + expect(getClient).toHaveBeenCalledWith(differentWorkspace, token, mockConfig.COLLABORATOR_URL) |
| 174 | + }) |
| 175 | + |
| 176 | + it('should handle different tokens', () => { |
| 177 | + const differentToken = 'different-token' |
| 178 | + createMarkupOperations(url, workspace, differentToken, mockConfig) |
| 179 | + |
| 180 | + expect(getClient).toHaveBeenCalledWith(workspace, differentToken, mockConfig.COLLABORATOR_URL) |
| 181 | + }) |
| 182 | + }) |
| 183 | +}) |
0 commit comments