add testcases
This commit is contained in:
@ -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",
|
||||
|
||||
@ -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
|
||||
|
||||
9
packages/thumbnail-handler/test/fixtures/findThumbnail.html
vendored
Normal file
9
packages/thumbnail-handler/test/fixtures/findThumbnail.html
vendored
Normal 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>
|
||||
BIN
packages/thumbnail-handler/test/fixtures/large_and_square.png
vendored
Normal file
BIN
packages/thumbnail-handler/test/fixtures/large_and_square.png
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 117 KiB |
BIN
packages/thumbnail-handler/test/fixtures/small.png
vendored
Normal file
BIN
packages/thumbnail-handler/test/fixtures/small.png
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 872 B |
BIN
packages/thumbnail-handler/test/fixtures/sprite.png
vendored
Normal file
BIN
packages/thumbnail-handler/test/fixtures/sprite.png
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 117 KiB |
BIN
packages/thumbnail-handler/test/fixtures/wide.png
vendored
Normal file
BIN
packages/thumbnail-handler/test/fixtures/wide.png
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 174 KiB |
30
packages/thumbnail-handler/test/index.test.ts
Normal file
30
packages/thumbnail-handler/test/index.test.ts
Normal 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()
|
||||
})
|
||||
})
|
||||
@ -1,8 +0,0 @@
|
||||
import 'mocha'
|
||||
import { expect } from 'chai'
|
||||
|
||||
describe('stub test', () => {
|
||||
it('should pass', () => {
|
||||
expect(true).to.be.true
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user