Skip to content

Commit

Permalink
test: Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
j3soon committed Aug 4, 2024
1 parent c49e3dd commit cdc8eeb
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 5 deletions.
33 changes: 28 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand All @@ -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.

Expand All @@ -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!

Expand Down
5 changes: 5 additions & 0 deletions tests/unit-test/install-and-run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

cd /app/tests/unit-test
npm install
npm run test
55 changes: 55 additions & 0 deletions tests/unit-test/navigation.test.js
Original file line number Diff line number Diff line change
@@ -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}`);
});

0 comments on commit cdc8eeb

Please sign in to comment.