213 lines
9.8 KiB
GDScript
213 lines
9.8 KiB
GDScript
extends SceneTree
|
|
|
|
const EntryShell = preload("res://entry/entry_shell.gd")
|
|
const SettingsStore = preload("res://entry/settings_store.gd")
|
|
const DraftService = preload("res://entry/initial_company_draft_service.gd")
|
|
const ContractService = preload("res://entry/guild_contract_service.gd")
|
|
|
|
const DRAFT_PATH := "user://dev6_growth_draft.json"
|
|
const COMPANY_PATH := "user://dev6_growth_company.json"
|
|
const SEED := "11223344556677889900aabbccddeeff"
|
|
const CONTRACT_OP := "abcdefabcdefabcdefabcdefabcdefab"
|
|
const HIRE_OP_1 := "11112222333344445555666677778888"
|
|
const HIRE_OP_2 := "88887777666655554444333322221111"
|
|
|
|
var failures: Array[String] = []
|
|
var parsed_touches := 0
|
|
|
|
|
|
func _initialize() -> void:
|
|
call_deferred("run_test")
|
|
|
|
|
|
func run_test() -> void:
|
|
_cleanup()
|
|
var company := _create_market_company()
|
|
if not company.is_empty():
|
|
await verify_equipment_ui(company)
|
|
_cleanup()
|
|
if failures.is_empty():
|
|
print("ABG_DEV6_GROWTH_OK equipment_offer=true inspect=true wearer=true purchase=true replace=true inventory=true coin=24,6 receipt=one duplicate=true conflict=true insufficient=true xp_level_persist=true restart=true parsed_touch=%d" % parsed_touches)
|
|
quit(0)
|
|
else:
|
|
for failure in failures:
|
|
push_error(failure)
|
|
quit(1)
|
|
|
|
|
|
func verify_equipment_ui(company: Dictionary) -> void:
|
|
var settings := SettingsStore.new().defaults()
|
|
settings.setup_completed = true
|
|
settings.locale = "en"
|
|
settings.text_scale = 1.3
|
|
root.content_scale_mode = Window.CONTENT_SCALE_MODE_CANVAS_ITEMS
|
|
# Single target size (owner policy).
|
|
root.content_scale_aspect = Window.CONTENT_SCALE_ASPECT_KEEP
|
|
root.content_scale_size = Vector2i(1280, 720)
|
|
root.size = Vector2i(1280, 720)
|
|
var shell := EntryShell.new()
|
|
shell.auto_start = false
|
|
shell.initial_company_service.path = DRAFT_PATH
|
|
shell.initial_company_service.company_path = COMPANY_PATH
|
|
root.add_child(shell)
|
|
await _frames(3)
|
|
shell.settings = settings
|
|
shell.setup_draft = settings.duplicate(true)
|
|
shell.show_market(company)
|
|
shell.set_market_category("equipment")
|
|
await _wait_for_node(shell, "MarketLayout")
|
|
var contract: Dictionary = company.active_contract
|
|
var offer: Dictionary = {}
|
|
for candidate in contract.market_board.equipment_offers:
|
|
if int(candidate.list_price) <= int(contract.coin) and (offer.is_empty() or int(candidate.list_price) < int(offer.list_price)):
|
|
offer = candidate
|
|
if offer.is_empty():
|
|
expect(false, "fixture exposes an affordable equipment offer")
|
|
return
|
|
var offer_id := String(offer.offer_id)
|
|
shell.select_market_offer("equipment", offer_id)
|
|
expect(await _wait_for_node(shell, "MarketReviewEquipment") != null, "Market exposes Equipment review")
|
|
await _touch(await _wait_for_node(shell, "MarketReviewEquipment"))
|
|
expect(await _wait_for_node(shell, "EquipmentReviewEffect") != null, "Equipment review exposes sourced effect and tradeoff")
|
|
var first_recruit_id := String(company.owned_recruits[0].recruit_id)
|
|
var wearer := await _wait_for_node(shell, "EquipmentEquipRecruit%s" % first_recruit_id) as Button
|
|
expect(wearer != null and not wearer.disabled, "Equipment review exposes a valid Recruit wearer")
|
|
await _touch(wearer)
|
|
expect(await _wait_for_node(shell, "MarketLayout") != null, "Equipment commit returns to Market")
|
|
var loaded: Dictionary = _draft_service().load_company()
|
|
expect(loaded.ok, "Company reloads after equipment commit")
|
|
if not loaded.ok:
|
|
return
|
|
var after: Dictionary = loaded.company
|
|
var after_contract: Dictionary = after.active_contract
|
|
var expected_coin := 24 - int(offer.list_price)
|
|
expect(int(after_contract.coin) == expected_coin and after_contract.available_equipment_offer_ids.size() == 3 and after_contract.equipment_receipts.size() == 1, "Equipment commit changes Coin, offer availability and receipt once")
|
|
var target := _recruit_by_id(after, first_recruit_id)
|
|
expect(String(target.equipped_item_id) == String(offer.catalog_id) and after.equipment_inventory.size() == 1, "Equipment replaces the slot and returns the previous item to inventory")
|
|
for recruit in company.owned_recruits:
|
|
var persisted_growth := _recruit_by_id(after, String(recruit.recruit_id))
|
|
expect(int(persisted_growth.level) == int(recruit.level) and int(persisted_growth.xp) == int(recruit.xp), "Equipment commit preserves individual level/XP for %s" % String(recruit.recruit_id))
|
|
var receipt: Dictionary = after_contract.equipment_receipts[0]
|
|
var operation_id := String(receipt.operation_id)
|
|
var duplicate: Dictionary = _contract_service().purchase_equipment(operation_id, offer_id, first_recruit_id, int(offer.list_price))
|
|
expect(duplicate.ok and duplicate.code == "already_equipped" and int(duplicate.company.active_contract.coin) == expected_coin and duplicate.company.active_contract.equipment_receipts.size() == 1, "Duplicate equipment operation does not charge twice")
|
|
var conflict: Dictionary = _contract_service().purchase_equipment(operation_id, offer_id, String(after.owned_recruits[1].recruit_id), int(offer.list_price))
|
|
expect(not conflict.ok and conflict.code == "EQUIPMENT_OPERATION_CONFLICT", "Conflicting retry names the changed wearer")
|
|
var unavailable: Dictionary = _contract_service().purchase_equipment(("%s-other" % operation_id).sha256_text().substr(0, 32), offer_id, String(after.owned_recruits[1].recruit_id), int(offer.list_price))
|
|
expect(not unavailable.ok and unavailable.code == "EQUIPMENT_OFFER_ALREADY_APPLIED_CONFLICT", "Already purchased offer is bound to its original wearer")
|
|
var next_offer: Dictionary = {}
|
|
for candidate in after_contract.market_board.equipment_offers:
|
|
if int(candidate.list_price) > int(after_contract.coin):
|
|
next_offer = candidate
|
|
break
|
|
if not next_offer.is_empty():
|
|
var insufficient: Dictionary = _contract_service().purchase_equipment("fedcbafedcbafedcbafedcbafedcbafe", String(next_offer.offer_id), String(after.owned_recruits[1].recruit_id), int(next_offer.list_price))
|
|
expect(not insufficient.ok and insufficient.code == "EQUIPMENT_INSUFFICIENT_COIN", "insufficient Coin is rejected before balance mutation")
|
|
var stale_price: Dictionary = _contract_service().purchase_equipment("00112233445566778899aabbccddeeff", String(next_offer.offer_id) if not next_offer.is_empty() else offer_id, String(after.owned_recruits[1].recruit_id), 999)
|
|
expect(not stale_price.ok and stale_price.code in ["EQUIPMENT_PRICE_MISMATCH", "EQUIPMENT_OFFER_UNAVAILABLE"], "stale price or unavailable offer is rejected before balance mutation")
|
|
var persisted: Dictionary = _draft_service().load_company()
|
|
expect(persisted.ok and String(_recruit_by_id(persisted.company, first_recruit_id).equipped_item_id) == String(offer.catalog_id), "equipped item persists after restart")
|
|
shell.queue_free()
|
|
await _timer_seconds(0.2)
|
|
var resumed := EntryShell.new()
|
|
resumed.auto_start = false
|
|
resumed.initial_company_service.path = DRAFT_PATH
|
|
resumed.initial_company_service.company_path = COMPANY_PATH
|
|
root.add_child(resumed)
|
|
await _frames(2)
|
|
resumed.settings = settings
|
|
resumed.setup_draft = settings.duplicate(true)
|
|
resumed._open_company_destination(persisted.company)
|
|
await _frames(3)
|
|
expect(resumed.current_screen == "market" and resumed.find_child("MarketLayout", true, false) != null, "restart returns to the same Market")
|
|
resumed.queue_free()
|
|
await _timer_seconds(0.2)
|
|
|
|
|
|
func _create_market_company() -> Dictionary:
|
|
var draft: Dictionary = _draft_service().begin_or_load(SEED)
|
|
if not draft.ok:
|
|
return {}
|
|
_draft_service().set_selection([String(draft.draft.offers[0].offer_id), String(draft.draft.offers[1].offer_id)])
|
|
if not _draft_service().commit_company().ok:
|
|
return {}
|
|
var created: Dictionary = _contract_service().create_contract(CONTRACT_OP, "CTR-R1-01", "standard")
|
|
if not created.ok:
|
|
return {}
|
|
var offers: Array = created.company.active_contract.market_board.recruit_offers
|
|
_contract_service().hire_recruit(HIRE_OP_1, String(offers[0].source_offer_id), 14)
|
|
var hired: Dictionary = _contract_service().hire_recruit(HIRE_OP_2, String(offers[1].source_offer_id), 14)
|
|
return hired.company if hired.ok else {}
|
|
|
|
|
|
func _draft_service() -> RefCounted:
|
|
var service := DraftService.new()
|
|
service.path = DRAFT_PATH
|
|
service.company_path = COMPANY_PATH
|
|
return service
|
|
|
|
|
|
func _contract_service() -> RefCounted:
|
|
var service := ContractService.new()
|
|
service.company_path = COMPANY_PATH
|
|
return service
|
|
|
|
|
|
func _recruit_by_id(company: Dictionary, recruit_id: String) -> Dictionary:
|
|
for recruit in company.get("owned_recruits", []):
|
|
if String(recruit.recruit_id) == recruit_id:
|
|
return Dictionary(recruit)
|
|
return {}
|
|
|
|
|
|
func _wait_for_node(parent: Node, node_name: String, max_frames := 600) -> Node:
|
|
for _i in range(max_frames):
|
|
var node := parent.find_child(node_name, true, false)
|
|
if node != null:
|
|
# Let the container/layout settle before the caller reads rects or
|
|
# touches the node (fixed frame budgets were the old flake source).
|
|
await _frames(2)
|
|
return node
|
|
await process_frame
|
|
return null
|
|
|
|
|
|
func _touch(control: Control) -> void:
|
|
if control == null:
|
|
expect(false, "touch target exists")
|
|
return
|
|
var center := control.get_global_rect().get_center()
|
|
var press := InputEventScreenTouch.new()
|
|
press.position = center
|
|
press.pressed = true
|
|
press.index = 0
|
|
Input.parse_input_event(press)
|
|
await _frames(2)
|
|
var release := InputEventScreenTouch.new()
|
|
release.position = center
|
|
release.pressed = false
|
|
release.index = 0
|
|
Input.parse_input_event(release)
|
|
parsed_touches += 1
|
|
await _frames(5)
|
|
|
|
|
|
func _frames(count: int) -> void:
|
|
for _index in range(count):
|
|
await process_frame
|
|
|
|
|
|
func _timer_seconds(seconds: float) -> void:
|
|
await create_timer(seconds).timeout
|
|
|
|
|
|
func _cleanup() -> void:
|
|
for path in [DRAFT_PATH, "%s.bak" % DRAFT_PATH, "%s.candidate" % DRAFT_PATH, "%s.bak.candidate" % DRAFT_PATH, COMPANY_PATH, "%s.bak" % COMPANY_PATH, "%s.candidate" % COMPANY_PATH, "%s.bak.candidate" % COMPANY_PATH, "%s.corrupt" % COMPANY_PATH]:
|
|
if FileAccess.file_exists(path):
|
|
DirAccess.remove_absolute(path)
|
|
|
|
|
|
func expect(condition: bool, message: String) -> void:
|
|
if not condition:
|
|
failures.append("DEV-6 growth: %s" % message)
|