import logging
import subprocess
import time
import unittest

from oraide import send_keys, ConnectionFailedError, SessionNotFoundError


class TestSendKeys(unittest.TestCase):
    session_name = 'oraide_test_session'
    verification_string = '7hh50zxnxj'

    def start_tmux_session(self):
        logging.info('Starting tmux session: {}'.format(self.session_name))
        subprocess.check_call(['tmux', 'new-session', '-d',
                               '-s{}'.format(self.session_name)])
        time.sleep(0.5)  # wait for the session to start
                         # TODO: find a way to determine whether the session is
                         #       ready, instead of waiting and hoping

    def get_tmux_session_contents(self):
        return subprocess.check_output(
            ['tmux', 'capture-pane', '-p',
             '-t{}'.format(self.session_name)])

    def test_literal_keys_appear_in_session(self):
        self.start_tmux_session()

        send_keys(self.session_name, self.verification_string)
        session_output = self.get_tmux_session_contents()

        self.assertIn(self.verification_string, session_output)

    def test_lookup_keys_appear_in_session(self):
        self.start_tmux_session()

        send_keys(self.session_name,
                  'echo "Hello, {}"'.format(self.verification_string))
        send_keys(self.session_name, 'Enter', literal=False)
        session_output = self.get_tmux_session_contents()
        count = session_output.count(self.verification_string)

        self.assertEquals(count, 2)

    def test_wrong_session_raises_session_not_found_error(self):
        self.start_tmux_session()

        with self.assertRaises(SessionNotFoundError):
            send_keys(self.session_name + '__', self.verification_string)

    def test_no_server_raises_connection_failed_error(self):
        with self.assertRaises(ConnectionFailedError):
            send_keys(self.session_name, self.verification_string)

    def tearDown(self):
        subprocess.call(['tmux', 'kill-server'])

@@ -74,16 +74,17 @@
  import 
+(
 send_key
@@ -85,16 +85,25 @@
 nd_keys,
+ Session,
  Connect
@@ -117,16 +117,36 @@
 edError,
+%0A                   
  Session
@@ -158,16 +158,63 @@
 undError
+)%0A%0ATESTING_SESSION_NAME = 'oraide_test_session'
 %0A%0A%0Aclass
@@ -266,37 +266,36 @@
 _name = 
-'oraide_test_session'
+TESTING_SESSION_NAME
 %0A    ver
@@ -2066,32 +2066,39 @@
 n(self):%0A       
+ proc =
  subprocess.call
@@ -2093,20 +2093,21 @@
 process.
-call
+Popen
 (%5B'tmux'
@@ -2122,10 +2122,704 @@
 server'%5D
+, stdout=subprocess.PIPE,%0A                                stderr=subprocess.STDOUT)%0A        proc.communicate()%0A%0A%0Aclass TestSession(unittest.TestCase):%0A    session_name = TESTING_SESSION_NAME%0A%0A    def test_auto_advance_context_manager_restores_state(self):%0A        s1 = Session(self.session_name, enable_auto_advance=True)%0A        s2 = Session(self.session_name)%0A%0A        self.assertTrue(s1.auto_advancing)%0A        with s1.auto_advance():%0A            self.assertTrue(s1.auto_advancing)%0A        self.assertTrue(s1.auto_advancing)%0A%0A        self.assertFalse(s2.auto_advancing)%0A        with s2.auto_advance():%0A            self.assertTrue(s1.auto_advancing)%0A        self.assertFalse(s2.auto_advancing
 )%0A

add test for the auto-advance context manager