import os
import threading
import time
import unittest

from OpenDrive.client_side import file_changes_json as c_json
from OpenDrive.client_side import interface
from OpenDrive.client_side import main
from OpenDrive.client_side import paths as client_paths
from OpenDrive.server_side import paths as server_paths
from tests.client_side.helper_client import h_register_dummy_user_device_client
from tests.helper_all import h_client_routine, h_start_server_process, h_stop_server_process, \
    h_clear_init_all_folders, h_create_empty


class TestMain(unittest.TestCase):

    def setUp(self) -> None:
        h_clear_init_all_folders()
        self._server_process = h_start_server_process()
        self.folder1_abs_local_path = client_paths.normalize_path(client_paths.LOCAL_CLIENT_DATA, "folder1")
        h_create_empty(self.folder1_abs_local_path)
        main.MIN_UPDATE_PAUSE_TIME = 1

    def tearDown(self) -> None:
        main.shutdown()
        h_stop_server_process(self._server_process)

    @h_client_routine(clear_folders=False)
    def putest_start_logged_in(self):
        user = h_register_dummy_user_device_client()
        main_thread = threading.Thread(target=main.start, daemon=True)
        main_thread.start()
        time.sleep(2)   # wait till changes.json is created

        interface.add_sync_folder(self.folder1_abs_local_path, "folder1")
        expected_content = c_json.get_all_data()
        file_path = os.path.join(self.folder1_abs_local_path, "dummy.txt")
        with open(file_path, "w") as f:
            f.write("Hello World")
        time.sleep(5)   # wait till synchronization finished
        expected_path = os.path.join(server_paths.get_users_root_folder(user.user_id), "folder1/dummy.txt")
        self.assertTrue(os.path.exists(expected_path), "dummy file is not pulled to server!")
        self.assertEqual(expected_content, c_json.get_all_data())
        time.sleep(1)   # wait till waiting...

[((1007, 1044), 'tests.helper_all.h_client_routine', 'h_client_routine', ([], {'clear_folders': '(False)'}), '(clear_folders=False)\n', (1023, 1044), False, 'from tests.helper_all import h_client_routine, h_start_server_process, h_stop_server_process, h_clear_init_all_folders, h_create_empty\n'), ((609, 635), 'tests.helper_all.h_clear_init_all_folders', 'h_clear_init_all_folders', ([], {}), '()\n', (633, 635), False, 'from tests.helper_all import h_client_routine, h_start_server_process, h_stop_server_process, h_clear_init_all_folders, h_create_empty\n'), ((667, 691), 'tests.helper_all.h_start_server_process', 'h_start_server_process', ([], {}), '()\n', (689, 691), False, 'from tests.helper_all import h_client_routine, h_start_server_process, h_stop_server_process, h_clear_init_all_folders, h_create_empty\n'), ((730, 800), 'OpenDrive.client_side.paths.normalize_path', 'client_paths.normalize_path', (['client_paths.LOCAL_CLIENT_DATA', '"""folder1"""'], {}), "(client_paths.LOCAL_CLIENT_DATA, 'folder1')\n", (757, 800), True, 'from OpenDrive.client_side import paths as client_paths\n'), ((809, 852), 'tests.helper_all.h_create_empty', 'h_create_empty', (['self.folder1_abs_local_path'], {}), '(self.folder1_abs_local_path)\n', (823, 852), False, 'from tests.helper_all import h_client_routine, h_start_server_process, h_stop_server_process, h_clear_init_all_folders, h_create_empty\n'), ((933, 948), 'OpenDrive.client_side.main.shutdown', 'main.shutdown', ([], {}), '()\n', (946, 948), False, 'from OpenDrive.client_side import main\n'), ((957, 1000), 'tests.helper_all.h_stop_server_process', 'h_stop_server_process', (['self._server_process'], {}), '(self._server_process)\n', (978, 1000), False, 'from tests.helper_all import h_client_routine, h_start_server_process, h_stop_server_process, h_clear_init_all_folders, h_create_empty\n'), ((1098, 1135), 'tests.client_side.helper_client.h_register_dummy_user_device_client', 'h_register_dummy_user_device_client', ([], {}), '()\n', (1133, 1135), False, 'from tests.client_side.helper_client import h_register_dummy_user_device_client\n'), ((1158, 1206), 'threading.Thread', 'threading.Thread', ([], {'target': 'main.start', 'daemon': '(True)'}), '(target=main.start, daemon=True)\n', (1174, 1206), False, 'import threading\n'), ((1243, 1256), 'time.sleep', 'time.sleep', (['(2)'], {}), '(2)\n', (1253, 1256), False, 'import time\n'), ((1304, 1369), 'OpenDrive.client_side.interface.add_sync_folder', 'interface.add_sync_folder', (['self.folder1_abs_local_path', '"""folder1"""'], {}), "(self.folder1_abs_local_path, 'folder1')\n", (1329, 1369), False, 'from OpenDrive.client_side import interface\n'), ((1397, 1418), 'OpenDrive.client_side.file_changes_json.get_all_data', 'c_json.get_all_data', ([], {}), '()\n', (1416, 1418), True, 'from OpenDrive.client_side import file_changes_json as c_json\n'), ((1439, 1493), 'os.path.join', 'os.path.join', (['self.folder1_abs_local_path', '"""dummy.txt"""'], {}), "(self.folder1_abs_local_path, 'dummy.txt')\n", (1451, 1493), False, 'import os\n'), ((1577, 1590), 'time.sleep', 'time.sleep', (['(5)'], {}), '(5)\n', (1587, 1590), False, 'import time\n'), ((1906, 1919), 'time.sleep', 'time.sleep', (['(1)'], {}), '(1)\n', (1916, 1919), False, 'import time\n'), ((1667, 1715), 'OpenDrive.server_side.paths.get_users_root_folder', 'server_paths.get_users_root_folder', (['user.user_id'], {}), '(user.user_id)\n', (1701, 1715), True, 'from OpenDrive.server_side import paths as server_paths\n'), ((1762, 1791), 'os.path.exists', 'os.path.exists', (['expected_path'], {}), '(expected_path)\n', (1776, 1791), False, 'import os\n'), ((1875, 1896), 'OpenDrive.client_side.file_changes_json.get_all_data', 'c_json.get_all_data', ([], {}), '()\n', (1894, 1896), True, 'from OpenDrive.client_side import file_changes_json as c_json\n')]