diff --git a/Server/src/cli/commands/code.py b/Server/src/cli/commands/code.py index 3fdaccfc0f..cd6d4dcb52 100644 --- a/Server/src/cli/commands/code.py +++ b/Server/src/cli/commands/code.py @@ -138,17 +138,15 @@ def read(path: str, start_line: Optional[int], line_count: Optional[int]): "path": directory, } - if start_line: - params["startLine"] = start_line - if line_count: - params["lineCount"] = line_count - result = run_command("manage_script", params, config) # For read, output content directly if available if result.get("success") and result.get("data"): data = result.get("data", {}) if isinstance(data, dict) and "contents" in data: - click.echo(data["contents"]) + # Unity returns the whole file, so the range is cut here + first = (start_line or 1) - 1 + lines = data["contents"].splitlines(keepends=True) + click.echo("".join(lines[first:first + line_count if line_count else None])) else: click.echo(format_output(result, config.format)) else: diff --git a/Server/src/cli/commands/editor.py b/Server/src/cli/commands/editor.py index 5b0dce7955..10b11b47c0 100644 --- a/Server/src/cli/commands/editor.py +++ b/Server/src/cli/commands/editor.py @@ -1,6 +1,7 @@ """Editor CLI commands.""" import sys +import time import click from typing import Optional, Any @@ -350,22 +351,22 @@ def run_tests(mode: str, async_mode: bool, wait: Optional[int], details: bool, f return params: dict[str, Any] = {"mode": mode} - if wait is not None: - params["wait_timeout"] = wait if details: params["include_details"] = True if failed_only: params["include_failed_tests"] = True result = run_command("run_tests", params, config) + job_id = result.get("data", {}).get("job_id") if result.get("success") else None # For async mode, just show job ID - if async_mode and result.get("success"): - job_id = result.get("data", {}).get("job_id") - if job_id: - click.echo(f"Test job started: {job_id}") - print_info("Poll with: unity-mcp editor poll-test " + job_id) - return + if async_mode and job_id: + click.echo(f"Test job started: {job_id}") + print_info("Poll with: unity-mcp editor poll-test " + job_id) + return + if wait and job_id: + click.get_current_context().invoke(poll_test, job_id=job_id, wait=wait, details=details, failed_only=failed_only) + return click.echo(format_output(result, config.format)) @@ -401,14 +402,21 @@ def poll_test(job_id: str, wait: int, details: bool, failed_only: bool): config = get_config() params: dict[str, Any] = {"job_id": job_id} - if wait: - params["wait_timeout"] = wait if details: params["include_details"] = True if failed_only: params["include_failed_tests"] = True - result = run_command("get_test_job", params, config) + # Unity answers at once, so the wait is here, as the MCP tool does it server-side + deadline = time.monotonic() + (wait or 0) + while True: + result = run_command("get_test_job", params, config) + if not (isinstance(result, dict) and result.get("success")) or result.get("data", {}).get("status") in ("succeeded", "failed", "cancelled"): + break + remaining = deadline - time.monotonic() + if remaining <= 0: + break + time.sleep(min(2.0, remaining)) click.echo(format_output(result, config.format)) if isinstance(result, dict) and result.get("success"): diff --git a/Server/src/cli/commands/script.py b/Server/src/cli/commands/script.py index a658bb4f87..3ee52a44c4 100644 --- a/Server/src/cli/commands/script.py +++ b/Server/src/cli/commands/script.py @@ -115,17 +115,15 @@ def read(path: str, start_line: Optional[int], line_count: Optional[int]): "path": directory, } - if start_line: - params["startLine"] = start_line - if line_count: - params["lineCount"] = line_count - result = run_command("manage_script", params, config) # For read, just output the content directly if result.get("success") and result.get("data"): data = result.get("data", {}) if isinstance(data, dict) and "contents" in data: - click.echo(data["contents"]) + # Unity returns the whole file, so the range is cut here + first = (start_line or 1) - 1 + lines = data["contents"].splitlines(keepends=True) + click.echo("".join(lines[first:first + line_count if line_count else None])) else: click.echo(format_output(result, config.format)) else: diff --git a/Server/tests/test_cli.py b/Server/tests/test_cli.py index 689c4d4a99..43192bee35 100644 --- a/Server/tests/test_cli.py +++ b/Server/tests/test_cli.py @@ -985,6 +985,20 @@ def test_script_read(self, runner): cli, ["script", "read", "Assets/Scripts/Test.cs"]) assert result.exit_code == 0 + # Unity's manage_script read returns the whole file and reads no line range, so the CLI cuts it. + def test_script_and_code_read_print_only_the_requested_lines(self, runner): + reply = {"success": True, "data": {"contents": "a\nb\nc\nd\n"}} + with patch("cli.commands.script.run_command", return_value=reply) as mock_run: + result = runner.invoke(cli, ["script", "read", "Assets/Scripts/Test.cs", "--start-line", "2", "--line-count", "2"]) + assert result.exit_code == 0, result.output + assert result.output.rstrip("\n").split("\n") == ["b", "c"] + assert mock_run.call_args[0][1] == {"action": "read", "name": "Test", "path": "Assets/Scripts"} + result = runner.invoke(cli, ["script", "read", "Assets/Scripts/Test.cs", "--start-line", "3"]) + assert result.output.rstrip("\n").split("\n") == ["c", "d"] + with patch("cli.commands.code.run_command", return_value=reply): + result = runner.invoke(cli, ["code", "read", "Assets/Scripts/Test.cs", "--start-line", "2", "--line-count", "2"]) + assert result.output.rstrip("\n").split("\n") == ["b", "c"] + def test_script_delete(self, runner, mock_unity_response): """Test script delete command.""" with patch("cli.commands.script.run_command", return_value=mock_unity_response): @@ -1410,6 +1424,33 @@ def test_editor_poll_test(self, runner): cli, ["editor", "poll-test", "test-job-123"]) assert result.exit_code == 0 + # Unity's run_tests and get_test_job return at once; only the MCP tool waited, server-side. + def test_editor_tests_wait_polls_the_job_until_it_finishes(self, runner): + running = {"success": True, "data": {"job_id": "j1", "status": "running"}} + done = {"success": True, "data": {"job_id": "j1", "status": "succeeded"}} + with patch("cli.commands.editor.run_command", side_effect=[running, running, done]) as mock_run, patch("time.sleep"): + result = runner.invoke(cli, ["editor", "tests", "--wait", "60", "--failed-only"]) + assert result.exit_code == 0, result.output + assert [c[0][:2] for c in mock_run.call_args_list] == [ + ("run_tests", {"mode": "EditMode", "include_failed_tests": True}), + ("get_test_job", {"job_id": "j1", "include_failed_tests": True}), + ("get_test_job", {"job_id": "j1", "include_failed_tests": True}), + ] + assert "succeeded" in result.output + + def test_editor_poll_test_waits_until_the_job_finishes_or_the_time_is_up(self, runner): + running = {"success": True, "data": {"job_id": "j1", "status": "running"}} + done = {"success": True, "data": {"job_id": "j1", "status": "failed"}} + with patch("cli.commands.editor.run_command", side_effect=[running, done]) as mock_run, patch("time.sleep"): + result = runner.invoke(cli, ["editor", "poll-test", "j1", "--wait", "10"]) + assert result.exit_code == 0, result.output + assert [c[0][:2] for c in mock_run.call_args_list] == [("get_test_job", {"job_id": "j1"})] * 2 + with patch("cli.commands.editor.run_command", return_value=running) as mock_run, patch("time.sleep"), \ + patch("time.monotonic", side_effect=[0.0, 5.0, 11.0]): + result = runner.invoke(cli, ["editor", "poll-test", "j1", "--wait", "10"]) + assert result.exit_code == 0, result.output + assert mock_run.call_count == 2 + # ============================================================================= # Code Search Tests