52 lines
2.3 KiB
Python
52 lines
2.3 KiB
Python
"""Execute production browser functions under Node and verify the current served build."""
|
|
from __future__ import annotations
|
|
|
|
import ast
|
|
import re
|
|
import shutil
|
|
import subprocess
|
|
from datetime import date
|
|
from functools import lru_cache
|
|
from pathlib import Path
|
|
from tempfile import TemporaryDirectory
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def run_timer_contract(scenario: str) -> None:
|
|
node = shutil.which("node")
|
|
assert node is not None, "Node is required for executable browser timer regressions"
|
|
result = subprocess.run([node, str(ROOT / "tests/runtime_timer_contracts.cjs"), scenario],
|
|
capture_output=True, text=True, timeout=15)
|
|
assert result.returncode == 0, result.stdout + result.stderr
|
|
assert f"PASS: {scenario}" in result.stdout
|
|
|
|
|
|
@lru_cache(maxsize=1)
|
|
def assert_current_build_contract() -> None:
|
|
"""Check the actual assignment and rendered boot, ignoring historical comments."""
|
|
from fastapi import FastAPI
|
|
from fastapi.testclient import TestClient
|
|
from ui_builder.integration import install_ui_builder
|
|
|
|
module = ast.parse((ROOT / "app.py").read_text(encoding="utf-8"))
|
|
assignments = [node for node in module.body if isinstance(node, ast.Assign)
|
|
and any(isinstance(target, ast.Name) and target.id == "BUILD_VERSION" for target in node.targets)]
|
|
assert len(assignments) == 1
|
|
version = ast.literal_eval(assignments[0].value)
|
|
assert isinstance(version, str) and re.fullmatch(r"\d{4}\.\d{2}\.\d{2}\.\d+", version)
|
|
date(*map(int, version.split(".")[:3]))
|
|
install_calls = [node for node in ast.walk(module) if isinstance(node, ast.Call)
|
|
and isinstance(node.func, ast.Name) and node.func.id == "install_ui_builder"]
|
|
assert len(install_calls) == 1
|
|
assert any(keyword.arg == "build_version" and isinstance(keyword.value, ast.Name)
|
|
and keyword.value.id == "BUILD_VERSION" for keyword in install_calls[0].keywords)
|
|
with TemporaryDirectory() as temporary:
|
|
app = FastAPI()
|
|
install_ui_builder(app, build_version=version, settings_dir=Path(temporary), data_providers={})
|
|
with TestClient(app) as client:
|
|
response = client.get("/")
|
|
assert response.status_code == 200
|
|
assert f'buildVersion: "{version}"' in response.text
|
|
assert "__UI_BUILDER_BUILD_VERSION__" not in response.text
|