From 750861e92443fae4d8d0086ddb2b11484d368fa3 Mon Sep 17 00:00:00 2001 From: Vadim Kovalenko Date: Wed, 20 Sep 2023 18:53:03 +0300 Subject: [PATCH] Add e2e test for article treatments per renderer --- test/e2e/bm.e2e.test.ts | 5 +- test/e2e/rendererList.test.ts | 2 +- test/e2e/treatments.e2e.test.ts | 92 +++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 test/e2e/treatments.e2e.test.ts diff --git a/test/e2e/bm.e2e.test.ts b/test/e2e/bm.e2e.test.ts index 3412db56..99830323 100644 --- a/test/e2e/bm.e2e.test.ts +++ b/test/e2e/bm.e2e.test.ts @@ -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) @@ -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 }) diff --git a/test/e2e/rendererList.test.ts b/test/e2e/rendererList.test.ts index 8fd1ef03..a144a247 100644 --- a/test/e2e/rendererList.test.ts +++ b/test/e2e/rendererList.test.ts @@ -16,7 +16,7 @@ describe('renderName', () => { outputDirectory: testId, redis: process.env.REDIS, format: ['nopic'], - articleList: 'France', + articleList: 'Fàransi, Kanada', } beforeAll(async () => { diff --git a/test/e2e/treatments.e2e.test.ts b/test/e2e/treatments.e2e.test.ts new file mode 100644 index 00000000..38b559eb --- /dev/null +++ b/test/e2e/treatments.e2e.test.ts @@ -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: 'test@kiwix.org', + 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) + }) + } + } +})