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