harvest/game/tests/test_farm_state.gd
2026-10-03 12:14:28 -04:00

71 lines
3.4 KiB
GDScript

extends RefCounted
## FarmState: creation from config, exact to_dict/from_dict round trips (also through JSON),
## rejection of invalid data, snapshot independence, and night loss records.
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_total == 0 and s.supplies_total == 0, "animals and supplies are not in play until Step 8")
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
s.crops = 31
s.animals_total = 8
s.animals = 5
s.supplies_total = 120
s.supplies = 77
s.night_log.append({"night": 1, "crops_lost": 4, "animals_lost": 0, "supplies_lost": 0})
s.night_log.append({"night": 2, "crops_lost": 5, "animals_lost": 3, "supplies_lost": 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 _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("crops"),
"an unknown phase": func(d: Dictionary) -> void: d["phase"] = 42,
"more crops than the total": func(d: Dictionary) -> void: d["crops"] = d["crops_total"] + 1,
"negative animals": func(d: Dictionary) -> void: d["animals"] = -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}],
}
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()
s.crops -= 7
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"] == 0,
"log_night records the losses since the night started")