Spaces:
Runtime error
Runtime error
File size: 6,167 Bytes
60e41bb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | from datetime import date, time
from fastapi.testclient import TestClient
from PIL import Image
from waterleaf.application import WaterleafApplication
from waterleaf.models import CareProfile, LocationMatch, TaxonCandidate, WeatherDay
from waterleaf.storage import GardenStore
from waterleaf.web import create_web_app
class FakeCare:
def get_care(self, scientific_name):
return CareProfile(
scientific_name=scientific_name,
common_name="English lavender",
min_days=7,
max_days=9,
watering_label="Minimum",
sunlight=["full sun"],
)
class FakeWeather:
def geocode(self, query):
assert query == "Stockholm"
return LocationMatch(
display_name="Stockholm, Sweden",
latitude=59.33,
longitude=18.07,
timezone="Europe/Stockholm",
)
def forecast(self, latitude, longitude):
return [
WeatherDay(
date=date(2026, 6, 16),
precipitation_mm=7.0,
max_temperature_c=20.0,
et0_mm=2.0,
)
]
def test_application_saves_plan_and_exports_whole_garden(tmp_path):
source = tmp_path / "lavender.png"
Image.new("RGB", (300, 500), color=(90, 120, 60)).save(source)
store = GardenStore(tmp_path / "waterleaf.sqlite3")
application = WaterleafApplication(
store=store,
media_directory=tmp_path / "media",
export_directory=tmp_path / "exports",
public_base_url="https://waterleaf.example",
care=FakeCare(),
weather=FakeWeather(),
)
candidate = TaxonCandidate(
taxon_key="2925518",
scientific_name="Lavandula angustifolia",
common_name="English lavender",
confidence=0.91,
rationale="Purple spikes and narrow leaves",
)
plan = application.preview_schedule(
candidate=candidate,
location_query="Stockholm",
is_container=True,
size_label="medium",
start=date(2026, 6, 8),
)
saved = application.save_plant(
owner="alice",
nickname="Patio lavender",
candidate=candidate,
source_image=source,
preferred_time=time(7, 30),
plan=plan,
)
exported = application.export_garden("alice", generated_at="20260608T100000Z")
assert saved.image_id
assert store.get_schedule("alice", saved.id)
content = exported.read_text()
unfolded = content.replace("\n ", "")
assert "SUMMARY:Water Patio lavender" in content
assert f"https://waterleaf.example/plants/{saved.public_slug}" in content
assert f"https://waterleaf.example/media/{saved.image_id}.jpg" in unfolded
def test_public_profile_and_media_routes_exclude_private_location(tmp_path):
source = tmp_path / "lavender.png"
Image.new("RGB", (100, 100), color=(90, 120, 60)).save(source)
store = GardenStore(tmp_path / "waterleaf.sqlite3")
application = WaterleafApplication(
store=store,
media_directory=tmp_path / "media",
export_directory=tmp_path / "exports",
public_base_url="https://waterleaf.example",
care=FakeCare(),
weather=FakeWeather(),
)
candidate = TaxonCandidate(
taxon_key="2925518",
scientific_name="Lavandula angustifolia",
common_name="English lavender",
)
plan = application.preview_schedule(
candidate=candidate,
location_query="Stockholm",
is_container=True,
size_label="medium",
start=date(2026, 6, 8),
)
saved = application.save_plant(
owner="alice",
nickname="Patio lavender",
candidate=candidate,
source_image=source,
preferred_time=time(7, 30),
plan=plan,
)
client = TestClient(create_web_app(application, mount_ui=False))
health = client.get("/health")
profile = client.get(f"/plants/{saved.public_slug}")
media = client.get(f"/media/{saved.image_id}.jpg")
assert health.json() == {"status": "ok"}
assert profile.status_code == 200
assert "Patio lavender" in profile.text
assert "Lavandula angustifolia" in profile.text
assert "7-9 days" in profile.text
assert "2026-06-" in profile.text
assert "Stockholm" not in profile.text
assert "alice" not in profile.text
assert media.status_code == 200
assert media.headers["content-type"] == "image/jpeg"
assert client.get("/media/../../private.jpg").status_code == 404
def test_application_delete_removes_unreferenced_media(tmp_path):
source = tmp_path / "lavender.png"
Image.new("RGB", (100, 100), color=(90, 120, 60)).save(source)
store = GardenStore(tmp_path / "waterleaf.sqlite3")
application = WaterleafApplication(
store=store,
media_directory=tmp_path / "media",
export_directory=tmp_path / "exports",
public_base_url="https://waterleaf.example",
care=FakeCare(),
weather=FakeWeather(),
)
candidate = TaxonCandidate(
taxon_key="2925518",
scientific_name="Lavandula angustifolia",
common_name="English lavender",
)
plan = application.preview_schedule(
candidate=candidate,
location_query="Stockholm",
is_container=True,
size_label="medium",
start=date(2026, 6, 8),
)
first = application.save_plant(
owner="alice",
nickname="Patio lavender",
candidate=candidate,
source_image=source,
preferred_time=time(7, 30),
plan=plan,
)
second = application.save_plant(
owner="alice",
nickname="Second lavender",
candidate=candidate,
source_image=source,
preferred_time=time(7, 30),
plan=plan,
)
media_path = application.media_directory / f"{first.image_id}.jpg"
assert first.image_id == second.image_id
assert media_path.exists()
assert application.delete_plant("alice", first.id) is True
assert media_path.exists()
assert application.delete_plant("alice", second.id) is True
assert not media_path.exists()
|