harvest/game/tests/test_farm_state.gd
Forrest eb89268b66 Task #11: Animal Pen boundary becomes player-built
Delete the 23 fence_024 scene props (and their unused ext_resource) from
Zones/AnimalPen in farm.tscn -- their StaticBody3D colliders permanently
walled the pen in FarmNavigation.build(), so today they're the only thing
enclosing it. FarmZone.Kind.ANIMAL_PEN and AnimalSystem.update_pen_open()
already drive the open/closed check; players now build ordinary fence/gate
structures around the existing pen rect to close it.

Remove the free starting gate from GameConfig.start_structures (now an
empty array): the pen is fully player-built, nothing stands at the start.

Add game/tests/pen_ring.gd, a shared helper with the pen rect's 24-piece
fence/gate layout (the same positions the deleted props and the old
starting gate used to occupy), built through StructureSystem's normal
validate/place path at normal cost.

Rework the pen tests that assumed a pre-built pen:
- test_structures.gd gets a new _pen_boundary check (open with nothing
  built, open with a gap, shut once the ring is complete -- the task's
  acceptance test) and builds the ring there for the rest of the suite;
  _breakout no longer assumes the gate specifically breaks (a retreating
  thief now follows a global escape-to-map-edge field across equally
  breakable ring pieces, not a single gap in a solid wall), checking
  whichever piece breaks and that patching it shuts the pen again.
- test_targets.gd builds the full ring in setup (gate first, so it keeps
  structure id 1) and spawns the animal thief near the gate so its
  "breaks the gate" narrative still holds.
- test_roster.gd's fence-breaker test places only the gate (not the full
  ring): FarmSim._breaker_goals() treats every structure near a zone as
  "guarding" it, so a full ring would turn its two-barriers-then-the-
  scarecrow narrative into twenty-five.
- test_farm_state.gd and test_cycle.gd's stale "the pen's gate" wording
  and assumptions are updated for the empty start_structures.

No new FAIL lines versus the pre-existing baseline (one known-flaky
test_roster check aside, confirmed to pass on isolated reruns).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-05 14:34:02 -04:00

121 lines
6.1 KiB
GDScript

