Spaces:
Sleeping
Sleeping
Deploy Myco from CI
Browse files- tests/test_myco_core.py +30 -0
- ui/gradio_app.py +31 -0
tests/test_myco_core.py
CHANGED
|
@@ -1,5 +1,8 @@
|
|
| 1 |
"""Regression tests for Myco's core game loop and UI builders."""
|
| 2 |
|
|
|
|
|
|
|
|
|
|
| 3 |
import unittest
|
| 4 |
|
| 5 |
from game.catalog import load_mushrooms
|
|
@@ -77,6 +80,33 @@ class MycoCoreTests(unittest.TestCase):
|
|
| 77 |
self.assertEqual(current_area(0).name, "Mossy Trail")
|
| 78 |
self.assertIsNotNone(next_area(0))
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
def test_gradio_app_builds(self):
|
| 81 |
demo = build_app()
|
| 82 |
self.assertEqual(type(demo).__name__, "Blocks")
|
|
|
|
| 1 |
"""Regression tests for Myco's core game loop and UI builders."""
|
| 2 |
|
| 3 |
+
import ast
|
| 4 |
+
import unittest
|
| 5 |
+
from pathlib import Path
|
| 6 |
import unittest
|
| 7 |
|
| 8 |
from game.catalog import load_mushrooms
|
|
|
|
| 80 |
self.assertEqual(current_area(0).name, "Mossy Trail")
|
| 81 |
self.assertIsNotNone(next_area(0))
|
| 82 |
|
| 83 |
+
def test_python_files_have_no_duplicate_functions_or_future_imports(self):
|
| 84 |
+
future_import = "from __future__ import " + "annotations"
|
| 85 |
+
for path in Path(".").glob("**/*.py"):
|
| 86 |
+
if "__pycache__" in path.parts or ".git" in path.parts:
|
| 87 |
+
continue
|
| 88 |
+
source = path.read_text()
|
| 89 |
+
tree = ast.parse(source)
|
| 90 |
+
function_lines = {}
|
| 91 |
+
for node in ast.walk(tree):
|
| 92 |
+
if isinstance(node, ast.FunctionDef):
|
| 93 |
+
function_lines.setdefault(node.name, []).append(node.lineno)
|
| 94 |
+
|
| 95 |
+
duplicates = {name: lines for name, lines in function_lines.items() if len(lines) > 1}
|
| 96 |
+
self.assertEqual(duplicates, {}, f"duplicate functions in {path}")
|
| 97 |
+
self.assertNotIn(future_import, source, f"future import in {path}")
|
| 98 |
+
def test_gradio_app_file_has_no_duplicate_functions_or_future_import(self):
|
| 99 |
+
source = Path("ui/gradio_app.py").read_text()
|
| 100 |
+
tree = ast.parse(source)
|
| 101 |
+
function_lines = {}
|
| 102 |
+
for node in ast.walk(tree):
|
| 103 |
+
if isinstance(node, ast.FunctionDef):
|
| 104 |
+
function_lines.setdefault(node.name, []).append(node.lineno)
|
| 105 |
+
|
| 106 |
+
duplicates = {name: lines for name, lines in function_lines.items() if len(lines) > 1}
|
| 107 |
+
self.assertEqual(duplicates, {})
|
| 108 |
+
self.assertNotIn("from __future__ import annotations", source)
|
| 109 |
+
|
| 110 |
def test_gradio_app_builds(self):
|
| 111 |
demo = build_app()
|
| 112 |
self.assertEqual(type(demo).__name__, "Blocks")
|
ui/gradio_app.py
CHANGED
|
@@ -21,6 +21,18 @@ from ui.renderers import (
|
|
| 21 |
|
| 22 |
|
| 23 |
def mushroom_card_from_state(current):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
from __future__ import annotations
|
| 25 |
|
| 26 |
import gradio as gr
|
|
@@ -47,6 +59,13 @@ def explore(collection: CollectionState) -> tuple[str, str, MushroomState, list[
|
|
| 47 |
)
|
| 48 |
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
def ask_myco(message, history, current):
|
| 51 |
def ask_myco(
|
| 52 |
message: str,
|
|
@@ -68,12 +87,15 @@ def add_to_dex(
|
|
| 68 |
return (
|
| 69 |
updated_collection,
|
| 70 |
forest_scene(current, updated_collection),
|
|
|
|
| 71 |
dex_markdown(updated_collection),
|
| 72 |
updated_history,
|
| 73 |
progress_markdown(updated_collection),
|
| 74 |
)
|
| 75 |
|
| 76 |
|
|
|
|
|
|
|
| 77 |
def study_mushroom(current, history, collection):
|
| 78 |
def study_mushroom(
|
| 79 |
current: MushroomState | None,
|
|
@@ -85,11 +107,20 @@ def study_mushroom(
|
|
| 85 |
return (
|
| 86 |
updated_current,
|
| 87 |
forest_scene(updated_current, collection),
|
|
|
|
| 88 |
mushroom_card_from_state(updated_current),
|
| 89 |
updated_history,
|
| 90 |
)
|
| 91 |
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
def eat_mushroom(current, history):
|
| 94 |
def eat_mushroom(
|
| 95 |
current: MushroomState | None,
|
|
|
|
| 21 |
|
| 22 |
|
| 23 |
def mushroom_card_from_state(current):
|
| 24 |
+
"""Render a mushroom card from Gradio state."""
|
| 25 |
+
if current is None:
|
| 26 |
+
return mushroom_card(None)
|
| 27 |
+
return mushroom_card(Mushroom.from_dict(current))
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def search_forest(collection):
|
| 31 |
+
"""Explore the forest and refresh every visible gameplay panel."""
|
| 32 |
+
mushroom, current, history = discover_mushroom(collection)
|
| 33 |
+
return (
|
| 34 |
+
forest_scene(current, collection),
|
| 35 |
+
game_status_markdown(current, collection),
|
| 36 |
from __future__ import annotations
|
| 37 |
|
| 38 |
import gradio as gr
|
|
|
|
| 59 |
)
|
| 60 |
|
| 61 |
|
| 62 |
+
def ask_companion(message, history, current):
|
| 63 |
+
"""Ask Myco about the active discovery."""
|
| 64 |
+
return myco_reply(message, history, current)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def collect_discovery(current, collection, history):
|
| 68 |
+
"""Collect the active mushroom and refresh collection-dependent panels."""
|
| 69 |
def ask_myco(message, history, current):
|
| 70 |
def ask_myco(
|
| 71 |
message: str,
|
|
|
|
| 87 |
return (
|
| 88 |
updated_collection,
|
| 89 |
forest_scene(current, updated_collection),
|
| 90 |
+
game_status_markdown(current, updated_collection),
|
| 91 |
dex_markdown(updated_collection),
|
| 92 |
updated_history,
|
| 93 |
progress_markdown(updated_collection),
|
| 94 |
)
|
| 95 |
|
| 96 |
|
| 97 |
+
def study_discovery(current, history, collection):
|
| 98 |
+
"""Study the active mushroom and refresh the discovery card."""
|
| 99 |
def study_mushroom(current, history, collection):
|
| 100 |
def study_mushroom(
|
| 101 |
current: MushroomState | None,
|
|
|
|
| 107 |
return (
|
| 108 |
updated_current,
|
| 109 |
forest_scene(updated_current, collection),
|
| 110 |
+
game_status_markdown(updated_current, collection),
|
| 111 |
mushroom_card_from_state(updated_current),
|
| 112 |
updated_history,
|
| 113 |
)
|
| 114 |
|
| 115 |
|
| 116 |
+
def block_unsafe_eating(current, history):
|
| 117 |
+
"""Let Myco prevent unsafe eating while preserving chat state."""
|
| 118 |
+
return eat_current(current, history)
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
def build_app():
|
| 122 |
+
"""Build the Gradio interface for Hugging Face Spaces."""
|
| 123 |
+
|
| 124 |
def eat_mushroom(current, history):
|
| 125 |
def eat_mushroom(
|
| 126 |
current: MushroomState | None,
|