mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import {RichText} from '@atproto/api'
2
3import {parseEmbedPlayerFromUrl} from 'lib/strings/embed-player'
4import {
5 createStarterPackGooglePlayUri,
6 createStarterPackLinkFromAndroidReferrer,
7 parseStarterPackUri,
8} from 'lib/strings/starter-pack'
9import {cleanError} from '../../src/lib/strings/errors'
10import {createFullHandle, makeValidHandle} from '../../src/lib/strings/handles'
11import {enforceLen} from '../../src/lib/strings/helpers'
12import {detectLinkables} from '../../src/lib/strings/rich-text-detection'
13import {shortenLinks} from '../../src/lib/strings/rich-text-manip'
14import {
15 makeRecordUri,
16 toNiceDomain,
17 toShareUrl,
18 toShortUrl,
19} from '../../src/lib/strings/url-helpers'
20
21describe('detectLinkables', () => {
22 const inputs = [
23 'no linkable',
24 '@start middle end',
25 'start @middle end',
26 'start middle @end',
27 '@start @middle @end',
28 '@full123.test-of-chars',
29 'not@right',
30 '@bad!@#$chars',
31 '@newline1\n@newline2',
32 'parenthetical (@handle)',
33 'start https://middle.com end',
34 'start https://middle.com/foo/bar end',
35 'start https://middle.com/foo/bar?baz=bux end',
36 'start https://middle.com/foo/bar?baz=bux#hash end',
37 'https://start.com/foo/bar?baz=bux#hash middle end',
38 'start middle https://end.com/foo/bar?baz=bux#hash',
39 'https://newline1.com\nhttps://newline2.com',
40 'start middle.com end',
41 'start middle.com/foo/bar end',
42 'start middle.com/foo/bar?baz=bux end',
43 'start middle.com/foo/bar?baz=bux#hash end',
44 'start.com/foo/bar?baz=bux#hash middle end',
45 'start middle end.com/foo/bar?baz=bux#hash',
46 'newline1.com\nnewline2.com',
47 'not.. a..url ..here',
48 'e.g.',
49 'e.g. real.com fake.notreal',
50 'something-cool.jpg',
51 'website.com.jpg',
52 'e.g./foo',
53 'website.com.jpg/foo',
54 'Classic article https://socket3.wordpress.com/2018/02/03/designing-windows-95s-user-interface/',
55 'Classic article https://socket3.wordpress.com/2018/02/03/designing-windows-95s-user-interface/ ',
56 'https://foo.com https://bar.com/whatever https://baz.com',
57 'punctuation https://foo.com, https://bar.com/whatever; https://baz.com.',
58 'parenthetical (https://foo.com)',
59 'except for https://foo.com/thing_(cool)',
60 ]
61 const outputs = [
62 ['no linkable'],
63 [{link: '@start'}, ' middle end'],
64 ['start ', {link: '@middle'}, ' end'],
65 ['start middle ', {link: '@end'}],
66 [{link: '@start'}, ' ', {link: '@middle'}, ' ', {link: '@end'}],
67 [{link: '@full123.test-of-chars'}],
68 ['not@right'],
69 [{link: '@bad'}, '!@#$chars'],
70 [{link: '@newline1'}, '\n', {link: '@newline2'}],
71 ['parenthetical (', {link: '@handle'}, ')'],
72 ['start ', {link: 'https://middle.com'}, ' end'],
73 ['start ', {link: 'https://middle.com/foo/bar'}, ' end'],
74 ['start ', {link: 'https://middle.com/foo/bar?baz=bux'}, ' end'],
75 ['start ', {link: 'https://middle.com/foo/bar?baz=bux#hash'}, ' end'],
76 [{link: 'https://start.com/foo/bar?baz=bux#hash'}, ' middle end'],
77 ['start middle ', {link: 'https://end.com/foo/bar?baz=bux#hash'}],
78 [{link: 'https://newline1.com'}, '\n', {link: 'https://newline2.com'}],
79 ['start ', {link: 'middle.com'}, ' end'],
80 ['start ', {link: 'middle.com/foo/bar'}, ' end'],
81 ['start ', {link: 'middle.com/foo/bar?baz=bux'}, ' end'],
82 ['start ', {link: 'middle.com/foo/bar?baz=bux#hash'}, ' end'],
83 [{link: 'start.com/foo/bar?baz=bux#hash'}, ' middle end'],
84 ['start middle ', {link: 'end.com/foo/bar?baz=bux#hash'}],
85 [{link: 'newline1.com'}, '\n', {link: 'newline2.com'}],
86 ['not.. a..url ..here'],
87 ['e.g.'],
88 ['e.g. ', {link: 'real.com'}, ' fake.notreal'],
89 ['something-cool.jpg'],
90 ['website.com.jpg'],
91 ['e.g./foo'],
92 ['website.com.jpg/foo'],
93 [
94 'Classic article ',
95 {
96 link: 'https://socket3.wordpress.com/2018/02/03/designing-windows-95s-user-interface/',
97 },
98 ],
99 [
100 'Classic article ',
101 {
102 link: 'https://socket3.wordpress.com/2018/02/03/designing-windows-95s-user-interface/',
103 },
104 ' ',
105 ],
106 [
107 {link: 'https://foo.com'},
108 ' ',
109 {link: 'https://bar.com/whatever'},
110 ' ',
111 {link: 'https://baz.com'},
112 ],
113 [
114 'punctuation ',
115 {link: 'https://foo.com'},
116 ', ',
117 {link: 'https://bar.com/whatever'},
118 '; ',
119 {link: 'https://baz.com'},
120 '.',
121 ],
122 ['parenthetical (', {link: 'https://foo.com'}, ')'],
123 ['except for ', {link: 'https://foo.com/thing_(cool)'}],
124 ]
125 it('correctly handles a set of text inputs', () => {
126 for (let i = 0; i < inputs.length; i++) {
127 const input = inputs[i]
128 const output = detectLinkables(input)
129 expect(output).toEqual(outputs[i])
130 }
131 })
132})
133
134describe('makeRecordUri', () => {
135 const inputs: [string, string, string][] = [
136 ['alice.test', 'app.bsky.feed.post', '3jk7x4irgv52r'],
137 ]
138 const outputs = ['at://alice.test/app.bsky.feed.post/3jk7x4irgv52r']
139
140 it('correctly builds a record URI', () => {
141 for (let i = 0; i < inputs.length; i++) {
142 const input = inputs[i]
143 const result = makeRecordUri(...input)
144 expect(result).toEqual(outputs[i])
145 }
146 })
147})
148
149describe('makeValidHandle', () => {
150 const inputs = [
151 'test-handle-123',
152 'test!"#$%&/()=?_',
153 'this-handle-should-be-too-big',
154 ]
155 const outputs = ['test-handle-123', 'test', 'this-handle-should-b']
156
157 it('correctly parses and corrects handles', () => {
158 for (let i = 0; i < inputs.length; i++) {
159 const result = makeValidHandle(inputs[i])
160 expect(result).toEqual(outputs[i])
161 }
162 })
163})
164
165describe('createFullHandle', () => {
166 const inputs: [string, string][] = [
167 ['test-handle-123', 'test'],
168 ['.test.handle', 'test.test.'],
169 ['test.handle.', '.test.test'],
170 ]
171 const outputs = [
172 'test-handle-123.test',
173 '.test.handle.test.test.',
174 'test.handle.test.test',
175 ]
176
177 it('correctly parses and corrects handles', () => {
178 for (let i = 0; i < inputs.length; i++) {
179 const input = inputs[i]
180 const result = createFullHandle(...input)
181 expect(result).toEqual(outputs[i])
182 }
183 })
184})
185
186describe('enforceLen', () => {
187 const inputs: [string, number][] = [
188 ['Hello World!', 5],
189 ['Hello World!', 20],
190 ['', 5],
191 ]
192 const outputs = ['Hello', 'Hello World!', '']
193
194 it('correctly enforces defined length on a given string', () => {
195 for (let i = 0; i < inputs.length; i++) {
196 const input = inputs[i]
197 const result = enforceLen(...input)
198 expect(result).toEqual(outputs[i])
199 }
200 })
201})
202
203describe('cleanError', () => {
204 const inputs = [
205 'TypeError: Network request failed',
206 'Error: Aborted',
207 'Error: TypeError "x" is not a function',
208 'Error: SyntaxError unexpected token "export"',
209 'Some other error',
210 ]
211 const outputs = [
212 'Unable to connect. Please check your internet connection and try again.',
213 'Unable to connect. Please check your internet connection and try again.',
214 'TypeError "x" is not a function',
215 'SyntaxError unexpected token "export"',
216 'Some other error',
217 ]
218
219 it('removes extra content from error message', () => {
220 for (let i = 0; i < inputs.length; i++) {
221 const result = cleanError(inputs[i])
222 expect(result).toEqual(outputs[i])
223 }
224 })
225})
226
227describe('toNiceDomain', () => {
228 const inputs = [
229 'https://example.com/index.html',
230 'https://bsky.app',
231 'https://bsky.social',
232 '#123123123',
233 ]
234 const outputs = ['example.com', 'bsky.app', 'Bluesky Social', '#123123123']
235
236 it("displays the url's host in a easily readable manner", () => {
237 for (let i = 0; i < inputs.length; i++) {
238 const result = toNiceDomain(inputs[i])
239 expect(result).toEqual(outputs[i])
240 }
241 })
242})
243
244describe('toShortUrl', () => {
245 const inputs = [
246 'https://bsky.app',
247 'https://bsky.app/3jk7x4irgv52r',
248 'https://bsky.app/3jk7x4irgv52r2313y182h9',
249 'https://very-long-domain-name.com/foo',
250 'https://very-long-domain-name.com/foo?bar=baz#andsomemore',
251 ]
252 const outputs = [
253 'bsky.app',
254 'bsky.app/3jk7x4irgv52r',
255 'bsky.app/3jk7x4irgv52...',
256 'very-long-domain-name.com/foo',
257 'very-long-domain-name.com/foo?bar=baz#...',
258 ]
259
260 it('shortens the url', () => {
261 for (let i = 0; i < inputs.length; i++) {
262 const result = toShortUrl(inputs[i])
263 expect(result).toEqual(outputs[i])
264 }
265 })
266})
267
268describe('toShareUrl', () => {
269 const inputs = ['https://bsky.app', '/3jk7x4irgv52r', 'item/test/123']
270 const outputs = [
271 'https://bsky.app',
272 'https://bsky.app/3jk7x4irgv52r',
273 'https://bsky.app/item/test/123',
274 ]
275
276 it('appends https, when not present', () => {
277 for (let i = 0; i < inputs.length; i++) {
278 const result = toShareUrl(inputs[i])
279 expect(result).toEqual(outputs[i])
280 }
281 })
282})
283
284describe('shortenLinks', () => {
285 const inputs = [
286 'start https://middle.com/foo/bar?baz=bux#hash end',
287 'https://start.com/foo/bar?baz=bux#hash middle end',
288 'start middle https://end.com/foo/bar?baz=bux#hash',
289 'https://newline1.com/very/long/url/here\nhttps://newline2.com/very/long/url/here',
290 'Classic article https://socket3.wordpress.com/2018/02/03/designing-windows-95s-user-interface/',
291 ]
292 const outputs = [
293 [
294 'start middle.com/foo/bar?baz=... end',
295 ['https://middle.com/foo/bar?baz=bux#hash'],
296 ],
297 [
298 'start.com/foo/bar?baz=... middle end',
299 ['https://start.com/foo/bar?baz=bux#hash'],
300 ],
301 [
302 'start middle end.com/foo/bar?baz=...',
303 ['https://end.com/foo/bar?baz=bux#hash'],
304 ],
305 [
306 'newline1.com/very/long/ur...\nnewline2.com/very/long/ur...',
307 [
308 'https://newline1.com/very/long/url/here',
309 'https://newline2.com/very/long/url/here',
310 ],
311 ],
312 [
313 'Classic article socket3.wordpress.com/2018/02/03/d...',
314 [
315 'https://socket3.wordpress.com/2018/02/03/designing-windows-95s-user-interface/',
316 ],
317 ],
318 ]
319
320 it('correctly shortens rich text while preserving facet URIs', () => {
321 for (let i = 0; i < inputs.length; i++) {
322 const input = inputs[i]
323 const inputRT = new RichText({text: input})
324 inputRT.detectFacetsWithoutResolution()
325 const outputRT = shortenLinks(inputRT)
326 expect(outputRT.text).toEqual(outputs[i][0])
327 expect(outputRT.facets?.length).toEqual(outputs[i][1].length)
328 for (let j = 0; j < outputs[i][1].length; j++) {
329 expect(outputRT.facets![j].features[0].uri).toEqual(outputs[i][1][j])
330 }
331 }
332 })
333})
334
335describe('parseEmbedPlayerFromUrl', () => {
336 const inputs = [
337 'https://youtu.be/videoId',
338 'https://www.youtube.com/watch?v=videoId',
339 'https://www.youtube.com/watch?v=videoId&feature=share',
340 'https://youtube.com/watch?v=videoId',
341 'https://youtube.com/watch?v=videoId&feature=share',
342 'https://youtube.com/shorts/videoId',
343 'https://youtube.com/live/videoId',
344 'https://m.youtube.com/watch?v=videoId',
345 'https://music.youtube.com/watch?v=videoId',
346
347 'https://youtube.com/shorts/',
348 'https://youtube.com/',
349 'https://youtube.com/random',
350 'https://youtube.com/live/',
351
352 'https://twitch.tv/channelName',
353 'https://www.twitch.tv/channelName',
354 'https://m.twitch.tv/channelName',
355
356 'https://twitch.tv/channelName/clip/clipId',
357 'https://twitch.tv/videos/videoId',
358
359 'https://open.spotify.com/playlist/playlistId',
360 'https://open.spotify.com/playlist/playlistId?param=value',
361 'https://open.spotify.com/locale/playlist/playlistId',
362
363 'https://open.spotify.com/track/songId',
364 'https://open.spotify.com/track/songId?param=value',
365 'https://open.spotify.com/locale/track/songId',
366
367 'https://open.spotify.com/album/albumId',
368 'https://open.spotify.com/album/albumId?param=value',
369 'https://open.spotify.com/locale/album/albumId',
370
371 'https://soundcloud.com/user/track',
372 'https://soundcloud.com/user/sets/set',
373 'https://soundcloud.com/user/',
374
375 'https://music.apple.com/us/playlist/playlistName/playlistId',
376 'https://music.apple.com/us/album/albumName/albumId',
377 'https://music.apple.com/us/album/albumName/albumId?i=songId',
378
379 'https://vimeo.com/videoId',
380 'https://vimeo.com/videoId?autoplay=0',
381
382 'https://giphy.com/gifs/some-random-gif-name-gifId',
383 'https://giphy.com/gif/some-random-gif-name-gifId',
384 'https://giphy.com/gifs/',
385
386 'https://giphy.com/gifs/39248209509382934029?hh=100&ww=100',
387
388 'https://media.giphy.com/media/gifId/giphy.webp',
389 'https://media0.giphy.com/media/gifId/giphy.webp',
390 'https://media1.giphy.com/media/gifId/giphy.gif',
391 'https://media2.giphy.com/media/gifId/giphy.webp',
392 'https://media3.giphy.com/media/gifId/giphy.mp4',
393 'https://media4.giphy.com/media/gifId/giphy.webp',
394 'https://media5.giphy.com/media/gifId/giphy.mp4',
395 'https://media0.giphy.com/media/gifId/giphy.mp3',
396 'https://media1.google.com/media/gifId/giphy.webp',
397
398 'https://media.giphy.com/media/trackingId/gifId/giphy.webp',
399
400 'https://i.giphy.com/media/gifId/giphy.webp',
401 'https://i.giphy.com/media/gifId/giphy.webp',
402 'https://i.giphy.com/gifId.gif',
403 'https://i.giphy.com/gifId.gif',
404
405 'https://tenor.com/view/gifId',
406 'https://tenor.com/notView/gifId',
407 'https://tenor.com/view',
408 'https://tenor.com/view/gifId.gif',
409 'https://tenor.com/intl/view/gifId.gif',
410
411 'https://media.tenor.com/someID_AAAAC/someName.gif?hh=100&ww=100',
412 'https://media.tenor.com/someID_AAAAC/someName.gif',
413 'https://media.tenor.com/someID/someName.gif',
414 'https://media.tenor.com/someID',
415 'https://media.tenor.com',
416
417 'https://www.flickr.com/photos/username/albums/72177720308493661',
418 'https://flickr.com/photos/username/albums/72177720308493661',
419 'https://flickr.com/photos/username/albums/72177720308493661/',
420 'https://flickr.com/photos/username/albums/72177720308493661//',
421 'https://flic.kr/s/aHBqjAES3i',
422
423 'https://flickr.com/foetoes/username/albums/3903',
424 'https://flickr.com/albums/3903',
425 'https://flic.kr/s/OolI',
426 'https://flic.kr/t/aHBqjAES3i',
427
428 'https://www.flickr.com/groups/898944@N23/pool',
429 'https://flickr.com/groups/898944@N23/pool',
430 'https://flickr.com/groups/898944@N23/pool/',
431 'https://flickr.com/groups/898944@N23/pool//',
432 'https://flic.kr/go/8WJtR',
433
434 'https://www.flickr.com/groups/898944@N23/',
435 'https://www.flickr.com/groups',
436 ]
437
438 const outputs = [
439 {
440 type: 'youtube_video',
441 source: 'youtube',
442 playerUri: 'https://bsky.app/iframe/youtube.html?videoId=videoId&start=0',
443 },
444 {
445 type: 'youtube_video',
446 source: 'youtube',
447 playerUri: 'https://bsky.app/iframe/youtube.html?videoId=videoId&start=0',
448 },
449 {
450 type: 'youtube_video',
451 source: 'youtube',
452 playerUri: 'https://bsky.app/iframe/youtube.html?videoId=videoId&start=0',
453 },
454 {
455 type: 'youtube_video',
456 source: 'youtube',
457 playerUri: 'https://bsky.app/iframe/youtube.html?videoId=videoId&start=0',
458 },
459 {
460 type: 'youtube_video',
461 source: 'youtube',
462 playerUri: 'https://bsky.app/iframe/youtube.html?videoId=videoId&start=0',
463 },
464 {
465 type: 'youtube_short',
466 source: 'youtubeShorts',
467 hideDetails: true,
468 playerUri: 'https://bsky.app/iframe/youtube.html?videoId=videoId&start=0',
469 },
470 {
471 type: 'youtube_video',
472 source: 'youtube',
473 playerUri: 'https://bsky.app/iframe/youtube.html?videoId=videoId&start=0',
474 },
475 {
476 type: 'youtube_video',
477 source: 'youtube',
478 playerUri: 'https://bsky.app/iframe/youtube.html?videoId=videoId&start=0',
479 },
480 {
481 type: 'youtube_video',
482 source: 'youtube',
483 playerUri: 'https://bsky.app/iframe/youtube.html?videoId=videoId&start=0',
484 },
485
486 undefined,
487 undefined,
488 undefined,
489 undefined,
490
491 {
492 type: 'twitch_video',
493 source: 'twitch',
494 playerUri: `https://player.twitch.tv/?volume=0.5&!muted&autoplay&channel=channelName&parent=localhost`,
495 },
496 {
497 type: 'twitch_video',
498 source: 'twitch',
499 playerUri: `https://player.twitch.tv/?volume=0.5&!muted&autoplay&channel=channelName&parent=localhost`,
500 },
501 {
502 type: 'twitch_video',
503 source: 'twitch',
504 playerUri: `https://player.twitch.tv/?volume=0.5&!muted&autoplay&channel=channelName&parent=localhost`,
505 },
506 {
507 type: 'twitch_video',
508 source: 'twitch',
509 playerUri: `https://clips.twitch.tv/embed?volume=0.5&autoplay=true&clip=clipId&parent=localhost`,
510 },
511 {
512 type: 'twitch_video',
513 source: 'twitch',
514 playerUri: `https://player.twitch.tv/?volume=0.5&!muted&autoplay&video=videoId&parent=localhost`,
515 },
516
517 {
518 type: 'spotify_playlist',
519 source: 'spotify',
520 playerUri: `https://open.spotify.com/embed/playlist/playlistId`,
521 },
522 {
523 type: 'spotify_playlist',
524 source: 'spotify',
525 playerUri: `https://open.spotify.com/embed/playlist/playlistId`,
526 },
527 {
528 type: 'spotify_playlist',
529 source: 'spotify',
530 playerUri: `https://open.spotify.com/embed/playlist/playlistId`,
531 },
532
533 {
534 type: 'spotify_song',
535 source: 'spotify',
536 playerUri: `https://open.spotify.com/embed/track/songId`,
537 },
538 {
539 type: 'spotify_song',
540 source: 'spotify',
541 playerUri: `https://open.spotify.com/embed/track/songId`,
542 },
543 {
544 type: 'spotify_song',
545 source: 'spotify',
546 playerUri: `https://open.spotify.com/embed/track/songId`,
547 },
548
549 {
550 type: 'spotify_album',
551 source: 'spotify',
552 playerUri: `https://open.spotify.com/embed/album/albumId`,
553 },
554 {
555 type: 'spotify_album',
556 source: 'spotify',
557 playerUri: `https://open.spotify.com/embed/album/albumId`,
558 },
559 {
560 type: 'spotify_album',
561 source: 'spotify',
562 playerUri: `https://open.spotify.com/embed/album/albumId`,
563 },
564
565 {
566 type: 'soundcloud_track',
567 source: 'soundcloud',
568 playerUri: `https://w.soundcloud.com/player/?url=https://soundcloud.com/user/track&auto_play=true&visual=false&hide_related=true`,
569 },
570 {
571 type: 'soundcloud_set',
572 source: 'soundcloud',
573 playerUri: `https://w.soundcloud.com/player/?url=https://soundcloud.com/user/sets/set&auto_play=true&visual=false&hide_related=true`,
574 },
575 undefined,
576
577 {
578 type: 'apple_music_playlist',
579 source: 'appleMusic',
580 playerUri:
581 'https://embed.music.apple.com/us/playlist/playlistName/playlistId',
582 },
583 {
584 type: 'apple_music_album',
585 source: 'appleMusic',
586 playerUri: 'https://embed.music.apple.com/us/album/albumName/albumId',
587 },
588 {
589 type: 'apple_music_song',
590 source: 'appleMusic',
591 playerUri:
592 'https://embed.music.apple.com/us/album/albumName/albumId?i=songId',
593 },
594
595 {
596 type: 'vimeo_video',
597 source: 'vimeo',
598 playerUri: 'https://player.vimeo.com/video/videoId?autoplay=1',
599 },
600 {
601 type: 'vimeo_video',
602 source: 'vimeo',
603 playerUri: 'https://player.vimeo.com/video/videoId?autoplay=1',
604 },
605
606 {
607 type: 'giphy_gif',
608 source: 'giphy',
609 isGif: true,
610 hideDetails: true,
611 metaUri: 'https://giphy.com/gifs/gifId',
612 playerUri: 'https://i.giphy.com/media/gifId/200.webp',
613 },
614 undefined,
615 undefined,
616 {
617 type: 'giphy_gif',
618 source: 'giphy',
619 isGif: true,
620 hideDetails: true,
621 metaUri: 'https://giphy.com/gifs/39248209509382934029',
622 playerUri: 'https://i.giphy.com/media/39248209509382934029/200.webp',
623 },
624 {
625 type: 'giphy_gif',
626 source: 'giphy',
627 isGif: true,
628 hideDetails: true,
629 metaUri: 'https://giphy.com/gifs/gifId',
630 playerUri: 'https://i.giphy.com/media/gifId/200.webp',
631 },
632 {
633 type: 'giphy_gif',
634 source: 'giphy',
635 isGif: true,
636 hideDetails: true,
637 metaUri: 'https://giphy.com/gifs/gifId',
638 playerUri: 'https://i.giphy.com/media/gifId/200.webp',
639 },
640 {
641 type: 'giphy_gif',
642 source: 'giphy',
643 isGif: true,
644 hideDetails: true,
645 metaUri: 'https://giphy.com/gifs/gifId',
646 playerUri: 'https://i.giphy.com/media/gifId/200.webp',
647 },
648 {
649 type: 'giphy_gif',
650 source: 'giphy',
651 isGif: true,
652 hideDetails: true,
653 metaUri: 'https://giphy.com/gifs/gifId',
654 playerUri: 'https://i.giphy.com/media/gifId/200.webp',
655 },
656 {
657 type: 'giphy_gif',
658 source: 'giphy',
659 isGif: true,
660 hideDetails: true,
661 metaUri: 'https://giphy.com/gifs/gifId',
662 playerUri: 'https://i.giphy.com/media/gifId/200.webp',
663 },
664 {
665 type: 'giphy_gif',
666 source: 'giphy',
667 isGif: true,
668 hideDetails: true,
669 metaUri: 'https://giphy.com/gifs/gifId',
670 playerUri: 'https://i.giphy.com/media/gifId/200.webp',
671 },
672 undefined,
673 undefined,
674 undefined,
675
676 {
677 type: 'giphy_gif',
678 source: 'giphy',
679 isGif: true,
680 hideDetails: true,
681 metaUri: 'https://giphy.com/gifs/gifId',
682 playerUri: 'https://i.giphy.com/media/gifId/200.webp',
683 },
684
685 {
686 type: 'giphy_gif',
687 source: 'giphy',
688 isGif: true,
689 hideDetails: true,
690 metaUri: 'https://giphy.com/gifs/gifId',
691 playerUri: 'https://i.giphy.com/media/gifId/200.webp',
692 },
693 {
694 type: 'giphy_gif',
695 source: 'giphy',
696 isGif: true,
697 hideDetails: true,
698 metaUri: 'https://giphy.com/gifs/gifId',
699 playerUri: 'https://i.giphy.com/media/gifId/200.webp',
700 },
701 {
702 type: 'giphy_gif',
703 source: 'giphy',
704 isGif: true,
705 hideDetails: true,
706 metaUri: 'https://giphy.com/gifs/gifId',
707 playerUri: 'https://i.giphy.com/media/gifId/200.webp',
708 },
709 {
710 type: 'giphy_gif',
711 source: 'giphy',
712 isGif: true,
713 hideDetails: true,
714 metaUri: 'https://giphy.com/gifs/gifId',
715 playerUri: 'https://i.giphy.com/media/gifId/200.webp',
716 },
717
718 undefined,
719 undefined,
720 undefined,
721 undefined,
722 undefined,
723
724 {
725 type: 'tenor_gif',
726 source: 'tenor',
727 isGif: true,
728 hideDetails: true,
729 playerUri: 'https://t.gifs.bsky.app/someID_AAAAM/someName.gif',
730 dimensions: {
731 width: 100,
732 height: 100,
733 },
734 },
735 undefined,
736 undefined,
737 undefined,
738 undefined,
739
740 {
741 type: 'flickr_album',
742 source: 'flickr',
743 playerUri: 'https://embedr.flickr.com/photosets/72177720308493661',
744 },
745 {
746 type: 'flickr_album',
747 source: 'flickr',
748 playerUri: 'https://embedr.flickr.com/photosets/72177720308493661',
749 },
750 {
751 type: 'flickr_album',
752 source: 'flickr',
753 playerUri: 'https://embedr.flickr.com/photosets/72177720308493661',
754 },
755 {
756 type: 'flickr_album',
757 source: 'flickr',
758 playerUri: 'https://embedr.flickr.com/photosets/72177720308493661',
759 },
760 {
761 type: 'flickr_album',
762 source: 'flickr',
763 playerUri: 'https://embedr.flickr.com/photosets/72177720308493661',
764 },
765
766 undefined,
767 undefined,
768 undefined,
769 undefined,
770
771 {
772 type: 'flickr_album',
773 source: 'flickr',
774 playerUri: 'https://embedr.flickr.com/groups/898944@N23',
775 },
776 {
777 type: 'flickr_album',
778 source: 'flickr',
779 playerUri: 'https://embedr.flickr.com/groups/898944@N23',
780 },
781 {
782 type: 'flickr_album',
783 source: 'flickr',
784 playerUri: 'https://embedr.flickr.com/groups/898944@N23',
785 },
786 {
787 type: 'flickr_album',
788 source: 'flickr',
789 playerUri: 'https://embedr.flickr.com/groups/898944@N23',
790 },
791 {
792 type: 'flickr_album',
793 source: 'flickr',
794 playerUri: 'https://embedr.flickr.com/groups/898944@N23',
795 },
796
797 undefined,
798 undefined,
799 ]
800
801 it('correctly grabs the correct id from uri', () => {
802 for (let i = 0; i < inputs.length; i++) {
803 const input = inputs[i]
804 const output = outputs[i]
805
806 const res = parseEmbedPlayerFromUrl(input)
807
808 expect(res).toEqual(output)
809 }
810 })
811})
812
813describe('createStarterPackLinkFromAndroidReferrer', () => {
814 const validOutput = 'at://haileyok.com/app.bsky.graph.starterpack/rkey'
815
816 it('returns a link when input contains utm_source and utm_content', () => {
817 expect(
818 createStarterPackLinkFromAndroidReferrer(
819 'utm_source=bluesky&utm_content=starterpack_haileyok.com_rkey',
820 ),
821 ).toEqual(validOutput)
822
823 expect(
824 createStarterPackLinkFromAndroidReferrer(
825 'utm_source=bluesky&utm_content=starterpack_test-lover-9000.com_rkey',
826 ),
827 ).toEqual('at://test-lover-9000.com/app.bsky.graph.starterpack/rkey')
828 })
829
830 it('returns a link when input contains utm_source and utm_content in different order', () => {
831 expect(
832 createStarterPackLinkFromAndroidReferrer(
833 'utm_content=starterpack_haileyok.com_rkey&utm_source=bluesky',
834 ),
835 ).toEqual(validOutput)
836 })
837
838 it('returns a link when input contains other parameters as well', () => {
839 expect(
840 createStarterPackLinkFromAndroidReferrer(
841 'utm_source=bluesky&utm_medium=starterpack&utm_content=starterpack_haileyok.com_rkey',
842 ),
843 ).toEqual(validOutput)
844 })
845
846 it('returns null when utm_source is not present', () => {
847 expect(
848 createStarterPackLinkFromAndroidReferrer(
849 'utm_content=starterpack_haileyok.com_rkey',
850 ),
851 ).toEqual(null)
852 })
853
854 it('returns null when utm_content is not present', () => {
855 expect(
856 createStarterPackLinkFromAndroidReferrer('utm_source=bluesky'),
857 ).toEqual(null)
858 })
859
860 it('returns null when utm_content is malformed', () => {
861 expect(
862 createStarterPackLinkFromAndroidReferrer(
863 'utm_content=starterpack_haileyok.com',
864 ),
865 ).toEqual(null)
866
867 expect(
868 createStarterPackLinkFromAndroidReferrer('utm_content=starterpack'),
869 ).toEqual(null)
870
871 expect(
872 createStarterPackLinkFromAndroidReferrer(
873 'utm_content=starterpack_haileyok.com_rkey_more',
874 ),
875 ).toEqual(null)
876
877 expect(
878 createStarterPackLinkFromAndroidReferrer(
879 'utm_content=notastarterpack_haileyok.com_rkey',
880 ),
881 ).toEqual(null)
882 })
883})
884
885describe('parseStarterPackHttpUri', () => {
886 const baseUri = 'https://bsky.app/start'
887
888 it('returns a valid at uri when http uri is valid', () => {
889 const validHttpUri = `${baseUri}/haileyok.com/rkey`
890 expect(parseStarterPackUri(validHttpUri)).toEqual({
891 name: 'haileyok.com',
892 rkey: 'rkey',
893 })
894
895 const validHttpUri2 = `${baseUri}/haileyok.com/ilovetesting`
896 expect(parseStarterPackUri(validHttpUri2)).toEqual({
897 name: 'haileyok.com',
898 rkey: 'ilovetesting',
899 })
900
901 const validHttpUri3 = `${baseUri}/testlover9000.com/rkey`
902 expect(parseStarterPackUri(validHttpUri3)).toEqual({
903 name: 'testlover9000.com',
904 rkey: 'rkey',
905 })
906 })
907
908 it('returns null when there is no rkey', () => {
909 const validHttpUri = `${baseUri}/haileyok.com`
910 expect(parseStarterPackUri(validHttpUri)).toEqual(null)
911 })
912
913 it('returns null when there is an extra path', () => {
914 const validHttpUri = `${baseUri}/haileyok.com/rkey/other`
915 expect(parseStarterPackUri(validHttpUri)).toEqual(null)
916 })
917
918 it('returns null when there is no handle or rkey', () => {
919 const validHttpUri = `${baseUri}`
920 expect(parseStarterPackUri(validHttpUri)).toEqual(null)
921 })
922
923 it('returns null when the route is not /start or /starter-pack', () => {
924 const validHttpUri = 'https://bsky.app/start/haileyok.com/rkey'
925 expect(parseStarterPackUri(validHttpUri)).toEqual({
926 name: 'haileyok.com',
927 rkey: 'rkey',
928 })
929
930 const validHttpUri2 = 'https://bsky.app/starter-pack/haileyok.com/rkey'
931 expect(parseStarterPackUri(validHttpUri2)).toEqual({
932 name: 'haileyok.com',
933 rkey: 'rkey',
934 })
935
936 const invalidHttpUri = 'https://bsky.app/profile/haileyok.com/rkey'
937 expect(parseStarterPackUri(invalidHttpUri)).toEqual(null)
938 })
939
940 it('returns the at uri when the input is a valid starterpack at uri', () => {
941 const validAtUri = 'at://did:123/app.bsky.graph.starterpack/rkey'
942 expect(parseStarterPackUri(validAtUri)).toEqual({
943 name: 'did:123',
944 rkey: 'rkey',
945 })
946 })
947
948 it('returns null when the at uri has no rkey', () => {
949 const validAtUri = 'at://did:123/app.bsky.graph.starterpack'
950 expect(parseStarterPackUri(validAtUri)).toEqual(null)
951 })
952
953 it('returns null when the collection is not app.bsky.graph.starterpack', () => {
954 const validAtUri = 'at://did:123/app.bsky.graph.list/rkey'
955 expect(parseStarterPackUri(validAtUri)).toEqual(null)
956 })
957
958 it('returns null when the input is undefined', () => {
959 expect(parseStarterPackUri(undefined)).toEqual(null)
960 })
961})
962
963describe('createStarterPackGooglePlayUri', () => {
964 const base =
965 'https://play.google.com/store/apps/details?id=xyz.blueskyweb.app&referrer=utm_source%3Dbluesky%26utm_medium%3Dstarterpack%26utm_content%3Dstarterpack_'
966
967 it('returns valid google play uri when input is valid', () => {
968 expect(createStarterPackGooglePlayUri('name', 'rkey')).toEqual(
969 `${base}name_rkey`,
970 )
971 })
972
973 it('returns null when no rkey is supplied', () => {
974 // @ts-expect-error test
975 expect(createStarterPackGooglePlayUri('name', undefined)).toEqual(null)
976 })
977
978 it('returns null when no name or rkey are supplied', () => {
979 // @ts-expect-error test
980 expect(createStarterPackGooglePlayUri(undefined, undefined)).toEqual(null)
981 })
982
983 it('returns null when rkey is supplied but no name', () => {
984 // @ts-expect-error test
985 expect(createStarterPackGooglePlayUri(undefined, 'rkey')).toEqual(null)
986 })
987})