Skip to content

Commit

Permalink
feat(jest): add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
okdargy committed May 26, 2024
1 parent b58d346 commit 46b98b9
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 55 deletions.
11 changes: 11 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
preset: 'ts-jest',
transform: { '^.+\\.ts?$': 'ts-jest' },
testEnvironment: 'node',
testRegex: '/tests/.*\\.(test|spec)?\\.(ts|tsx)$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
roots: ['<rootDir>/src'],
moduleNameMapper: {
'@/(.*)': '<rootDir>/src/$1'
}
}
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"dev": "wrangler dev src/index.ts --env=local",
"build": "wrangler build src/index.ts",
"deploy": "wrangler deploy --minify src/index.ts",
"test": "jest",
"prettier": "prettier . --write"
},
"dependencies": {
Expand All @@ -14,8 +15,11 @@
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20230914.0",
"@types/jest": "^29.5.12",
"@types/react": "^18.2.46",
"jest": "^29.7.0",
"prettier": "3.2.5",
"ts-jest": "^29.1.3",
"wrangler": "^3.21.0"
},
"homepage": "https://tnktok.com"
Expand Down
51 changes: 25 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,24 @@ const app = new Hono()

app.get('/test/:videoId', async (c) => {
const { videoId } = c.req.param()
const awemeId = await scrapeVideoData(videoId)

if (awemeId instanceof Error) {
return new Response((awemeId as Error).message, { status: 500 })
}
try {
const videoData = await scrapeVideoData(videoId)

return new Response(JSON.stringify(awemeId), {
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
})
return new Response(JSON.stringify(videoData), {
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
})
} catch (e) {
return new Response((e as Error).message, {
status: 500,
headers: {
'Content-Type': 'text/plain; charset=utf-8'
}
})
}
})

app.get('/', (c) => {
Expand Down Expand Up @@ -59,7 +65,7 @@ async function handleVideo(c: any): Promise<Response> {
id = awemeId
} catch (e) {
const responseContent = await ErrorResponse((e as Error).message)
return returnHTMLResponse(responseContent, 201)
return returnHTMLResponse(responseContent, 500)
}
}

Expand All @@ -68,7 +74,7 @@ async function handleVideo(c: any): Promise<Response> {

if (videoInfo instanceof Error) {
const responseContent = await ErrorResponse((videoInfo as Error).message)
return returnHTMLResponse(responseContent, 201)
return returnHTMLResponse(responseContent, 500)
}

const url = new URL(c.req.url)
Expand Down Expand Up @@ -99,9 +105,8 @@ async function handleVideo(c: any): Promise<Response> {
return returnHTMLResponse(responseContent)
}
} catch (e) {
console.log(e)
const responseContent = await ErrorResponse((e as Error).message)
return returnHTMLResponse(responseContent, 201)
return returnHTMLResponse(responseContent, 500)
}
}

Expand All @@ -116,21 +121,13 @@ app.get('/generate/alternate', (c) => {
})
})

app.get(
'/generate/*',
cache({
cacheName: 'my-app',
cacheControl: 'max-age=3600'
})
)

