import unittest

from lib.substituir_placeholders import substituir_placeholders_arquivo


class ClienteLinuxFake:
    def __init__(self):
        self.comandos = []

    def executar_comando(self, comando, disparar_excecao_se_retorno_nao_zero=True, echoar=True, descricao=""):
        self.comandos.append(comando)


class TestSubstituirPlaceholders(unittest.TestCase):
    def test_substitui_placeholders_maiores_antes_dos_menores(self):
        cliente_linux = ClienteLinuxFake()

        substituir_placeholders_arquivo(
            cliente_linux,
            "/tmp/docker-compose.yml",
            {
                r"\$PORTA": 9988,
                r"\$PORTA_INTERNA": 5000,
                r"\$NOME_CONTAINER_ENTERPRISE": "enterprise",
                r"\$NOME_CONTAINER_ENTERPRISE_EVENT_HANDLER": "enterprise-event-handler",
                r"\$NOME_CONTAINER_API_GATEWAY": "api-gateway",
                r"\$NOME_CONTAINER_API_GATEWAY_MTLS_PROXY": "mtls-proxy",
                r"\$NOME_CONTAINER_MICROKAFKA": "microkafka",
                r"\$NOME_CONTAINER_MICROKAFKA_UI": "microkafka-ui",
            }
        )

        indice_event_handler = cliente_linux.comandos.index(
            "sed -i 's,\\$NOME_CONTAINER_ENTERPRISE_EVENT_HANDLER,enterprise-event-handler,g' /tmp/docker-compose.yml"
        )
        indice_enterprise = cliente_linux.comandos.index(
            "sed -i 's,\\$NOME_CONTAINER_ENTERPRISE,enterprise,g' /tmp/docker-compose.yml"
        )
        indice_mtls_proxy = cliente_linux.comandos.index(
            "sed -i 's,\\$NOME_CONTAINER_API_GATEWAY_MTLS_PROXY,mtls-proxy,g' /tmp/docker-compose.yml"
        )
        indice_api_gateway = cliente_linux.comandos.index(
            "sed -i 's,\\$NOME_CONTAINER_API_GATEWAY,api-gateway,g' /tmp/docker-compose.yml"
        )
        indice_microkafka_ui = cliente_linux.comandos.index(
            "sed -i 's,\\$NOME_CONTAINER_MICROKAFKA_UI,microkafka-ui,g' /tmp/docker-compose.yml"
        )
        indice_microkafka = cliente_linux.comandos.index(
            "sed -i 's,\\$NOME_CONTAINER_MICROKAFKA,microkafka,g' /tmp/docker-compose.yml"
        )
        indice_porta_interna = cliente_linux.comandos.index(
            "sed -i 's,\\$PORTA_INTERNA,5000,g' /tmp/docker-compose.yml"
        )
        indice_porta = cliente_linux.comandos.index(
            "sed -i 's,\\$PORTA,9988,g' /tmp/docker-compose.yml"
        )

        self.assertLess(indice_event_handler, indice_enterprise)
        self.assertLess(indice_mtls_proxy, indice_api_gateway)
        self.assertLess(indice_microkafka_ui, indice_microkafka)
        self.assertLess(indice_porta_interna, indice_porta)


if __name__ == "__main__":
    unittest.main()
