# Path: siebenapp/cli.py
# class IO:
#     """Simple wrapper for all input/output interactions"""
# 
#     def write(self, text: str, *args) -> None:
#         raise NotImplementedError
# 
#     def read(self) -> str:
#         raise NotImplementedError
# 
# def update_message(message=""):
#     global USER_MESSAGE
#     USER_MESSAGE = message
# 
# def loop(io: IO, goals: Graph, db_name: str):
#     cmd = ""
#     while cmd != "q":
#         render_result = Renderer(goals, 100).build()
#         rows = render_result.graph
#         index = sorted(
#             [
#                 (goal_id, goal_vars["row"], goal_vars["col"])
#                 for goal_id, goal_vars in rows.items()
#                 if isinstance(goal_id, int)
#             ],
#             key=itemgetter(1, 2),
#             reverse=True,
#         )
#         id_width = len(str(max(g for g in rows.keys() if isinstance(g, int))))
#         for item in index:
#             goal_id = item[0]
#             goal_vars = rows[goal_id]
#             io.write(fmt(goal_id, goal_vars, id_width))
#         if USER_MESSAGE:
#             io.write(USER_MESSAGE)
#         update_message()
#         try:
#             cmd = io.read().strip()
#         except EOFError:
#             break
#         actions = build_actions(cmd)
#         if actions:
#             goals.accept_all(*actions)
#         save(goals, db_name)
# 
# Path: siebenapp/system.py
# def load(filename: str, message_fn: Callable[[str], None] = None) -> Enumeration:
#     zoom_data: ZoomData = []
#     autolink_data: AutoLinkData = []
#     if path.isfile(filename):
#         connection = sqlite3.connect(filename)
#         run_migrations(connection)
#         cur = connection.cursor()
#         names = list(cur.execute("select * from goals"))
#         edges = list(cur.execute("select parent, child, reltype from edges"))
#         settings = list(cur.execute("select * from settings"))
#         zoom_data = list(cur.execute("select * from zoom"))
#         autolink_data = list(cur.execute("select * from autolink"))
#         cur.close()
#         goals = Goals.build(names, edges, settings, message_fn)
#     else:
#         goals = Goals("Rename me", message_fn)
#     result = Enumeration(all_layers(goals, zoom_data, autolink_data))
#     result.verify()
#     return result


from typing import List
from approvaltests import verify, Options  # type: ignore
from approvaltests.reporters import GenericDiffReporterFactory  # type: ignore
from approvaltests.reporters.generic_diff_reporter import GenericDiffReporter  # type: ignore
from siebenapp.cli import IO, update_message, loop
from siebenapp.system import load
class DummyIO(IO):
    def __init__(self, commands: List[str], log: List[str]):
        super().__init__()
        self.commands = commands
        self.log = log

    def write(self, text: str, *args) -> None:
        self.log.append(" ".join([text, *args]))

    def read(self) -> str:
        if self.commands:
            command = self.commands.pop(0)
            self.log.append(f"> {command}\n")
            return command
        self.log.append("*USER BREAK*")
        raise EOFError("Commands list is empty")


def test_simple_scenario():
    log = []
    commands = [
        "r Approval testing",
        "a Measure coverage",
        "2",
        "a Build simple test",
        "3",
        "c",
    ]
    io = DummyIO(commands, log)
    db_name = ":memory:"

# Path: siebenapp/cli.py
# class IO:
#     """Simple wrapper for all input/output interactions"""
# 
#     def write(self, text: str, *args) -> None:
#         raise NotImplementedError
# 
#     def read(self) -> str:
#         raise NotImplementedError
# 
# def update_message(message=""):
#     global USER_MESSAGE
#     USER_MESSAGE = message
# 
# def loop(io: IO, goals: Graph, db_name: str):
#     cmd = ""
#     while cmd != "q":
#         render_result = Renderer(goals, 100).build()
#         rows = render_result.graph
#         index = sorted(
#             [
#                 (goal_id, goal_vars["row"], goal_vars["col"])
#                 for goal_id, goal_vars in rows.items()
#                 if isinstance(goal_id, int)
#             ],
#             key=itemgetter(1, 2),
#             reverse=True,
#         )
#         id_width = len(str(max(g for g in rows.keys() if isinstance(g, int))))
#         for item in index:
#             goal_id = item[0]
#             goal_vars = rows[goal_id]
#             io.write(fmt(goal_id, goal_vars, id_width))
#         if USER_MESSAGE:
#             io.write(USER_MESSAGE)
#         update_message()
#         try:
#             cmd = io.read().strip()
#         except EOFError:
#             break
#         actions = build_actions(cmd)
#         if actions:
#             goals.accept_all(*actions)
#         save(goals, db_name)
# 
# Path: siebenapp/system.py
# def load(filename: str, message_fn: Callable[[str], None] = None) -> Enumeration:
#     zoom_data: ZoomData = []
#     autolink_data: AutoLinkData = []
#     if path.isfile(filename):
#         connection = sqlite3.connect(filename)
#         run_migrations(connection)
#         cur = connection.cursor()
#         names = list(cur.execute("select * from goals"))
#         edges = list(cur.execute("select parent, child, reltype from edges"))
#         settings = list(cur.execute("select * from settings"))
#         zoom_data = list(cur.execute("select * from zoom"))
#         autolink_data = list(cur.execute("select * from autolink"))
#         cur.close()
#         goals = Goals.build(names, edges, settings, message_fn)
#     else:
#         goals = Goals("Rename me", message_fn)
#     result = Enumeration(all_layers(goals, zoom_data, autolink_data))
#     result.verify()
#     return result


# Path: siebenapp/tests/test_cli.py
from typing import List
from approvaltests import verify, Options  # type: ignore
from approvaltests.reporters import GenericDiffReporterFactory  # type: ignore
from approvaltests.reporters.generic_diff_reporter import GenericDiffReporter  # type: ignore
from siebenapp.cli import IO, update_message, loop
from siebenapp.system import load

class DummyIO(IO):
    def __init__(self, commands: List[str], log: List[str]):
        super().__init__()
        self.commands = commands
        self.log = log

    def write(self, text: str, *args) -> None:
        self.log.append(" ".join([text, *args]))

    def read(self) -> str:
        if self.commands:
            command = self.commands.pop(0)
            self.log.append(f"> {command}\n")
            return command
        self.log.append("*USER BREAK*")
        raise EOFError("Commands list is empty")


def test_simple_scenario():
    log = []
    commands = [
        "r Approval testing",
        "a Measure coverage",
        "2",
        "a Build simple test",
        "3",
        "c",
    ]
    io = DummyIO(commands, log)
    db_name = ":memory:"

    goals = load(db_name, update_message)