Skip to content

Commit 6bbed8e

Browse files
authored
Fixup tests, add ruff lint fixes (#459)
1 parent 836da7d commit 6bbed8e

9 files changed

Lines changed: 150 additions & 109 deletions

File tree

src/godot_ai/handlers/editor.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,4 @@ async def game_eval(runtime: DirectRuntime, code: str) -> dict:
346346
Runtime errors in the eval code are not caught — if eval times out,
347347
check ``logs_read(source='game')`` for push_error output.
348348
"""
349-
return await runtime.send_command(
350-
"game_eval", {"code": code}, timeout=15.0
351-
)
349+
return await runtime.send_command("game_eval", {"code": code}, timeout=15.0)

test_project/tests/test_plugin_telemetry.gd

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ const _TENV2 := "DISABLE_TELEMETRY"
3333

3434
var _saved_tenv1: Variant = null
3535
var _saved_tenv2: Variant = null
36-
var _had_telemetry_setting: bool = false
3736
var _saved_telemetry_setting: Variant = null
3837

3938

@@ -45,20 +44,14 @@ func suite_setup(_ctx: Dictionary) -> void:
4544
_saved_tenv1 = OS.get_environment(_TENV1) if OS.has_environment(_TENV1) else null
4645
_saved_tenv2 = OS.get_environment(_TENV2) if OS.has_environment(_TENV2) else null
4746
var es := EditorInterface.get_editor_settings()
48-
_had_telemetry_setting = es.has_setting(McpSettings.SETTING_TELEMETRY_ENABLED)
49-
if _had_telemetry_setting:
47+
if es.has_setting(McpSettings.SETTING_TELEMETRY_ENABLED):
5048
_saved_telemetry_setting = es.get_setting(McpSettings.SETTING_TELEMETRY_ENABLED)
5149

5250

5351
func suite_teardown() -> void:
5452
_restore_tenv(_TENV1, _saved_tenv1)
5553
_restore_tenv(_TENV2, _saved_tenv2)
56-
var es := EditorInterface.get_editor_settings()
57-
if not _had_telemetry_setting:
58-
if es.has_setting(McpSettings.SETTING_TELEMETRY_ENABLED):
59-
es.set_setting(McpSettings.SETTING_TELEMETRY_ENABLED, null)
60-
else:
61-
es.set_setting(McpSettings.SETTING_TELEMETRY_ENABLED, _saved_telemetry_setting)
54+
EditorInterface.get_editor_settings().set_setting(McpSettings.SETTING_TELEMETRY_ENABLED, _saved_telemetry_setting)
6255

6356

6457
func _restore_tenv(name: String, saved: Variant) -> void:

test_project/tests/test_server_lifecycle.gd

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ const _TENV2 := "DISABLE_TELEMETRY"
8989

9090
var _saved_tenv1: Variant = null
9191
var _saved_tenv2: Variant = null
92-
var _had_telemetry_setting: bool = false
9392
var _saved_telemetry_setting: Variant = null
9493

9594

@@ -101,20 +100,14 @@ func suite_setup(_ctx: Dictionary) -> void:
101100
_saved_tenv1 = OS.get_environment(_TENV1) if OS.has_environment(_TENV1) else null
102101
_saved_tenv2 = OS.get_environment(_TENV2) if OS.has_environment(_TENV2) else null
103102
var es := EditorInterface.get_editor_settings()
104-
_had_telemetry_setting = es.has_setting(McpSettings.SETTING_TELEMETRY_ENABLED)
105-
if _had_telemetry_setting:
103+
if es.has_setting(McpSettings.SETTING_TELEMETRY_ENABLED):
106104
_saved_telemetry_setting = es.get_setting(McpSettings.SETTING_TELEMETRY_ENABLED)
107105

108106

109107
func suite_teardown() -> void:
110108
_restore_tenv(_TENV1, _saved_tenv1)
111109
_restore_tenv(_TENV2, _saved_tenv2)
112-
var es := EditorInterface.get_editor_settings()
113-
if not _had_telemetry_setting:
114-
if es.has_setting(McpSettings.SETTING_TELEMETRY_ENABLED):
115-
es.set_setting(McpSettings.SETTING_TELEMETRY_ENABLED, null)
116-
else:
117-
es.set_setting(McpSettings.SETTING_TELEMETRY_ENABLED, _saved_telemetry_setting)
110+
EditorInterface.get_editor_settings().set_setting(McpSettings.SETTING_TELEMETRY_ENABLED, _saved_telemetry_setting)
118111

119112

120113
func _restore_tenv(name: String, saved: Variant) -> void:
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
@tool
2+
extends McpTestSuite
3+
4+
## Tests for McpSettings utility helpers: truthy(), env_truthy(),
5+
## and telemetry_enabled().
6+
7+
const _TENV1 := "GODOT_AI_DISABLE_TELEMETRY"
8+
const _TENV2 := "DISABLE_TELEMETRY"
9+
10+
var _saved_tenv1: Variant = null
11+
var _saved_tenv2: Variant = null
12+
var _saved_telemetry_setting: Variant = null
13+
14+
15+
func suite_name() -> String:
16+
return "settings"
17+
18+
19+
func suite_setup(_ctx: Dictionary) -> void:
20+
_saved_tenv1 = OS.get_environment(_TENV1) if OS.has_environment(_TENV1) else null
21+
_saved_tenv2 = OS.get_environment(_TENV2) if OS.has_environment(_TENV2) else null
22+
var es := EditorInterface.get_editor_settings()
23+
if es.has_setting(McpSettings.SETTING_TELEMETRY_ENABLED):
24+
_saved_telemetry_setting = es.get_setting(McpSettings.SETTING_TELEMETRY_ENABLED)
25+
26+
27+
func suite_teardown() -> void:
28+
_restore_env(_TENV1, _saved_tenv1)
29+
_restore_env(_TENV2, _saved_tenv2)
30+
# NB: If originally unset, _saved_telemetry_setting will be null and this will unset any
31+
# value set by tests. No-op if already unset and then set to null.
32+
EditorInterface.get_editor_settings().set_setting(McpSettings.SETTING_TELEMETRY_ENABLED, _saved_telemetry_setting)
33+
34+
35+
func _restore_env(name: String, saved: Variant) -> void:
36+
if saved == null:
37+
OS.unset_environment(name)
38+
else:
39+
OS.set_environment(name, str(saved))
40+
41+
42+
# ----- truthy -----
43+
44+
func test_truthy_accepts_1() -> void:
45+
assert_true(McpSettings.truthy("1"))
46+
47+
func test_truthy_accepts_true() -> void:
48+
assert_true(McpSettings.truthy("true"))
49+
50+
func test_truthy_accepts_yes() -> void:
51+
assert_true(McpSettings.truthy("yes"))
52+
53+
func test_truthy_accepts_on() -> void:
54+
assert_true(McpSettings.truthy("on"))
55+
56+
func test_truthy_is_case_insensitive() -> void:
57+
assert_true(McpSettings.truthy("TRUE"))
58+
assert_true(McpSettings.truthy("True"))
59+
assert_true(McpSettings.truthy("YES"))
60+
assert_true(McpSettings.truthy("ON"))
61+
62+
func test_truthy_strips_whitespace() -> void:
63+
assert_true(McpSettings.truthy(" 1 "))
64+
assert_true(McpSettings.truthy("\ttrue\n"))
65+
66+
func test_truthy_rejects_empty() -> void:
67+
assert_false(McpSettings.truthy(""))
68+
69+
func test_truthy_rejects_false() -> void:
70+
assert_false(McpSettings.truthy("false"))
71+
72+
func test_truthy_rejects_zero() -> void:
73+
assert_false(McpSettings.truthy("0"))
74+
75+
func test_truthy_rejects_arbitrary_string() -> void:
76+
assert_false(McpSettings.truthy("maybe"))
77+
78+
79+
# ----- env_truthy -----
80+
81+
func test_env_truthy_returns_true_when_var_set_truthy() -> void:
82+
OS.set_environment(_TENV1, "1")
83+
assert_true(McpSettings.env_truthy(_TENV1))
84+
85+
func test_env_truthy_returns_false_when_var_set_falsy() -> void:
86+
OS.set_environment(_TENV1, "false")
87+
assert_false(McpSettings.env_truthy(_TENV1))
88+
89+
func test_env_truthy_returns_false_when_var_absent() -> void:
90+
OS.unset_environment(_TENV1)
91+
assert_false(McpSettings.env_truthy(_TENV1))
92+
93+
94+
# ----- telemetry_enabled -----
95+
96+
func test_telemetry_enabled_returns_false_when_disable_env_set() -> void:
97+
OS.set_environment(_TENV1, "1")
98+
OS.unset_environment(_TENV2)
99+
assert_false(McpSettings.telemetry_enabled())
100+
101+
func test_telemetry_enabled_returns_false_when_alt_env_set() -> void:
102+
OS.unset_environment(_TENV1)
103+
OS.set_environment(_TENV2, "true")
104+
assert_false(McpSettings.telemetry_enabled())
105+
106+
func test_telemetry_enabled_reads_editor_setting_when_no_env() -> void:
107+
OS.unset_environment(_TENV1)
108+
OS.unset_environment(_TENV2)
109+
EditorInterface.get_editor_settings().set_setting(McpSettings.SETTING_TELEMETRY_ENABLED, false)
110+
assert_false(McpSettings.telemetry_enabled())
111+
EditorInterface.get_editor_settings().set_setting(McpSettings.SETTING_TELEMETRY_ENABLED, true)
112+
assert_true(McpSettings.telemetry_enabled())
113+
114+
func test_telemetry_enabled_defaults_true_when_no_env_and_no_setting() -> void:
115+
OS.unset_environment(_TENV1)
116+
OS.unset_environment(_TENV2)
117+
var es := EditorInterface.get_editor_settings()
118+
es.set_setting(McpSettings.SETTING_TELEMETRY_ENABLED, null)
119+
assert_true(McpSettings.telemetry_enabled(), "absent setting must default to enabled")
120+
121+
func test_telemetry_env_overrides_editor_setting() -> void:
122+
OS.set_environment(_TENV1, "1")
123+
EditorInterface.get_editor_settings().set_setting(McpSettings.SETTING_TELEMETRY_ENABLED, true)
124+
assert_false(McpSettings.telemetry_enabled(),
125+
"env var opt-out must win over EditorSetting true")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uid://frat5s1xesgo

test_project/tests/test_telemetry_setting.gd

Lines changed: 0 additions & 66 deletions
This file was deleted.

test_project/tests/test_telemetry_toggle.gd

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,21 @@ const _ENV2 := "DISABLE_TELEMETRY"
1616

1717
var _saved_env1: Variant = null
1818
var _saved_env2: Variant = null
19-
var _had_setting: bool = false
2019
var _saved_setting: Variant = null
2120

2221

2322
func suite_setup(_ctx: Dictionary) -> void:
2423
_saved_env1 = OS.get_environment(_ENV1) if OS.has_environment(_ENV1) else null
2524
_saved_env2 = OS.get_environment(_ENV2) if OS.has_environment(_ENV2) else null
2625
var es := EditorInterface.get_editor_settings()
27-
_had_setting = es.has_setting(McpSettings.SETTING_TELEMETRY_ENABLED)
28-
if _had_setting:
26+
if es.has_setting(McpSettings.SETTING_TELEMETRY_ENABLED):
2927
_saved_setting = es.get_setting(McpSettings.SETTING_TELEMETRY_ENABLED)
3028

3129

3230
func suite_teardown() -> void:
3331
_restore_env(_ENV1, _saved_env1)
3432
_restore_env(_ENV2, _saved_env2)
35-
var es := EditorInterface.get_editor_settings()
36-
if not _had_setting:
37-
if es.has_setting(McpSettings.SETTING_TELEMETRY_ENABLED):
38-
## Setting didn't exist before tests ran — remove it if we added it.
39-
## NB: Passing null to set_setting is the intended way to unset editor settings in Godot 3/4.
40-
es.set_setting(McpSettings.SETTING_TELEMETRY_ENABLED, null)
41-
else:
42-
es.set_setting(McpSettings.SETTING_TELEMETRY_ENABLED, _saved_setting)
33+
EditorInterface.get_editor_settings().set_setting(McpSettings.SETTING_TELEMETRY_ENABLED, _saved_setting)
4334

4435

4536
func _restore_env(name: String, saved: Variant) -> void:

tests/unit/test_game_eval.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,21 @@ def __init__(self, *, eval_result: dict | None = None):
1414
self.calls: list[dict] = []
1515
self._eval_result = eval_result or {"data": {"result": "42", "source": "game"}}
1616

17-
async def send(self, command: str, params: dict | None = None,
18-
session_id: str | None = None, timeout: float = 5.0) -> dict:
19-
self.calls.append({
20-
"command": command, "params": params,
21-
"session_id": session_id, "timeout": timeout,
22-
})
17+
async def send(
18+
self,
19+
command: str,
20+
params: dict | None = None,
21+
session_id: str | None = None,
22+
timeout: float = 5.0,
23+
) -> dict:
24+
self.calls.append(
25+
{
26+
"command": command,
27+
"params": params,
28+
"session_id": session_id,
29+
"timeout": timeout,
30+
}
31+
)
2332
if command == "game_eval":
2433
return self._eval_result
2534
return {"data": {}}
@@ -48,8 +57,5 @@ async def test_game_eval_uses_custom_timeout():
4857
async def test_game_eval_passes_code_verbatim():
4958
client = _StubGameEvalClient()
5059
runtime = DirectRuntime(registry=SessionRegistry(), client=client)
51-
await editor_handlers.game_eval(
52-
runtime,
53-
code="return get_tree().root.name"
54-
)
60+
await editor_handlers.game_eval(runtime, code="return get_tree().root.name")
5561
assert client.calls[-1]["params"]["code"] == "return get_tree().root.name"

tests/unit/test_runtime_handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3385,7 +3385,7 @@ async def send(self, command, params=None, session_id=None, timeout=5.0):
33853385
message=(
33863386
"The 3D viewport is empty because the current scene is 2D "
33873387
"(Node2D root). Options: (a) open a 3D scene, "
3388-
"(b) use source=\"cinematic\" if a Camera3D exists in the scene, "
3388+
'(b) use source="cinematic" if a Camera3D exists in the scene, '
33893389
"(c) call scene_get_hierarchy first to inspect what's available."
33903390
),
33913391
data={

0 commit comments

Comments
 (0)