rinkplanner/rinkplanner/tests/run_history_tests.gd
torsten 3ca794194f feat: Segmente beschriften und Labels ein-/ausblenden
- Freitext-Label pro PathSegment (max. 40 Zeichen), getrimmt, mit
  Undo/Redo, Dirty-State und Persistenz
- Label-Dialog für das ausgewählte Segment; leeren entfernt die
  Beschriftung, Enter übernimmt direkt
- Globaler Schalter „Labels“ in der Timing-Leiste blendet alle
  Beschriftungen in der Ansicht aus
- Canvas zeichnet Labels am Segmentmittelpunkt mit Pill-Hintergrund,
  ausgewähltes Segment mit Akzentrahmen; Präsentation zeigt sie
- SVG-Export mit weiß umrandeten Labels, PNG-Export immer mit Labels
- Esc hebt die Segmentauswahl auf
- Tests: Label-API, Dialog, Undo/Redo, Persistenz, SVG-Ausgabe
- Manueller Visual-Check für Labels (tests/manual/capture_labels.gd)
2026-10-30 10:34:21 +01:00

271 lines
8.2 KiB
GDScript

extends SceneTree
## Headless tests for the snapshot-based undo/redo history.
##
## Run with:
## godot --headless --path rinkplanner --script res://tests/run_history_tests.gd
const WhiteboardScene := preload("res://scenes/whiteboard/whiteboard.tscn")
var _assertions: int = 0
var _failures: PackedStringArray = PackedStringArray()
func _init() -> void:
_run.call_deferred()
func _run() -> void:
var scene := WhiteboardScene.instantiate()
var controller := scene as WhiteboardController
if controller != null:
controller.restore_autosave = false
controller.autosave_enabled = false
root.add_child(scene)
await process_frame
_check(controller != null, "history scene has a controller")
if controller == null:
_finish()
return
_check(controller.undo_button.disabled, "undo starts disabled")
_check(controller.redo_button.disabled, "redo starts disabled")
var section := controller.get_active_section()
controller.add_segment(
Vector2(-10.0, 0.0),
Vector2(0.0, 0.0),
PathSegment.Direction.FORWARD,
PathSegment.Edge.OUTSIDE
)
_check(section.segments.size() == 1, "segment was added")
_check(not controller.undo_button.disabled, "undo is enabled after a change")
_check(controller.redo_button.disabled, "redo stays disabled after a change")
_check(controller.undo(), "undo returns true")
_check(
controller.get_active_section().segments.is_empty(),
"undo removes the added segment"
)
_check(controller.undo_button.disabled, "undo disables at history start")
_check(not controller.redo_button.disabled, "redo is enabled after undo")
_check(controller.redo(), "redo returns true")
_check(
controller.get_active_section().segments.size() == 1,
"redo restores the segment"
)
_check(controller.redo_button.disabled, "redo disables at history end")
_check(not controller.undo_button.disabled, "undo is enabled again")
controller.undo_button.pressed.emit()
_check(
controller.get_active_section().segments.is_empty(),
"undo button triggers undo"
)
controller.redo_button.pressed.emit()
_check(
controller.get_active_section().segments.size() == 1,
"redo button triggers redo"
)
var restored := controller.get_active_section().segments[0]
controller.select_segment(restored.segment_id)
controller.inside_button.pressed.emit()
_check(
restored.edge == PathSegment.Edge.INSIDE,
"edge change is applied"
)
var origin_before := controller.rink_canvas.get_view_transform().origin_px
_check(controller.undo(), "undo edge change")
_check_vec(
controller.rink_canvas.get_view_transform().origin_px,
origin_before,
"undo preserves the current pan/zoom view"
)
restored = controller.get_active_section().segments[0]
_check(
restored.edge == PathSegment.Edge.OUTSIDE,
"undo restores the edge"
)
_check(controller.redo(), "redo edge change")
restored = controller.get_active_section().segments[0]
_check(
restored.edge == PathSegment.Edge.INSIDE,
"redo reapplies the edge"
)
var first_section_id := controller.get_active_section().section_id
var second := controller.add_section()
_check(
controller.get_routine().sections.size() == 2,
"section was added"
)
_check(controller.undo(), "undo section add")
_check(
controller.get_routine().sections.size() == 1,
"undo removes the added section"
)
_check(
controller.get_active_section().section_id == first_section_id,
"undo restores the previous active section"
)
_check(controller.redo(), "redo section add")
_check(
controller.get_routine().sections.size() == 2,
"redo restores the section"
)
_check(
controller.get_active_section().section_id == second.section_id,
"redo restores the added section as active"
)
controller.set_active_section(first_section_id)
var old_title := controller.get_active_section().title
controller.rename_section(first_section_id, "Eröffnung")
_check(
controller.get_active_section().title == "Eröffnung",
"section rename is applied"
)
_check(controller.undo(), "undo section rename")
_check(
controller.get_active_section().title == old_title,
"undo restores the section title"
)
_check(controller.redo(), "redo section rename")
_check(
controller.get_active_section().title == "Eröffnung",
"redo reapplies the section title"
)
var canvas := controller.rink_canvas
var view := canvas.get_view_transform()
var count_before_draw := controller.get_active_section().segments.size()
controller.set_tool(WhiteboardController.Tool.LINE)
_mouse_button(canvas, view.model_to_screen(Vector2(-15.0, 5.0)), true)
_mouse_motion(canvas, view.model_to_screen(Vector2(-5.0, 5.0)))
_mouse_button(canvas, view.model_to_screen(Vector2(-5.0, 5.0)), false)
_check(
controller.get_active_section().segments.size() == count_before_draw + 1,
"drawing gesture adds a segment"
)
_check(controller.undo(), "undo drawing gesture")
_check(
controller.get_active_section().segments.size() == count_before_draw,
"undo removes the drawn segment"
)
_check(controller.redo(), "redo drawing gesture")
_check(
controller.get_active_section().segments.size() == count_before_draw + 1,
"redo restores the drawn segment"
)
var arc := controller.add_segment(
Vector2(0.0, -5.0),
Vector2(10.0, -5.0),
PathSegment.Direction.FORWARD,
PathSegment.Edge.OUTSIDE,
WhiteboardController.Tool.ARC_LEFT
)
controller.select_segment(arc.segment_id)
var old_start := arc.curve.get_point_position(0)
var new_start := Vector2(-3.0, -6.0)
_mouse_button(canvas, view.model_to_screen(old_start), true)
_mouse_motion(canvas, view.model_to_screen(new_start))
_mouse_button(canvas, view.model_to_screen(new_start), false)
var edited := controller.get_active_section().get_segment_by_id(arc.segment_id)
_check(edited != null, "arc is still present after handle drag")
if edited != null:
_check_vec(
edited.curve.get_point_position(0),
new_start,
"handle drag moves the arc start"
)
_check(controller.undo(), "undo handle drag")
var undone := controller.get_active_section().get_segment_by_id(arc.segment_id)
if undone != null:
_check_vec(
undone.curve.get_point_position(0),
old_start,
"undo restores the arc start"
)
_check(controller.redo(), "redo handle drag")
var redone := controller.get_active_section().get_segment_by_id(arc.segment_id)
if redone != null:
_check_vec(
redone.curve.get_point_position(0),
new_start,
"redo reapplies the arc start"
)
controller.select_segment(arc.segment_id)
_check(
controller.get_selected_segment() != null,
"segment can be selected for labelling"
)
controller.set_selected_segment_label("Eingang")
var labelled := controller.get_selected_segment()
_check(
labelled != null and labelled.label == "Eingang",
"label edit is applied"
)
_check(controller.undo(), "undo label edit")
var unlabelled := controller.get_active_section().get_segment_by_id(arc.segment_id)
_check(
unlabelled != null and unlabelled.label.is_empty(),
"undo clears the label"
)
_check(controller.redo(), "redo label edit")
var relabelled := controller.get_active_section().get_segment_by_id(arc.segment_id)
_check(
relabelled != null and relabelled.label == "Eingang",
"redo restores the label"
)
controller.new_routine()
_check(controller.undo_button.disabled, "new routine clears undo history")
_check(controller.redo_button.disabled, "new routine clears redo history")
scene.queue_free()
await process_frame
_finish()
func _mouse_button(canvas: RinkCanvas, position: Vector2, pressed: bool) -> void:
var event := InputEventMouseButton.new()
event.button_index = MOUSE_BUTTON_LEFT
event.pressed = pressed
event.position = position
canvas._gui_input(event)
func _mouse_motion(canvas: RinkCanvas, position: Vector2) -> void:
var event := InputEventMouseMotion.new()
event.position = position
canvas._gui_input(event)
func _check(condition: bool, message: String) -> void:
_assertions += 1
if not condition:
_failures.append(message)
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)