Files
windroost/tools/fullgame/test_effects.py
T
wushenghua 16d3cbe911 feat: effect-primitive engine foundation (stage A of gameplay-depth goal)
Continued PRODUCTION/51. Built the ADR-008 effect-primitive system that turns
skills from damage numbers into real distinct combat effects:
- tools/fullgame/effects.py + GDScript effects.gd mirror: 16 primitives
  (damage/heal/shield/buff/debuff/stun/slow/poison/taunt/summon/cleanse/
  execute/chain/lifesteal/energy/reflect) with real battle semantics.
- test_effects.py PASS 9 checks (each primitive distinct + identity).
- check_effects_parity.py PASS 17 cases (python==gd).
- skill_runtime.py bridge: folds skill effects into per-tick battle state;
  test_skill_runtime.py PASS 7 checks proving >=4 skills give distinct outcomes.
- Identity invariant: absent skills -> zero change (all 64 gates green).

Next: wire skill_runtime into both battle sims (with parity + identity guard).
P2_CHECKS_PASS. prototype/ untracked.
2026-11-19 13:07:43 +08:00

128 lines
5.7 KiB
Python

#!/usr/bin/env python3
"""Focused tests: each effect primitive produces a DISTINCT, observable outcome."""
import sys
sys.path.insert(0, __import__("pathlib").Path(__file__).parent)
import effects as E
def check(cond, msg):
if not cond:
raise AssertionError(msg)
def enemies():
return [
{"id": "E1#1", "content_id": "E1", "hp": 100.0, "max_hp": 100.0, "dead": False},
{"id": "E2#2", "content_id": "E2", "hp": 30.0, "max_hp": 100.0, "dead": False},
{"id": "E3#3", "content_id": "E3", "hp": 80.0, "max_hp": 100.0, "dead": False},
]
def test_identity():
s = E.new_skill_state()
check(E.ally_damage_mult(s) == 1.0, "empty state -> identity damage mult")
check(E.ally_incoming_mult(s) == 1.0, "empty state -> identity incoming mult")
check(E.extra_dps(s) == 0.0, "empty state -> no summon dps")
check(E.tick_effects(s, 0.1)["poison_damage"] == {}, "empty state -> no poison")
def test_damage_vs_stun_distinct():
s1 = E.new_skill_state(); s2 = E.new_skill_state()
d1 = E.apply_effect(s1, {"type": "damage", "value": 20}, {"enemies": enemies(), "target_id": "E1#1"})
d2 = E.apply_effect(s2, {"type": "stun", "duration": 2.0}, {"enemies": enemies(), "target_id": "E1#1"})
check(d1["damage"] == 20 and d1["skipped"] == [], "damage deals damage, no skip")
check(d2["damage"] == 0 and d2["skipped"] == ["E1#1"], "stun skips target, no damage")
check("E1#1" in s2["stunned"], "stun sets stunned state")
def test_execute_low_hp_only():
s = E.new_skill_state()
out = E.apply_effect(s, {"type": "execute", "threshold": 0.35},
{"enemies": enemies(), "target_id": "E2#2"})
check(out["killed_ids"] == ["E2#2"], "execute kills the 30/100 target at 35% line")
out2 = E.apply_effect(s, {"type": "execute", "threshold": 0.35},
{"enemies": enemies(), "target_id": "E1#1"})
check(out2["killed_ids"] == [], "execute does not kill a full-HP target")
def test_chain_hits_multiple():
s = E.new_skill_state()
out = E.apply_effect(s, {"type": "chain", "value": 10, "jumps": 2},
{"enemies": enemies(), "target_id": "E1#1"})
check(out["damage"] == 20, "chain hits 2 extra targets for 10 each")
check(len(out["meta"]["chain_targets"]) == 2, "chain records 2 targets")
def test_poison_ticks():
s = E.new_skill_state()
E.apply_effect(s, {"type": "poison", "value": 5, "duration": 1.0},
{"enemies": enemies(), "target_id": "E1#1", "tick_seconds": 0.1})
t1 = E.tick_effects(s, 0.1)
check(t1["poison_damage"]["E1#1"] == 0.5, "poison deals 5 dps * 0.1s = 0.5")
for _ in range(10):
E.tick_effects(s, 0.1)
check("E1#1" not in s["debuffs"], "poison expires after duration")
def test_shield_and_buff_and_heal():
s = E.new_skill_state()
sh = E.apply_effect(s, {"type": "shield", "value": 50}, {"enemies": []})
check(sh["shield_gain"] == 50, "shield provides absorb pool")
E.apply_effect(s, {"type": "buff", "stat": "atk", "value": 0.3}, {"enemies": []})
check(abs(E.ally_damage_mult(s) - 1.3) < 1e-9, "atk buff raises damage mult")
E.apply_effect(s, {"type": "buff", "stat": "def", "value": 0.5}, {"enemies": []})
check(abs(E.ally_incoming_mult(s) - 0.5) < 1e-9, "def buff lowers incoming damage")
he = E.apply_effect(s, {"type": "heal", "value": 25}, {"enemies": []})
check(he["healed"] == 25, "heal returns heal amount")
def test_debuff_slow_stun_summon_reflect():
s = E.new_skill_state()
E.apply_effect(s, {"type": "debuff", "stat": "atk", "value": 0.4, "duration": 1.0},
{"enemies": enemies(), "target_id": "E1#1", "tick_seconds": 0.1})
check(s["debuffs"]["E1#1"]["atk"] == 0.4, "debuff lowers enemy atk")
E.apply_effect(s, {"type": "slow", "value": 0.5, "duration": 1.0},
{"enemies": enemies(), "target_id": "E1#1", "tick_seconds": 0.1})
check(s["debuffs"]["E1#1"]["slow"] == 0.5, "slow sets enemy slow")
E.apply_effect(s, {"type": "summon", "value": 15}, {"enemies": []})
check(E.extra_dps(s) == 15, "summon adds dps")
E.apply_effect(s, {"type": "reflect", "value": 0.3}, {"enemies": []})
check(abs(s["reflect_pct"] - 0.3) < 1e-9, "reflect sets pct")
before = E.ally_damage_mult(s)
E.apply_effect(s, {"type": "energy", "value": 0.5}, {"enemies": []})
check(E.ally_damage_mult(s) > before, "energy bonus raises mult")
def test_taunt_cleanse_lifesteal_distinct():
s = E.new_skill_state()
E.apply_effect(s, {"type": "taunt", "value": 0.6}, {"enemies": enemies(), "target_id": "E1#1"})
check(s["taunt_target"] == "E1#1" and s["ally_buff_def"] > 0, "taunt redirects + reduces dmg")
E.apply_effect(s, {"type": "lifesteal", "value": 0.2}, {"enemies": []})
check(abs(s["lifesteal_pct"] - 0.2) < 1e-9, "lifesteal sets pct")
E.apply_effect(s, {"type": "cleanse", "value": 0}, {"enemies": []})
check(len(s["debuffs"]) == 0, "cleanse removes debuffs")
def test_all_primitives_covered():
problems = E.validate_primitives()
check(not problems, f"primitive validation: {problems}")
# every primitive must be exercised above (event_counts populated)
s = E.new_skill_state()
ctx = {"enemies": enemies(), "target_id": "E1#1", "tick_seconds": 0.1}
for p in E.PRIMITIVES:
E.apply_effect(s, {"type": p, "value": 1, "duration": 1.0}, ctx)
for p in E.PRIMITIVES:
check(s["event_counts"].get(p, 0) >= 1, f"{p} was applied")
def main():
fns = [v for k, v in sorted(globals().items()) if k.startswith("test_")]
for fn in fns:
fn()
print(f"EFFECTS_PASS {len(fns)} checks, {len(E.PRIMITIVES)} primitives distinct")
return 0
if __name__ == "__main__":
raise SystemExit(main())