Skip to content

Commit

Permalink
Refactor tests (partial impl)
Browse files Browse the repository at this point in the history
  • Loading branch information
VadimKovalenkoSNF committed Sep 21, 2023
1 parent 32e1ff6 commit 6e2d33c
Show file tree
Hide file tree
Showing 19 changed files with 90 additions and 85 deletions.
14 changes: 7 additions & 7 deletions src/Downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,23 +171,23 @@ class Downloader {
if (!forceRender) {
//* Objects order in array matters!
this.baseUrl = basicURLDirector.buildDownloaderBaseUrl([
{ condition: await MediaWiki.hasWikimediaMobileRestApi(), value: MediaWiki.mobileRestApiUrl.href },
{ condition: await MediaWiki.hasWikimediaDesktopRestApi(), value: MediaWiki.desktopRestApiUrl.href },
{ condition: await MediaWiki.hasWikimediaMobileApi(), value: MediaWiki.mobileApiUrl.href },
{ condition: await MediaWiki.hasWikimediaDesktopApi(), value: MediaWiki.desktopApiUrl.href },
{ condition: await MediaWiki.hasVisualEditorApi(), value: MediaWiki.visualEditorApiUrl.href },
])

//* Objects order in array matters!
this.baseUrlForMainPage = basicURLDirector.buildDownloaderBaseUrl([
{ condition: await MediaWiki.hasWikimediaDesktopRestApi(), value: MediaWiki.desktopRestApiUrl.href },
{ condition: await MediaWiki.hasWikimediaDesktopApi(), value: MediaWiki.desktopApiUrl.href },
{ condition: await MediaWiki.hasVisualEditorApi(), value: MediaWiki.visualEditorApiUrl.href },
{ condition: await MediaWiki.hasWikimediaMobileRestApi(), value: MediaWiki.mobileRestApiUrl.href },
{ condition: await MediaWiki.hasWikimediaMobileApi(), value: MediaWiki.mobileApiUrl.href },
])
} else {
switch (forceRender) {
case 'WikimediaDesktop':
if (MediaWiki.hasWikimediaDesktopRestApi()) {
this.baseUrl = MediaWiki.desktopRestApiUrl.href
this.baseUrlForMainPage = MediaWiki.desktopRestApiUrl.href
if (MediaWiki.hasWikimediaDesktopApi()) {
this.baseUrl = MediaWiki.desktopApiUrl.href
this.baseUrlForMainPage = MediaWiki.desktopApiUrl.href
break
}
break
Expand Down
40 changes: 20 additions & 20 deletions src/MediaWiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ class MediaWiki {
public modulePath: string // only for reading
public _modulePathOpt: string // only for whiting to generate modulePath
public webUrl: URL
public desktopRestApiUrl: URL
public mobileRestApiUrl: URL
public desktopApiUrl: URL
public mobileApiUrl: URL

#hasWikimediaDesktopRestApi: boolean | null
#hasWikimediaMobileRestApi: boolean | null
#hasWikimediaDesktopApi: boolean | null
#hasWikimediaMobileApi: boolean | null
#hasVisualEditorApi: boolean | null
#hasCoordinates: boolean | null

Expand Down Expand Up @@ -123,8 +123,8 @@ class MediaWiki {
formatversion: '2',
}

this.#hasWikimediaDesktopRestApi = null
this.#hasWikimediaMobileRestApi = null
this.#hasWikimediaDesktopApi = null
this.#hasWikimediaMobileApi = null
this.#hasVisualEditorApi = null
this.#hasCoordinates = null
}
Expand All @@ -133,20 +133,20 @@ class MediaWiki {
this.initializeMediaWikiDefaults()
}

public async hasWikimediaDesktopRestApi(): Promise<boolean> {
if (this.#hasWikimediaDesktopRestApi === null) {
this.#hasWikimediaDesktopRestApi = await checkApiAvailability(this.wikimediaDesktopUrlDirector.buildArticleURL(this.apiCheckArticleId))
return this.#hasWikimediaDesktopRestApi
public async hasWikimediaDesktopApi(): Promise<boolean> {
if (this.#hasWikimediaDesktopApi === null) {
this.#hasWikimediaDesktopApi = await checkApiAvailability(this.wikimediaDesktopUrlDirector.buildArticleURL(this.apiCheckArticleId))
return this.#hasWikimediaDesktopApi
}
return this.#hasWikimediaDesktopRestApi
return this.#hasWikimediaDesktopApi
}

public async hasWikimediaMobileRestApi(): Promise<boolean> {
if (this.#hasWikimediaMobileRestApi === null) {
this.#hasWikimediaMobileRestApi = await checkApiAvailability(this.wikimediaMobileUrlDirector.buildArticleURL(this.apiCheckArticleId))
return this.#hasWikimediaMobileRestApi
public async hasWikimediaMobileApi(): Promise<boolean> {
if (this.#hasWikimediaMobileApi === null) {
this.#hasWikimediaMobileApi = await checkApiAvailability(this.wikimediaMobileUrlDirector.buildArticleURL(this.apiCheckArticleId))
return this.#hasWikimediaMobileApi
}
return this.#hasWikimediaMobileRestApi
return this.#hasWikimediaMobileApi
}

public async hasVisualEditorApi(): Promise<boolean> {
Expand Down Expand Up @@ -182,11 +182,11 @@ class MediaWiki {
this.apiUrl = baseUrlDirector.buildURL(this.#apiPath)
this.apiUrlDirector = new ApiURLDirector(this.apiUrl.href)
this.visualEditorApiUrl = this.apiUrlDirector.buildVisualEditorURL()
this.desktopRestApiUrl = baseUrlDirector.buildDesktopRestApiURL(this.#restApiPath)
this.mobileRestApiUrl = baseUrlDirector.buildMobileRestApiURL(this.#restApiPath)
this.desktopApiUrl = baseUrlDirector.buildDesktopApiUrl(this.#restApiPath)
this.mobileApiUrl = baseUrlDirector.buildMobileApiUrl(this.#restApiPath)
this.modulePath = baseUrlDirector.buildModuleURL(this._modulePathOpt)
this.wikimediaDesktopUrlDirector = new DesktopURLDirector(this.desktopRestApiUrl.href)
this.wikimediaMobileUrlDirector = new MobileURLDirector(this.mobileRestApiUrl.href)
this.wikimediaDesktopUrlDirector = new DesktopURLDirector(this.desktopApiUrl.href)
this.wikimediaMobileUrlDirector = new MobileURLDirector(this.mobileApiUrl.href)
this.visualEditorURLDirector = new VisualEditorURLDirector(this.visualEditorApiUrl.href)
}

Expand Down
6 changes: 3 additions & 3 deletions src/mwoffliner.lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ async function execute(argv: any) {

MediaWiki.apiCheckArticleId = mwMetaData.mainPage
await MediaWiki.hasCoordinates(downloader)
await MediaWiki.hasWikimediaDesktopRestApi()
const hasWikimediaMobileRestApi = await MediaWiki.hasWikimediaMobileRestApi()
await MediaWiki.hasWikimediaDesktopApi()
const hasWikimediaMobileApi = await MediaWiki.hasWikimediaMobileApi()
await MediaWiki.hasVisualEditorApi()
await downloader.setBaseUrls(forceRender)

Expand Down Expand Up @@ -421,7 +421,7 @@ async function execute(argv: any) {

logger.log('Getting articles')
stime = Date.now()
const { jsModuleDependencies, cssModuleDependencies } = await saveArticles(zimCreator, downloader, dump, hasWikimediaMobileRestApi, forceRender)
const { jsModuleDependencies, cssModuleDependencies } = await saveArticles(zimCreator, downloader, dump, hasWikimediaMobileApi, forceRender)
logger.log(`Fetching Articles finished in ${(Date.now() - stime) / 1000} seconds`)

logger.log(`Found [${jsModuleDependencies.size}] js module dependencies`)
Expand Down
18 changes: 9 additions & 9 deletions src/renderers/renderer.builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ export class RendererBuilder {
public async createRenderer(options: RendererBuilderOptions): Promise<Renderer> {
const { renderType, renderName } = options

const [hasVisualEditorApi, hasWikimediaDesktopRestApi, hasWikimediaMobileRestApi] = await Promise.all([
const [hasVisualEditorApi, hasWikimediaDesktopApi, hasWikimediaMobileApi] = await Promise.all([
MediaWiki.hasVisualEditorApi(),
MediaWiki.hasWikimediaDesktopRestApi(),
MediaWiki.hasWikimediaMobileRestApi(),
MediaWiki.hasWikimediaDesktopApi(),
MediaWiki.hasWikimediaMobileApi(),
])

switch (renderType) {
case 'desktop':
if (hasWikimediaDesktopRestApi) {
if (hasWikimediaDesktopApi) {
// Choose WikimediaDesktopRenderer if it's present, regardless of hasVisualEditorApi value
return new WikimediaDesktopRenderer()
} else if (hasVisualEditorApi) {
Expand All @@ -28,18 +28,18 @@ export class RendererBuilder {
process.exit(1)
}
case 'mobile':
if (hasWikimediaMobileRestApi) {
if (hasWikimediaMobileApi) {
return new WikimediaMobileRenderer()
}
logger.error('No available mobile renderer.')
process.exit(1)
case 'auto':
if (hasWikimediaDesktopRestApi) {
if (hasWikimediaDesktopApi) {
// Choose WikimediaDesktopRenderer if it's present, regardless of hasVisualEditorApi value
return new WikimediaDesktopRenderer()
} else if (hasVisualEditorApi) {
return new VisualEditorRenderer()
} else if (hasWikimediaMobileRestApi) {
} else if (hasWikimediaMobileApi) {
return new WikimediaMobileRenderer()
} else {
logger.error('No render available at all.')
Expand All @@ -49,7 +49,7 @@ export class RendererBuilder {
// renderName argument is required for 'specific' mode
switch (renderName) {
case 'WikimediaDesktop':
if (hasWikimediaDesktopRestApi) {
if (hasWikimediaDesktopApi) {
return new WikimediaDesktopRenderer()
}
logger.error('Cannot create an instance of WikimediaDesktop renderer.')
Expand All @@ -61,7 +61,7 @@ export class RendererBuilder {
logger.error('Cannot create an instance of VisualEditor renderer.')
process.exit(1)
case 'WikimediaMobile':
if (hasWikimediaMobileRestApi) {
if (hasWikimediaMobileApi) {
return new WikimediaMobileRenderer()
}
logger.error('No available mobile renderer.')
Expand Down
3 changes: 3 additions & 0 deletions src/renderers/wikimedia-mobile.renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ export class WikimediaMobileRenderer extends Renderer {
.reference-link::after {
content: none !important;
}
.mw-body h3, .mw-body h2 {
width: auto;
}
`
doc.head.appendChild(styleEl)

Expand Down
6 changes: 3 additions & 3 deletions src/util/builders/url/base.director.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ export default class BaseURLDirector {
return urlBuilder.setDomain(this.baseDomain).setPath(path).build(true)
}

buildRestApiURL(path?: string) {
buildWikimediaApiURL(path?: string) {
return urlBuilder
.setDomain(this.baseDomain)
.setPath(path ?? 'api/rest_v1')
.build(true, '/')
}

buildDesktopRestApiURL(path?: string) {
buildDesktopApiUrl(path?: string) {
return urlBuilder
.setDomain(this.baseDomain)
.setPath(path ?? 'api/rest_v1/page/html')
.build(true, '/')
}

buildMobileRestApiURL(path?: string) {
buildMobileApiUrl(path?: string) {
return urlBuilder
.setDomain(this.baseDomain)
.setPath(path ?? 'api/rest_v1/page/mobile-html')
Expand Down
27 changes: 11 additions & 16 deletions src/util/saveArticles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { config } from '../config.js'
import { getSizeFromUrl, cleanupAxiosError } from './misc.js'
import { CONCURRENCY_LIMIT, DELETED_ARTICLE_ERROR, MAX_FILE_DOWNLOAD_RETRIES } from './const.js'
import urlHelper from './url.helper.js'
import { RendererBuilderOptions, Renderer } from '../renderers/abstract.renderer.js'
import { Renderer } from '../renderers/abstract.renderer.js'
import { RendererBuilder } from '../renderers/renderer.builder.js'

export async function downloadFiles(fileStore: RKVS<FileDetail>, retryStore: RKVS<FileDetail>, zimCreator: ZimCreator, dump: Dump, downloader: Downloader, retryCounter = 0) {
Expand Down Expand Up @@ -231,7 +231,7 @@ export function getArticleUrl(downloader: Downloader, dump: Dump, articleId: str
/*
* Fetch Articles
*/
export async function saveArticles(zimCreator: ZimCreator, downloader: Downloader, dump: Dump, hasWikimediaMobileRestApi: boolean, forceRender = null) {
export async function saveArticles(zimCreator: ZimCreator, downloader: Downloader, dump: Dump, hasWikimediaMobileApi: boolean, forceRender = null) {
const jsModuleDependencies = new Set<string>()
const cssModuleDependencies = new Set<string>()
let jsConfigVars = ''
Expand All @@ -241,26 +241,21 @@ export async function saveArticles(zimCreator: ZimCreator, downloader: Downloade

const rendererBuilder = new RendererBuilder()

let rendererBuilderOptions: RendererBuilderOptions

let mainPageRenderer
let articlesRenderer
if (forceRender) {
rendererBuilderOptions = {
// All articles and main page will use the same renderer if 'forceRender' is specified
const renderer = await rendererBuilder.createRenderer({
renderType: 'specific',
renderName: forceRender,
}
// All articles and main page will use the same renderer if 'forceRender' is specified
mainPageRenderer = await rendererBuilder.createRenderer(rendererBuilderOptions)
articlesRenderer = await rendererBuilder.createRenderer(rendererBuilderOptions)
})
mainPageRenderer = renderer
articlesRenderer = renderer
} else {
rendererBuilderOptions = {
renderType: 'desktop',
}
mainPageRenderer = await rendererBuilder.createRenderer(rendererBuilderOptions)
// If the mobile renderer API is not available, switch articles rendering to the auto mode instead
rendererBuilderOptions.renderType = hasWikimediaMobileRestApi ? 'mobile' : 'auto'
articlesRenderer = await rendererBuilder.createRenderer(rendererBuilderOptions)
mainPageRenderer = await rendererBuilder.createRenderer({ renderType: 'desktop' })
articlesRenderer = await rendererBuilder.createRenderer({
renderType: hasWikimediaMobileApi ? 'mobile' : 'auto',
})
}

if (dump.customProcessor?.shouldKeepArticle) {
Expand Down
1 change: 1 addition & 0 deletions test/e2e/articleLists.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('articleList', () => {
outputDirectory: testId,
redis: process.env.REDIS,
format: ['nopic'],
forceRender: 'WikimediaDesktop',
}

test('articleList and articleListIgnore check', async () => {
Expand Down
1 change: 1 addition & 0 deletions test/e2e/bm.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('bm', () => {
outputDirectory: testId,
redis: process.env.REDIS,
format: ['nopic'],
forceRender: 'WikimediaDesktop',
}

test('Simple articleList', async () => {
Expand Down
1 change: 1 addition & 0 deletions test/e2e/en10.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('en10', () => {
redis: process.env.REDIS,
// format: ['nopic', 'novid', 'nopdf', 'nodet'],
format: ['nopic', 'nopdf'],
forceRender: 'WikimediaDesktop',
}

test('Simple articleList', async () => {
Expand Down
1 change: 1 addition & 0 deletions test/e2e/extra.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ AC/DC`
outputDirectory: testId,
redis: process.env.REDIS,
format: ['nopic'],
forceRender: 'WikimediaDesktop',
})

// Created 1 outputs
Expand Down
18 changes: 9 additions & 9 deletions test/unit/builders/url/base.director.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,43 @@ describe('BaseURLDirector', () => {
})
})

describe('buildRestApiURL', () => {
describe('buildWikimediaApiURL', () => {
it('should return rest URL with provided path and trailing char at the end', () => {
const url = baseUrlDirector.buildRestApiURL('api/rest_v2')
const url = baseUrlDirector.buildWikimediaApiURL('api/rest_v2')

expect(url.href).toBe('https://en.m.wikipedia.com/api/rest_v2/')
})

it('should return rest URL with default path and trailing char at the end', () => {
const url = baseUrlDirector.buildRestApiURL()
const url = baseUrlDirector.buildWikimediaApiURL()

expect(url.href).toBe('https://en.m.wikipedia.com/api/rest_v1/')
})
})

describe('buildMobileRestApiURL', () => {
describe('buildMobileApiUrl', () => {
it('should return mobile rest URL with provided path and trailing char', () => {
const url = baseUrlDirector.buildMobileRestApiURL('api/rest_v2/page/mobile-html')
const url = baseUrlDirector.buildMobileApiUrl('api/rest_v2/page/mobile-html')

expect(url.href).toBe('https://en.m.wikipedia.com/api/rest_v2/page/mobile-html/')
})

it('should return mobile rest URL with default path and trailing char', () => {
const url = baseUrlDirector.buildMobileRestApiURL()
const url = baseUrlDirector.buildMobileApiUrl()

expect(url.href).toBe('https://en.m.wikipedia.com/api/rest_v1/page/mobile-html/')
})
})

describe('buildDesktopRestApiURL', () => {
describe('buildDesktopApiUrl', () => {
it('should return a desktop URL with provided path and trailing char', () => {
const url = baseUrlDirector.buildDesktopRestApiURL('api/rest_v2/page/html')
const url = baseUrlDirector.buildDesktopApiUrl('api/rest_v2/page/html')

expect(url.href).toBe('https://en.m.wikipedia.com/api/rest_v2/page/html/')
})

it('should return a desktop URL with default path and trailing char', () => {
const url = baseUrlDirector.buildDesktopRestApiURL()
const url = baseUrlDirector.buildDesktopApiUrl()

expect(url.href).toBe('https://en.m.wikipedia.com/api/rest_v1/page/html/')
})
Expand Down
4 changes: 2 additions & 2 deletions test/unit/downloader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ describe('Downloader class', () => {

await MediaWiki.getMwMetaData(downloader)
await MediaWiki.hasCoordinates(downloader)
await MediaWiki.hasWikimediaDesktopRestApi()
await MediaWiki.hasWikimediaMobileRestApi()
await MediaWiki.hasWikimediaDesktopApi()
await MediaWiki.hasWikimediaMobileApi()
await MediaWiki.hasVisualEditorApi()
await downloader.setBaseUrls()
})
Expand Down
4 changes: 2 additions & 2 deletions test/unit/mwApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ describe('mwApi', () => {

await MediaWiki.getMwMetaData(downloader)
await MediaWiki.hasCoordinates(downloader)
await MediaWiki.hasWikimediaDesktopRestApi()
await MediaWiki.hasWikimediaMobileRestApi()
await MediaWiki.hasWikimediaDesktopApi()
await MediaWiki.hasWikimediaMobileApi()
await MediaWiki.hasVisualEditorApi()

await MediaWiki.getNamespaces([], downloader)
Expand Down
Loading

0 comments on commit 6e2d33c

Please sign in to comment.