From f10a8308d9f6bfb907c8a2458cbf78b4cfad88d2 Mon Sep 17 00:00:00 2001 From: Aryan Goharzad Date: Thu, 19 Jan 2023 13:53:11 -0500 Subject: Fixes youtube embed issues (#50) * fixes youtube embed * move extractMetaHtml test to its own file * tests cleanup * Add fallback for youtube meta data * lint * Check for youtube in the url domain * use hostname instead of full url to check for link domain * checks only for domain --- __tests__/lib/__mocks__/exampleComHtml.ts | 47 +++++++++++++++++++++ __tests__/lib/__mocks__/youtubeHtml.ts | 19 +++++++++ __tests__/lib/extractMetaHtml.test.ts | 70 +++++++++++++++++++++++++++++++ __tests__/lib/link-meta.test.ts | 49 +--------------------- __tests__/lib/string.test.ts | 43 ------------------- 5 files changed, 137 insertions(+), 91 deletions(-) create mode 100644 __tests__/lib/__mocks__/exampleComHtml.ts create mode 100644 __tests__/lib/__mocks__/youtubeHtml.ts create mode 100644 __tests__/lib/extractMetaHtml.test.ts (limited to '__tests__/lib') diff --git a/__tests__/lib/__mocks__/exampleComHtml.ts b/__tests__/lib/__mocks__/exampleComHtml.ts new file mode 100644 index 000000000..6633e40ca --- /dev/null +++ b/__tests__/lib/__mocks__/exampleComHtml.ts @@ -0,0 +1,47 @@ +export const exampleComHtml = ` + + + Example Domain + + + + + + + + + +
+

Example Domain

+

This domain is for use in illustrative examples in documents. You may use this + domain in literature without prior coordination or asking for permission.

+

More information...

+
+ +` diff --git a/__tests__/lib/__mocks__/youtubeHtml.ts b/__tests__/lib/__mocks__/youtubeHtml.ts new file mode 100644 index 000000000..7fd9f819d --- /dev/null +++ b/__tests__/lib/__mocks__/youtubeHtml.ts @@ -0,0 +1,19 @@ +export const youtubeHTML = ` + +YouTube
+ +` diff --git a/__tests__/lib/extractMetaHtml.test.ts b/__tests__/lib/extractMetaHtml.test.ts new file mode 100644 index 000000000..4ac653db0 --- /dev/null +++ b/__tests__/lib/extractMetaHtml.test.ts @@ -0,0 +1,70 @@ +import {extractHtmlMeta} from '../../src/lib/extractHtmlMeta' +import {exampleComHtml} from './__mocks__/exampleComHtml' +import {youtubeHTML} from './__mocks__/youtubeHtml' + +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(input) + 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(input) + 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(input) + expect(output).toEqual(expectedOutput) + }) +}) diff --git a/__tests__/lib/link-meta.test.ts b/__tests__/lib/link-meta.test.ts index 5931a4ccb..eaf8732b2 100644 --- a/__tests__/lib/link-meta.test.ts +++ b/__tests__/lib/link-meta.test.ts @@ -1,54 +1,7 @@ import {LikelyType, getLinkMeta, getLikelyType} from '../../src/lib/link-meta' +import {exampleComHtml} from './__mocks__/exampleComHtml' import {mockedRootStore} from '../../__mocks__/state-mock' -const exampleComHtml = ` - - - Example Domain - - - - - - - - - -
-

Example Domain

-

This domain is for use in illustrative examples in documents. You may use this - domain in literature without prior coordination or asking for permission.

-

More information...

-
- -` - describe('getLinkMeta', () => { const inputs = [ '', diff --git a/__tests__/lib/string.test.ts b/__tests__/lib/string.test.ts index d8a56b36b..0032ebf31 100644 --- a/__tests__/lib/string.test.ts +++ b/__tests__/lib/string.test.ts @@ -1,7 +1,6 @@ import { extractEntities, detectLinkables, - extractHtmlMeta, pluralize, makeRecordUri, ago, @@ -286,48 +285,6 @@ describe('detectLinkables', () => { }) }) -describe('extractHtmlMeta', () => { - const inputs = [ - '', - 'nothing', - 'title', - ' aSd!@#AC ', - '\n title\n ', - '', - '', - '', - '', - '', - '', - '', - '', - '', - ] - const outputs = [ - {}, - {}, - {title: 'title'}, - {title: 'aSd!@#AC'}, - {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('correctly handles a set of text inputs', () => { - for (let i = 0; i < inputs.length; i++) { - const input = inputs[i] - const output = extractHtmlMeta(input) - expect(output).toEqual(outputs[i]) - } - }) -}) - describe('pluralize', () => { const inputs: [number, string, string?][] = [ [1, 'follower'], -- cgit 1.4.1