Test that initialize_result is None before init and contains the full result after.
()
| 584 | |
| 585 | @pytest.mark.anyio |
| 586 | async def test_initialize_result(): |
| 587 | """Test that initialize_result is None before init and contains the full result after.""" |
| 588 | client_to_server_send, client_to_server_receive = anyio.create_memory_object_stream[SessionMessage](1) |
| 589 | server_to_client_send, server_to_client_receive = anyio.create_memory_object_stream[SessionMessage](1) |
| 590 | |
| 591 | expected_capabilities = ServerCapabilities( |
| 592 | logging=types.LoggingCapability(), |
| 593 | prompts=types.PromptsCapability(list_changed=True), |
| 594 | resources=types.ResourcesCapability(subscribe=True, list_changed=True), |
| 595 | tools=types.ToolsCapability(list_changed=False), |
| 596 | ) |
| 597 | expected_server_info = Implementation(name="mock-server", version="0.1.0") |
| 598 | expected_instructions = "Use the tools wisely." |
| 599 | |
| 600 | async def mock_server(): |
| 601 | session_message = await client_to_server_receive.receive() |
| 602 | jsonrpc_request = session_message.message |
| 603 | assert isinstance(jsonrpc_request, JSONRPCRequest) |
| 604 | request = client_request_adapter.validate_python( |
| 605 | jsonrpc_request.model_dump(by_alias=True, mode="json", exclude_none=True) |
| 606 | ) |
| 607 | assert isinstance(request, InitializeRequest) |
| 608 | |
| 609 | result = InitializeResult( |
| 610 | protocol_version=LATEST_HANDSHAKE_VERSION, |
| 611 | capabilities=expected_capabilities, |
| 612 | server_info=expected_server_info, |
| 613 | instructions=expected_instructions, |
| 614 | ) |
| 615 | |
| 616 | async with server_to_client_send: |
| 617 | await server_to_client_send.send( |
| 618 | SessionMessage( |
| 619 | JSONRPCResponse( |
| 620 | jsonrpc="2.0", |
| 621 | id=jsonrpc_request.id, |
| 622 | result=result.model_dump(by_alias=True, mode="json", exclude_none=True), |
| 623 | ) |
| 624 | ) |
| 625 | ) |
| 626 | await client_to_server_receive.receive() |
| 627 | |
| 628 | async with ( |
| 629 | ClientSession( |
| 630 | server_to_client_receive, |
| 631 | client_to_server_send, |
| 632 | ) as session, |
| 633 | anyio.create_task_group() as tg, |
| 634 | client_to_server_send, |
| 635 | client_to_server_receive, |
| 636 | server_to_client_send, |
| 637 | server_to_client_receive, |
| 638 | ): |
| 639 | assert session.initialize_result is None |
| 640 | |
| 641 | tg.start_soon(mock_server) |
| 642 | await session.initialize() |
| 643 |
nothing calls this directly
no test coverage detected