74 lines
2.4 KiB
Python
Raw Normal View History

Add Opinion Wars: week-long two-faction battles attached to posts A new post attachment type beside polls: the composer gains a Start Opinion War builder (same disabled-inputs opt-in as the poll builder) that names exactly two factions; the battle runs for exactly 7 days from post creation. Members join a side, may defect at any time (damage already dealt stays with the faction it was dealt to), and fight once per 24 hours per battle. A fight spends 25 Code Farm coins and deals deterministic level-weighted damage: 100 + 10 * min(level, 20) HP, so a newcomer deals 110 and a veteran caps at 300 - no randomness anywhere. The battle renders on the post card as a CSS pixel-art battlefield (box-shadow sprites: castles, faction flags, marching soldiers, a flickering campfire; steps() animation, disabled under reduced motion) with live HP bars, a countdown, the viewer's faction strip, top contributors and an event ticker. Live frames ride pub/sub on public.battle.{uid} via a relay on the service-lock owner, with the durable opinion_war_events trail (per-war atomic seq) as the source of truth and a 15s incremental poller as fallback. /battles lists battles with active/ended/mine filters, search and pagination. Every mutation is a conditional UPDATE via conditional_update_row: the fight sequence claims the cooldown first, then spends coins, then lands the damage, compensating earlier steps on any later refusal so a crash costs a turn, never coins. Resolution is lazy on read (no cron): an exactly-once CAS computes the winner in the statement, awards XP (participation, winner bonus, top damage dealer bonus; draws pay participation only), emits the result event and notifies fighters. The OpinionWarService backstop resolves unviewed wars and sends fight-ready notifications, exactly-once via a marker CAS. Fan-out: battle notification type, four badges, audit keys (battle.create/join/switch/fight/resolve), Devii actions (join/fight confirm-gated), API docs group, docs prose page, sitemap and topnav entries, REPORTABLE_TARGETS registration, post-delete cascades, README and nested CLAUDE.md documentation. Verified with the four-layer procedure: property checks over the full damage domain, 1200-step stateful fuzz (hp-sum invariant, coins never negative, resolved totals frozen), and real 8-process races proving exactly-once semantics for concurrent fights, double-spends across two wars, resolution XP and double-joins. Persisted tests in tests/unit/services/opinionwar, tests/api/battles, tests/e2e/battles and tests/api/posts/create.py. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-20 23:30:02 +02:00
# retoor <retoor@molodetz.nl>
import time
import requests
from tests.api.battles._helpers import JSON, _create_war_post, _session_battles
from tests.conftest import BASE_URL
def test_listing_renders_battle_card(app_server):
s, _ = _session_battles()
_create_war_post(s, f"listing-{int(time.time() * 1000)}", "Vim", "Emacs")
r = s.get(f"{BASE_URL}/battles")
assert r.status_code == 200
assert "dp-opinion-war" in r.text
assert "Vim" in r.text
def test_listing_json_shape(app_server):
s, _ = _session_battles()
_, war = _create_war_post(s, f"json-{int(time.time() * 1000)}")
r = s.get(f"{BASE_URL}/battles", headers=JSON)
assert r.status_code == 200
data = r.json()
assert data["current_filter"] == "active"
assert set(data["counts"]) == {"active", "ended", "mine"}
uids = [battle["uid"] for battle in data["battles"]]
assert war["uid"] in uids
battle = next(b for b in data["battles"] if b["uid"] == war["uid"])
assert battle["faction_a"] == "Tabs"
assert battle["pct_a"] + battle["pct_b"] == 100
assert battle["fight_cost"] > 0
def test_listing_guest_access(app_server):
s, _ = _session_battles()
_create_war_post(s, f"guest-{int(time.time() * 1000)}")
r = requests.get(f"{BASE_URL}/battles")
assert r.status_code == 200
assert "dp-opinion-war" in r.text
def test_listing_search_matches_faction(app_server):
s, _ = _session_battles()
marker = f"Zx{int(time.time() * 1000) % 100000}"
_, war = _create_war_post(s, f"search-{marker}", marker, "Others")
r = s.get(f"{BASE_URL}/battles", params={"search": marker}, headers=JSON)
uids = [battle["uid"] for battle in r.json()["battles"]]
assert uids == [war["uid"]]
def test_listing_invalid_filter_falls_back(app_server):
s, _ = _session_battles()
r = s.get(f"{BASE_URL}/battles", params={"filter": "bogus"}, headers=JSON)
assert r.json()["current_filter"] == "active"
def test_battle_state_json(app_server):
s, _ = _session_battles()
_, war = _create_war_post(s, f"state-{int(time.time() * 1000)}")
r = requests.get(f"{BASE_URL}/battles/{war['uid']}")
assert r.status_code == 200
data = r.json()
assert data["uid"] == war["uid"]
assert data["status"] == "active"
assert data["viewer"] is None
assert data["ends_in"]
def test_battle_state_unknown_uid_404(app_server):
r = requests.get(f"{BASE_URL}/battles/does-not-exist", headers=JSON)
assert r.status_code == 404