rinkplanner/rinkplanner/tests/run_model_tests.gd
torsten c609f065dd feat: Bogenparameter nachträglich bearbeiten
- Auswahlmodus mit Start-, End- und Bogen-Griff
- Bogen-Griff ändert Tiefe und Seite, Endpunkte verschieben die Kurve
- Bogen-Metadaten (Template, Tiefe, Seite) am PathSegment gespeichert
- ArcGeometry als eigenständige, headless testbare Geometrie
- Ghost-Sections bleiben nicht interaktiv
- Tests auf 203 Assertions erweitert
2026-10-30 07:42:43 +01:00

290 lines
9.2 KiB
GDScript

extends SceneTree
## Headless tests for the Phase 1 data model.
##
## Run with:
## godot --headless --path rinkplanner --script res://tests/run_model_tests.gd
const RinkDimensionsModel := preload("res://src/core/model/rink_dimensions.gd")
const PathSegmentModel := preload("res://src/core/model/path_segment.gd")
const RoutineSectionModel := preload("res://src/core/model/routine_section.gd")
const RoutineModel := preload("res://src/core/model/routine.gd")
var _assertions: int = 0
var _failures: PackedStringArray = PackedStringArray()
func _init() -> void:
_test_rink_geometry()
_test_segment_timing()
_test_section_aggregation()
_test_routine_validation()
_finish()
func _test_rink_geometry() -> void:
var rink: RinkDimensions = RinkDimensionsModel.new()
_check(rink.is_valid(), "default rink dimensions are valid")
_check_approx(rink.get_bounds().size.x, 60.0, "rink length is 60 m")
_check_approx(rink.get_bounds().size.y, 30.0, "rink width is 30 m")
_check(rink.contains_point(Vector2.ZERO), "rink center is inside")
_check(rink.contains_point(Vector2(29.0, 0.0)), "point near long board is inside")
_check(not rink.contains_point(Vector2(30.5, 0.0)), "point beyond long board is outside")
_check(
not rink.contains_point(Vector2(29.5, 14.5)),
"point in the rounded corner cut-off is outside"
)
_check(
rink.contains_point(Vector2(27.0, 12.0)),
"point inside the rounded corner arc is inside"
)
var inside_curve := _make_curve(Vector2(-5.0, 0.0), Vector2(5.0, 0.0))
_check(rink.contains_curve(inside_curve), "curve inside the rink is accepted")
var outside_curve := _make_curve(Vector2(-35.0, 0.0), Vector2(35.0, 0.0))
_check(not rink.contains_curve(outside_curve), "curve leaving the rink is rejected")
_check_vec(
rink.clamp_point(Vector2(1.0, 2.0)),
Vector2(1.0, 2.0),
"clamp leaves points inside the rink unchanged"
)
_check_vec(
rink.clamp_point(Vector2(35.0, 0.0)),
Vector2(30.0, 0.0),
"clamp moves a point outside the long board onto the board"
)
_check_vec(
rink.clamp_point(Vector2(0.0, 20.0)),
Vector2(0.0, 15.0),
"clamp moves a point outside the short board onto the board"
)
var clamped_corner := rink.clamp_point(Vector2(35.0, 20.0))
_check_approx(
clamped_corner.distance_to(Vector2(21.5, 6.5)),
8.5,
"clamp projects corner points onto the corner arc"
)
_check(
rink.contains_point(clamped_corner),
"clamped corner point lies on the rink surface"
)
var rectangular: RinkDimensions = RinkDimensionsModel.new()
rectangular.corner_radius_m = 0.0
_check_vec(
rectangular.clamp_point(Vector2(35.0, 20.0)),
Vector2(30.0, 15.0),
"clamp falls back to the rectangle without rounded corners"
)
func _test_segment_timing() -> void:
var segment: PathSegment = PathSegmentModel.new()
segment.segment_id = "seg-straight"
segment.curve = _make_curve(Vector2(-5.0, 0.0), Vector2(5.0, 0.0))
segment.duration_seconds = 5.0
_check(segment.is_valid(), "straight segment with duration is valid")
_check_approx(segment.get_length_m(), 10.0, "straight segment length is 10 m")
_check_approx(segment.get_speed_mps(), 2.0, "speed is derived as length / duration")
_check(not segment.is_marker(), "segment with length is not a marker")
var marker: PathSegment = PathSegmentModel.new()
marker.segment_id = "seg-marker"
marker.curve = _make_curve(Vector2(0.0, 0.0), Vector2(0.0, 0.0))
marker.duration_seconds = 0.0
_check(marker.is_valid(), "zero-length marker without duration is valid")
_check(marker.is_marker(), "zero-length segment reports as marker")
_check_approx(marker.get_speed_mps(), 0.0, "marker speed is 0")
var missing_duration: PathSegment = PathSegmentModel.new()
missing_duration.segment_id = "seg-missing-duration"
missing_duration.curve = _make_curve(Vector2(-5.0, 0.0), Vector2(5.0, 0.0))
missing_duration.duration_seconds = 0.0
_check(
not missing_duration.is_valid(),
"segment with length but no duration is invalid"
)
var arc_segment: PathSegment = PathSegmentModel.new()
arc_segment.segment_id = "seg-arc"
arc_segment.curve = _make_curve(Vector2(-5.0, 0.0), Vector2(5.0, 0.0))
arc_segment.duration_seconds = 2.0
arc_segment.template = PathSegment.Template.ARC
arc_segment.arc_depth_ratio = 0.4
_check(arc_segment.is_arc(), "arc template reports as arc")
_check(arc_segment.is_valid(), "arc with a valid depth ratio is valid")
arc_segment.arc_depth_ratio = 0.5
_check(
not arc_segment.is_valid(),
"arc depth ratio at or above 0.5 is invalid"
)
func _test_section_aggregation() -> void:
var section: RoutineSection = RoutineSectionModel.new()
section.section_id = "section-steps"
section.sequence_order = 0
section.segments = [
_make_segment("seg-1", 4.0, Vector2(-5.0, 0.0), Vector2(0.0, 0.0)),
_make_segment("seg-2", 2.5, Vector2(0.0, 0.0), Vector2(5.0, 0.0)),
]
_check(section.is_valid(), "section with two valid segments is valid")
_check_approx(section.get_duration_seconds(), 6.5, "section duration is the sum of segments")
_check_approx(section.get_length_m(), 10.0, "section length is the sum of segments")
_check(
section.get_segment_by_id("seg-2") != null,
"segment can be looked up by id"
)
_check(
section.get_segment_by_id("missing") == null,
"unknown segment id returns null"
)
_check(
section.get_segment_by_id("seg-2") == section.segments[1],
"segment lookup returns the matching instance"
)
func _test_routine_validation() -> void:
var routine: Routine = _make_routine()
_check(routine.is_valid(), "complete routine is valid")
_check(routine.validate().is_empty(), "valid routine reports no errors")
_check_approx(routine.get_duration_seconds(), 9.5, "routine duration sums all sections")
_check_approx(routine.get_length_m(), 15.0, "routine length sums all segments")
_check(
routine.get_ordered_sections()[0].section_id == "section-opening",
"sections are ordered by sequence_order"
)
_check(
routine.get_section_by_id("section-steps") != null,
"section can be looked up by id"
)
var outside_routine: Routine = _make_routine()
outside_routine.sections[0].segments[0].curve = _make_curve(
Vector2(-35.0, 0.0),
Vector2(35.0, 0.0)
)
_check(
not outside_routine.is_valid(),
"routine with a segment outside the rink is invalid"
)
_check(
_contains_error(outside_routine.validate(), "leaves the rink surface"),
"outside-rink error is reported"
)
var duplicate_section: Routine = _make_routine()
duplicate_section.sections[1].section_id = duplicate_section.sections[0].section_id
_check(
_contains_error(duplicate_section.validate(), "duplicate section_id"),
"duplicate section ids are reported"
)
var duplicate_order: Routine = _make_routine()
duplicate_order.sections[1].sequence_order = duplicate_order.sections[0].sequence_order
_check(
_contains_error(duplicate_order.validate(), "duplicate sequence_order"),
"duplicate sequence orders are reported"
)
var duplicate_segment: Routine = _make_routine()
duplicate_segment.sections[1].segments[0].segment_id = "seg-opening-1"
_check(
_contains_error(duplicate_segment.validate(), "duplicate segment_id"),
"duplicate segment ids across sections are reported"
)
var empty_routine: Routine = RoutineModel.new()
_check(not empty_routine.is_valid(), "routine without id and sections is invalid")
func _make_routine() -> Routine:
var routine: Routine = RoutineModel.new()
routine.routine_id = "routine-test"
routine.title = "Test Routine"
routine.rink = RinkDimensionsModel.new()
var opening: RoutineSection = RoutineSectionModel.new()
opening.section_id = "section-opening"
opening.sequence_order = 0
opening.segments = [
_make_segment("seg-opening-1", 4.0, Vector2(-5.0, 0.0), Vector2(0.0, 0.0)),
_make_segment("seg-opening-2", 2.5, Vector2(0.0, 0.0), Vector2(5.0, 0.0)),
]
var steps: RoutineSection = RoutineSectionModel.new()
steps.section_id = "section-steps"
steps.sequence_order = 1
steps.segments = [
_make_segment("seg-steps-1", 3.0, Vector2(5.0, 0.0), Vector2(5.0, 5.0)),
]
routine.sections = [opening, steps]
return routine
func _make_segment(
segment_id: String,
duration_seconds: float,
from: Vector2,
to: Vector2
) -> PathSegment:
var segment: PathSegment = PathSegmentModel.new()
segment.segment_id = segment_id
segment.curve = _make_curve(from, to)
segment.duration_seconds = duration_seconds
return segment
func _make_curve(from: Vector2, to: Vector2) -> Curve2D:
var curve := Curve2D.new()
curve.add_point(from)
curve.add_point(to)
return curve
func _contains_error(errors: PackedStringArray, fragment: String) -> bool:
for error: String in errors:
if error.contains(fragment):
return true
return false
func _check(condition: bool, message: String) -> void:
_assertions += 1
if not condition:
_failures.append(message)
func _check_approx(actual: float, expected: float, message: String) -> void:
_check(
absf(actual - expected) <= 0.001,
"%s (expected %.3f, got %.3f)" % [message, expected, actual]
)
func _check_vec(actual: Vector2, expected: Vector2, message: String) -> void:
_check(
actual.distance_to(expected) <= 0.001,
"%s (expected %s, got %s)" % [message, expected, actual]
)
func _finish() -> void:
if _failures.is_empty():
print("PASS: %d assertions" % _assertions)
quit(0)
return
for failure: String in _failures:
printerr("FAIL: %s" % failure)
printerr("%d of %d assertions failed" % [_failures.size(), _assertions])
quit(1)