add testcases

This commit is contained in:
Hongbo Wu
2023-06-07 10:45:46 +08:00
parent 45609335b6
commit d194709b0e
9 changed files with 45 additions and 11 deletions

View File

@ -17,7 +17,8 @@
"devDependencies": {
"chai": "^4.3.6",
"eslint-plugin-prettier": "^4.0.0",
"mocha": "^10.0.0"
"mocha": "^10.0.0",
"nock": "^13.3.1"
},
"dependencies": {
"@google-cloud/functions-framework": "3.1.2",

View File

@ -158,8 +158,10 @@ const getImageSize = async (url: string): Promise<[number, number] | null> => {
}
}
// credit to https://github.com/reddit-archive/reddit/blob/753b17407e9a9dca09558526805922de24133d53/r2/r2/lib/media.py#L706
const findThumbnail = async (content: string): Promise<string | null> => {
// credit: https://github.com/reddit-archive/reddit/blob/753b17407e9a9dca09558526805922de24133d53/r2/r2/lib/media.py#L706
export const findThumbnail = async (
content: string
): Promise<string | null> => {
const dom = parseHTML(content).document
// find the largest and squarest image as the thumbnail

View File

@ -0,0 +1,9 @@
<DIV class="page" id="readability-page-1">
<img src="https://omnivore.app/small.png" alt="small image" />
<img src="https://omnivore.app/large_and_square.png" alt="large and square image" />
<img src="https://omnivore.app/wide.png" alt="wide image" />
<img src="https://omnivore.app/sprite.png" alt="sprite image" />
</DIV>

Binary file not shown.

After

Width:  |  Height:  |  Size: 117 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 872 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 117 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 174 KiB

View File

@ -0,0 +1,30 @@
import { expect } from 'chai'
import fs from 'fs'
import 'mocha'
import nock from 'nock'
import path from 'path'
import { findThumbnail } from '../src'
describe('findThumbnail', () => {
it('finds the largest and squarest image', async () => {
const images = ['large_and_square', 'small', 'sprite', 'wide']
// mock getting image by url
images.forEach((image) => {
nock('https://omnivore.app')
.get(`/${image}.png`)
.replyWithFile(200, path.join(__dirname, 'fixtures', `${image}.png`))
})
// get html content from file
const content = fs.readFileSync(
path.join(__dirname, 'fixtures', 'findThumbnail.html'),
'utf8'
)
// find thumbnail
const thumbnail = await findThumbnail(content)
expect(thumbnail).to.eql('https://omnivore.app/large_and_square.png')
// clean up
nock.cleanAll()
})
})

View File

@ -1,8 +0,0 @@
import 'mocha'
import { expect } from 'chai'
describe('stub test', () => {
it('should pass', () => {
expect(true).to.be.true
})
})