mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
at uiwork 412 lines 13 kB view raw
1import {RichText} from '@atproto/api' 2import { 3 getYoutubeVideoId, 4 makeRecordUri, 5 toNiceDomain, 6 toShortUrl, 7 toShareUrl, 8} from '../../src/lib/strings/url-helpers' 9import {pluralize, enforceLen} from '../../src/lib/strings/helpers' 10import {ago} from '../../src/lib/strings/time' 11import {detectLinkables} from '../../src/lib/strings/rich-text-detection' 12import {shortenLinks} from '../../src/lib/strings/rich-text-manip' 13import {makeValidHandle, createFullHandle} from '../../src/lib/strings/handles' 14import {cleanError} from '../../src/lib/strings/errors' 15 16describe('detectLinkables', () => { 17 const inputs = [ 18 'no linkable', 19 '@start middle end', 20 'start @middle end', 21 'start middle @end', 22 '@start @middle @end', 23 '@full123.test-of-chars', 24 'not@right', 25 '@bad!@#$chars', 26 '@newline1\n@newline2', 27 'parenthetical (@handle)', 28 'start https://middle.com end', 29 'start https://middle.com/foo/bar end', 30 'start https://middle.com/foo/bar?baz=bux end', 31 'start https://middle.com/foo/bar?baz=bux#hash end', 32 'https://start.com/foo/bar?baz=bux#hash middle end', 33 'start middle https://end.com/foo/bar?baz=bux#hash', 34 'https://newline1.com\nhttps://newline2.com', 35 'start middle.com end', 36 'start middle.com/foo/bar end', 37 'start middle.com/foo/bar?baz=bux end', 38 'start middle.com/foo/bar?baz=bux#hash end', 39 'start.com/foo/bar?baz=bux#hash middle end', 40 'start middle end.com/foo/bar?baz=bux#hash', 41 'newline1.com\nnewline2.com', 42 'not.. a..url ..here', 43 'e.g.', 44 'e.g. real.com fake.notreal', 45 'something-cool.jpg', 46 'website.com.jpg', 47 'e.g./foo', 48 'website.com.jpg/foo', 49 'Classic article https://socket3.wordpress.com/2018/02/03/designing-windows-95s-user-interface/', 50 'Classic article https://socket3.wordpress.com/2018/02/03/designing-windows-95s-user-interface/ ', 51 'https://foo.com https://bar.com/whatever https://baz.com', 52 'punctuation https://foo.com, https://bar.com/whatever; https://baz.com.', 53 'parenthetical (https://foo.com)', 54 'except for https://foo.com/thing_(cool)', 55 ] 56 const outputs = [ 57 ['no linkable'], 58 [{link: '@start'}, ' middle end'], 59 ['start ', {link: '@middle'}, ' end'], 60 ['start middle ', {link: '@end'}], 61 [{link: '@start'}, ' ', {link: '@middle'}, ' ', {link: '@end'}], 62 [{link: '@full123.test-of-chars'}], 63 ['not@right'], 64 [{link: '@bad'}, '!@#$chars'], 65 [{link: '@newline1'}, '\n', {link: '@newline2'}], 66 ['parenthetical (', {link: '@handle'}, ')'], 67 ['start ', {link: 'https://middle.com'}, ' end'], 68 ['start ', {link: 'https://middle.com/foo/bar'}, ' end'], 69 ['start ', {link: 'https://middle.com/foo/bar?baz=bux'}, ' end'], 70 ['start ', {link: 'https://middle.com/foo/bar?baz=bux#hash'}, ' end'], 71 [{link: 'https://start.com/foo/bar?baz=bux#hash'}, ' middle end'], 72 ['start middle ', {link: 'https://end.com/foo/bar?baz=bux#hash'}], 73 [{link: 'https://newline1.com'}, '\n', {link: 'https://newline2.com'}], 74 ['start ', {link: 'middle.com'}, ' end'], 75 ['start ', {link: 'middle.com/foo/bar'}, ' end'], 76 ['start ', {link: 'middle.com/foo/bar?baz=bux'}, ' end'], 77 ['start ', {link: 'middle.com/foo/bar?baz=bux#hash'}, ' end'], 78 [{link: 'start.com/foo/bar?baz=bux#hash'}, ' middle end'], 79 ['start middle ', {link: 'end.com/foo/bar?baz=bux#hash'}], 80 [{link: 'newline1.com'}, '\n', {link: 'newline2.com'}], 81 ['not.. a..url ..here'], 82 ['e.g.'], 83 ['e.g. ', {link: 'real.com'}, ' fake.notreal'], 84 ['something-cool.jpg'], 85 ['website.com.jpg'], 86 ['e.g./foo'], 87 ['website.com.jpg/foo'], 88 [ 89 'Classic article ', 90 { 91 link: 'https://socket3.wordpress.com/2018/02/03/designing-windows-95s-user-interface/', 92 }, 93 ], 94 [ 95 'Classic article ', 96 { 97 link: 'https://socket3.wordpress.com/2018/02/03/designing-windows-95s-user-interface/', 98 }, 99 ' ', 100 ], 101 [ 102 {link: 'https://foo.com'}, 103 ' ', 104 {link: 'https://bar.com/whatever'}, 105 ' ', 106 {link: 'https://baz.com'}, 107 ], 108 [ 109 'punctuation ', 110 {link: 'https://foo.com'}, 111 ', ', 112 {link: 'https://bar.com/whatever'}, 113 '; ', 114 {link: 'https://baz.com'}, 115 '.', 116 ], 117 ['parenthetical (', {link: 'https://foo.com'}, ')'], 118 ['except for ', {link: 'https://foo.com/thing_(cool)'}], 119 ] 120 it('correctly handles a set of text inputs', () => { 121 for (let i = 0; i < inputs.length; i++) { 122 const input = inputs[i] 123 const output = detectLinkables(input) 124 expect(output).toEqual(outputs[i]) 125 } 126 }) 127}) 128 129describe('pluralize', () => { 130 const inputs: [number, string, string?][] = [ 131 [1, 'follower'], 132 [1, 'member'], 133 [100, 'post'], 134 [1000, 'repost'], 135 [10000, 'upvote'], 136 [100000, 'other'], 137 [2, 'man', 'men'], 138 ] 139 const outputs = [ 140 'follower', 141 'member', 142 'posts', 143 'reposts', 144 'upvotes', 145 'others', 146 'men', 147 ] 148 149 it('correctly pluralizes a set of words', () => { 150 for (let i = 0; i < inputs.length; i++) { 151 const input = inputs[i] 152 const output = pluralize(...input) 153 expect(output).toEqual(outputs[i]) 154 } 155 }) 156}) 157 158describe('makeRecordUri', () => { 159 const inputs: [string, string, string][] = [ 160 ['alice.test', 'app.bsky.feed.post', '3jk7x4irgv52r'], 161 ] 162 const outputs = ['at://alice.test/app.bsky.feed.post/3jk7x4irgv52r'] 163 164 it('correctly builds a record URI', () => { 165 for (let i = 0; i < inputs.length; i++) { 166 const input = inputs[i] 167 const result = makeRecordUri(...input) 168 expect(result).toEqual(outputs[i]) 169 } 170 }) 171}) 172 173describe('ago', () => { 174 const inputs = [ 175 1671461038, 176 '04 Dec 1995 00:12:00 GMT', 177 new Date(), 178 new Date().setSeconds(new Date().getSeconds() - 10), 179 new Date().setMinutes(new Date().getMinutes() - 10), 180 new Date().setHours(new Date().getHours() - 1), 181 new Date().setDate(new Date().getDate() - 1), 182 new Date().setMonth(new Date().getMonth() - 1), 183 ] 184 const outputs = [ 185 new Date(1671461038).toLocaleDateString(), 186 new Date('04 Dec 1995 00:12:00 GMT').toLocaleDateString(), 187 'now', 188 '10s', 189 '10m', 190 '1h', 191 '1d', 192 '1mo', 193 ] 194 195 it('correctly calculates how much time passed, in a string', () => { 196 for (let i = 0; i < inputs.length; i++) { 197 const result = ago(inputs[i]) 198 expect(result).toEqual(outputs[i]) 199 } 200 }) 201}) 202 203describe('makeValidHandle', () => { 204 const inputs = [ 205 'test-handle-123', 206 'test!"#$%&/()=?_', 207 'this-handle-should-be-too-big', 208 ] 209 const outputs = ['test-handle-123', 'test', 'this-handle-should-b'] 210 211 it('correctly parses and corrects handles', () => { 212 for (let i = 0; i < inputs.length; i++) { 213 const result = makeValidHandle(inputs[i]) 214 expect(result).toEqual(outputs[i]) 215 } 216 }) 217}) 218 219describe('createFullHandle', () => { 220 const inputs: [string, string][] = [ 221 ['test-handle-123', 'test'], 222 ['.test.handle', 'test.test.'], 223 ['test.handle.', '.test.test'], 224 ] 225 const outputs = [ 226 'test-handle-123.test', 227 '.test.handle.test.test.', 228 'test.handle.test.test', 229 ] 230 231 it('correctly parses and corrects handles', () => { 232 for (let i = 0; i < inputs.length; i++) { 233 const input = inputs[i] 234 const result = createFullHandle(...input) 235 expect(result).toEqual(outputs[i]) 236 } 237 }) 238}) 239 240describe('enforceLen', () => { 241 const inputs: [string, number][] = [ 242 ['Hello World!', 5], 243 ['Hello World!', 20], 244 ['', 5], 245 ] 246 const outputs = ['Hello', 'Hello World!', ''] 247 248 it('correctly enforces defined length on a given string', () => { 249 for (let i = 0; i < inputs.length; i++) { 250 const input = inputs[i] 251 const result = enforceLen(...input) 252 expect(result).toEqual(outputs[i]) 253 } 254 }) 255}) 256 257describe('cleanError', () => { 258 const inputs = [ 259 'TypeError: Network request failed', 260 'Error: Aborted', 261 'Error: TypeError "x" is not a function', 262 'Error: SyntaxError unexpected token "export"', 263 'Some other error', 264 ] 265 const outputs = [ 266 'Unable to connect. Please check your internet connection and try again.', 267 'Unable to connect. Please check your internet connection and try again.', 268 'TypeError "x" is not a function', 269 'SyntaxError unexpected token "export"', 270 'Some other error', 271 ] 272 273 it('removes extra content from error message', () => { 274 for (let i = 0; i < inputs.length; i++) { 275 const result = cleanError(inputs[i]) 276 expect(result).toEqual(outputs[i]) 277 } 278 }) 279}) 280 281describe('toNiceDomain', () => { 282 const inputs = [ 283 'https://example.com/index.html', 284 'https://bsky.app', 285 'https://bsky.social', 286 '#123123123', 287 ] 288 const outputs = ['example.com', 'bsky.app', 'Bluesky Social', '#123123123'] 289 290 it("displays the url's host in a easily readable manner", () => { 291 for (let i = 0; i < inputs.length; i++) { 292 const result = toNiceDomain(inputs[i]) 293 expect(result).toEqual(outputs[i]) 294 } 295 }) 296}) 297 298describe('toShortUrl', () => { 299 const inputs = [ 300 'https://bsky.app', 301 'https://bsky.app/3jk7x4irgv52r', 302 'https://bsky.app/3jk7x4irgv52r2313y182h9', 303 'https://very-long-domain-name.com/foo', 304 'https://very-long-domain-name.com/foo?bar=baz#andsomemore', 305 ] 306 const outputs = [ 307 'bsky.app', 308 'bsky.app/3jk7x4irgv52r', 309 'bsky.app/3jk7x4irgv52...', 310 'very-long-domain-name.com/foo', 311 'very-long-domain-name.com/foo?bar=baz#...', 312 ] 313 314 it('shortens the url', () => { 315 for (let i = 0; i < inputs.length; i++) { 316 const result = toShortUrl(inputs[i]) 317 expect(result).toEqual(outputs[i]) 318 } 319 }) 320}) 321 322describe('toShareUrl', () => { 323 const inputs = ['https://bsky.app', '/3jk7x4irgv52r', 'item/test/123'] 324 const outputs = [ 325 'https://bsky.app', 326 'https://bsky.app/3jk7x4irgv52r', 327 'https://bsky.app/item/test/123', 328 ] 329 330 it('appends https, when not present', () => { 331 for (let i = 0; i < inputs.length; i++) { 332 const result = toShareUrl(inputs[i]) 333 expect(result).toEqual(outputs[i]) 334 } 335 }) 336}) 337 338describe('getYoutubeVideoId', () => { 339 it(' should return undefined for invalid youtube links', () => { 340 expect(getYoutubeVideoId('')).toBeUndefined() 341 expect(getYoutubeVideoId('https://www.google.com')).toBeUndefined() 342 expect(getYoutubeVideoId('https://www.youtube.com')).toBeUndefined() 343 expect( 344 getYoutubeVideoId('https://www.youtube.com/channelName'), 345 ).toBeUndefined() 346 expect( 347 getYoutubeVideoId('https://www.youtube.com/channel/channelName'), 348 ).toBeUndefined() 349 }) 350 351 it('getYoutubeVideoId should return video id for valid youtube links', () => { 352 expect(getYoutubeVideoId('https://www.youtube.com/watch?v=videoId')).toBe( 353 'videoId', 354 ) 355 expect( 356 getYoutubeVideoId( 357 'https://www.youtube.com/watch?v=videoId&feature=share', 358 ), 359 ).toBe('videoId') 360 expect(getYoutubeVideoId('https://youtu.be/videoId')).toBe('videoId') 361 }) 362}) 363 364describe('shortenLinks', () => { 365 const inputs = [ 366 'start https://middle.com/foo/bar?baz=bux#hash end', 367 'https://start.com/foo/bar?baz=bux#hash middle end', 368 'start middle https://end.com/foo/bar?baz=bux#hash', 369 'https://newline1.com/very/long/url/here\nhttps://newline2.com/very/long/url/here', 370 'Classic article https://socket3.wordpress.com/2018/02/03/designing-windows-95s-user-interface/', 371 ] 372 const outputs = [ 373 [ 374 'start middle.com/foo/bar?baz=... end', 375 ['https://middle.com/foo/bar?baz=bux#hash'], 376 ], 377 [ 378 'start.com/foo/bar?baz=... middle end', 379 ['https://start.com/foo/bar?baz=bux#hash'], 380 ], 381 [ 382 'start middle end.com/foo/bar?baz=...', 383 ['https://end.com/foo/bar?baz=bux#hash'], 384 ], 385 [ 386 'newline1.com/very/long/ur...\nnewline2.com/very/long/ur...', 387 [ 388 'https://newline1.com/very/long/url/here', 389 'https://newline2.com/very/long/url/here', 390 ], 391 ], 392 [ 393 'Classic article socket3.wordpress.com/2018/02/03/d...', 394 [ 395 'https://socket3.wordpress.com/2018/02/03/designing-windows-95s-user-interface/', 396 ], 397 ], 398 ] 399 it('correctly shortens rich text while preserving facet URIs', () => { 400 for (let i = 0; i < inputs.length; i++) { 401 const input = inputs[i] 402 const inputRT = new RichText({text: input}) 403 inputRT.detectFacetsWithoutResolution() 404 const outputRT = shortenLinks(inputRT) 405 expect(outputRT.text).toEqual(outputs[i][0]) 406 expect(outputRT.facets?.length).toEqual(outputs[i][1].length) 407 for (let j = 0; j < outputs[i][1].length; j++) { 408 expect(outputRT.facets![j].features[0].uri).toEqual(outputs[i][1][j]) 409 } 410 } 411 }) 412})