fix: return FT components directly for proper toast injection

POST routes were returning HTMLResponse(content=to_xml(...)) which
bypassed FastHTML's toast middleware. The middleware only injects
toasts for tuple, FT, or FtResponse responses.

Changed 12 routes to return render_page() directly:
- actions.py: 7 routes (cohort, hatch, tag-add, tag-end, attrs, outcome, status-correct)
- eggs.py: 2 routes (product-collected, product-sold)
- feed.py: 2 routes (feed-given, feed-purchased)
- move.py: 1 route (animal-move)

Updated tests to check for toast content in response body instead of
session cookie, since middleware now renders toasts inline.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-02 20:11:05 +00:00
parent 628d5cc6e6
commit 9fbda655f5
6 changed files with 120 additions and 224 deletions

View File

@@ -149,7 +149,7 @@ class TestCohortCreationSuccess:
assert count_after == count_before + 3
def test_cohort_success_returns_toast(self, client, seeded_db, location_strip1_id):
"""Successful cohort creation stores toast in session."""
"""Successful cohort creation renders toast in response body."""
resp = client.post(
"/actions/animal-cohort",
data={
@@ -164,20 +164,8 @@ class TestCohortCreationSuccess:
)
assert resp.status_code == 200
# Toast is stored in session cookie (FastHTML's add_toast mechanism)
# The session cookie contains base64-encoded toast data with "toasts" key
assert "set-cookie" in resp.headers
session_cookie = resp.headers["set-cookie"]
assert "session_=" in session_cookie
# Base64 decode contains toast message (eyJ0b2FzdHMi... = {"toasts"...)
import base64
# Extract base64 portion from cookie value
cookie_value = session_cookie.split("session_=")[1].split(";")[0]
# FastHTML uses itsdangerous, so format is base64.timestamp.signature
base64_data = cookie_value.split(".")[0]
decoded = base64.b64decode(base64_data).decode()
assert "Created 2 duck" in decoded
# Toast is injected into response body by FastHTML's toast middleware
assert "Created 2 duck" in resp.text
class TestCohortCreationValidation:
@@ -375,7 +363,7 @@ class TestHatchRecordingSuccess:
assert count_at_nursery >= 3
def test_hatch_success_returns_toast(self, client, seeded_db, location_strip1_id):
"""Successful hatch recording stores toast in session."""
"""Successful hatch recording renders toast in response body."""
resp = client.post(
"/actions/hatch-recorded",
data={
@@ -387,16 +375,8 @@ class TestHatchRecordingSuccess:
)
assert resp.status_code == 200
# Toast is stored in session cookie (FastHTML's add_toast mechanism)
assert "set-cookie" in resp.headers
session_cookie = resp.headers["set-cookie"]
assert "session_=" in session_cookie
import base64
cookie_value = session_cookie.split("session_=")[1].split(";")[0]
base64_data = cookie_value.split(".")[0]
decoded = base64.b64decode(base64_data).decode()
assert "Recorded 2 hatchling" in decoded
# Toast is injected into response body by FastHTML's toast middleware
assert "Recorded 2 hatchling" in resp.text
class TestHatchRecordingValidation:
@@ -729,8 +709,7 @@ class TestTagAddSuccess:
assert tag_count >= len(animals_for_tagging)
def test_tag_add_success_returns_toast(self, client, seeded_db, animals_for_tagging):
"""Successful tag add stores toast in session."""
import base64
"""Successful tag add renders toast in response body."""
import time
from animaltrack.selection import compute_roster_hash
@@ -751,14 +730,8 @@ class TestTagAddSuccess:
)
assert resp.status_code == 200
# Toast is stored in session cookie
assert "set-cookie" in resp.headers
session_cookie = resp.headers["set-cookie"]
assert "session_=" in session_cookie
cookie_value = session_cookie.split("session_=")[1].split(";")[0]
base64_data = cookie_value.split(".")[0]
decoded = base64.b64decode(base64_data).decode()
assert "Tagged" in decoded and "test-tag-toast" in decoded
# Toast is injected into response body by FastHTML's toast middleware
assert "Tagged" in resp.text and "test-tag-toast" in resp.text
class TestTagAddValidation:
@@ -925,8 +898,7 @@ class TestTagEndSuccess:
assert open_after == 0
def test_tag_end_success_returns_toast(self, client, seeded_db, tagged_animals):
"""Successful tag end stores toast in session."""
import base64
"""Successful tag end renders toast in response body."""
import time
from animaltrack.selection import compute_roster_hash
@@ -947,14 +919,8 @@ class TestTagEndSuccess:
)
assert resp.status_code == 200
# Toast is stored in session cookie
assert "set-cookie" in resp.headers
session_cookie = resp.headers["set-cookie"]
assert "session_=" in session_cookie
cookie_value = session_cookie.split("session_=")[1].split(";")[0]
base64_data = cookie_value.split(".")[0]
decoded = base64.b64decode(base64_data).decode()
assert "Ended tag" in decoded and "test-end-tag" in decoded
# Toast is injected into response body by FastHTML's toast middleware
assert "Ended tag" in resp.text and "test-end-tag" in resp.text
class TestTagEndValidation:
@@ -1103,8 +1069,7 @@ class TestAttrsSuccess:
assert adult_count == len(animals_for_tagging)
def test_attrs_success_returns_toast(self, client, seeded_db, animals_for_tagging):
"""Successful attrs update stores toast in session."""
import base64
"""Successful attrs update renders toast in response body."""
import time
from animaltrack.selection import compute_roster_hash
@@ -1125,14 +1090,8 @@ class TestAttrsSuccess:
)
assert resp.status_code == 200
# Toast is stored in session cookie
assert "set-cookie" in resp.headers
session_cookie = resp.headers["set-cookie"]
assert "session_=" in session_cookie
cookie_value = session_cookie.split("session_=")[1].split(";")[0]
base64_data = cookie_value.split(".")[0]
decoded = base64.b64decode(base64_data).decode()
assert "Updated attributes" in decoded
# Toast is injected into response body by FastHTML's toast middleware
assert "Updated attributes" in resp.text
class TestAttrsValidation:
@@ -1280,8 +1239,7 @@ class TestOutcomeSuccess:
assert harvested_count == len(animals_for_tagging)
def test_outcome_success_returns_toast(self, client, seeded_db, animals_for_tagging):
"""Successful outcome recording stores toast in session."""
import base64
"""Successful outcome recording renders toast in response body."""
import time
from animaltrack.selection import compute_roster_hash
@@ -1302,14 +1260,8 @@ class TestOutcomeSuccess:
)
assert resp.status_code == 200
# Toast is stored in session cookie
assert "set-cookie" in resp.headers
session_cookie = resp.headers["set-cookie"]
assert "session_=" in session_cookie
cookie_value = session_cookie.split("session_=")[1].split(";")[0]
base64_data = cookie_value.split(".")[0]
decoded = base64.b64decode(base64_data).decode()
assert "Recorded sold" in decoded
# Toast is injected into response body by FastHTML's toast middleware
assert "Recorded sold" in resp.text
class TestOutcomeValidation:

View File

@@ -198,7 +198,7 @@ class TestMoveAnimalSuccess:
location_strip2_id,
ducks_at_strip1,
):
"""Successful move returns session cookie with toast."""
"""Successful move renders toast in response body."""
ts_utc = int(time.time() * 1000)
filter_str = 'location:"Strip 1"'
filter_ast = parse_filter(filter_str)
@@ -219,16 +219,8 @@ class TestMoveAnimalSuccess:
)
assert resp.status_code == 200
assert "set-cookie" in resp.headers
session_cookie = resp.headers["set-cookie"]
assert "session_=" in session_cookie
# Base64 decode contains toast message
import base64
cookie_value = session_cookie.split("session_=")[1].split(";")[0]
base64_data = cookie_value.split(".")[0]
decoded = base64.b64decode(base64_data).decode()
assert "Moved 5 animals to Strip 2" in decoded
# Toast is injected into response body by FastHTML's toast middleware
assert "Moved 5 animals to Strip 2" in resp.text
def test_move_success_resets_form(
self,