# -*- coding: utf-8 -*-
from homehub_backend.testing import services as testing_services


class Test_StopService:
    async def test_unknown_service_instance(self, homehub_client):
        response = await homehub_client.post('/backend/rpc', json={
            'jsonrpc': '2.0',
            'id': 'testing',
            'method': 'services.stop',
            'params': ['testing.FakeService', 'idontexist'],
        })
        assert response.status == 200

        data = await response.json()
        assert 'result' not in data
        assert data['error']['data'] == (
            'Unknown service: idontexist of testing.FakeService'
        )

    async def test_unknown_service_kind(self, homehub_client):
        response = await homehub_client.post('/backend/rpc', json={
            'jsonrpc': '2.0',
            'id': 'testing',
            'method': 'services.stop',
            'params': ['IDontExist', 'fake_instance'],
        })
        assert response.status == 200

        data = await response.json()
        assert 'result' not in data
        assert data['error']['data'] == 'Unknown service: IDontExist'

    async def test_ok(self, homehub_client):
        service = testing_services.FakeService(
            homehub_client.server.app, 'fake_instance', {},
        )
        homehub_client.server.app['SERVICES'].update({
            'testing.FakeService.fake_instance': service,
        })

        response = await homehub_client.post('/backend/rpc', json={
            'jsonrpc': '2.0',
            'id': 'testing',
            'method': 'services.stop',
            'params': ['testing.FakeService', 'fake_instance'],
        })
        assert response.status == 200

        data = await response.json()
        assert data['result'] == 'ok'
        assert 'error' not in data

        assert service.stop.called is True

        assert len(homehub_client.server.app['SERVICES']) == 0
