2026-11-10 19:53:07 -06:00
|
|
|
extends TestBase
|
2026-11-08 12:11:26 -06:00
|
|
|
## Headless checks for collections, LibraryStore and StateStore.
|
|
|
|
|
## Run: godot --headless --path . -s res://tests/test_storage.gd
|
|
|
|
|
|
|
|
|
|
const ROOT := "user://test_storage_tmp"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _init() -> void:
|
|
|
|
|
_wipe(ROOT)
|
|
|
|
|
_test_versions()
|
|
|
|
|
_test_library()
|
|
|
|
|
_test_migration()
|
|
|
|
|
_test_corrupt()
|
|
|
|
|
_test_state()
|
|
|
|
|
_wipe(ROOT)
|
2026-11-10 19:53:07 -06:00
|
|
|
finish()
|
2026-11-08 12:11:26 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
func _test_versions() -> void:
|
|
|
|
|
var sample := SampleBoard.create()
|
|
|
|
|
var v1 := JSON.stringify({"format_version": 1, "tables": sample.to_dict()["tables"]})
|
|
|
|
|
var b := TowerBoard.new()
|
|
|
|
|
_check(b.load_json(v1), "v1 text loads")
|
|
|
|
|
_check(b.tables.size() == sample.tables.size(), "v1 keeps every table")
|
|
|
|
|
_check(b.id == "" and b.name == "", "v1 has no id or name")
|
|
|
|
|
var d := b.to_dict()
|
|
|
|
|
_check(int(d["format_version"]) == 2, "saving writes format_version 2")
|
|
|
|
|
var b2 := TowerBoard.new()
|
|
|
|
|
b2.id = "x"
|
|
|
|
|
b2.name = "X"
|
|
|
|
|
b2.tables = b.tables
|
|
|
|
|
var b3 := TowerBoard.new()
|
|
|
|
|
_check(b3.load_json(b2.to_json()), "v2 text loads")
|
|
|
|
|
_check(b3.id == "x" and b3.name == "X", "v2 round-trips id and name")
|
|
|
|
|
_check(not TowerBoard.new().load_json("not json"), "garbage is rejected")
|
|
|
|
|
_check(TowerBoard.migrate({"format_version": 1, "tables": []})["format_version"] == 2, "migrate steps 1 to 2")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _test_library() -> void:
|
|
|
|
|
var root := ROOT + "/lib"
|
|
|
|
|
var lib := LibraryStore.new(root)
|
|
|
|
|
_check(lib.list().is_empty(), "empty library lists nothing")
|
|
|
|
|
var a := lib.create("Fantasy Loot")
|
|
|
|
|
_check(a.id == "fantasy-loot", "slug id from name")
|
|
|
|
|
var a2 := lib.create("Fantasy Loot")
|
|
|
|
|
_check(a2.id == "fantasy-loot-2", "duplicate names get unique ids")
|
|
|
|
|
_check(LibraryStore.slugify(" !! ") == "collection", "empty slug falls back")
|
|
|
|
|
_check(lib.exists("fantasy-loot") and not lib.exists("nope") and not lib.exists("../x"), "exists")
|
|
|
|
|
|
|
|
|
|
a.tables = SampleBoard.create().tables
|
|
|
|
|
_check(lib.save(a), "save succeeds")
|
|
|
|
|
var back := lib.load_collection("fantasy-loot")
|
|
|
|
|
_check(back != null and back.tables.size() == a.tables.size() and back.name == "Fantasy Loot", "load round trip")
|
|
|
|
|
|
|
|
|
|
_check(lib.rename("fantasy-loot", "Dark Loot"), "rename succeeds")
|
|
|
|
|
_check(lib.load_collection("fantasy-loot").name == "Dark Loot", "rename keeps id, changes name")
|
|
|
|
|
|
|
|
|
|
var copy := lib.duplicate("fantasy-loot", "Dark Loot Copy")
|
|
|
|
|
_check(copy != null and copy.id == "dark-loot-copy" and copy.tables.size() == a.tables.size(), "duplicate copies tables")
|
|
|
|
|
var names: Array = []
|
|
|
|
|
for e in lib.list():
|
|
|
|
|
names.append(e["name"])
|
|
|
|
|
_check(names == ["Dark Loot", "Dark Loot Copy", "Fantasy Loot"], "list sorted by name: %s" % str(names))
|
|
|
|
|
|
|
|
|
|
# No temp files left behind by saves.
|
|
|
|
|
var leftovers := 0
|
|
|
|
|
for f in DirAccess.get_files_at(lib.collections_dir()):
|
|
|
|
|
if not f.ends_with(".json"):
|
|
|
|
|
leftovers += 1
|
|
|
|
|
_check(leftovers == 0, "atomic save leaves no temp files")
|
|
|
|
|
|
|
|
|
|
# Delete removes the file and its state.
|
|
|
|
|
lib.state.set_deck("fantasy-loot", "loot", {"drawn": [1]})
|
|
|
|
|
lib.state.set_variable("fantasy-loot", "weapon", "Dagger", "loot")
|
|
|
|
|
lib.state.add_history({"collection": "fantasy-loot", "root": "loot"})
|
|
|
|
|
lib.state.add_history({"collection": "dark-loot-copy", "root": "loot"})
|
|
|
|
|
lib.state.set_setting("last_collection", "fantasy-loot")
|
|
|
|
|
_check(lib.delete("fantasy-loot"), "delete succeeds")
|
|
|
|
|
_check(not lib.exists("fantasy-loot"), "deleted file is gone")
|
|
|
|
|
_check(lib.state.get_deck("fantasy-loot", "loot").is_empty(), "delete drops decks")
|
|
|
|
|
_check(lib.state.get_variables("fantasy-loot").is_empty(), "delete drops variables")
|
|
|
|
|
_check(lib.state.get_history("fantasy-loot").is_empty() and lib.state.get_history().size() == 1, "delete drops only its history")
|
|
|
|
|
_check(lib.state.get_setting("last_collection") == null, "delete clears last_collection if it pointed there")
|
|
|
|
|
_check(not lib.delete("fantasy-loot"), "deleting twice returns false")
|
2026-11-10 18:24:23 -06:00
|
|
|
_check(lib.load_collection("fantasy-loot") == null, "loading a missing spread returns null")
|
2026-11-08 12:11:26 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
func _test_migration() -> void:
|
|
|
|
|
var root := ROOT + "/mig"
|
|
|
|
|
DirAccess.make_dir_recursive_absolute(root)
|
|
|
|
|
var legacy := root + "/board.json"
|
|
|
|
|
var sample := SampleBoard.create()
|
|
|
|
|
var f := FileAccess.open(legacy, FileAccess.WRITE)
|
|
|
|
|
f.store_string(JSON.stringify({"format_version": 1, "tables": sample.to_dict()["tables"]}))
|
|
|
|
|
f.close()
|
|
|
|
|
var lib := LibraryStore.new(root)
|
|
|
|
|
var b := lib.migrate_legacy()
|
2026-11-10 18:24:23 -06:00
|
|
|
_check(b != null and b.name == "My Tables" and b.id == "my-tables", "legacy spread imported as My Tables")
|
2026-11-08 12:11:26 -06:00
|
|
|
_check(lib.load_collection("my-tables").tables.size() == sample.tables.size(), "imported tables are saved")
|
|
|
|
|
_check(not FileAccess.file_exists(legacy) and FileAccess.file_exists(legacy + ".bak"), "old file renamed to .bak")
|
|
|
|
|
_check(lib.migrate_legacy() == null, "migration is a no-op once done")
|
|
|
|
|
|
|
|
|
|
# Not run when collections already exist.
|
|
|
|
|
var f2 := FileAccess.open(legacy, FileAccess.WRITE)
|
|
|
|
|
f2.store_string(JSON.stringify({"format_version": 1, "tables": sample.to_dict()["tables"]}))
|
|
|
|
|
f2.close()
|
2026-11-10 18:24:23 -06:00
|
|
|
_check(lib.migrate_legacy() == null and FileAccess.file_exists(legacy), "no migration when spreads exist")
|
2026-11-08 12:11:26 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
func _test_corrupt() -> void:
|
|
|
|
|
var root := ROOT + "/bad"
|
|
|
|
|
var lib := LibraryStore.new(root)
|
|
|
|
|
lib.create("Good")
|
|
|
|
|
var f := FileAccess.open(lib.path_for("broken"), FileAccess.WRITE)
|
|
|
|
|
f.store_string("{ this is not json")
|
|
|
|
|
f.close()
|
|
|
|
|
var f2 := FileAccess.open(lib.path_for("wrongshape"), FileAccess.WRITE)
|
|
|
|
|
f2.store_string("[1, 2, 3]")
|
|
|
|
|
f2.close()
|
2026-11-10 18:24:23 -06:00
|
|
|
_check(lib.load_collection("broken") == null, "corrupt spread loads as null")
|
2026-11-08 12:11:26 -06:00
|
|
|
var ids: Array = []
|
|
|
|
|
for e in lib.list():
|
|
|
|
|
ids.append(e["id"])
|
|
|
|
|
_check(ids == ["good"], "list skips unreadable files: %s" % str(ids))
|
|
|
|
|
_check(not lib.errors.is_empty(), "errors are recorded")
|
|
|
|
|
|
|
|
|
|
# Corrupt legacy file is left alone.
|
|
|
|
|
var legacy := root + "/board.json"
|
|
|
|
|
var f3 := FileAccess.open(legacy, FileAccess.WRITE)
|
|
|
|
|
f3.store_string("nope")
|
|
|
|
|
f3.close()
|
|
|
|
|
var lib2 := LibraryStore.new(root + "/empty")
|
|
|
|
|
_check(lib2.migrate_legacy(legacy) == null and FileAccess.file_exists(legacy), "corrupt legacy file is not touched")
|
|
|
|
|
|
|
|
|
|
# Corrupt state file starts fresh.
|
|
|
|
|
var st := StateStore.new(root)
|
|
|
|
|
DirAccess.make_dir_recursive_absolute(root + "/state")
|
|
|
|
|
var f4 := FileAccess.open(st.state_path(), FileAccess.WRITE)
|
|
|
|
|
f4.store_string("garbage")
|
|
|
|
|
f4.close()
|
|
|
|
|
_check(st.get_variables("c").is_empty(), "corrupt state file reads as empty")
|
|
|
|
|
st.set_setting("k", 1)
|
|
|
|
|
_check(st.save() and FileAccess.file_exists(st.state_path() + ".corrupt"), "corrupt state moved aside, new one saved")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _test_state() -> void:
|
|
|
|
|
var root := ROOT + "/state"
|
|
|
|
|
var st := StateStore.new(root)
|
|
|
|
|
_check(st.save() and not FileAccess.file_exists(st.state_path()), "saving an untouched store writes nothing")
|
|
|
|
|
st.set_deck("c1", "t1", {"drawn": [3, 5], "n": 10})
|
|
|
|
|
st.set_deck("c1", "t2", {"drawn": []})
|
|
|
|
|
st.set_variable("c1", "weapon", "Dagger", "t1")
|
|
|
|
|
st.set_variable("c1", "armor", "Plate", "t2")
|
|
|
|
|
st.set_setting("last_collection", "c1")
|
|
|
|
|
for i in 60:
|
|
|
|
|
st.add_history({"collection": "c1", "n": i})
|
|
|
|
|
_check(st.is_dirty(), "mutation sets dirty")
|
|
|
|
|
_check(st.save() and not st.is_dirty(), "save clears dirty")
|
|
|
|
|
_check(not FileAccess.file_exists(st.state_path() + ".tmp"), "no temp file after state save")
|
|
|
|
|
|
|
|
|
|
var st2 := StateStore.new(root)
|
|
|
|
|
var deck := st2.get_deck("c1", "t1")
|
|
|
|
|
_check(deck.get("n") == 10.0 and (deck["drawn"] as Array).size() == 2, "deck round trip")
|
|
|
|
|
_check(st2.get_deck("c1", "nope").is_empty() and st2.get_deck("zz", "t1").is_empty(), "missing deck is empty")
|
|
|
|
|
_check(st2.get_variables("c1")["weapon"] == {"value": "Dagger", "source": "t1"}, "variable round trip")
|
|
|
|
|
_check(st2.get_setting("last_collection") == "c1" and st2.get_setting("none", "d") == "d", "settings round trip")
|
|
|
|
|
var h := st2.get_history()
|
|
|
|
|
_check(h.size() == StateStore.HISTORY_CAP, "history capped at 50 (got %d)" % h.size())
|
|
|
|
|
_check(h[0]["n"] == 59.0 and h[49]["n"] == 10.0, "history is most recent first")
|
|
|
|
|
|
|
|
|
|
st2.clear_variable("c1", "weapon")
|
|
|
|
|
_check(not st2.get_variables("c1").has("weapon") and st2.get_variables("c1").has("armor"), "clear one variable")
|
|
|
|
|
st2.forget_table("c1", "t2")
|
|
|
|
|
_check(st2.get_deck("c1", "t2").is_empty() and not st2.get_variables("c1").has("armor"), "forget_table drops deck and its variables")
|
|
|
|
|
_check(not st2.get_deck("c1", "t1").is_empty(), "forget_table leaves other tables")
|
|
|
|
|
st2.clear_deck("c1", "t1")
|
|
|
|
|
_check(st2.get_deck("c1", "t1").is_empty(), "clear_deck")
|
|
|
|
|
st2.set_variable("c1", "a", "1")
|
|
|
|
|
st2.clear_variables("c1")
|
|
|
|
|
_check(st2.get_variables("c1").is_empty(), "clear all variables")
|
|
|
|
|
st2.clear_history()
|
|
|
|
|
_check(st2.get_history().is_empty(), "clear_history")
|
|
|
|
|
st2.save()
|
|
|
|
|
var st3 := StateStore.new(root)
|
|
|
|
|
_check(st3.get_history().is_empty() and st3.get_deck("c1", "t1").is_empty(), "cleared state persists")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _wipe(path: String) -> void:
|
|
|
|
|
if not DirAccess.dir_exists_absolute(path):
|
|
|
|
|
return
|
|
|
|
|
for d in DirAccess.get_directories_at(path):
|
|
|
|
|
_wipe(path.path_join(d))
|
|
|
|
|
for f in DirAccess.get_files_at(path):
|
|
|
|
|
DirAccess.remove_absolute(path.path_join(f))
|
|
|
|
|
DirAccess.remove_absolute(path)
|