async def test_state_proxy(
    grandchild_state: GrandchildState, mock_app: rx.App, token: str
):
    """Test that the state proxy works.

    Args:
        grandchild_state: A grandchild state.
        mock_app: An app that will be returned by `get_app()`
        token: A token.
    """
    child_state = grandchild_state.parent_state
    assert child_state is not None
    parent_state = child_state.parent_state
    assert parent_state is not None
    router_data = RouterData({"query": {}, "token": token, "sid": "test_sid"})
    grandchild_state.router = router_data
    namespace = mock_app.event_namespace
    assert namespace is not None
    namespace.sid_to_token[router_data.session.session_id] = token
    if isinstance(mock_app.state_manager, (StateManagerMemory, StateManagerDisk)):
        mock_app.state_manager.states[parent_state.router.session.client_token] = (
            parent_state
        )
    elif isinstance(mock_app.state_manager, StateManagerRedis):
        pickle_state = parent_state._serialize()
        if pickle_state:
            await mock_app.state_manager.redis.set(
                _substate_key(parent_state.router.session.client_token, parent_state),
                pickle_state,
                ex=mock_app.state_manager.token_expiration,
            )

    sp = StateProxy(grandchild_state)
    assert sp.__wrapped__ == grandchild_state
    assert sp._self_substate_path == tuple(grandchild_state.get_full_name().split("."))
    assert sp._self_app is mock_app
    assert not sp._self_mutable
    assert sp._self_actx is None

    # cannot use normal contextmanager protocol
    with pytest.raises(TypeError), sp:
        pass

    with pytest.raises(ImmutableStateError):
        # cannot directly modify state proxy outside of async context
        sp.value2 = "16"

    with pytest.raises(ImmutableStateError):
        # Cannot get_state
        await sp.get_state(ChildState)

    with pytest.raises(ImmutableStateError):
        # Cannot access get_substate
        sp.get_substate([])

    with pytest.raises(ImmutableStateError):
        # Cannot access parent state
        sp.parent_state.get_name()

    with pytest.raises(ImmutableStateError):
        # Cannot access substates
        sp.substates[""]

    async with sp:
        assert sp._self_actx is not None
        assert sp._self_mutable  # proxy is mutable inside context
        if isinstance(mock_app.state_manager, (StateManagerMemory, StateManagerDisk)):
            # For in-process store, only one instance of the state exists
            assert sp.__wrapped__ is grandchild_state
        else:
            # When redis is used, a new+updated instance is assigned to the proxy
            assert sp.__wrapped__ is not grandchild_state
        sp.value2 = "42"
    assert not sp._self_mutable  # proxy is not mutable after exiting context
    assert sp._self_actx is None
    assert sp.value2 == "42"

    # Get the state from the state manager directly and check that the value is updated
    gotten_state = await mock_app.state_manager.get_state(
        _substate_key(grandchild_state.router.session.client_token, grandchild_state)
    )
    if isinstance(mock_app.state_manager, (StateManagerMemory, StateManagerDisk)):
        # For in-process store, only one instance of the state exists
        assert gotten_state is parent_state
    else:
        assert gotten_state is not parent_state
    gotten_grandchild_state = gotten_state.get_substate(sp._self_substate_path)
    assert gotten_grandchild_state is not None
    assert gotten_grandchild_state.value2 == "42"

    # ensure state update was emitted
    assert mock_app.event_namespace is not None
    mock_app.event_namespace.emit.assert_called_once()  # pyright: ignore [reportFunctionMemberAccess]
    mcall = mock_app.event_namespace.emit.mock_calls[0]  # pyright: ignore [reportFunctionMemberAccess]
    assert mcall.args[0] == str(SocketEvent.EVENT)
    assert mcall.args[1] == StateUpdate(
        delta={
            TestState.get_full_name(): {"router": router_data},
            grandchild_state.get_full_name(): {
                "value2": "42",
            },
            GrandchildState3.get_full_name(): {
                "computed": "",
            },
        }
    )
    assert mcall.kwargs["to"] == grandchild_state.router.session.session_id
Test that the state proxy works.

    Args:
        grandchild_state: A grandchild state.
        mock_app: An app that will be returned by `get_app()`
        token: A token.
    
tests/units/test_state.py