# Path: tests/commands/execution/utils.py
# def get_execution_data_mock():
#     exec_id = EXECUTION_DATA['id']
#     datum_id = OUTPUT_DATUM_DATA['id']
#     m = requests_mock.mock()
#     project_id = PROJECT_DATA['id']
#     execution_counter = EXECUTION_DATA['counter']
#     m.get(f'{API_PREFIX}projects/{project_id}/', json=PROJECT_DATA)
#     m.get(f'{API_PREFIX}executions/', json={'results': [EXECUTION_DATA]})
#     m.get(f'{API_PREFIX}executions/{exec_id}/', json=EXECUTION_DATA)
#     m.get(f'{API_PREFIX}executions/{exec_id}/events/', json=EVENT_RESPONSE_DATA)
#     m.get(f'{API_PREFIX}data/?output_execution={exec_id}&limit=9000', json=OUTPUT_DATUM_RESPONSE_DATA)
#     m.get(f'{API_PREFIX}data/{datum_id}/download/', json=OUTPUT_DATUM_DOWNLOAD_RESPONSE_DATA)
#     execution_by_counter_url = f'{API_PREFIX}executions/{project_id}:{execution_counter}/'
#     m.get(url=get_startswith_re(execution_by_counter_url), json=EXECUTION_DATA)
#     m.delete(execution_by_counter_url, json={'ok': True})
#     m.post(re.compile('^https://app.valohai.com/api/v0/data/(.+?)/purge/$'), json={'ok': True})
#     return m
# 
# Path: tests/fixture_data.py
# EXECUTION_DATA = {
#     'counter': random.randint(1, 100),
#     'ctime': '2017-02-08T11:09:16.120102Z',
#     'id': execution_id,
#     'project': PROJECT_DATA,
#     'commit': {
#         'repository': 666,
#         'identifier': '000',
#         'ref': 'master',
#         'commit_time': '2017-02-15T08:46:58Z',
#         'url': 'https://app.valohai.com/api/v0/commits/7/'
#     },
#     'task': None,
#     'duration': 777,
#     'status': 'complete',
#     'step': 'run training',
#     'url': f'https://app.valohai.com/api/v0/executions/{execution_id}/',
#     'urls': {
#         'copy': 'https://app.valohai.com/api/v0/executions/34/copy/',
#         'display': 'https://app.valohai.com/p/test/mnist/execution/34/',
#         'stop': 'https://app.valohai.com/api/v0/executions/34/stop/',
#     },
#     'parameters': {
#         'dropout': 0.9,
#         'learning_rate': 0.001,
#         'max_steps': 300,
#     },
#     'outputs': [
#         OUTPUT_DATUM_DATA,
#     ],
#     'environment': {
#         'id': '88888888-8888-8888-8888-888888888888',
#         'name': 'local',
#         'owner': None,
#         'unfinished_job_count': 0,
#     },
#     'cumulative_metadata': {
#         'oispa': 'beer',
#     },
# }
# 
# PROJECT_DATA = {
#     'id': '000',
#     'name': 'nyan',
#     'description': 'nyan',
#     'owner': 1,
#     'ctime': '2016-12-16T12:25:52.718310Z',
#     'mtime': '2017-01-20T14:35:02.196871Z',
#     'urls': {
#         'display': 'https://app.valohai.com/p/nyan/nyan/',
#     }
# }
# 
# Path: valohai_cli/commands/execution/watch.py
# @click.command()
# @counter_argument
# def watch(counter: str) -> None:
#     """
#     Watch execution progress in a console UI.
#     """
#     execution = get_project(require=True).get_execution_from_counter(counter=counter)
#     tui = WatchTUI(execution)
#     try:
#         while True:
#             tui.refresh()
#             time.sleep(1)
#     except KeyboardInterrupt:
#         get_current_context().exit()


import time
from tests.commands.execution.utils import get_execution_data_mock
from tests.fixture_data import EXECUTION_DATA, PROJECT_DATA
from valohai_cli.commands.execution.watch import watch



def no_sleep(t):
    raise KeyboardInterrupt('no... sleep... til... Brooklyn!')


def test_execution_watch(runner, logged_in_and_linked, monkeypatch):
    monkeypatch.setattr(time, 'sleep', no_sleep)
    with get_execution_data_mock():
        output = runner.invoke(watch, [str(EXECUTION_DATA['counter'])], catch_exceptions=False).output

