|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { |
| 3 | + extractExternalUrlAtOffset, |
| 4 | + findExternalUrlAtCoords, |
| 5 | + handleExternalLinkClick, |
| 6 | + handleExternalLinkMouseDown, |
| 7 | + isExternalLinkNavigationEnabled, |
| 8 | + isSupportedExternalUrl, |
| 9 | +} from '../externalLinks' |
| 10 | + |
| 11 | +const { invoke } = vi.hoisted(() => ({ |
| 12 | + invoke: vi.fn(async () => undefined), |
| 13 | +})) |
| 14 | + |
| 15 | +vi.mock('@/electron', () => ({ |
| 16 | + ipc: { |
| 17 | + invoke, |
| 18 | + }, |
| 19 | +})) |
| 20 | + |
| 21 | +vi.mock('@/utils', () => ({ |
| 22 | + isMac: true, |
| 23 | +})) |
| 24 | + |
| 25 | +function createView(lineText: string, pos = 3) { |
| 26 | + return { |
| 27 | + posAtCoords: vi.fn(() => pos), |
| 28 | + state: { |
| 29 | + doc: { |
| 30 | + lineAt: vi.fn(() => ({ |
| 31 | + from: 0, |
| 32 | + text: lineText, |
| 33 | + })), |
| 34 | + }, |
| 35 | + }, |
| 36 | + } as any |
| 37 | +} |
| 38 | + |
| 39 | +beforeEach(() => { |
| 40 | + vi.clearAllMocks() |
| 41 | +}) |
| 42 | + |
| 43 | +describe('extractExternalUrlAtOffset', () => { |
| 44 | + it('extracts markdown link url when clicking inside the label', () => { |
| 45 | + expect( |
| 46 | + extractExternalUrlAtOffset('[Docs](https://masscode.io/docs)', 3), |
| 47 | + ).toBe('https://masscode.io/docs') |
| 48 | + }) |
| 49 | + |
| 50 | + it('extracts plain url when clicking inside the url text', () => { |
| 51 | + expect( |
| 52 | + extractExternalUrlAtOffset('Docs https://masscode.io/docs now', 12), |
| 53 | + ).toBe('https://masscode.io/docs') |
| 54 | + }) |
| 55 | + |
| 56 | + it('returns null when offset is outside a link', () => { |
| 57 | + expect( |
| 58 | + extractExternalUrlAtOffset('Docs https://masscode.io/docs now', 1), |
| 59 | + ).toBeNull() |
| 60 | + }) |
| 61 | +}) |
| 62 | + |
| 63 | +describe('isSupportedExternalUrl', () => { |
| 64 | + it('accepts supported protocols', () => { |
| 65 | + expect(isSupportedExternalUrl('https://masscode.io')).toBe(true) |
| 66 | + expect(isSupportedExternalUrl('http://masscode.io')).toBe(true) |
| 67 | + expect(isSupportedExternalUrl('masscode://notes-asset/example.png')).toBe( |
| 68 | + true, |
| 69 | + ) |
| 70 | + }) |
| 71 | + |
| 72 | + it('rejects unsupported protocols', () => { |
| 73 | + expect(isSupportedExternalUrl('mailto:test@masscode.io')).toBe(false) |
| 74 | + expect(isSupportedExternalUrl('/notes/example.md')).toBe(false) |
| 75 | + }) |
| 76 | +}) |
| 77 | + |
| 78 | +describe('isExternalLinkNavigationEnabled', () => { |
| 79 | + it('keeps external links clickable in live preview', () => { |
| 80 | + expect(isExternalLinkNavigationEnabled('livePreview')).toBe(true) |
| 81 | + }) |
| 82 | + |
| 83 | + it('keeps external links clickable in preview', () => { |
| 84 | + expect(isExternalLinkNavigationEnabled('preview')).toBe(true) |
| 85 | + }) |
| 86 | + |
| 87 | + it('does not enable navigation in raw mode', () => { |
| 88 | + expect(isExternalLinkNavigationEnabled('raw')).toBe(false) |
| 89 | + }) |
| 90 | +}) |
| 91 | + |
| 92 | +describe('findExternalUrlAtCoords', () => { |
| 93 | + it('returns supported url for the clicked position', () => { |
| 94 | + expect( |
| 95 | + findExternalUrlAtCoords(createView('[Docs](https://masscode.io/docs)'), { |
| 96 | + x: 20, |
| 97 | + y: 10, |
| 98 | + }), |
| 99 | + ).toBe('https://masscode.io/docs') |
| 100 | + }) |
| 101 | + |
| 102 | + it('returns null when clicked text is not an external url', () => { |
| 103 | + expect( |
| 104 | + findExternalUrlAtCoords(createView('[[Internal note]]'), { |
| 105 | + x: 20, |
| 106 | + y: 10, |
| 107 | + }), |
| 108 | + ).toBeNull() |
| 109 | + }) |
| 110 | +}) |
| 111 | + |
| 112 | +describe('external link mouse handlers', () => { |
| 113 | + it('opens the external url on cmd+mousedown', () => { |
| 114 | + const preventDefault = vi.fn() |
| 115 | + |
| 116 | + const handled = handleExternalLinkMouseDown( |
| 117 | + createView('[Docs](https://masscode.io/docs)'), |
| 118 | + { |
| 119 | + clientX: 10, |
| 120 | + clientY: 20, |
| 121 | + ctrlKey: false, |
| 122 | + metaKey: true, |
| 123 | + preventDefault, |
| 124 | + } as unknown as MouseEvent, |
| 125 | + ) |
| 126 | + |
| 127 | + expect(handled).toBe(true) |
| 128 | + expect(preventDefault).toHaveBeenCalledTimes(1) |
| 129 | + expect(invoke).toHaveBeenCalledWith( |
| 130 | + 'system:open-external', |
| 131 | + 'https://masscode.io/docs', |
| 132 | + ) |
| 133 | + }) |
| 134 | + |
| 135 | + it('does not open the external url on plain mousedown', () => { |
| 136 | + const preventDefault = vi.fn() |
| 137 | + |
| 138 | + const handled = handleExternalLinkMouseDown( |
| 139 | + createView('[Docs](https://masscode.io/docs)'), |
| 140 | + { |
| 141 | + clientX: 10, |
| 142 | + clientY: 20, |
| 143 | + ctrlKey: false, |
| 144 | + metaKey: false, |
| 145 | + preventDefault, |
| 146 | + } as unknown as MouseEvent, |
| 147 | + ) |
| 148 | + |
| 149 | + expect(handled).toBe(false) |
| 150 | + expect(preventDefault).not.toHaveBeenCalled() |
| 151 | + expect(invoke).not.toHaveBeenCalled() |
| 152 | + }) |
| 153 | + |
| 154 | + it('suppresses the follow-up click for cmd+click', () => { |
| 155 | + const preventDefault = vi.fn() |
| 156 | + |
| 157 | + const handled = handleExternalLinkClick( |
| 158 | + createView('[Docs](https://masscode.io/docs)'), |
| 159 | + { |
| 160 | + clientX: 10, |
| 161 | + clientY: 20, |
| 162 | + ctrlKey: false, |
| 163 | + metaKey: true, |
| 164 | + preventDefault, |
| 165 | + } as unknown as MouseEvent, |
| 166 | + ) |
| 167 | + |
| 168 | + expect(handled).toBe(true) |
| 169 | + expect(preventDefault).toHaveBeenCalledTimes(1) |
| 170 | + expect(invoke).not.toHaveBeenCalled() |
| 171 | + }) |
| 172 | + |
| 173 | + it('does not suppress plain click on external link', () => { |
| 174 | + const preventDefault = vi.fn() |
| 175 | + |
| 176 | + const handled = handleExternalLinkClick( |
| 177 | + createView('[Docs](https://masscode.io/docs)'), |
| 178 | + { |
| 179 | + clientX: 10, |
| 180 | + clientY: 20, |
| 181 | + ctrlKey: false, |
| 182 | + metaKey: false, |
| 183 | + preventDefault, |
| 184 | + } as unknown as MouseEvent, |
| 185 | + ) |
| 186 | + |
| 187 | + expect(handled).toBe(false) |
| 188 | + expect(preventDefault).not.toHaveBeenCalled() |
| 189 | + expect(invoke).not.toHaveBeenCalled() |
| 190 | + }) |
| 191 | +}) |
0 commit comments