extends RefCounted
## FarmState: creation from config (all three asset categories, and no starting structures --
## the pen is fully player-built), exact to_dict/from_dict round trips (also through JSON),
## rejection of invalid data, snapshot independence, and night loss records for every category.
func run(r: GameTestRunner) -> void:
var cfg := (load("res://game/resources/game_config.tres") as GameConfig).duplicate() as GameConfig
var s := FarmState.create(cfg)
r.check(s.night == 1 and s.nights_survived == 0, "a new farm starts before night 1")
r.check(s.phase == CampaignController.Phase.MORNING, "a new farm starts in the morning")
r.check(s.scarecrow_hp == cfg.scarecrow_max_hp and s.scarecrow_max_hp == cfg.scarecrow_max_hp, "scarecrow starts at full HP")
r.check(s.crops == cfg.start_crops and s.crops_total == cfg.start_crops, "crops start at the configured count")
r.check(s.animals == cfg.start_animals and s.animals_total == cfg.start_animals, "animals start at the configured count")
var stock := cfg.start_wood + cfg.start_scrap + cfg.start_straw
r.check(s.supplies == stock and s.supplies_total == stock and not s.shed_raided, "supplies are the shed's starting stock (%d)" % stock)
r.check(s.structures.size() == cfg.start_structures.size() and s.structures.is_empty() and s.next_structure_id == 1,
"no structures stand at the start: the pen is fully player-built")
r.check(s.is_valid(), "a new farm is valid")
# A state with every field away from its default.
s.night = 3
s.nights_survived = 2
s.phase = CampaignController.Phase.DUSK
s.phase_time_left = 12.5
s.scarecrow_hp = 6
s.crops_total = 40
for id in [3, 7, 8, 12, 19, 20, 21, 33, 40]:
s.lose_crop(id)
s.night_events = {"crops_stolen": 2, "crops_recovered": 1, "animals_strayed": 1, "supplies_lost": 5}
for id in [2, 5, 7]:
s.lose_animal(id)
s.supplies_total = 120
s.shed_raided = true
s.night_log.append(_entry(1, 4, 0, 0))
s.night_log.append(_entry(2, 5, 3, 43))
var d := s.to_dict()
var copy := FarmState.from_dict(d)
r.check(copy != null and copy.to_dict() == d, "to_dict -> from_dict round trip is exact")
var parsed: Variant = JSON.parse_string(JSON.stringify(d))
var from_json := FarmState.from_dict(parsed as Dictionary)
r.check(from_json != null and from_json.to_dict() == d, "round trip through JSON is exact (numbers coerced back to ints)")
r.check(from_json != null and typeof(from_json.night_log[1]["supplies_lost"]) == TYPE_INT, "night log values are ints after JSON")
d["night_log"][0]["crops_lost"] = 99
r.check(s.night_log[0]["crops_lost"] == 4, "a snapshot is independent of the live state")
_rejects(r, s.to_dict())
_night_records(r, cfg)
func _entry(night: int, crops: int, animals: int, supplies: int) -> Dictionary:
var e := {"night": night}
for key in FarmState.LOG_KEYS:
if key != "night":
e[key] = 0
e["crops_lost"] = crops
e["animals_lost"] = animals
e["supplies_lost"] = supplies
e["crops_stolen"] = crops + 1
e["animals_stolen"] = animals
e["supplies_stolen"] = supplies + 5
return e
func _rejects(r: GameTestRunner, good: Dictionary) -> void:
var cases := {
"another version": func(d: Dictionary) -> void: d["version"] = FarmState.VERSION + 1,
"a missing key": func(d: Dictionary) -> void: d.erase("lost_crops"),
"an unknown phase": func(d: Dictionary) -> void: d["phase"] = 42,
"a lost crop id beyond the total": func(d: Dictionary) -> void: d["lost_crops"] = [d["crops_total"] + 1],
"a crop lost twice": func(d: Dictionary) -> void: d["lost_crops"] = [3, 3],
"an unknown night event": func(d: Dictionary) -> void: d["night_events"] = {"cows_tipped": 1},
"a lost animal id beyond the total": func(d: Dictionary) -> void: d["lost_animals"] = [d["animals_total"] + 1],
"an animal lost twice": func(d: Dictionary) -> void: d["lost_animals"] = [2, 2],
"negative supplies in play": func(d: Dictionary) -> void: d["supplies_total"] = -1,
"HP above max": func(d: Dictionary) -> void: d["scarecrow_hp"] = d["scarecrow_max_hp"] + 1,
"night 0": func(d: Dictionary) -> void: d["night"] = 0,
"more nights survived than played": func(d: Dictionary) -> void: d["nights_survived"] = d["night"] + 1,
"a malformed night log": func(d: Dictionary) -> void: d["night_log"] = [{"night": 1, "crops_lost": 0, "animals_lost": 0, "supplies_lost": 0}],
}
for name: String in cases:
var d := good.duplicate(true)
(cases[name] as Callable).call(d)
r.check(FarmState.from_dict(d) == null, "from_dict rejects %s" % name)
func _night_records(r: GameTestRunner, cfg: GameConfig) -> void:
var s := FarmState.create(cfg)
var start := s.asset_counts()
for id in range(1, 8):
s.lose_crop(id)
s.lose_animal(3)
s.record_event("crops_stolen")
s.record_event("crops_stolen")
s.record_event("crops_recovered")
s.record_event("supplies_stolen", 5)
s.record_event("supplies_lost", 5)
s.resources.shed["wood"] = 99 # stored and spent materials don't count as losses
s.log_night(start)
var e: Dictionary = s.night_log[0]
r.check(s.night_log.size() == 1 and e["night"] == 1 and e["crops_lost"] == 7 and e["animals_lost"] == 1,
"log_night records the crops and animals lost since the night started")
r.check(e["supplies_lost"] == 5 and e["supplies_stolen"] == 5, "supplies lost are the units carried off (%d)" % e["supplies_lost"])
r.check(e["crops_stolen"] == 2 and e["crops_recovered"] == 1 and s.night_events.is_empty(),
"log_night copies the night's theft events and clears them")
s = FarmState.create(cfg)
r.check(s.lose_crop(5) and not s.lose_crop(5) and not s.lose_crop(0) and not s.lose_crop(s.crops_total + 1),
"lose_crop accepts each known unit once")
r.check(s.crops == s.crops_total - 1 and s.is_crop_lost(5) and not s.is_crop_lost(6), "crops is derived from the lost units")
r.check(s.lose_animal(1) and not s.lose_animal(1) and not s.lose_animal(0) and s.animals == s.animals_total - 1,
"lose_animal accepts each known animal once; animals is derived")
s.supplies_in_transit = 4
r.check(s.supplies == s.resources.shed_total() + 4, "supplies count the shed's stock and the sacks still on the farm")
s.supplies_in_transit = 0
s.animals_total = 0
s.supplies_total = 0
s.lose_all_crops()
r.check(s.crops == 0 and s.is_stripped(), "losing every crop unit strips a crops-only farm")