Skip to content

Commit

Permalink
fix(core): webHub test
Browse files Browse the repository at this point in the history
  • Loading branch information
LarchLiu committed May 8, 2023
1 parent 09f5848 commit 891151d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 30 deletions.
10 changes: 5 additions & 5 deletions packages/webHub/test/website.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@ import { getWebsiteInfo } from './website'

describe('get website info', () => {
test('github info', async () => {
const res = await getWebsiteInfo({ webUrl: 'https://github.com/LarchLiu/star-nexus' })
const res = await getWebsiteInfo('https://github.com/LarchLiu/star-nexus')
expect((res.data?.title)?.includes('star-nexus')).toBeTruthy()
expect(res.data?.url).toBe('https://github.com/LarchLiu/star-nexus')
})

test('tweet info', async () => {
const res = await getWebsiteInfo({ webUrl: 'https://twitter.com/LarchLiu/status/1594316498377621504' })
const res = await getWebsiteInfo('https://twitter.com/LarchLiu/status/1594316498377621504')
expect((res.data?.title)?.includes('Alex Liu')).toBeTruthy()
expect(res.data?.url).toBe('https://twitter.com/LarchLiu/status/1594316498377621504')
})
})

describe('handle error', () => {
test('github repo path error', async () => {
const res = await getWebsiteInfo({ webUrl: 'https://github.com/LarchLiu' })
const res = await getWebsiteInfo('https://github.com/LarchLiu')
expect(res.error).toMatchSnapshot()
})
test('twitter path error', async () => {
const res = await getWebsiteInfo({ webUrl: 'https://twitter.com/LarchLiu/statu/1594316498377621504' })
const res = await getWebsiteInfo('https://twitter.com/LarchLiu/statu/1594316498377621504')
expect(res.error).toMatchSnapshot()
})
test('website domain error', async () => {
const res = await getWebsiteInfo({ webUrl: 'https://domain-error.com/xxx/yyy' })
const res = await getWebsiteInfo('https://domain-error.com/xxx/yyy')
expect(res.error).toMatchSnapshot()
})
})
28 changes: 3 additions & 25 deletions packages/webHub/test/website.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,8 @@
import type { FetchWebsite, LoaderUrls } from '@starnexus/core'
import { getDomain } from '@starnexus/core'
import { getWebsiteInfo as getInfo } from '../../core/src/website'
import { routes } from '../src/routes-auto-generate'

export async function getWebsiteInfo(urls: LoaderUrls, header?: Record<string, string>): Promise<FetchWebsite> {
let info: FetchWebsite = { error: 'StarNexus error: Not supported website.' }
const domain = getDomain(urls.webUrl)

if (routes[domain]) {
const router = routes[domain]
if (router.paths) {
for (let i = 0; i < router.paths.length; i++) {
const pathInfo = router.paths[i]
const loaderUrls = pathInfo.filter(urls)
if (loaderUrls) {
info = await pathInfo.loader(loaderUrls, header)
if (info.data) {
info.data.meta.domain = domain
info.data.meta.website = router.name
}

return info
}
}
}
return { error: `${router.name} error: Not supported website.` }
}
export async function getWebsiteInfo(url: string) {
const info = await getInfo({ webUrl: url }, routes)

return info
}

0 comments on commit 891151d

Please sign in to comment.