File size: 3,572 Bytes
cb00361
eb60226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb00361
 
 
 
 
 
 
 
 
 
 
eb60226
 
cb00361
 
eb60226
 
 
 
cb00361
eb60226
 
 
 
 
cb00361
eb60226
 
 
cb00361
eb60226
 
 
cb00361
eb60226
 
 
 
cb00361
eb60226
 
cb00361
eb60226
 
87c0f85
eb60226
 
 
 
 
 
 
 
 
cb00361
eb60226
 
 
 
9ccc0da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""Headless smoke tests for the Streamlit UI (demo mode + live mode controls)."""

from __future__ import annotations

from pathlib import Path

import pytest

pytest.importorskip("streamlit")
from streamlit.testing.v1 import AppTest  # noqa: E402

APP = str(Path(__file__).resolve().parents[2] / "src" / "ui" / "streamlit_app.py")


def _button(at: AppTest, label_part: str):
    return next(b for b in at.button if label_part in b.label)


def _subheaders(at: AppTest) -> list[str]:
    return [s.value for s in at.subheader]


def _start_demo() -> AppTest:
    at = AppTest.from_file(APP, default_timeout=30).run()
    assert not at.exception
    at.radio[0].set_value("Scripted Demo (no API calls)")
    at.run()
    at.slider[0].set_value(0.0)
    _button(at, "Start run").click()
    return at.run()


def test_initial_page_renders_mode_and_repo_controls():
    at = AppTest.from_file(APP, default_timeout=30).run()
    assert not at.exception
    assert len(at.radio) >= 1
    assert len(at.selectbox) >= 1
    assert any("Start run" in i.value for i in at.info)


def test_run_pauses_for_approval_with_fix_and_tests():
    at = _start_demo()
    assert not at.exception
    assert any("Waiting for your approval" in s for s in _subheaders(at))
    code = " ".join(c.value for c in at.code)
    assert "sum(xs) / len(xs)" in code and "2 passed" in code
    assert at.metric[0].value == "2 / 8"
    assert not any("commit" in str(s.value).lower() for s in at.success)


def test_approve_shows_simulated_commit():
    at = _start_demo()
    _button(at, "Approve").click()
    at.run()
    assert not at.exception
    assert any("commit" in s.value.lower() for s in at.success)
    assert not any("Waiting for your approval" in s for s in _subheaders(at))


def test_request_changes_requires_feedback_then_loops_back():
    at = _start_demo()
    _button(at, "Request changes").click()
    at.run()
    assert any("Add feedback" in w.value for w in at.warning)
    assert any("Waiting for your approval" in s for s in _subheaders(at))

    at.text_area(key="fb-2").set_value("handle the empty list")
    _button(at, "Request changes").click()
    at.run()
    assert not at.exception
    assert at.metric[0].value == "3 / 8"
    assert any("ValueError" in c.value for c in at.code)
    assert any("Waiting for your approval" in s for s in _subheaders(at))


def test_reject_escalates():
    at = _start_demo()
    _button(at, "Reject").click()
    at.run()
    assert not at.exception
    assert any("Escalated" in e.value for e in at.error)


def test_pdf_report_generation(tmp_path: Path):
    from agent.state import AgentStatus, FilePatch, FixAttempt, IssueInput, ReproTest, SandboxResult
    from ui.report_pdf import generate_pdf_report

    values = {
        "status": AgentStatus.COMMITTED,
        "iteration": 1,
        "branch": "agent/fix-issue",
        "commit_sha": "abc1234",
        "issue": IssueInput(title="Bug in average()", body="Off by one", repo_path=tmp_path),
        "repro_test": ReproTest(path="test_repro.py", content="def test_x(): assert False", confirmed_failing=True),
        "attempts": [
            FixAttempt(
                iteration=1,
                rationale="Fix denominator",
                patches=[FilePatch(path="calc.py", unified_diff="--- a/calc.py\n+++ b/calc.py\n-bad\n+good\n")],
                result=SandboxResult(exit_code=0, stdout="1 passed", stderr=""),
            )
        ],
    }
    pdf_bytes = generate_pdf_report(values)
    assert pdf_bytes.startswith(b"%PDF-")
    assert len(pdf_bytes) > 500