extends SceneTree # ChatUI 输入交互回归测试(无需 GUT) # # 运行方式(Godot 4.6): # "D:\technology\biancheng\Godot_v4.6-stable_win64.exe\Godot_v4.6-stable_win64_console.exe" --headless --path . --script tests/unit/test_chat_ui_input_regressions.gd var _failures: Array[String] = [] func _init() -> void: call_deferred("_run") func _run() -> void: await _test_t_opens_enter_sends_outside_click_hides() if _failures.is_empty(): print("PASS: test_chat_ui_input_regressions") quit(0) return for failure in _failures: push_error(failure) print("FAIL: test_chat_ui_input_regressions (%d)" % _failures.size()) quit(1) func _test_t_opens_enter_sends_outside_click_hides() -> void: var packed_scene: PackedScene = load("res://scenes/ui/ChatUI.tscn") _assert(packed_scene != null, "应能加载 ChatUI.tscn") if packed_scene == null: return var chat_ui = packed_scene.instantiate() root.add_child(chat_ui) await process_frame await process_frame var chat_panel = chat_ui.chat_panel var chat_input = chat_ui.chat_input _assert(not chat_panel.visible, "初始化时聊天框应隐藏") _assert(not chat_ui._is_chat_visible, "初始化时 _is_chat_visible 应为 false") var enter_event := InputEventKey.new() enter_event.pressed = true enter_event.keycode = KEY_ENTER chat_ui._input(enter_event) await process_frame _assert(not chat_panel.visible, "聊天框隐藏时按 Enter 不应唤起聊天框") var t_event := InputEventKey.new() t_event.pressed = true t_event.keycode = KEY_T chat_ui._input(t_event) await create_timer(0.7).timeout _assert(chat_panel.visible, "按 T 后聊天框应显示") _assert(chat_ui._is_chat_visible, "按 T 后 _is_chat_visible 应为 true") _assert(chat_input.has_focus(), "按 T 后输入框应获得焦点") chat_input.text = "hello world" chat_ui._on_chat_input_submitted(chat_input.text) await process_frame _assert(chat_input.text.is_empty(), "输入框有内容时按 Enter 应触发发送并清空输入框") _assert(chat_input.has_focus(), "按 Enter 发送后输入框应自动保持焦点,便于连续输入") # 将鼠标移动到左上角,通常在聊天框外 var viewport := root.get_viewport() if viewport: viewport.warp_mouse(Vector2.ZERO) var mouse_event := InputEventMouseButton.new() mouse_event.button_index = MOUSE_BUTTON_LEFT mouse_event.pressed = true chat_ui._gui_input(mouse_event) await create_timer(0.7).timeout _assert(not chat_ui._is_chat_visible, "点击聊天框外部后 _is_chat_visible 应为 false") _assert(not chat_panel.visible, "点击聊天框外部后聊天框应隐藏") chat_ui.queue_free() await process_frame func _assert(condition: bool, message: String) -> void: if not condition: _failures.append(message)