spaceship/tests/test_conquered_planet_clear.gd
Loïc Wisniewski 2f161621c3 #84: move 215 root files into a folder hierarchy
Everything lived in one flat directory, so a diff gave no clue what area it
touched and finding the file you wanted meant knowing its exact name. Twelve
folders now, grouped by what a thing IS rather than what type it is - a
vessel's definition, its component scenes and its upgrade resources sit
together, because that is the set you read when you change one of them.

  buildings  core  controllers  enemies  logistics  planets
  resources  shaders  surface  ui  vessels  vfx  world

332 files moved and every res:// reference rewritten - 306 files touched,
including the autoload and main-scene paths in project.godot. Nothing renamed:
this is purely where files sit, so the diff is a pile of pure moves and no
behaviour is buried in it. ui_theme.tres, icon.svg and web_shell.html stay at
the root, where Godot and the export preset expect them.

Verified three ways, because a suite alone would not have caught all of it:
the full 117 pass, the web export builds clean, and the game renders.

The export is why that matters. The FIRST export after the move failed with
"Attempt to open script 'res://GameState.gd'" - the old path - while
project.godot on disk plainly said res://core/GameState.gd. That is this
project's documented Android FUSE mtime trap: Godot served a cached copy of a
file whose content had changed. A `touch` fixed it. Nothing in the test suite
would have found that, because the suite runs from a copy on a native
filesystem - the one place the bug cannot happen.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-21 19:44:14 +02:00

137 lines
4.9 KiB
GDScript

extends SceneTree
# Headless gameplay test: run with
# godot --headless -s res://tests/test_conquered_planet_clear.gd
# A planet's garrison was destroyed to take it, so it must not be standing
# there again the next time the player lands. Capture points already came
# back marked captured; the enemies came back alive, which read as the
# conquest having quietly been undone.
#
# Covers:
# A. an unconquered planet lays out its authored enemies, as before
# B. once every capture point is taken, re-landing places no enemies - and
# still places the capture points, marked captured
# C. a setup that authors enemies but NO capture points keeps them. Such a
# planet counts as vacuously conquered (nothing to capture), so keying
# only off the conquered flag would delete a garrison nobody has beaten
const PLANET_KEY: String = "test_conquered_planet_clear"
const OPEN_PLANET_KEY: String = "test_conquered_planet_clear_open"
var game_state: Node
var host_scene: PackedScene
var stage: int = 0
var elapsed: float = 0.0
func _initialize() -> void:
game_state = root.get_node("GameState")
game_state.planet_capture.clear()
host_scene = load("res://surface/PlanetSetupHost.tscn")
func _process(delta: float) -> bool:
elapsed += delta
if stage == 0:
# One frame of grace so the autoload is fully up, same as the other
# scene-driven tests.
if elapsed < 0.1:
return false
# --- A. unconquered: the garrison is there ---------------------------
game_state.planet_id = PLANET_KEY
game_state.planet_definition_id = "rocky"
var setup: Resource = _make_setup(2, 3)
var enemies: int = _apply_and_count_enemies(setup)
if enemies != 3:
return _fail("an unconquered planet placed %d of its 3 authored enemies" % enemies)
if game_state.is_planet_conquered(PLANET_KEY):
return _fail("a planet with 2 uncaptured points reported itself conquered")
print("unconquered planet: 3 enemies placed")
# --- B. conquered: the garrison stays destroyed ----------------------
game_state.mark_point_captured(PLANET_KEY, 0)
game_state.mark_point_captured(PLANET_KEY, 1)
if not game_state.is_planet_conquered(PLANET_KEY):
return _fail("capturing every point did not conquer the planet")
var host: Node = host_scene.instantiate()
root.add_child(host)
host.apply_setup(setup)
var remaining: int = _count_enemies(host)
var points: int = _count_capture_points(host)
host.free()
if remaining != 0:
return _fail("re-landing on a conquered planet placed %d enemies" % remaining)
if points != 2:
return _fail("a conquered planet placed %d of its 2 capture points - conquest should clear the garrison, not the map" % points)
print("conquered planet: 0 enemies, both capture points still placed")
# --- C. a setup with nothing to capture keeps its enemies ------------
game_state.planet_id = OPEN_PLANET_KEY
var open_setup: Resource = _make_setup(0, 2)
var open_enemies: int = _apply_and_count_enemies(open_setup)
if not game_state.is_planet_conquered(OPEN_PLANET_KEY):
return _fail("a setup with no capture points was expected to register as vacuously conquered, and did not - so this case no longer tests what it claims to")
if open_enemies != 2:
return _fail("a setup with no capture points lost %d of its 2 enemies to the conquest rule" \
% (2 - open_enemies))
print("setup with no capture points: both enemies kept, despite counting as conquered")
print("TEST_CONQUERED_PLANET_CLEAR: PASS")
quit(0)
return true
return false
func _make_setup(capture_count: int, enemy_count: int) -> Resource:
var setup: Resource = load("res://surface/PlanetSetup.gd").new()
setup.setup_id = "in_memory"
setup.surface_profile_id = "rocky"
var points: PackedVector2Array = PackedVector2Array()
for i in range(capture_count):
points.append(Vector2(200.0 * float(i + 1), 0.0))
setup.capture_points = points
var spawn_script: Script = load("res://surface/PlanetSetupSpawn.gd")
for i in range(enemy_count):
var spawn: Resource = spawn_script.new()
spawn.enemy_id = "chaser"
spawn.position = Vector2(0.0, 200.0 * float(i + 1))
setup.enemy_spawns.append(spawn)
return setup
func _apply_and_count_enemies(setup: Resource) -> int:
var host: Node = host_scene.instantiate()
root.add_child(host)
host.apply_setup(setup)
var count: int = _count_enemies(host)
host.free()
return count
# Counted by group rather than by script or node name: that is how everything
# else in the game finds enemies, so this cannot pass while the real hunters
# see something different.
func _count_enemies(host: Node) -> int:
var count: int = 0
for child in host.get_children():
if child.is_in_group("enemies"):
count += 1
return count
func _count_capture_points(host: Node) -> int:
var count: int = 0
for child in host.get_children():
if child.is_in_group("enemies"):
continue
if "point_index" in child:
count += 1
return count
func _fail(message: String) -> bool:
print("TEST_CONQUERED_PLANET_CLEAR: FAIL (%s)" % message)
quit(1)
return true