OverviewFilesCommitsIssuesDocsReleasesPeers

hyperswarm-peer / integration / test_meta_gd.gd

110 lines · gd ref:
extends SceneTree
## Minimal test: meta.set with 3 keys via GDScript UDS client

func _init() -> void:
	_start.call_deferred()

func _start() -> void:
	# Load UDS client
	var uds_script = load("res://scripts/keet_uds_client.gd")
	if uds_script == null:
		print("FAIL: cannot load keet_uds_client.gd")
		quit()
		return

	var uds = Node.new()
	uds.set_script(uds_script)
	uds.set("socket_path", "/tmp/keet-daemon.sock")
	uds.set("auto_connect", false)
	root.add_child(uds)
	await create_timer(0.1).timeout

	# Connect
	uds.try_connect()
	var elapsed := 0.0
	while elapsed < 10.0 and not uds.is_connected:
		await create_timer(0.1).timeout
		elapsed += 0.1
	if not uds.is_connected:
		print("FAIL: not connected")
		quit()
		return
	print("connected to daemon")

	# Create room
	var create_resp = await uds.rpc_async("room.create", {"title": "GD Meta Test"})
	if create_resp.error != null:
		print("FAIL: room.create: %s" % str(create_resp.error))
		quit()
		return
	var room_id: String = str(create_resp.result)
	print("room created: %s" % room_id.left(20))

	# Test 1: Send 3 keys in one meta.set (plain strings, no var_to_str)
	print("\n--- Test 1: 3 keys, plain strings ---")
	var meta1 := {"game:map": "arena", "game:mode": "deathmatch", "game:max_players": "8"}
	print("sending: %s" % JSON.stringify(meta1))
	var set_resp1 = await uds.rpc_async("meta.set", {"roomId": room_id, "metadata": meta1})
	print("set response: %s" % str(set_resp1.result).left(300))

	var list1 = await uds.rpc_async("meta.list", {})
	print("list result: %s" % str(list1.result).left(500))
	if list1.result is Dictionary:
		var rm: Variant = list1.result.get(room_id, null)
		if rm is Dictionary:
			print("room keys: %s" % str(rm.keys()))
			for k in rm.keys():
				print("  %s = %s" % [str(k), str(rm[k])])
		else:
			print("room not found or not dict: %s" % str(rm))

	# Test 2: Send 3 keys with var_to_str values
	print("\n--- Test 2: 3 keys, var_to_str values ---")
	var meta2 := {
		"game:v_map": var_to_str("arena"),
		"game:v_mode": var_to_str("deathmatch"),
		"game:v_max": var_to_str("8")
	}
	print("sending: %s" % JSON.stringify(meta2))
	var set_resp2 = await uds.rpc_async("meta.set", {"roomId": room_id, "metadata": meta2})
	print("set response: %s" % str(set_resp2.result).left(300))

	var list2 = await uds.rpc_async("meta.list", {})
	if list2.result is Dictionary:
		var rm2: Variant = list2.result.get(room_id, null)
		if rm2 is Dictionary:
			print("room keys: %s" % str(rm2.keys()))
			for k in rm2.keys():
				print("  %s = %s" % [str(k), str(rm2[k])])

	# Test 3: Fire 3 individual set calls (callback, not awaited)
	print("\n--- Test 3: 3 individual call_rpc (concurrent) ---")
	var done_count := {"n": 0}
	uds.call_rpc("meta.set", {"roomId": room_id, "metadata": {"game:c1": "one"}}, func(_r, _e):
		done_count["n"] += 1
	)
	uds.call_rpc("meta.set", {"roomId": room_id, "metadata": {"game:c2": "two"}}, func(_r, _e):
		done_count["n"] += 1
	)
	uds.call_rpc("meta.set", {"roomId": room_id, "metadata": {"game:c3": "three"}}, func(_r, _e):
		done_count["n"] += 1
	)
	# Wait for all 3
	elapsed = 0.0
	while elapsed < 10.0 and done_count["n"] < 3:
		await create_timer(0.1).timeout
		elapsed += 0.1
	print("concurrent calls done: %d/3" % done_count["n"])

	var list3 = await uds.rpc_async("meta.list", {})
	if list3.result is Dictionary:
		var rm3: Variant = list3.result.get(room_id, null)
		if rm3 is Dictionary:
			print("room keys: %s" % str(rm3.keys()))
			print("  game:c1 = %s" % str(rm3.get("game:c1", "MISSING")))
			print("  game:c2 = %s" % str(rm3.get("game:c2", "MISSING")))
			print("  game:c3 = %s" % str(rm3.get("game:c3", "MISSING")))

	print("\nDONE")
	quit()