Files
cad-building-showcase/tests/test_browser_smoke.py

124 lines
3.7 KiB
Python

from __future__ import annotations
import json
import os
import socket
import subprocess
import sys
import time
from pathlib import Path
import pytest
ROOT = Path(__file__).resolve().parents[1]
@pytest.mark.skipif(
os.environ.get("RUN_BROWSER_SMOKE") != "1", reason="set RUN_BROWSER_SMOKE=1 with Playwright installed"
)
def test_served_viewer_in_desktop_and_mobile(tmp_path: Path) -> None:
from test_contracts import minimal_spec
spec = tmp_path / "spec.json"
glb = tmp_path / "model.glb"
metadata = tmp_path / "model_metadata.json"
web = tmp_path / "web"
spec.write_text(json.dumps(minimal_spec()), encoding="utf-8")
subprocess.run(
[
sys.executable,
str(ROOT / "scripts" / "spec_to_glb.py"),
"--spec",
str(spec),
"--out",
str(glb),
"--metadata-out",
str(metadata),
],
check=True,
)
subprocess.run(
[
sys.executable,
str(ROOT / "scripts" / "write_web_showcase.py"),
"--glb",
str(glb),
"--metadata",
str(metadata),
"--out-dir",
str(web),
],
check=True,
)
subprocess.run(["npm", "install", "--prefix", str(web), "--ignore-scripts"], check=True)
with socket.socket() as sock:
sock.bind(("127.0.0.1", 0))
port = sock.getsockname()[1]
server = subprocess.Popen(
[sys.executable, "-m", "http.server", str(port), "--bind", "127.0.0.1"],
cwd=web,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
try:
time.sleep(0.8)
report = tmp_path / "browser_report.json"
proc = subprocess.run(
[
"node",
str(ROOT / "scripts" / "web_smoke_test.mjs"),
"--url",
f"http://127.0.0.1:{port}",
"--out",
str(report),
"--screenshots-dir",
str(tmp_path / "screenshots"),
],
text=True,
capture_output=True,
)
assert proc.returncode == 0, proc.stderr
result = json.loads(report.read_text(encoding="utf-8"))
assert result["status"] == "PASS"
checks = {item["name"]: item for item in result["checks"]}
assert checks["desktop:viewport"]["detail"] == "1440x900 expected 1440x900"
assert checks["mobile:viewport"]["detail"] == "390x844 expected 390x844"
offline = tmp_path / "viewer.html"
subprocess.run(
[
sys.executable,
str(ROOT / "scripts" / "write_offline_viewer.py"),
"--spec",
str(spec),
"--glb",
str(glb),
"--out",
str(offline),
],
check=True,
)
offline_report = tmp_path / "offline_browser_report.json"
offline_screenshot = tmp_path / "offline_browser.png"
proc = subprocess.run(
[
"node",
str(ROOT / "scripts" / "offline_viewer_smoke.mjs"),
"--viewer",
str(offline),
"--out",
str(offline_report),
"--screenshot",
str(offline_screenshot),
],
text=True,
capture_output=True,
)
assert proc.returncode == 0, proc.stderr
offline_result = json.loads(offline_report.read_text(encoding="utf-8"))
assert offline_result["status"] == "PASS"
assert offline_screenshot.exists()
finally:
server.terminate()
server.wait(timeout=10)