mirror of
https://github.com/deunix-educ/aventuriers-des-mots.git
synced 2026-10-13 02:11:58 +02:00
Add profile and save persistence, wired into the three autoloads
scripts/profiles/: SaveIO handles real file I/O in user://saves (one file per profile_id, atomic write via temp-file-then-rename, backup copy before each overwrite, corrupted-main falls back to backup, schema-invalid content is rejected like corruption). SaveMigration and SaveFactory are pure; SaveMigration refuses a schema_version it doesn't recognize (older or newer) rather than guessing. ProgressUpdates is pure and wires ReviewScheduler/DifficultyManager onto a save's skills dict, including per-session overdue-review marking. The three autoloads (SaveManager, ProfileManager, ProgressManager) stay thin: in-memory current-profile state and orchestration only, no logic of their own, so the actual behavior lives in the tested pure/file-IO modules above rather than in untestable glue. 26 new headless tests, including the failure modes called out in the launch prompt: a truncated .tmp file never corrupts the readable save, a corrupted main file falls back to its backup, two profiles never mix (down through the real autoload layer, not just SaveIO), and an unknown future schema_version is rejected outright. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
extends GutTest
|
||||
## Tests d'integration des autoloads ProfileManager / SaveManager /
|
||||
## ProgressManager : la "colle" fine entre la logique pure (deja testee
|
||||
## isolement) et la persistance reelle sur disque.
|
||||
|
||||
var created_profile_ids: Array[String] = []
|
||||
|
||||
|
||||
func after_each() -> void:
|
||||
for profile_id in created_profile_ids:
|
||||
ProfileManager.delete_profile(profile_id)
|
||||
created_profile_ids.clear()
|
||||
|
||||
|
||||
func test_create_and_select_profile_round_trips_through_disk() -> void:
|
||||
var profile_id := ProfileManager.create_profile("Explorateur", "CE1")
|
||||
created_profile_ids.append(profile_id)
|
||||
|
||||
assert_true(ProfileManager.select_profile(profile_id))
|
||||
assert_eq(ProfileManager.current_profile_id, profile_id)
|
||||
|
||||
ProgressManager.record_attempt("accord_groupe_nominal", true, false, "multiple_choice")
|
||||
assert_true(ProgressManager.save_now())
|
||||
|
||||
var reloaded: Variant = SaveManager.load(profile_id)
|
||||
assert_not_null(reloaded)
|
||||
assert_true(reloaded["skills"].has("accord_groupe_nominal"))
|
||||
|
||||
|
||||
func test_selecting_unknown_profile_fails_without_side_effects() -> void:
|
||||
assert_false(ProfileManager.select_profile("profile_does_not_exist"))
|
||||
|
||||
|
||||
func test_two_profiles_are_isolated_through_the_autoload_layer() -> void:
|
||||
var profile_a := ProfileManager.create_profile("Alice", "CE1")
|
||||
var profile_b := ProfileManager.create_profile("Bob", "CE2")
|
||||
created_profile_ids.append(profile_a)
|
||||
created_profile_ids.append(profile_b)
|
||||
|
||||
ProfileManager.select_profile(profile_a)
|
||||
ProgressManager.record_attempt("accord_groupe_nominal", true, false, "multiple_choice")
|
||||
ProgressManager.save_now()
|
||||
|
||||
ProfileManager.select_profile(profile_b)
|
||||
ProgressManager.record_attempt("homophones", false, true, "sentence_correction")
|
||||
ProgressManager.save_now()
|
||||
|
||||
var save_a: Variant = SaveManager.load(profile_a)
|
||||
var save_b: Variant = SaveManager.load(profile_b)
|
||||
assert_true(save_a["skills"].has("accord_groupe_nominal"))
|
||||
assert_false(save_a["skills"].has("homophones"))
|
||||
assert_true(save_b["skills"].has("homophones"))
|
||||
assert_false(save_b["skills"].has("accord_groupe_nominal"))
|
||||
|
||||
|
||||
func test_delete_profile_removes_it_from_disk_and_listing() -> void:
|
||||
var profile_id := ProfileManager.create_profile("Explorateur", "CE1")
|
||||
|
||||
assert_true(ProfileManager.list_profiles().has(profile_id))
|
||||
|
||||
ProfileManager.delete_profile(profile_id)
|
||||
|
||||
assert_false(ProfileManager.list_profiles().has(profile_id))
|
||||
assert_null(SaveManager.load(profile_id))
|
||||
@@ -0,0 +1 @@
|
||||
uid://cx1dljkp8ilqk
|
||||
@@ -0,0 +1,83 @@
|
||||
extends GutTest
|
||||
## Tests de ProgressUpdates : application pure des tentatives et des debuts de
|
||||
## seance a une sauvegarde.
|
||||
|
||||
|
||||
func _base_save() -> Dictionary:
|
||||
return SaveFactory.new_save("profile_test", "Explorateur", "CE1")
|
||||
|
||||
|
||||
func test_record_attempt_creates_skill_state_on_first_attempt() -> void:
|
||||
var data := _base_save()
|
||||
var attempt := {
|
||||
"skill": "accord_groupe_nominal",
|
||||
"success": true,
|
||||
"hint_used": false,
|
||||
"exercise_type": "multiple_choice",
|
||||
}
|
||||
var updated := ProgressUpdates.record_attempt(data, attempt)
|
||||
assert_true(updated["skills"].has("accord_groupe_nominal"))
|
||||
assert_eq(
|
||||
updated["skills"]["accord_groupe_nominal"]["status"], ReviewScheduler.STATUS_EN_ACQUISITION
|
||||
)
|
||||
|
||||
|
||||
func test_record_attempt_uses_current_session_count() -> void:
|
||||
var data := _base_save()
|
||||
data["session_count"] = 5
|
||||
var attempt := {
|
||||
"skill": "accord_groupe_nominal",
|
||||
"success": true,
|
||||
"hint_used": false,
|
||||
"exercise_type": "multiple_choice",
|
||||
}
|
||||
var updated := ProgressUpdates.record_attempt(data, attempt)
|
||||
var skill_state: Dictionary = updated["skills"]["accord_groupe_nominal"]
|
||||
assert_eq(skill_state["last_session_practiced"], 5)
|
||||
|
||||
|
||||
func test_record_attempt_does_not_mutate_input() -> void:
|
||||
var data := _base_save()
|
||||
var attempt := {
|
||||
"skill": "accord_groupe_nominal",
|
||||
"success": true,
|
||||
"hint_used": false,
|
||||
"exercise_type": "multiple_choice",
|
||||
}
|
||||
ProgressUpdates.record_attempt(data, attempt)
|
||||
assert_eq(data["skills"], {})
|
||||
|
||||
|
||||
func test_start_session_increments_session_count() -> void:
|
||||
var data := _base_save()
|
||||
var updated := ProgressUpdates.start_session(data)
|
||||
assert_eq(updated["session_count"], 1)
|
||||
updated = ProgressUpdates.start_session(updated)
|
||||
assert_eq(updated["session_count"], 2)
|
||||
|
||||
|
||||
func test_start_session_marks_overdue_skills_as_a_reviser() -> void:
|
||||
var data := _base_save()
|
||||
var acquise_state: Dictionary = ReviewScheduler.initial_state()
|
||||
acquise_state["status"] = ReviewScheduler.STATUS_ACQUISE
|
||||
acquise_state["next_review_session"] = 2
|
||||
data["skills"] = {"accord_groupe_nominal": acquise_state}
|
||||
data["session_count"] = 1
|
||||
|
||||
var updated := ProgressUpdates.start_session(data)
|
||||
assert_eq(updated["session_count"], 2)
|
||||
assert_eq(
|
||||
updated["skills"]["accord_groupe_nominal"]["status"], ReviewScheduler.STATUS_A_REVISER
|
||||
)
|
||||
|
||||
|
||||
func test_unlock_region_adds_region_once() -> void:
|
||||
var data := _base_save()
|
||||
data = ProgressUpdates.unlock_region(data, "foret_des_accords")
|
||||
data = ProgressUpdates.unlock_region(data, "foret_des_accords")
|
||||
assert_eq(data["unlocked_regions"], ["foret_des_accords"])
|
||||
|
||||
|
||||
func test_next_difficulty_for_unknown_skill_returns_current_difficulty() -> void:
|
||||
var data := _base_save()
|
||||
assert_eq(ProgressUpdates.next_difficulty_for_skill(data, "inconnue", 2), 2)
|
||||
@@ -0,0 +1 @@
|
||||
uid://cde72oleqofin
|
||||
@@ -0,0 +1,23 @@
|
||||
extends GutTest
|
||||
## Tests de SaveFactory : une nouvelle sauvegarde doit etre conforme a
|
||||
## data/schemas/save.schema.json.
|
||||
|
||||
var save_schema: Dictionary = {}
|
||||
|
||||
|
||||
func before_all() -> void:
|
||||
save_schema = DataLoader.load_schema("save")
|
||||
|
||||
|
||||
func test_new_save_is_valid_against_the_save_schema() -> void:
|
||||
var data := SaveFactory.new_save("profile_abc123", "Explorateur", "CE1")
|
||||
var errors := SchemaValidator.validate(data, save_schema)
|
||||
assert_eq(errors, [])
|
||||
|
||||
|
||||
func test_new_save_starts_at_session_zero_with_no_skills() -> void:
|
||||
var data := SaveFactory.new_save("profile_abc123", "Explorateur", "CE1")
|
||||
assert_eq(data["session_count"], 0)
|
||||
assert_eq(data["skills"], {})
|
||||
assert_eq(data["unlocked_regions"], [])
|
||||
assert_eq(data["rewards"], {"stars": 0, "items": []})
|
||||
@@ -0,0 +1 @@
|
||||
uid://bsf48s4utme77
|
||||
@@ -0,0 +1,122 @@
|
||||
extends GutTest
|
||||
## Tests de SaveIO : ecriture atomique, copie de secours, isolation entre
|
||||
## profils. Utilise de vrais chemins user:// (headless Godot les fournit),
|
||||
## avec des profile_id de test nettoyes avant et apres la suite.
|
||||
|
||||
const PROFILE_IDS: Array[String] = [
|
||||
"test_save_io_roundtrip",
|
||||
"test_save_io_profile_a",
|
||||
"test_save_io_profile_b",
|
||||
"test_save_io_truncated_tmp",
|
||||
"test_save_io_corrupted_main",
|
||||
"test_save_io_invalid_content",
|
||||
"test_save_io_missing",
|
||||
"test_save_io_delete",
|
||||
]
|
||||
|
||||
var save_schema: Dictionary = {}
|
||||
|
||||
|
||||
func before_all() -> void:
|
||||
save_schema = DataLoader.load_schema("save")
|
||||
for profile_id in PROFILE_IDS:
|
||||
SaveIO.delete(profile_id)
|
||||
|
||||
|
||||
func after_all() -> void:
|
||||
for profile_id in PROFILE_IDS:
|
||||
SaveIO.delete(profile_id)
|
||||
|
||||
|
||||
func test_write_then_read_roundtrip() -> void:
|
||||
var data := SaveFactory.new_save("test_save_io_roundtrip", "Explorateur", "CE1")
|
||||
data["session_count"] = 3
|
||||
assert_true(SaveIO.write("test_save_io_roundtrip", data))
|
||||
var read_back: Variant = SaveIO.read("test_save_io_roundtrip", save_schema)
|
||||
assert_eq(int(read_back["session_count"]), 3)
|
||||
assert_eq(read_back["profile_id"], "test_save_io_roundtrip")
|
||||
|
||||
|
||||
func test_two_profiles_do_not_mix() -> void:
|
||||
var data_a := SaveFactory.new_save("test_save_io_profile_a", "Alice", "CE1")
|
||||
data_a["session_count"] = 1
|
||||
var data_b := SaveFactory.new_save("test_save_io_profile_b", "Bob", "CE2")
|
||||
data_b["session_count"] = 7
|
||||
|
||||
SaveIO.write("test_save_io_profile_a", data_a)
|
||||
SaveIO.write("test_save_io_profile_b", data_b)
|
||||
|
||||
var read_a: Variant = SaveIO.read("test_save_io_profile_a", save_schema)
|
||||
var read_b: Variant = SaveIO.read("test_save_io_profile_b", save_schema)
|
||||
assert_eq(read_a["display_name"], "Alice")
|
||||
assert_eq(int(read_a["session_count"]), 1)
|
||||
assert_eq(read_b["display_name"], "Bob")
|
||||
assert_eq(int(read_b["session_count"]), 7)
|
||||
|
||||
|
||||
func test_missing_profile_returns_null() -> void:
|
||||
assert_null(SaveIO.read("test_save_io_missing", save_schema))
|
||||
|
||||
|
||||
func test_truncated_tmp_file_does_not_corrupt_the_readable_save() -> void:
|
||||
# Simule un plantage pendant l'ecriture : le fichier temporaire existe,
|
||||
# tronque et invalide, mais n'a jamais ete renomme sur le fichier
|
||||
# principal. La derniere sauvegarde valide doit rester lisible.
|
||||
var profile_id := "test_save_io_truncated_tmp"
|
||||
var data := SaveFactory.new_save(profile_id, "Explorateur", "CE1")
|
||||
SaveIO.write(profile_id, data)
|
||||
|
||||
var tmp_path := "%s/%s.json.tmp" % [SaveIO.SAVE_DIR, profile_id]
|
||||
var tmp_file := FileAccess.open(tmp_path, FileAccess.WRITE)
|
||||
tmp_file.store_string('{"schema_version": 1, "profile_id": ')
|
||||
tmp_file.close()
|
||||
|
||||
var read_back: Variant = SaveIO.read(profile_id, save_schema)
|
||||
assert_not_null(read_back)
|
||||
assert_eq(read_back["profile_id"], profile_id)
|
||||
|
||||
|
||||
func test_corrupted_main_file_falls_back_to_backup() -> void:
|
||||
var profile_id := "test_save_io_corrupted_main"
|
||||
var first_save := SaveFactory.new_save(profile_id, "Explorateur", "CE1")
|
||||
first_save["session_count"] = 1
|
||||
SaveIO.write(profile_id, first_save)
|
||||
|
||||
# Deuxieme ecriture : copie la version precedente (valide) en secours
|
||||
# avant de l'ecraser.
|
||||
var second_save := first_save.duplicate(true)
|
||||
second_save["session_count"] = 2
|
||||
SaveIO.write(profile_id, second_save)
|
||||
|
||||
var main_path := "%s/%s.json" % [SaveIO.SAVE_DIR, profile_id]
|
||||
var main_file := FileAccess.open(main_path, FileAccess.WRITE)
|
||||
main_file.store_string("ceci n'est pas du json valide {{{")
|
||||
main_file.close()
|
||||
|
||||
var read_back: Variant = SaveIO.read(profile_id, save_schema)
|
||||
assert_engine_error("Parse JSON failed")
|
||||
assert_not_null(read_back)
|
||||
assert_eq(int(read_back["session_count"]), 1)
|
||||
|
||||
|
||||
func test_schema_invalid_content_without_backup_returns_null() -> void:
|
||||
var profile_id := "test_save_io_invalid_content"
|
||||
var main_path := "%s/%s.json" % [SaveIO.SAVE_DIR, profile_id]
|
||||
DirAccess.make_dir_recursive_absolute(SaveIO.SAVE_DIR)
|
||||
var main_file := FileAccess.open(main_path, FileAccess.WRITE)
|
||||
main_file.store_string(JSON.stringify({"schema_version": 1, "profile_id": profile_id}))
|
||||
main_file.close()
|
||||
|
||||
assert_null(SaveIO.read(profile_id, save_schema))
|
||||
|
||||
|
||||
func test_delete_removes_main_backup_and_tmp_files() -> void:
|
||||
var profile_id := "test_save_io_delete"
|
||||
var data := SaveFactory.new_save(profile_id, "Explorateur", "CE1")
|
||||
SaveIO.write(profile_id, data)
|
||||
SaveIO.write(profile_id, data)
|
||||
|
||||
SaveIO.delete(profile_id)
|
||||
|
||||
assert_null(SaveIO.read(profile_id, save_schema))
|
||||
assert_false(SaveIO.list_profile_ids().has(profile_id))
|
||||
@@ -0,0 +1 @@
|
||||
uid://cuh4j0qf788kx
|
||||
@@ -0,0 +1,36 @@
|
||||
extends GutTest
|
||||
## Tests de SaveMigration : garde-fous sur la version de schema.
|
||||
|
||||
|
||||
func test_current_version_is_returned_unchanged() -> void:
|
||||
var data := {"schema_version": 1, "profile_id": "profile_test"}
|
||||
var migrated := SaveMigration.migrate(data)
|
||||
assert_eq(migrated["schema_version"], 1)
|
||||
assert_eq(migrated["profile_id"], "profile_test")
|
||||
|
||||
|
||||
func test_missing_version_is_rejected() -> void:
|
||||
var data := {"profile_id": "profile_test"}
|
||||
assert_eq(SaveMigration.migrate(data), {})
|
||||
|
||||
|
||||
func test_future_unknown_version_is_rejected_rather_than_guessed() -> void:
|
||||
var data := {"schema_version": 99, "profile_id": "profile_test"}
|
||||
assert_eq(SaveMigration.migrate(data), {})
|
||||
|
||||
|
||||
func test_zero_or_negative_version_is_rejected() -> void:
|
||||
var data := {"schema_version": 0, "profile_id": "profile_test"}
|
||||
assert_eq(SaveMigration.migrate(data), {})
|
||||
|
||||
|
||||
func test_non_numeric_version_is_rejected() -> void:
|
||||
var data := {"schema_version": "one", "profile_id": "profile_test"}
|
||||
assert_eq(SaveMigration.migrate(data), {})
|
||||
|
||||
|
||||
func test_migration_does_not_mutate_the_input_in_place() -> void:
|
||||
var data := {"schema_version": 1, "profile_id": "profile_test"}
|
||||
var migrated := SaveMigration.migrate(data)
|
||||
migrated["profile_id"] = "changed"
|
||||
assert_eq(data["profile_id"], "profile_test")
|
||||
@@ -0,0 +1 @@
|
||||
uid://d0k3uwi76t0s8
|
||||
Reference in New Issue
Block a user