Files
windroost/p2_godot_sample/scripts/offline_anchor_touch_test.gd
T

186 lines
7.6 KiB
GDScript

extends SceneTree
## S2 offline anchor/receipt runtime evidence through the REAL input path.
##
## No claim callback is invoked directly. Every action is dispatched with
## Input.parse_input_event(InputEventScreenTouch) (plus one ScreenDrag probe)
## so the engine's own viewport/Control routing must reach the skyport claim
## button. Covers 360/390/430 x zh/en x normal/reduced.
const OUTPUT_PATH := "res://artifacts/offline_anchor_touch_test.json"
const SIZES := [Vector2i(360, 800), Vector2i(390, 844), Vector2i(430, 932)]
const LOCALES := ["zh_CN", "en_US"]
const REDUCED := [false, true]
var _problems: Array = []
var _steps: Array = []
func _init() -> void:
_run.call_deferred()
func _check(cond: bool, msg: String) -> void:
if not cond:
_problems.append(msg)
func _record(name: String, data: Dictionary) -> void:
var e := data.duplicate()
e["step"] = name
_steps.append(e)
func _run() -> void:
var root_window := get_root()
for size in SIZES:
for locale in LOCALES:
for reduced in REDUCED:
await _case(root_window, size as Vector2i, str(locale), bool(reduced))
_finish()
func _case(root_window: Window, size: Vector2i, locale: String, reduced: bool) -> void:
var tag := "%dx%d/%s/%s" % [size.x, size.y, locale, "reduced" if reduced else "normal"]
root_window.content_scale_mode = Window.CONTENT_SCALE_MODE_CANVAS_ITEMS
root_window.content_scale_size = size
root_window.size = size
await _wait_frames(3)
var shell := load("res://scenes/game_root.tscn").instantiate() as GameRoot
root_window.add_child(shell)
await _wait_frames(2)
shell.set_locale_for_test(locale)
shell.set_reduced_motion_for_test(reduced)
shell.reset_profile_for_test()
# Seed a one-hour offline report directly on the in-memory profile. The
# anchor is the ms high-water mark; no save happens until the claim, so the
# settlement keeps its elapsed window.
var gp := shell.get_profile()
var now := Time.get_unix_time_from_system()
gp["seen_intro"] = true
gp["last_saved_at"] = now - 3600
gp["offline_anchor_ms"] = (now - 3600) * 1000
gp["offline_revision"] = 0
gp["offline_pending"] = false
gp["offline_receipt"] = {}
var receipt := ProfileStore.build_offline_report(gp, 40.0)
var expected_gold := int(gp.get("pending_offline_gold", 0))
_check(not receipt.is_empty(), "%s: seeded offline settlement must produce a receipt" % tag)
_check(expected_gold > 0, "%s: seeded report must be payable" % tag)
_check(bool(gp.get("offline_pending", false)), "%s: seeded report must be pending" % tag)
shell.show_page_for_test("skyport")
shell.refresh_pages_for_test()
await _wait_frames(4)
# Real ScreenDrag probe on the scroll surface. This only proves the engine
# routes a drag; positioning below is done deterministically for the tap.
var viewport_size := root_window.get_visible_rect().size
var drag := InputEventScreenDrag.new()
drag.index = 12
drag.position = Vector2(viewport_size.x * 0.5, viewport_size.y * 0.6)
drag.relative = Vector2(0.0, -24.0)
Input.parse_input_event(drag)
await _wait_frames(2)
var claim_rect := shell.u2_target_rect_for_test("skyport_claim")
_check(claim_rect != Rect2(), "%s: offline claim button has no global rect" % tag)
var scroll := shell.find_skyport_scroll_for_test()
if scroll != null:
scroll.scroll_horizontal = int(maxf(0.0, claim_rect.position.x - (viewport_size.x - claim_rect.size.x) * 0.5))
scroll.scroll_vertical = int(maxf(0.0, claim_rect.position.y - viewport_size.y * 0.5))
await _wait_frames(2)
claim_rect = shell.u2_target_rect_for_test("skyport_claim")
_check(claim_rect.position.x >= 0.0 and claim_rect.position.x + claim_rect.size.x <= viewport_size.x,
"%s: offline claim button could not be scrolled into view" % tag)
# 1. REAL ScreenTouch on the claim button pays once and clears the book.
await _tap(claim_rect.get_center())
await _wait_frames(3)
var p := shell.get_profile()
_check(int(p.get("gold", 0)) == expected_gold, "%s: real touch must credit offline gold once" % tag)
_check(int(p.get("pending_offline_gold", 0)) == 0, "%s: real touch must clear pending gold" % tag)
_check(not bool(p.get("offline_pending", false)), "%s: real touch must clear the pending flag" % tag)
var final_receipt: Dictionary = p.get("offline_receipt", {})
_check(int(final_receipt.get("id", -1)) == int(p.get("offline_revision", -2)),
"%s: acknowledged receipt id must match the revision" % tag)
_check(str(p.get("offline_report_key", "")) == "", "%s: claim key must be cleared" % tag)
_check((p.get("claimed_rewards", []) as Array).has("offline_%d" % int(p.get("offline_revision", -1))),
"%s: claim must be recorded once by its revision key" % tag)
_check(shell.get_modal_visible_for_test(), "%s: claim touch must open the report modal" % tag)
var body := shell.get_modal_body_for_test()
_check(body.contains(str(expected_gold)), "%s: report body must show the credited gold" % tag)
_check(not body.contains("[shell."), "%s: offline report locale key missing" % tag)
# 2. Persistence: the save-first claim already wrote the credited state.
var reloaded := ProfileStore.load_profile()
_check(int(reloaded.get("gold", 0)) == expected_gold, "%s: offline claim must persist exactly once" % tag)
_check(not bool(reloaded.get("offline_pending", false)), "%s: persisted book must not be pending" % tag)
_check(int(reloaded.get("pending_offline_gold", 0)) == 0, "%s: persisted report must be empty" % tag)
_record("claim", {"case": tag, "gold": expected_gold, "persisted": reloaded.get("gold"),
"revision": reloaded.get("offline_revision"),
"claim_rect": [claim_rect.position.x, claim_rect.position.y, claim_rect.size.x, claim_rect.size.y],
"visible_rect": [viewport_size.x, viewport_size.y],
"input_method": "Input.parse_input_event(InputEventScreenTouch)"})
# 3. A second real touch on the now-hidden claim path cannot double-pay.
var second := ProfileStore.claim_offline(p)
_check(second == 0, "%s: a repeated claim must pay nothing" % tag)
# 4. REAL ScreenTouch on the accept action closes the report modal.
var accept_center := shell.modal_button_center_for_test("map")
_check(accept_center != Vector2.ZERO, "%s: accept button has no global rect" % tag)
await _tap(accept_center)
await _wait_frames(2)
_check(not shell.get_modal_visible_for_test(), "%s: accept touch must close the modal" % tag)
root_window.remove_child(shell)
shell.free()
await process_frame
func _wait_frames(count: int) -> void:
for _i in range(count):
await process_frame
await RenderingServer.frame_post_draw
func _tap(position: Vector2) -> void:
var down := InputEventScreenTouch.new()
down.index = 11
down.position = position
down.pressed = true
Input.parse_input_event(down)
await process_frame
await process_frame
var up := InputEventScreenTouch.new()
up.index = 11
up.position = position
up.pressed = false
Input.parse_input_event(up)
await process_frame
await process_frame
func _finish() -> void:
var passed := _problems.is_empty()
var report := {
"schema": 1, "kind": "windroost_offline_anchor_touch_test",
"input_method": "Input.parse_input_event(InputEventScreenTouch/ScreenDrag)",
"matrix": {"sizes": ["360x800", "390x844", "430x932"], "locales": LOCALES, "reduced": REDUCED},
"overall": "PASS" if passed else "FAIL", "problems": _problems, "steps": _steps,
}
var dir := ProjectSettings.globalize_path("res://artifacts")
DirAccess.make_dir_recursive_absolute(dir)
var out := FileAccess.open(dir.path_join("offline_anchor_touch_test.json"), FileAccess.WRITE)
if out != null:
out.store_string(JSON.stringify(report, "\t", true))
out.close()
if passed:
print("OFFLINE_TOUCH_PASS %d real touch cases verified" % _steps.size())
else:
print("OFFLINE_TOUCH_FAIL %s" % "; ".join(_problems))
quit(0 if passed else 1)