Skip to content

Commit

Permalink
Add e2e test for article treatments per renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
VadimKovalenkoSNF committed Sep 20, 2023
1 parent e3b51eb commit 750861e
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 4 deletions.
5 changes: 2 additions & 3 deletions test/e2e/bm.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import * as mwoffliner from '../../src/mwoffliner.lib.js'
import { execa } from 'execa'
import { renderers } from './rendererList.js'
import rimraf from 'rimraf'
import { zimcheckAvailable, zimcheck } from '../util.js'
import { zimcheckAvailable, zimdumpAvailable, zimcheck, zimdump } from '../util.js'
import 'dotenv/config.js'
import { jest } from '@jest/globals'
import { zimdumpAvailable, zimdump } from '../util.js'

jest.setTimeout(200000)

Expand All @@ -24,7 +23,7 @@ describe('bm', () => {
format: ['nopic'],
}

const renderParameters = { ...parameters, renderName: renderer }
const renderParameters = { ...parameters, forceRender: renderer }

test('Simple articleList', async () => {
await execa('redis-cli flushall', { shell: true })
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/rendererList.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('renderName', () => {
outputDirectory: testId,
redis: process.env.REDIS,
format: ['nopic'],
articleList: 'France',
articleList: 'Fàransi, Kanada',
}

beforeAll(async () => {
Expand Down
92 changes: 92 additions & 0 deletions test/e2e/treatments.e2e.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import 'dotenv/config.js'
import * as mwoffliner from '../../src/mwoffliner.lib.js'
import domino from 'domino'
import rimraf from 'rimraf'
import { execa } from 'execa'
import { jest } from '@jest/globals'
import { renderers } from './rendererList.js'
import { zimcheckAvailable, zimdumpAvailable, zimcheck, zimdump } from '../util.js'

jest.setTimeout(200000)

let zimcheckIsAvailable
let zimdumpIsAvailable

beforeAll(async () => {
zimcheckIsAvailable = await zimcheckAvailable()
zimdumpIsAvailable = await zimdumpAvailable()
})

const setDefaultParams = (renderName: string, testId: string) => {
return {
mwUrl: 'https://en.wikipedia.org',
adminEmail: '[email protected]',
outputDirectory: testId,
redis: process.env.REDIS,
articleList: 'User:Kelson/MWoffliner_CI_reference',
forceRender: renderName,
}
}

// Check the integrity of img elements between zim file and article html taken from it
const verifyImgElements = (imgFilesArr: string[], imgElements: DominoElement[]) => {
for (const img of imgElements) {
for (const imgFile of imgFilesArr) {
if (img.getAttribute('src').includes(imgFile)) {
return true
}
}
}
return false
}

const commonTreatmentTest = async (renderer) => {
if (!zimcheckIsAvailable) {
console.log('Zimcheck not installed, skipping test')
return
}
if (!zimdumpIsAvailable) {
console.log('Zimcdump not installed, skipping test')
return
}
const now = new Date()
const testId = `mwo-test-${+now}`

const parameters = setDefaultParams(renderer, testId)
await execa('redis-cli flushall', { shell: true })
const outFiles = await mwoffliner.execute(parameters)
await expect(zimcheck(outFiles[0].outFile)).resolves.not.toThrowError()

const articleFromDump = await zimdump(`show --url A/${parameters.articleList} ${outFiles[0].outFile}`)
const articleDoc = domino.createDocument(articleFromDump)

// TODO: test collapsible sections

// Test page header title
expect(articleDoc.querySelector('h1.article-header')).toBeTruthy()

// Check media files
const mediaFiles = await zimdump(`list --ns I ${outFiles[0].outFile}`)
const mediaFilesArr = mediaFiles.split('\n')
const imgFilesArr = mediaFilesArr.filter((elem) => elem.endsWith('pdf') || elem.endsWith('png') || elem.endsWith('jpg'))
const imgElements = Array.from(articleDoc.querySelectorAll('img'))

expect(verifyImgElements(imgFilesArr, imgElements)).toBe(true)

rimraf.sync(`./${testId}`)
}

describe('Treatments e2e', () => {
for (const renderer of renderers) {
if (renderer === 'WikimediaDesktop') {
test('WikimediaDesktop e2e', async () => {
await commonTreatmentTest(renderer)
})
}
if (renderer === 'VisualEditor') {
test('VisualEditor e2e', async () => {
await commonTreatmentTest(renderer)
})
}
}
})

0 comments on commit 750861e

Please sign in to comment.