rinkplanner/rinkplanner/tests/run_sync_tests.gd

276 lines
8.8 KiB
GDScript

extends SceneTree
## Headless tests for the manual FTP sync: planning rules, conflict naming,
## state persistence, protocol parsing and the settings UI wiring.
##
## Run with:
## godot --headless --path rinkplanner --script res://tests/run_sync_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:
_test_plan()
_test_naming()
_test_parsers()
_test_state_roundtrip()
await _test_controller_wiring()
_finish()
func _test_plan() -> void:
var local := {
"neu.json": {"mtime": 100, "size": 10},
"lokal.json": {"mtime": 200, "size": 10},
"server.json": {"mtime": 300, "size": 10},
"beide.json": {"mtime": 400, "size": 10},
"gleich.json": {"mtime": 500, "size": 10},
"entfernt.json": {"mtime": 600, "size": 10},
}
var remote := {
"server-neu.json": {"modify": 700, "size": 10},
"lokal.json": {"modify": 200, "size": 10},
"server.json": {"modify": 900, "size": 10},
"beide.json": {"modify": 900, "size": 10},
"gleich.json": {"modify": 500, "size": 10},
"entfernt.json": {"modify": 600, "size": 10},
}
var state := {
"lokal.json": {"local_mtime": 100, "remote_modify": 200},
"server.json": {"local_mtime": 300, "remote_modify": 300},
"beide.json": {"local_mtime": 401, "remote_modify": 400},
"gleich.json": {"local_mtime": 500, "remote_modify": 500},
"entfernt.json": {"local_mtime": 600, "remote_modify": 600},
}
var plan := SyncPlan.build(local, remote, state)
var actions := {}
for entry: Dictionary in plan:
actions[entry["name"]] = entry["action"]
_check(actions.get("neu.json") == SyncPlan.Action.UPLOAD, "nur lokal wird hochgeladen")
_check(actions.get("server-neu.json") == SyncPlan.Action.DOWNLOAD, "nur Server wird geladen")
_check(
actions.get("beide.json") == SyncPlan.Action.CONFLICT,
"beidseitige Änderung ist ein Konflikt"
)
_check(actions.get("lokal.json") == SyncPlan.Action.UPLOAD, "lokale Änderung wird hochgeladen")
_check(actions.get("server.json") == SyncPlan.Action.DOWNLOAD, "Server-Änderung wird geladen")
_check(actions.get("gleich.json") == SyncPlan.Action.SKIP, "unverändert wird übersprungen")
_check(
actions.get("entfernt.json") == SyncPlan.Action.SKIP,
"Server-Datei verschwunden: nicht wiederherstellen"
)
var first_sync := SyncPlan.build(
{"a.json": {"mtime": 1, "size": 1}},
{"a.json": {"modify": 2, "size": 1}},
{}
)
_check(
first_sync.size() == 1 and first_sync[0]["action"] == SyncPlan.Action.TAKE_SERVER,
"erster Abgleich ohne Stand übernimmt die Serverfassung"
)
var local_deleted := SyncPlan.build(
{},
{"weg.json": {"modify": 2, "size": 1}},
{"weg.json": {"local_mtime": 1, "remote_modify": 2}}
)
_check(
local_deleted.size() == 1 and local_deleted[0]["action"] == SyncPlan.Action.SKIP,
"lokal gelöschte Datei wird nicht zurückgeholt"
)
func _test_naming() -> void:
_check(
SyncPlan.conflict_file_name("kuer.json", "2026-10-30 1050")
== "kuer (Server 2026-10-30 1050).json",
"Konfliktname enthält Zeitstempel"
)
var used := {"kuer (Server 2026-10-30 1050).json": true}
_check(
SyncPlan.unique_name("kuer (Server 2026-10-30 1050).json", used)
== "kuer (Server 2026-10-30 1050) 2.json",
"Konfliktname wird bei Bedarf durchnummeriert"
)
_check(
SyncPlan.unique_name("frei.json", used) == "frei.json",
"freier Name bleibt unverändert"
)
_check(
SyncPlan.local_backup_file_name("kuer.json", "2026-10-30 1050")
== "kuer (Lokal 2026-10-30 1050).json",
"lokale Sicherung trägt den Lokal-Zeitstempel"
)
_check(
SyncPlan.conflict_file_name("alt.tres", "2026-10-30 1050")
== "alt (Server 2026-10-30 1050).tres",
"Legacy-Dateien behalten ihre .tres-Endung"
)
_check(
RoutineSync.is_routine_file("kuer.json")
and RoutineSync.is_routine_file("alt.tres")
and not RoutineSync.is_routine_file("sync.cfg"),
"der Sync akzeptiert JSON und Legacy-.tres"
)
_check(
RoutineSync.is_synced_file("kuer.mp3")
and RoutineSync.is_synced_file("kuer.ogg")
and RoutineSync.is_synced_file("kuer.wav")
and not RoutineSync.is_synced_file("notizen.txt"),
"der Sync akzeptiert Musikdateien der Kür"
)
_check(
SyncPlan.conflict_file_name("kuer.mp3", "2026-10-30 1050")
== "kuer (Server 2026-10-30 1050).mp3",
"Konfliktnamen behalten die Audio-Endung"
)
var label := SyncPlan.timestamp_label(0)
_check(
label.length() == 15 and label.contains("-") and label.contains(" "),
"Zeitstempel hat das Format YYYY-MM-DD HHMM"
)
func _test_parsers() -> void:
var pasv := FtpClient.parse_pasv("227 Entering Passive Mode (10,1,2,3,200,10)")
_check(
pasv.get("host") == "10.1.2.3" and pasv.get("port") == 200 * 256 + 10,
"PASV-Antwort wird geparst"
)
_check(
FtpClient.parse_epsv("229 Entering Extended Passive Mode (|||6446|)") == 6446,
"EPSV-Antwort wird geparst"
)
_check(
FtpClient.parse_epsv("229 Entering Extended Passive Mode (|||)") == -1,
"ungültige EPSV-Antwort liefert -1"
)
var expected := int(Time.get_unix_time_from_datetime_string("2026-10-30T09:48:28"))
_check(
FtpClient.parse_mdtm("20261030094828") == expected,
"MDTM wird als Unix-Zeit gelesen"
)
_check(FtpClient.parse_mdtm("kaputt") == -1, "ungültiges MDTM liefert -1")
func _test_state_roundtrip() -> void:
var path := "user://test_sync_state.cfg"
var state := {
"kuer.json": {"local_mtime": 111, "remote_modify": 222},
"zweite.json": {"local_mtime": 333, "remote_modify": 444},
}
RoutineSync.save_state(path, state)
var loaded := RoutineSync.load_state(path)
_check(loaded.size() == 2, "Sync-Stand wird gespeichert und geladen")
_check(
int(loaded.get("kuer.json", {}).get("remote_modify", 0)) == 222,
"Sync-Stand behält die Server-Zeit"
)
DirAccess.remove_absolute(ProjectSettings.globalize_path(path))
var files := RoutineSync.local_files("user://does_not_exist")
_check(files.is_empty(), "fehlendes Verzeichnis liefert keine Dateien")
var local_dir := "user://test_sync_files"
DirAccess.make_dir_recursive_absolute(ProjectSettings.globalize_path(local_dir))
for name: String in ["kuer.json", "kuer.mp3", "notizen.txt"]:
var file := FileAccess.open(local_dir.path_join(name), FileAccess.WRITE)
file.store_string("x")
file.close()
var listed := RoutineSync.local_files(local_dir)
_check(
listed.has("kuer.json") and listed.has("kuer.mp3"),
"lokale Dateiliste enthält Kür und Musik"
)
_check(not listed.has("notizen.txt"), "lokale Dateiliste ignoriert Fremddateien")
for name: String in ["kuer.json", "kuer.mp3", "notizen.txt"]:
DirAccess.remove_absolute(
ProjectSettings.globalize_path(local_dir.path_join(name))
)
DirAccess.remove_absolute(ProjectSettings.globalize_path(local_dir))
func _test_controller_wiring() -> void:
var scene := WhiteboardScene.instantiate()
var controller := scene as WhiteboardController
controller.restore_autosave = false
controller.autosave_enabled = false
root.add_child(scene)
await process_frame
_check(controller != null, "sync scene has a controller")
if controller == null:
return
controller.save_sync_settings({
"host": "ftp.example.org",
"port": 2121,
"user": "konto",
"password": "geheim",
"directory": "rinkplanner",
"ftps": false,
"verify_certificate": false,
})
var loaded := controller.load_sync_settings()
_check(loaded["host"] == "ftp.example.org", "Sync-Einstellungen speichern den Host")
_check(int(loaded["port"]) == 2121, "Sync-Einstellungen speichern den Port")
_check(loaded["ftps"] == false, "Sync-Einstellungen speichern FTPS")
_check(
loaded["verify_certificate"] == false,
"Sync-Einstellungen speichern die Zertifikatsprüfung"
)
controller._open_sync_settings_dialog()
_check(
controller.sync_settings_dialog.visible,
"Einstellungsdialog öffnet sich"
)
_check(
controller.sync_host_edit.text == "ftp.example.org",
"Dialog ist mit den gespeicherten Werten gefüllt"
)
_check(
controller.sync_password_edit.secret,
"Passwortfeld ist verdeckt"
)
controller.sync_settings_dialog.hide()
# Ohne Host öffnet "Jetzt synchronisieren" die Einstellungen statt zu starten.
controller.save_sync_settings({"host": "", "port": 21, "user": "", "password": ""})
controller.start_sync()
_check(
controller.sync_settings_dialog.visible,
"Sync ohne Zugangsdaten öffnet die Einstellungen"
)
_check(not controller._sync_running, "ohne Zugangsdaten startet kein Sync")
controller.sync_settings_dialog.hide()
scene.queue_free()
await process_frame
func _check(condition: bool, message: String) -> void:
_assertions += 1
if not condition:
_failures.append(message)
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)