async def test_get_state(mock_app: rx.App, token: str):
    """Test that a get_state populates the top level state and delta calculation is correct.

    Args:
        mock_app: An app that will be returned by `get_app()`
        token: A token.
    """
    mock_app.state_manager.state = mock_app._state = TestState

    # Get instance of ChildState2.
    test_state = await mock_app.state_manager.get_state(
        _substate_key(token, ChildState2)
    )
    assert isinstance(test_state, TestState)
    if isinstance(mock_app.state_manager, (StateManagerMemory, StateManagerDisk)):
        # All substates are available
        assert tuple(sorted(test_state.substates)) == (
            ChildState.get_name(),
            ChildState2.get_name(),
            ChildState3.get_name(),
        )
    else:
        # Sibling states are only populated if they have computed vars
        assert tuple(sorted(test_state.substates)) == (
            ChildState2.get_name(),
            ChildState3.get_name(),
        )

    # Because ChildState3 has a computed var, it is always dirty, and always populated.
    assert (
        test_state.substates[ChildState3.get_name()]
        .substates[GrandchildState3.get_name()]
        .computed
        == ""
    )

    # Get the child_state2 directly.
    child_state2_direct = test_state.get_substate([ChildState2.get_name()])
    child_state2_get_state = await test_state.get_state(ChildState2)
    # These should be the same object.
    assert child_state2_direct is child_state2_get_state

    # Get arbitrary GrandchildState.
    grandchild_state = await child_state2_get_state.get_state(GrandchildState)
    assert isinstance(grandchild_state, GrandchildState)

    # Now the original root should have all substates populated.
    assert tuple(sorted(test_state.substates)) == (
        ChildState.get_name(),
        ChildState2.get_name(),
        ChildState3.get_name(),
    )

    # ChildState should be retrievable
    child_state_direct = test_state.get_substate([ChildState.get_name()])
    child_state_get_state = await test_state.get_state(ChildState)
    # These should be the same object.
    assert child_state_direct is child_state_get_state

    # GrandchildState instance should be the same as the one retrieved from the child_state2.
    assert grandchild_state is child_state_direct.get_substate(
        [GrandchildState.get_name()]
    )
    grandchild_state.value2 = "set_value"

    assert test_state.get_delta() == {
        GrandchildState.get_full_name(): {
            "value2": "set_value",
        },
        GrandchildState3.get_full_name(): {
            "computed": "",
        },
    }

    # Get a fresh instance
    new_test_state = await mock_app.state_manager.get_state(
        _substate_key(token, ChildState2)
    )
    assert isinstance(new_test_state, TestState)
    if isinstance(mock_app.state_manager, (StateManagerMemory, StateManagerDisk)):
        # In memory, it's the same instance
        assert new_test_state is test_state
        test_state._clean()
        # All substates are available
        assert tuple(sorted(new_test_state.substates)) == (
            ChildState.get_name(),
            ChildState2.get_name(),
            ChildState3.get_name(),
        )
    else:
        # With redis, we get a whole new instance
        assert new_test_state is not test_state
        # Sibling states are only populated if they have computed vars
        assert tuple(sorted(new_test_state.substates)) == (
            ChildState2.get_name(),
            ChildState3.get_name(),
        )

    # Set a value on child_state2, should update cached var in grandchild_state2
    child_state2 = new_test_state.get_substate((ChildState2.get_name(),))
    child_state2.value = "set_c2_value"

    assert new_test_state.get_delta() == {
        ChildState2.get_full_name(): {
            "value": "set_c2_value",
        },
        GrandchildState2.get_full_name(): {
            "cached": "set_c2_value",
        },
        GrandchildState3.get_full_name(): {
            "computed": "",
        },
    }
Test that a get_state populates the top level state and delta calculation is correct.

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