import asyncio
import json
import pytest

from asynctest import TestCase as AsyncTestCase
from asynctest import mock as async_mock
from copy import deepcopy

from collections import OrderedDict

from ....cache.base import BaseCache
from ....cache.in_memory import InMemoryCache
from ....core.in_memory import InMemoryProfile
from ....ledger.base import BaseLedger
from ....messaging.responder import BaseResponder

from ...error import LedgerError
from ...indy_vdr import IndyVdrLedger, IndyVdrLedgerPool
from ...merkel_validation.tests.test_data import GET_NYM_REPLY

from .. import indy_vdr_manager as test_module
from ..base_manager import MultipleLedgerManagerError
from ..indy_vdr_manager import MultiIndyVDRLedgerManager

GET_NYM_INDY_VDR_REPLY = {
    "data": {
        "dest": "Av63wJYM7xYR4AiygYq4c3",
        "identifier": "V4SGRU86Z58d6TV7PBUe6f",
        "role": "101",
        "seqNo": 17794,
        "txnTime": 1632262244,
        "verkey": "6QSduYdf8Bi6t8PfNm5vNomGWDtXhmMmTRzaciudBXYJ",
    },
    "dest": "Av63wJYM7xYR4AiygYq4c3",
    "identifier": "LibindyDid111111111111",
    "reqId": 1632267113185021500,
    "seqNo": 17794,
    "state_proof": {
        "multi_signature": {
            "participants": ["Node2", "Node3", "Node1"],
            "signature": "Qye7WDGrhwEpr2MUmQ2hhm8yWAsUG6gKKf4TXxrw7BybGA96HWXLXhnV5gm5HBQCb4sDXiirTKuyWgMDyfDxKewya9mZhkGXf5WzaADFuaoJkTeSywqqmsrfpcHc2e49eEyncpCxFzhJn6sius4jLgJ7MAfSeVGwyydeR1YsJb3Nm5",
            "value": {
                "ledger_id": 1,
                "pool_state_root_hash": "7siDH8Qanh82UviK4zjBSfLXcoCvLaeGkrByi1ow9Tsm",
                "state_root_hash": "GJq4XL4pJYnDGg3MJ64y3QnfuezxsuBEezk5GC5yaZPM",
                "timestamp": 1632266842,
                "txn_root_hash": "BTnnWQ7imcHSoMykHLeYZX5q8eGEHWdbUydQNA4RG8La",
            },
        },
        "proof_nodes": r"+QZT+QIRoPfBdyHC/yQ9E7ccJxuGSGyin0AZ5xy0zfA8N6Wc75n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64y3QnfuezxsuBEezk5GC5yaZPM",
    },
    "txnTime": 1632262244,
    "type": "105",
}


