From 2fce1637b4ae01667da8ceafaa07a6266ab88450 Mon Sep 17 00:00:00 2001 From: Aryan Goharzad Date: Fri, 20 Jan 2023 13:54:30 -0500 Subject: Fixes embed links for twitter and tiktok (#74) --- __tests__/lib/extractHtmlMeta.test.ts | 116 ++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 __tests__/lib/extractHtmlMeta.test.ts (limited to '__tests__/lib/extractHtmlMeta.test.ts') diff --git a/__tests__/lib/extractHtmlMeta.test.ts b/__tests__/lib/extractHtmlMeta.test.ts new file mode 100644 index 000000000..c33084072 --- /dev/null +++ b/__tests__/lib/extractHtmlMeta.test.ts @@ -0,0 +1,116 @@ +import {extractHtmlMeta} from '../../src/lib/extractHtmlMeta' +import {exampleComHtml} from './__mocks__/exampleComHtml' +import {youtubeHTML} from './__mocks__/youtubeHtml' +import {tiktokHtml} from './__mocks__/tiktokHtml' + +describe('extractHtmlMeta', () => { + const cases = [ + ['', {}], + ['nothing', {}], + ['title', {title: 'title'}], + [' aSd!@#AC ', {title: 'aSd!@#AC'}], + ['\n title\n ', {title: 'title'}], + ['', {title: 'meta title'}], + [ + '', + {description: 'meta description'}, + ], + ['', {title: 'og title'}], + [ + '', + {description: 'og description'}, + ], + [ + '', + {image: 'https://ogimage.com/foo.png'}, + ], + [ + '', + {title: 'twitter title'}, + ], + [ + '', + {description: 'twitter description'}, + ], + [ + '', + {image: 'https://twitterimage.com/foo.png'}, + ], + ['', {title: 'meta title'}], + ] + + it.each(cases)( + 'given the html tag %p, returns %p', + (input, expectedResult) => { + const output = extractHtmlMeta({html: input as string, hostname: ''}) + expect(output).toEqual(expectedResult) + }, + ) + + it('extracts title and description from a generic HTML page', () => { + const input = exampleComHtml + const expectedOutput = { + title: 'Example Domain', + description: 'An example website', + } + const output = extractHtmlMeta({html: input, hostname: 'example.com'}) + expect(output).toEqual(expectedOutput) + }) + + it('extracts title and description from a Tiktok HTML page', () => { + const input = tiktokHtml + const expectedOutput = { + title: + 'Coca-Cola and Mentos! Super Reaction! #cocacola #mentos #reaction #bal... | TikTok', + description: + '5.5M Likes, 20.8K Comments. TikTok video from Power Vision Tests (@_powervision_): "Coca-Cola and Mentos! Super Reaction! #cocacola #mentos #reaction #balloon #sciencemoment #scienceexperiment #experiment #test #amazing #pvexp". оригинальный звук - Power Vision Tests.', + } + const output = extractHtmlMeta({html: input, hostname: 'tiktok.com'}) + expect(output).toEqual(expectedOutput) + }) + + it('extracts title and description from a generic youtube page', () => { + const input = youtubeHTML + const expectedOutput = { + title: 'HD Video (1080p) with Relaxing Music of Native American Shamans', + description: + 'Stunning HD Video ( 1080p ) of Patagonian Nature with Relaxing Native American Shamanic Music. HD footage used from ', + image: 'https://i.ytimg.com/vi/x6UITRjhijI/sddefault.jpg', + } + const output = extractHtmlMeta({html: input, hostname: 'youtube.com'}) + expect(output).toEqual(expectedOutput) + }) + + it('extracts username from the url a twitter profile page', () => { + const expectedOutput = { + title: '@bluesky on Twitter', + } + const output = extractHtmlMeta({ + hostname: 'twitter.com', + pathname: '/bluesky', + }) + expect(output).toEqual(expectedOutput) + }) + + it('extracts username from the url a tweet', () => { + const expectedOutput = { + title: 'Tweet by @bluesky', + } + const output = extractHtmlMeta({ + hostname: 'twitter.com', + pathname: '/bluesky/status/1582437529969917953', + }) + expect(output).toEqual(expectedOutput) + }) + + it("does not extract username from the url when it's not a tweet or profile page", () => { + const expectedOutput = { + title: 'Twitter', + } + const output = extractHtmlMeta({ + hostname: 'twitter.com', + pathname: '/i/articles/follows/-1675653703?time_window=24', + }) + expect(output).toEqual(expectedOutput) + }) +}) -- cgit 1.4.1