"""Tests for generated RPC method behavior.""" import json from unittest.mock import AsyncMock import pytest from copilot.rpc import ( CommandsApi, CommandsInvokeRequest, CommandsRespondToQueuedCommandRequest, LocalSessionMetadataValue, QueuedCommandHandled, QueuedCommandNotHandled, RemoteControlStatusOff, RemoteControlStatusResult, RemoteSessionMetadataValue, SessionList, SlashCommandTextResult, ) @pytest.mark.asyncio async def test_commands_invoke_deserializes_slash_command_result(): client = AsyncMock() client.request = AsyncMock(return_value={"kind": "text", "text": "hello", "markdown": True}) api = CommandsApi(client, "sess-1") result = await api.invoke(CommandsInvokeRequest(name="help")) assert isinstance(result, SlashCommandTextResult) assert result.text == "hello" assert result.markdown is True def test_remote_control_status_deserializes_string_discriminated_union(): result = RemoteControlStatusResult.from_dict({"status": {"state": "off"}}) assert isinstance(result.status, RemoteControlStatusOff) assert result.status.state == "off" assert result.status.to_dict() == {"state": "off"} def test_session_list_deserializes_boolean_discriminated_entries(): payload = { "sessions": [ { "sessionId": "example-local", "startTime": "2026-07-26T10:00:00.000Z", "modifiedTime": "2026-07-26T10:05:00.000Z", "isRemote": False, }, { "sessionId": "example-remote", "startTime": "2026-07-26T11:00:00.000Z", "modifiedTime": "2026-07-26T11:05:00.000Z", "isRemote": True, "remoteSessionIds": ["example-remote"], "repository": {"owner": "github", "name": "copilot-sdk", "branch": "main"}, }, ] } result = SessionList.from_dict(payload) local, remote = result.sessions assert isinstance(local, LocalSessionMetadataValue) assert local.session_id == "example-local" assert local.is_remote is False assert isinstance(remote, RemoteSessionMetadataValue) assert remote.session_id == "example-remote" assert remote.is_remote is True assert remote.repository.owner == "github" @pytest.mark.parametrize( ("handled", "expected_type"), [(True, QueuedCommandHandled), (False, QueuedCommandNotHandled)], ) def test_queued_command_result_deserializes_boolean_discriminator(handled, expected_type): request = CommandsRespondToQueuedCommandRequest.from_dict( {"requestId": "example-request", "result": {"handled": handled}} ) assert isinstance(request.result, expected_type) @pytest.mark.parametrize( ("variant", "expected_handled", "expected_json"), [ (QueuedCommandHandled(), True, '{"handled": true}'), (QueuedCommandNotHandled(), False, '{"handled": false}'), ], ) def test_queued_command_result_serializes_boolean_discriminator( variant, expected_handled, expected_json ): encoded = variant.to_dict() assert encoded["handled"] is expected_handled assert json.dumps(encoded) == expected_json request = CommandsRespondToQueuedCommandRequest(request_id="example-request", result=variant) round_tripped = CommandsRespondToQueuedCommandRequest.from_dict(request.to_dict()) assert request.to_dict()["result"]["handled"] is expected_handled assert isinstance(round_tripped.result, type(variant))