Skip to content

Commit

Permalink
Apply renders to unit tests (partial impl)
Browse files Browse the repository at this point in the history
  • Loading branch information
VadimKovalenkoSNF committed Oct 19, 2023
1 parent 3976656 commit 39ba175
Show file tree
Hide file tree
Showing 4 changed files with 317 additions and 302 deletions.
1 change: 1 addition & 0 deletions test/e2e/formatParams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ await testAllRenders({ ...parameters, format: 'nopdf', articleList: 'PDF' }, asy
const articleFromDump = await zimdump(`show --url A/PDF ${outFiles[0].outFile}`)
const articleDoc = domino.createDocument(articleFromDump)
// TODO: blocked by issues/1928
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const anchorElements = Array.from(articleDoc.querySelectorAll('a'))
})
})
Expand Down
165 changes: 91 additions & 74 deletions test/unit/downloader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import { mwRetToArticleDetail, stripHttpFromUrl, isImageUrl } from '../../src/ut
import S3 from '../../src/S3.js'
import { Dump } from '../../src/Dump.js'
import { getArticleUrl } from '../../src/util/saveArticles.js'
import { WikimediaDesktopRenderer } from '../../src/renderers/wikimedia-desktop.renderer.js'
import { config } from '../../src/config.js'
import 'dotenv/config.js'
import * as FileType from 'file-type'
import { jest } from '@jest/globals'
import urlParser from 'url'
import { setTimeout } from 'timers/promises'
import domino from 'domino'
import { WikimediaDesktopRenderer } from '../../src/renderers/wikimedia-desktop.renderer.js'
import { VisualEditorRenderer } from '../../src/renderers/visual-editor.renderer.js'
import { WikimediaMobileRenderer } from '../../src/renderers/wikimedia-mobile.renderer.js'
import { RENDERERS_LIST } from '../../src/util/const.js'

jest.setTimeout(200000)

Expand Down Expand Up @@ -125,89 +128,103 @@ describe('Downloader class', () => {
})

describe('getArticle method', () => {
let dump: Dump
const wikimediaDesktopRenderer = new WikimediaDesktopRenderer()

beforeAll(async () => {
const mwMetadata = await MediaWiki.getMwMetaData(downloader)
dump = new Dump('', {} as any, mwMetadata)
})

test('getArticle of "London" returns one article', async () => {
const articleId = 'London'
const articleUrl = getArticleUrl(downloader, dump, articleId)
const articleDetail = {
title: articleId,
thumbnail: {
width: 50,
height: 28,
source: 'https://upload.wikimedia.org/wikipedia/commons/thumb/6/67/London_Skyline_%28125508655%29.jpeg/50px-London_Skyline_%28125508655%29.jpeg',
},
revisionId: 1171340841,
timestamp: '2023-08-20T14:54:01Z',
coordinates: '51.50722222;-0.1275',
for (const renderer of RENDERERS_LIST) {
let rendererInstance
switch (renderer) {
case 'VisualEditor':
rendererInstance = new VisualEditorRenderer()
break
case 'WikimediaDesktop':
rendererInstance = new WikimediaDesktopRenderer()
break
case 'WikimediaMobile':
rendererInstance = new WikimediaMobileRenderer()
break
default:
throw new Error(`Unknown renderer: ${renderer}`)
}
const _moduleDependencies = await downloader.getModuleDependencies(articleDetail.title)
const LondonArticle = await downloader.getArticle(
downloader.webp,
_moduleDependencies,
articleId,
RedisStore.articleDetailXId,
wikimediaDesktopRenderer,
articleUrl,
dump,
articleDetail,
dump.isMainPage(articleId),
)
expect(LondonArticle).toHaveLength(1)
})
let dump: Dump
beforeAll(async () => {
const mwMetadata = await MediaWiki.getMwMetaData(downloader)
dump = new Dump('', {} as any, mwMetadata)
})

test('Categories with many subCategories are paginated', async () => {
const articleId = 'Category:Container_categories'
const _moduleDependencies = await downloader.getModuleDependencies(articleId)
const articleDetail = {
title: articleId,
ns: 14,
revisionId: 1168361498,
timestamp: '2023-08-02T09:57:11Z',
}
const articleUrl = getArticleUrl(downloader, dump, articleDetail.title)
const PaginatedArticle = await downloader.getArticle(
downloader.webp,
_moduleDependencies,
articleId,
RedisStore.articleDetailXId,
wikimediaDesktopRenderer,
articleUrl,
dump,
articleDetail,
dump.isMainPage(articleId),
)
expect(PaginatedArticle.length).toBeGreaterThan(100)
})
test(`getArticle of "London" returns one article for ${renderer} render`, async () => {
const articleId = 'London'
const articleUrl = getArticleUrl(downloader, dump, articleId)
const articleDetail = {
title: articleId,
thumbnail: {
width: 50,
height: 28,
source: 'https://upload.wikimedia.org/wikipedia/commons/thumb/6/67/London_Skyline_%28125508655%29.jpeg/50px-London_Skyline_%28125508655%29.jpeg',
},
revisionId: 1171340841,
timestamp: '2023-08-20T14:54:01Z',
coordinates: '51.50722222;-0.1275',
}
const _moduleDependencies = await downloader.getModuleDependencies(articleDetail.title)
const LondonArticle = await downloader.getArticle(
downloader.webp,
_moduleDependencies,
articleId,
RedisStore.articleDetailXId,
rendererInstance,
articleUrl,
dump,
articleDetail,
dump.isMainPage(articleId),
)
expect(LondonArticle).toHaveLength(1)
})

test('getArticle response status for non-existent article id is 404', async () => {
const articleId = 'NeverExistingArticle'
const articleUrl = getArticleUrl(downloader, dump, articleId)
const articleDetail = {
title: articleId,
missing: '',
}
const _moduleDependencies = await downloader.getModuleDependencies(articleDetail.title)
await expect(
downloader.getArticle(
test(`Categories with many subCategories are paginated for ${renderer} render`, async () => {
const articleId = 'Category:Container_categories'
const _moduleDependencies = await downloader.getModuleDependencies(articleId)
const articleDetail = {
title: articleId,
ns: 14,
revisionId: 1168361498,
timestamp: '2023-08-02T09:57:11Z',
}
const articleUrl = getArticleUrl(downloader, dump, articleDetail.title)
const PaginatedArticle = await downloader.getArticle(
downloader.webp,
_moduleDependencies,
'NeverExistingArticle',
articleId,
RedisStore.articleDetailXId,
wikimediaDesktopRenderer,
rendererInstance,
articleUrl,
dump,
articleDetail,
dump.isMainPage(articleId),
),
).rejects.toThrowError(new Error('Request failed with status code 404'))
})
)
expect(PaginatedArticle.length).toBeGreaterThan(100)
})

test(`getArticle response status for non-existent article id is 404 for ${renderer} render`, async () => {
const articleId = 'NeverExistingArticle'
const articleUrl = getArticleUrl(downloader, dump, articleId)
const articleDetail = {
title: articleId,
missing: '',
}
const _moduleDependencies = await downloader.getModuleDependencies(articleDetail.title)
await expect(
downloader.getArticle(
downloader.webp,
_moduleDependencies,
'NeverExistingArticle',
RedisStore.articleDetailXId,
rendererInstance,
articleUrl,
dump,
articleDetail,
dump.isMainPage(articleId),
),
).rejects.toThrowError(new Error('Request failed with status code 404'))
})
}
})

describe('isImageUrl method', () => {
Expand Down
Loading

0 comments on commit 39ba175

Please sign in to comment.