# Path: siebenapp/domain.py
# class Select(Command):
#     """Select a goal by its id whether it exist. Do nothing in other case"""
# 
#     goal_id: int
# 
# class ToggleClose(Command):
#     """Close an open selected goal. Re-open a closed selected goal"""
# 
#     root: int = 0
# 
# Path: siebenapp/progress_view.py
# class ProgressView(Graph):
#     def __init__(self, goaltree: Graph):
#         super().__init__(goaltree)
#         self.show_progress = False
# 
#     def accept_ToggleProgress(self, command: ToggleProgress) -> None:
#         self.show_progress = not self.show_progress
# 
#     def settings(self, key: str) -> Any:
#         if key == "filter_progress":
#             return self.show_progress
#         return self.goaltree.settings(key)
# 
#     def reconfigure_from(self, origin: "Graph") -> None:
#         super().reconfigure_from(origin)
#         self.show_progress = origin.settings("filter_progress")
# 
#     @with_key("name")
#     @with_key("open")
#     @with_key("edge")
#     def q(self, keys: str = "name") -> Dict[int, Any]:
#         if not self.show_progress:
#             return self.goaltree.q(keys)
#         progress_cache: Dict[int, Tuple[int, int]] = {}
#         result = self.goaltree.q(keys)
#         queue: List[int] = list(result.keys())
#         while queue:
#             goal_id = queue.pop(0)
#             children = [
#                 x[0] for x in result[goal_id]["edge"] if x[1] == EdgeType.PARENT
#             ]
#             open_count = 0 if result[goal_id]["open"] else 1
#             if not children:
#                 progress_cache[goal_id] = (open_count, 1)
#             elif all(g in progress_cache for g in children):
#                 progress_cache[goal_id] = (
#                     sum(progress_cache[x][0] for x in children) + open_count,
#                     sum(progress_cache[x][1] for x in children) + 1,
#                 )
#             else:
#                 queue.append(goal_id)
# 
#         for goal_id, attr in result.items():
#             progress = progress_cache[goal_id]
#             old_name = attr["name"]
#             attr["name"] = f"[{progress[0]}/{progress[1]}] {old_name}"
# 
#         return result
# 
# class ToggleProgress(Command):
#     pass
# 
# Path: siebenapp/tests/dsl.py
# class GoalPrototype:
# def _build_goal_prototype(goal_id, name, is_open, children, blockers, select):
# def open_(goal_id, name, children=None, blockers=None, select=None):
# def clos_(goal_id, name, children=None, blockers=None, select=None):
# def build_goaltree(*goal_prototypes, message_fn=None):


from _pytest.fixtures import fixture
from siebenapp.domain import Select, ToggleClose
from siebenapp.progress_view import ProgressView, ToggleProgress
from siebenapp.tests.dsl import build_goaltree, selected, open_
def test_no_progress_by_default(goaltree):
    assert goaltree.q("name") == {
        1: {"name": "Root"},
        2: {"name": "With blocker"},
        3: {"name": "With subgoal"},
        4: {"name": "Top goal"},
    }


def test_show_progress(goaltree):
    goaltree.accept(ToggleProgress())
    assert goaltree.q("name") == {
        1: {"name": "[0/4] Root"},
        2: {"name": "[0/1] With blocker"},
        3: {"name": "[0/2] With subgoal"},
        4: {"name": "[0/1] Top goal"},
    }


def test_toggle_hide_progress(goaltree):
    goaltree.accept_all(ToggleProgress(), ToggleProgress())
    assert goaltree.q("name") == {
        1: {"name": "Root"},
        2: {"name": "With blocker"},
        3: {"name": "With subgoal"},
        4: {"name": "Top goal"},
    }


def test_change_progress_on_close(goaltree):

# Path: siebenapp/domain.py
# class Select(Command):
#     """Select a goal by its id whether it exist. Do nothing in other case"""
# 
#     goal_id: int
# 
# class ToggleClose(Command):
#     """Close an open selected goal. Re-open a closed selected goal"""
# 
#     root: int = 0
# 
# Path: siebenapp/progress_view.py
# class ProgressView(Graph):
#     def __init__(self, goaltree: Graph):
#         super().__init__(goaltree)
#         self.show_progress = False
# 
#     def accept_ToggleProgress(self, command: ToggleProgress) -> None:
#         self.show_progress = not self.show_progress
# 
#     def settings(self, key: str) -> Any:
#         if key == "filter_progress":
#             return self.show_progress
#         return self.goaltree.settings(key)
# 
#     def reconfigure_from(self, origin: "Graph") -> None:
#         super().reconfigure_from(origin)
#         self.show_progress = origin.settings("filter_progress")
# 
#     @with_key("name")
#     @with_key("open")
#     @with_key("edge")
#     def q(self, keys: str = "name") -> Dict[int, Any]:
#         if not self.show_progress:
#             return self.goaltree.q(keys)
#         progress_cache: Dict[int, Tuple[int, int]] = {}
#         result = self.goaltree.q(keys)
#         queue: List[int] = list(result.keys())
#         while queue:
#             goal_id = queue.pop(0)
#             children = [
#                 x[0] for x in result[goal_id]["edge"] if x[1] == EdgeType.PARENT
#             ]
#             open_count = 0 if result[goal_id]["open"] else 1
#             if not children:
#                 progress_cache[goal_id] = (open_count, 1)
#             elif all(g in progress_cache for g in children):
#                 progress_cache[goal_id] = (
#                     sum(progress_cache[x][0] for x in children) + open_count,
#                     sum(progress_cache[x][1] for x in children) + 1,
#                 )
#             else:
#                 queue.append(goal_id)
# 
#         for goal_id, attr in result.items():
#             progress = progress_cache[goal_id]
#             old_name = attr["name"]
#             attr["name"] = f"[{progress[0]}/{progress[1]}] {old_name}"
# 
#         return result
# 
# class ToggleProgress(Command):
#     pass
# 
# Path: siebenapp/tests/dsl.py
# class GoalPrototype:
# def _build_goal_prototype(goal_id, name, is_open, children, blockers, select):
# def open_(goal_id, name, children=None, blockers=None, select=None):
# def clos_(goal_id, name, children=None, blockers=None, select=None):
# def build_goaltree(*goal_prototypes, message_fn=None):


# Path: siebenapp/tests/test_progress_view.py
from _pytest.fixtures import fixture
from siebenapp.domain import Select, ToggleClose
from siebenapp.progress_view import ProgressView, ToggleProgress
from siebenapp.tests.dsl import build_goaltree, selected, open_

def test_no_progress_by_default(goaltree):
    assert goaltree.q("name") == {
        1: {"name": "Root"},
        2: {"name": "With blocker"},
        3: {"name": "With subgoal"},
        4: {"name": "Top goal"},
    }


def test_show_progress(goaltree):
    goaltree.accept(ToggleProgress())
    assert goaltree.q("name") == {
        1: {"name": "[0/4] Root"},
        2: {"name": "[0/1] With blocker"},
        3: {"name": "[0/2] With subgoal"},
        4: {"name": "[0/1] Top goal"},
    }


def test_toggle_hide_progress(goaltree):
    goaltree.accept_all(ToggleProgress(), ToggleProgress())
    assert goaltree.q("name") == {
        1: {"name": "Root"},
        2: {"name": "With blocker"},
        3: {"name": "With subgoal"},
        4: {"name": "Top goal"},
    }


def test_change_progress_on_close(goaltree):

    goaltree.accept_all(ToggleProgress(), Select(4), ToggleClose())