1+ import json
12import pathlib
23from unittest import mock
34
45import pytest
6+ import respx
57import yaml
68
79from mergify_cli .ci .scopes import base_detector
@@ -230,7 +232,7 @@ def test_detect_with_matches(
230232
231233 # Capture output
232234 with mock .patch ("click.echo" ) as mock_echo :
233- cli .detect (str (config_file ))
235+ result = cli .detect (str (config_file ))
234236
235237 # Verify calls
236238 mock_detect_base .assert_called_once ()
@@ -244,6 +246,9 @@ def test_detect_with_matches(
244246 assert "- backend" in calls
245247 assert "- merge-queue" in calls
246248
249+ assert result .base_ref == "main"
250+ assert result .scopes == {"backend" , "merge-queue" }
251+
247252
248253@mock .patch ("mergify_cli.ci.scopes.cli.base_detector.detect" )
249254@mock .patch ("mergify_cli.ci.scopes.changed_files.git_changed_files" )
@@ -265,12 +270,14 @@ def test_detect_no_matches(
265270
266271 # Capture output
267272 with mock .patch ("click.echo" ) as mock_echo :
268- cli .detect (str (config_file ))
273+ result = cli .detect (str (config_file ))
269274
270275 # Verify output
271276 calls = [call .args [0 ] for call in mock_echo .call_args_list ]
272277 assert "Base: main" in calls
273278 assert "No scopes matched." in calls
279+ assert result .scopes == set ()
280+ assert result .base_ref == "main"
274281
275282
276283@mock .patch ("mergify_cli.ci.scopes.cli.base_detector.detect" )
@@ -295,9 +302,58 @@ def test_detect_debug_output(
295302
296303 # Capture output
297304 with mock .patch ("click.echo" ) as mock_echo :
298- cli .detect (str (config_file ))
305+ result = cli .detect (str (config_file ))
299306
300307 # Verify debug output includes file details
301308 calls = [call .args [0 ] for call in mock_echo .call_args_list ]
302309 assert any (" api/models.py" in call for call in calls )
303310 assert any (" api/views.py" in call for call in calls )
311+
312+ assert result .base_ref == "main"
313+ assert result .scopes == {"backend" }
314+
315+
316+ async def test_upload_scopes (respx_mock : respx .MockRouter ) -> None :
317+ api_url = "https://api.mergify.test"
318+ token = "test-token" # noqa: S105
319+ repository = "owner/repo"
320+ pull_request = 123
321+
322+ # Mock the HTTP request
323+ route = respx_mock .post (
324+ f"{ api_url } /v1/repos/{ repository } /pulls/{ pull_request } /scopes" ,
325+ ).mock (
326+ return_value = respx .MockResponse (200 , json = {"status" : "ok" }),
327+ )
328+
329+ # Call the upload function
330+ await cli .send_scopes (
331+ api_url ,
332+ token ,
333+ repository ,
334+ pull_request ,
335+ ["backend" , "frontend" ],
336+ )
337+
338+ # Verify the request was made
339+ assert route .called
340+ assert route .call_count == 1
341+
342+ # Verify the request body
343+ request = route .calls [0 ].request
344+ assert request .headers ["Authorization" ] == "Bearer test-token"
345+ assert request .headers ["Accept" ] == "application/json"
346+
347+ # Verify the JSON payload
348+ payload = json .loads (request .content )
349+ assert payload == {"scopes" : ["backend" , "frontend" ]}
350+
351+
352+ def test_dump (tmp_path : pathlib .Path ) -> None :
353+ config_file = tmp_path / "scopes.json"
354+ saved = cli .DetectedScope (base_ref = "main" , scopes = {"backend" , "merge-queue" })
355+ saved .save_to_file (str (config_file ))
356+
357+ loaded = cli .DetectedScope .load_from_file (str (config_file ))
358+ assert loaded .scopes == saved .scopes
359+ assert loaded .base_ref == saved .base_ref
0 commit comments