app.get('/generate/video/:videoId', async (c) => {
const { videoId } = c.req.param()

try {
/*
const data = await scrapeVideoData(videoId);
const data = await scrapeVideoData(videoId)

/*
if (!(data instanceof Error)) {
if(data.video.playAddr) {
return c.redirect(data.video.playAddr)
Expand All @@ -143,6 +140,7 @@ app.get('/generate/video/:videoId', async (c) => {
}
}
*/

return c.redirect(`https://tikwm.com/video/media/play/${videoId}.mp4`)
} catch (e) {
return new Response((e as Error).message, {
Expand All @@ -158,9 +156,9 @@ app.get('/generate/image/:videoId', async (c) => {
const { videoId } = c.req.param()

try {
/*
const data = await scrapeVideoData(videoId);
const data = await scrapeVideoData(videoId)

/*
if (!(data instanceof Error)) {
if(data.imagePost.images.length > 0) {
return c.redirect(data.imagePost.images[0].imageURL.urlList[0])
Expand All @@ -169,6 +167,7 @@ app.get('/generate/image/:videoId', async (c) => {
}
}
*/

return c.redirect(`https://tikwm.com/video/cover/${videoId}.webp`)
} catch (e) {
return new Response((e as Error).message, {
Expand Down
8 changes: 1 addition & 7 deletions src/services/tiktok.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ export async function grabAwemeId(videoId: string): Promise<String | Error> {
}

export async function scrapeVideoData(awemeId: string, author?: string): Promise<ItemStruct | Error> {
console.log('before', cookie.getUpdatingCookies())
const res = await fetch(`https://www.tiktok.com/@${author || 'i'}"/video/${awemeId}`, {
const res = await fetch(`https://www.tiktok.com/@${author || 'i'}/video/${awemeId}`, {
method: 'GET',
headers: {
Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
Expand All @@ -34,8 +33,6 @@ export async function scrapeVideoData(awemeId: string, author?: string): Promise
}
})

console.log('string', cookie.getCookiesAsString())
console.log(res.headers)
let cookies = cookieParser(res.headers.get('set-cookie')!)
cookie.setCookies(cookies)

Expand All @@ -47,18 +44,15 @@ export async function scrapeVideoData(awemeId: string, author?: string): Promise
.split('</script>')[0]
const json: WebJSONResponse = JSON.parse(resJson)

//console.log(Object.keys(json["__DEFAULT_SCOPE__"]));
if (
!json['__DEFAULT_SCOPE__']['webapp.video-detail'] ||
json['__DEFAULT_SCOPE__']['webapp.video-detail'].statusCode == 10204
)
throw new Error('Could not find video data')
const videoInfo = json['__DEFAULT_SCOPE__']['webapp.video-detail']['itemInfo']['itemStruct']
//console.log(videoInfo)

return videoInfo
} catch (err) {
console.log(err)
throw new Error('Could not parse video info')
}
}
2 changes: 1 addition & 1 deletion src/services/tiktokv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function getVideoInfo(awemeId: string): Promise<AwemeList | Error>
cacheTtlByStatus: { '200-299': 86400, 404: 1, '500-599': 0 }
}
})
const json: TikTokAPIResponse = await res.json()
const json = (await res.json()) as TikTokAPIResponse
const videoInfo: AwemeList | undefined = json.aweme_list.find((aweme) => aweme.aweme_id === awemeId)

if (videoInfo) {
Expand Down
1 change: 0 additions & 1 deletion src/templates/pages/VideoResponse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export function VideoResponse(data: ItemStruct): JSX.Element {
}
*/

console.log(data)
return (
<>
{MetaHelper(
Expand Down
20 changes: 20 additions & 0 deletions src/tests/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import app from '@/index'

describe('GET /test/:videoId', () => {
it('should return 200', async () => {
const res = await app.request('/test/7332187682480590112', {
method: 'GET'
})

expect(res.status).toBe(200)
})

it('should return 500', async () => {
const res = await app.request('/test/123', {
method: 'GET'
})

expect(res.status).toBe(500)
expect(await res.text()).toBe('Could not parse video info')
})
})
53 changes: 53 additions & 0 deletions src/tests/photo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import app from '@/index'

describe('GET /@i/photo/:videoId', () => {
it('should return 200', async () => {
const res = await app.request('/@i/photo/7335753580093164833', {
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (compatible; Discordbot/2.0; +https://discordapp.com)'
}
})

expect(res.status).toBe(200)
})

// no discord user agent, redirects
it('should return 302', async () => {
const res = await app.request('/@i/photo/7335753580093164833', {
method: 'GET'
})

expect(res.status).toBe(302)
})

it('should return 500', async () => {
const res = await app.request('/@i/photo/123', {
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (compatible; Discordbot/2.0; +https://discordapp.com)'
}
})

expect(res.status).toBe(500)
expect(await res.text()).toContain('An error occurred while trying to fetch the video. Please try again later.')
})
})

describe('GET /generate/image/:videoId', () => {
it('should return 302', async () => {
const res = await app.request('/generate/image/7335753580093164833', {
method: 'GET'
})

expect(res.status).toBe(302)
})

it('should return 500', async () => {
const res = await app.request('/generate/image/123', {
method: 'GET'
})

expect(res.status).toBe(500)
})
})
66 changes: 66 additions & 0 deletions src/tests/video.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import app from '@/index'

describe('GET /@i/video/:videoId', () => {
it('should return 200', async () => {
const res = await app.request('/@pr4yforgabs/video/7332187682480590112', {
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (compatible; Discordbot/2.0; +https://discordapp.com)'
}
})

expect(res.status).toBe(200)
})

// no discord user agent, redirects
it('should return 302', async () => {
const res = await app.request('/@pr4yforgabs/video/7332187682480590112', {
method: 'GET'
})

expect(res.status).toBe(302)
})

it('should return 500', async () => {
const res = await app.request('/@i/video/123', {
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (compatible; Discordbot/2.0; +https://discordapp.com)'
}
})

expect(res.status).toBe(500)
expect(await res.text()).toContain('An error occurred while trying to fetch the video. Please try again later.')
})
})

describe('GET /t/:videoId', () => {
it('should return 200', async () => {
const res = await app.request('/t/ZPRKrbUB1', {
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (compatible; Discordbot/2.0; +https://discordapp.com)'
}
})

expect(res.status).toBe(200)
})
})

describe('GET /generate/video/:videoId', () => {
it('should return 302', async () => {
const res = await app.request('/generate/video/7332187682480590112', {
method: 'GET'
})

expect(res.status).toBe(302)
})

it('should return 500', async () => {
const res = await app.request('/generate/video/123', {
method: 'GET'
})

expect(res.status).toBe(500)
})
})
18 changes: 0 additions & 18 deletions src/util/cookieHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,8 @@ export default class CookieHelper {
return Object.freeze({ ...this.cookies })
}
getCookiesAsString() {
/*
this.cookies
[
{
name: 'ttwid',
value: '1|8WN5g_4UuJAXRCCDkpvHVs1VcAxDbE1YbchrWF9WLpQ|1716658463|7e41f1aa8f1a95fa8a4d5e4e56dc24eb6c05bbff5e66db010e6cd7f543a5a463',
domain: '.tiktok.com',
path: '/',
expires: 2024-11-21T17:34:23.000Z,
'httponly, tt_csrf_token': '3fBpsXXj-X7xQXEbXEchtyGe7ZxGbSzQGJHo',
sameSite: 'lax',
secure: true,
'httponly, tt_chain_token': 'ATA0mqdlbe36/vayBw/R2g==',
httpOnly: true
}
]
*/
return this.cookies
.map((cookie) => {
console.log(cookie)
return `${cookie.name}=${cookie.value}`
})
.join('; ')
Expand Down
7 changes: 5 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
"esModuleInterop": true,
"strict": true,
"lib": ["esnext"],
"types": ["@cloudflare/workers-types"],
"types": ["@cloudflare/workers-types", "@types/jest"],
"jsx": "react-jsx",
"jsxImportSource": "hono/jsx"
"jsxImportSource": "hono/jsx",
"paths": {
"@/*": ["./src/*"]
}
}
}

0 comments on commit 46b98b9

Please sign in to comment.