# Path: tests/commands/execution/utils.py
# def get_execution_data_mock():
#     exec_id = EXECUTION_DATA['id']
#     datum_id = OUTPUT_DATUM_DATA['id']
#     m = requests_mock.mock()
#     project_id = PROJECT_DATA['id']
#     execution_counter = EXECUTION_DATA['counter']
#     m.get(f'{API_PREFIX}projects/{project_id}/', json=PROJECT_DATA)
#     m.get(f'{API_PREFIX}executions/', json={'results': [EXECUTION_DATA]})
#     m.get(f'{API_PREFIX}executions/{exec_id}/', json=EXECUTION_DATA)
#     m.get(f'{API_PREFIX}executions/{exec_id}/events/', json=EVENT_RESPONSE_DATA)
#     m.get(f'{API_PREFIX}data/?output_execution={exec_id}&limit=9000', json=OUTPUT_DATUM_RESPONSE_DATA)
#     m.get(f'{API_PREFIX}data/{datum_id}/download/', json=OUTPUT_DATUM_DOWNLOAD_RESPONSE_DATA)
#     execution_by_counter_url = f'{API_PREFIX}executions/{project_id}:{execution_counter}/'
#     m.get(url=get_startswith_re(execution_by_counter_url), json=EXECUTION_DATA)
#     m.delete(execution_by_counter_url, json={'ok': True})
#     m.post(re.compile('^https://app.valohai.com/api/v0/data/(.+?)/purge/$'), json={'ok': True})
#     return m
# 
# Path: tests/fixture_data.py
# EXECUTION_DATA = {
#     'counter': random.randint(1, 100),
#     'ctime': '2017-02-08T11:09:16.120102Z',
#     'id': execution_id,
#     'project': PROJECT_DATA,
#     'commit': {
#         'repository': 666,
#         'identifier': '000',
#         'ref': 'master',
#         'commit_time': '2017-02-15T08:46:58Z',
#         'url': 'https://app.valohai.com/api/v0/commits/7/'
#     },
#     'task': None,
#     'duration': 777,
#     'status': 'complete',
#     'step': 'run training',
#     'url': f'https://app.valohai.com/api/v0/executions/{execution_id}/',
#     'urls': {
#         'copy': 'https://app.valohai.com/api/v0/executions/34/copy/',
#         'display': 'https://app.valohai.com/p/test/mnist/execution/34/',
#         'stop': 'https://app.valohai.com/api/v0/executions/34/stop/',
#     },
#     'parameters': {
#         'dropout': 0.9,
#         'learning_rate': 0.001,
#         'max_steps': 300,
#     },
#     'outputs': [
#         OUTPUT_DATUM_DATA,
#     ],
#     'environment': {
#         'id': '88888888-8888-8888-8888-888888888888',
#         'name': 'local',
#         'owner': None,
#         'unfinished_job_count': 0,
#     },
#     'cumulative_metadata': {
#         'oispa': 'beer',
#     },
# }
# 
# PROJECT_DATA = {
#     'id': '000',
#     'name': 'nyan',
#     'description': 'nyan',
#     'owner': 1,
#     'ctime': '2016-12-16T12:25:52.718310Z',
#     'mtime': '2017-01-20T14:35:02.196871Z',
#     'urls': {
#         'display': 'https://app.valohai.com/p/nyan/nyan/',
#     }
# }
# 
# Path: valohai_cli/commands/execution/watch.py
# @click.command()
# @counter_argument
# def watch(counter: str) -> None:
#     """
#     Watch execution progress in a console UI.
#     """
#     execution = get_project(require=True).get_execution_from_counter(counter=counter)
#     tui = WatchTUI(execution)
#     try:
#         while True:
#             tui.refresh()
#             time.sleep(1)
#     except KeyboardInterrupt:
#         get_current_context().exit()


# Path: tests/commands/execution/test_watch.py
import time
from tests.commands.execution.utils import get_execution_data_mock
from tests.fixture_data import EXECUTION_DATA, PROJECT_DATA
from valohai_cli.commands.execution.watch import watch




def no_sleep(t):
    raise KeyboardInterrupt('no... sleep... til... Brooklyn!')


def test_execution_watch(runner, logged_in_and_linked, monkeypatch):
    monkeypatch.setattr(time, 'sleep', no_sleep)
    with get_execution_data_mock():
        output = runner.invoke(watch, [str(EXECUTION_DATA['counter'])], catch_exceptions=False).output

        assert EXECUTION_DATA['status'] in output