Files
aetherbound-guild/runtime/tests/dev3/market_runtime_capture_test.gd
T

155 lines
6.2 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 FIXTURES := [
{"width": 760, "height": 360, "locale": "zh_CN", "scale": 1.0},
{"width": 844, "height": 390, "locale": "en", "scale": 1.0},
{"width": 1280, "height": 720, "locale": "zh_CN", "scale": 1.0},
{"width": 1280, "height": 720, "locale": "en", "scale": 1.0},
]
const STATES := ["market", "recruit_selected", "equipment", "battle"]
const SEED := "99887766554433221100ffeeddccbbaa"
const CONTRACT_OPERATION := "0123456789abcdef0123456789abcdef"
var failures: Array[String] = []
var output_root := ""
func _initialize() -> void:
call_deferred("run_test")
func run_test() -> void:
output_root = OS.get_environment("ABG_EVIDENCE_DIR")
if output_root.is_empty():
output_root = "/tmp/abg-m09-market-captures"
DirAccess.make_dir_recursive_absolute(output_root)
var requested := OS.get_environment("ABG_CAPTURE_FIXTURE_INDEX")
var fixture_indexes: Array = range(FIXTURES.size())
if not requested.is_empty():
var index := requested.to_int()
if index < 0 or index >= FIXTURES.size() or str(index) != requested:
push_error("invalid ABG_CAPTURE_FIXTURE_INDEX: %s" % requested)
quit(2)
return
fixture_indexes = [index]
for fixture_index in fixture_indexes:
for state_index in range(STATES.size()):
await _capture_fixture(FIXTURES[fixture_index], String(STATES[state_index]), fixture_index * STATES.size() + state_index)
if failures.is_empty():
print("ABG_M09_MARKET_CAPTURE_OK fixtures=4 states=market,recruit_selected,equipment,battle scale=100 canvas=16:9 strict=0")
quit(0)
else:
for failure in failures:
push_error(failure)
quit(1)
func _capture_fixture(fixture: Dictionary, state: String, ordinal: int) -> void:
var draft_path := "user://m09_market_capture_%d.json" % ordinal
var company_path := "%s.company" % draft_path
_cleanup_path(draft_path)
var company := _create_company(draft_path, company_path)
if company.is_empty():
return
var viewport := SubViewport.new()
viewport.size = Vector2i(int(fixture.width), int(fixture.height))
viewport.render_target_update_mode = SubViewport.UPDATE_ALWAYS
root.add_child(viewport)
var shell := EntryShell.new()
shell.auto_start = false
shell.initial_company_service.path = draft_path
shell.initial_company_service.company_path = company_path
viewport.add_child(shell)
await _frames(3)
var settings := SettingsStore.new().defaults()
settings.setup_completed = true
settings.locale = String(fixture.locale)
settings.text_scale = float(fixture.scale)
shell.settings = settings
var contract_service := ContractService.new()
contract_service.company_path = company_path
var created := contract_service.create_contract(CONTRACT_OPERATION, "CTR-R1-01", "standard")
if not created.ok:
failures.append("%s contract fixture failed" % state)
viewport.queue_free()
return
company = created.company
var offers: Array = company.active_contract.market_board.recruit_offers
shell.market_category = "recruit"
shell.market_selected_kind = ""
shell.market_selected_offer_id = ""
match state:
"recruit_selected":
shell.market_selected_kind = "recruit"
shell.market_selected_offer_id = String(offers[0].source_offer_id)
"equipment": shell.market_category = "equipment"
"battle": shell.market_category = "battle"
shell.show_market(company)
await _frames(7)
_verify_runtime(shell, fixture, state)
await RenderingServer.frame_post_draw
var image := viewport.get_texture().get_image()
if image.is_empty() or image.get_width() != int(fixture.width) or image.get_height() != int(fixture.height):
failures.append("%s frame dimensions invalid" % state)
else:
var filename := "%s-%dx%d-%s-100.png" % [state, int(fixture.width), int(fixture.height), String(fixture.locale)]
if image.save_png(output_root.path_join(filename)) != OK:
failures.append("%s capture save failed" % filename)
viewport.queue_free()
await _frames(4)
_cleanup_path(draft_path)
func _verify_runtime(shell: Control, fixture: Dictionary, state: String) -> void:
expect(shell.current_screen == "market", "%s remains on Market" % state)
var canvas := shell.find_child("MarketMasterCanvas", true, false) as Control
if canvas == null:
failures.append("%s missing contained canvas" % state)
return
var bounds := Rect2(Vector2.ZERO, Vector2(float(fixture.width), float(fixture.height)))
expect(bounds.encloses(canvas.get_global_rect()), "%s canvas stays inside viewport" % state)
expect(is_equal_approx(canvas.size.x / canvas.size.y, 16.0 / 9.0), "%s canvas keeps 16:9" % state)
expect(shell.find_child("MarketCompanyStatus", true, false) != null, "%s shows Company/Line" % state)
expect(shell.find_child("MarketCoinStatus", true, false) != null, "%s shows Coin" % state)
var list := shell.find_child("MarketOfferList", true, false) as Control
var expected := 3 if state in ["market", "recruit_selected", "battle"] else 4
expect(list != null and list.get_child_count() == expected, "%s offer count=%d" % [state, expected])
func _create_company(draft_path: String, company_path: String) -> Dictionary:
var service := DraftService.new()
service.path = draft_path
service.company_path = company_path
var draft: Dictionary = service.begin_or_load(SEED)
if not draft.ok:
failures.append("fixture draft failed")
return {}
service.set_selection([String(draft.draft.offers[0].offer_id), String(draft.draft.offers[2].offer_id)])
var committed := service.commit_company()
return committed.company if committed.ok else {}
func _cleanup_path(base: String) -> void:
for suffix in ["", ".bak", ".candidate", ".bak.candidate", ".corrupt"]:
var path := "%s%s" % [base, suffix]
if FileAccess.file_exists(path):
DirAccess.remove_absolute(path)
var company_path := "%s.company%s" % [base, suffix]
if FileAccess.file_exists(company_path):
DirAccess.remove_absolute(company_path)
func _frames(count: int) -> void:
for _frame in range(count):
await process_frame
func expect(condition: bool, message: String) -> void:
if not condition:
failures.append("M09 Market capture: %s" % message)