Files
devplacepy/tests/e2e/game/farm.py
T
retoorandClaude Sonnet 5 3c69de9d55 Add Happy 404, featured/related sidebars, and next-post nav to the post page
Happy 404: an HTML 404 (unmatched route, or an explicit not-found inside a
real route) now renders a random existing post instead of the error page,
using the exact same context builder as a real post view. Toggle is the
happy_404_enabled site setting (default on, /admin/settings); JSON/API
requests and a handful of excluded prefixes are never affected. The pool of
candidate slugs is cached in-process and resampled periodically so it stays
fast and eventually cycles the whole posts table; on any internal failure it
falls straight through to the real 404 page.

Applying this everywhere surfaced ~60 existing tests that asserted a literal
404 for a legitimate resource-not-found flow (deleted post, unknown
container, wrong project slug, etc.) - each now disables the setting for the
duration of that specific check and restores it after, so the underlying
not-found behavior stays covered independently of the new feature.

Post page also gained, all built on the same shared post_page_context() so
they render identically on both a real post and a happy-404 page:
- A left sidebar (three separate cards, matching /feed's sidebar-card
  convention) for "Gists from {author}", "Projects from {author}" (private
  projects filtered through the normal visibility check), and "Related
  Discussions" - each cached per author and invalidated on create/edit/
  delete so new content shows up immediately.
- A right column reusing /feed's exact Daily Topic widget class for up to
  three "Featured" articles (the existing but previously-unused `featured`
  news flag), cached as a pool with per-request random sampling.
- A "Next post -> " link beside "Back to Feed", pointing at the next older
  post site-wide (blocked authors skipped). Wired through the same next_url
  mechanism already used for listing pagination, so it emits a real
  backend-rendered <link rel="next"> tag for SEO, not just a visible link.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BWJy6PrMMt5hwWxQwia2rd
2026-09-08 03:44:30 +02:00

188 lines
6.9 KiB
Python

