diff --git a/README.md b/README.md index 52e61a9..f2859da 100644 --- a/README.md +++ b/README.md @@ -183,12 +183,35 @@ Other functions should still be tested manually: - The help menu item in the context menu should link to this GitHub page. - ar5iv tabs should have renamed title, and support navigation. -### Run Tests Locally +### Run Unit Tests Locally Launch the docker containers: ```sh -cd tests +cd tests/unit-test +docker compose up -d +``` + +Then run the tests: + +```sh +docker exec -t unit-test-jest-tests-1 \ + /app/tests/unit-test/install-and-run.sh +``` + +When done, stop the containers: + +```sh +cd tests/unit-test +docker compose down +``` + +### Run End-to-End Tests Locally + +Launch the docker containers: + +```sh +cd tests/end-to-end-test docker compose up -d ``` @@ -214,11 +237,11 @@ View the logs or open the following URLs for more details: When done, stop the containers: ```sh -cd tests +cd tests/end-to-end-test docker compose down ``` -### Interactive Testing +### Interactive End-to-End Testing Install VSCode and [Dev Containers](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) plugin. @@ -239,7 +262,7 @@ Launch a Terminal inside the dev container and run: apk add build-base linux-headers ``` -Open `tests/test_interactive.py`, select the first cell and press `Shift + Enter` and click `Install` (Install the `ipykernel`). +Open `tests/end-to-end-test/test_interactive.py`, select the first cell and press `Shift + Enter` and click `Install` (Install the `ipykernel`). You can now begin interactive testing! diff --git a/tests/unit-test/install-and-run.sh b/tests/unit-test/install-and-run.sh new file mode 100755 index 0000000..c84a968 --- /dev/null +++ b/tests/unit-test/install-and-run.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +cd /app/tests/unit-test +npm install +npm run test diff --git a/tests/unit-test/navigation.test.js b/tests/unit-test/navigation.test.js new file mode 100644 index 0000000..a178ec9 --- /dev/null +++ b/tests/unit-test/navigation.test.js @@ -0,0 +1,55 @@ +const yaml = require('js-yaml'); +const fs = require('fs'); + +import TARGET_URL_REGEXP_REPLACE from '../../firefox/target_url_regexp_replace.js'; + +function getTargetURL(url) { + for (const [regexp, replacement] of TARGET_URL_REGEXP_REPLACE) { + if (regexp.test(url)) + return url.replace(regexp, replacement); + } + return null; +} + +test('navigation rules', () => { + const testcases_path = "/app/tests/testcases/testcases.yaml"; + const testcases = yaml.load(fs.readFileSync(testcases_path, 'utf8')); + let n_success = 0 + for (const testcase of testcases.navigation) { + const { url, title, pdf_url, pdf_title, url2, title2, description, abs2pdf = true, pdf2abs = true } = testcase; + if (!abs2pdf && !pdf2abs) { + throw new Error("Both `abs2pdf` and `pdf2abs` are False."); + } + console.log(`Running navigation testcase: +- URL: ${url} +- Title: \`${title}\` +- PDF URL: ${pdf_url} +- PDF Title: \`${pdf_title}\` +- URL2: ${url2} +- Title2: \`${title2}\` +- Description: ${description} +- Tests + - Test abs2pdf? ${abs2pdf} + - Test pdf2abs? ${pdf2abs}` +); + if (abs2pdf) { + if (pdf_url) { + console.log("Checking (abs) url -> pdf_url...") + expect(getTargetURL(url)).toBe(pdf_url); + } else if (url2) { + console.log("Checking url -> url2...") + expect(getTargetURL(url)).toBe(url2); + } + } + if (pdf2abs) { + if (pdf_url) { + console.log("Checking pdf_url -> (abs) url...") + expect(getTargetURL(pdf_url)).toBe(url); + } + } + console.log("Testcase Succeeded") + n_success += 1 + } + console.log("All tests passed successfully!\n" + + `Success: ${n_success}/${n_success}`); +});