@pytest.mark.indy_vdr
class TestMultiIndyVDRLedgerManager(AsyncTestCase):
    async def setUp(self):
        self.profile = InMemoryProfile.test_profile(bind={BaseCache: InMemoryCache()})
        self.context = self.profile.context
        setattr(self.context, "profile", self.profile)
        self.responder = async_mock.CoroutineMock(send=async_mock.CoroutineMock())
        self.context.injector.bind_instance(BaseResponder, self.responder)
        self.production_ledger = OrderedDict()
        self.non_production_ledger = OrderedDict()
        test_prod_ledger = IndyVdrLedger(IndyVdrLedgerPool("test_prod_1"), self.profile)
        writable_ledgers = set()
        self.production_ledger["test_prod_1"] = test_prod_ledger
        self.production_ledger["test_prod_2"] = IndyVdrLedger(
            IndyVdrLedgerPool("test_prod_2"), self.profile
        )
        self.non_production_ledger["test_non_prod_1"] = IndyVdrLedger(
            IndyVdrLedgerPool("test_non_prod_1"), self.profile
        )
        self.non_production_ledger["test_non_prod_2"] = IndyVdrLedger(
            IndyVdrLedgerPool("test_non_prod_2"), self.profile
        )
        writable_ledgers.add("test_prod_1")
        writable_ledgers.add("test_prod_2")
        self.manager = MultiIndyVDRLedgerManager(
            self.profile,
            production_ledgers=self.production_ledger,
            non_production_ledgers=self.non_production_ledger,
            writable_ledgers=writable_ledgers,
        )

    def test_get_endorser_info_for_ledger(self):
        writable_ledgers = set()
        writable_ledgers.add("test_prod_1")
        writable_ledgers.add("test_prod_2")

        endorser_info_map = {}
        endorser_info_map["test_prod_1"] = {
            "endorser_did": "test_public_did_1",
            "endorser_alias": "endorser_1",
        }
        endorser_info_map["test_prod_2"] = {
            "endorser_did": "test_public_did_2",
            "endorser_alias": "endorser_2",
        }
        manager = MultiIndyVDRLedgerManager(
            self.profile,
            production_ledgers=self.production_ledger,
            non_production_ledgers=self.non_production_ledger,
            writable_ledgers=writable_ledgers,
            endorser_map=endorser_info_map,
        )
        assert (
            "endorser_1"
        ), "test_public_did_1" == manager.get_endorser_info_for_ledger("test_prod_1")
        assert (
            "endorser_2"
        ), "test_public_did_2" == manager.get_endorser_info_for_ledger("test_prod_2")

    async def test_get_write_ledgers(self):
        ledger_ids = await self.manager.get_write_ledgers()
        assert "test_prod_1" in ledger_ids
        assert "test_prod_2" in ledger_ids

    async def test_get_write_ledger_from_base_ledger(self):
        ledger_id = await self.manager.get_ledger_id_by_ledger_pool_name("test_prod_1")
        assert ledger_id == "test_prod_1"

    async def test_set_profile_write_ledger(self):
        writable_ledgers = set()
        writable_ledgers.add("test_prod_1")
        writable_ledgers.add("test_prod_2")
        endorser_info_map = {}
        endorser_info_map["test_prod_2"] = {
            "endorser_did": "test_public_did_2",
            "endorser_alias": "endorser_2",
        }
        manager = MultiIndyVDRLedgerManager(
            self.profile,
            production_ledgers=self.production_ledger,
            non_production_ledgers=self.non_production_ledger,
            writable_ledgers=writable_ledgers,
            endorser_map=endorser_info_map,
        )
        profile = InMemoryProfile.test_profile()
        assert not profile.inject_or(BaseLedger)
        assert "test_prod_2" in manager.writable_ledgers
        new_write_ledger_id = await manager.set_profile_write_ledger(
            profile=profile, ledger_id="test_prod_2"
        )
        assert new_write_ledger_id == "test_prod_2"
        new_write_ledger = profile.inject_or(BaseLedger)
        assert new_write_ledger.pool_name == "test_prod_2"

    async def test_set_profile_write_ledger_x(self):
        profile = InMemoryProfile.test_profile()
        with self.assertRaises(MultipleLedgerManagerError) as cm:
            new_write_ledger_id = await self.manager.set_profile_write_ledger(
                profile=profile, ledger_id="test_non_prod_1"
            )
        assert "is not write configurable" in str(cm.exception.message)

    async def test_get_ledger_inst_by_id(self):
        ledger_inst = await self.manager.get_ledger_inst_by_id("test_prod_2")
        assert ledger_inst
        ledger_inst = await self.manager.get_ledger_inst_by_id("test_non_prod_2")
        assert ledger_inst
        ledger_inst = await self.manager.get_ledger_inst_by_id("test_invalid")
        assert not ledger_inst

    @async_mock.patch("aries_cloudagent.ledger.indy_vdr.IndyVdrLedgerPool.context_open")
    @async_mock.patch(
        "aries_cloudagent.ledger.indy_vdr.IndyVdrLedgerPool.context_close"
    )
    @async_mock.patch("indy_vdr.ledger.build_get_nym_request")
    @async_mock.patch("aries_cloudagent.ledger.indy_vdr.IndyVdrLedger._submit")
    async def test_get_ledger_by_did_self_cert_a(
        self, mock_submit, mock_build_get_nym_req, mock_close, mock_open
    ):
        with async_mock.patch.object(
            test_module.asyncio, "wait", async_mock.CoroutineMock()
        ) as mock_wait:
            mock_build_get_nym_req.return_value = async_mock.MagicMock()
            mock_submit.return_value = json.dumps(GET_NYM_INDY_VDR_REPLY)
            mock_wait.return_value = mock_submit.return_value
            (
                ledger_id,
                ledger_inst,
                is_self_certified,
            ) = await self.manager._get_ledger_by_did(
                "test_prod_1", "Av63wJYM7xYR4AiygYq4c3"
            )
            assert ledger_id == "test_prod_1"
            assert ledger_inst.pool.name == "test_prod_1"
            assert is_self_certified

    @async_mock.patch("aries_cloudagent.ledger.indy_vdr.IndyVdrLedgerPool.context_open")
    @async_mock.patch(
        "aries_cloudagent.ledger.indy_vdr.IndyVdrLedgerPool.context_close"
    )
    @async_mock.patch("indy_vdr.ledger.build_get_nym_request")
    @async_mock.patch("aries_cloudagent.ledger.indy_vdr.IndyVdrLedger._submit")
    async def test_get_ledger_by_did_self_cert_b(
        self, mock_submit, mock_build_get_nym_req, mock_close, mock_open
    ):
        self.non_production_ledger = OrderedDict()
        self.non_production_ledger["test_non_prod_1"] = IndyVdrLedger(
            IndyVdrLedgerPool("test_non_prod_1"), self.profile
        )
        self.non_production_ledger["test_non_prod_2"] = IndyVdrLedger(
            IndyVdrLedgerPool("test_non_prod_2"), self.profile
        )
        self.manager = MultiIndyVDRLedgerManager(
            self.profile,
            non_production_ledgers=self.non_production_ledger,
        )
        with async_mock.patch.object(
            test_module.asyncio, "wait", async_mock.CoroutineMock()
        ) as mock_wait:
            mock_build_get_nym_req.return_value = async_mock.MagicMock()
            mock_submit.return_value = json.dumps(GET_NYM_INDY_VDR_REPLY)
            mock_wait.return_value = mock_submit.return_value
            (
                ledger_id,
                ledger_inst,
                is_self_certified,
            ) = await self.manager._get_ledger_by_did(
                "test_non_prod_1", "Av63wJYM7xYR4AiygYq4c3"
            )
            assert ledger_id == "test_non_prod_1"
            assert ledger_inst.pool.name == "test_non_prod_1"
            assert is_self_certified

    @async_mock.patch("aries_cloudagent.ledger.indy_vdr.IndyVdrLedgerPool.context_open")
    @async_mock.patch(
        "aries_cloudagent.ledger.indy_vdr.IndyVdrLedgerPool.context_close"
    )
    @async_mock.patch("indy_vdr.ledger.build_get_nym_request")
    @async_mock.patch("aries_cloudagent.ledger.indy_vdr.IndyVdrLedger._submit")
    async def test_get_ledger_by_did_not_self_cert(
        self, mock_submit, mock_build_get_nym_req, mock_close, mock_open
    ):
        get_nym_reply = deepcopy(GET_NYM_INDY_VDR_REPLY)
        get_nym_reply["data"] = json.dumps(
            {
                "dest": "Av63wJYM7xYR4AiygYq4c3",
                "identifier": "V4SGRU86Z58d6TV7PBUe6f",
                "role": "101",
                "seqNo": 17794,
                "txnTime": 1632262244,
                "verkey": "ABUF7uxYTxZ6qYdZ4G9e1Gi",
            }
        )
        with async_mock.patch.object(
            test_module.asyncio, "wait", async_mock.CoroutineMock()
        ) as mock_wait, async_mock.patch.object(
            test_module.SubTrie, "verify_spv_proof", async_mock.CoroutineMock()
        ) as mock_verify_spv_proof:
            mock_build_get_nym_req.return_value = async_mock.MagicMock()
            mock_submit.return_value = get_nym_reply
            mock_wait.return_value = mock_submit.return_value
            mock_verify_spv_proof.return_value = True
            (
                ledger_id,
                ledger_inst,
                is_self_certified,
            ) = await self.manager._get_ledger_by_did(
                "test_prod_1", "Av63wJYM7xYR4AiygYq4c3"
            )
            assert ledger_id == "test_prod_1"
            assert ledger_inst.pool.name == "test_prod_1"
            assert not is_self_certified

    @async_mock.patch("aries_cloudagent.ledger.indy_vdr.IndyVdrLedgerPool.context_open")
    @async_mock.patch(
        "aries_cloudagent.ledger.indy_vdr.IndyVdrLedgerPool.context_close"
    )
    @async_mock.patch("indy_vdr.ledger.build_get_nym_request")
    @async_mock.patch("aries_cloudagent.ledger.indy_vdr.IndyVdrLedger._submit")
    async def test_get_ledger_by_did_state_proof_not_valid(
        self, mock_submit, mock_build_get_nym_req, mock_close, mock_open
    ):
        get_nym_reply = deepcopy(GET_NYM_REPLY)
        get_nym_reply["result"]["data"]["verkey"] = "ABUF7uxYTxZ6qYdZ4G9e1Gi"
        with async_mock.patch.object(
            test_module.asyncio, "wait", async_mock.CoroutineMock()
        ) as mock_wait:
            mock_build_get_nym_req.return_value = async_mock.MagicMock()
            mock_submit.return_value = json.dumps(get_nym_reply)
            mock_wait.return_value = mock_submit.return_value
            assert not await self.manager._get_ledger_by_did(
                "test_prod_1", "Av63wJYM7xYR4AiygYq4c3"
            )

    @async_mock.patch("aries_cloudagent.ledger.indy_vdr.IndyVdrLedgerPool.context_open")
    @async_mock.patch(
        "aries_cloudagent.ledger.indy_vdr.IndyVdrLedgerPool.context_close"
    )
    @async_mock.patch("indy_vdr.ledger.build_get_nym_request")
    @async_mock.patch("aries_cloudagent.ledger.indy_vdr.IndyVdrLedger._submit")
    async def test_get_ledger_by_did_no_data(
        self, mock_submit, mock_build_get_nym_req, mock_close, mock_open
    ):
        get_nym_reply = deepcopy(GET_NYM_INDY_VDR_REPLY)
        get_nym_reply.pop("data")
        with async_mock.patch.object(
            test_module.asyncio, "wait", async_mock.CoroutineMock()
        ) as mock_wait:
            mock_build_get_nym_req.return_value = async_mock.MagicMock()
            mock_submit.return_value = get_nym_reply
            mock_wait.return_value = mock_submit.return_value
            assert not await self.manager._get_ledger_by_did(
                "test_prod_1", "Av63wJYM7xYR4AiygYq4c3"
            )

    @async_mock.patch("aries_cloudagent.ledger.indy_vdr.IndyVdrLedgerPool.context_open")
    @async_mock.patch(
        "aries_cloudagent.ledger.indy_vdr.IndyVdrLedgerPool.context_close"
    )
    @async_mock.patch("indy_vdr.ledger.build_get_nym_request")
    @async_mock.patch("aries_cloudagent.ledger.indy_vdr.IndyVdrLedger._submit")
    async def test_get_ledger_by_did_timeout(
        self, mock_submit, mock_build_get_nym_req, mock_close, mock_open
    ):
        mock_build_get_nym_req.return_value = async_mock.MagicMock()
        mock_submit.side_effect = asyncio.TimeoutError
        assert not await self.manager._get_ledger_by_did(
            "test_prod_1", "Av63wJYM7xYR4AiygYq4c3"
        )

    @async_mock.patch("aries_cloudagent.ledger.indy_vdr.IndyVdrLedgerPool.context_open")
    @async_mock.patch(
        "aries_cloudagent.ledger.indy_vdr.IndyVdrLedgerPool.context_close"
    )
    @async_mock.patch("indy_vdr.ledger.build_get_nym_request")
    @async_mock.patch("aries_cloudagent.ledger.indy_vdr.IndyVdrLedger._submit")
    async def test_get_ledger_by_did_ledger_error(
        self, mock_submit, mock_build_get_nym_req, mock_close, mock_open
    ):
        mock_build_get_nym_req.return_value = async_mock.MagicMock()
        mock_submit.side_effect = LedgerError
        assert not await self.manager._get_ledger_by_did(
            "test_prod_1", "Av63wJYM7xYR4AiygYq4c3"
        )

    @async_mock.patch("aries_cloudagent.ledger.indy_vdr.IndyVdrLedgerPool.context_open")
    @async_mock.patch(
        "aries_cloudagent.ledger.indy_vdr.IndyVdrLedgerPool.context_close"
    )
    @async_mock.patch("indy_vdr.ledger.build_get_nym_request")
    @async_mock.patch("aries_cloudagent.ledger.indy_vdr.IndyVdrLedger._submit")
    async def test_lookup_did_in_configured_ledgers_self_cert_prod(
        self, mock_submit, mock_build_get_nym_req, mock_close, mock_open
    ):
        with async_mock.patch.object(
            test_module.asyncio, "wait", async_mock.CoroutineMock()
        ) as mock_wait:
            mock_build_get_nym_req.return_value = async_mock.MagicMock()
            mock_submit.return_value = json.dumps(GET_NYM_INDY_VDR_REPLY)
            mock_wait.return_value = mock_submit.return_value
            (
                ledger_id,
                ledger_inst,
            ) = await self.manager.lookup_did_in_configured_ledgers(
                "Av63wJYM7xYR4AiygYq4c3", cache_did=True
            )
            assert ledger_id == "test_prod_1"
            assert ledger_inst.pool.name == "test_prod_1"

    @async_mock.patch("aries_cloudagent.ledger.indy_vdr.IndyVdrLedgerPool.context_open")
    @async_mock.patch(
        "aries_cloudagent.ledger.indy_vdr.IndyVdrLedgerPool.context_close"
    )
    @async_mock.patch("indy_vdr.ledger.build_get_nym_request")
    @async_mock.patch("aries_cloudagent.ledger.indy_vdr.IndyVdrLedger._submit")
    async def test_get_ledger_by_did_not_self_cert_not_self_cert_prod(
        self, mock_submit, mock_build_get_nym_req, mock_close, mock_open
    ):
        get_nym_reply = deepcopy(GET_NYM_INDY_VDR_REPLY)
        get_nym_reply["data"]["verkey"] = "ABUF7uxYTxZ6qYdZ4G9e1Gi"
        with async_mock.patch.object(
            test_module.asyncio, "wait", async_mock.CoroutineMock()
        ) as mock_wait, async_mock.patch.object(
            test_module.SubTrie, "verify_spv_proof", async_mock.CoroutineMock()
        ) as mock_verify_spv_proof:
            mock_build_get_nym_req.return_value = async_mock.MagicMock()
            mock_submit.return_value = get_nym_reply
            mock_wait.return_value = mock_submit.return_value
            mock_verify_spv_proof.return_value = True
            (
                ledger_id,
                ledger_inst,
            ) = await self.manager.lookup_did_in_configured_ledgers(
                "Av63wJYM7xYR4AiygYq4c3", cache_did=True
            )
            assert ledger_id == "test_prod_1"
            assert ledger_inst.pool.name == "test_prod_1"

    @async_mock.patch("aries_cloudagent.ledger.indy_vdr.IndyVdrLedgerPool.context_open")
    @async_mock.patch(
        "aries_cloudagent.ledger.indy_vdr.IndyVdrLedgerPool.context_close"
    )
    @async_mock.patch("indy_vdr.ledger.build_get_nym_request")
    @async_mock.patch("aries_cloudagent.ledger.indy_vdr.IndyVdrLedger._submit")
    async def test_lookup_did_in_configured_ledgers_self_cert_non_prod(
        self, mock_submit, mock_build_get_nym_req, mock_close, mock_open
    ):
        self.non_production_ledger = OrderedDict()
        self.non_production_ledger["test_non_prod_1"] = IndyVdrLedger(
            IndyVdrLedgerPool("test_non_prod_1"), self.profile
        )
        self.non_production_ledger["test_non_prod_2"] = IndyVdrLedger(
            IndyVdrLedgerPool("test_non_prod_2"), self.profile
        )
        self.manager = MultiIndyVDRLedgerManager(
            self.profile,
            non_production_ledgers=self.non_production_ledger,
        )
        with async_mock.patch.object(
            test_module.asyncio, "wait", async_mock.CoroutineMock()
        ) as mock_wait:
            mock_build_get_nym_req.return_value = async_mock.MagicMock()
            mock_submit.return_value = GET_NYM_INDY_VDR_REPLY
            mock_wait.return_value = mock_submit.return_value
            (
                ledger_id,
                ledger_inst,
            ) = await self.manager.lookup_did_in_configured_ledgers(
                "Av63wJYM7xYR4AiygYq4c3", cache_did=True
            )
            assert ledger_id == "test_non_prod_1"
            assert ledger_inst.pool.name == "test_non_prod_1"

    @async_mock.patch("aries_cloudagent.ledger.indy_vdr.IndyVdrLedgerPool.context_open")
    @async_mock.patch(
        "aries_cloudagent.ledger.indy_vdr.IndyVdrLedgerPool.context_close"
    )
    @async_mock.patch("indy_vdr.ledger.build_get_nym_request")
    @async_mock.patch("aries_cloudagent.ledger.indy_vdr.IndyVdrLedger._submit")
    async def test_get_ledger_by_did_not_self_cert_non_prod(
        self, mock_submit, mock_build_get_nym_req, mock_close, mock_open
    ):
        self.non_production_ledger = OrderedDict()
        self.non_production_ledger["test_non_prod_1"] = IndyVdrLedger(
            IndyVdrLedgerPool("test_non_prod_1"), self.profile
        )
        self.non_production_ledger["test_non_prod_2"] = IndyVdrLedger(
            IndyVdrLedgerPool("test_non_prod_2"), self.profile
        )
        self.manager = MultiIndyVDRLedgerManager(
            self.profile,
            non_production_ledgers=self.non_production_ledger,
        )
        get_nym_reply = deepcopy(GET_NYM_REPLY)
        get_nym_reply["result"]["data"]["verkey"] = "ABUF7uxYTxZ6qYdZ4G9e1Gi"
        with async_mock.patch.object(
            test_module.asyncio, "wait", async_mock.CoroutineMock()
        ) as mock_wait, async_mock.patch.object(
            test_module.SubTrie, "verify_spv_proof", async_mock.CoroutineMock()
        ) as mock_verify_spv_proof:
            mock_build_get_nym_req.return_value = async_mock.MagicMock()
            mock_submit.return_value = get_nym_reply
            mock_wait.return_value = mock_submit.return_value
            mock_verify_spv_proof.return_value = True
            (
                ledger_id,
                ledger_inst,
            ) = await self.manager.lookup_did_in_configured_ledgers(
                "Av63wJYM7xYR4AiygYq4c3", cache_did=True
            )
            assert ledger_id == "test_non_prod_1"
            assert ledger_inst.pool.name == "test_non_prod_1"

    @async_mock.patch("aries_cloudagent.ledger.indy_vdr.IndyVdrLedgerPool.context_open")
    @async_mock.patch(
        "aries_cloudagent.ledger.indy_vdr.IndyVdrLedgerPool.context_close"
    )
    @async_mock.patch("indy_vdr.ledger.build_get_nym_request")
    @async_mock.patch("aries_cloudagent.ledger.indy_vdr.IndyVdrLedger._submit")
    async def test_lookup_did_in_configured_ledgers_x(
        self, mock_submit, mock_build_get_nym_req, mock_close, mock_open
    ):
        with async_mock.patch.object(
            test_module.asyncio, "wait", async_mock.CoroutineMock()
        ) as mock_wait, async_mock.patch.object(
            test_module.SubTrie, "verify_spv_proof", async_mock.CoroutineMock()
        ) as mock_verify_spv_proof:
            mock_build_get_nym_req.return_value = async_mock.MagicMock()
            mock_submit.return_value = GET_NYM_INDY_VDR_REPLY
            mock_wait.return_value = mock_submit.return_value
            mock_verify_spv_proof.return_value = False
            with self.assertRaises(MultipleLedgerManagerError) as cm:
                await self.manager.lookup_did_in_configured_ledgers(
                    "Av63wJYM7xYR4AiygYq4c3", cache_did=True
                )
                assert "not found in any of the ledgers total: (production: " in cm

    @async_mock.patch("aries_cloudagent.ledger.indy_vdr.IndyVdrLedgerPool.context_open")
    @async_mock.patch(
        "aries_cloudagent.ledger.indy_vdr.IndyVdrLedgerPool.context_close"
    )
    @async_mock.patch("indy_vdr.ledger.build_get_nym_request")
    @async_mock.patch("aries_cloudagent.ledger.indy_vdr.IndyVdrLedger._submit")
    async def test_lookup_did_in_configured_ledgers_prod_not_cached(
        self, mock_submit, mock_build_get_nym_req, mock_close, mock_open
    ):
        with async_mock.patch.object(
            test_module.asyncio, "wait", async_mock.CoroutineMock()
        ) as mock_wait:
            mock_build_get_nym_req.return_value = async_mock.MagicMock()
            mock_submit.return_value = GET_NYM_INDY_VDR_REPLY
            mock_wait.return_value = mock_submit.return_value
            (
                ledger_id,
                ledger_inst,
            ) = await self.manager.lookup_did_in_configured_ledgers(
                "Av63wJYM7xYR4AiygYq4c3", cache_did=False
            )
            assert ledger_id == "test_prod_1"
            assert ledger_inst.pool.name == "test_prod_1"

    async def test_lookup_did_in_configured_ledgers_cached_prod_ledger(self):
        cache = InMemoryCache()
        await cache.set("did_ledger_id_resolver::Av63wJYM7xYR4AiygYq4c3", "test_prod_1")
        self.profile.context.injector.bind_instance(BaseCache, cache)
        (
            ledger_id,
            ledger_inst,
        ) = await self.manager.lookup_did_in_configured_ledgers(
            "Av63wJYM7xYR4AiygYq4c3", cache_did=True
        )
        assert ledger_id == "test_prod_1"
        assert ledger_inst.pool.name == "test_prod_1"

    async def test_lookup_did_in_configured_ledgers_cached_non_prod_ledger(self):
        cache = InMemoryCache()
        await cache.set(
            "did_ledger_id_resolver::Av63wJYM7xYR4AiygYq4c3", "test_non_prod_2", None
        )
        self.profile.context.injector.bind_instance(BaseCache, cache)
        (
            ledger_id,
            ledger_inst,
        ) = await self.manager.lookup_did_in_configured_ledgers(
            "Av63wJYM7xYR4AiygYq4c3", cache_did=True
        )
        assert ledger_id == "test_non_prod_2"
        assert ledger_inst.pool.name == "test_non_prod_2"

    async def test_lookup_did_in_configured_ledgers_cached_x(self):
        cache = InMemoryCache()
        await cache.set("did_ledger_id_resolver::Av63wJYM7xYR4AiygYq4c3", "invalid_id")
        self.profile.context.injector.bind_instance(BaseCache, cache)
        with self.assertRaises(MultipleLedgerManagerError) as cm:
            await self.manager.lookup_did_in_configured_ledgers(
                "Av63wJYM7xYR4AiygYq4c3", cache_did=True
            )
            assert "cached ledger_id invalid_id not found in either" in cm

    async def test_get_production_ledgers(self):
        assert len(await self.manager.get_prod_ledgers()) == 2

    async def test_get_non_production_ledgers(self):
        assert len(await self.manager.get_nonprod_ledgers()) == 2
