"""Offline routing and input-boundary checks for the small study view.""" from contextlib import asynccontextmanager import importlib.util import json from pathlib import Path import shutil import subprocess import sys import tempfile from types import SimpleNamespace import unittest from unittest import mock try: from starlette.applications import Starlette from starlette.responses import JSONResponse from starlette.routing import Route from starlette.testclient import TestClient except ImportError: Starlette = None @unittest.skipIf(Starlette is None, "Space tests require the pinned tracking environment") class SpaceViewTests(unittest.TestCase): @classmethod def setUpClass(cls): path = Path(__file__).parents[1] / "space" / "app.py" spec = importlib.util.spec_from_file_location("study_space_app", path) cls.app_module = importlib.util.module_from_spec(spec) spec.loader.exec_module(cls.app_module) def setUp(self): self.temp = tempfile.TemporaryDirectory() self.root = Path(self.temp.name) self.transitions = [] @asynccontextmanager async def lifespan(app): self.transitions.append("started") yield self.transitions.append("stopped") async def preserved(request): return JSONResponse({"trackio": True, "project": request.query_params.get("project")}) backend = Starlette(routes=[Route("/", preserved), Route("/api/test", preserved), Route("/oauth/callback", preserved)], lifespan=lifespan) self.app = self.app_module.create_app(backend, results_dir=self.root) def tearDown(self): self.temp.cleanup() def test_existing_dashboard_api_oauth_and_lifespan_survive_wrapper(self): with TestClient(self.app) as client: for route in ["/?project=beam-pi-programbench-20261009", "/api/test", "/oauth/callback"]: response = client.get(route) self.assertEqual(response.status_code, 200) self.assertTrue(response.json()["trackio"]) self.assertEqual(self.transitions, ["started"]) self.assertIn("Beam + Pi ยท ProgramBench", client.get("/study").text) self.assertEqual(self.transitions, ["started", "stopped"]) def test_missing_aggregates_have_empty_chart_and_never_fabricated_scores(self): plotter = mock.Mock(return_value={"data": [], "layout": {}}) with mock.patch.dict(sys.modules, {"plot_results": SimpleNamespace(build_plotly=plotter)}): with TestClient(self.app) as client: payload = client.get("/study/data").json() self.assertFalse(payload["available"]) self.assertEqual(payload["figure"]["data"], []) self.assertEqual(payload["downloads"], []) plotter.assert_called_once_with(None, [], axis="reference_seconds") def test_invalid_axis_unknown_download_and_raw_exception_are_not_exposed(self): with TestClient(self.app) as client: self.assertEqual(client.get("/study/data?axis=untrusted").status_code, 400) self.assertEqual(client.get("/study/download/budget.sqlite").status_code, 404) with mock.patch.object(self.app_module, "study_snapshot", side_effect=ValueError("private path and credential")): response = client.get("/study/data") self.assertEqual(response.status_code, 503) self.assertNotIn("private", response.text) def test_symlink_download_is_rejected(self): target = self.root / "private" target.write_text("credential") (self.root / "summary.json").symlink_to(target) with TestClient(self.app) as client: self.assertEqual(client.get("/study/download/summary.json").status_code, 404) @unittest.skipUnless(shutil.which("node"), "Responsive layout check requires Node.js") def test_minimal_chart_preserves_score_curves_at_each_width(self): from plot_results import build_plotly from study.tests.test_plot_results import fixture summary, rows = fixture() figures = [build_plotly(None, [])] figures.extend(build_plotly(summary, rows, axis=axis) for axis in self.app_module.AXES) # Run the same pure layout helper delivered to browsers, without a browser dependency. script = self.app_module.PAGE.split("// Responsive layout start\n", 1)[1].split( "// Responsive layout end", 1)[0] script += "\nfor(const original of " + json.dumps(figures) + "){\n" script += """ const before=JSON.stringify(original); for(const width of [341,1000]){ const view=fitFigure(original,width), result=view.figure; if(JSON.stringify(original)!==before) throw new Error('Published figure mutated'); if(view.config.displayModeBar!==false) throw new Error('Download toolbar remains'); if(result.layout.title.text || result.layout.annotations.length) throw new Error('Extra title or notes remain'); if(result.layout.yaxis2 || result.layout.xaxis.anchor!=='y' || JSON.stringify(result.layout.yaxis.domain)!=='[0,1]') throw new Error('Coverage axis remains'); const source=original.data.filter(trace=>trace.yaxis!=='y2'); if(result.data.length!==source.length) throw new Error('Score curves lost'); for(let i=0;i