157 lines
5.9 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 playwright.sync_api import expect
from tests.conftest import BASE_URL
_counter_wars = [0]
def _seed_war(faction_a="Tabs", faction_b="Spaces"):
_counter_wars[0] += 1
name = f"ewar{int(time.time() * 1000)}{_counter_wars[0]}"
s = requests.Session()
s.post(
f"{BASE_URL}/auth/signup",
data={
"username": name,
"email": f"{name}@t.dev",
"password": "secret123",
"confirm_password": "secret123",
"birth_date": "1990-01-01",
"accept_terms": "1",
},
allow_redirects=True,
)
r = s.post(
f"{BASE_URL}/posts/create",
data={
"content": "Seeded battle post for browser tests.",
"title": f"battle-{name}",
"topic": "question",
"war_faction_a": faction_a,
"war_faction_b": faction_b,
},
allow_redirects=False,
)
return r.headers["location"].split("/posts/")[-1]
def _open_composer(page):
page.goto(f"{BASE_URL}/feed", wait_until="domcontentloaded")
page.locator(".feed-fab").first.wait_for(state="visible")
page.locator(".feed-fab").first.click()
page.locator("#create-post-modal.visible").wait_for(state="visible")
def test_battles_page_lists_cards(alice):
page, _ = alice
_seed_war("Vikings", "Knights")
page.goto(f"{BASE_URL}/battles", wait_until="domcontentloaded")
page.locator("dp-opinion-war").first.wait_for(state="visible")
expect(page.locator(".topnav-link.active:has-text('Battles')")).to_be_visible()
expect(page.locator("dp-opinion-war .war-vs").first).to_have_text("VS")
def test_battle_card_on_post_page(alice):
page, _ = alice
slug = _seed_war("Ninjas", "Pirates")
page.goto(f"{BASE_URL}/posts/{slug}", wait_until="domcontentloaded")
card = page.locator("dp-opinion-war")
card.wait_for(state="visible")
expect(card.locator(".war-side-a .war-side-name")).to_have_text("Ninjas")
expect(card.locator(".war-field")).to_be_visible()
expect(card.locator(".war-all-link")).to_have_attribute("href", "/battles")
def test_composer_war_toggle_and_poll_exclusivity(alice):
page, _ = alice
_open_composer(page)
modal = page.locator("#create-post-modal")
expect(modal.locator("[data-war-builder]")).to_be_hidden()
modal.locator("[data-war-toggle]").click()
expect(modal.locator("[data-war-builder]")).to_be_visible()
expect(modal.locator("input[name='war_faction_a']")).to_be_enabled()
modal.locator("[data-poll-toggle]").click()
expect(modal.locator("[data-poll-builder]")).to_be_visible()
expect(modal.locator("[data-war-builder]")).to_be_hidden()
modal.locator("[data-war-toggle]").click()
expect(modal.locator("[data-war-builder]")).to_be_visible()
expect(modal.locator("[data-poll-builder]")).to_be_hidden()
def test_composer_war_validation_blocks_submit(alice):
page, _ = alice
_open_composer(page)
modal = page.locator("#create-post-modal")
page.fill("#post-content", "A war with only one named faction must not submit.")
modal.locator("[data-war-toggle]").click()
modal.locator("input[name='war_faction_a']").fill("Solo")
modal.locator("button.btn-primary:has-text('Post')").click()
expect(modal.locator("[data-war-error]")).to_be_visible()
assert "/feed" in page.url
def test_create_join_and_fight_flow(alice):
page, _ = alice
from devplacepy.database import get_table, refresh_snapshot
from devplacepy.services.game.store import ensure_farm
refresh_snapshot()
row = get_table("users").find_one(username="alice_test")
farm = ensure_farm(row["uid"])
get_table("game_farms").update({"uid": farm["uid"], "coins": 1000}, ["uid"])
_open_composer(page)
modal = page.locator("#create-post-modal")
page.fill("#post-content", "Full battle flow: create, join and fight in browser.")
modal.locator("[data-war-toggle]").click()
modal.locator("input[name='war_faction_a']").fill("Coffee")
modal.locator("input[name='war_faction_b']").fill("Tea")
modal.locator("button.btn-primary:has-text('Post')").click()
page.wait_for_url(f"{BASE_URL}/posts/*", wait_until="domcontentloaded")
card = page.locator("dp-opinion-war")
card.wait_for(state="visible")
card.locator(".war-join-a").click()
card.locator("[data-war-mine]").wait_for(state="visible")
hp_before = card.locator("[data-war-hp-a]").text_content()
card.locator("[data-war-fight]").click()
page.wait_for_function(
"before => document.querySelector('[data-war-hp-a]').textContent !== before",
arg=hp_before,
)
expect(card.locator("[data-war-fight]")).to_be_disabled()
expect(card.locator("[data-war-ticker]")).to_contain_text("dealt")
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
def test_switch_faction_shows_confirm(alice):
page, _ = alice
slug = _seed_war("Left", "Right")
page.goto(f"{BASE_URL}/posts/{slug}", wait_until="domcontentloaded")
card = page.locator("dp-opinion-war")
card.wait_for(state="visible")
card.locator(".war-join-b").click()
page.wait_for_url(f"{BASE_URL}/posts/*", wait_until="domcontentloaded")
card = page.locator("dp-opinion-war")
card.locator(".war-switch-btn").wait_for(state="visible")
card.locator(".war-switch-btn").click()
page.locator(".dialog-overlay.visible .dialog-confirm").wait_for(state="visible")
page.locator(".dialog-overlay.visible .dialog-confirm").click()
page.wait_for_url(f"{BASE_URL}/posts/*", wait_until="domcontentloaded")
card = page.locator("dp-opinion-war")
expect(card.locator(".war-mine-faction")).to_have_text("Left")
def test_guest_sees_disabled_actions(page, app_server):
slug = _seed_war("Sun", "Moon")
page.goto(f"{BASE_URL}/posts/{slug}", wait_until="domcontentloaded")
card = page.locator("dp-opinion-war")
card.wait_for(state="visible")
expect(card.locator(".war-join-a")).to_be_disabled()
expect(card.locator(".war-join-b")).to_be_disabled()