File size: 2,987 Bytes
8c3e275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""End-to-end tests for PageParse using Playwright.

Requires: pip install playwright && playwright install chromium
Run: pytest tests/test_e2e.py
"""

import pytest
import subprocess
import time
import sys
from pathlib import Path

pytest.importorskip("playwright")

HERE = Path(__file__).resolve().parent.parent
SERVER_SCRIPT = str(HERE / "src" / "pageparse" / "cli.py")


@pytest.fixture(scope="module")
def server():
    proc = subprocess.Popen(
        [sys.executable, "-m", "pageparse.cli", "serve", "--port", "8765"],
        cwd=HERE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    time.sleep(3)
    yield
    proc.terminate()
    proc.wait()


@pytest.fixture(scope="module")
def browser():
    from playwright.sync_api import sync_playwright
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=True)
        yield browser
        browser.close()


def test_homepage_loads(server, browser):
    page = browser.new_page()
    page.goto("http://127.0.0.1:8765/")
    assert "PageParse" in page.title() or "PageParse" in page.content()
    page.close()


def test_upload_form_present(server, browser):
    page = browser.new_page()
    page.goto("http://127.0.0.1:8765/")
    content = page.content()
    assert "upload" in content.lower() or "ingest" in content.lower()
    page.close()


def test_telemetry_endpoint(server):
    import urllib.request
    import json
    resp = urllib.request.urlopen("http://127.0.0.1:8765/telemetry")
    data = json.loads(resp.read())
    assert "cpu_percent" in data
    assert "total_gb" in data


def test_airgap_mode(server):
    import urllib.request
    import json

    req = urllib.request.Request(
        "http://127.0.0.1:8765/airgap",
        data=json.dumps({"airgap": True}).encode(),
        headers={"Content-Type": "application/json"},
        method="POST",
    )
    resp = urllib.request.urlopen(req)
    data = json.loads(resp.read())
    assert data["airgap"] is True

    resp = urllib.request.urlopen("http://127.0.0.1:8765/airgap")
    data = json.loads(resp.read())
    assert data["airgap"] is True


def test_stats_endpoint(server):
    import urllib.request
    import json

    resp = urllib.request.urlopen("http://127.0.0.1:8765/stats")
    data = json.loads(resp.read())
    assert "sources" in data
    assert "records" in data


def test_metrics_endpoint(server):
    import urllib.request

    resp = urllib.request.urlopen("http://127.0.0.1:8765/metrics")
    content = resp.read().decode()
    assert "pageparse_http_requests_total" in content


def test_translate_endpoint(server):
    import urllib.request
    import json

    req = urllib.request.Request(
        "http://127.0.0.1:8765/translate",
        data=json.dumps({"text": "Hello", "target_lang": "Hindi"}).encode(),
        headers={"Content-Type": "application/json"},
    )
    resp = urllib.request.urlopen(req)
    data = json.loads(resp.read())
    assert "translated_text" in data