# retoor <retoor@molodetz.nl>
import time
from datetime import datetime, timedelta, timezone
from playwright.sync_api import expect
from tests.conftest import BASE_URL, CACHE_VERSION_PROPAGATION_SECONDS
from tests.e2e.game.index import reset_farm, warp_ready
from devplacepy.database import set_setting
def _plant_growing(username, crop="python", slot=0):
from devplacepy.database import get_table
from devplacepy.services.game import store
user = get_table("users").find_one(username=username)
store.plant({"uid": user["uid"], "username": username}, slot, crop)
def _warp_stealable(username):
from devplacepy.database import get_table
from devplacepy.services.game import economy, store
user = get_table("users").find_one(username=username)
farm = store.get_farm(user["uid"])
past = (
datetime.now(timezone.utc)
- timedelta(seconds=economy.STEAL_GRACE_SECONDS + 10)
).isoformat()
for plot in store.get_plots(farm["uid"]):
if plot.get("crop_key"):
get_table("game_plots").update(
{"uid": plot["uid"], "ready_at": past}, ["uid"]
)
def _farm_coins(username):
from devplacepy.database import get_table
from devplacepy.services.game import store
user = get_table("users").find_one(username=username)
farm = store.get_farm(user["uid"])
return int(farm.get("coins", 0))
def _has_badge(username, badge_name):
from devplacepy.database import get_table
user = get_table("users").find_one(username=username)
return (
get_table("badges").find_one(user_uid=user["uid"], badge_name=badge_name)
is not None
)
def test_view_other_farm(bob):
page, _ = bob
reset_farm("alice_test")
page.goto(f"{BASE_URL}/game/farm/alice_test", wait_until="domcontentloaded")
page.locator(".game-header h1").wait_for(state="visible")
assert "alice_test" in page.locator(".game-header h1").inner_text()
assert page.locator("[data-game-grid]").first.is_visible()
def test_water_neighbour_build(bob):
page, _ = bob
reset_farm("alice_test")
_plant_growing("alice_test")
page.goto(f"{BASE_URL}/game/farm/alice_test", wait_until="domcontentloaded")
page.locator("[data-game-grid]").first.wait_for(state="visible")
page.wait_for_timeout(800)
water = page.locator("form[data-game-action='water'] button").first
water.wait_for(state="visible")
water.click()
expect(page.locator(".plot-water-count").first).to_contain_text("1/3")
def test_guest_can_view_farm(page):
reset_farm("alice_test")
page.goto(f"{BASE_URL}/game/farm/alice_test", wait_until="domcontentloaded")
page.wait_for_timeout(500)
assert "/auth/login" not in page.url
assert "alice_test" in page.locator(".game-header h1").inner_text()
def test_unknown_farm_is_404(bob):
page, _ = bob
set_setting("happy_404_enabled", "0")
time.sleep(CACHE_VERSION_PROPAGATION_SECONDS)
try:
response = page.goto(
f"{BASE_URL}/game/farm/nope_nobody", wait_until="domcontentloaded"
)
assert response is not None and response.status == 404
finally:
set_setting("happy_404_enabled", "1")
time.sleep(CACHE_VERSION_PROPAGATION_SECONDS)
def test_ready_build_is_protected_during_grace(bob):
page, _ = bob
reset_farm("alice_test")
_plant_growing("alice_test", crop="python")
warp_ready("alice_test")
page.goto(f"{BASE_URL}/game/farm/alice_test", wait_until="domcontentloaded")
page.locator("[data-game-grid]").first.wait_for(state="visible")
page.wait_for_timeout(800)
expect(page.locator(".plot-ready-label").first).to_be_visible()
assert page.locator("form[data-game-action='steal']").count() == 0
def test_steal_button_appears_after_grace(bob):
page, _ = bob
reset_farm("alice_test")
_plant_growing("alice_test", crop="python")
_warp_stealable("alice_test")
page.goto(f"{BASE_URL}/game/farm/alice_test", wait_until="domcontentloaded")
page.locator("[data-game-grid]").first.wait_for(state="visible")
steal = page.locator("form[data-game-action='steal'] button").first
steal.wait_for(state="visible")
assert "Steal" in steal.inner_text()
def test_steal_takes_build_and_pays_thief(bob):
page, _ = bob
reset_farm("alice_test")
reset_farm("bob_test", coins=0)
_plant_growing("alice_test", crop="python")
_warp_stealable("alice_test")
alice_coins_before = _farm_coins("alice_test")
page.goto(f"{BASE_URL}/game/farm/alice_test", wait_until="domcontentloaded")
page.locator("[data-game-grid]").first.wait_for(state="visible")
steal = page.locator("form[data-game-action='steal'] button").first
steal.wait_for(state="visible")
steal.click()
confirm_btn = page.locator(".dialog-confirm")
confirm_btn.wait_for(state="visible")
with page.expect_response(lambda r: "/steal" in r.url and r.request.method == "POST"):
confirm_btn.click()
expect(page.locator("form[data-game-action='steal']")).to_have_count(0)
page.wait_for_timeout(400)
assert _farm_coins("bob_test") == 18
assert _farm_coins("alice_test") == alice_coins_before
assert _has_badge("bob_test", "Cat Burglar")
assert _has_badge("alice_test", "Robbed")
def test_owner_sees_harvest_not_steal(alice):
page, _ = alice
reset_farm("alice_test")
_plant_growing("alice_test", crop="python")
_warp_stealable("alice_test")
page.goto(f"{BASE_URL}/game", wait_until="domcontentloaded")
page.locator("[data-game-grid]").first.wait_for(state="visible")
expect(page.locator("form[data-game-action='harvest'] button").first).to_be_visible()
assert page.locator("form[data-game-action='steal']").count() == 0
def test_victim_notification_names_the_raider_and_the_amount(bob):
page, _ = bob
from devplacepy.database import get_table
reset_farm("alice_test")
reset_farm("bob_test", coins=0)
alice = get_table("users").find_one(username="alice_test")
get_table("notifications").delete(user_uid=alice["uid"], type="harvest_stolen")
_plant_growing("alice_test", crop="python")
_warp_stealable("alice_test")
page.goto(f"{BASE_URL}/game/farm/alice_test", wait_until="domcontentloaded")
page.locator("[data-game-grid]").first.wait_for(state="visible")
steal = page.locator("form[data-game-action='steal'] button").first
steal.wait_for(state="visible")
steal.click()
confirm_btn = page.locator(".dialog-confirm")
confirm_btn.wait_for(state="visible")
with page.expect_response(lambda r: "/steal" in r.url and r.request.method == "POST"):
confirm_btn.click()
expect(page.locator("form[data-game-action='steal']")).to_have_count(0)
page.wait_for_timeout(400)
note = get_table("notifications").find_one(
user_uid=alice["uid"], type="harvest_stolen"
)
assert note is not None
assert "bob_test" in note["message"]
assert "Python Script" in note["message"]
assert "harvest it" in note["message"]