add unit tests

This commit is contained in:
ading2210 2024-01-31 21:38:57 +00:00
parent b071019761
commit 2d98b82ee3
9 changed files with 147 additions and 2 deletions

View file

@ -12,11 +12,15 @@ jobs:
- name: install deps
run: |
sudo apt-get update
sudo apt-get install -y make cmake emscripten autoconf automake libtool pkg-config wget xxd
sudo apt-get install -y make cmake emscripten autoconf automake libtool pkg-config wget xxd python3-selenium python3-websockets
- name: run build
working-directory: ./client
run: ./build.sh all
- name: run tests
working-directory: ./client
run: ./tests/run.sh
- name: upload img
uses: actions/upload-artifact@v4

43
client/tests/index.html Normal file
View file

@ -0,0 +1,43 @@
<!DOCTYPE html>
<html>
<head>
<link rel="icon" href="data:;base64,=">
<script defer src="/out/libcurl.js" onload="libcurl.load_wasm('/out/libcurl.wasm');"></script>
<script>
function create_flag(result) {
let element = document.createElement("div");
element.setAttribute("result", result);
element.className = "flag";
document.body.append(element);
}
function assert(condition, message) {
if (!condition) {
throw new Error(message || "Assertion failed");
}
}
let script = document.createElement("script");
script.src = "/tests/scripts/" + location.hash.substring(1);
script.async = false;
script.defer = false;
document.head.append(script);
document.addEventListener("libcurl_load", async ()=>{
try {
libcurl.set_websocket(`ws://localhost:6001/ws/`);
await test();
create_flag("success");
}
catch (e) {
console.error(e.stack || e);
create_flag("error");
}
});
</script>
</head>
<body>
<p>libcurl.js unit test runner</p>
</body>
</html>

11
client/tests/run.sh Executable file
View file

@ -0,0 +1,11 @@
#!/bin/bash
set -e
trap "exit" INT TERM
trap "kill 0" EXIT
STATIC="$(pwd)" python3 ../server/wisp_server/main.py >/dev/null &
sleep 1
echo "Running tests"
python3 tests/run_tests.py

44
client/tests/run_tests.py Normal file
View file

@ -0,0 +1,44 @@
import unittest
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class JSTest(unittest.TestCase):
def setUp(self):
options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.set_capability("goog:loggingPrefs", {"browser": "ALL"})
self.browser = webdriver.Chrome(options=options)
def tearDown(self):
self.browser.quit()
def run_test(self, script):
self.browser.get(f"http://localhost:6001/tests/#{script}")
wait = WebDriverWait(self.browser, 20)
result = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.flag'))).get_attribute("result")
if result != "success":
for entry in self.browser.get_log("browser"):
print(f"{entry['level']}: {entry['message']}")
assert result == "success"
def test_fetch_once(self):
self.run_test("fetch_once.js")
def test_fetch_multiple(self):
self.run_test("fetch_multiple.js")
def test_fetch_parallel(self):
self.run_test("fetch_parallel.js")
def test_websocket(self):
self.run_test("test_websocket.js")
if __name__ == "__main__":
unittest.main()

View file

@ -0,0 +1,7 @@
async function test() {
await libcurl.fetch("https://example.com/");
for (let i=0; i<20; i++) {
let r = await libcurl.fetch("https://example.com/");
assert(r.status === 200, "wrong status");
}
}

View file

@ -0,0 +1,4 @@
async function test() {
let r = await libcurl.fetch("https://example.com/");
assert(r.status === 200, "wrong status");
}

View file

@ -0,0 +1,8 @@
async function test() {
await libcurl.fetch("https://www.example.com/");
let promises = [];
for (let i=0; i<10; i++) {
promises.push(libcurl.fetch("https://www.example.com/"))
}
await Promise.all(promises);
}

View file

@ -0,0 +1,24 @@
function test() {
let message_len = 128*1024;
return new Promise((resolve, reject) => {
let ws = new libcurl.WebSocket("wss://echo.websocket.org");
ws.addEventListener("open", () => {
ws.send("hello".repeat(message_len));
});
let messages = 0;
ws.addEventListener("message", (event) => {
messages += 1;
if (messages >= 2) {
if (event.data !== "hello".repeat(message_len)) reject("unexpected response");
if (messages >= 11) resolve();
ws.send("hello".repeat(message_len));
}
});
ws.addEventListener("error", () => {
reject("ws error occurred");
});
})
}

@ -1 +1 @@
Subproject commit 138ef0f73027b3d237ec84bee7ea43b4f30c8b74
Subproject commit bd7bca97b22023a1b49d0f5a09081ddcbb4ad49c