|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import * as Y from 'yjs'; |
| 3 | + |
| 4 | +import { LinkReach, LinkRole, Role } from '../types'; |
| 5 | +import { |
| 6 | + base64ToBlocknoteXmlFragment, |
| 7 | + base64ToYDoc, |
| 8 | + currentDocRole, |
| 9 | + getDocLinkReach, |
| 10 | + getDocLinkRole, |
| 11 | + getEmojiAndTitle, |
| 12 | +} from '../utils'; |
| 13 | + |
| 14 | +// Mock Y.js |
| 15 | +vi.mock('yjs', () => ({ |
| 16 | + Doc: vi.fn().mockImplementation(() => ({ |
| 17 | + getXmlFragment: vi.fn().mockReturnValue('mocked-xml-fragment'), |
| 18 | + })), |
| 19 | + applyUpdate: vi.fn(), |
| 20 | +})); |
| 21 | + |
| 22 | +describe('doc-management utils', () => { |
| 23 | + beforeEach(() => { |
| 24 | + vi.clearAllMocks(); |
| 25 | + }); |
| 26 | + |
| 27 | + describe('currentDocRole', () => { |
| 28 | + it('should return OWNER when destroy ability is true', () => { |
| 29 | + const abilities = { |
| 30 | + destroy: true, |
| 31 | + accesses_manage: false, |
| 32 | + partial_update: false, |
| 33 | + } as any; |
| 34 | + |
| 35 | + const result = currentDocRole(abilities); |
| 36 | + |
| 37 | + expect(result).toBe(Role.OWNER); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should return ADMIN when accesses_manage ability is true and destroy is false', () => { |
| 41 | + const abilities = { |
| 42 | + destroy: false, |
| 43 | + accesses_manage: true, |
| 44 | + partial_update: false, |
| 45 | + } as any; |
| 46 | + |
| 47 | + const result = currentDocRole(abilities); |
| 48 | + |
| 49 | + expect(result).toBe(Role.ADMIN); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should return EDITOR when partial_update ability is true and higher abilities are false', () => { |
| 53 | + const abilities = { |
| 54 | + destroy: false, |
| 55 | + accesses_manage: false, |
| 56 | + partial_update: true, |
| 57 | + } as any; |
| 58 | + |
| 59 | + const result = currentDocRole(abilities); |
| 60 | + |
| 61 | + expect(result).toBe(Role.EDITOR); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should return READER when no higher abilities are true', () => { |
| 65 | + const abilities = { |
| 66 | + destroy: false, |
| 67 | + accesses_manage: false, |
| 68 | + partial_update: false, |
| 69 | + } as any; |
| 70 | + |
| 71 | + const result = currentDocRole(abilities); |
| 72 | + |
| 73 | + expect(result).toBe(Role.READER); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('base64ToYDoc', () => { |
| 78 | + it('should convert base64 string to Y.Doc', () => { |
| 79 | + const base64String = 'dGVzdA=='; // "test" in base64 |
| 80 | + const mockYDoc = { getXmlFragment: vi.fn() }; |
| 81 | + |
| 82 | + (Y.Doc as any).mockReturnValue(mockYDoc); |
| 83 | + |
| 84 | + const result = base64ToYDoc(base64String); |
| 85 | + |
| 86 | + expect(Y.Doc).toHaveBeenCalled(); |
| 87 | + expect(Y.applyUpdate).toHaveBeenCalledWith(mockYDoc, expect.any(Buffer)); |
| 88 | + expect(result).toBe(mockYDoc); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should handle empty base64 string', () => { |
| 92 | + const base64String = ''; |
| 93 | + const mockYDoc = { getXmlFragment: vi.fn() }; |
| 94 | + |
| 95 | + (Y.Doc as any).mockReturnValue(mockYDoc); |
| 96 | + |
| 97 | + const result = base64ToYDoc(base64String); |
| 98 | + |
| 99 | + expect(Y.Doc).toHaveBeenCalled(); |
| 100 | + expect(Y.applyUpdate).toHaveBeenCalledWith(mockYDoc, expect.any(Buffer)); |
| 101 | + expect(result).toBe(mockYDoc); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + describe('base64ToBlocknoteXmlFragment', () => { |
| 106 | + it('should convert base64 to Blocknote XML fragment', () => { |
| 107 | + const base64String = 'dGVzdA=='; |
| 108 | + const mockYDoc = { |
| 109 | + getXmlFragment: vi.fn().mockReturnValue('mocked-xml-fragment'), |
| 110 | + }; |
| 111 | + |
| 112 | + (Y.Doc as any).mockReturnValue(mockYDoc); |
| 113 | + |
| 114 | + const result = base64ToBlocknoteXmlFragment(base64String); |
| 115 | + |
| 116 | + expect(Y.Doc).toHaveBeenCalled(); |
| 117 | + expect(Y.applyUpdate).toHaveBeenCalledWith(mockYDoc, expect.any(Buffer)); |
| 118 | + expect(mockYDoc.getXmlFragment).toHaveBeenCalledWith('document-store'); |
| 119 | + expect(result).toBe('mocked-xml-fragment'); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('getDocLinkReach', () => { |
| 124 | + it('should return computed_link_reach when available', () => { |
| 125 | + const doc = { |
| 126 | + computed_link_reach: LinkReach.PUBLIC, |
| 127 | + link_reach: LinkReach.RESTRICTED, |
| 128 | + } as any; |
| 129 | + |
| 130 | + const result = getDocLinkReach(doc); |
| 131 | + |
| 132 | + expect(result).toBe(LinkReach.PUBLIC); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should fallback to link_reach when computed_link_reach is not available', () => { |
| 136 | + const doc = { |
| 137 | + link_reach: LinkReach.AUTHENTICATED, |
| 138 | + } as any; |
| 139 | + |
| 140 | + const result = getDocLinkReach(doc); |
| 141 | + |
| 142 | + expect(result).toBe(LinkReach.AUTHENTICATED); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should handle undefined computed_link_reach', () => { |
| 146 | + const doc = { |
| 147 | + computed_link_reach: undefined, |
| 148 | + link_reach: LinkReach.RESTRICTED, |
| 149 | + } as any; |
| 150 | + |
| 151 | + const result = getDocLinkReach(doc); |
| 152 | + |
| 153 | + expect(result).toBe(LinkReach.RESTRICTED); |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
| 157 | + describe('getDocLinkRole', () => { |
| 158 | + it('should return computed_link_role when available', () => { |
| 159 | + const doc = { |
| 160 | + computed_link_role: LinkRole.EDITOR, |
| 161 | + link_role: LinkRole.READER, |
| 162 | + } as any; |
| 163 | + |
| 164 | + const result = getDocLinkRole(doc); |
| 165 | + |
| 166 | + expect(result).toBe(LinkRole.EDITOR); |
| 167 | + }); |
| 168 | + |
| 169 | + it('should fallback to link_role when computed_link_role is not available', () => { |
| 170 | + const doc = { |
| 171 | + link_role: LinkRole.READER, |
| 172 | + } as any; |
| 173 | + |
| 174 | + const result = getDocLinkRole(doc); |
| 175 | + |
| 176 | + expect(result).toBe(LinkRole.READER); |
| 177 | + }); |
| 178 | + |
| 179 | + it('should handle undefined computed_link_role', () => { |
| 180 | + const doc = { |
| 181 | + computed_link_role: undefined, |
| 182 | + link_role: LinkRole.EDITOR, |
| 183 | + } as any; |
| 184 | + |
| 185 | + const result = getDocLinkRole(doc); |
| 186 | + |
| 187 | + expect(result).toBe(LinkRole.EDITOR); |
| 188 | + }); |
| 189 | + }); |
| 190 | + |
| 191 | + describe('getEmojiAndTitle', () => { |
| 192 | + it('should extract emoji and title when emoji is present at the beginning', () => { |
| 193 | + const title = '🚀 My Awesome Document'; |
| 194 | + |
| 195 | + const result = getEmojiAndTitle(title); |
| 196 | + |
| 197 | + expect(result.emoji).toBe('🚀'); |
| 198 | + expect(result.titleWithoutEmoji).toBe('My Awesome Document'); |
| 199 | + }); |
| 200 | + |
| 201 | + it('should handle complex emojis with modifiers', () => { |
| 202 | + const title = '👨💻 Developer Notes'; |
| 203 | + |
| 204 | + const result = getEmojiAndTitle(title); |
| 205 | + |
| 206 | + expect(result.emoji).toBe('👨💻'); |
| 207 | + expect(result.titleWithoutEmoji).toBe('Developer Notes'); |
| 208 | + }); |
| 209 | + |
| 210 | + it('should handle emojis with skin tone modifiers', () => { |
| 211 | + const title = '👍 Great Work!'; |
| 212 | + |
| 213 | + const result = getEmojiAndTitle(title); |
| 214 | + |
| 215 | + expect(result.emoji).toBe('👍'); |
| 216 | + expect(result.titleWithoutEmoji).toBe('Great Work!'); |
| 217 | + }); |
| 218 | + |
| 219 | + it('should return null emoji and full title when no emoji is present', () => { |
| 220 | + const title = 'Document Without Emoji'; |
| 221 | + |
| 222 | + const result = getEmojiAndTitle(title); |
| 223 | + |
| 224 | + expect(result.emoji).toBeNull(); |
| 225 | + expect(result.titleWithoutEmoji).toBe('Document Without Emoji'); |
| 226 | + }); |
| 227 | + |
| 228 | + it('should handle empty title', () => { |
| 229 | + const title = ''; |
| 230 | + |
| 231 | + const result = getEmojiAndTitle(title); |
| 232 | + |
| 233 | + expect(result.emoji).toBeNull(); |
| 234 | + expect(result.titleWithoutEmoji).toBe(''); |
| 235 | + }); |
| 236 | + |
| 237 | + it('should handle title with only emoji', () => { |
| 238 | + const title = '📝'; |
| 239 | + |
| 240 | + const result = getEmojiAndTitle(title); |
| 241 | + |
| 242 | + expect(result.emoji).toBe('📝'); |
| 243 | + expect(result.titleWithoutEmoji).toBe(''); |
| 244 | + }); |
| 245 | + |
| 246 | + it('should handle title with emoji in the middle (should not extract)', () => { |
| 247 | + const title = 'My 📝 Document'; |
| 248 | + |
| 249 | + const result = getEmojiAndTitle(title); |
| 250 | + |
| 251 | + expect(result.emoji).toBeNull(); |
| 252 | + expect(result.titleWithoutEmoji).toBe('My 📝 Document'); |
| 253 | + }); |
| 254 | + |
| 255 | + it('should handle title with multiple emojis at the beginning', () => { |
| 256 | + const title = '🚀📚 Project Documentation'; |
| 257 | + |
| 258 | + const result = getEmojiAndTitle(title); |
| 259 | + |
| 260 | + expect(result.emoji).toBe('🚀'); |
| 261 | + expect(result.titleWithoutEmoji).toBe('📚 Project Documentation'); |
| 262 | + }); |
| 263 | + }); |
| 264 | +}); |
0 commit comments