Skip to content

Commit

Permalink
Add unit test for donwloader.getArticleUrl(), fix VE url builder
Browse files Browse the repository at this point in the history
  • Loading branch information
VadimKovalenkoSNF committed Oct 17, 2023
1 parent 3a5526e commit c0b1112
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
2 changes: 0 additions & 2 deletions src/Downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ export const defaultStreamRequestOptions: AxiosRequestConfig = {
class Downloader {
public loginCookie = ''
public readonly speed: number
public baseUrl: string
public baseUrlForMainPage: string
public cssDependenceUrls: KVS<boolean> = {}
public readonly webp: boolean = false
public readonly requestTimeout: number
Expand Down
5 changes: 1 addition & 4 deletions src/util/builders/url/api.director.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ export default class ApiURLDirector {
}

buildVisualEditorURL() {
return urlBuilder
.setDomain(this.baseDomain)
.setQueryParams({ action: 'visualeditor', mobileformat: 'html', format: 'json', paction: 'parse', formatversion: '2', page: '' })
.build(true)
return urlBuilder.setDomain(this.baseDomain).setQueryParams({ action: 'visualeditor', mobileformat: 'html', format: 'json', paction: 'parse', formatversion: '2' }).build(true)
}

buildArticleApiURL(articleId: string) {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/builders/url/api.director.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('ApiURLDirector', () => {
it('should return base visual editor URL object with default query params', () => {
const url = apiUrlDirector.buildVisualEditorURL()

expect(url.href).toBe('https://en.wikipedia.org/w/api.php?action=visualeditor&mobileformat=html&format=json&paction=parse&formatversion=2&page=')
expect(url.href).toBe('https://en.wikipedia.org/w/api.php?action=visualeditor&mobileformat=html&format=json&paction=parse&formatversion=2')
})
})
})
24 changes: 22 additions & 2 deletions test/unit/downloader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Axios from 'axios'
import { mwRetToArticleDetail, stripHttpFromUrl, isImageUrl } from '../../src/util/index.js'
import S3 from '../../src/S3.js'
import { Dump } from '../../src/Dump.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'
Expand All @@ -15,6 +14,9 @@ import urlParser from 'url'
import { setTimeout } from 'timers/promises'
import domino from 'domino'
import { setupScrapeClasses } from '../util.js'
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'

jest.setTimeout(200000)

Expand All @@ -27,7 +29,6 @@ describe('Downloader class', () => {
beforeAll(async () => {
MediaWiki.base = 'https://en.wikipedia.org'
MediaWiki.getCategories = true

downloader = new Downloader({ uaString: `${config.userAgent} ([email protected])`, speed: 1, reqTimeout: 1000 * 60, webp: true, optimisationCacheUrl: '' })

await MediaWiki.getMwMetaData(downloader)
Expand Down Expand Up @@ -212,6 +213,25 @@ describe('Downloader class', () => {
})
})

describe('getArticleUrl method for en.wikipedia.org', () => {
const articleId = 'Canada'
test('test article url for WikimediaDesktop renderer', () => {
const wikimediaDesktopRenderer = new WikimediaDesktopRenderer()
const articleUrl = downloader.getArticleUrl(wikimediaDesktopRenderer, articleId)
expect(articleUrl).toBe('https://en.wikipedia.org/api/rest_v1/page/html/Canada')
})
test('test article url for WikimediaMobile renderer', () => {
const wikimediaMobileRenderer = new WikimediaMobileRenderer()
const articleUrl = downloader.getArticleUrl(wikimediaMobileRenderer, articleId)
expect(articleUrl).toBe('https://en.wikipedia.org/api/rest_v1/page/mobile-html/Canada')
})
test('test article url for VisualEditor renderer', () => {
const visualEditorRenderer = new VisualEditorRenderer()
const articleUrl = downloader.getArticleUrl(visualEditorRenderer, articleId)
expect(articleUrl).toBe('https://en.wikipedia.org/w/api.php?action=visualeditor&mobileformat=html&format=json&paction=parse&formatversion=2&page=Canada')
})
})

describe('isImageUrl method', () => {
test('Checked Image type: png', async () => {
const isPngFile = isImageUrl('https://bm.wikipedia.org/static/images/project-logos/bmwiki-2x.svg.png')
Expand Down

0 comments on commit c0b1112

Please sign in to comment.