Files
idle/tests/test_gold_mine_click.gd

103 lines
3.3 KiB
GDScript

extends Node
var passed: int = 0
var failed: int = 0
var _game_root: Node = null
func _ready():
pass # run() is called by the test runner; avoid double-execution.
func run():
print("\n=== TEST: Gold Mine Click ===\n")
TestUtils.set_test_name("gold_mine_click")
# Load tiny_sword scene
var scene = load("res://docs/gyms/tiny_sword/tiny_sword.tscn")
_game_root = scene.instantiate()
# Use a unique save path to avoid contamination from previous test runs.
var save_path: String = "user://test_gold_mine_%d.json" % randi()
DirAccess.remove_absolute(ProjectSettings.globalize_path(save_path))
var gs: LevelGameState = _game_root.get_node("LevelGameState")
gs.save_file_path = save_path
add_child(_game_root)
await _wait()
var game_state: LevelGameState = _game_root.find_child("LevelGameState")
var gold_mine_node: Node = _game_root.find_child("GoldMine")
if game_state == null:
print("[ERROR] LevelGameState not found")
_print_summary()
return
if gold_mine_node == null:
print("[ERROR] GoldMine node not found")
_print_summary()
return
var gold_mine: CurrencyGenerator = gold_mine_node.find_child("CurrencyGenerator") as CurrencyGenerator
if gold_mine == null:
print("[ERROR] CurrencyGenerator not found under GoldMine")
_print_summary()
return
# Verify game_state is reachable and generator is available
TestUtils.assert_not_null(gold_mine.game_state, "game_state is not null")
TestUtils.assert_true(gold_mine.is_available_to_player(), "Gold Mine is available to player")
# Record initial gold (copy to avoid BigNumber reference sharing)
var initial_gold: BigNumber = BigNumber.new(
game_state.get_currency_amount_by_id(&"gold").mantissa,
game_state.get_currency_amount_by_id(&"gold").exponent
)
print("[TARGET] initial_gold: %s" % initial_gold.to_string_suffix(2))
# Temporarily set press_buys_generator to false so _on_pressed grants click currency
# (Gold Mine defaults to true which would try to buy with workers instead)
var original_press_buys: bool = gold_mine.press_buys_generator
gold_mine.press_buys_generator = false
# Simulate clicking the generator
gold_mine._on_pressed()
await _wait()
# Validate gold increased (copy to avoid BigNumber reference sharing)
var final_gold: BigNumber = BigNumber.new(
game_state.get_currency_amount_by_id(&"gold").mantissa,
game_state.get_currency_amount_by_id(&"gold").exponent
)
print("[TARGET] final_gold: %s" % final_gold.to_string_suffix(2))
TestUtils.assert_greater_than(
final_gold.mantissa,
initial_gold.mantissa,
"Gold increased after generator click"
)
# Check specific value (adjust based on click_mantissa in data)
var expected_min: float = initial_gold.mantissa + 1.0
TestUtils.assert_true(
final_gold.mantissa >= expected_min,
"Gold increased by at least 1.0"
)
_print_summary()
func _wait() -> void:
await get_tree().create_timer(0.5).timeout
func _print_summary():
TestUtils.print_result()
passed = TestUtils.get_passed()
failed = TestUtils.get_failed()
func get_passed() -> int:
return passed
func get_failed() -> int:
return failed