mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
214 lines
9.5 KiB
Python
214 lines
9.5 KiB
Python
"""Shared shell contracts: readable status, honest measurements and navigation."""
|
|
|
|
from html.parser import HTMLParser
|
|
import math
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from obliteratus.ui_theme import (
|
|
APP_CSS, WORKSPACE_PATHS, build_theme, render_runtime_status, render_shell_header,
|
|
)
|
|
|
|
|
|
class Markup(HTMLParser):
|
|
def __init__(self, source):
|
|
super().__init__()
|
|
self.elements, self.words = [], []
|
|
self.feed(source)
|
|
|
|
def handle_starttag(self, tag, attrs):
|
|
self.elements.append((tag, dict(attrs)))
|
|
|
|
def handle_data(self, data):
|
|
self.words.append(data)
|
|
|
|
@property
|
|
def text(self):
|
|
return " ".join(self.words)
|
|
|
|
|
|
def memory(used=4., total=16., reserved=5., name="Test accelerator"):
|
|
return SimpleNamespace(used_gb=used, total_gb=total, reserved_gb=reserved, device_name=name)
|
|
|
|
|
|
def device(memories=None, *, cuda=True, cpu=False):
|
|
memories = memories if memories is not None else [memory()]
|
|
return SimpleNamespace(is_gpu_available=lambda: not cpu, is_cuda=lambda: cuda,
|
|
device_count=lambda: len(memories),
|
|
get_memory_info=lambda index: memories[index])
|
|
|
|
|
|
@pytest.mark.cpu
|
|
def test_theme_has_distinct_legible_modes_and_no_remote_fonts():
|
|
calls = {}
|
|
|
|
class Theme:
|
|
def __init__(self, **kwargs):
|
|
calls["base"] = kwargs
|
|
|
|
def set(self, **kwargs):
|
|
calls["values"] = kwargs
|
|
return self
|
|
|
|
assert isinstance(build_theme(SimpleNamespace(themes=SimpleNamespace(Base=Theme))), Theme)
|
|
values = calls["values"]
|
|
assert values["body_text_color"] != values["body_text_color_dark"]
|
|
assert values["body_background_fill"] != values["body_background_fill_dark"]
|
|
assert values["button_primary_text_color_dark"] != values["button_primary_background_fill_dark"]
|
|
assert all(isinstance(font, str) for font in calls["base"]["font"])
|
|
assert "@import" not in APP_CSS and "url(" not in APP_CSS
|
|
assert "body::before" not in APP_CSS and "glitch" not in APP_CSS
|
|
assert "prefers-reduced-motion" in APP_CSS
|
|
|
|
|
|
@pytest.mark.cpu
|
|
def test_header_and_destinations_describe_real_work_without_fake_status():
|
|
markup = Markup(render_shell_header())
|
|
assert "OBLITERATUS" in markup.text
|
|
assert "A workbench for taking models apart" in markup.text
|
|
assert "connected" not in markup.text.lower() and "ready" not in markup.text.lower()
|
|
assert {destination for _, destination in WORKSPACE_PATHS} == {
|
|
"concept_lab", "obliterate", "benchmark", "chat", "export",
|
|
}
|
|
assert any("neurons" in caption for caption, _ in WORKSPACE_PATHS)
|
|
assert next(attrs for tag, attrs in markup.elements if tag == "svg")["aria-hidden"] == "true"
|
|
|
|
|
|
@pytest.mark.cpu
|
|
def test_cpu_status_has_no_fake_memory_reading_or_loaded_model():
|
|
markup = Markup(render_runtime_status(device(cpu=True)))
|
|
assert "CPU · No accelerator detected" in markup.text
|
|
assert "No shared model loaded" in markup.text
|
|
assert "Neuron Lab uses its own session" in markup.text
|
|
assert not any(attrs.get("role") == "meter" for _, attrs in markup.elements)
|
|
assert "GB" not in markup.text
|
|
|
|
|
|
@pytest.mark.cpu
|
|
def test_hardware_status_preserves_cuda_device_order_and_real_measurements():
|
|
source = render_runtime_status(device([
|
|
memory(0., 24., 0., "First"), memory(14., 24., 16., "Second"),
|
|
memory(22., 24., 23., "Third"),
|
|
]), model_name="org/model", model_loaded=True)
|
|
markup = Markup(source)
|
|
assert markup.text.index("GPU 0 · First") < markup.text.index("GPU 1 · Second") < markup.text.index("GPU 2 · Third")
|
|
assert "0.0 / 24.0 GB" in markup.text and "0.0 GB reserved" in markup.text
|
|
assert "Torch allocation / device capacity" in markup.text
|
|
assert "org/model" in markup.text
|
|
meters = [attrs for _, attrs in markup.elements if attrs.get("role") == "meter"]
|
|
assert [float(item["aria-valuenow"]) for item in meters] == [0., 58.3, 91.7]
|
|
assert "ob-high" in source and "ob-critical" in source
|
|
|
|
|
|
@pytest.mark.cpu
|
|
def test_mps_capacity_is_labelled_as_a_proxy_not_a_gpu_measurement():
|
|
markup = Markup(render_runtime_status(device([memory(4., 16., 0., "Apple MPS")], cuda=False)))
|
|
assert "Apple MPS" in markup.text and "4.0 / 16.0 GB" in markup.text
|
|
assert "Unified memory estimate · system RAM proxy" in markup.text
|
|
assert "Torch allocation" not in markup.text and "reserved" not in markup.text
|
|
assert [attrs["data-device-index"] for _, attrs in markup.elements
|
|
if "data-device-index" in attrs] == ["single"]
|
|
|
|
|
|
@pytest.mark.cpu
|
|
@pytest.mark.parametrize("used,total", [(float("nan"), 16), (4, float("inf")),
|
|
(-1, 16), (4, 0), (True, 16), ("4", 16),
|
|
(10 ** 400, 16)])
|
|
def test_unavailable_memory_does_not_turn_into_a_zero_or_invalid_meter(used, total):
|
|
markup = Markup(render_runtime_status(device([memory(used, total)])))
|
|
assert "Memory unavailable" in markup.text
|
|
assert not any(attrs.get("role") == "meter" for _, attrs in markup.elements)
|
|
assert "nan" not in markup.text and "inf" not in markup.text
|
|
|
|
|
|
@pytest.mark.cpu
|
|
def test_extreme_finite_memory_keeps_the_meter_bounded():
|
|
markup = Markup(render_runtime_status(device([memory(1e308, 1e-308, None)])))
|
|
meter = next(attrs for _, attrs in markup.elements if attrs.get("role") == "meter")
|
|
assert math.isfinite(float(meter["aria-valuenow"]))
|
|
assert meter["aria-valuenow"] == "100.0"
|
|
|
|
|
|
@pytest.mark.cpu
|
|
def test_hardware_and_model_names_are_escaped_in_text_and_attributes():
|
|
hostile = '\"><img src=x onerror="bad"> & <script>bad</script>'
|
|
markup = Markup(render_runtime_status(device([memory(name=hostile)]),
|
|
model_name=hostile, model_loaded=True))
|
|
assert hostile in markup.text
|
|
assert not any(tag in {"img", "script"} for tag, _ in markup.elements)
|
|
assert not any(key.startswith("on") for _, attrs in markup.elements for key in attrs)
|
|
assert any(attrs.get("title") == hostile for _, attrs in markup.elements)
|
|
|
|
|
|
@pytest.mark.cpu
|
|
def test_missing_hardware_and_model_statuses_remain_explicit():
|
|
assert "Accelerator inventory unavailable" in Markup(render_runtime_status(device([]))).text
|
|
broken = device()
|
|
broken.get_memory_info = lambda index: (_ for _ in ()).throw(RuntimeError("driver unavailable"))
|
|
assert "Hardware status unavailable" in Markup(render_runtime_status(broken)).text
|
|
assert "next refresh will retry" in Markup(render_runtime_status(broken)).text
|
|
assert "Model intervention running" in Markup(render_runtime_status(device(cpu=True), model_status="obliterating")).text
|
|
assert "org/model · unloaded" in Markup(render_runtime_status(device(cpu=True), model_status="ready", model_name="org/model")).text
|
|
assert "Model identity unavailable" in Markup(render_runtime_status(device(cpu=True), model_loaded=True)).text
|
|
|
|
|
|
@pytest.mark.operator_ui
|
|
def test_real_application_navigation_opens_the_requested_workspace_without_model_work():
|
|
pytest.importorskip("gradio", reason="install the locked spaces extra")
|
|
script = r'''
|
|
import asyncio
|
|
import app
|
|
from tests.conditional.test_operator_ui import _process_api
|
|
|
|
config = app.demo.get_config_file()
|
|
assert app.demo.local_url is None
|
|
assert app.workspace_tabs.selected == "concept_lab"
|
|
assert app.concept_lab_tabs.selected == "concept_neurons"
|
|
components = {item["id"]: item for item in config["components"]}
|
|
tabs = {item["props"]["label"] for item in components.values() if item["type"] == "tabitem"}
|
|
assert {"Obliterate", "Concept Lab", "Concepts", "Neurons", "Benchmark", "Chat", "A/B Compare",
|
|
"Strength Sweep", "Tourney", "Export", "Push to Hub", "Leaderboard", "About"} <= tabs
|
|
chat = next(item for item in components.values()
|
|
if "ob-chat" in item["props"].get("elem_classes", []))
|
|
assert chat["props"]["height"] == 460
|
|
assert app.THEME._stylesheets == []
|
|
masthead = next(item for item in components.values()
|
|
if "ob-masthead" in item["props"].get("elem_classes", []))
|
|
assert masthead["props"]["head"] == f'<style id="obliteratus-shell-style">{app.CSS}</style>'
|
|
assert config["css"] is None
|
|
|
|
# The production launcher must not also pass these rules through Gradio's
|
|
# CSS rewriter: it duplicates the container prefix inside media queries.
|
|
launch_calls = []
|
|
app.demo.launch = lambda **kwargs: launch_calls.append(kwargs)
|
|
app.launch(quiet=True)
|
|
assert len(launch_calls) == 1 and not launch_calls[0].get("css")
|
|
|
|
async def exercise():
|
|
for button, target in app.workspace_buttons:
|
|
events = [item for item in config["dependencies"]
|
|
if (button._id, "click") in item["targets"]]
|
|
assert len(events) == 1
|
|
event = events[0]
|
|
assert event["inputs"] == [] and event["queue"] is False
|
|
assert event["outputs"] == [app.workspace_tabs._id, app.concept_lab_tabs._id]
|
|
result = await _process_api(app.demo, event["id"], [], state=None)
|
|
assert result["data"][0] == {"selected": target, "__type__": "update"}
|
|
if target == "concept_lab":
|
|
assert result["data"][1]["selected"] == "concept_neurons"
|
|
else:
|
|
assert result["data"][1] == {"__type__": "update"}
|
|
assert app._state["model"] is None
|
|
assert app._state["status"] == "idle"
|
|
|
|
asyncio.run(exercise())
|
|
'''
|
|
result = subprocess.run([sys.executable, "-c", script], capture_output=True,
|
|
text=True, check=False, timeout=120,
|
|
env={**os.environ, "GRADIO_ANALYTICS_ENABLED": "False"})
|
|
assert result.returncode == 0, result.stdout + result.stderr
|