from datetime import datetime, timezone from pathlib import Path from src.progress import progress_from_events from src.view_models import build_run_view_model def test_progress_elapsed_ticks_from_created_at_when_running(): result = progress_from_events( [{"ts": "2026-06-02T10:00:10+00:00", "step": "bootstrap", "status": "started"}], state={"status": "running", "created_at": "2026-06-02T10:00:00+00:00"}, ) assert result["elapsed_seconds"] >= 0 def test_view_model_elapsed_uses_now_for_non_terminal_runs(): bundle = { "summary": { "run_id": "run-1", "status": "running", "created_at": "2026-06-02T10:00:00+00:00", "target_space": "alice/demo", "target_space_url": "https://huggingface.co/spaces/alice/demo", }, "state": {"status": "running"}, "launch": {"status": "running", "job_id": "j1"}, "events": [{"ts": "2026-06-02T10:01:00+00:00", "step": "bootstrap", "status": "started"}], "inference_gate": {}, "generation_smoke": {}, "hardware_strategy": {}, "technical_blockers": {}, } view = build_run_view_model( "run-1", bundle, bucket_source="alice/space-factory-runs", now=datetime(2026, 6, 2, 10, 5, tzinfo=timezone.utc), ) assert view["header"]["elapsed_seconds"] == 300 def test_cancel_button_is_visible_in_active_run_panel(): html = Path("web/index.html").read_text(encoding="utf-8") active = html.split('id="activeRunPanel"', 1)[1].split('id="runsExplorerPanel"', 1)[0] assert 'id="cancelRun"' in active assert "Cancel Job" in active support = html.split('class="support-elements"', 1)[1] assert 'id="cancelRun"' not in support def test_frontend_enables_cancel_from_progress_status_and_job_link(): js = Path("web/static/progress.js").read_text(encoding="utf-8") assert "const cancel=$('cancelRun')" in js assert "TERMINAL_STATUSES.has(s)" in js assert "view.links?.job_url" in js assert "p.elapsed_seconds??header.elapsed_seconds" in js def test_job_result_persists_created_at_for_elapsed_time(): jobs = Path("src/jobs.py").read_text(encoding="utf-8") assert '"created_at": str(_job_field(job, "created_at") or utc_now_iso())' in jobs assert '"started_at": str(_job_field(job, "started_at") or _job_field(job, "created_at") or utc_now_iso())' in jobs