Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

4.8 KB
Raw Blame
"""Exercise the running production image before and after a container restart.

This standalone CLI uses loopback HTTP and a disposable test account. Its state
file holds generated test credentials and stays in the temporary CI volume.
"""

import argparse
import json
import secrets
import time
from contextlib import closing
from pathlib import Path

import requests


def wait_until_ready(base_url):
    deadline = time.monotonic() + 120
    with requests.Session() as client:
        client.trust_env = False
        while time.monotonic() < deadline:
            try:
                response = client.get(base_url + "/api/v1/health", timeout=2)
                if response.status_code == 200:
                    assert response.json()["status"] == "ok"
                    return
            except requests.RequestException:
                pass
            # CLI readiness polling; this probe is not collected by pytest.
            time.sleep(1)  # allow: unmarked-sleep
    raise RuntimeError(
        "Production server did not become ready within 120 seconds"
    )


def csrf(client, base_url):
    response = client.get(base_url + "/auth/csrf-token", timeout=10)
    response.raise_for_status()
    return response.json()["csrf_token"]


def login(client, base_url, state):
    response = client.post(
        base_url + "/auth/login",
        data={
            "username": state["username"],
            "password": state["password"],
            "csrf_token": csrf(client, base_url),
        },
        allow_redirects=False,
        timeout=30,
    )
    assert response.status_code == 302, "Login failed"
    response = client.get(base_url + "/auth/check", timeout=10)
    response.raise_for_status()
    assert response.json()["username"] == state["username"]


def check_saved_setting(client, base_url, value):
    response = client.get(
        base_url + "/settings/api/search.iterations", timeout=10
    )
    response.raise_for_status()
    assert response.json()["value"] == value, "Saved setting did not survive"
    response = client.get(base_url + "/api/v1/health", timeout=10)
    response.raise_for_status()
    assert response.json()["subsystems"]["queue_processor"] == "ok"
    assert response.json()["subsystems"]["db_manager"] == "ok"


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("phase", choices=("seed", "verify"))
    parser.add_argument("state", type=Path)
    parser.add_argument("--base-url", default="http://127.0.0.1:5000")
    args = parser.parse_args()
    wait_until_ready(args.base_url)

    # Import the actual production SQLCipher wheel on each native architecture.
    from sqlcipher3 import dbapi2

    with closing(dbapi2.connect(":memory:")) as connection:
        assert connection.execute("PRAGMA cipher_version").fetchone()[0]

    with requests.Session() as client:
        client.trust_env = False
        client.headers["Accept"] = "application/json"
        if args.phase == "seed":
            state = {
                "username": "prod_smoke_" + secrets.token_hex(6),
                "password": secrets.token_urlsafe(24) + "aA1!",
                "value": 7,
            }
            response = client.post(
                args.base_url + "/auth/register",
                data={
                    "username": state["username"],
                    "password": state["password"],
                    "confirm_password": state["password"],
                    "acknowledge": "true",
                    "csrf_token": csrf(client, args.base_url),
                },
                allow_redirects=False,
                timeout=60,
            )
            assert response.status_code == 302, "Registration failed"
            login(client, args.base_url, state)
            response = client.put(
                args.base_url + "/settings/api/search.iterations",
                json={"value": state["value"]},
                headers={"X-CSRFToken": csrf(client, args.base_url)},
                timeout=30,
            )
            response.raise_for_status()
            check_saved_setting(client, args.base_url, state["value"])
            state["cookies"] = client.cookies.get_dict()
            with args.state.open("x", encoding="utf-8") as target:
                args.state.chmod(0o600)
                json.dump(state, target)
        else:
            state = json.loads(args.state.read_text(encoding="utf-8"))
            client.cookies.update(state["cookies"])
            response = client.get(args.base_url + "/auth/check", timeout=10)
            assert response.status_code == 401, (
                "Restart retained an old session"
            )
            client.cookies.clear()
            login(client, args.base_url, state)
            check_saved_setting(client, args.base_url, state["value"])
    print(f"Production lifecycle {args.phase}: passed")


if __name__ == "__main__":
    main()