|
| 1 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 2 | +import { |
| 3 | + getMatrixToRoom, |
| 4 | + getMatrixToRoomEvent, |
| 5 | + getMatrixToUser, |
| 6 | + parseMatrixToRoom, |
| 7 | + parseMatrixToRoomEvent, |
| 8 | + parseMatrixToUser, |
| 9 | + setMatrixToBase, |
| 10 | + testMatrixTo, |
| 11 | +} from './matrix-to'; |
| 12 | + |
| 13 | +// Reset to default after each test so state doesn't leak between tests. |
| 14 | +afterEach(() => { |
| 15 | + setMatrixToBase(undefined); |
| 16 | +}); |
| 17 | + |
| 18 | +// --------------------------------------------------------------------------- |
| 19 | +// Link generation |
| 20 | +// --------------------------------------------------------------------------- |
| 21 | + |
| 22 | +describe('getMatrixToUser', () => { |
| 23 | + it('generates a standard matrix.to user link', () => { |
| 24 | + expect(getMatrixToUser('@alice:example.com')).toBe('https://matrix.to/#/@alice:example.com'); |
| 25 | + }); |
| 26 | + |
| 27 | + it('uses custom base when configured', () => { |
| 28 | + setMatrixToBase('https://matrix.example.org'); |
| 29 | + expect(getMatrixToUser('@alice:example.com')).toBe( |
| 30 | + 'https://matrix.example.org/#/@alice:example.com' |
| 31 | + ); |
| 32 | + }); |
| 33 | + |
| 34 | + it('strips trailing slash from custom base', () => { |
| 35 | + setMatrixToBase('https://matrix.example.org/'); |
| 36 | + expect(getMatrixToUser('@alice:example.com')).toBe( |
| 37 | + 'https://matrix.example.org/#/@alice:example.com' |
| 38 | + ); |
| 39 | + }); |
| 40 | +}); |
| 41 | + |
| 42 | +describe('getMatrixToRoom', () => { |
| 43 | + it('generates a standard matrix.to room link', () => { |
| 44 | + expect(getMatrixToRoom('!room:example.com')).toBe('https://matrix.to/#/!room:example.com'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('appends via servers', () => { |
| 48 | + expect(getMatrixToRoom('!room:example.com', ['s1.org', 's2.org'])).toBe( |
| 49 | + 'https://matrix.to/#/!room:example.com?via=s1.org&via=s2.org' |
| 50 | + ); |
| 51 | + }); |
| 52 | + |
| 53 | + it('uses custom base when configured', () => { |
| 54 | + setMatrixToBase('https://matrix.example.org'); |
| 55 | + expect(getMatrixToRoom('#general:example.com')).toBe( |
| 56 | + 'https://matrix.example.org/#/#general:example.com' |
| 57 | + ); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +describe('getMatrixToRoomEvent', () => { |
| 62 | + it('generates a standard matrix.to event link', () => { |
| 63 | + expect(getMatrixToRoomEvent('!room:example.com', '$event123')).toBe( |
| 64 | + 'https://matrix.to/#/!room:example.com/$event123' |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + it('appends via servers', () => { |
| 69 | + expect(getMatrixToRoomEvent('!room:example.com', '$event123', ['s1.org'])).toBe( |
| 70 | + 'https://matrix.to/#/!room:example.com/$event123?via=s1.org' |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + it('uses custom base when configured', () => { |
| 75 | + setMatrixToBase('https://matrix.example.org'); |
| 76 | + expect(getMatrixToRoomEvent('!room:example.com', '$event123')).toBe( |
| 77 | + 'https://matrix.example.org/#/!room:example.com/$event123' |
| 78 | + ); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +// --------------------------------------------------------------------------- |
| 83 | +// testMatrixTo |
| 84 | +// --------------------------------------------------------------------------- |
| 85 | + |
| 86 | +describe('testMatrixTo', () => { |
| 87 | + it('matches standard matrix.to URLs', () => { |
| 88 | + expect(testMatrixTo('https://matrix.to/#/@alice:example.com')).toBe(true); |
| 89 | + expect(testMatrixTo('https://matrix.to/#/!room:example.com')).toBe(true); |
| 90 | + expect(testMatrixTo('https://matrix.to/#/!room:example.com/$event')).toBe(true); |
| 91 | + expect(testMatrixTo('http://matrix.to/#/@alice:example.com')).toBe(true); |
| 92 | + }); |
| 93 | + |
| 94 | + it('rejects non-matrix.to URLs', () => { |
| 95 | + expect(testMatrixTo('https://example.com')).toBe(false); |
| 96 | + expect(testMatrixTo('https://notmatrix.to/#/@alice:example.com')).toBe(false); |
| 97 | + }); |
| 98 | + |
| 99 | + it('matches custom base URLs after setMatrixToBase', () => { |
| 100 | + setMatrixToBase('https://matrix.example.org'); |
| 101 | + expect(testMatrixTo('https://matrix.example.org/#/@alice:example.com')).toBe(true); |
| 102 | + }); |
| 103 | + |
| 104 | + it('still matches standard matrix.to after setMatrixToBase (cross-client compat)', () => { |
| 105 | + setMatrixToBase('https://matrix.example.org'); |
| 106 | + expect(testMatrixTo('https://matrix.to/#/@alice:example.com')).toBe(true); |
| 107 | + }); |
| 108 | +}); |
| 109 | + |
| 110 | +// --------------------------------------------------------------------------- |
| 111 | +// parseMatrixToUser |
| 112 | +// --------------------------------------------------------------------------- |
| 113 | + |
| 114 | +describe('parseMatrixToUser', () => { |
| 115 | + it('parses a standard matrix.to user link', () => { |
| 116 | + expect(parseMatrixToUser('https://matrix.to/#/@alice:example.com')).toBe('@alice:example.com'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('returns undefined for non-user links', () => { |
| 120 | + expect(parseMatrixToUser('https://matrix.to/#/!room:example.com')).toBeUndefined(); |
| 121 | + }); |
| 122 | + |
| 123 | + it('parses user links from custom base', () => { |
| 124 | + setMatrixToBase('https://matrix.example.org'); |
| 125 | + expect(parseMatrixToUser('https://matrix.example.org/#/@alice:example.com')).toBe( |
| 126 | + '@alice:example.com' |
| 127 | + ); |
| 128 | + }); |
| 129 | + |
| 130 | + it('parses standard matrix.to user links even after custom base is set', () => { |
| 131 | + setMatrixToBase('https://matrix.example.org'); |
| 132 | + expect(parseMatrixToUser('https://matrix.to/#/@alice:example.com')).toBe('@alice:example.com'); |
| 133 | + }); |
| 134 | +}); |
| 135 | + |
| 136 | +// --------------------------------------------------------------------------- |
| 137 | +// parseMatrixToRoom |
| 138 | +// --------------------------------------------------------------------------- |
| 139 | + |
| 140 | +describe('parseMatrixToRoom', () => { |
| 141 | + it('parses a room ID link', () => { |
| 142 | + expect(parseMatrixToRoom('https://matrix.to/#/!room:example.com')).toEqual({ |
| 143 | + roomIdOrAlias: '!room:example.com', |
| 144 | + viaServers: undefined, |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + it('parses a room alias link', () => { |
| 149 | + expect(parseMatrixToRoom('https://matrix.to/#/#general:example.com')).toEqual({ |
| 150 | + roomIdOrAlias: '#general:example.com', |
| 151 | + viaServers: undefined, |
| 152 | + }); |
| 153 | + }); |
| 154 | + |
| 155 | + it('parses via servers', () => { |
| 156 | + expect( |
| 157 | + parseMatrixToRoom('https://matrix.to/#/!room:example.com?via=s1.org&via=s2.org') |
| 158 | + ).toEqual({ |
| 159 | + roomIdOrAlias: '!room:example.com', |
| 160 | + viaServers: ['s1.org', 's2.org'], |
| 161 | + }); |
| 162 | + }); |
| 163 | + |
| 164 | + it('returns undefined for event links (too many segments)', () => { |
| 165 | + expect(parseMatrixToRoom('https://matrix.to/#/!room:example.com/$event123')).toBeUndefined(); |
| 166 | + }); |
| 167 | + |
| 168 | + it('parses room links from custom base', () => { |
| 169 | + setMatrixToBase('https://matrix.example.org'); |
| 170 | + expect(parseMatrixToRoom('https://matrix.example.org/#/!room:example.com')).toEqual({ |
| 171 | + roomIdOrAlias: '!room:example.com', |
| 172 | + viaServers: undefined, |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + it('still parses standard matrix.to room links after custom base is set', () => { |
| 177 | + setMatrixToBase('https://matrix.example.org'); |
| 178 | + expect(parseMatrixToRoom('https://matrix.to/#/!room:example.com')).toEqual({ |
| 179 | + roomIdOrAlias: '!room:example.com', |
| 180 | + viaServers: undefined, |
| 181 | + }); |
| 182 | + }); |
| 183 | +}); |
| 184 | + |
| 185 | +// --------------------------------------------------------------------------- |
| 186 | +// parseMatrixToRoomEvent |
| 187 | +// --------------------------------------------------------------------------- |
| 188 | + |
| 189 | +describe('parseMatrixToRoomEvent', () => { |
| 190 | + it('parses a room event link', () => { |
| 191 | + expect(parseMatrixToRoomEvent('https://matrix.to/#/!room:example.com/$event123')).toEqual({ |
| 192 | + roomIdOrAlias: '!room:example.com', |
| 193 | + eventId: '$event123', |
| 194 | + viaServers: undefined, |
| 195 | + }); |
| 196 | + }); |
| 197 | + |
| 198 | + it('parses via servers', () => { |
| 199 | + expect( |
| 200 | + parseMatrixToRoomEvent('https://matrix.to/#/!room:example.com/$event123?via=s1.org') |
| 201 | + ).toEqual({ |
| 202 | + roomIdOrAlias: '!room:example.com', |
| 203 | + eventId: '$event123', |
| 204 | + viaServers: ['s1.org'], |
| 205 | + }); |
| 206 | + }); |
| 207 | + |
| 208 | + it('returns undefined for room-only links', () => { |
| 209 | + expect(parseMatrixToRoomEvent('https://matrix.to/#/!room:example.com')).toBeUndefined(); |
| 210 | + }); |
| 211 | + |
| 212 | + it('parses event links from custom base', () => { |
| 213 | + setMatrixToBase('https://matrix.example.org'); |
| 214 | + expect( |
| 215 | + parseMatrixToRoomEvent('https://matrix.example.org/#/!room:example.com/$event123') |
| 216 | + ).toEqual({ |
| 217 | + roomIdOrAlias: '!room:example.com', |
| 218 | + eventId: '$event123', |
| 219 | + viaServers: undefined, |
| 220 | + }); |
| 221 | + }); |
| 222 | + |
| 223 | + it('still parses standard matrix.to event links after custom base is set', () => { |
| 224 | + setMatrixToBase('https://matrix.example.org'); |
| 225 | + expect(parseMatrixToRoomEvent('https://matrix.to/#/!room:example.com/$event123')).toEqual({ |
| 226 | + roomIdOrAlias: '!room:example.com', |
| 227 | + eventId: '$event123', |
| 228 | + viaServers: undefined, |
| 229 | + }); |
| 230 | + }); |
| 231 | +}); |
0 commit comments