Files
themiz/eunomia/tests/test_ui_stack_gate.py
T

56 lines
2.3 KiB
Python

"""UI Stack gate: the eight audit defects of 22.09.2026 must stay fixed in the built interface.
Boots Eunomia on a free port and runs ui_stack_check.py against it, so the regression fails inside the
regular `python3 -m unittest discover -s eunomia/tests -p 'test_*.py'` run without a manual server.
"""
import os
import socket
import subprocess
import sys
import time
import unittest
import urllib.request
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
PROBE = Path(__file__).with_name('ui_stack_check.py')
def free_port():
with socket.socket() as sock:
sock.bind(('127.0.0.1', 0))
return sock.getsockname()[1]
class UiStackGateTests(unittest.TestCase):
def test_ui_stack_probe_is_green_on_built_interface(self):
try:
import playwright # noqa: F401
except ImportError:
self.fail('Python Playwright не установлен: проба UI Stack не может открыть интерфейс')
port = free_port()
server = subprocess.Popen([sys.executable, str(ROOT / 'scripts/eunomia.py'), '--serve', '--root', str(ROOT),
'--port', str(port), '--no-open'], cwd=ROOT,
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
self.addCleanup(server.wait, 10)
self.addCleanup(server.terminate)
base = f'http://127.0.0.1:{port}'
# Ready means the data API answers, not just the static index: the interface renders from bootstrap.
deadline = time.monotonic() + 180
while True:
try:
if urllib.request.urlopen(base + '/api/bootstrap', timeout=30).status == 200:
break
except OSError:
pass
if server.poll() is not None or time.monotonic() > deadline:
self.fail(f'Эвномия не отдала /api/bootstrap на {base} за 180 с (код процесса {server.poll()})')
time.sleep(0.5)
probe = subprocess.run([sys.executable, str(PROBE)], cwd=ROOT, env={**os.environ, 'EUNOMIA_URL': base},
capture_output=True, text=True, timeout=900)
self.assertEqual(probe.returncode, 0, 'проба UI Stack красная:\n' + probe.stdout[-6000:] + probe.stderr[-2000:])
if __name__ == '__main__':
unittest.main()