Skip to content

Commit

Permalink
Add tests for Mediawiki capabilities, improve checkApiAvailability() …
Browse files Browse the repository at this point in the history
…method
  • Loading branch information
VadimKovalenkoSNF committed Nov 15, 2023
1 parent e075a8b commit 40c5227
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/util/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ export const WEBP_HANDLER_URL = 'https://gist.githubusercontent.com/rgaudin/60bb
export const MAX_FILE_DOWNLOAD_RETRIES = 5
export const BLACKLISTED_NS = ['Story'] // 'Story' Wikipedia namespace is content, but not indgestable by Parsoid https://github.com/openzim/mwoffliner/issues/1853
export const RENDERERS_LIST = ['WikimediaDesktop', 'VisualEditor', 'WikimediaMobile', 'MediawikiRestApi']
/*
Handle redirection pages for 3rd party wikis that have 200 response code
Check this link: https://pokemon.fandom.com/w/api.php?action=visualeditor&mobileformat=html&format=json&paction=parse&formatversion=2&page=MediaWiki%3ASidebar
*/
export const REDIRECT_PAGE_SIGNATURE = 'Moved to'
7 changes: 6 additions & 1 deletion src/util/mw-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Timer from './Timer.js'
import axios from 'axios'
import RedisStore from '../RedisStore.js'
import MediaWiki from '../MediaWiki.js'
import { REDIRECT_PAGE_SIGNATURE } from './const.js'

export async function getArticlesByIds(articleIds: string[], downloader: Downloader, log = true): Promise<void> {
let from = 0
Expand Down Expand Up @@ -260,7 +261,11 @@ export function mwRetToArticleDetail(obj: QueryMwRet): KVS<ArticleDetail> {
export async function checkApiAvailability(url: string, loginCookie = ''): Promise<boolean> {
try {
const resp = await axios.get(decodeURI(url), { maxRedirects: 0, headers: { cookie: loginCookie } })
return resp.status === 200 && !resp.headers['mediawiki-api-error']

const isRedirectPage = typeof resp.data === 'string' && resp.data.startsWith(REDIRECT_PAGE_SIGNATURE)
const isSuccess = resp.status === 200 && !resp.headers['mediawiki-api-error']

return !isRedirectPage && isSuccess
} catch (err) {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/openstreetmap.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ await testRenders(
})
})
},
['MediawikiRestApi'],
['MediawikiRestApi', 'VisualEditor'],
)
2 changes: 1 addition & 1 deletion test/e2e/wikisource.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,5 @@ await testRenders(
}
})
},
['WikimediaDesktop', 'VisualEditor'],
['WikimediaDesktop', 'VisualEditor', 'MediawikiRestApi'],
)
85 changes: 85 additions & 0 deletions test/unit/mwApiCapabilities.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import MediaWiki from '../../src/MediaWiki.js'

describe('Checking Mediawiki capabilities', () => {
beforeEach(() => {
MediaWiki.reset()
})

afterEach(() => {
MediaWiki.reset()
})

test('test capabilities of en.wikipedia.org', async () => {
MediaWiki.base = 'https://en.wikipedia.org'
expect(await MediaWiki.hasWikimediaDesktopApi()).toBe(true)
expect(await MediaWiki.hasWikimediaMobileApi()).toBe(true)
expect(await MediaWiki.hasMediawikiRestApi()).toBe(true)
expect(await MediaWiki.hasVisualEditorApi()).toBe(true)
})

test('test capabilities of wiki.openstreetmap.org', async () => {
MediaWiki.base = 'https://wiki.openstreetmap.org'

expect(await MediaWiki.hasWikimediaDesktopApi()).toBe(false)
expect(await MediaWiki.hasWikimediaMobileApi()).toBe(false)
expect(await MediaWiki.hasMediawikiRestApi()).toBe(true)
expect(await MediaWiki.hasVisualEditorApi()).toBe(true)
})

test('test capabilities of fo.wikisource.org', async () => {
MediaWiki.base = 'https://fo.wikisource.org'

expect(await MediaWiki.hasWikimediaDesktopApi()).toBe(true)
expect(await MediaWiki.hasWikimediaMobileApi()).toBe(false)
expect(await MediaWiki.hasMediawikiRestApi()).toBe(true)
expect(await MediaWiki.hasVisualEditorApi()).toBe(true)
})

test('test capabilities of minecraft.wiki with correct VisualEditor receipt', async () => {
MediaWiki.base = 'https://minecraft.wiki'
MediaWiki.wikiPath = '/'
MediaWiki.actionApiPath = 'api.php'

expect(await MediaWiki.hasWikimediaDesktopApi()).toBe(false)
expect(await MediaWiki.hasWikimediaMobileApi()).toBe(false)
expect(await MediaWiki.hasMediawikiRestApi()).toBe(false)
expect(await MediaWiki.hasVisualEditorApi()).toBe(true)
})

test('test capabilities of pokemon.fandom.com with correct VisualEditor receipt', async () => {
MediaWiki.base = 'https://pokemon.fandom.com/'
MediaWiki.wikiPath = '/'
MediaWiki.actionApiPath = 'api.php'

expect(await MediaWiki.hasWikimediaDesktopApi()).toBe(false)
expect(await MediaWiki.hasWikimediaMobileApi()).toBe(false)
expect(await MediaWiki.hasMediawikiRestApi()).toBe(false)
expect(await MediaWiki.hasVisualEditorApi()).toBe(true)
})

test('test capabilities of pokemon.fandom.com with default receipt', async () => {
MediaWiki.base = 'https://pokemon.fandom.com/'

expect(await MediaWiki.hasWikimediaDesktopApi()).toBe(false)
expect(await MediaWiki.hasWikimediaMobileApi()).toBe(false)
expect(await MediaWiki.hasMediawikiRestApi()).toBe(false)
expect(await MediaWiki.hasVisualEditorApi()).toBe(false)
})

test('test capabilities of pokemon.fandom.com with MediawikiRestApi receipt', async () => {
MediaWiki.base = 'https://pokemon.fandom.com/'
MediaWiki.wikiPath = '/'
MediaWiki.mediawikiRestApiPath = 'rest.php/v1/page/'

expect(await MediaWiki.hasWikimediaDesktopApi()).toBe(false)
expect(await MediaWiki.hasWikimediaMobileApi()).toBe(false)
expect(await MediaWiki.hasVisualEditorApi()).toBe(false)

/* TODO:
Title MediaWiki:Sidebar does not exist for Mediawiki Rest Api in pokemon.fandom.com for some reason. This will lead to incorrect capability check
See: https://pokemon.fandom.com/rest.php/v1/page/MediaWiki%3ASidebar/html
*/
MediaWiki.apiCheckArticleId = 'Volcarona'
expect(await MediaWiki.hasMediawikiRestApi()).toBe(true)
})
})

0 comments on commit 40c5227

Please sign in to comment.