OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-tps / scenes / test / gate_flatten_room.gd

106 lines · gd ref:
extends SceneTree
## GATE — flattening the WHOLE ROOM visits every map with level history, flattens each
## while it is loaded, reports a map it cannot load, and returns to where you were.
## Stub game + stub maps: no real map change, no daemon.
##
## Run: "$HYPER_TPS_GODOT" --headless --path . --script res://scenes/test/gate_flatten_room.gd

const FR := preload("res://scenes/game/flatten_room.gd")

var ok := true
var _step := 0
var _game: Node = null
var _fr: Node = null
var _report: Array = []
var _got_done := false
var _done_checks := false


class StubPersist:
	extends Node
	var map_id := ""
	var flattened := 0

	func refold() -> Dictionary:
		return {}

	func flatten_map() -> Dictionary:
		flattened += 1
		# Counted on the GAME, which outlives the map (the next change frees this).
		var g: Node = get_parent().get_parent()
		g.flat_count[map_id] = int(g.flat_count.get(map_id, 0)) + 1
		return {"before": 10, "after": 2, "backup": "user://x-%s.json" % map_id}


class StubMap:
	extends Node
	var _persist: Node = null


class StubGame:
	extends Node
	var current_map_path := ""
	var current_map: Node = null
	var visits: Array = []
	var flat_by_map := {}
	var flat_count := {}

	func change_map(path: String, _pos: Vector3) -> void:
		visits.append(path)
		if current_map != null:
			current_map.queue_free()
		current_map = StubMap.new()
		var p := StubPersist.new()
		p.map_id = path.get_file().get_basename()
		p.name = "LevelPersistence"
		current_map._persist = p
		current_map.add_child(p)
		add_child(current_map)
		flat_by_map[p.map_id] = p
		current_map_path = path
		await get_tree().process_frame


func _p(c: bool, m: String) -> void:
	print(("ok   " if c else "FAIL: ") + m)
	if not c:
		ok = false


func _process(_d: float) -> bool:
	_step += 1
	if _step == 1:
		var ids := FR.maps_with_history({
			"map:map_test:world:level:a:op:1": 1, "map:map_test:world:level:a:op:2": 1,
			"map:map_flat:world:level:b:op:9": 1,
			"map:map_dc_terrain:world:level:a:source": 1,     # a source key only: not history
			"map:map_dc_erosion:world:sky:time": 1,           # not level
			"presence:x": 1})
		_p(ids == ["map_flat", "map_test"], "maps_with_history: only maps with level records, sorted (%s)" % str(ids))
		var gs: Node = root.get_node_or_null("GameState")
		if gs != null:
			gs.room_state_loaded = true
		_game = StubGame.new()
		root.add_child(_game)
		_game.change_map("res://scenes/maps/map_test.tscn", Vector3.ZERO)
	elif _step == 3:
		_game.visits.clear()
		_fr = FR.start(_game, ["map_flat", "map_test", "map_nope"])
		_fr.done.connect(func(r: Array) -> void:
			_report = r
			_got_done = true)
	elif (_got_done or _step > 3000) and not _done_checks:
		_p(_got_done, "it finished")
		_done_checks = true
		_p(int(_game.flat_count.get("map_flat", 0)) == 1, "map_flat flattened once (%s)" % str(_game.flat_count))
		_p(_report.any(func(e: Dictionary) -> bool: return String(e.get("map", "")) == "map_test" and int(e.get("after", -1)) == 2),
				"map_test flattened")
		_p(_report.any(func(e: Dictionary) -> bool: return String(e.get("map", "")) == "map_nope" and String(e.get("error", "")) != ""),
				"a map with no scene is reported, not fatal")
		_p(String(_game.current_map_path) == "res://scenes/maps/map_test.tscn", "back on the map it started from (%s)" % _game.current_map_path)
		_p(_game.visits.size() >= 2 and String(_game.visits[0]).ends_with("map_flat.tscn"), "visited map_flat first (%s)" % str(_game.visits))
		print("GATE flatten_room: %s" % ("PASS" if ok else "FAIL"))
		quit(0 if ok else 1)
		return true
	return false