|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | + |
| 3 | +vi.mock('@superdoc/core/collaboration/helpers.js', () => ({ |
| 4 | + syncCommentsToClients: vi.fn(), |
| 5 | +})); |
| 6 | + |
| 7 | +import useComment from './use-comment.js'; |
| 8 | +import { syncCommentsToClients } from '@superdoc/core/collaboration/helpers.js'; |
| 9 | + |
| 10 | +const makeSuperdoc = () => ({ |
| 11 | + emit: vi.fn(), |
| 12 | + activeEditor: { |
| 13 | + commands: { |
| 14 | + resolveComment: vi.fn(), |
| 15 | + setCommentInternal: vi.fn(), |
| 16 | + setActiveComment: vi.fn(), |
| 17 | + }, |
| 18 | + }, |
| 19 | +}); |
| 20 | + |
| 21 | +describe('use-comment: extended coverage', () => { |
| 22 | + beforeEach(() => { |
| 23 | + syncCommentsToClients.mockClear(); |
| 24 | + }); |
| 25 | + |
| 26 | + describe('resolveComment', () => { |
| 27 | + it('sets resolved fields and emits a resolved event', () => { |
| 28 | + const c = useComment({ commentId: 'c-1', fileId: 'doc-1' }); |
| 29 | + const superdoc = makeSuperdoc(); |
| 30 | + c.resolveComment({ email: 'a@b.com', name: 'Alice', superdoc }); |
| 31 | + expect(c.resolvedByEmail).toBe('a@b.com'); |
| 32 | + expect(c.resolvedByName).toBe('Alice'); |
| 33 | + expect(typeof c.resolvedTime).toBe('number'); |
| 34 | + expect(superdoc.emit).toHaveBeenCalledWith( |
| 35 | + 'comments-update', |
| 36 | + expect.objectContaining({ type: expect.any(String) }), |
| 37 | + ); |
| 38 | + expect(superdoc.activeEditor.commands.resolveComment).toHaveBeenCalled(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('is a no-op when already resolved', () => { |
| 42 | + const c = useComment({ commentId: 'c-1', resolvedTime: 1234 }); |
| 43 | + const superdoc = makeSuperdoc(); |
| 44 | + c.resolveComment({ email: 'a@b.com', name: 'Alice', superdoc }); |
| 45 | + expect(c.resolvedByEmail).toBeNull(); |
| 46 | + expect(superdoc.emit).not.toHaveBeenCalled(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('emits when tracked change is present (suggestion resolve path)', () => { |
| 50 | + const c = useComment({ commentId: 'c-1', trackedChange: { insert: {} } }); |
| 51 | + const superdoc = makeSuperdoc(); |
| 52 | + c.resolveComment({ email: 'a@b.com', name: 'Alice', superdoc }); |
| 53 | + expect(superdoc.emit).toHaveBeenCalled(); |
| 54 | + expect(superdoc.activeEditor.commands.resolveComment).toHaveBeenCalled(); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('setIsInternal', () => { |
| 59 | + it('updates the flag and emits', () => { |
| 60 | + const c = useComment({ commentId: 'c-1', isInternal: true }); |
| 61 | + const superdoc = makeSuperdoc(); |
| 62 | + c.setIsInternal({ isInternal: false, superdoc }); |
| 63 | + expect(c.isInternal).toBe(false); |
| 64 | + expect(superdoc.emit).toHaveBeenCalled(); |
| 65 | + expect(superdoc.activeEditor.commands.setCommentInternal).toHaveBeenCalledWith( |
| 66 | + expect.objectContaining({ isInternal: false }), |
| 67 | + ); |
| 68 | + }); |
| 69 | + |
| 70 | + it('short-circuits when value is unchanged', () => { |
| 71 | + const c = useComment({ commentId: 'c-1', isInternal: true }); |
| 72 | + const superdoc = makeSuperdoc(); |
| 73 | + c.setIsInternal({ isInternal: true, superdoc }); |
| 74 | + expect(superdoc.emit).not.toHaveBeenCalled(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('does not call editor commands when activeEditor is missing', () => { |
| 78 | + const c = useComment({ commentId: 'c-1', isInternal: true }); |
| 79 | + const superdoc = { emit: vi.fn(), activeEditor: null }; |
| 80 | + c.setIsInternal({ isInternal: false, superdoc }); |
| 81 | + expect(superdoc.emit).toHaveBeenCalled(); |
| 82 | + expect(c.isInternal).toBe(false); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + describe('setActive', () => { |
| 87 | + it('invokes setActiveComment on the active editor', () => { |
| 88 | + const c = useComment({ commentId: 'c-1' }); |
| 89 | + const superdoc = makeSuperdoc(); |
| 90 | + c.setActive(superdoc); |
| 91 | + expect(superdoc.activeEditor.commands.setActiveComment).toHaveBeenCalledWith( |
| 92 | + expect.objectContaining({ commentId: 'c-1' }), |
| 93 | + ); |
| 94 | + }); |
| 95 | + |
| 96 | + it('is a no-op when no active editor', () => { |
| 97 | + const c = useComment({ commentId: 'c-1' }); |
| 98 | + expect(() => c.setActive({ activeEditor: null })).not.toThrow(); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + describe('setText', () => { |
| 103 | + it('updates comment text and extracts mentions', () => { |
| 104 | + const c = useComment({ commentId: 'c-1' }); |
| 105 | + const superdoc = makeSuperdoc(); |
| 106 | + c.setText({ |
| 107 | + text: |
| 108 | + 'Hello <span data-type="mention" email="a@b.com" name="Alice"></span> and ' + |
| 109 | + '<span data-type="mention" email="c@d.com" name="Carol"></span>', |
| 110 | + superdoc, |
| 111 | + }); |
| 112 | + expect(c.commentText).toContain('Hello'); |
| 113 | + expect(c.mentions).toHaveLength(2); |
| 114 | + expect(c.mentions[0]).toMatchObject({ email: 'a@b.com', name: 'Alice' }); |
| 115 | + expect(superdoc.emit).toHaveBeenCalled(); |
| 116 | + }); |
| 117 | + |
| 118 | + it('deduplicates repeated mentions by email + name', () => { |
| 119 | + const c = useComment({ commentId: 'c-1' }); |
| 120 | + const superdoc = makeSuperdoc(); |
| 121 | + c.setText({ |
| 122 | + text: |
| 123 | + '<span data-type="mention" email="a@b.com" name="Alice"></span>' + |
| 124 | + '<span data-type="mention" email="a@b.com" name="Alice"></span>', |
| 125 | + superdoc, |
| 126 | + }); |
| 127 | + expect(c.mentions).toHaveLength(1); |
| 128 | + }); |
| 129 | + |
| 130 | + it('skips emit when suppressUpdate is true', () => { |
| 131 | + const c = useComment({ commentId: 'c-1' }); |
| 132 | + const superdoc = makeSuperdoc(); |
| 133 | + c.setText({ text: 'silent update', superdoc, suppressUpdate: true }); |
| 134 | + expect(c.commentText).toBe('silent update'); |
| 135 | + expect(superdoc.emit).not.toHaveBeenCalled(); |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + describe('updatePosition', () => { |
| 140 | + it('translates coords relative to the parent bounding rect', () => { |
| 141 | + const c = useComment({ |
| 142 | + commentId: 'c-1', |
| 143 | + selection: { documentId: 'doc-1', selectionBounds: { top: 0, left: 0 } }, |
| 144 | + }); |
| 145 | + const parent = { getBoundingClientRect: () => ({ top: 50, left: 0 }) }; |
| 146 | + c.updatePosition({ top: 100, left: 10, right: 20, bottom: 150 }, parent); |
| 147 | + expect(c.selection.selectionBounds).toEqual({ |
| 148 | + top: 50, |
| 149 | + left: 10, |
| 150 | + right: 20, |
| 151 | + bottom: 100, |
| 152 | + }); |
| 153 | + expect(c.selection.source).toBe('super-editor'); |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
| 157 | + describe('getCommentUser', () => { |
| 158 | + it('returns imported author when present', () => { |
| 159 | + const c = useComment({ |
| 160 | + commentId: 'c-1', |
| 161 | + importedAuthor: { name: 'Imported One', email: 'imp@x.com' }, |
| 162 | + }); |
| 163 | + expect(c.getCommentUser()).toEqual({ name: 'Imported One', email: 'imp@x.com' }); |
| 164 | + }); |
| 165 | + |
| 166 | + it('uses fallback "(Imported)" when importedAuthor.name is missing', () => { |
| 167 | + const c = useComment({ |
| 168 | + commentId: 'c-1', |
| 169 | + importedAuthor: { email: 'imp@x.com' }, |
| 170 | + }); |
| 171 | + expect(c.getCommentUser()).toEqual({ name: '(Imported)', email: 'imp@x.com' }); |
| 172 | + }); |
| 173 | + |
| 174 | + it('returns creator info when no imported author', () => { |
| 175 | + const c = useComment({ |
| 176 | + commentId: 'c-1', |
| 177 | + creatorName: 'Alice', |
| 178 | + creatorEmail: 'a@b.com', |
| 179 | + creatorImage: '/a.png', |
| 180 | + }); |
| 181 | + expect(c.getCommentUser()).toEqual({ |
| 182 | + name: 'Alice', |
| 183 | + email: 'a@b.com', |
| 184 | + image: '/a.png', |
| 185 | + }); |
| 186 | + }); |
| 187 | + }); |
| 188 | + |
| 189 | + describe('getValues', () => { |
| 190 | + it('maps mentions to fall back to email when name is missing', () => { |
| 191 | + const c = useComment({ commentId: 'c-1' }); |
| 192 | + const superdoc = makeSuperdoc(); |
| 193 | + c.setText({ |
| 194 | + text: '<span data-type="mention" email="x@y.com"></span>', |
| 195 | + superdoc, |
| 196 | + suppressUpdate: true, |
| 197 | + }); |
| 198 | + const v = c.getValues(); |
| 199 | + expect(v.mentions[0]).toEqual({ email: 'x@y.com', name: 'x@y.com' }); |
| 200 | + }); |
| 201 | + |
| 202 | + it('returns selection.getValues() when selection was provided', () => { |
| 203 | + const c = useComment({ |
| 204 | + commentId: 'c-1', |
| 205 | + selection: { documentId: 'doc-1', page: 2, source: 'superdoc' }, |
| 206 | + }); |
| 207 | + const v = c.getValues(); |
| 208 | + expect(v.selection.documentId).toBe('doc-1'); |
| 209 | + expect(v.selection.page).toBe(2); |
| 210 | + }); |
| 211 | + |
| 212 | + it('falls back to synthetic selection when none provided', () => { |
| 213 | + const c = useComment({ commentId: 'c-1', fileId: 'doc-1' }); |
| 214 | + const v = c.getValues(); |
| 215 | + expect(v.selection).toBeDefined(); |
| 216 | + expect(v.selection.documentId).toBe('doc-1'); |
| 217 | + }); |
| 218 | + }); |
| 219 | +}); |
0 commit comments