rinkplanner/rinkplanner/tests/run_model_tests.gd
torsten 6eca94e6db feat: Element-Standzeit und rotierender Pirouetten-Dot
- PathSegment.element_duration_seconds (0-120 s) fuer Punktmarker
- Typ-Defaults: Sprung 0,7 s, Pirouette 8 s, im Inspector uebersteuerbar
- Section- und Routine-Dauer sowie Timeline enthalten die Standzeiten
- Playback haelt am Marker; Pirouette dreht den Dot mit 1,5 U/s
- Tests: 599 Assertions gruen; Doku aktualisiert
2026-10-30 23:48:47 +01:00

472 lines
14 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_element_markers()
_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 timed: PathSegment = PathSegmentModel.new()
timed.segment_id = "seg-timed"
timed.curve = _make_curve(Vector2(0.0, 0.0), Vector2(10.0, 0.0))
_check_approx(timed.speed_mps, 4.3, "default segment speed is adult gold")
_check(
timed.timing_mode == PathSegment.TimingMode.SPEED,
"default timing mode is speed"
)
timed.set_speed_mps(5.0)
_check_approx(timed.duration_seconds, 2.0, "speed sets the duration")
timed.set_duration_seconds(4.0)
_check_approx(timed.get_speed_mps(), 2.5, "duration sets the speed")
_check(
timed.timing_mode == PathSegment.TimingMode.DURATION,
"duration mode is stored"
)
timed.curve.set_point_position(1, Vector2(20.0, 0.0))
timed.apply_geometry_length(20.0)
_check_approx(
timed.duration_seconds,
4.0,
"duration mode preserves the authored duration"
)
_check_approx(
timed.get_speed_mps(),
5.0,
"duration mode updates the implied speed"
)
timed.set_speed_mps(4.0)
timed.curve.set_point_position(1, Vector2(8.0, 0.0))
timed.apply_geometry_length(8.0)
_check_approx(
timed.duration_seconds,
2.0,
"speed mode preserves the authored speed"
)
_check_approx(
timed.get_speed_mps(),
4.0,
"speed mode keeps the speed after a geometry edit"
)
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_element_markers() -> void:
var jump: PathSegment = PathSegmentModel.new()
jump.segment_id = "seg-jump"
var curve := Curve2D.new()
curve.add_point(Vector2(5.0, 3.0))
jump.curve = curve
jump.duration_seconds = 0.0
jump.speed_mps = 4.3
jump.set_element(PathSegmentModel.ElementType.JUMP, " 3Lz ", 0)
_check(jump.is_marker(), "zero-length element is a marker")
_check(jump.is_element(), "element marker reports an element type")
_check(jump.is_element_marker(), "zero-length element is a point marker")
_check(jump.is_valid(), "zero-length jump marker is valid")
_check(jump.element_code == "3Lz", "element code is trimmed")
_check(
jump.get_element_caption() == "3Lz",
"jump caption is the element code"
)
_check(
PathSegmentModel.element_type_name(PathSegmentModel.ElementType.JUMP) == "Sprung",
"jump type has a German name"
)
var spin: PathSegment = PathSegmentModel.new()
spin.segment_id = "seg-spin"
spin.curve = curve
spin.duration_seconds = 0.0
spin.speed_mps = 4.3
spin.set_element(PathSegmentModel.ElementType.SPIN, "CCoSp4", 3)
_check(
spin.get_element_caption() == "CCoSp4 L3",
"spin caption includes the level"
)
_check(spin.is_valid(), "spin marker with a level is valid")
spin.set_element(PathSegmentModel.ElementType.SPIN, "", 9)
_check(spin.element_level == 4, "level is clamped to the maximum")
_check(
spin.get_element_caption() == "Pirouette L4",
"caption falls back to the type name"
)
spin.set_element_duration(9.5)
_check_approx(
spin.element_duration_seconds,
9.5,
"stand time is stored on the marker"
)
_check_approx(
spin.get_effective_duration_seconds(),
9.5,
"a point marker uses its stand time as timeline duration"
)
spin.set_element_duration(-3.0)
_check_approx(
spin.element_duration_seconds,
0.0,
"negative stand time is clamped to zero"
)
spin.element_duration_seconds = 200.0
_check(
not spin.is_valid(),
"stand time above the maximum is invalid"
)
spin.set_element_duration(8.0)
spin.set_element(
PathSegmentModel.ElementType.SEQUENCE,
"123456789012345678901234567890",
0
)
_check(
spin.element_code.length() == PathSegmentModel.ELEMENT_CODE_MAX_LENGTH,
"element code is truncated to the maximum length"
)
_check(
spin.get_element_caption() == spin.element_code,
"sequence caption uses the truncated code"
)
spin.set_element(PathSegmentModel.ElementType.NONE, "rest", 3)
_check(
spin.element_type == PathSegmentModel.ElementType.NONE
and spin.element_code.is_empty()
and spin.element_level == 0,
"clearing the element resets code and level"
)
_check(not spin.is_element(), "cleared segment is not an element")
var missing_type: PathSegment = PathSegmentModel.new()
missing_type.segment_id = "seg-code-only"
missing_type.curve = _make_curve(Vector2(0.0, 0.0), Vector2(1.0, 0.0))
missing_type.duration_seconds = 1.0
missing_type.element_code = "3Lz"
_check(
not missing_type.is_valid(),
"element code without an element type is invalid"
)
var plain: PathSegment = _make_segment(
"seg-plain",
1.0,
Vector2(0.0, 0.0),
Vector2(1.0, 0.0)
)
_check(not plain.is_element(), "plain segment has no element")
_check(not plain.is_element_marker(), "plain segment is not a point marker")
_check_approx(
plain.get_effective_duration_seconds(),
1.0,
"path segments use their normal duration on the timeline"
)
_check(
plain.get_element_caption() == "Element",
"plain segment caption falls back to the generic name"
)
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"
)
var marker: PathSegment = PathSegmentModel.new()
marker.segment_id = "seg-jump"
var marker_curve := Curve2D.new()
marker_curve.add_point(Vector2(0.0, 0.0))
marker.curve = marker_curve
marker.speed_mps = 4.3
marker.set_element(PathSegmentModel.ElementType.JUMP, "3Lz", 0)
marker.set_element_duration(0.7)
section.segments.append(marker)
_check_approx(
section.get_duration_seconds(),
7.2,
"section duration includes the stand time of markers"
)
_check_approx(section.get_length_m(), 10.0, "markers keep the section length")
func _test_routine_validation() -> void:
var routine: Routine = _make_routine()
_check(
routine.schema_version == Routine.CURRENT_SCHEMA_VERSION,
"new routines use the current schema version"
)
_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)