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 .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 .ref == "main"
274281
275282
276283@mock .patch ("mergify_cli.ci.scopes.cli.base_detector.detect" )
@@ -295,9 +302,70 @@ 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 .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+ detected = cli .DetectedScope (ref = "main" , scopes = {"backend" , "frontend" })
323+
324+ # Mock the HTTP request
325+ route = respx_mock .post (
326+ f"{ api_url } /v1/repos/{ repository } /pulls/{ pull_request } /scopes" ,
327+ ).mock (
328+ return_value = respx .MockResponse (200 , json = {"status" : "ok" }),
329+ )
330+
331+ # Call the upload function
332+ await cli .upload_scopes (api_url , token , repository , pull_request , detected )
333+
334+ # Verify the request was made
335+ assert route .called
336+ assert route .call_count == 1
337+
338+ # Verify the request body
339+ request = route .calls [0 ].request
340+ assert request .headers ["Authorization" ] == "token test-token"
341+ assert request .headers ["Accept" ] == "application/json"
342+
343+ # Verify the JSON payload
344+ payload = json .loads (request .content )
345+ assert payload == {"scopes" : ["backend" , "frontend" ]}
346+
347+
348+ async def test_upload_scopes_with_merge_queue (respx_mock : respx .MockRouter ) -> None :
349+ api_url = "https://api.mergify.com"
350+ token = "secret-token" # noqa: S105
351+ repository = "test-org/test-repo"
352+ pull_request = 456
353+
354+ detected = cli .DetectedScope (ref = "abc123" , scopes = {"merge-queue" })
355+
356+ # Mock the HTTP request
357+ route = respx_mock .post (
358+ f"{ api_url } /v1/repos/{ repository } /pulls/{ pull_request } /scopes" ,
359+ ).mock (
360+ return_value = respx .MockResponse (201 ),
361+ )
362+
363+ # Call the upload function
364+ await cli .upload_scopes (api_url , token , repository , pull_request , detected )
365+
366+ # Verify the request was made with correct data
367+ assert route .called
368+ request = route .calls [0 ].request
369+
370+ payload = json .loads (request .content )
371+ assert "merge-queue" in payload ["scopes" ]
0 commit comments