From d30475a920e4e195bcbe34f128e7927e091b4386 Mon Sep 17 00:00:00 2001 From: Saffron <263493777+itsmiso-ai@users.noreply.github.com> Date: Wed, 8 Jul 2026 01:13:51 +0000 Subject: [PATCH] Remove use_local_storage = false test workaround Default use_local_storage to false so tests don't need to set it. Guard JavaScriptBridge.eval() in _ready() with an if statement instead of using `and` (which doesn't short-circuit in GDScript), preventing crashes when the node is added to the scene tree in headless mode. Fixes #238 Signed-off-by: Saffron <263493777+itsmiso-ai@users.noreply.github.com> --- scripts/game_state.gd | 3 ++- tests/test_dirty_state_tracking.gd | 6 ------ tests/test_e2e.gd | 7 ------- tests/test_reservations.gd | 6 ------ tests/test_runner.gd | 1 - tests/test_save_backup.gd | 3 --- 6 files changed, 2 insertions(+), 24 deletions(-) diff --git a/scripts/game_state.gd b/scripts/game_state.gd index f038a64..31cd386 100644 --- a/scripts/game_state.gd +++ b/scripts/game_state.gd @@ -14,7 +14,8 @@ var _backup_counter := 0 func _ready() -> void: save_supported = true - use_local_storage = OS.has_feature("web") and JavaScriptBridge.eval("typeof localStorage !== 'undefined'", true) + if OS.has_feature("web"): + use_local_storage = JavaScriptBridge.eval("typeof localStorage !== 'undefined'", true) func save_game(data: Dictionary) -> void: var payload := JSON.stringify(data) diff --git a/tests/test_dirty_state_tracking.gd b/tests/test_dirty_state_tracking.gd index aa9f407..76e60b5 100644 --- a/tests/test_dirty_state_tracking.gd +++ b/tests/test_dirty_state_tracking.gd @@ -80,7 +80,6 @@ func seed_tiles(state: Dictionary) -> void: func flow_dirty_state_triggers_save() -> void: print("\n=== Flow 1: Dirty state triggers save ===") var gs := load_game_state() - gs.use_local_storage = false gs.clear_game() # Simulate: _mark_dirty() was called (state changed) @@ -105,7 +104,6 @@ func flow_dirty_state_triggers_save() -> void: func flow_clean_state_skips_save() -> void: print("\n=== Flow 2: Clean state skips save (persist returns early) ===") var gs := load_game_state() - gs.use_local_storage = false gs.clear_game() # Initial save with dirty state @@ -138,7 +136,6 @@ func flow_clean_state_skips_save() -> void: func flow_persist_resets_dirty_flag() -> void: print("\n=== Flow 3: persist() resets _dirty flag ===") var gs := load_game_state() - gs.use_local_storage = false gs.clear_game() # Initial save (dirty=true) @@ -167,7 +164,6 @@ func flow_persist_resets_dirty_flag() -> void: func flow_multiple_mutations_single_persist() -> void: print("\n=== Flow 4: Multiple mutations → single persist ===") var gs := load_game_state() - gs.use_local_storage = false gs.clear_game() # Initial state @@ -198,7 +194,6 @@ func flow_multiple_mutations_single_persist() -> void: func flow_idle_tick_skips_persist() -> void: print("\n=== Flow 5: Idle tick (no mutations) skips persist ===") var gs := load_game_state() - gs.use_local_storage = false gs.clear_game() # Save initial state @@ -225,7 +220,6 @@ func flow_idle_tick_skips_persist() -> void: func flow_dirty_flag_covers_all_mutation_categories() -> void: print("\n=== Flow 6: Dirty flag covers all mutation categories ===") var gs := load_game_state() - gs.use_local_storage = false gs.clear_game() # Initial state with workers and builds diff --git a/tests/test_e2e.gd b/tests/test_e2e.gd index 61a12e7..613e121 100644 --- a/tests/test_e2e.gd +++ b/tests/test_e2e.gd @@ -77,7 +77,6 @@ func _assert_array_has(arr: Array, val, msg: String) -> void: func flow_boot_and_bootstrap() -> void: print("\n=== Flow 1: Boot and bootstrap ===") var gs := load_game_state() - gs.use_local_storage = false gs.clear_game() # Simulate bootstrap by constructing initial state directly @@ -158,7 +157,6 @@ func flow_boot_and_bootstrap() -> void: func flow_save_and_reload() -> void: print("\n=== Flow 2: Save and reload ===") var gs := load_game_state() - gs.use_local_storage = false gs.clear_game() var state := { @@ -227,7 +225,6 @@ func flow_save_and_reload() -> void: func flow_tick_simulation() -> void: print("\n=== Flow 3: Tick simulation ===") var gs := load_game_state() - gs.use_local_storage = false gs.clear_game() # Initial state with resources available for gathering @@ -290,7 +287,6 @@ func flow_tick_simulation() -> void: func flow_build_placement() -> void: print("\n=== Flow 4: Build placement, queue, and completion ===") var gs := load_game_state() - gs.use_local_storage = false gs.clear_game() var state := { @@ -390,7 +386,6 @@ func flow_build_placement() -> void: func flow_anchor_switching() -> void: print("\n=== Flow 5: Anchor switching ===") var gs := load_game_state() - gs.use_local_storage = false gs.clear_game() # Test that state is preserved across anchor changes @@ -449,7 +444,6 @@ func flow_anchor_switching() -> void: func flow_priority_order() -> void: print("\n=== Flow 6: Priority order changes ===") var gs := load_game_state() - gs.use_local_storage = false gs.clear_game() var state := { @@ -510,7 +504,6 @@ func flow_priority_order() -> void: func flow_save_compatibility() -> void: print("\n=== Flow 7: Save compatibility ===") var gs := load_game_state() - gs.use_local_storage = false gs.clear_game() # Test 1: Old save version (version 0) — rejected as invalid diff --git a/tests/test_reservations.gd b/tests/test_reservations.gd index 7bdf751..b43d245 100644 --- a/tests/test_reservations.gd +++ b/tests/test_reservations.gd @@ -68,7 +68,6 @@ func _assert_no_key(d: Dictionary, key: String, name: String) -> void: func test_reservation_prevents_double_haul(gs: Node) -> void: print("") print("--- reservation: prevents double-haul ---") - gs.use_local_storage = false # Build needs 1 stone, stockpile has 1 stone var state := { @@ -129,7 +128,6 @@ func test_reservation_prevents_double_haul(gs: Node) -> void: func test_reservation_clamps_delivery(gs: Node) -> void: print("") print("--- reservation: clamps delivery ---") - gs.use_local_storage = false # Hut costs 2 stone. Build starts with 0 delivered, 0 reserved. # Worker picks up 1 stone → reserves it (reserved=1). @@ -221,7 +219,6 @@ func test_reservation_clamps_delivery(gs: Node) -> void: func test_reservation_released_on_build_complete(gs: Node) -> void: print("") print("--- reservation: released on completion ---") - gs.use_local_storage = false # Build is complete — reservations should be cleaned up by _clean_stale_reservations var state := { @@ -263,7 +260,6 @@ func test_reservation_released_on_build_complete(gs: Node) -> void: func test_stale_reservations_cleaned_up(gs: Node) -> void: print("") print("--- reservation: stale cleanup ---") - gs.use_local_storage = false # Build has 1 stone reserved but no worker is hauling to it (worker broke) var state := { @@ -321,7 +317,6 @@ func test_stale_reservations_cleaned_up(gs: Node) -> void: func test_two_workers_one_need_only_one_succeeds(gs: Node) -> void: print("") print("--- reservation: two workers one need ---") - gs.use_local_storage = false # Hut needs 2 stone. Stockpile has 1 stone. # Build has 0 delivered, 0 reserved initially. @@ -406,7 +401,6 @@ func test_two_workers_one_need_only_one_succeeds(gs: Node) -> void: func test_reserve_field_added_to_new_builds(gs: Node) -> void: print("") print("--- reservation: reserved field on new builds ---") - gs.use_local_storage = false # Simulate a newly queued build — should have reserved field var new_build := { diff --git a/tests/test_runner.gd b/tests/test_runner.gd index 7880bb2..2f70341 100644 --- a/tests/test_runner.gd +++ b/tests/test_runner.gd @@ -80,7 +80,6 @@ func test_persistence_roundtrip(gs: Node) -> void: "events": [{"tick": 42, "text": "test event"}], } - gs.use_local_storage = false gs.save_game(payload) var loaded = gs.load_game() _assert_not_empty(loaded, "persistence_roundtrip: load returned data") diff --git a/tests/test_save_backup.gd b/tests/test_save_backup.gd index f6ae901..9d1f961 100644 --- a/tests/test_save_backup.gd +++ b/tests/test_save_backup.gd @@ -42,7 +42,6 @@ func _assert_eq(actual, expected, msg: String) -> void: func flow_backup_creates_file() -> void: print("\n=== Flow 1: backup_save creates a timestamped file ===") var gs := load_game_state() - gs.use_local_storage = false gs.clear_game() # Save some state first @@ -70,7 +69,6 @@ func flow_backup_creates_file() -> void: func flow_restore_from_backup() -> void: print("\n=== Flow 2: restore_backup restores from the latest backup ===") var gs := load_game_state() - gs.use_local_storage = false gs.clear_game() # Save original state @@ -121,7 +119,6 @@ func flow_restore_from_backup() -> void: func flow_list_backups_sorted() -> void: print("\n=== Flow 3: list_backups returns newest-first sorted list ===") var gs := load_game_state() - gs.use_local_storage = false gs.clear_game() # Create multiple backups