Spaces:
Sleeping
Sleeping
Deploy Myco from CI
Browse files- tests/test_myco_core.py +86 -0
- ui/gradio_app.py +22 -0
- ui/renderers.py +23 -0
- ui/styles.py +2 -0
tests/test_myco_core.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Regression tests for Myco's core game loop and UI builders."""
|
| 2 |
+
|
| 3 |
+
import unittest
|
| 4 |
+
|
| 5 |
+
from game.catalog import load_mushrooms
|
| 6 |
+
from game.engine import (
|
| 7 |
+
collect_current,
|
| 8 |
+
companion_reply,
|
| 9 |
+
discover_mushroom,
|
| 10 |
+
eat_current,
|
| 11 |
+
study_current,
|
| 12 |
+
)
|
| 13 |
+
from game.progression import current_area, next_area
|
| 14 |
+
from ui.gradio_app import build_app
|
| 15 |
+
from ui.renderers import (
|
| 16 |
+
dex_markdown,
|
| 17 |
+
forest_scene,
|
| 18 |
+
game_status_markdown,
|
| 19 |
+
progress_markdown,
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class MycoCoreTests(unittest.TestCase):
|
| 24 |
+
def test_catalog_has_expected_size_and_rarities(self):
|
| 25 |
+
mushrooms = load_mushrooms()
|
| 26 |
+
rarities = {mushroom.rarity for mushroom in mushrooms}
|
| 27 |
+
|
| 28 |
+
self.assertGreaterEqual(len(mushrooms), 20)
|
| 29 |
+
self.assertIn("Common", rarities)
|
| 30 |
+
self.assertIn("Rare", rarities)
|
| 31 |
+
self.assertIn("Legendary", rarities)
|
| 32 |
+
|
| 33 |
+
def test_discovery_chat_and_scene_render(self):
|
| 34 |
+
mushroom, current, history = discover_mushroom([])
|
| 35 |
+
scene = forest_scene(current, [])
|
| 36 |
+
|
| 37 |
+
self.assertEqual(current["name"], mushroom.name)
|
| 38 |
+
self.assertEqual(history[0]["role"], "assistant")
|
| 39 |
+
self.assertIn("forest-scene", scene)
|
| 40 |
+
self.assertIn(mushroom.name, scene)
|
| 41 |
+
|
| 42 |
+
def test_companion_reply_adds_user_and_assistant_messages(self):
|
| 43 |
+
_mushroom, current, history = discover_mushroom([])
|
| 44 |
+
prompt, updated_history = companion_reply("What is this?", history, current)
|
| 45 |
+
|
| 46 |
+
self.assertEqual(prompt, "")
|
| 47 |
+
self.assertEqual(updated_history[-2]["role"], "user")
|
| 48 |
+
self.assertEqual(updated_history[-1]["role"], "assistant")
|
| 49 |
+
self.assertIn(current["name"], updated_history[-1]["content"])
|
| 50 |
+
|
| 51 |
+
def test_collect_current_prevents_duplicates(self):
|
| 52 |
+
_mushroom, current, history = discover_mushroom([])
|
| 53 |
+
collection, history = collect_current(current, [], history)
|
| 54 |
+
duplicate_collection, duplicate_history = collect_current(current, collection, history)
|
| 55 |
+
|
| 56 |
+
self.assertEqual(len(collection), 1)
|
| 57 |
+
self.assertEqual(duplicate_collection, collection)
|
| 58 |
+
self.assertIn("already", duplicate_history[-1]["content"])
|
| 59 |
+
|
| 60 |
+
def test_study_and_eat_are_safe(self):
|
| 61 |
+
_mushroom, current, history = discover_mushroom([])
|
| 62 |
+
studied, study_history = study_current(current, history)
|
| 63 |
+
eat_history = eat_current(studied, study_history)
|
| 64 |
+
|
| 65 |
+
self.assertEqual(studied["studied"], "Yes")
|
| 66 |
+
self.assertNotEqual(studied["magic"], "Unknown")
|
| 67 |
+
self.assertIn("blocks", eat_history[-1]["content"])
|
| 68 |
+
|
| 69 |
+
def test_progress_and_dex_markdown_handle_empty_and_collected_state(self):
|
| 70 |
+
_mushroom, current, _history = discover_mushroom([])
|
| 71 |
+
|
| 72 |
+
self.assertIn("MycoDex is empty", dex_markdown([]))
|
| 73 |
+
self.assertIn(current["name"], dex_markdown([current]))
|
| 74 |
+
self.assertIn("Forest Progress", progress_markdown([current]))
|
| 75 |
+
self.assertIn("Quest Log", game_status_markdown(current, [current]))
|
| 76 |
+
self.assertIn(current["name"], game_status_markdown(current, [current]))
|
| 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")
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
if __name__ == "__main__":
|
| 86 |
+
unittest.main()
|
ui/gradio_app.py
CHANGED
|
@@ -9,6 +9,18 @@ import gradio as gr
|
|
| 9 |
|
| 10 |
from game.engine import collect_current, discover_mushroom, eat_current, myco_reply, study_current
|
| 11 |
from game.state import welcome_history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
from __future__ import annotations
|
| 13 |
|
| 14 |
import gradio as gr
|
|
@@ -102,6 +114,7 @@ def build_app():
|
|
| 102 |
mushroom, current, history = discover_mushroom(collection)
|
| 103 |
return (
|
| 104 |
forest_scene(current, collection),
|
|
|
|
| 105 |
mushroom_card(mushroom),
|
| 106 |
current,
|
| 107 |
history,
|
|
@@ -117,6 +130,7 @@ def build_app():
|
|
| 117 |
return (
|
| 118 |
updated_collection,
|
| 119 |
forest_scene(current, updated_collection),
|
|
|
|
| 120 |
dex_markdown(updated_collection),
|
| 121 |
updated_history,
|
| 122 |
progress_markdown(updated_collection),
|
|
@@ -127,6 +141,7 @@ def build_app():
|
|
| 127 |
return (
|
| 128 |
updated_current,
|
| 129 |
forest_scene(updated_current, collection),
|
|
|
|
| 130 |
mushroom_card_from_state(updated_current),
|
| 131 |
updated_history,
|
| 132 |
)
|
|
@@ -160,6 +175,7 @@ def build_app() -> gr.Blocks:
|
|
| 160 |
|
| 161 |
with gr.Tab("🎮 Play"):
|
| 162 |
scene = gr.HTML(forest_scene(None, []))
|
|
|
|
| 163 |
|
| 164 |
with gr.Row():
|
| 165 |
with gr.Column(scale=2):
|
|
@@ -204,12 +220,15 @@ def build_app() -> gr.Blocks:
|
|
| 204 |
search_button.click(
|
| 205 |
search_forest,
|
| 206 |
inputs=[collection],
|
|
|
|
| 207 |
outputs=[scene, discovery, current_mushroom, chat, dex, progress],
|
| 208 |
)
|
| 209 |
ask_button.click(ask_companion, inputs=[prompt, chat, current_mushroom], outputs=[prompt, chat])
|
| 210 |
prompt.submit(ask_companion, inputs=[prompt, chat, current_mushroom], outputs=[prompt, chat])
|
| 211 |
collect_button.click(
|
| 212 |
collect_discovery,
|
|
|
|
|
|
|
| 213 |
explore_button.click(
|
| 214 |
begin_forest_search,
|
| 215 |
explore,
|
|
@@ -226,6 +245,9 @@ def build_app() -> gr.Blocks:
|
|
| 226 |
study_button.click(
|
| 227 |
study_discovery,
|
| 228 |
inputs=[current_mushroom, chat, collection],
|
|
|
|
|
|
|
|
|
|
| 229 |
outputs=[current_mushroom, scene, discovery, chat],
|
| 230 |
)
|
| 231 |
eat_button.click(block_unsafe_eating, inputs=[current_mushroom, chat], outputs=[chat])
|
|
|
|
| 9 |
|
| 10 |
from game.engine import collect_current, discover_mushroom, eat_current, myco_reply, study_current
|
| 11 |
from game.state import welcome_history
|
| 12 |
+
from models.mushroom import Mushroom
|
| 13 |
+
from ui.renderers import (
|
| 14 |
+
dex_markdown,
|
| 15 |
+
forest_scene,
|
| 16 |
+
game_status_markdown,
|
| 17 |
+
hero_markdown,
|
| 18 |
+
mushroom_card,
|
| 19 |
+
progress_markdown,
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def mushroom_card_from_state(current):
|
| 24 |
from __future__ import annotations
|
| 25 |
|
| 26 |
import gradio as gr
|
|
|
|
| 114 |
mushroom, current, history = discover_mushroom(collection)
|
| 115 |
return (
|
| 116 |
forest_scene(current, collection),
|
| 117 |
+
game_status_markdown(current, collection),
|
| 118 |
mushroom_card(mushroom),
|
| 119 |
current,
|
| 120 |
history,
|
|
|
|
| 130 |
return (
|
| 131 |
updated_collection,
|
| 132 |
forest_scene(current, updated_collection),
|
| 133 |
+
game_status_markdown(current, updated_collection),
|
| 134 |
dex_markdown(updated_collection),
|
| 135 |
updated_history,
|
| 136 |
progress_markdown(updated_collection),
|
|
|
|
| 141 |
return (
|
| 142 |
updated_current,
|
| 143 |
forest_scene(updated_current, collection),
|
| 144 |
+
game_status_markdown(updated_current, collection),
|
| 145 |
mushroom_card_from_state(updated_current),
|
| 146 |
updated_history,
|
| 147 |
)
|
|
|
|
| 175 |
|
| 176 |
with gr.Tab("🎮 Play"):
|
| 177 |
scene = gr.HTML(forest_scene(None, []))
|
| 178 |
+
status = gr.Markdown(game_status_markdown(None, []), elem_id="quest-log")
|
| 179 |
|
| 180 |
with gr.Row():
|
| 181 |
with gr.Column(scale=2):
|
|
|
|
| 220 |
search_button.click(
|
| 221 |
search_forest,
|
| 222 |
inputs=[collection],
|
| 223 |
+
outputs=[scene, status, discovery, current_mushroom, chat, dex, progress],
|
| 224 |
outputs=[scene, discovery, current_mushroom, chat, dex, progress],
|
| 225 |
)
|
| 226 |
ask_button.click(ask_companion, inputs=[prompt, chat, current_mushroom], outputs=[prompt, chat])
|
| 227 |
prompt.submit(ask_companion, inputs=[prompt, chat, current_mushroom], outputs=[prompt, chat])
|
| 228 |
collect_button.click(
|
| 229 |
collect_discovery,
|
| 230 |
+
inputs=[current_mushroom, collection, chat],
|
| 231 |
+
outputs=[collection, scene, status, dex, chat, progress],
|
| 232 |
explore_button.click(
|
| 233 |
begin_forest_search,
|
| 234 |
explore,
|
|
|
|
| 245 |
study_button.click(
|
| 246 |
study_discovery,
|
| 247 |
inputs=[current_mushroom, chat, collection],
|
| 248 |
+
outputs=[current_mushroom, scene, status, discovery, chat],
|
| 249 |
+
)
|
| 250 |
+
eat_button.click(block_unsafe_eating, inputs=[current_mushroom, chat], outputs=[chat])
|
| 251 |
outputs=[current_mushroom, scene, discovery, chat],
|
| 252 |
)
|
| 253 |
eat_button.click(block_unsafe_eating, inputs=[current_mushroom, chat], outputs=[chat])
|
ui/renderers.py
CHANGED
|
@@ -74,6 +74,29 @@ def _mushroom_sprite(rarity: str) -> str:
|
|
| 74 |
return "🍄"
|
| 75 |
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
def mushroom_card(mushroom: Mushroom | None) -> str:
|
| 78 |
"""Render the current mushroom discovery as markdown."""
|
| 79 |
if mushroom is None:
|
|
|
|
| 74 |
return "🍄"
|
| 75 |
|
| 76 |
|
| 77 |
+
def game_status_markdown(current: MushroomState | None, collection: CollectionState | None) -> str:
|
| 78 |
+
"""Render the current objective like a compact game quest log."""
|
| 79 |
+
count = len(_safe_collection(collection))
|
| 80 |
+
if current is None:
|
| 81 |
+
objective = "Click **Start Exploring** to discover your first mushroom."
|
| 82 |
+
discovery = "No active discovery yet."
|
| 83 |
+
else:
|
| 84 |
+
name = current.get("name", "Mystery mushroom")
|
| 85 |
+
studied = current.get("studied") == "Yes"
|
| 86 |
+
next_step = "Collect it in the MycoDex" if studied else "Ask Myco, then Study or Collect"
|
| 87 |
+
objective = f"Current objective: **{next_step}**."
|
| 88 |
+
discovery = f"Active discovery: **{name}** · Rarity: **{current.get('rarity', 'Unknown')}**"
|
| 89 |
+
|
| 90 |
+
return "\n".join(
|
| 91 |
+
(
|
| 92 |
+
"### 🎮 Quest Log",
|
| 93 |
+
f"**MycoDex:** {count} discoveries",
|
| 94 |
+
discovery,
|
| 95 |
+
objective,
|
| 96 |
+
)
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
|
| 100 |
def mushroom_card(mushroom: Mushroom | None) -> str:
|
| 101 |
"""Render the current mushroom discovery as markdown."""
|
| 102 |
if mushroom is None:
|
ui/styles.py
CHANGED
|
@@ -154,6 +154,8 @@ APP_CSS = """
|
|
| 154 |
}
|
| 155 |
#discovery-card,
|
| 156 |
#mycodex,
|
|
|
|
|
|
|
| 157 |
#progress-panel {
|
| 158 |
#discovery-card,
|
| 159 |
#mycodex,
|
|
|
|
| 154 |
}
|
| 155 |
#discovery-card,
|
| 156 |
#mycodex,
|
| 157 |
+
#progress-panel,
|
| 158 |
+
#quest-log {
|
| 159 |
#progress-panel {
|
| 160 |
#discovery-card,
|
| 161 |
#mycodex,
|