diff --git a/backend/tests/test_api.py b/backend/tests/test_api.py index 4cbb309..96659a3 100644 --- a/backend/tests/test_api.py +++ b/backend/tests/test_api.py @@ -36,22 +36,57 @@ def test_prices_random_list(): assert all(item["price"] <= 3 for item in items) -def test_build_history(): +def test_build_history(monkeypatch): + jenkins_builds = [ + { + "number": 205, + "result": "SUCCESS", + "timestamp": 1719992400000, + "duration": 75000, + "url": "http://jenkins.local/job/demo/205", + "changeSets": [ + { + "items": [ + { + "commitId": "9ac3f91d9bd0f0b7bfc5", + "msg": "Anade la API de Jenkins", + "author": {"fullName": "Dev One"}, + } + ] + } + ], + }, + { + "number": 204, + "result": None, + "timestamp": 1719988800000, + "duration": 1500, + "url": "http://jenkins.local/job/demo/204", + "changeSets": [], + }, + ] + + monkeypatch.setattr("app.services.builds.fetch_builds", lambda limit=5: jenkins_builds) + response = client.get("/builds") assert response.status_code == 200 body = response.json() builds = body["builds"] assert isinstance(builds, list) - assert len(builds) >= 1 + assert len(builds) == 2 first = builds[0] - assert "number" in first and "status" in first and "branch" in first + assert first["number"] == 205 + assert first["status"] == "success" + assert first["finished_at"] == 1719992400000 + assert first["duration_seconds"] == 75 + assert first["url"].endswith("/205") + assert first["commits"] == [ + {"commit": "9ac3f91", "message": "Anade la API de Jenkins", "author": "Dev One"} + ] - # Ensure descending order by build number - numbers = [build["number"] for build in builds] - assert numbers == sorted(numbers, reverse=True) - - failed_builds = [build for build in builds if build["status"] == "failed"] - if failed_builds: - failed = failed_builds[0] - assert "failed_stage" in failed and "fun_message" in failed + second = builds[1] + assert second["number"] == 204 + assert second["status"] == "running" + assert second["duration_seconds"] == 1 + assert second["commits"] == []