test: tighten HUD layout contracts

This commit is contained in:
goose
2026-10-08 19:46:53 -04:00
parent 885781cf9c
commit 59e922ce29
2 changed files with 199 additions and 89 deletions
+37 -8
View File
@@ -9,14 +9,20 @@ func before_each() -> void:
await get_tree().process_frame
func test_real_game_scene_exposes_readable_fov_zoom_and_framed_center_viewport() -> void:
var renderer := game.get_node("World/MapRenderer") as MapRenderer
var camera := game.get_node("Camera2D") as Camera2D
var hud := game.get_node("HUD") as GameHud
var results := game.submit_test_action(Vector2i.DOWN)
var renderer := game.get_node_or_null("World/MapRenderer") as MapRenderer
var camera := game.get_node_or_null("Camera2D") as Camera2D
var hud := game.get_node_or_null("HUD") as GameHud
assert_not_null(renderer, "Game scene requires World/MapRenderer")
assert_not_null(camera, "Game scene requires Camera2D")
assert_not_null(hud, "Game scene requires HUD")
if renderer == null or camera == null or hud == null:
return
var layout := hud.get_node_or_null("ResponsiveLayout") as Control
assert_not_null(layout)
assert_not_null(layout, "HUD requires ResponsiveLayout")
if layout == null:
return
_assert_legacy_panel_paths(layout)
var top_bar := layout.get_node_or_null("TopBar") as Control
var left_rail := layout.get_node_or_null("LeftRail") as Control
@@ -31,12 +37,35 @@ func test_real_game_scene_exposes_readable_fov_zoom_and_framed_center_viewport()
if top_bar == null or left_rail == null or play_area_frame == null or right_rail == null or command_bar == null:
return
var play_rect := play_area_frame.get_global_rect()
var results := game.submit_test_action(Vector2i.DOWN)
assert_gte(results.size(), 1, "the real game action must produce a result")
if results.is_empty():
return
assert_eq(results[0].kind, &"moved")
assert_eq(camera.zoom, Vector2(2.0, 2.0))
assert_eq(renderer.get_cell_render_color(Vector2i(4, 12)), Color.WHITE)
assert_eq(renderer.get_cell_render_color(Vector2i(29, 19)), renderer.UNKNOWN_COLOR)
assert_true(play_rect.has_point(Vector2(Game.REFERENCE_VIEWPORT) / 2.0), "the play frame must occupy the center of the game viewport")
assert_gt(play_rect.get_area(), 0.0)
_assert_play_area_is_centered_and_inside_effective_viewport(play_area_frame)
var play_rect := play_area_frame.get_global_rect()
for region: Control in [top_bar, left_rail, right_rail, command_bar]:
assert_false(region.get_global_rect().intersects(play_rect), "%s obscures the framed center viewport" % region.name)
func _assert_legacy_panel_paths(layout: Control) -> void:
assert_not_null(layout.get_node_or_null("StatsPanel") as PanelContainer, "HUD must preserve ResponsiveLayout/StatsPanel")
assert_not_null(layout.get_node_or_null("LogPanel") as PanelContainer, "HUD must preserve ResponsiveLayout/LogPanel because GameHud uses that path")
assert_not_null(layout.get_node_or_null("PromptPanel") as PanelContainer, "HUD must preserve ResponsiveLayout/PromptPanel")
assert_not_null(layout.get_node_or_null("HelpPanel") as PanelContainer, "HUD must preserve ResponsiveLayout/HelpPanel")
func _assert_play_area_is_centered_and_inside_effective_viewport(play_area_frame: Control) -> void:
var viewport_rect := get_viewport().get_visible_rect()
var expected_viewport := Vector2(Game.REFERENCE_VIEWPORT)
var play_rect := play_area_frame.get_global_rect()
assert_eq(viewport_rect.size, expected_viewport, "HUD layout is evaluated in the effective 1920x1080 viewport")
assert_gte(play_rect.position.x, viewport_rect.position.x, "PlayAreaFrame must stay inside the viewport on the left")
assert_gte(play_rect.position.y, viewport_rect.position.y, "PlayAreaFrame must stay inside the viewport at the top")
assert_lte(play_rect.end.x, viewport_rect.end.x, "PlayAreaFrame must stay inside the viewport on the right")
assert_lte(play_rect.end.y, viewport_rect.end.y, "PlayAreaFrame must stay inside the viewport at the bottom")
assert_gte(play_rect.size.x, viewport_rect.size.x * 0.40, "PlayAreaFrame must have meaningful viewport width")
assert_gte(play_rect.size.y, viewport_rect.size.y * 0.40, "PlayAreaFrame must have meaningful viewport height")
assert_gte(play_rect.get_area(), viewport_rect.get_area() * 0.25, "PlayAreaFrame must occupy a meaningful part of the viewport")
assert_lte(play_rect.get_center().distance_to(viewport_rect.get_center()), 2.0, "PlayAreaFrame must be centered in the effective viewport")
+162 -81
View File
@@ -1,6 +1,9 @@
extends GutTest
const GameScene = preload("res://scenes/Game.tscn")
const HEADER_VALUES_GROUP := &"hud_header_values"
const CHARACTER_VALUES_GROUP := &"hud_character_values"
const COMMAND_HINTS_GROUP := &"hud_command_hints"
var game: Game
func before_each() -> void:
@@ -19,7 +22,10 @@ func test_help_band_displays_the_exact_control_labels() -> void:
assert_string_contains(help_label.text, "Interagir : Espace ou E")
func test_xp_bar_shows_progress_inside_the_current_level_and_refreshes_after_xp_gain() -> void:
var hud := game.get_node("HUD") as GameHud
var hud := game.get_node_or_null("HUD") as GameHud
assert_not_null(hud, "Game scene requires HUD")
if hud == null:
return
var state := game.get_game_state()
var xp_bar := hud.get_node_or_null("ExperienceBar") as ProgressBar
var xp_label := hud.get_node_or_null("ExperienceLabel") as Label
@@ -40,10 +46,49 @@ func test_xp_bar_shows_progress_inside_the_current_level_and_refreshes_after_xp_
assert_eq(xp_bar.value, 25.0)
assert_eq(xp_label.text, "Niveau 2 — XP 25/200")
func test_hud_defines_a_full_frame_shell_with_distinct_centered_regions() -> void:
var hud := game.get_node("HUD") as GameHud
func test_hud_preserves_legacy_responsive_panel_paths() -> void:
var hud := game.get_node_or_null("HUD") as GameHud
assert_not_null(hud, "Game scene requires HUD")
if hud == null:
return
var layout := hud.get_node_or_null("ResponsiveLayout") as Control
assert_not_null(layout)
assert_not_null(layout, "HUD requires ResponsiveLayout")
if layout == null:
return
var stats_panel := layout.get_node_or_null("StatsPanel") as PanelContainer
var log_panel := layout.get_node_or_null("LogPanel") as PanelContainer
var prompt_panel := layout.get_node_or_null("PromptPanel") as PanelContainer
var help_panel := layout.get_node_or_null("HelpPanel") as PanelContainer
assert_not_null(stats_panel, "HUD must preserve ResponsiveLayout/StatsPanel")
assert_not_null(log_panel, "HUD must preserve ResponsiveLayout/LogPanel because GameHud uses that path")
assert_not_null(prompt_panel, "HUD must preserve ResponsiveLayout/PromptPanel")
assert_not_null(help_panel, "HUD must preserve ResponsiveLayout/HelpPanel")
if stats_panel == null or log_panel == null or prompt_panel == null or help_panel == null:
return
var player_health := hud.get_node_or_null("PlayerHealthLabel") as Control
var event_log := hud.get_node_or_null("EventLog") as Control
var prompt := hud.get_node_or_null("ContextPromptLabel") as Control
var help := hud.get_node_or_null("HelpLabel") as Control
assert_not_null(player_health, "StatsPanel requires the existing health control")
assert_not_null(event_log, "LogPanel requires the existing event log")
assert_not_null(prompt, "PromptPanel requires the existing context prompt")
assert_not_null(help, "HelpPanel requires the existing help label")
if player_health == null or event_log == null or prompt == null or help == null:
return
assert_true(stats_panel.get_global_rect().encloses(player_health.get_global_rect()))
assert_true(log_panel.get_global_rect().encloses(event_log.get_global_rect()))
assert_true(prompt_panel.get_global_rect().encloses(prompt.get_global_rect()))
assert_true(help_panel.get_global_rect().encloses(help.get_global_rect()))
func test_hud_defines_a_full_frame_shell_with_a_significant_centered_play_area() -> void:
var hud := game.get_node_or_null("HUD") as GameHud
assert_not_null(hud, "Game scene requires HUD")
if hud == null:
return
var layout := hud.get_node_or_null("ResponsiveLayout") as Control
assert_not_null(layout, "HUD requires ResponsiveLayout")
if layout == null:
return
@@ -62,23 +107,24 @@ func test_hud_defines_a_full_frame_shell_with_distinct_centered_regions() -> voi
assert_eq(layout.anchor_right, 1.0)
assert_eq(layout.anchor_bottom, 1.0)
var top_rect := top_bar.get_global_rect()
var left_rect := left_rail.get_global_rect()
_assert_play_area_is_centered_and_inside_effective_viewport(play_area_frame)
var play_rect := play_area_frame.get_global_rect()
var right_rect := right_rail.get_global_rect()
var command_rect := command_bar.get_global_rect()
assert_true(play_rect.has_point(Vector2(Game.REFERENCE_VIEWPORT) / 2.0), "the framed play area remains centered in the reference viewport")
assert_gt(play_rect.get_area(), 0.0)
assert_false(left_rect.intersects(play_rect), "LeftRail must not cover the framed play area")
assert_false(right_rect.intersects(play_rect), "RightRail must not cover the framed play area")
assert_false(top_rect.intersects(play_rect), "TopBar must not cover the framed play area")
assert_false(command_rect.intersects(play_rect), "CommandBar must not cover the framed play area")
assert_false(left_rect.intersects(right_rect), "the two rails must be distinct")
assert_false(top_rect.intersects(command_rect), "the top and command bars must be distinct")
assert_false(left_rail.get_global_rect().intersects(play_rect), "LeftRail must not cover the framed play area")
assert_false(right_rail.get_global_rect().intersects(play_rect), "RightRail must not cover the framed play area")
assert_false(top_bar.get_global_rect().intersects(play_rect), "TopBar must not cover the framed play area")
assert_false(command_bar.get_global_rect().intersects(play_rect), "CommandBar must not cover the framed play area")
assert_false(left_rail.get_global_rect().intersects(right_rail.get_global_rect()), "the two rails must be distinct")
assert_false(top_bar.get_global_rect().intersects(command_bar.get_global_rect()), "the top and command bars must be distinct")
func test_live_hud_content_is_enclosed_by_its_intended_shell_region() -> void:
var hud := game.get_node("HUD") as GameHud
var layout := hud.get_node("ResponsiveLayout") as Control
var hud := game.get_node_or_null("HUD") as GameHud
assert_not_null(hud, "Game scene requires HUD")
if hud == null:
return
var layout := hud.get_node_or_null("ResponsiveLayout") as Control
assert_not_null(layout, "HUD requires ResponsiveLayout")
if layout == null:
return
var top_bar := layout.get_node_or_null("TopBar") as Control
var left_rail := layout.get_node_or_null("LeftRail") as Control
var right_rail := layout.get_node_or_null("RightRail") as Control
@@ -90,59 +136,41 @@ func test_live_hud_content_is_enclosed_by_its_intended_shell_region() -> void:
if top_bar == null or left_rail == null or right_rail == null or command_bar == null:
return
var regions := {
top_bar: [
hud.get_node_or_null("AchievementToast"),
hud.get_node_or_null("TitleLabel"),
hud.get_node_or_null("HeaderPlayerNameLabel"),
hud.get_node_or_null("HeaderFloorLabel"),
hud.get_node_or_null("HeaderLocationLabel"),
hud.get_node_or_null("HeaderExplorationLabel"),
],
left_rail: [
hud.get_node_or_null("PlayerHealthLabel"),
hud.get_node_or_null("PlayerFocusLabel"),
hud.get_node_or_null("ExperienceBar"),
hud.get_node_or_null("ExperienceLabel"),
hud.get_node_or_null("GoldLabel"),
hud.get_node_or_null("QuestTrackerLabel"),
hud.get_node_or_null("SpellLabel"),
hud.get_node_or_null("EnemyHealthLabel"),
hud.get_node_or_null("CombatSummaryLabel"),
hud.get_node_or_null("DeadlineLabel"),
],
right_rail: [hud.get_node_or_null("EventLog")],
command_bar: [
hud.get_node_or_null("ContextPromptLabel"),
hud.get_node_or_null("HelpLabel"),
],
}
for region: Control in regions:
for live_control: Control in regions[region]:
assert_not_null(live_control, "%s must retain its live HUD control" % region.name)
if live_control != null:
assert_true(region.get_global_rect().encloses(live_control.get_global_rect()), "%s must enclose %s" % [region.name, live_control.name])
_assert_controls_enclosed(top_bar, [
hud.get_node_or_null("AchievementToast") as Control,
hud.get_node_or_null("TitleLabel") as Control,
] + _controls_in_group(hud, HEADER_VALUES_GROUP), "TopBar")
_assert_controls_enclosed(left_rail, [
hud.get_node_or_null("PlayerHealthLabel") as Control,
hud.get_node_or_null("PlayerFocusLabel") as Control,
hud.get_node_or_null("ExperienceBar") as Control,
hud.get_node_or_null("ExperienceLabel") as Control,
hud.get_node_or_null("GoldLabel") as Control,
hud.get_node_or_null("QuestTrackerLabel") as Control,
hud.get_node_or_null("SpellLabel") as Control,
hud.get_node_or_null("EnemyHealthLabel") as Control,
hud.get_node_or_null("CombatSummaryLabel") as Control,
hud.get_node_or_null("DeadlineLabel") as Control,
] + _controls_in_group(hud, CHARACTER_VALUES_GROUP), "LeftRail")
_assert_controls_enclosed(right_rail, [hud.get_node_or_null("EventLog") as Control], "RightRail")
_assert_controls_enclosed(command_bar, _controls_in_group(hud, COMMAND_HINTS_GROUP), "CommandBar")
func test_header_character_and_command_fields_refresh_from_live_game_values() -> void:
var hud := game.get_node("HUD") as GameHud
var layout := hud.get_node("ResponsiveLayout") as Control
func test_dynamic_hud_groups_refresh_from_live_game_values() -> void:
var hud := game.get_node_or_null("HUD") as GameHud
assert_not_null(hud, "Game scene requires HUD")
if hud == null:
return
var layout := hud.get_node_or_null("ResponsiveLayout") as Control
assert_not_null(layout, "HUD requires ResponsiveLayout")
if layout == null:
return
var top_bar := layout.get_node_or_null("TopBar") as Control
var left_rail := layout.get_node_or_null("LeftRail") as Control
var command_bar := layout.get_node_or_null("CommandBar") as Control
var player_name := hud.get_node_or_null("HeaderPlayerNameLabel") as Label
var floor := hud.get_node_or_null("HeaderFloorLabel") as Label
var location := hud.get_node_or_null("HeaderLocationLabel") as Label
var exploration := hud.get_node_or_null("HeaderExplorationLabel") as Label
var player_health := hud.get_node_or_null("PlayerHealthLabel") as Label
var prompt := hud.get_node_or_null("ContextPromptLabel") as Label
assert_not_null(top_bar, "ResponsiveLayout requires a TopBar")
assert_not_null(left_rail, "ResponsiveLayout requires a LeftRail")
assert_not_null(command_bar, "ResponsiveLayout requires a CommandBar")
assert_not_null(player_name, "TopBar requires a live HeaderPlayerNameLabel")
assert_not_null(floor, "TopBar requires a live HeaderFloorLabel")
assert_not_null(location, "TopBar requires a live HeaderLocationLabel")
assert_not_null(exploration, "TopBar requires a live HeaderExplorationLabel")
assert_not_null(player_health)
assert_not_null(prompt)
if top_bar == null or command_bar == null or player_name == null or floor == null or location == null or exploration == null or player_health == null or prompt == null:
if top_bar == null or left_rail == null or command_bar == null:
return
var state := game.get_game_state()
@@ -152,24 +180,36 @@ func test_header_character_and_command_fields_refresh_from_live_game_values() ->
state.player.cell = state.floor_state.entrance
hud.refresh(state, [], run)
assert_string_contains(player_name.text, run.player_name)
assert_string_contains(floor.text, str(state.floor_state.floor_number))
assert_string_contains(location.text, String(state.floor_state.room_at(state.player.cell).id))
assert_string_contains(exploration.text, str(state.explored_cells.size()))
assert_string_contains(player_health.text, str(state.player.hit_points))
var header_values := _labels_in_group(hud, HEADER_VALUES_GROUP)
var character_values := _labels_in_group(hud, CHARACTER_VALUES_GROUP)
var command_hints := _labels_in_group(hud, COMMAND_HINTS_GROUP)
assert_gte(header_values.size(), 1, "TopBar exposes dynamic header values through the hud_header_values group")
assert_gte(character_values.size(), 1, "LeftRail exposes dynamic character values through the hud_character_values group")
assert_gte(command_hints.size(), 1, "CommandBar exposes dynamic hints through the hud_command_hints group")
if header_values.is_empty() or character_values.is_empty() or command_hints.is_empty():
return
var header_text := _combined_label_text(header_values)
var character_text := _combined_label_text(character_values)
assert_string_contains(header_text, run.player_name)
assert_string_contains(header_text, str(state.floor_state.floor_number))
assert_string_contains(header_text, String(state.floor_state.room_at(state.player.cell).id))
assert_string_contains(header_text, str(state.explored_cells.size()))
assert_string_contains(character_text, str(state.player.hit_points))
_assert_controls_enclosed(top_bar, header_values, "TopBar")
_assert_controls_enclosed(left_rail, character_values, "LeftRail")
state.player.cell = Vector2i(35, 7)
hud.refresh(state, [], run)
assert_eq(prompt.text, hud.get_context_prompt())
assert_string_contains(prompt.text, "Ouvrir la porte")
assert_true(top_bar.get_global_rect().encloses(player_name.get_global_rect()))
assert_true(top_bar.get_global_rect().encloses(floor.get_global_rect()))
assert_true(top_bar.get_global_rect().encloses(location.get_global_rect()))
assert_true(top_bar.get_global_rect().encloses(exploration.get_global_rect()))
assert_true(command_bar.get_global_rect().encloses(prompt.get_global_rect()))
assert_string_contains(_combined_label_text(command_hints), hud.get_context_prompt())
assert_string_contains(_combined_label_text(command_hints), "Ouvrir la porte")
_assert_controls_enclosed(command_bar, command_hints, "CommandBar")
func test_long_deadline_text_wraps_and_stays_inside_the_left_rail() -> void:
var hud := game.get_node("HUD") as GameHud
var hud := game.get_node_or_null("HUD") as GameHud
assert_not_null(hud, "Game scene requires HUD")
if hud == null:
return
var state := game.get_game_state()
var run := game.get_run_state()
run.current_floor = 1
@@ -178,10 +218,51 @@ func test_long_deadline_text_wraps_and_stays_inside_the_left_rail() -> void:
await get_tree().process_frame
var left_rail := hud.get_node_or_null("ResponsiveLayout/LeftRail") as Control
var deadline := hud.get_node("DeadlineLabel") as Label
var deadline := hud.get_node_or_null("DeadlineLabel") as Label
assert_not_null(left_rail, "ResponsiveLayout requires a LeftRail")
if left_rail == null:
assert_not_null(deadline, "HUD requires DeadlineLabel")
if left_rail == null or deadline == null:
return
assert_true(left_rail.get_global_rect().encloses(deadline.get_global_rect()))
assert_lte(deadline.get_minimum_size().x, deadline.size.x)
assert_gt(deadline.size.y, deadline.get_line_height())
func _assert_play_area_is_centered_and_inside_effective_viewport(play_area_frame: Control) -> void:
var viewport_rect := get_viewport().get_visible_rect()
var expected_viewport := Vector2(Game.REFERENCE_VIEWPORT)
var play_rect := play_area_frame.get_global_rect()
assert_eq(viewport_rect.size, expected_viewport, "HUD layout is evaluated in the effective 1920x1080 viewport")
assert_gte(play_rect.position.x, viewport_rect.position.x, "PlayAreaFrame must stay inside the viewport on the left")
assert_gte(play_rect.position.y, viewport_rect.position.y, "PlayAreaFrame must stay inside the viewport at the top")
assert_lte(play_rect.end.x, viewport_rect.end.x, "PlayAreaFrame must stay inside the viewport on the right")
assert_lte(play_rect.end.y, viewport_rect.end.y, "PlayAreaFrame must stay inside the viewport at the bottom")
assert_gte(play_rect.size.x, viewport_rect.size.x * 0.40, "PlayAreaFrame must have meaningful viewport width")
assert_gte(play_rect.size.y, viewport_rect.size.y * 0.40, "PlayAreaFrame must have meaningful viewport height")
assert_gte(play_rect.get_area(), viewport_rect.get_area() * 0.25, "PlayAreaFrame must occupy a meaningful part of the viewport")
assert_lte(play_rect.get_center().distance_to(viewport_rect.get_center()), 2.0, "PlayAreaFrame must be centered in the effective viewport")
func _assert_controls_enclosed(region: Control, controls: Array, region_name: String) -> void:
for live_control: Control in controls:
assert_not_null(live_control, "%s must retain its live HUD control" % region_name)
if live_control != null:
assert_true(region.get_global_rect().encloses(live_control.get_global_rect()), "%s must enclose %s" % [region_name, live_control.get_path()])
func _controls_in_group(hud: GameHud, group_name: StringName) -> Array[Control]:
var controls: Array[Control] = []
for node in get_tree().get_nodes_in_group(group_name):
if node is Control and hud.is_ancestor_of(node):
controls.append(node as Control)
return controls
func _labels_in_group(hud: GameHud, group_name: StringName) -> Array[Label]:
var labels: Array[Label] = []
for node in _controls_in_group(hud, group_name):
if node is Label:
labels.append(node as Label)
return labels
func _combined_label_text(labels: Array[Label]) -> String:
var text := ""
for label in labels:
text += label.text + "\n"
return text